diff --git a/.travis.yml b/.travis.yml index 0129b7c..696441b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 diff --git a/exe/colorls b/exe/colorls index 6f5fac9..1c5e87a 100755 --- a/exe/colorls +++ b/exe/colorls @@ -23,4 +23,4 @@ require 'colorls' $loading = false # rubocop:enable Style/GlobalVars -ColorLS::Flags.new(*ARGV).process +exit ColorLS::Flags.new(*ARGV).process diff --git a/lib/colorls/flags.rb b/lib/colorls/flags.rb index c964666..17811a7 100644 --- a/lib/colorls/flags.rb +++ b/lib/colorls/flags.rb @@ -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 diff --git a/man/colorls.1 b/man/colorls.1 index c95c897..22c0e91 100644 --- a/man/colorls.1 +++ b/man/colorls.1 @@ -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 diff --git a/spec/color_ls/flags_spec.rb b/spec/color_ls/flags_spec.rb index d6b1cd8..4425bd4 100644 --- a/spec/color_ls/flags_spec.rb +++ b/spec/color_ls/flags_spec.rb @@ -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