Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion tools/stubgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,11 @@ def write_outputs(outputs):
temporary.replace(path)
finally:
for temporary, path in staged:
temporary.unlink(missing_ok=True)
try:
temporary.unlink(missing_ok=True)
except PermissionError:
temporary.chmod(0o600)
temporary.unlink(missing_ok=True)


def main(argv=None):
Expand Down
39 changes: 39 additions & 0 deletions tools/tests/test_stubgen.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import contextlib
import io
import os
import stat
import tempfile
from pathlib import Path
import sys
Expand Down Expand Up @@ -121,5 +122,43 @@ def test_valid_generation_keeps_the_code_and_debt(self):
self.assertEqual(code.stat().st_mode & 0o777, 0o666 & ~mask)


class ReadOnlyOutputTests(unittest.TestCase):
def test_later_staging_failure_preserves_original_error_and_cleans_readonly_temporary(self):
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
nids, output = root / "nids.txt", root / "Export.cpp"
nids.write_text("AAAAAAAAAAA\n", encoding="utf-8")
original = "existing source\n"
output.write_text(original, encoding="utf-8")
output.chmod(0o444)
errors = io.StringIO()
arguments = ["--module", "libSceExample", "--nids", str(nids), "--out", str(output),
"--td-out", str(root / "absent" / "debt.txt")]
with contextlib.redirect_stderr(errors), self.assertRaises(SystemExit) as error:
stubgen.main(arguments)
self.assertEqual(error.exception.code, 2)
self.assertIn("absent", errors.getvalue())
self.assertNotIn("Export.cpp.", errors.getvalue())
self.assertEqual(output.read_text(encoding="utf-8"), original)
self.assertFalse(output.stat().st_mode & stat.S_IWRITE)
self.assertEqual(sorted(path.name for path in root.iterdir()), ["Export.cpp", "nids.txt"])

@unittest.skipUnless(os.name == "nt", "Windows denies replacing read-only output files")
def test_denied_replace_preserves_readonly_output_without_leaking_temporary(self):
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
nids, output = root / "nids.txt", root / "Export.cpp"
nids.write_text("AAAAAAAAAAA\n", encoding="utf-8")
original = "existing source\n"
output.write_text(original, encoding="utf-8")
output.chmod(0o444)
with contextlib.redirect_stderr(io.StringIO()), self.assertRaises(SystemExit) as error:
stubgen.main(["--module", "libSceExample", "--nids", str(nids), "--out", str(output)])
self.assertEqual(error.exception.code, 2)
self.assertEqual(output.read_text(encoding="utf-8"), original)
self.assertFalse(output.stat().st_mode & stat.S_IWRITE)
self.assertEqual(sorted(path.name for path in root.iterdir()), ["Export.cpp", "nids.txt"])


if __name__ == "__main__":
unittest.main()
Loading