Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(PUP-11184) Expand to long paths #8768

Merged
merged 2 commits into from
Oct 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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