Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions glazier/lib/actions/sysprep.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ def _EditUnattend(self,
"""
lines = []
try:
with open(unattend_path) as unattend:
with open(unattend_path, encoding='utf-8') as unattend:
lines = unattend.readlines()
lines = [
re.sub('<TimeZone>.*?</TimeZone>', '<TimeZone>%s</TimeZone>' % zone,
l) for l in lines
]
with open(unattend_path, 'w') as unattend:
with open(unattend_path, 'w', encoding='utf-8') as unattend:
unattend.write(''.join(lines))
except IOError as e:
raise ActionError('Unable to set time zone in unattend.xml') from e
Expand Down
37 changes: 37 additions & 0 deletions glazier/lib/actions/sysprep_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,20 @@

"""Tests for glazier.lib.actions.sysprep."""

import builtins
from unittest import mock

from absl.testing import absltest
from glazier.lib import test_utils
from glazier.lib.actions import sysprep
from glazier.lib.config import files

_REAL_OPEN = builtins.open


def _open_with_cp1252_default(*args, **kwargs):
kwargs.setdefault('encoding', 'cp1252')
return _REAL_OPEN(*args, **kwargs)


UNATTEND_XML = r"""<?xml version='1.0' encoding='utf-8'?>
Expand Down Expand Up @@ -57,6 +66,34 @@

class SysprepTest(test_utils.GlazierTestCase):

def test_config_uses_utf8(self):
yaml_path = self.create_tempfile(file_path='utf8.yaml').full_path
with _REAL_OPEN(yaml_path, 'wb') as handle:
handle.write(b'name: caf\xc3\xa9')

with mock.patch('builtins.open', side_effect=_open_with_cp1252_default):
config = files.Read(yaml_path)

self.assertEqual(config['name'], 'caf\u00e9')

def test_unattend_uses_utf8(self):
unattend_path = self.create_tempfile(
file_path='utf8-unattend.xml', content=UNATTEND_XML).full_path
xml_data = UNATTEND_XML.encode('ascii').replace(
b'<RegisteredOwner>Employee</RegisteredOwner>',
b'<RegisteredOwner>cafe\xcc\x81</RegisteredOwner>')
with _REAL_OPEN(unattend_path, 'wb') as handle:
handle.write(xml_data)

st = sysprep.SetUnattendTimeZone([], mock.Mock())
with mock.patch('builtins.open', side_effect=_open_with_cp1252_default):
st._EditUnattend('Yakutsk Standard Time', unattend_path)

with _REAL_OPEN(unattend_path, 'rb') as handle:
updated_xml = handle.read().decode('utf-8')
self.assertIn('<RegisteredOwner>cafe\u0301</RegisteredOwner>', updated_xml)
self.assertIn('<TimeZone>Yakutsk Standard Time</TimeZone>', updated_xml)

@mock.patch('glazier.lib.buildinfo.BuildInfo', autospec=True)
def test_set_unattend_time_zone_edit_unattend(self, mock_buildinfo):

Expand Down
2 changes: 1 addition & 1 deletion glazier/lib/config/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def _YamlReader(path: str) -> Any:
The parsed content of the yaml file.
"""
try:
with open(path, 'r') as yaml_file:
with open(path, 'r', encoding='utf-8') as yaml_file:
yaml_config = yaml.safe_load(yaml_file)
except IOError as e:
raise FileReadError(path) from e
Expand Down
Loading