Check type of `lines` parameter in `ScratchFile` Python test helper
Since strings are iterable in Python, accidentally passing a string to the `ScratchFile` test helper ends up writing one character per line, which leads to very confusing error messages.
Closes #16519.
PiperOrigin-RevId: 483372166
Change-Id: Ic01d9a1c1ebd7ad549b1275d3eae6666b4ef9abf
diff --git a/src/test/py/bazel/py_test.py b/src/test/py/bazel/py_test.py
index c75923b..c5cc3e4 100644
--- a/src/test/py/bazel/py_test.py
+++ b/src/test/py/bazel/py_test.py
@@ -124,7 +124,7 @@
' main = "bin.py",',
')',
])
- self.ScratchFile('bin.py', 'print("Hello, world")')
+ self.ScratchFile('bin.py', ['print("Hello, world")'])
exit_code, _, stderr = self.RunBazel(
['build', '--build_python_zip', '//:bin.v1'])
self.AssertExitCode(exit_code, 0, stderr)
diff --git a/src/test/py/bazel/test_base.py b/src/test/py/bazel/test_base.py
index dbffa83..35388aa 100644
--- a/src/test/py/bazel/test_base.py
+++ b/src/test/py/bazel/test_base.py
@@ -285,6 +285,8 @@
"""
if not path:
return
+ if lines is not None and not isinstance(lines, list):
+ raise ValueError('expected lines to be a list, got ' + str(type(lines)))
abspath = self.Path(path)
if os.path.exists(abspath) and not os.path.isfile(abspath):
raise IOError('"%s" (%s) exists and is not a file' % (path, abspath))