diff options
Diffstat (limited to 'spec/ruby/library/pathname')
20 files changed, 219 insertions, 72 deletions
diff --git a/spec/ruby/library/pathname/absolute_spec.rb b/spec/ruby/library/pathname/absolute_spec.rb index af0639493a..109abb8ee9 100644 --- a/spec/ruby/library/pathname/absolute_spec.rb +++ b/spec/ruby/library/pathname/absolute_spec.rb @@ -1,23 +1,22 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' require 'pathname' describe "Pathname#absolute?" do it "returns true for the root directory" do - Pathname.new('/').absolute?.should == true + Pathname.new('/').should.absolute? end it "returns true for a dir starting with a slash" do - Pathname.new('/usr/local/bin').absolute?.should == true + Pathname.new('/usr/local/bin').should.absolute? end it "returns false for a dir not starting with a slash" do - Pathname.new('fish').absolute?.should == false + Pathname.new('fish').should_not.absolute? end it "returns false for a dir not starting with a slash" do - Pathname.new('fish/dog/cow').absolute?.should == false + Pathname.new('fish/dog/cow').should_not.absolute? end end - diff --git a/spec/ruby/library/pathname/birthtime_spec.rb b/spec/ruby/library/pathname/birthtime_spec.rb new file mode 100644 index 0000000000..387f0aa54d --- /dev/null +++ b/spec/ruby/library/pathname/birthtime_spec.rb @@ -0,0 +1,16 @@ +require_relative '../../spec_helper' +require 'pathname' + +describe "Pathname#birthtime" do + platform_is :windows, :darwin, :freebsd, :netbsd do + it "returns the birth time for self" do + Pathname.new(__FILE__).birthtime.should.is_a?(Time) + end + end + + platform_is :openbsd do + it "raises an NotImplementedError" do + -> { Pathname.new(__FILE__).birthtime }.should.raise(NotImplementedError) + end + end +end diff --git a/spec/ruby/library/pathname/divide_spec.rb b/spec/ruby/library/pathname/divide_spec.rb new file mode 100644 index 0000000000..8af79d0c8f --- /dev/null +++ b/spec/ruby/library/pathname/divide_spec.rb @@ -0,0 +1,6 @@ +require_relative '../../spec_helper' +require_relative 'shared/plus' + +describe "Pathname#/" do + it_behaves_like :pathname_plus, :/ +end diff --git a/spec/ruby/library/pathname/empty_spec.rb b/spec/ruby/library/pathname/empty_spec.rb index e573021491..9f0305a0f0 100644 --- a/spec/ruby/library/pathname/empty_spec.rb +++ b/spec/ruby/library/pathname/empty_spec.rb @@ -1,34 +1,32 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' require 'pathname' -ruby_version_is '2.4' do - describe 'Pathname#empty?' do - before :all do - @file = tmp 'new_file_path_name.txt' - touch @file - @dir = tmp 'new_directory_path_name' - Dir.mkdir @dir - end +describe 'Pathname#empty?' do + before :all do + @file = tmp 'new_file_path_name.txt' + touch @file + @dir = tmp 'new_directory_path_name' + Dir.mkdir @dir + end - after :all do - rm_r @file - rm_r @dir - end + after :all do + rm_r @file + rm_r @dir + end - it 'returns true when file is not empty' do - Pathname.new(__FILE__).empty?.should be_false - end + it 'returns true when file is not empty' do + Pathname.new(__FILE__).empty?.should == false + end - it 'returns false when the directory is not empty' do - Pathname.new(__dir__).empty?.should be_false - end + it 'returns false when the directory is not empty' do + Pathname.new(__dir__).empty?.should == false + end - it 'return true when file is empty' do - Pathname.new(@file).empty?.should be_true - end + it 'return true when file is empty' do + Pathname.new(@file).empty?.should == true + end - it 'returns true when directory is empty' do - Pathname.new(@dir).empty?.should be_true - end + it 'returns true when directory is empty' do + Pathname.new(@dir).empty?.should == true end end diff --git a/spec/ruby/library/pathname/equal_value_spec.rb b/spec/ruby/library/pathname/equal_value_spec.rb index afcdb08de8..92d4767e76 100644 --- a/spec/ruby/library/pathname/equal_value_spec.rb +++ b/spec/ruby/library/pathname/equal_value_spec.rb @@ -1,4 +1,4 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' require 'pathname' describe "Pathname#==" do @@ -12,4 +12,3 @@ describe "Pathname#==" do end end - diff --git a/spec/ruby/library/pathname/glob_spec.rb b/spec/ruby/library/pathname/glob_spec.rb new file mode 100644 index 0000000000..e20e6f8f85 --- /dev/null +++ b/spec/ruby/library/pathname/glob_spec.rb @@ -0,0 +1,92 @@ +require_relative '../../spec_helper' +require 'pathname' + +describe 'Pathname.glob' do + before :all do + @dir = tmp('pathname_glob') + '/' + @file_1 = @dir + 'lib/ipaddr.rb' + @file_2 = @dir + 'lib/irb.rb' + @file_3 = @dir + 'lib/.hidden.rb' + + touch @file_1 + touch @file_2 + touch @file_3 + end + + after :all do + rm_r @dir[0...-1] + end + + it 'returns [] for no match' do + Pathname.glob(@dir + 'lib/*.js').should == [] + end + + it 'returns [] when the pathname does not exist' do + Pathname.glob('i_dont_exist/lib/*.js').should == [] + end + + it 'returns matching file paths' do + Pathname.glob(@dir + 'lib/*i*.rb').sort.should == [Pathname.new(@file_1), Pathname.new(@file_2)].sort + end + + it 'returns matching file paths when a flag is provided' do + expected = [Pathname.new(@file_1), Pathname.new(@file_2), Pathname.new(@file_3)].sort + Pathname.glob(@dir + 'lib/*i*.rb', File::FNM_DOTMATCH).sort.should == expected + end + + it 'returns matching file paths when supplied :base keyword argument' do + Pathname.glob('*i*.rb', base: @dir + 'lib').sort.should == [Pathname.new('ipaddr.rb'), Pathname.new('irb.rb')].sort + end + + it "raises an ArgumentError when supplied a keyword argument other than :base" do + -> { + Pathname.glob('*i*.rb', foo: @dir + 'lib') + }.should.raise(ArgumentError, "unknown keyword: :foo") + end + + it "does not raise an ArgumentError when supplied a flag and :base keyword argument" do + expected = [Pathname.new('ipaddr.rb'), Pathname.new('irb.rb'), Pathname.new('.hidden.rb')].sort + Pathname.glob('*i*.rb', File::FNM_DOTMATCH, base: @dir + 'lib').sort.should == expected + end +end + + +describe 'Pathname#glob' do + before :all do + @dir = tmp('pathname_glob') + '/' + @file_1 = @dir + 'lib/ipaddr.rb' + @file_2 = @dir + 'lib/irb.rb' + @file_3 = @dir + 'lib/.hidden.rb' + + touch @file_1 + touch @file_2 + touch @file_3 + end + + after :all do + rm_r @dir[0...-1] + end + + it 'returns [] for no match' do + Pathname.new(@dir).glob('lib/*.js').should == [] + end + + it 'returns [] when the pathname does not exist' do + Pathname.new('./i_dont_exist').glob('lib/*.js').should == [] + end + + it 'returns matching file paths' do + Pathname.new(@dir).glob('lib/*i*.rb').sort.should == [Pathname.new(@file_1), Pathname.new(@file_2)].sort + end + + it 'yields matching file paths to block' do + ary = [] + Pathname.new(@dir).glob('lib/*i*.rb') { |p| ary << p }.should == nil + ary.sort.should == [Pathname.new(@file_1), Pathname.new(@file_2)].sort + end + + it 'returns matching file paths when a flag is provided' do + expected = [Pathname.new(@file_1), Pathname.new(@file_2), Pathname.new(@file_3)].sort + Pathname.new(@dir).glob('lib/*i*.rb', File::FNM_DOTMATCH).sort.should == expected + end +end diff --git a/spec/ruby/library/pathname/hash_spec.rb b/spec/ruby/library/pathname/hash_spec.rb index f3201e2f4f..da1b8f4f76 100644 --- a/spec/ruby/library/pathname/hash_spec.rb +++ b/spec/ruby/library/pathname/hash_spec.rb @@ -1,4 +1,4 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' require 'pathname' describe "Pathname#hash" do @@ -12,4 +12,3 @@ describe "Pathname#hash" do end end - diff --git a/spec/ruby/library/pathname/inspect_spec.rb b/spec/ruby/library/pathname/inspect_spec.rb new file mode 100644 index 0000000000..3abba6cbb5 --- /dev/null +++ b/spec/ruby/library/pathname/inspect_spec.rb @@ -0,0 +1,10 @@ +require_relative '../../spec_helper' +require 'pathname' + +describe "Pathname#inspect" do + it "returns a consistent String" do + result = Pathname.new('/tmp').inspect + result.should.instance_of?(String) + result.should == "#<Pathname:/tmp>" + end +end diff --git a/spec/ruby/library/pathname/join_spec.rb b/spec/ruby/library/pathname/join_spec.rb index 8c77bb1f59..a0877777fc 100644 --- a/spec/ruby/library/pathname/join_spec.rb +++ b/spec/ruby/library/pathname/join_spec.rb @@ -1,4 +1,4 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' require 'pathname' describe "Pathname#join" do diff --git a/spec/ruby/library/pathname/new_spec.rb b/spec/ruby/library/pathname/new_spec.rb index a888e98736..3ef9d9b76d 100644 --- a/spec/ruby/library/pathname/new_spec.rb +++ b/spec/ruby/library/pathname/new_spec.rb @@ -1,25 +1,20 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' require 'pathname' describe "Pathname.new" do it "returns a new Pathname Object with 1 argument" do - Pathname.new('').should be_kind_of(Pathname) + Pathname.new('').should.is_a?(Pathname) end it "raises an ArgumentError when called with \0" do - lambda { Pathname.new("\0")}.should raise_error(ArgumentError) - end - - it "is tainted if path is tainted" do - path = '/usr/local/bin'.taint - Pathname.new(path).tainted?.should == true + -> { Pathname.new("\0")}.should.raise(ArgumentError) end it "raises a TypeError if not passed a String type" do - lambda { Pathname.new(nil) }.should raise_error(TypeError) - lambda { Pathname.new(0) }.should raise_error(TypeError) - lambda { Pathname.new(true) }.should raise_error(TypeError) - lambda { Pathname.new(false) }.should raise_error(TypeError) + -> { Pathname.new(nil) }.should.raise(TypeError) + -> { Pathname.new(0) }.should.raise(TypeError) + -> { Pathname.new(true) }.should.raise(TypeError) + -> { Pathname.new(false) }.should.raise(TypeError) end it "initializes with an object with to_path" do diff --git a/spec/ruby/library/pathname/parent_spec.rb b/spec/ruby/library/pathname/parent_spec.rb index 53d3f1e50e..3843bb22ed 100644 --- a/spec/ruby/library/pathname/parent_spec.rb +++ b/spec/ruby/library/pathname/parent_spec.rb @@ -1,4 +1,4 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' require 'pathname' describe "Pathname#parent" do @@ -16,4 +16,3 @@ describe "Pathname#parent" do end end - diff --git a/spec/ruby/library/pathname/pathname_spec.rb b/spec/ruby/library/pathname/pathname_spec.rb new file mode 100644 index 0000000000..6fa6fd2bcb --- /dev/null +++ b/spec/ruby/library/pathname/pathname_spec.rb @@ -0,0 +1,19 @@ +require_relative '../../spec_helper' +require 'pathname' + +describe "Kernel#Pathname" do + it "is a private instance method" do + Kernel.private_instance_methods(false).should.include?(:Pathname) + end + + it "is also a public method" do + Kernel.should.respond_to?(:Pathname) + end + + it "returns same argument when called with a pathname argument" do + path = Pathname('foo') + new_path = Pathname(path) + + path.should.equal?(new_path) + end +end diff --git a/spec/ruby/library/pathname/plus_spec.rb b/spec/ruby/library/pathname/plus_spec.rb new file mode 100644 index 0000000000..57e472c266 --- /dev/null +++ b/spec/ruby/library/pathname/plus_spec.rb @@ -0,0 +1,6 @@ +require_relative '../../spec_helper' +require_relative 'shared/plus' + +describe "Pathname#+" do + it_behaves_like :pathname_plus, :+ +end diff --git a/spec/ruby/library/pathname/realdirpath_spec.rb b/spec/ruby/library/pathname/realdirpath_spec.rb index f76e37602c..e50741a737 100644 --- a/spec/ruby/library/pathname/realdirpath_spec.rb +++ b/spec/ruby/library/pathname/realdirpath_spec.rb @@ -1,10 +1,10 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' require 'pathname' describe "Pathname#realdirpath" do it "returns a Pathname" do - Pathname.pwd.realdirpath.should be_an_instance_of(Pathname) + Pathname.pwd.realdirpath.should.instance_of?(Pathname) end end diff --git a/spec/ruby/library/pathname/realpath_spec.rb b/spec/ruby/library/pathname/realpath_spec.rb index e1c9eb34ea..d8b87f57d0 100644 --- a/spec/ruby/library/pathname/realpath_spec.rb +++ b/spec/ruby/library/pathname/realpath_spec.rb @@ -1,10 +1,10 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' require 'pathname' describe "Pathname#realpath" do it "returns a Pathname" do - Pathname.pwd.realpath.should be_an_instance_of(Pathname) + Pathname.pwd.realpath.should.instance_of?(Pathname) end end diff --git a/spec/ruby/library/pathname/relative_path_from_spec.rb b/spec/ruby/library/pathname/relative_path_from_spec.rb index 416eaa1a50..7cbd22c3d6 100644 --- a/spec/ruby/library/pathname/relative_path_from_spec.rb +++ b/spec/ruby/library/pathname/relative_path_from_spec.rb @@ -1,4 +1,4 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' require 'pathname' describe "Pathname#relative_path_from" do @@ -7,11 +7,11 @@ describe "Pathname#relative_path_from" do end it "raises an error when the two paths do not share a common prefix" do - lambda { relative_path_str('/usr', 'foo') }.should raise_error(ArgumentError) + -> { relative_path_str('/usr', 'foo') }.should.raise(ArgumentError) end it "raises an error when the base directory has .." do - lambda { relative_path_str('a', '..') }.should raise_error(ArgumentError) + -> { relative_path_str('a', '..') }.should.raise(ArgumentError) end it "returns a path relative from root" do @@ -48,4 +48,8 @@ describe "Pathname#relative_path_from" do relative_path_str('..', '..').should == '.' relative_path_str('..', '.').should == '..' end + + it 'converts string argument to Pathname' do + Pathname.new('/usr/bin/ls').relative_path_from('/usr').to_s.should == 'bin/ls' + end end diff --git a/spec/ruby/library/pathname/relative_spec.rb b/spec/ruby/library/pathname/relative_spec.rb index a44d8c5a66..0fab9a7b9f 100644 --- a/spec/ruby/library/pathname/relative_spec.rb +++ b/spec/ruby/library/pathname/relative_spec.rb @@ -1,23 +1,22 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' require 'pathname' describe "Pathname#relative?" do it "returns false for the root directory" do - Pathname.new('/').relative?.should == false + Pathname.new('/').should_not.relative? end it "returns false for a dir starting with a slash" do - Pathname.new('/usr/local/bin').relative?.should == false + Pathname.new('/usr/local/bin').should_not.relative? end it "returns true for a dir not starting with a slash" do - Pathname.new('fish').relative?.should == true + Pathname.new('fish').should.relative? end it "returns true for a dir not starting with a slash" do - Pathname.new('fish/dog/cow').relative?.should == true + Pathname.new('fish/dog/cow').should.relative? end end - diff --git a/spec/ruby/library/pathname/root_spec.rb b/spec/ruby/library/pathname/root_spec.rb index a5efcf69b4..cd2be24516 100644 --- a/spec/ruby/library/pathname/root_spec.rb +++ b/spec/ruby/library/pathname/root_spec.rb @@ -1,27 +1,26 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' require 'pathname' describe "Pathname#root?" do it "returns true for root directories" do - Pathname.new('/').root?.should == true + Pathname.new('/').should.root? end it "returns false for empty string" do - Pathname.new('').root?.should == false + Pathname.new('').should_not.root? end it "returns false for a top level directory" do - Pathname.new('/usr').root?.should == false + Pathname.new('/usr').should_not.root? end it "returns false for a top level with .. appended directory" do - Pathname.new('/usr/..').root?.should == false + Pathname.new('/usr/..').should_not.root? end it "returns false for a directory below top level" do - Pathname.new('/usr/local/bin/').root?.should == false + Pathname.new('/usr/local/bin/').should_not.root? end end - diff --git a/spec/ruby/library/pathname/shared/plus.rb b/spec/ruby/library/pathname/shared/plus.rb new file mode 100644 index 0000000000..b3b896ea43 --- /dev/null +++ b/spec/ruby/library/pathname/shared/plus.rb @@ -0,0 +1,8 @@ +require 'pathname' + +describe :pathname_plus, shared: true do + it "appends a pathname to self" do + p = Pathname.new("/usr") + p.send(@method, "bin/ruby").should == Pathname.new("/usr/bin/ruby") + end +end diff --git a/spec/ruby/library/pathname/sub_spec.rb b/spec/ruby/library/pathname/sub_spec.rb index 36b6ebb247..ad2900f62b 100644 --- a/spec/ruby/library/pathname/sub_spec.rb +++ b/spec/ruby/library/pathname/sub_spec.rb @@ -1,4 +1,4 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' require 'pathname' describe "Pathname#sub" do @@ -13,4 +13,3 @@ describe "Pathname#sub" do end end - |
