rspec-shell_command 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 54297361d1210fa38749606c602fe876824c6f64
4
+ data.tar.gz: 140a1130b2ff8d625482fb8be917d49b49d2832a
5
+ SHA512:
6
+ metadata.gz: 817f0cf1d2019695c447f0948e44dd6bb5a81a4517289bd1cb7ef3daba99c64cee7bd0f534d385c7ae7d8177bec9aaf6ba9c213e991dd9d606429580daf92589
7
+ data.tar.gz: bd5e483707d092ce8dae401965b1ce122942f3bc3aa064e77b94b1c318969d56c92622439ee7db658881eb4683326f0e0b2fee27bfb11f6847e99756dfc757e1
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /vendor/bundle
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,3 @@
1
+ Style/SignalException:
2
+ Exclude:
3
+ - spec/**/*_spec.rb
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rspec-shell_command.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 autopp
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,86 @@
1
+ # rspec-shell_command
2
+ ![Build Status](https://circleci.com/gh/autopp/rspec-shell_command.svg?style=shield&circle-token=afa2f781f5b99c17e1a41dc10439a2c94eb9c43d)
3
+
4
+ The RSpec extention for writing a behavior of external command.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'rspec-shell_command'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install rspec-shell_command
21
+
22
+ ## Usage
23
+
24
+ ```ruby
25
+ require 'rspec/shell_command'
26
+
27
+ describe '`echo hello; echo goodbye >&2`' do
28
+ # Enables DSL
29
+ include RSpec::ShellCommand::DSL
30
+
31
+ # Check whether success or fail
32
+ it 'successes' do
33
+ expect(`echo hello; echo goodbye >&2`).to success
34
+ end
35
+
36
+ # Check the status code
37
+ it 'exit with status code 0' do
38
+ expect(`echo hello; echo goodbye >&2`).to exit_with(0)
39
+ end
40
+
41
+ # Check standard output
42
+ # (The output matcher behaves like the builtin output)
43
+ it 'output "hello\n" to stdout' do
44
+ expect(`echo hello; echo goodbye >&2`).to output("hello\n").to_stdout
45
+ end
46
+
47
+ # Check standard error
48
+ it 'output a some string to stderr' do
49
+ expect(`echo hello; echo goodbye >&2`).to output.to_stderr
50
+ end
51
+ end
52
+
53
+ # Composing matcher is supported!
54
+ describe '`echo hello; echo goodbye >&2; exit 2`' do
55
+ include RSpec::ShellCommand::DSL
56
+
57
+ subject { `echo hello; echo goodbye >&2; exit 2` }
58
+
59
+ # Check the status code using a composing matcher
60
+ it { is_expected.to exit_with(a_value_between(1, 3)) }
61
+
62
+ # Check standard output using a Regexp
63
+ it { is_expected.to output(/ell/).to_stdout }
64
+
65
+ # Check standard error using a composing matcher
66
+ it { is_expected.to output(a_string_starting_with('good')).to_stderr }
67
+ end
68
+ ```
69
+
70
+ NOTE: When including `RSpec::ShellCommand::DSL`, a backquote operator returns a wrapper object of given command, instead of the standard output.
71
+
72
+ If you need to use `RSpec::ShellCommand::DSL` at all examples, you can write as bellow at your `spec/spec_helper.rb`:
73
+
74
+ ```ruby
75
+ require 'rspec/shell_command'
76
+
77
+ RSpec.configure do |c|
78
+ c.include RSpec::ShellCommand::DSL
79
+ end
80
+ ```
81
+
82
+ ## Author
83
+ [@AuToPP](https://twitter.com/AuToPP)
84
+
85
+ ## LICENSE
86
+ [MIT](./LICENSE.txt)
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -0,0 +1,16 @@
1
+ dependencies:
2
+ pre:
3
+ - gem install bundler
4
+
5
+ test:
6
+ post:
7
+ - bundle exec rubocop -D
8
+
9
+ deployment:
10
+ rubygems:
11
+ branch: master
12
+ commands:
13
+ - git config --local user.name "autopp"
14
+ - git config --local user.email "autopp@gmail.com"
15
+ - 'echo ":rubygems_api_key: ${RUBYGEMS_API_KEY}" > ~/.gem/credentials && chmod 0600 ~/.gem/credentials'
16
+ - bundle exec rake release
@@ -0,0 +1,50 @@
1
+ require 'rspec/shell_command/version'
2
+ require 'open3'
3
+
4
+ module RSpec
5
+ # Wrapper of external command
6
+ #
7
+ # @author autopp <autopp@gmail.com>
8
+ #
9
+ class ShellCommand
10
+ # @!attribute [r] stdout
11
+ # @return [String,nil]
12
+ # Standard output if given command is already executed, otherwise nil
13
+ attr_reader :stdout
14
+
15
+ # @!attribute [r] stderr
16
+ # @return [String]
17
+ # Standard error if given command is already executed, otherwise nil
18
+ attr_reader :stderr
19
+
20
+ # @!attribute [r] status
21
+ # @return [Process::Status]
22
+ # Status of given command if already executed, otherwise nil
23
+ attr_reader :status
24
+
25
+ #
26
+ # @param [String] wrapped command string
27
+ #
28
+ def initialize(cmd)
29
+ @cmd = cmd.to_s
30
+ @stdout = @stderr = @status = nil
31
+ end
32
+
33
+ def execute
34
+ execute! unless executed?
35
+ self
36
+ end
37
+
38
+ def execute!
39
+ @stdout, @stderr, @status = Open3.capture3(@cmd)
40
+ self
41
+ end
42
+
43
+ def executed?
44
+ !@status.nil?
45
+ end
46
+ end
47
+ end
48
+
49
+ require 'rspec/shell_command/matchers'
50
+ require 'rspec/shell_command/dsl'
@@ -0,0 +1,17 @@
1
+ module RSpec
2
+ class ShellCommand
3
+ # The module for using rspec-shell_command's DSL in a spec
4
+ #
5
+ # @author autopp <autopp@gmail.com>
6
+ #
7
+ module DSL
8
+ def self.included(cls)
9
+ cls.include RSpec::ShellCommand::Matchers
10
+ end
11
+
12
+ def `(other)
13
+ RSpec::ShellCommand.new(other)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,28 @@
1
+ module RSpec
2
+ class ShellCommand
3
+ # Provide matcher methods
4
+ #
5
+ # @author autopp <autopp@gmail.com>
6
+ #
7
+ module Matchers
8
+ def success
9
+ Success.new
10
+ end
11
+
12
+ def exit_with(status)
13
+ ExitWith.new(status)
14
+ end
15
+
16
+ def output(*args, &block)
17
+ orig = super
18
+ expected = args.first
19
+ Output.new(expected, orig)
20
+ end
21
+ end
22
+ end
23
+ end
24
+
25
+ require 'rspec/shell_command/matchers/base'
26
+ require 'rspec/shell_command/matchers/success'
27
+ require 'rspec/shell_command/matchers/exit_with'
28
+ require 'rspec/shell_command/matchers/output'
@@ -0,0 +1,42 @@
1
+ module RSpec
2
+ class ShellCommand
3
+ module Matchers
4
+ # Base class for a matcher class
5
+ #
6
+ # NOTE: This is a internal class
7
+ #
8
+ # @author autopp <autopp@gmail.com>
9
+ #
10
+ class Base
11
+ def matches?(actual)
12
+ unless actual.is_a?(RSpec::ShellCommand)
13
+ raise TypeError,
14
+ "expected RSpec::ShellCommand, but got #{actual.inspect}"
15
+ end
16
+ @actual = actual
17
+ perform_match(actual.execute)
18
+ end
19
+
20
+ def actual_status
21
+ @actual.status
22
+ end
23
+
24
+ def actual_status_message
25
+ if actual_status.exited?
26
+ "exited with status #{actual_status.exitstatus}"
27
+ elsif actual_status.signaled?
28
+ "terminated by signal #{actual_status.termsig}"
29
+ else
30
+ "been unknown status: #{@actual.inspect}"
31
+ end
32
+ end
33
+
34
+ def make_expected_and_got_message(expected, got)
35
+ "\n"\
36
+ "expected: #{expected}\n"\
37
+ " got: #{got}"
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,42 @@
1
+ module RSpec
2
+ class ShellCommand
3
+ module Matchers
4
+ # Provide implementation of exit_with matcher
5
+ #
6
+ # Check whether given command exit with expected status
7
+ #
8
+ # @author autopp <autopp@gmail.com>
9
+ #
10
+ class ExitWith < Base
11
+ include RSpec::Matchers::Composable
12
+
13
+ def initialize(status)
14
+ @status = status
15
+ end
16
+
17
+ def perform_match(actual)
18
+ actual_status = actual.status
19
+ values_match?(@status, actual_status.exitstatus)
20
+ end
21
+
22
+ def failure_message
23
+ make_expected_and_got_message(
24
+ "exited with status #{@status}",
25
+ actual_status_message
26
+ )
27
+ end
28
+
29
+ def failure_message_when_negated
30
+ make_expected_and_got_message(
31
+ "not exited with status #{@status}",
32
+ actual_status_message
33
+ )
34
+ end
35
+
36
+ def description
37
+ "exit with #{description_of(@status)}"
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,87 @@
1
+ module RSpec
2
+ class ShellCommand
3
+ module Matchers
4
+ # Provide implementation of out matcher
5
+ #
6
+ # Check whether given command output expected string or pattern
7
+ # to stdout or stderr
8
+ #
9
+ # @author autopp <autopp@gmail.com>
10
+ #
11
+ class Output < Base
12
+ include RSpec::Matchers::Composable
13
+
14
+ def initialize(expected, orig)
15
+ @expected = expected
16
+ @orig = orig
17
+ end
18
+
19
+ def matches?(actual)
20
+ @actual_is_command = actual.is_a?(ShellCommand)
21
+ return @orig.matches?(actual) unless @actual_is_command
22
+
23
+ @actual = actual.execute
24
+
25
+ return false unless @stream == :stdout || @stream == :stderr
26
+
27
+ if @expected
28
+ values_match?(@expected, actual_output)
29
+ else
30
+ !actual_output.empty?
31
+ end
32
+ end
33
+
34
+ def failure_message
35
+ if @actual_is_command
36
+ "expect command to #{description}"
37
+ else
38
+ @orig.failure_message
39
+ end
40
+ end
41
+
42
+ def failure_message_when_negated
43
+ if @actual_is_command
44
+ "expect command to not #{description}"
45
+ else
46
+ @orig.failure_message_when_negated
47
+ end
48
+ end
49
+
50
+ def supports_block_expectations?
51
+ true
52
+ end
53
+
54
+ def to_stdout
55
+ @stream = :stdout
56
+ @orig.to_stdout
57
+ self
58
+ end
59
+
60
+ def to_stderr
61
+ @stream = :stderr
62
+ @orig.to_stderr
63
+ self
64
+ end
65
+
66
+ def description
67
+ if @command
68
+ "output #{description_of(@expected)} to #{@stream}"
69
+ else
70
+ "output to #{@stream}"
71
+ end
72
+ end
73
+
74
+ private
75
+
76
+ def actual_output
77
+ case @stream
78
+ when :stdout
79
+ @actual.stdout
80
+ when :stderr
81
+ @actual.stderr
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,29 @@
1
+ module RSpec
2
+ class ShellCommand
3
+ module Matchers
4
+ # Provide implementation of success matcher
5
+ #
6
+ # Check whether given command successed
7
+ #
8
+ # @author autopp <autopp@gmail.com>
9
+ #
10
+ class Success < Base
11
+ def perform_match(actual)
12
+ actual.status.success?
13
+ end
14
+
15
+ def failure_message
16
+ "expect command have to successed, but #{actual_status_message}"
17
+ end
18
+
19
+ def failure_message_when_negated
20
+ 'expect command have not to successed, but successed'
21
+ end
22
+
23
+ def description
24
+ 'success'
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,5 @@
1
+ module RSpec
2
+ class ShellCommand
3
+ VERSION = '0.1.0'.freeze
4
+ end
5
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rspec/shell_command/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'rspec-shell_command'
8
+ spec.version = RSpec::ShellCommand::VERSION
9
+ spec.authors = ['autopp']
10
+ spec.email = ['autopp.inc@gmail.com']
11
+
12
+ spec.summary = 'The RSpec extention for describe behavier of external command'
13
+ spec.description = spec.summary
14
+ spec.homepage = 'https://github.com/autopp/rspec-shell_command'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = 'exe'
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ['lib']
22
+
23
+ spec.add_runtime_dependency 'rspec', '~> 3.0'
24
+ spec.add_development_dependency 'bundler', '~> 1.11'
25
+ spec.add_development_dependency 'rake', '~> 10.0'
26
+ spec.add_development_dependency 'rubocop'
27
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rspec-shell_command
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - autopp
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-08-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.11'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.11'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: The RSpec extention for describe behavier of external command
70
+ email:
71
+ - autopp.inc@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".rubocop.yml"
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - circle.yml
84
+ - lib/rspec/shell_command.rb
85
+ - lib/rspec/shell_command/dsl.rb
86
+ - lib/rspec/shell_command/matchers.rb
87
+ - lib/rspec/shell_command/matchers/base.rb
88
+ - lib/rspec/shell_command/matchers/exit_with.rb
89
+ - lib/rspec/shell_command/matchers/output.rb
90
+ - lib/rspec/shell_command/matchers/success.rb
91
+ - lib/rspec/shell_command/version.rb
92
+ - rspec-shell_command.gemspec
93
+ homepage: https://github.com/autopp/rspec-shell_command
94
+ licenses: []
95
+ metadata: {}
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 2.4.5.1
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: The RSpec extention for describe behavier of external command
116
+ test_files: []