diff options
Diffstat (limited to 'spec/ruby/library/pathname')
20 files changed, 458 insertions, 0 deletions
diff --git a/spec/ruby/library/pathname/absolute_spec.rb b/spec/ruby/library/pathname/absolute_spec.rb new file mode 100644 index 0000000000..109abb8ee9 --- /dev/null +++ b/spec/ruby/library/pathname/absolute_spec.rb @@ -0,0 +1,22 @@ +require_relative '../../spec_helper' +require 'pathname' + +describe "Pathname#absolute?" do + + it "returns true for the root directory" do + Pathname.new('/').should.absolute? + end + + it "returns true for a dir starting with a slash" do + Pathname.new('/usr/local/bin').should.absolute? + end + + it "returns false for a dir not starting with a slash" do + Pathname.new('fish').should_not.absolute? + end + + it "returns false for a dir not starting with a slash" do + 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..109c112303 --- /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 be_kind_of(Time) + end + end + + platform_is :openbsd do + it "raises an NotImplementedError" do + -> { Pathname.new(__FILE__).birthtime }.should raise_error(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 new file mode 100644 index 0000000000..4deade5b64 --- /dev/null +++ b/spec/ruby/library/pathname/empty_spec.rb @@ -0,0 +1,32 @@ +require_relative '../../spec_helper' +require 'pathname' + +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 + + it 'returns true when file is not empty' do + Pathname.new(__FILE__).empty?.should be_false + end + + it 'returns false when the directory is not empty' do + Pathname.new(__dir__).empty?.should be_false + end + + it 'return true when file is empty' do + Pathname.new(@file).empty?.should be_true + end + + it 'returns true when directory is empty' do + Pathname.new(@dir).empty?.should be_true + end +end diff --git a/spec/ruby/library/pathname/equal_value_spec.rb b/spec/ruby/library/pathname/equal_value_spec.rb new file mode 100644 index 0000000000..92d4767e76 --- /dev/null +++ b/spec/ruby/library/pathname/equal_value_spec.rb @@ -0,0 +1,14 @@ +require_relative '../../spec_helper' +require 'pathname' + +describe "Pathname#==" do + + it "returns true when identical paths are used" do + (Pathname.new('') == Pathname.new('')).should == true + end + + it "returns true when identical paths are used" do + (Pathname.new('') == Pathname.new('/usr/local/bin')).should == false + 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..de322bab47 --- /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_error(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 be_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 new file mode 100644 index 0000000000..da1b8f4f76 --- /dev/null +++ b/spec/ruby/library/pathname/hash_spec.rb @@ -0,0 +1,14 @@ +require_relative '../../spec_helper' +require 'pathname' + +describe "Pathname#hash" do + + it "is equal to the hash of the pathname" do + Pathname.new('/usr/local/bin/').hash.should == '/usr/local/bin/'.hash + end + + it "is not equal the hash of a different pathname" do + Pathname.new('/usr/local/bin/').hash.should_not == '/usr/bin/'.hash + 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..304746fbe5 --- /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 be_an_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 new file mode 100644 index 0000000000..a0877777fc --- /dev/null +++ b/spec/ruby/library/pathname/join_spec.rb @@ -0,0 +1,40 @@ +require_relative '../../spec_helper' +require 'pathname' + +describe "Pathname#join" do + it "without separators" do + Pathname.new('/usr').join(Pathname.new('foo')).should == Pathname.new('/usr/foo') + end + + it "with separators" do + Pathname.new('/usr').join(Pathname.new('/foo')).should == Pathname.new('/foo') + end + + it "with a string" do + Pathname.new('/usr').join('foo').should == Pathname.new('/usr/foo') + end + + it "with root" do + Pathname.new('/usr').join(Pathname.new('/')).should == Pathname.new('/') + end + + it "with a relative path" do + Pathname.new('/usr').join(Pathname.new('../foo')).should == Pathname.new('/foo') + end + + it "a relative path with current" do + Pathname.new('.').join(Pathname.new('foo')).should == Pathname.new('foo') + end + + it "an absolute path with current" do + Pathname.new('.').join(Pathname.new('/foo')).should == Pathname.new('/foo') + end + + it "a prefixed relative path with current" do + Pathname.new('.').join(Pathname.new('./foo')).should == Pathname.new('foo') + end + + it "multiple paths" do + Pathname.new('.').join(Pathname.new('./foo'), 'bar').should == Pathname.new('foo/bar') + end +end diff --git a/spec/ruby/library/pathname/new_spec.rb b/spec/ruby/library/pathname/new_spec.rb new file mode 100644 index 0000000000..36226ed515 --- /dev/null +++ b/spec/ruby/library/pathname/new_spec.rb @@ -0,0 +1,23 @@ +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) + end + + it "raises an ArgumentError when called with \0" do + -> { Pathname.new("\0")}.should raise_error(ArgumentError) + end + + it "raises a TypeError if not passed a String type" do + -> { Pathname.new(nil) }.should raise_error(TypeError) + -> { Pathname.new(0) }.should raise_error(TypeError) + -> { Pathname.new(true) }.should raise_error(TypeError) + -> { Pathname.new(false) }.should raise_error(TypeError) + end + + it "initializes with an object with to_path" do + Pathname.new(mock_to_path('foo')).should == Pathname.new('foo') + end +end diff --git a/spec/ruby/library/pathname/parent_spec.rb b/spec/ruby/library/pathname/parent_spec.rb new file mode 100644 index 0000000000..3843bb22ed --- /dev/null +++ b/spec/ruby/library/pathname/parent_spec.rb @@ -0,0 +1,18 @@ +require_relative '../../spec_helper' +require 'pathname' + +describe "Pathname#parent" do + + it "has parent of root as root" do + Pathname.new('/').parent.to_s.should == '/' + end + + it "has parent of /usr/ as root" do + Pathname.new('/usr/').parent.to_s.should == '/' + end + + it "has parent of /usr/local as root" do + Pathname.new('/usr/local').parent.to_s.should == '/usr' + 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..0fb2881468 --- /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.should have_private_instance_method(:Pathname) + end + + it "is also a public method" do + Kernel.should have_method(: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 new file mode 100644 index 0000000000..a9e44e354e --- /dev/null +++ b/spec/ruby/library/pathname/realdirpath_spec.rb @@ -0,0 +1,10 @@ +require_relative '../../spec_helper' +require 'pathname' + +describe "Pathname#realdirpath" do + + it "returns a Pathname" do + Pathname.pwd.realdirpath.should be_an_instance_of(Pathname) + end + +end diff --git a/spec/ruby/library/pathname/realpath_spec.rb b/spec/ruby/library/pathname/realpath_spec.rb new file mode 100644 index 0000000000..f2c654308e --- /dev/null +++ b/spec/ruby/library/pathname/realpath_spec.rb @@ -0,0 +1,10 @@ +require_relative '../../spec_helper' +require 'pathname' + +describe "Pathname#realpath" do + + it "returns a Pathname" do + Pathname.pwd.realpath.should be_an_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 new file mode 100644 index 0000000000..133a149849 --- /dev/null +++ b/spec/ruby/library/pathname/relative_path_from_spec.rb @@ -0,0 +1,55 @@ +require_relative '../../spec_helper' +require 'pathname' + +describe "Pathname#relative_path_from" do + def relative_path_str(dest, base) + Pathname.new(dest).relative_path_from(Pathname.new(base)).to_s + end + + it "raises an error when the two paths do not share a common prefix" do + -> { relative_path_str('/usr', 'foo') }.should raise_error(ArgumentError) + end + + it "raises an error when the base directory has .." do + -> { relative_path_str('a', '..') }.should raise_error(ArgumentError) + end + + it "returns a path relative from root" do + relative_path_str('/usr', '/').should == 'usr' + end + + it 'returns 1 level up when both paths are relative' do + relative_path_str('a', 'b').should == '../a' + relative_path_str('a', 'b/').should == '../a' + end + + it 'returns a relative path when both are absolute' do + relative_path_str('/a', '/b').should == '../a' + end + + it "returns a path relative to the current directory" do + relative_path_str('/usr/bin/ls', '/usr').should == 'bin/ls' + end + + it 'returns a . when base and dest are the same' do + relative_path_str('/usr', '/usr').should == '.' + end + + it 'returns the same directory with a non clean base that matches the current dir' do + relative_path_str('/usr', '/stuff/..').should == 'usr' + end + + it 'returns a relative path with a non clean base that matches a different dir' do + relative_path_str('/usr', '/stuff/../foo').should == '../usr' + end + + it 'returns current and pattern when only those patterns are used' do + relative_path_str('.', '.').should == '.' + 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 new file mode 100644 index 0000000000..0fab9a7b9f --- /dev/null +++ b/spec/ruby/library/pathname/relative_spec.rb @@ -0,0 +1,22 @@ +require_relative '../../spec_helper' +require 'pathname' + +describe "Pathname#relative?" do + + it "returns false for the root directory" do + Pathname.new('/').should_not.relative? + end + + it "returns false for a dir starting with a slash" do + Pathname.new('/usr/local/bin').should_not.relative? + end + + it "returns true for a dir not starting with a slash" do + Pathname.new('fish').should.relative? + end + + it "returns true for a dir not starting with a slash" do + 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 new file mode 100644 index 0000000000..cd2be24516 --- /dev/null +++ b/spec/ruby/library/pathname/root_spec.rb @@ -0,0 +1,26 @@ +require_relative '../../spec_helper' +require 'pathname' + +describe "Pathname#root?" do + + it "returns true for root directories" do + Pathname.new('/').should.root? + end + + it "returns false for empty string" do + Pathname.new('').should_not.root? + end + + it "returns false for a top level directory" do + Pathname.new('/usr').should_not.root? + end + + it "returns false for a top level with .. appended directory" do + Pathname.new('/usr/..').should_not.root? + end + + it "returns false for a directory below top level" do + 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 new file mode 100644 index 0000000000..ad2900f62b --- /dev/null +++ b/spec/ruby/library/pathname/sub_spec.rb @@ -0,0 +1,15 @@ +require_relative '../../spec_helper' +require 'pathname' + +describe "Pathname#sub" do + + it "replaces the pattern with rest" do + Pathname.new('/usr/local/bin/').sub(/local/, 'fish').to_s.should == '/usr/fish/bin/' + end + + it "returns a new object" do + p = Pathname.new('/usr/local/bin/') + p.sub(/local/, 'fish').should_not == p + end + +end |
