Skip to content

Commit a9bd784

Browse files
committed
Swift: fix stub revert prevention
It turns out the threshold of 5 lines for stub modification detection was too strict: in case of a long class name the QL formatter will put the closing brace of the empty class definition on a new line, leading to codegen fail with an error thinking the stub was modified. On the other side of things, also adding a base to a stub class was not being detected as a modification. Now the modification test is slightly smarter. If the stub still marked as generated and * has more than 6 lines, or * the contents does not match a regexp aproximation of a plain stub then codegen will abort. The test will still avoid reading the whole contents of all the stubs.
1 parent eb1b3f8 commit a9bd784

File tree

2 files changed

+3
-3
lines changed

2 files changed

+3
-3
lines changed

swift/codegen/generators/qlgen.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ def _is_generated_stub(file, check_modification=False):
103103
# no lines
104104
return False
105105
if check_modification:
106-
# one line already read, if we can read 4 other we are past the normal stub generation
107-
line_threshold = 4
106+
# one line already read, if we can read 5 other we are past the normal stub generation
107+
line_threshold = 5
108108
if sum(1 for _ in zip(range(line_threshold), contents)) == line_threshold:
109109
raise ModifiedStubMarkedAsGeneratedError(
110110
f"{file.name} stub was modified but is still marked as generated")

swift/codegen/test/test_qlgen.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ def test_non_empty_cleanup(opts, generate, renderer):
378378

379379
def test_modified_stub_still_generated(qlgen_opts, renderer):
380380
stub = qlgen_opts.ql_stub_output / "A.qll"
381-
write(stub, "// generated\n\n\n\nsomething\n")
381+
write(stub, "// generated\nprivate import foo\n\nclass Bar extends BarBase {\n int x() { none() }\n}\n")
382382
with pytest.raises(qlgen.ModifiedStubMarkedAsGeneratedError):
383383
run_generation(qlgen.generate, qlgen_opts, renderer)
384384

0 commit comments

Comments
 (0)