Skip to content

Commit

Permalink
Merge pull request #338 from nanoc/bug/case-insensitive-utf-8-check
Browse files Browse the repository at this point in the history
Allow lowercase environment variables in UTF-8 check
  • Loading branch information
denisdefreyne committed Oct 3, 2013
2 parents 28221d2 + 1a95618 commit 4f1b35c
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/nanoc/cli.rb
Expand Up @@ -184,7 +184,7 @@ def self.setup_cleaning_streams
def self.enable_utf8?(io)
return true if !io.tty?

%w( LC_ALL LC_CTYPE LANG ).any? { |e| ENV[e] =~ /UTF/ }
%w( LC_ALL LC_CTYPE LANG ).any? { |e| ENV[e] =~ /UTF/i }
end

# @return [Boolean] true if color support is present, false if not
Expand All @@ -196,7 +196,7 @@ def self.enable_ansi_colors?(io)
rescue LoadError
return false
end

return true
end

Expand Down
43 changes: 43 additions & 0 deletions test/cli/test_cli.rb
Expand Up @@ -102,4 +102,47 @@ def test_load_custom_commands_broken
end
end

def test_enable_utf8_only_on_tty
new_env_diff = {
'LC_ALL' => 'en_US.ISO-8859-1',
'LC_CTYPE' => 'en_US.ISO-8859-1',
'LANG' => 'en_US.ISO-8859-1',
}
with_env_vars(new_env_diff) do
io = StringIO.new
def io.tty? ; true ; end
refute Nanoc::CLI.enable_utf8?(io)

io = StringIO.new
def io.tty? ; false ; end
assert Nanoc::CLI.enable_utf8?(io)
end
end

def test_enable_utf8
io = StringIO.new
def io.tty? ; true ; end

new_env_diff = {
'LC_ALL' => 'en_US.ISO-8859-1',
'LC_CTYPE' => 'en_US.ISO-8859-1',
'LANG' => 'en_US.ISO-8859-1',
}
with_env_vars(new_env_diff) do
refute Nanoc::CLI.enable_utf8?(io)

with_env_vars({ 'LC_ALL' => 'en_US.UTF-8' }) { assert Nanoc::CLI.enable_utf8?(io) }
with_env_vars({ 'LC_CTYPE' => 'en_US.UTF-8' }) { assert Nanoc::CLI.enable_utf8?(io) }
with_env_vars({ 'LANG' => 'en_US.UTF-8' }) { assert Nanoc::CLI.enable_utf8?(io) }

with_env_vars({ 'LC_ALL' => 'en_US.utf-8' }) { assert Nanoc::CLI.enable_utf8?(io) }
with_env_vars({ 'LC_CTYPE' => 'en_US.utf-8' }) { assert Nanoc::CLI.enable_utf8?(io) }
with_env_vars({ 'LANG' => 'en_US.utf-8' }) { assert Nanoc::CLI.enable_utf8?(io) }

with_env_vars({ 'LC_ALL' => 'en_US.utf8' }) { assert Nanoc::CLI.enable_utf8?(io) }
with_env_vars({ 'LC_CTYPE' => 'en_US.utf8' }) { assert Nanoc::CLI.enable_utf8?(io) }
with_env_vars({ 'LANG' => 'en_US.utf8' }) { assert Nanoc::CLI.enable_utf8?(io) }
end
end

end
10 changes: 9 additions & 1 deletion test/helper.rb
Expand Up @@ -181,7 +181,7 @@ def assert_examples_correct(object)
lines.each_slice(2) do |pair|
actual_out = eval(pair.first, b)
expected_out = eval(pair.last.match(/# ?=>(.*)/)[1], b)

assert_equal expected_out, actual_out,
"Incorrect example:\n#{pair.first}"
end
Expand All @@ -205,6 +205,14 @@ def assert_raises_frozen_error
assert_match(/(^can't modify frozen |^unable to modify frozen object$)/, error.message)
end

def with_env_vars(hash, &block)
orig_env_hash = ENV.to_hash
hash.each_pair { |k,v| ENV[k] = v }
yield
ensure
orig_env_hash.each_pair { |k,v| ENV[k] = v }
end

end

class Nanoc::TestCase < MiniTest::Unit::TestCase
Expand Down

0 comments on commit 4f1b35c

Please sign in to comment.