From 5e6957492d519d6ef9eeffab8d9a6de6a8a492bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dennis=20G=C3=BCnnewig?= Date: Tue, 28 Apr 2015 11:52:39 +0200 Subject: [PATCH 1/9] Make aruba-api creation reusable --- spec/aruba/api_spec.rb | 32 +-------------------------- spec/support/shared_contexts/aruba.rb | 32 +++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 31 deletions(-) create mode 100644 spec/support/shared_contexts/aruba.rb diff --git a/spec/aruba/api_spec.rb b/spec/aruba/api_spec.rb index 21bd08ade..bfdc2287b 100644 --- a/spec/aruba/api_spec.rb +++ b/spec/aruba/api_spec.rb @@ -2,37 +2,7 @@ require 'securerandom' describe Aruba::Api do - - def random_string(options = {}) - options[:prefix].to_s + SecureRandom.hex + options[:suffix].to_s - end - - before(:each) do - klass = Class.new { - include Aruba::Api - - def set_tag(tag_name, value) - self.instance_variable_set "@#{tag_name}", value - end - - def announce_or_puts(*args) - p caller[0..2] - end - } - @aruba = klass.new - - @file_name = "test.txt" - @file_size = 256 - @file_path = File.join(@aruba.current_directory, @file_name) - - (@aruba.dirs.length - 1).times do |depth| #Ensure all parent dirs exists - dir = File.join(*@aruba.dirs[0..depth]) - Dir.mkdir(dir) unless File.exist?(dir) - end - raise "We must work with relative paths, everything else is dangerous" if ?/ == @aruba.current_directory[0] - FileUtils.rm_rf(@aruba.current_directory) - Dir.mkdir(@aruba.current_directory) - end + include_context 'uses aruba API' describe 'current_directory' do it "should return the current dir as 'tmp/aruba'" do diff --git a/spec/support/shared_contexts/aruba.rb b/spec/support/shared_contexts/aruba.rb new file mode 100644 index 000000000..bbd3fd95d --- /dev/null +++ b/spec/support/shared_contexts/aruba.rb @@ -0,0 +1,32 @@ +RSpec.shared_context 'uses aruba API' do + def random_string(options = {}) + options[:prefix].to_s + SecureRandom.hex + options[:suffix].to_s + end + + before(:each) do + klass = Class.new { + include Aruba::Api + + def set_tag(tag_name, value) + self.instance_variable_set "@#{tag_name}", value + end + + def announce_or_puts(*args) + p caller[0..2] + end + } + @aruba = klass.new + + @file_name = "test.txt" + @file_size = 256 + @file_path = File.join(@aruba.current_directory, @file_name) + + (@aruba.dirs.length - 1).times do |depth| #Ensure all parent dirs exists + dir = File.join(*@aruba.dirs[0..depth]) + Dir.mkdir(dir) unless File.exist?(dir) + end + raise "We must work with relative paths, everything else is dangerous" if ?/ == @aruba.current_directory[0] + FileUtils.rm_rf(@aruba.current_directory) + Dir.mkdir(@aruba.current_directory) + end +end From f855a29a1f3c9cc4f1d33de1661e4b2694c717a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dennis=20G=C3=BCnnewig?= Date: Tue, 28 Apr 2015 11:53:05 +0200 Subject: [PATCH 2/9] Added tests for matcher --- spec/aruba/matchers/file_spec.rb | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 spec/aruba/matchers/file_spec.rb diff --git a/spec/aruba/matchers/file_spec.rb b/spec/aruba/matchers/file_spec.rb new file mode 100644 index 000000000..0fb1cf6df --- /dev/null +++ b/spec/aruba/matchers/file_spec.rb @@ -0,0 +1,23 @@ +require 'spec_helper' + +RSpec.describe 'File Matchers' do + include_context 'uses aruba API' + + def absolute_path(*args) + @aruba.absolute_path(*args) + end + + describe 'to_be_exsting_file' do + context 'when file exists' do + before :each do + File.write(@file_path, '') + end + + it { expect(@file_name).to be_existing_file } + end + + context 'when file does not exist' do + it { expect(@file_name).not_to be_existing_file } + end + end +end From 08005a77fad38d01fc4d6082d3f438c067bb0eb1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dennis=20G=C3=BCnnewig?= Date: Tue, 28 Apr 2015 11:53:14 +0200 Subject: [PATCH 3/9] Added implementation for matcher --- lib/aruba/matchers/file.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/lib/aruba/matchers/file.rb b/lib/aruba/matchers/file.rb index 95ccd1dbb..602394c48 100644 --- a/lib/aruba/matchers/file.rb +++ b/lib/aruba/matchers/file.rb @@ -15,3 +15,17 @@ format("expected that file \"%s\" differs from file \"%s\".", actual, expected) end end + +RSpec::Matchers.define :be_existing_file do |_| + match do |actual| + File.file?(absolute_path(actual)) + end + + failure_message do |actual| + format("expected that file \"%s\" exists", actual) + end + + failure_message_when_negated do |actual| + format("expected that file \"%s\" does not exist", actual) + end +end From a006632d61637c232a65dcf2d89ac7a8b8fb701e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dennis=20G=C3=BCnnewig?= Date: Tue, 28 Apr 2015 14:02:48 +0200 Subject: [PATCH 4/9] Added test for file content matcher --- spec/aruba/matchers/file_spec.rb | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/spec/aruba/matchers/file_spec.rb b/spec/aruba/matchers/file_spec.rb index 0fb1cf6df..11087f15a 100644 --- a/spec/aruba/matchers/file_spec.rb +++ b/spec/aruba/matchers/file_spec.rb @@ -20,4 +20,32 @@ def absolute_path(*args) it { expect(@file_name).not_to be_existing_file } end end + + describe 'to_have_content' do + context 'when file exists' do + before :each do + File.write(@file_path, 'aba') + end + + context 'and file content is exactly equal string ' do + it { expect(@file_name).to have_content('aba') } + end + + context 'and file content contains string' do + it { expect(@file_name).to have_content(/b/) } + end + + context 'and file content is not exactly equal string' do + it { expect(@file_name).not_to have_content('c') } + end + + context 'and file content not contains string' do + it { expect(@file_name).not_to have_content(/c/) } + end + end + + context 'when file does not exist' do + it { expect(@file_name).not_to have_content('a') } + end + end end From c0440edffe8f5de3d95998415e3ed72c20c2927e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dennis=20G=C3=BCnnewig?= Date: Tue, 28 Apr 2015 14:25:14 +0200 Subject: [PATCH 5/9] Added implementation for file matcher --- lib/aruba/matchers/file.rb | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/lib/aruba/matchers/file.rb b/lib/aruba/matchers/file.rb index 602394c48..88ceddad9 100644 --- a/lib/aruba/matchers/file.rb +++ b/lib/aruba/matchers/file.rb @@ -29,3 +29,28 @@ format("expected that file \"%s\" does not exist", actual) end end + +RSpec::Matchers.define :have_content do |expected| + match do |actual| + path = absolute_path(actual) + + next false unless File.file? path + + content = File.read(path).chomp + next expected === content if expected.is_a? Regexp + + content == expected.chomp + end + + failure_message do |actual| + next format("expected that file \"%s\" contains:\n%s", actual, expected) if expected.is_a? Regexp + + format("expected that file \"%s\" contains exactly:\n%s", actual, expected) + end + + failure_message_when_negated do |actual| + next format("expected that file \"%s\" does not contain:\n%s", actual, expected) if expected.is_a? Regexp + + format("expected that file \"%s\" does not contains exactly:\n%s", actual, expected) + end +end From ad0ddaa48f22e17e0ebe7a5192809bc0d96f3271 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dennis=20G=C3=BCnnewig?= Date: Tue, 28 Apr 2015 14:38:33 +0200 Subject: [PATCH 6/9] Remove private flag to add this matcher to the public api --- lib/aruba/matchers/file.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/aruba/matchers/file.rb b/lib/aruba/matchers/file.rb index 88ceddad9..0cb797976 100644 --- a/lib/aruba/matchers/file.rb +++ b/lib/aruba/matchers/file.rb @@ -1,4 +1,3 @@ -# @private RSpec::Matchers.define :have_same_file_content_like do |expected| match do |actual| FileUtils.compare_file( From 0eff4ccba8ccfcb115549605758a0bf4047fddce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dennis=20G=C3=BCnnewig?= Date: Tue, 28 Apr 2015 15:03:02 +0200 Subject: [PATCH 7/9] Added documentation --- lib/aruba/matchers/file.rb | 62 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/lib/aruba/matchers/file.rb b/lib/aruba/matchers/file.rb index 0cb797976..50a1f61e6 100644 --- a/lib/aruba/matchers/file.rb +++ b/lib/aruba/matchers/file.rb @@ -1,3 +1,22 @@ +# @!method have_same_file_content_like(file_name) +# This matchers checks if has the same content like +# +# @param [String] file_name +# The name of the file which should be compared with the file in the +# `expect()`-call. +# +# @return [TrueClass, FalseClass] The result +# +# false: +# * if file1 is not equal file2 +# true: +# * if file1 is equal file2 +# +# @example Use matcher +# +# RSpec.describe do +# it { expect(file1).to have_same_file_content_like(file2) } +# end RSpec::Matchers.define :have_same_file_content_like do |expected| match do |actual| FileUtils.compare_file( @@ -15,6 +34,21 @@ end end +# @!method be_existing_file +# This matchers checks if exists in filesystem +# +# @return [TrueClass, FalseClass] The result +# +# false: +# * if file does not exist +# true: +# * if file exists +# +# @example Use matcher +# +# RSpec.describe do +# it { expect(file1).to be_existing_file(file2) } +# end RSpec::Matchers.define :be_existing_file do |_| match do |actual| File.file?(absolute_path(actual)) @@ -29,6 +63,34 @@ end end +# @!method have_content(content) +# This matchers checks if has content. It checks exactly if `'string'` +# is given and does a substring check if `/regexp/` is given. +# +# @param [String, Regexp] content +# The content of the file +# +# @return [TrueClass, FalseClass] The result +# +# false: +# * if file does not exist +# * if file content is not equal string +# * if file content does not include regexp +# true: +# * if file content includes regexp +# * if file content is equal string +# +# @example Use matcher with string +# +# RSpec.describe do +# it { expect(file1).to have_content('a') } +# end +# +# @example Use matcher with regexp +# +# RSpec.describe do +# it { expect(file1).to have_content(/a/) } +# end RSpec::Matchers.define :have_content do |expected| match do |actual| path = absolute_path(actual) From 89beb67af4032167d9a0f08ab983c766118a2542 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dennis=20G=C3=BCnnewig?= Date: Thu, 30 Apr 2015 10:30:01 +0200 Subject: [PATCH 8/9] Rename matcher to prevent problems with capybara matcher have_content --- lib/aruba/matchers/file.rb | 8 ++++---- spec/aruba/matchers/file_spec.rb | 12 ++++++------ 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/lib/aruba/matchers/file.rb b/lib/aruba/matchers/file.rb index 50a1f61e6..041a87de2 100644 --- a/lib/aruba/matchers/file.rb +++ b/lib/aruba/matchers/file.rb @@ -63,7 +63,7 @@ end end -# @!method have_content(content) +# @!method have_file_content(content) # This matchers checks if has content. It checks exactly if `'string'` # is given and does a substring check if `/regexp/` is given. # @@ -83,15 +83,15 @@ # @example Use matcher with string # # RSpec.describe do -# it { expect(file1).to have_content('a') } +# it { expect(file1).to have_file_content('a') } # end # # @example Use matcher with regexp # # RSpec.describe do -# it { expect(file1).to have_content(/a/) } +# it { expect(file1).to have_file_content(/a/) } # end -RSpec::Matchers.define :have_content do |expected| +RSpec::Matchers.define :have_file_content do |expected| match do |actual| path = absolute_path(actual) diff --git a/spec/aruba/matchers/file_spec.rb b/spec/aruba/matchers/file_spec.rb index 11087f15a..db0c1ae61 100644 --- a/spec/aruba/matchers/file_spec.rb +++ b/spec/aruba/matchers/file_spec.rb @@ -21,31 +21,31 @@ def absolute_path(*args) end end - describe 'to_have_content' do + describe 'to_have_file_content' do context 'when file exists' do before :each do File.write(@file_path, 'aba') end context 'and file content is exactly equal string ' do - it { expect(@file_name).to have_content('aba') } + it { expect(@file_name).to have_file_content('aba') } end context 'and file content contains string' do - it { expect(@file_name).to have_content(/b/) } + it { expect(@file_name).to have_file_content(/b/) } end context 'and file content is not exactly equal string' do - it { expect(@file_name).not_to have_content('c') } + it { expect(@file_name).not_to have_file_content('c') } end context 'and file content not contains string' do - it { expect(@file_name).not_to have_content(/c/) } + it { expect(@file_name).not_to have_file_content(/c/) } end end context 'when file does not exist' do - it { expect(@file_name).not_to have_content('a') } + it { expect(@file_name).not_to have_file_content('a') } end end end From f357bdcf27fe7f182ed2d062aa7652e9e0f60902 Mon Sep 17 00:00:00 2001 From: Max Meyer Date: Sat, 2 May 2015 10:29:33 +0200 Subject: [PATCH 9/9] Added matcher for file size --- lib/aruba/matchers/file.rb | 33 ++++++++++++++++++++++++++++++++ spec/aruba/matchers/file_spec.rb | 20 +++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/lib/aruba/matchers/file.rb b/lib/aruba/matchers/file.rb index 041a87de2..f34480363 100644 --- a/lib/aruba/matchers/file.rb +++ b/lib/aruba/matchers/file.rb @@ -115,3 +115,36 @@ format("expected that file \"%s\" does not contains exactly:\n%s", actual, expected) end end + +# @!method have_file_size(size) +# This matchers checks if path has file size +# +# @param [Fixnum] size +# The size to check +# +# @return [TrueClass, FalseClass] The result +# +# false: +# * if path does not have size +# true: +# * if path has size +# +# @example Use matcher +# +# RSpec.describe do +# it { expect('file.txt').to have_file_size(0) } +# end +RSpec::Matchers.define :have_file_size do |expected| + match do |actual| + next false unless File.file? expand_path(actual) + File.size(expand_path(actual)) == expected + end + + failure_message do |actual| + format("expected that file \"%s\" has size \"%s\"", actual) + end + + failure_message_when_negated do |actual| + format("expected that file \"%s\" does not have size \"%s\"", actual) + end +end diff --git a/spec/aruba/matchers/file_spec.rb b/spec/aruba/matchers/file_spec.rb index db0c1ae61..15e942aa8 100644 --- a/spec/aruba/matchers/file_spec.rb +++ b/spec/aruba/matchers/file_spec.rb @@ -48,4 +48,24 @@ def absolute_path(*args) it { expect(@file_name).not_to have_file_content('a') } end end + + describe 'to_have_file_size' do + context 'when file exists' do + before :each do + File.write(@file_path, '') + end + + context 'and file size is equal' do + it { expect(@file_name).to have_file_size(0) } + end + + context 'and file size is not equal' do + it { expect(@file_name).not_to have_file_size(1) } + end + end + + context 'when file does not exist' do + it { expect(@file_name).not_to have_file_size(0) } + end + end end