diff options
Diffstat (limited to 'spec/ruby/core/dir/home_spec.rb')
| -rw-r--r-- | spec/ruby/core/dir/home_spec.rb | 85 |
1 files changed, 72 insertions, 13 deletions
diff --git a/spec/ruby/core/dir/home_spec.rb b/spec/ruby/core/dir/home_spec.rb index 6d99678034..f0b20e0687 100644 --- a/spec/ruby/core/dir/home_spec.rb +++ b/spec/ruby/core/dir/home_spec.rb @@ -1,26 +1,85 @@ -require File.expand_path('../../../spec_helper', __FILE__) -require File.expand_path('../fixtures/common', __FILE__) +require_relative '../../spec_helper' +require_relative 'fixtures/common' describe "Dir.home" do - it "returns the current user's home directory as a string if called without arguments" do - home_directory = ENV['HOME'] - platform_is :windows do - unless home_directory - home_directory = ENV['HOMEDRIVE'] + ENV['HOMEPATH'] + before :each do + @home = ENV['HOME'] + ENV['HOME'] = "/rubyspec_home" + end + + after :each do + ENV['HOME'] = @home + end + + describe "when called without arguments" do + it "returns the current user's home directory, reading $HOME first" do + Dir.home.should == "/rubyspec_home" + end + + it "returns a non-frozen string" do + Dir.home.should_not.frozen? + end + + it "returns a string with the filesystem encoding" do + Dir.home.encoding.should == Encoding.find("filesystem") + end + + platform_is_not :windows do + it "works even if HOME is unset" do + ENV.delete('HOME') + Dir.home.should.start_with?('/') + Dir.home.encoding.should == Encoding.find("filesystem") end - home_directory = home_directory.tr('\\', '/').chomp('/') end - Dir.home.should == home_directory + platform_is :windows do + it "returns the home directory with forward slashs and as UTF-8" do + ENV['HOME'] = "C:\\rubyspäc\\home" + home = Dir.home + home.should == "C:/rubyspäc/home" + home.encoding.should == Encoding::UTF_8 + end + + it "retrieves the directory from HOME, USERPROFILE, HOMEDRIVE/HOMEPATH and the WinAPI in that order" do + old_dirs = [ENV.delete('HOME'), ENV.delete('USERPROFILE'), ENV.delete('HOMEDRIVE'), ENV.delete('HOMEPATH')] + + Dir.home.should == old_dirs[1].gsub("\\", "/") + ENV['HOMEDRIVE'] = "C:" + ENV['HOMEPATH'] = "\\rubyspec\\home1" + Dir.home.should == "C:/rubyspec/home1" + ENV['USERPROFILE'] = "C:\\rubyspec\\home2" + Dir.home.should == "C:/rubyspec/home2" + ENV['HOME'] = "C:\\rubyspec\\home3" + Dir.home.should == "C:/rubyspec/home3" + ensure + ENV['HOME'], ENV['USERPROFILE'], ENV['HOMEDRIVE'], ENV['HOMEPATH'] = *old_dirs + end + end end - platform_is_not :windows do - it "returns the named user's home directory as a string if called with an argument" do - Dir.home(ENV['USER']).should == ENV['HOME'] + describe "when called with the current user name" do + platform_is_not :windows, :android, :wasi do + it "returns the named user's home directory, from the user database" do + Dir.home(ENV['USER']).should == `echo ~#{ENV['USER']}`.chomp + end + end + + it "returns a non-frozen string" do + Dir.home(ENV['USER']).should_not.frozen? + end + + it "returns a string with the filesystem encoding" do + Dir.home(ENV['USER']).encoding.should == Encoding.find("filesystem") end end it "raises an ArgumentError if the named user doesn't exist" do - lambda { Dir.home('geuw2n288dh2k') }.should raise_error(ArgumentError) + -> { Dir.home('geuw2n288dh2k') }.should.raise(ArgumentError) + end + + describe "when called with a nil user name" do + it "returns the current user's home directory, reading $HOME first" do + Dir.home(nil).should == "/rubyspec_home" + end end end |
