| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
import io |
| 5 |
import os |
| 6 |
import tempfile |
| 7 |
import unittest |
| 8 |
from contextlib import redirect_stdout |
| 9 |
|
| 10 |
from common import find_target_items, reverse_items, sort_items |
| 11 |
|
| 12 |
|
| 13 |
class CommonScriptTest(unittest.TestCase): |
| 14 |
def run_file_op(self, func, input_text, *, input_bytes=None): |
| 15 |
with tempfile.TemporaryDirectory() as tmpdir: |
| 16 |
src = os.path.join(tmpdir, "in.txt") |
| 17 |
dst = os.path.join(tmpdir, "out.txt") |
| 18 |
|
| 19 |
if input_bytes is not None: |
| 20 |
with open(src, "wb") as f: |
| 21 |
f.write(input_bytes) |
| 22 |
else: |
| 23 |
with open(src, "w", encoding="utf-8") as f: |
| 24 |
f.write(input_text) |
| 25 |
|
| 26 |
func(src, dst) |
| 27 |
|
| 28 |
with open(dst, "rb") as f: |
| 29 |
return f.read() |
| 30 |
|
| 31 |
def test_sort_entries(self): |
| 32 |
out = self.run_file_op(sort_items, "b\tB\na\tA\n") |
| 33 |
self.assertEqual(out.decode("utf-8"), "a\tA\nb\tB\n") |
| 34 |
|
| 35 |
def test_sort_moves_comment_with_following_entry(self): |
| 36 |
out = self.run_file_op(sort_items, "b\tB\n# comment for a\na\tA\n") |
| 37 |
self.assertEqual( |
| 38 |
out.decode("utf-8"), |
| 39 |
"# comment for a\na\tA\nb\tB\n", |
| 40 |
) |
| 41 |
|
| 42 |
def test_sort_preserves_blank_lines_in_anchored_block(self): |
| 43 |
out = self.run_file_op(sort_items, "b\tB\n\n# comment for a\na\tA\n") |
| 44 |
self.assertEqual( |
| 45 |
out.decode("utf-8"), |
| 46 |
"\n# comment for a\na\tA\nb\tB\n", |
| 47 |
) |
| 48 |
|
| 49 |
def test_sort_splits_header_from_first_entry_comment(self): |
| 50 |
out = self.run_file_op( |
| 51 |
sort_items, |
| 52 |
"# Header\n\n# comment for b\nb\tB\na\tA\n", |
| 53 |
) |
| 54 |
self.assertEqual( |
| 55 |
out.decode("utf-8"), |
| 56 |
"# Header\n\na\tA\n# comment for b\nb\tB\n", |
| 57 |
) |
| 58 |
|
| 59 |
def test_sort_keeps_footer_at_end(self): |
| 60 |
out = self.run_file_op(sort_items, "b\tB\na\tA\n\n# footer\n") |
| 61 |
self.assertEqual( |
| 62 |
out.decode("utf-8"), |
| 63 |
"a\tA\nb\tB\n\n# footer\n", |
| 64 |
) |
| 65 |
|
| 66 |
def test_sort_preserves_missing_final_newline(self): |
| 67 |
out = self.run_file_op( |
| 68 |
sort_items, |
| 69 |
"", |
| 70 |
input_bytes=b"b\tB\na\tA", |
| 71 |
) |
| 72 |
self.assertEqual(out, b"a\tA\nb\tB") |
| 73 |
|
| 74 |
def test_sort_normalizes_crlf_input(self): |
| 75 |
out = self.run_file_op( |
| 76 |
sort_items, |
| 77 |
"", |
| 78 |
input_bytes=b"b\tB\r\na\tA\r\n", |
| 79 |
) |
| 80 |
self.assertEqual(out, b"a\tA\nb\tB\n") |
| 81 |
|
| 82 |
def test_reverse_items(self): |
| 83 |
out = self.run_file_op(reverse_items, "A\tx y\nB\tx\n") |
| 84 |
self.assertEqual( |
| 85 |
out.decode("utf-8"), |
| 86 |
"x\tA B\ny\tA\n", |
| 87 |
) |
| 88 |
|
| 89 |
def test_reverse_items_applies_reverse_preference(self): |
| 90 |
out = self.run_file_op( |
| 91 |
reverse_items, |
| 92 |
"# @reverse-prefer: x B\nA\tx\nB\tx\n", |
| 93 |
) |
| 94 |
self.assertEqual(out.decode("utf-8"), "x\tB A\n") |
| 95 |
|
| 96 |
def test_reverse_items_uses_key_as_default_reverse_preference(self): |
| 97 |
out = self.run_file_op( |
| 98 |
reverse_items, |
| 99 |
"# @reverse-prefer: x\nx\tx\nA\tx\n", |
| 100 |
) |
| 101 |
self.assertEqual(out.decode("utf-8"), "x\tx A\n") |
| 102 |
|
| 103 |
def test_reverse_items_rejects_unmatched_reverse_preference(self): |
| 104 |
with self.assertRaises(ValueError): |
| 105 |
self.run_file_op( |
| 106 |
reverse_items, |
| 107 |
"# @reverse-prefer: x C\nA\tx\nB\tx\n", |
| 108 |
) |
| 109 |
|
| 110 |
def test_find_target_items_matches_values_only(self): |
| 111 |
with tempfile.TemporaryDirectory() as tmpdir: |
| 112 |
src = os.path.join(tmpdir, "in.txt") |
| 113 |
with open(src, "w", encoding="utf-8") as f: |
| 114 |
f.write("foo\tbar baz\n") |
| 115 |
f.write("target\txxx\n") |
| 116 |
f.write("abc\ttarget-value\n") |
| 117 |
|
| 118 |
buf = io.StringIO() |
| 119 |
with redirect_stdout(buf): |
| 120 |
find_target_items(src, "target") |
| 121 |
|
| 122 |
self.assertEqual(buf.getvalue(), "abc\ttarget-value\n") |
| 123 |
|
| 124 |
|
| 125 |
if __name__ == "__main__": |
| 126 |
unittest.main() |