Skip to content

Commit ee43ce6

Browse files
committed
(PUP-11345) Add missing assert_path
Puppet::FileSystem.chmod didn't assert its path.
1 parent 96c5117 commit ee43ce6

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

lib/puppet/file_system.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ def self.exclusive_create(path, mode, &block)
396396
# @api public
397397
#
398398
def self.chmod(mode, path)
399-
@impl.chmod(mode, path)
399+
@impl.chmod(mode, assert_path(path))
400400
end
401401

402402
# Replace the contents of a file atomically, creating the file if necessary.

spec/unit/file_system_spec.rb

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -984,11 +984,12 @@ def increment_counter_in_multiple_processes(file, num_procs, options)
984984
end
985985

986986
it 'preserves file ownership' do
987-
allow(Puppet::FileSystem).to receive(:lstat)
988-
.with(Puppet::FileSystem.pathname(dest))
989-
.and_return(double(uid: 1, gid: 2))
987+
FileUtils.touch(dest)
988+
allow(File).to receive(:lstat).and_call_original
989+
allow(File).to receive(:lstat).with(Pathname.new(dest)).and_return(double(uid: 1, gid: 2, 'directory?': false))
990990

991-
expect(FileUtils).to receive(:chown).with(1, 2, /#{dest}/)
991+
allow(File).to receive(:chown).and_call_original
992+
expect(FileUtils).to receive(:chown).with(1, 2, any_args)
992993

993994
Puppet::FileSystem.replace_file(dest, 0644) { |f| f.write(content) }
994995
end
@@ -1163,4 +1164,33 @@ def increment_counter_in_multiple_processes(file, num_procs, options)
11631164
expect(File.mtime(dest)).to be_within(1).of(tomorrow)
11641165
end
11651166
end
1167+
1168+
context '#chmod' do
1169+
let(:dest) { tmpfile('abs_file') }
1170+
1171+
it "changes the mode given an absolute string" do
1172+
Puppet::FileSystem.touch(dest)
1173+
Puppet::FileSystem.chmod(0644, dest)
1174+
expect(File.stat(dest).mode & 0777).to eq(0644)
1175+
end
1176+
1177+
it "returns true if given an absolute pathname" do
1178+
Puppet::FileSystem.touch(dest)
1179+
Puppet::FileSystem.chmod(0644, Pathname.new(dest))
1180+
expect(File.stat(dest).mode & 0777).to eq(0644)
1181+
end
1182+
1183+
it "raises if the file doesn't exist" do
1184+
klass = Puppet::Util::Platform.windows? ? Puppet::Error : Errno::ENOENT
1185+
expect {
1186+
Puppet::FileSystem.chmod(0644, dest)
1187+
}.to raise_error(klass)
1188+
end
1189+
1190+
it "raises ArgumentError if dest is invalid" do
1191+
expect {
1192+
Puppet::FileSystem.chmod(0644, nil)
1193+
}.to raise_error(ArgumentError, /expected Pathname, got: 'NilClass'/)
1194+
end
1195+
end
11661196
end

0 commit comments

Comments
 (0)