Skip to content

Commit

Permalink
Only warn if locale settings are broken
Browse files Browse the repository at this point in the history
Keep running if calling `setlocale` fails, because of invalid locale settings.

Fixes #335.
  • Loading branch information
avdv committed Apr 16, 2020
1 parent 86cfa2f commit 8a675e4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/colorls/flags.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ def initialize(*args)
end

def process
# initialize locale from environment
CLocale.setlocale(CLocale::LC_COLLATE, '')
init_locale

@args = [Dir.pwd] if @args.empty?
@args.sort!.each_with_index do |path, i|
Expand Down Expand Up @@ -69,6 +68,13 @@ def options

private

def init_locale
# initialize locale from environment
CLocale.setlocale(CLocale::LC_COLLATE, '')
rescue RuntimeError => e
warn "WARN: #{e}, check your locale settings"
end

def add_sort_options(options)
options.separator ''
options.separator 'sorting options:'
Expand Down
10 changes: 10 additions & 0 deletions spec/color_ls/flags_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -290,4 +290,14 @@
expect { subject }.to raise_error('colorls exited with 2').and output(/--help/).to_stderr
end
end

context 'for invalid locale' do
let(:args) { [FIXTURES] }

it 'should warn but not raise an error' do
allow(CLocale).to receive(:setlocale).with(CLocale::LC_COLLATE, '').and_raise(RuntimeError.new("setlocale error"))

expect { subject }.to output(/setlocale error/).to_stderr.and output.to_stdout
end
end
end

0 comments on commit 8a675e4

Please sign in to comment.