Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow lowercase environment variables in UTF-8 check #338

Merged
merged 2 commits into from Oct 3, 2013
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.
+54 −3
Diff settings

Always

Just for now

Copy path View file
@@ -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 }

This comment has been minimized.

Copy link
@bobthecow

bobthecow Oct 3, 2013

Member

do we have to worry about utf16 and other dumb things like that?

This comment has been minimized.

Copy link
@ddfreyne

ddfreyne Oct 3, 2013

Author Member

Nah.

end

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

return true
end

Copy path View file
@@ -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
Copy path View file
@@ -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
@@ -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
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.