Skip to content

Commit

Permalink
Merge pull request #8768 from joshcooper/expand_long_path
Browse files Browse the repository at this point in the history
(PUP-11184) Expand to long paths
  • Loading branch information
GabrielNagy authored Oct 1, 2021
2 parents 39aaa94 + ca7fac8 commit 90ec8bd
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
3 changes: 2 additions & 1 deletion lib/puppet/file_system.rb
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,8 @@ def self.pathname(path)
# value ~ will be expanded to something like /Users/Foo
#
# This method exists primarlily to resolve a Ruby deficiency where
# File.expand_path doesn't handle ~ in each segment on a Windows path
# File.expand_path doesn't convert short paths to long paths, which is
# important when resolving the path to load.
#
# @param path [Object] a path handle produced by {#pathname}
# @return [String] a string representation of the path
Expand Down
2 changes: 1 addition & 1 deletion lib/puppet/util/autoload.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def files_to_load(path, env)

# @api private
def files_in_dir(dir, path)
dir = Pathname.new(File.expand_path(dir))
dir = Pathname.new(Puppet::FileSystem.expand_path(dir))
Dir.glob(File.join(dir, path, "*.rb")).collect do |file|
Pathname.new(file).relative_path_from(dir).to_s
end
Expand Down
33 changes: 25 additions & 8 deletions spec/unit/util/autoload_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -178,15 +178,14 @@ def with_libdir(libdir)
end

describe "when loading all files" do
let(:basedir) { tmpdir('autoloader') }
let(:path) { File.join(basedir, @autoload.path, 'file.rb') }

before do
allow(@autoload.class).to receive(:search_directories).and_return([make_absolute("/a")])
allow(FileTest).to receive(:directory?).and_return(true)
allow(Dir).to receive(:glob).and_return([make_absolute("/a/foo/file.rb")])
allow(Puppet::FileSystem).to receive(:exist?).and_return(true)
@time_a = Time.utc(2010, 'jan', 1, 6, 30)
allow(File).to receive(:mtime).and_return(@time_a)
FileUtils.mkdir_p(File.dirname(path))
FileUtils.touch(path)

allow(@autoload.class).to receive(:loaded?).and_return(false)
allow(@autoload.class).to receive(:search_directories).and_return([basedir])
end

[RuntimeError, LoadError, SyntaxError].each do |error|
Expand All @@ -198,7 +197,25 @@ def with_libdir(libdir)
end

it "should require the full path to the file" do
expect(Kernel).to receive(:load).with(make_absolute("/a/foo/file.rb"), any_args)
expect(Kernel).to receive(:load).with(path, any_args)

@autoload.loadall(env)
end

it "autoloads from a directory whose ancestor is Windows 8.3", if: Puppet::Util::Platform.windows? do
# File.expand_path will expand ~ in the last directory component only(!)
# so create an ancestor directory with a long path
dir = File.join(tmpdir('longpath'), 'short')
path = File.join(dir, @autoload.path, 'file.rb')

FileUtils.mkdir_p(File.dirname(path))
FileUtils.touch(path)

dir83 = File.join(File.dirname(basedir), 'longpa~1', 'short')
path83 = File.join(dir83, @autoload.path, 'file.rb')

allow(@autoload.class).to receive(:search_directories).and_return([dir83])
expect(Kernel).to receive(:load).with(path83, any_args)

@autoload.loadall(env)
end
Expand Down

0 comments on commit 90ec8bd

Please sign in to comment.