unit-test: cppcheck: use temporary directory

The previous code would create a directory named `cppcheck-temp`
in the root of the repository under test as the cppcheck cache but
never cleaned it up.  This left stray directories around when using
the unit test locally.

Switch the mechanism to use tempfile.TemporaryDirectory so that
the directory is automatically cleaned up when the testing is
complete.

Tested: Ran unit-test locally and observed cppcheck-temp directory
no longer remains[1].

1. https://github.com/williamspatrick/dotfiles/blob/840250f0c56b299a8de47cc2ea7c558a14eeb517/env/30_linux/lfopenbmc.zsh#L76

Signed-off-by: Patrick Williams <patrick@stwcx.xyz>
Change-Id: Idde8feb28bef663d156c5d1d1f21a3d8c629c72d
diff --git a/scripts/unit-test.py b/scripts/unit-test.py
index aaa61dc..d1b8ef9 100755
--- a/scripts/unit-test.py
+++ b/scripts/unit-test.py
@@ -16,6 +16,7 @@
 from mesonbuild.mesonlib import version_compare as meson_version_compare
 from urllib.parse import urljoin
 from subprocess import check_call, call, CalledProcessError
+from tempfile import TemporaryDirectory
 import os
 import sys
 import argparse
@@ -373,28 +374,25 @@
     if not os.path.exists(os.path.join("build", "compile_commands.json")):
         return None
 
-    try:
-        os.mkdir("cppcheck-temp")
-    except FileExistsError as e:
-        pass
+    with TemporaryDirectory() as cpp_dir:
 
-    # http://cppcheck.sourceforge.net/manual.pdf
-    try:
-        check_call_cmd(
-            'cppcheck',
-            '-j', str(multiprocessing.cpu_count()),
-            '--enable=style,performance,portability,missingInclude',
-            '--suppress=useStlAlgorithm',
-            '--suppress=unusedStructMember',
-            '--suppress=postfixOperator',
-            '--suppress=unreadVariable',
-            '--suppress=knownConditionTrueFalse',
-            '--library=googletest',
-            '--project=build/compile_commands.json',
-            '--cppcheck-build-dir=cppcheck-temp',
-        )
-    except subprocess.CalledProcessError:
-        print("cppcheck found errors")
+        # http://cppcheck.sourceforge.net/manual.pdf
+        try:
+            check_call_cmd(
+                'cppcheck',
+                '-j', str(multiprocessing.cpu_count()),
+                '--enable=style,performance,portability,missingInclude',
+                '--suppress=useStlAlgorithm',
+                '--suppress=unusedStructMember',
+                '--suppress=postfixOperator',
+                '--suppress=unreadVariable',
+                '--suppress=knownConditionTrueFalse',
+                '--library=googletest',
+                '--project=build/compile_commands.json',
+                f'--cppcheck-build-dir={cpp_dir}',
+            )
+        except subprocess.CalledProcessError:
+            print("cppcheck found errors")
 
 
 def is_valgrind_safe():