Skip to content

Commit

Permalink
dev: add encoding to open() calls for cross-platform test compatibility
Browse files Browse the repository at this point in the history
- either missing before, or mistakenly removed in commit f491ea8
  • Loading branch information
lindsay-stevens committed Jan 17, 2024
1 parent 13f1601 commit 46edd80
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 19 deletions.
2 changes: 1 addition & 1 deletion pyxform/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def import_from_xml(self, xml_string_or_filename):
import os.path

if os.path.isfile(xml_string_or_filename):
xml_str = open(xml_string_or_filename).read()
xml_str = open(xml_string_or_filename, encoding="utf-8").read()
else:
xml_str = xml_string_or_filename
key_val_dict = parse_xform_instance(xml_str)
Expand Down
7 changes: 4 additions & 3 deletions pyxform/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ def xls_sheet_to_csv(workbook_path, csv_path, sheet_name):
return False
if not sheet or sheet.nrows < 2:
return False
with open(csv_path, "w", newline="") as f:
with open(csv_path, mode="w", encoding="utf-8", newline="") as f:
writer = csv.writer(f, quoting=csv.QUOTE_ALL)
mask = [v and len(v.strip()) > 0 for v in sheet.row_values(0)]
for row_idx in range(sheet.nrows):
Expand Down Expand Up @@ -185,7 +185,7 @@ def xlsx_sheet_to_csv(workbook_path, csv_path, sheet_name):
except KeyError:
return False

with open(csv_path, "w", newline="") as f:
with open(csv_path, mode="w", encoding="utf-8", newline="") as f:
writer = csv.writer(f, quoting=csv.QUOTE_ALL)
mask = [not is_empty(cell.value) for cell in sheet[1]]
for row in sheet.rows:
Expand Down Expand Up @@ -225,7 +225,8 @@ def get_languages_with_bad_tags(languages):
"""
Returns languages with invalid or missing IANA subtags.
"""
with open(os.path.join(os.path.dirname(__file__), "iana_subtags.txt")) as f:
path = os.path.join(os.path.dirname(__file__), "iana_subtags.txt")
with open(path, encoding="utf-8") as f:
iana_subtags = f.read().splitlines()

lang_code_regex = re.compile(r"\((.*)\)$")
Expand Down
8 changes: 4 additions & 4 deletions pyxform/validators/updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,15 @@ def _read_json(file_path):
Read the JSON file to a string.
"""
_UpdateHandler._check_path(file_path=file_path)
with open(file_path) as in_file:
with open(file_path, encoding="utf-8") as in_file:
return json.load(in_file)

@staticmethod
def _write_json(file_path, content):
"""
Save the JSON data to a file.
"""
with open(file_path, mode="w", newline="\n") as out_file:
with open(file_path, mode="w", encoding="utf-8", newline="\n") as out_file:
data = json.dumps(content, indent=2, sort_keys=True)
out_file.write(str(data))

Expand All @@ -114,7 +114,7 @@ def _read_last_check(file_path):
Read the .last_check file.
"""
_UpdateHandler._check_path(file_path=file_path)
with open(file_path) as in_file:
with open(file_path, encoding="utf-8") as in_file:
first_line = in_file.readline()
try:
last_check = datetime.strptime(first_line, UTC_FMT)
Expand All @@ -128,7 +128,7 @@ def _write_last_check(file_path, content):
"""
Write the .last_check file.
"""
with open(file_path, mode="w", newline="\n") as out_file:
with open(file_path, mode="w", encoding="utf-8", newline="\n") as out_file:
out_file.write(str(content.strftime(UTC_FMT)))

@staticmethod
Expand Down
2 changes: 1 addition & 1 deletion pyxform/xls2json.py
Original file line number Diff line number Diff line change
Expand Up @@ -1693,7 +1693,7 @@ def __init__(self, path_or_file, default_name=None):

def print_warning_log(self, warn_out_file):
# Open file to print warning log to.
warn_out = open(warn_out_file, "w")
warn_out = open(warn_out_file, mode="w", encoding="utf-8")
warn_out.write("\n".join(self._warnings))


Expand Down
2 changes: 1 addition & 1 deletion tests/test_external_instances_for_selects.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ def test_itemset_csv_generated_from_external_choices(self):
self.assertIn(log_msg, [r.message for r in log.records])
self.assertTrue(os.path.exists(itemsets_path))

