Merge pull request #397 from t-mangoe/set_error_code

Set exit status code when specified path doesn't exist
This commit is contained in:
Claudio Bley 2020-10-07 10:01:29 +02:00
commit 035bcec412
5 changed files with 29 additions and 3 deletions

View file

@ -89,6 +89,17 @@ script:
- colorls --tree=1
- LC_ALL=C colorls spec/fixtures/
- LC_ALL=C colorls --git spec/fixtures/
- |
if colorls does-not-exist; then
echo "expected error!"
exit 1
else
ret=$?
if [ $ret -ne 2 ]; then
echo "unexpected error: $ret"
exit 1
fi
fi
deploy:
provider: rubygems

View file

@ -23,4 +23,4 @@ require 'colorls'
$loading = false
# rubocop:enable Style/GlobalVars
ColorLS::Flags.new(*ARGV).process
exit ColorLS::Flags.new(*ARGV).process

View file

@ -40,8 +40,13 @@ module ColorLS
init_locale
@args = ['.'] if @args.empty?
exit_status_code = 0
@args.sort!.each_with_index do |path, i|
next $stderr.puts "\n Specified path '#{path}' doesn't exist.".colorize(:red) unless File.exist?(path)
unless File.exist?(path)
$stderr.puts "\n Specified path '#{path}' doesn't exist.".colorize(:red)
exit_status_code = 2
next
end
puts '' if i.positive?
puts "\n#{path}:" if Dir.exist?(path) && @args.size > 1
@ -49,6 +54,7 @@ module ColorLS
rescue SystemCallError => e
$stderr.puts "#{path}: #{e}".colorize(:red)
end
exit_status_code
end
def options

View file

@ -1,7 +1,7 @@
.\" generated with Ronn/v0.7.3
.\" http://github.com/rtomayko/ronn/tree/0.7.3
.
.TH "COLORLS" "1" "May 2020" "colorls 1.4.2" "colorls Manual"
.TH "COLORLS" "1" "September 2020" "colorls 1.4.2" "colorls Manual"
.
.SH "NAME"
\fBcolorls\fR \- list directory contents with colors and icons

View file

@ -308,4 +308,13 @@ RSpec.describe ColorLS::Flags do
expect { subject }.to output(/Unrecognized files\s+: 3/).to_stdout
end
end
context 'with non-existent path' do
let(:args) { ['not_exist_file'] }
it 'should exit with status code 2' do
expect {subject}.to output(/ Specified path 'not_exist_file' doesn't exist./).to_stderr
expect(subject).to eq 2
end
end
end