cli_tester 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 96b04015e8e4ba1f7f601ce673e7992c4e0e0921
4
+ data.tar.gz: 6734dbd213f3ff50f53c1cb3d17e3c79f8fb0775
5
+ SHA512:
6
+ metadata.gz: 721c3c34801cc2b0b7862cfc78c510bec00bd228dcd3456b7615bddf4eaca4107067c4b3ffbcf3a8ded3e3be2e2651080ff3c1c19525e9463a68b5699ac02010
7
+ data.tar.gz: 5c92c09ffe28f8ea36df8de8e40ed20f596c5d78e79db91a946d917c582e28237327885f8d961530fe2a1767abf2daa9c669a15ea043c78607ee6e39e5b01828
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ *.gem
2
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
data/MIT-LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2014 Cornelius Schumacher <schumacher@kde.org>
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,12 @@
1
+ CliTester is a simple set of helpers for testing command line interfaces with
2
+ RSpec. It provides a method to run command line applications and a way to check
3
+ the output and exit codes.
4
+
5
+ Use it in combination with
6
+ [GivenFilesystem](https://github.com/cornelius/given_filesystem) for convenient
7
+ testing of command line applications.
8
+
9
+ CliTester is licensed under the MIT license.
10
+
11
+ If you have questions or comments, please get in touch with Cornelius Schumacher
12
+ <schumacher@kde.org>.
data/bin/cli_tester ADDED
@@ -0,0 +1,22 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ if ARGV == ["help"] || ARGV == [] || ARGV == ["--help"]
4
+ puts "Commands:"
5
+ exit 0
6
+ elsif ARGV == ["help", "list"]
7
+ puts "inqlude list"
8
+ exit 0
9
+ elsif ARGV == ["--xxx"]
10
+ STDERR.puts "Unknown option"
11
+ exit 0
12
+ elsif ARGV == ["list"]
13
+ puts "one"
14
+ puts "two"
15
+ exit 0
16
+ elsif ARGV == ["ls"]
17
+ puts Dir.entries(Dir.pwd).sort
18
+ exit 0
19
+ else
20
+ STDERR.puts "Unrecognized arguments: #{ARGV}"
21
+ exit 1
22
+ end
data/bin/the_other_one ADDED
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ puts "I'm the other one"
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path("../lib/version", __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "cli_tester"
6
+ s.version = CliTester::VERSION
7
+ s.license = 'MIT'
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ['Cornelius Schumacher']
10
+ s.email = ['schumacher@kde.org']
11
+ s.homepage = "http://github.com/cornelius/cli_tester"
12
+ s.summary = "RSpec helpers for testing command line interfaces"
13
+ s.description = "CliTester provides a set of RSpec helpers to test command line interfaces by running commands and checking output and exit codes."
14
+
15
+ s.required_rubygems_version = ">= 1.3.6"
16
+ s.rubyforge_project = "cli_tester"
17
+
18
+ s.add_dependency "cheetah"
19
+
20
+ s.add_development_dependency "rspec", "~>3"
21
+ s.add_development_dependency "given_filesystem"
22
+
23
+ s.files = `git ls-files`.split("\n")
24
+ s.require_path = 'lib'
25
+ end
data/lib/cli_tester.rb ADDED
@@ -0,0 +1,135 @@
1
+ # Copyright (c) 2015 Cornelius Schumacher <schumacher@kde.org>
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+
21
+ require "cheetah"
22
+
23
+ module CliTester
24
+ def self.included(example_group)
25
+ example_group.extend(self)
26
+ end
27
+
28
+ class CommandResult
29
+ attr_accessor :stdout, :stderr, :exit_code, :cmd, :error
30
+ end
31
+
32
+ def run_command(cmd: nil, args: nil, working_directory: nil)
33
+ if cmd
34
+ path = "bin/" + cmd
35
+ else
36
+ path = "bin/" + File.basename(Dir.pwd)
37
+ end
38
+ if !File.exist?(path)
39
+ executable = [cmd]
40
+ else
41
+ executable = [File.expand_path(path)]
42
+ end
43
+
44
+ if args
45
+ executable += args
46
+ end
47
+
48
+ if working_directory
49
+ current_working_directory = Dir.pwd
50
+ Dir.chdir(working_directory)
51
+ end
52
+
53
+ result = CommandResult.new
54
+ result.cmd = executable
55
+ begin
56
+ o, e = Cheetah.run(executable, stdout: :capture, stderr: :capture )
57
+ result.exit_code = 0
58
+ result.stdout = o
59
+ result.stderr = e
60
+ rescue Cheetah::ExecutionFailed => e
61
+ result.error = e
62
+ result.exit_code = e.status.exitstatus
63
+ result.stdout = e.stdout
64
+ result.stderr = e.stderr
65
+ end
66
+
67
+ if working_directory
68
+ Dir.chdir(current_working_directory)
69
+ end
70
+
71
+ result
72
+ end
73
+ end
74
+
75
+ RSpec::Matchers.define :exit_with_success do |expected_stdout, expected_stderr|
76
+ match do |result|
77
+ return false if result.exit_code != 0
78
+ if expected_stderr
79
+ if expected_stderr.is_a?(Regexp)
80
+ return false if !expected_stderr.match(result.stderr)
81
+ else
82
+ return false if expected_stderr != result.stderr
83
+ end
84
+ else
85
+ return false if !result.stderr.empty?
86
+ end
87
+ if expected_stdout.is_a?(Regexp)
88
+ expected_stdout.match(result.stdout)
89
+ else
90
+ expected_stdout == result.stdout
91
+ end
92
+ end
93
+
94
+ failure_message do |result|
95
+ message = "ran #{result.cmd}\n"
96
+ if result.error
97
+ message += "error message: #{result.error}\n"
98
+ end
99
+ if result.exit_code != 0
100
+ message += "expected exit code to be zero (was #{result.exit_code})\n"
101
+ end
102
+ if expected_stderr
103
+ if expected_stderr.is_a?(Regexp)
104
+ message += "stderr didn't match #{expected_stderr.inspect}"
105
+ message += " (was '#{Regexp.escape(result.stderr)}')"
106
+ else
107
+ if result.stderr != expected_stderr
108
+ message += "expected stderr to be '#{Regexp.escape(expected_stderr)}'"
109
+ message += " (was '#{Regexp.escape(result.stderr)}')"
110
+ message += "\n\nDiff of stderr:"
111
+ differ = RSpec::Support::Differ.new(color: true)
112
+ message += differ.diff(result.stderr, expected_stderr)
113
+ end
114
+ end
115
+ else
116
+ if !result.stderr.empty?
117
+ message += "expected stderr to be empty"
118
+ message += " (was '#{Regexp.escape(result.stderr)}')\n"
119
+ end
120
+ end
121
+ if expected_stdout.is_a?(Regexp)
122
+ message += "stdout didn't match #{expected_stdout.inspect}"
123
+ message += " (was '#{Regexp.escape(result.stdout)}')"
124
+ else
125
+ if result.stdout != expected_stdout
126
+ message += "expected stdout to be '#{Regexp.escape(expected_stdout)}'"
127
+ message += " (was '#{Regexp.escape(result.stdout)}')"
128
+ message += "\n\nDiff of stdout:"
129
+ differ = RSpec::Support::Differ.new(color: true)
130
+ message += differ.diff(result.stdout, expected_stdout)
131
+ end
132
+ end
133
+ message
134
+ end
135
+ end
data/lib/version.rb ADDED
@@ -0,0 +1,3 @@
1
+ module CliTester
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,61 @@
1
+ require_relative "spec_helper"
2
+
3
+ include CliTester
4
+ include GivenFilesystemSpecHelpers
5
+
6
+ describe "Command line interface" do
7
+ describe "help" do
8
+ it "shows help when run with no args" do
9
+ expect(run_command).to exit_with_success(/Commands:/)
10
+ end
11
+
12
+ it "shows help when run with help command" do
13
+ expect(run_command(args: ["help"])).to exit_with_success(/Commands:/)
14
+ end
15
+
16
+ it "shows help when run with --help option" do
17
+ expect(run_command(args: ["--help"])).to exit_with_success(/Commands:/)
18
+ end
19
+
20
+ it "shows help for command" do
21
+ expect(run_command(args: ["help", "list"])).to exit_with_success(/inqlude list/)
22
+ end
23
+ end
24
+
25
+ describe "errors" do
26
+ it "fails with unknown option" do
27
+ expect(run_command(args: ["--xxx"])).to exit_with_success("", /Unknown/)
28
+ end
29
+ end
30
+
31
+ describe "list" do
32
+ it "lists libraries" do
33
+ expect(run_command(args: ["list"])).to exit_with_success("one\ntwo\n")
34
+ end
35
+ end
36
+ end
37
+
38
+ describe "options" do
39
+ use_given_filesystem
40
+
41
+ it "runs local cmd" do
42
+ result = run_command(cmd: "the_other_one")
43
+ expect(result).to exit_with_success("I'm the other one\n")
44
+ end
45
+
46
+ it "runs global cmd in specified working directory" do
47
+ dir = given_directory do
48
+ given_dummy_file("hello")
49
+ end
50
+ result = run_command(cmd: "ls", working_directory: dir)
51
+ expect(result).to exit_with_success("hello\n")
52
+ end
53
+
54
+ it "runs local cmd in specified working directory" do
55
+ dir = given_directory do
56
+ given_dummy_file("hello")
57
+ end
58
+ result = run_command(args: ["ls"], working_directory: dir)
59
+ expect(result).to exit_with_success(".\n..\nhello\n")
60
+ end
61
+ end
@@ -0,0 +1,3 @@
1
+ require 'given_filesystem/spec_helpers'
2
+
3
+ require_relative '../lib/cli_tester'
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cli_tester
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Cornelius Schumacher
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-06-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: cheetah
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: given_filesystem
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: CliTester provides a set of RSpec helpers to test command line interfaces
56
+ by running commands and checking output and exit codes.
57
+ email:
58
+ - schumacher@kde.org
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - .gitignore
64
+ - .rspec
65
+ - Gemfile
66
+ - MIT-LICENSE
67
+ - README.md
68
+ - bin/cli_tester
69
+ - bin/the_other_one
70
+ - cli_tester.gemspec
71
+ - lib/cli_tester.rb
72
+ - lib/version.rb
73
+ - spec/cli_tester_spec.rb
74
+ - spec/spec_helper.rb
75
+ homepage: http://github.com/cornelius/cli_tester
76
+ licenses:
77
+ - MIT
78
+ metadata: {}
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - '>='
91
+ - !ruby/object:Gem::Version
92
+ version: 1.3.6
93
+ requirements: []
94
+ rubyforge_project: cli_tester
95
+ rubygems_version: 2.0.3
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: RSpec helpers for testing command line interfaces
99
+ test_files: []