Skip to content

Commit

Permalink
Enable cache for MISRA mutation test (commaai#1799)
Browse files Browse the repository at this point in the history
* work with 1 cache

* enable cache in mutation

* name to hash

* cleanup

---------

Co-authored-by: Adeeb Shihadeh <[email protected]>
  • Loading branch information
bongbui321 and adeebshihadeh authored Jan 15, 2024
1 parent 6ae65db commit d854abe
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
2 changes: 1 addition & 1 deletion tests/misra/test_misra.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ cd $PANDA_DIR
scons -j8

cppcheck() {
hashed_args=$(echo -n "$@" | md5sum | awk '{print $1}')
hashed_args=$(echo -n "$@$DIR" | md5sum | awk '{print $1}')
build_dir=/tmp/cppcheck_build/$hashed_args
mkdir -p $build_dir

Expand Down
32 changes: 19 additions & 13 deletions tests/misra/test_mutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import shutil
import subprocess
import tempfile
import hashlib

HERE = os.path.abspath(os.path.dirname(__file__))
ROOT = os.path.join(HERE, "../../")
Expand All @@ -23,19 +24,24 @@

@pytest.mark.parametrize("fn, patch, should_fail", mutations)
def test_misra_mutation(fn, patch, should_fail):
with tempfile.TemporaryDirectory() as tmp:
shutil.copytree(ROOT, tmp, dirs_exist_ok=True)

# apply patch
if fn is not None:
r = os.system(f"cd {tmp} && sed -i '{patch}' {fn}")
assert r == 0

# run test
r = subprocess.run("tests/misra/test_misra.sh", cwd=tmp, shell=True,
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
failed = r.returncode != 0
assert failed == should_fail
key = hashlib.md5((str(fn) + str(patch)).encode()).hexdigest()
tmp = os.path.join(tempfile.gettempdir(), key)

if os.path.exists(tmp):
shutil.rmtree(tmp)
shutil.copytree(ROOT, tmp)

# apply patch
if fn is not None:
r = os.system(f"cd {tmp} && sed -i '{patch}' {fn}")
assert r == 0

# run test
r = subprocess.run("tests/misra/test_misra.sh", cwd=tmp, shell=True)
failed = r.returncode != 0
assert failed == should_fail

shutil.rmtree(tmp)

if __name__ == "__main__":
pytest.main([__file__, "-n 4"])

0 comments on commit d854abe

Please sign in to comment.