Skip to content

Commit 7cfe15c

Browse files
authored
Merge pull request github#12205 from github/redsun82/swift-codegen
Swift: fix subtle `codegen` bug on missing files
2 parents 430b432 + 3ec2a3c commit 7cfe15c

File tree

5 files changed

+33
-9
lines changed

5 files changed

+33
-9
lines changed

swift/codegen/lib/render.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ def __enter__(self):
106106
def __exit__(self, exc_type, exc_val, exc_tb):
107107
if exc_val is None:
108108
for f in self._existing - self._skipped - self.written:
109-
self._hashes.pop(self._get_path(f), None)
110109
f.unlink(missing_ok=True)
111110
log.info(f"removed {f.name}")
112111
for f in self.written:
@@ -116,6 +115,10 @@ def __exit__(self, exc_type, exc_val, exc_tb):
116115
# so that they get the chance to be regenerated again during the next run
117116
for f in self.written:
118117
self._hashes.pop(self._get_path(f), None)
118+
# clean up the registry from files that do not exist any more
119+
for f in list(self._hashes):
120+
if not (self._swift_dir / f).exists():
121+
self._hashes.pop(f)
119122
self._dump_registry()
120123

121124
def _do_write(self, mnemonic: str, contents: str, output: pathlib.Path):

swift/codegen/test/test_render.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,23 +214,40 @@ class MyError(Exception):
214214
pass
215215

216216

217-
def test_managed_render_exception_drops_written_from_registry(pystache_renderer, sut):
217+
def test_managed_render_exception_drops_written_and_inexsistent_from_registry(pystache_renderer, sut):
218218
data = mock.Mock(spec=("template",))
219219
text = "some text"
220220
pystache_renderer.render_name.side_effect = (text,)
221221
output = paths.swift_dir / "some/output.txt"
222-
registry = paths.swift_dir / "a/registry.list"
222+
registry = paths.swift_dir / "x/registry.list"
223223
write(output, text)
224+
write(paths.swift_dir / "a")
225+
write(paths.swift_dir / "c")
224226
write(registry, "a a a\n"
225227
f"some/output.txt whatever {hash(text)}\n"
226-
"b b b")
228+
"b b b\n"
229+
"c c c")
227230

228231
with pytest.raises(MyError):
229232
with sut.manage(generated=(), stubs=(), registry=registry) as renderer:
230233
renderer.render(data, output)
231234
raise MyError
232235

233-
assert_file(registry, "a a a\nb b b\n")
236+
assert_file(registry, "a a a\nc c c\n")
237+
238+
239+
def test_managed_render_drops_inexsistent_from_registry(pystache_renderer, sut):
240+
registry = paths.swift_dir / "x/registry.list"
241+
write(paths.swift_dir / "a")
242+
write(paths.swift_dir / "c")
243+
write(registry, f"a {hash('')} {hash('')}\n"
244+
"b b b\n"
245+
f"c {hash('')} {hash('')}")
246+
247+
with sut.manage(generated=(), stubs=(), registry=registry):
248+
pass
249+
250+
assert_file(registry, f"a {hash('')} {hash('')}\nc {hash('')} {hash('')}\n")
234251

235252

236253
def test_managed_render_exception_does_not_erase(pystache_renderer, sut):

swift/ql/.generated.list

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -800,10 +800,6 @@ ql/test/extractor-tests/generated/expr/InterpolatedStringLiteralExpr/MISSING_SOU
800800
ql/test/extractor-tests/generated/expr/IsExpr/MISSING_SOURCE.txt 7e714762ffb48c436102027d560fb5addc1f7dc6dd6936b06e0d3cca031d67fd 7e714762ffb48c436102027d560fb5addc1f7dc6dd6936b06e0d3cca031d67fd
801801
ql/test/extractor-tests/generated/expr/KeyPathApplicationExpr/MISSING_SOURCE.txt 7e714762ffb48c436102027d560fb5addc1f7dc6dd6936b06e0d3cca031d67fd 7e714762ffb48c436102027d560fb5addc1f7dc6dd6936b06e0d3cca031d67fd
802802
ql/test/extractor-tests/generated/expr/KeyPathDotExpr/MISSING_SOURCE.txt 7e714762ffb48c436102027d560fb5addc1f7dc6dd6936b06e0d3cca031d67fd 7e714762ffb48c436102027d560fb5addc1f7dc6dd6936b06e0d3cca031d67fd
803-
ql/test/extractor-tests/generated/expr/KeyPathExpr/KeyPathExpr.ql 088e9955681f87045f77ce1acd8ccf24d9a418c0b200dc8936511776dda8c8dd a35eb69033c53fd4a986d02b10ec52ac192ea8a5ce7479f191427a329b4704df
804-
ql/test/extractor-tests/generated/expr/KeyPathExpr/KeyPathExpr_getParsedPath.ql 5f88eaba63c02a42c6ed2093cba9d442f5a3595a5e0888312f51e196c0471cfc b30b8ca1894143a2e0c334e9c32b29c50d13ef5bc94d62d338e019d9e0caeca5
805-
ql/test/extractor-tests/generated/expr/KeyPathExpr/KeyPathExpr_getRoot.ql 61d8d0f50c62e6bdf98005609861f6f4fd16e59c439706abf03ba27f87ed3cb1 403ee884bb83b7a4207993afbda7964e676f5f64923ce11e65a0cf8bd199e01d
806-
ql/test/extractor-tests/generated/expr/KeyPathExpr/KeyPathExpr_getType.ql 992497671107be454ffe1f42b513a5bca37bd31849587ad55f6bd87d8ac5d4a7 b51109f0d9e5e6238d8ab9e67f24d435a873a7884308c4f01ec4ecad51ed031d
807803
ql/test/extractor-tests/generated/expr/KeyPathExpr/MISSING_SOURCE.txt 7e714762ffb48c436102027d560fb5addc1f7dc6dd6936b06e0d3cca031d67fd 7e714762ffb48c436102027d560fb5addc1f7dc6dd6936b06e0d3cca031d67fd
808804
ql/test/extractor-tests/generated/expr/LazyInitializerExpr/MISSING_SOURCE.txt 7e714762ffb48c436102027d560fb5addc1f7dc6dd6936b06e0d3cca031d67fd 7e714762ffb48c436102027d560fb5addc1f7dc6dd6936b06e0d3cca031d67fd
809805
ql/test/extractor-tests/generated/expr/MagicIdentifierLiteralExpr/MISSING_SOURCE.txt 7e714762ffb48c436102027d560fb5addc1f7dc6dd6936b06e0d3cca031d67fd 7e714762ffb48c436102027d560fb5addc1f7dc6dd6936b06e0d3cca031d67fd
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// generated by codegen/codegen.py
2+
3+
After a swift source file is added in this directory and codegen/codegen.py is run again, test queries
4+
will appear and this file will be deleted
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// generated by codegen/codegen.py
2+
3+
After a swift source file is added in this directory and codegen/codegen.py is run again, test queries
4+
will appear and this file will be deleted

0 commit comments

Comments
 (0)