with open(itemsets_path) as csv:
with open(itemsets_path, encoding="utf-8") as csv:
rows = csv.readlines()
# Should have the non-empty headers in the first row.
self.assertEqual('"list_name","name","state","city"\n', rows[0])
Expand Down
4 changes: 2 additions & 2 deletions tests/test_xform2json.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def test_try_parse_with_path(self):
"""Should return root node from XML file path."""
xml_path = os.path.join(test_output.PATH, "test_try_parse.xml")
self.tidy_file = xml_path
with open(xml_path, "w") as xml_file:
with open(xml_path, mode="w", encoding="utf-8") as xml_file:
xml_file.write(self.xml)
root = _try_parse(xml_path)
self.assertEqual("a", root.tag)
Expand All @@ -97,7 +97,7 @@ def test_try_parse_with_bad_file(self):
"""Should raise XMLSyntaxError: file exists but content is not valid."""
xml_path = os.path.join(test_output.PATH, "test_try_parse.xml")
self.tidy_file = xml_path
with open(xml_path, "w") as xml_file:
with open(xml_path, mode="w", encoding="utf-8") as xml_file:
xml_file.write("not valid xml :(")
with self.assertRaises(ParseError):
_try_parse(xml_path)
Expand Down
8 changes: 6 additions & 2 deletions tests/test_xls2json_xls.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ def test_simple_yes_or_no_question(self):
with open(output_path, mode="w", encoding="utf-8") as fp:
json.dump(x_results, fp=fp, ensure_ascii=False, indent=4)
# Compare with the expected output:
with open(expected_output_path) as expected, open(output_path) as observed:
with open(expected_output_path, encoding="utf-8") as expected, open(
output_path, encoding="utf-8"
) as observed:
self.assertEqual(json.load(expected), json.load(observed))

def test_hidden(self):
Expand Down Expand Up @@ -126,7 +128,9 @@ def test_table(self):
with open(output_path, mode="w", encoding="utf-8") as fp:
json.dump(x_results, fp=fp, ensure_ascii=False, indent=4)
# Compare with the expected output:
with open(expected_output_path) as expected, open(output_path) as observed:
with open(expected_output_path, encoding="utf-8") as expected, open(
output_path, encoding="utf-8"
) as observed:
self.assertEqual(json.load(expected), json.load(observed))

def test_choice_filter_choice_fields(self):
Expand Down
2 changes: 1 addition & 1 deletion tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def truncate_temp_files(temp_dir):
truncate_temp_files(f.path)
# Check still in temp directory
elif f.path.startswith(temp_root):
with open(f.path, mode="w") as _:
with open(f.path, mode="w", encoding="utf-8") as _:
pass


Expand Down
4 changes: 3 additions & 1 deletion tests/xform_test_case/test_attribute_columns.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,7 @@ def test_conversion(self):
survey.print_xform_to_file(self.output_path, warnings=warnings)
# print warnings
# Compare with the expected output:
with open(expected_output_path) as expected, open(self.output_path) as observed:
with open(expected_output_path, encoding="utf-8") as expected, open(
self.output_path, encoding="utf-8"
) as observed:
self.assertXFormEqual(expected.read(), observed.read())
12 changes: 9 additions & 3 deletions tests/xform_test_case/test_bugs.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ def test_conversion(self):
survey.print_xform_to_file(self.output_path, warnings=warnings)
# print warnings
# Compare with the expected output:
with open(expected_output_path) as expected, open(self.output_path) as observed:
with open(expected_output_path, encoding="utf-8") as expected, open(
self.output_path, encoding="utf-8"
) as observed:
self.assertXFormEqual(expected.read(), observed.read())


Expand All @@ -116,7 +118,9 @@ def test_conversion(self):
survey.print_xform_to_file(self.output_path, warnings=warnings)
# print warnings
# Compare with the expected output:
with open(expected_output_path) as expected, open(self.output_path) as observed:
with open(expected_output_path, encoding="utf-8") as expected, open(
self.output_path, encoding="utf-8"
) as observed:
self.assertXFormEqual(expected.read(), observed.read())


Expand All @@ -141,7 +145,9 @@ def test_conversion(self):
survey.print_xform_to_file(output_path, warnings=warnings)
# print warnings
# Compare with the expected output:
with open(expected_output_path) as expected, open(output_path) as observed:
with open(expected_output_path, encoding="utf-8") as expected, open(
output_path, encoding="utf-8"
) as observed:
self.assertXFormEqual(expected.read(), observed.read())


Expand Down

0 comments on commit 46edd80

Please sign in to comment.