Skip to content

Commit

Permalink
feat(out): detect non-UTF8 environment and adapt output
Browse files Browse the repository at this point in the history
This test has to be run in docker, because the host system
may only have unicode locales available, and adding a new one
requires root access.
  • Loading branch information
k-dovgan authored Sep 21, 2020
1 parent ad7f264 commit 1ee5e87
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
22 changes: 22 additions & 0 deletions tests/test_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,25 @@ def test_p4_multiple_spaces_in_mappings(perforce_workspace, tmpdir):
environment.settings.PerforceMainVcs.force_clean = True
environment.settings.PerforceWithMappings.mappings = [f"{perforce_workspace.depot} /..."]
assert not __main__.run(environment.settings)


def test_non_utf8_environment(docker_main):
# POSIX has no 'UTF-8' in it's name, but supports Unicode
output = docker_main.run("""
from universum.configuration_support import Variations
configs = Variations([dict(name="Test configuration", command=["ls", "-la"])])
""", vcs_type="none", environment=['LANG=POSIX', 'LC_ALL=POSIX'])
assert "\u2514" in output

# 'en_US', unlike 'en_US.UTF-8', is latin-1
docker_main.clean_artifacts()
docker_main.environment.assert_successful_execution('apt install -y locales')
docker_main.environment.assert_successful_execution('locale-gen --purge en_US')
docker_main.environment.assert_successful_execution('update-locale LANG=en_US')
output = docker_main.run("""
from universum.configuration_support import Variations
configs = Variations([dict(name="Test configuration", command=["ls", "-la"])])
""", vcs_type="none", environment=['LANG=en_US', 'LC_ALL=en_US'])
assert "\u2514" not in output
10 changes: 8 additions & 2 deletions universum/modules/output/terminal_based_output.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import locale
import sys

from six.moves import range
Expand Down Expand Up @@ -28,6 +29,7 @@ class TerminalBasedOutput(BaseOutput):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.block_level = 0
self.unicode_acceptable = (locale.getpreferredencoding() == "UTF-8")

def indent(self):
for x in range(0, self.block_level):
Expand All @@ -48,11 +50,15 @@ def open_block(self, num_str, name):
def close_block(self, num_str, name, status):
self.block_level -= 1
self.indent()
if self.unicode_acceptable:
block_end = " \u2514 "
else:
block_end = " | "

if status == "Failed":
stdout(self.block_level * " ", " \u2514 ", Colors.red, "[Failed]", Colors.reset)
stdout(self.block_level * " ", block_end, Colors.red, "[Failed]", Colors.reset)
else:
stdout(self.block_level * " ", " \u2514 ", Colors.green, "[Success]", Colors.reset)
stdout(self.block_level * " ", block_end, Colors.green, "[Success]", Colors.reset)
self.indent()
stdout()

Expand Down

0 comments on commit 1ee5e87

Please sign in to comment.