[DRE-commits] [ruby-aruba] 30/98: Add new matcher for a check if command can be found in PATH
Hideki Yamane
henrich at moszumanska.debian.org
Tue Mar 22 12:20:36 UTC 2016
This is an automated email from the git hooks/post-receive script.
henrich pushed a commit to branch debian/sid
in repository ruby-aruba.
commit 5fc54713573c76e9fae5f4d0d720497f3c910e75
Author: Dennis Günnewig <dev at fedux.org>
Date: Sat Jan 2 13:26:32 2016 +0100
Add new matcher for a check if command can be found in PATH
---
History.md | 4 +
.../file/be_a_command_found_in_path.feature | 98 ++++++++++++++++++++++
.../matchers/file/be_a_command_found_in_path.rb | 36 ++++++++
3 files changed, 138 insertions(+)
diff --git a/History.md b/History.md
index 324ce82..22dee6a 100644
--- a/History.md
+++ b/History.md
@@ -489,6 +489,10 @@
# Upcoming un-released versions
+## [v0.12.0](https://github.com/cucumber/aruba/compare/v0.11.2...v0.12.0)
+
+* Add matcher to check if a command can be found in PATH
+
## [v1.0.0](https://github.com/cucumber/aruba/compare/v0.11.0...v1.0.0)
* Support for rubies older than 1.9.3 is discontinued - e.g 1.8.7 and 1.9.2
diff --git a/features/matchers/file/be_a_command_found_in_path.feature b/features/matchers/file/be_a_command_found_in_path.feature
new file mode 100644
index 0000000..86990df
--- /dev/null
+++ b/features/matchers/file/be_a_command_found_in_path.feature
@@ -0,0 +1,98 @@
+Feature: Check if command can be found in PATH
+
+ If you need to check if a given command can be found in path, you can use the
+ `be_an_existing_executable`-matcher.
+
+ ```ruby
+ require 'spec_helper'
+
+ RSpec.describe 'Check if command can be found in PATH', :type => :aruba do
+ let(:file) { 'file.sh' }
+ before(:each) { touch(file) }
+ before(:each) { chmod(0755, file) }
+ before(:each) { prepend_environment_variable('PATH', format('%s:', expand_path('.')) }
+
+ it { expect(file).to be_an_existing_executable }
+ end
+ ```
+
+ Background:
+ Given I use a fixture named "cli-app"
+
+ Scenario: Expect single existing executable file
+ Given a file named "spec/existing_executable_spec.rb" with:
+ """
+ require 'spec_helper'
+
+ RSpec.describe 'Check if command can be found in PATH', :type => :aruba do
+ let(:file) { 'file.sh' }
+
+ before(:each) { touch(file) }
+ before(:each) { chmod(0755, file) }
+ before(:each) { prepend_environment_variable('PATH', format('%s:', expand_path('.'))) }
+
+ it { expect(file).to be_a_command_found_in_path }
+ end
+ """
+ When I run `rspec`
+ Then the specs should all pass
+
+ Scenario: Expect single non-existing executable file
+ Given a file named "spec/existing_executable_spec.rb" with:
+ """
+ require 'spec_helper'
+
+ RSpec.describe 'Check if command can be found in PATH', :type => :aruba do
+ let(:file) { 'file.sh' }
+
+ before(:each) { prepend_environment_variable('PATH', format('%s:', expand_path('.'))) }
+
+ it { expect(file).not_to be_a_command_found_in_path }
+ end
+ """
+ When I run `rspec`
+ Then the specs should all pass
+
+ Scenario: Expect multiple existing executable files
+ Given a file named "spec/existing_executable_spec.rb" with:
+ """
+ require 'spec_helper'
+
+ RSpec.describe 'Check if file exists and is an executable file', :type => :aruba do
+ let(:files) { %w(file1.sh file2.sh) }
+
+ before :each do
+ files.each do |f|
+ touch(f)
+ chmod(0755, f)
+ end
+ end
+
+ before(:each) { prepend_environment_variable('PATH', format('%s:', expand_path('.'))) }
+
+ it { expect(files).to all be_a_command_found_in_path }
+ end
+ """
+ When I run `rspec`
+ Then the specs should all pass
+
+ Scenario: Expect a least one existing executable file
+ Given a file named "spec/existing_executable_spec.rb" with:
+ """
+ require 'spec_helper'
+
+ RSpec.describe 'Check if file exists and is an executable file', :type => :aruba do
+ let(:files) { %w(file1.sh file2.sh) }
+
+ before :each do
+ touch(files.first)
+ chmod(0755, files.first)
+ end
+
+ before(:each) { prepend_environment_variable('PATH', format('%s:', expand_path('.'))) }
+
+ it { expect(files).to include a_command_found_in_path }
+ end
+ """
+ When I run `rspec`
+ Then the specs should all pass
diff --git a/lib/aruba/matchers/file/be_a_command_found_in_path.rb b/lib/aruba/matchers/file/be_a_command_found_in_path.rb
new file mode 100644
index 0000000..180d78d
--- /dev/null
+++ b/lib/aruba/matchers/file/be_a_command_found_in_path.rb
@@ -0,0 +1,36 @@
+require 'rspec/expectations/version'
+
+# @!method be_a_command_found_in_path
+# This matchers checks if <command> can be found in path
+#
+# @return [TrueClass, FalseClass] The result
+#
+# false:
+# * if command was not found in PATH
+# true:
+# * if command can be found in PATH
+#
+# @example Use matcher
+#
+# RSpec.describe do
+# it { expect(cmd).to be_a_command_found_in_path }
+# end
+RSpec::Matchers.define :be_a_command_found_in_path do
+ match do |actual|
+ @actual = Shellwords.split(actual.commandline).first if actual.respond_to? :commandline
+
+ !which(@actual).nil?
+ end
+
+ failure_message do |actual|
+ format(%(expected that command "%s" can be found in PATH "#{ENV['PATH']}".), actual)
+ end
+
+ failure_message_when_negated do |actual|
+ format(%(expected that command "%s" cannot be found in PATH "#{ENV['PATH']}".), actual)
+ end
+end
+
+if RSpec::Expectations::Version::STRING >= '3.0'
+ RSpec::Matchers.alias_matcher :a_command_found_in_path, :be_a_command_found_in_path
+end
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-ruby-extras/ruby-aruba.git
More information about the Pkg-ruby-extras-commits
mailing list