|
3 | 3 |
|
4 | 4 | import pytest |
5 | 5 |
|
6 | | -from file_utils import load_linked_resources, store_response_files |
| 6 | +from diff_utils import get_code_diff |
| 7 | +from file_utils import get_existing_files_content, load_linked_resources, store_response_files |
7 | 8 | from plain2code_exceptions import UnsupportedBase64Content, UnsupportedResourceType |
8 | 9 | from plain2code_utils import MAX_BASE64_BLOB_LENGTH |
9 | 10 |
|
@@ -84,9 +85,64 @@ def test_load_linked_resources_real_base64_image_raises(template_dir): |
84 | 85 | def test_store_response_files_writes_unicode_as_utf8(template_dir): |
85 | 86 | # Content with a non-cp1252 character (📍 U+1F4CD) must be written as UTF-8 |
86 | 87 | # regardless of the platform's default text encoding (e.g. cp1252 on Windows). |
87 | | - content = "Location 📍 marker" |
| 88 | + content = "Location 📍 marker\n" |
88 | 89 | store_response_files(template_dir, {"notes.md": content}, []) |
89 | 90 |
|
90 | 91 | file_path = os.path.join(template_dir, "notes.md") |
91 | 92 | with open(file_path, "rb") as f: |
92 | 93 | assert f.read().decode("utf-8") == content |
| 94 | + |
| 95 | + |
| 96 | +def _read_bytes(folder, file_name): |
| 97 | + with open(os.path.join(folder, file_name), "rb") as f: |
| 98 | + return f.read().decode("utf-8") |
| 99 | + |
| 100 | + |
| 101 | +@pytest.mark.parametrize( |
| 102 | + "content, expected", |
| 103 | + [ |
| 104 | + ("class Foo\nend", "class Foo\nend\n"), |
| 105 | + ("class Foo\nend\n", "class Foo\nend\n"), |
| 106 | + ("class Foo\nend\n\n", "class Foo\nend\n\n"), |
| 107 | + ("class Foo\r\nend\r\n", "class Foo\r\nend\r\n"), |
| 108 | + ("", ""), |
| 109 | + ], |
| 110 | +) |
| 111 | +def test_store_response_files_ends_every_text_file_with_a_newline(template_dir, content, expected): |
| 112 | + # Linters gate on a final newline (RuboCop Layout/TrailingEmptyLines, ESLint eol-last, Ruff W292), so a |
| 113 | + # whole-file rewrite that comes back without one must not reach the disk that way. |
| 114 | + store_response_files(template_dir, {"app/foo.rb": content}, []) |
| 115 | + |
| 116 | + assert _read_bytes(template_dir, "app/foo.rb") == expected |
| 117 | + |
| 118 | + |
| 119 | +def test_store_response_files_hands_back_the_content_it_wrote(template_dir): |
| 120 | + # Callers diff the response files against the build folder after storing them (fix journal, conformance |
| 121 | + # fixes handed to the unit tests fixer), so the response files must match what is on disk. |
| 122 | + response_files = {"app/foo.rb": "class Foo\nend", "app/bar.rb": "class Bar\nend\n"} |
| 123 | + |
| 124 | + store_response_files(template_dir, response_files, []) |
| 125 | + |
| 126 | + assert response_files == {"app/foo.rb": "class Foo\nend\n", "app/bar.rb": "class Bar\nend\n"} |
| 127 | + |
| 128 | + |
| 129 | +def test_rewriting_a_file_without_its_final_newline_does_not_change_it(template_dir): |
| 130 | + existing_files = store_response_files(template_dir, {"app/foo.rb": "class Foo\nend\n"}, []) |
| 131 | + existing_files_content = get_existing_files_content(template_dir, existing_files) |
| 132 | + |
| 133 | + rewrite = {"app/foo.rb": "class Foo\nend"} |
| 134 | + store_response_files(template_dir, rewrite, existing_files) |
| 135 | + |
| 136 | + assert get_existing_files_content(template_dir, existing_files) == existing_files_content |
| 137 | + assert get_code_diff(rewrite, existing_files_content) == {} |
| 138 | + |
| 139 | + |
| 140 | +def test_store_response_files_still_deletes_a_file_whose_content_is_none(template_dir): |
| 141 | + existing_files = store_response_files(template_dir, {"app/foo.rb": "class Foo\nend\n"}, []) |
| 142 | + |
| 143 | + response_files = {"app/foo.rb": None} |
| 144 | + store_response_files(template_dir, response_files, existing_files) |
| 145 | + |
| 146 | + assert not os.path.exists(os.path.join(template_dir, "app/foo.rb")) |
| 147 | + assert existing_files == [] |
| 148 | + assert response_files == {"app/foo.rb": None} |
0 commit comments