Detect test functions correctly with customized `IFS` variable.
Set `IFS` variable explicitly for `read` to successfully detect the test functions even if the variable was changed globally by the test.
PiperOrigin-RevId: 420356425
diff --git a/src/test/shell/unittest.bash b/src/test/shell/unittest.bash
index 75b97d1..b920b54 100644
--- a/src/test/shell/unittest.bash
+++ b/src/test/shell/unittest.bash
@@ -517,7 +517,7 @@
[ "$TEST_SHARD_INDEX" -lt 0 -o "$TEST_SHARD_INDEX" -ge "$TEST_TOTAL_SHARDS" ] &&
{ echo "Invalid shard $shard_index" >&2; exit 1; }
- read -rd $'\0' -a TESTS < <(
+ IFS=$'\n' read -rd $'\0' -a TESTS < <(
for test in "${TESTS[@]}"; do echo "$test"; done |
awk "NR % $TEST_TOTAL_SHARDS == $TEST_SHARD_INDEX" && echo -en '\0')
@@ -669,7 +669,7 @@
if [ ${#TESTS[@]} -eq 0 ]; then
# Even if there aren't any tests, this needs to succeed.
local all_tests=()
- read -d $'\0' -ra all_tests < <(
+ IFS=$'\n' read -d $'\0' -ra all_tests < <(
declare -F | awk '{print $3}' | grep ^test_ || true; echo -en '\0')
if (( "${#_TEST_FILTERS[@]}" == 0 )); then
diff --git a/src/test/shell/unittest_test.py b/src/test/shell/unittest_test.py
index 1314290..b2e2eb6 100644
--- a/src/test/shell/unittest_test.py
+++ b/src/test/shell/unittest_test.py
@@ -560,6 +560,32 @@
"WARNING: Both --test_arg and --test_filter specified, ignoring --test_arg"
)
+ def test_custom_ifs_variable_finds_and_runs_test(self):
+ for sharded in (False, True):
+ with self.subTest(sharded=sharded):
+ self.__custom_ifs_variable_finds_and_runs_test(sharded)
+
+ def __custom_ifs_variable_finds_and_runs_test(self, sharded):
+ self.write_file(
+ "thing.sh",
+ textwrap.dedent(r"""
+ IFS=$'\t'
+ function test_foo() {
+ :
+ }
+
+ run_suite "custom IFS test"
+ """))
+
+ result = self.execute_test(
+ "thing.sh",
+ env={} if not sharded else {
+ "TEST_TOTAL_SHARDS": 2,
+ "TEST_SHARD_INDEX": 1
+ })
+ result.assertSuccess("custom IFS test")
+ result.assertTestPassed("test_foo")
+
if __name__ == "__main__":
unittest.main()