Merge pull request #222 from avdv/fix-220

Check whether Etc.getpwuid/getgrid returns nil
This commit is contained in:
Claudio Bley 2018-10-05 23:15:20 +02:00 committed by GitHub
commit 3b36d36b0b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 4 deletions

View file

@ -25,7 +25,8 @@ module ColorLS
def owner
return @@users[@stats.uid] if @@users.key? @stats.uid
@@users[@stats.uid] = Etc.getpwuid(@stats.uid).name
user = Etc.getpwuid(@stats.uid)
@@users[@stats.uid] = user.nil? ? @stats.uid.to_s : user.name
rescue ArgumentError
@stats.uid.to_s
end
@ -36,7 +37,8 @@ module ColorLS
def group
return @@groups[@stats.gid] if @@groups.key? @stats.gid
@@groups[@stats.gid] = Etc.getgrgid(@stats.gid).name
group = Etc.getgrgid(@stats.gid)
@@groups[@stats.gid] = group.nil? ? @stats.gid.to_s : group.name
rescue ArgumentError
@stats.gid.to_s
end

View file

@ -71,6 +71,22 @@ RSpec.describe ColorLS::Flags do
end
end
context 'with --long flag on windows' do
let(:args) { ['--long', "#{FIXTURES}/a.txt"] }
before {
ColorLS::FileInfo.class_variable_set :@@users, {}
ColorLS::FileInfo.class_variable_set :@@groups, {}
}
it 'returns no user / group info' do
expect(::Etc).to receive(:getpwuid).and_return(nil)
expect(::Etc).to receive(:getgrgid).and_return(nil)
is_expected.to match(/\s+ \d+ \s+ \d+ .* a.txt/mx)
end
end
context 'with --all flag' do
let(:args) { ['--all', FIXTURES] }
@ -107,9 +123,9 @@ RSpec.describe ColorLS::Flags do
end
context 'with --sort=size flag' do
let(:args) { ['--sort=size', FIXTURES] }
let(:args) { ['--sort=size', '--group-directories-first', FIXTURES] }
it { is_expected.to match(/a-file.+z-file.+symlinks/) } # sorts results by size
it { is_expected.to match(/symlinks.+a-file.+z-file/) } # sorts results by size
end
context 'with --sort=extension flag' do