tests: add test to ensure rust test scope
Test to ensure src files containing the rust #[test] attribute have a
module named "tests" defined.
@@ -32,6 +32,43 @@ KCOV_TOTAL_LINES_REGEX = r'"total_lines" : "(\d+)"'
"""Regex for extracting number of total executable lines found by kcov."""
+@pytest.mark.timeout(120)
+@pytest.mark.skipif(
+ platform.machine() != "x86_64",
+ reason="no need to test it on multiple platforms"
+)
+def test_ensure_mod_tests():
+ """Check that files containing unit tests have a 'tests' module defined."""
+ # List all source files containing rust #[test] attribute,
+ # (excluding generated files and integration test directories).
+ # Take the list and check each file contains 'mod tests {', output file
+ # name if it doesn't.
+ cmd = (
+ '/bin/bash '
+ '-c '
+ '"grep '
+ '--files-without-match '
+ '\'mod tests {\' '
+ '\\$(grep '
+ '--files-with-matches '
+ '--recursive '
+ '--exclude-dir=src/*_gen/* '
+ '\'\\#\\[test\\]\' ../src/*/src)" '
+ )
+
+ # The outer grep returns 0 even if it finds files without the match, so we
+ # ignore the return code.
+ result = utils.run_cmd(cmd, no_shell=False, ignore_return_code=True)
+
+ error_msg = (
+ 'Tests found in files without a "tests" module:\n {}'
+ 'To ensure code coverage is reported correctly, please check that '
+ 'your tests are in a module named "tests".'.format(result.stdout)
+ )
+
+ assert not result.stdout, error_msg
+
+
@pytest.mark.timeout(400)
@pytest.mark.skipif(
platform.machine() != "x86_64",
