From ae76d3d32ffea4213638184e56802a4f3c5e98db Mon Sep 17 00:00:00 2001
From: Dennis <dennis.glaeser@iws.uni-stuttgart.de>
Date: Fri, 21 May 2021 11:37:27 +0200
Subject: [PATCH] [bin][findtests] make functions accept build-dir

---
 bin/testing/findtests.py | 35 +++++++++++++++++------------------
 1 file changed, 17 insertions(+), 18 deletions(-)

diff --git a/bin/testing/findtests.py b/bin/testing/findtests.py
index 81550ff835..249dd40c29 100755
--- a/bin/testing/findtests.py
+++ b/bin/testing/findtests.py
@@ -25,10 +25,10 @@ def hasCommonMember(myset, mylist):
 
 
 # make dry run and return the compilation command
-def getCompileCommand(testConfig):
-    lines = subprocess.check_output(["make", "--dry-run",
-                                     testConfig["target"]],
-                                    encoding='ascii').splitlines()
+def getCompileCommand(testConfig, buildTreeRoot='.'):
+    lines = subprocess.check_output(["make", "--dry-run", testConfig["target"]],
+                                    encoding='ascii',
+                                    cwd=buildTreeRoot).splitlines()
 
     def hasCppCommand(line):
         return any(cpp in line for cpp in ['g++', 'clang++'])
@@ -39,8 +39,8 @@ def getCompileCommand(testConfig):
 
 
 # get the command and folder to compile the given test
-def buildCommandAndDir(testConfig, cache):
-    compCommand = getCompileCommand(testConfig)
+def buildCommandAndDir(testConfig, cache, buildTreeRoot='.'):
+    compCommand = getCompileCommand(testConfig, buildTreeRoot)
     if compCommand is None:
         with open(cache) as c:
             data = json.load(c)
@@ -53,12 +53,13 @@ def buildCommandAndDir(testConfig, cache):
 
 
 # check if a test is affected by changes in the given files
-def isAffectedTest(testConfigFile, changedFiles):
+def isAffectedTest(testConfigFile, changedFiles, buildTreeRoot='.'):
     with open(testConfigFile) as configFile:
         testConfig = json.load(configFile)
 
     cacheFile = "TestTargets/" + testConfig["target"] + ".json"
-    command, dir = buildCommandAndDir(testConfig, cacheFile)
+    cacheFile = os.path.join(buildTreeRoot, cacheFile)
+    command, dir = buildCommandAndDir(testConfig, cacheFile, buildTreeRoot)
     mainFile = command[-1]
 
     # detect headers included in this test
@@ -94,27 +95,27 @@ if __name__ == '__main__':
                         help='The path to the top-level build directory of the project to be checked')
     args = vars(parser.parse_args())
 
+    buildDir = os.path.abspath(args['build_dir'])
     targetFile = os.path.abspath(args['outfile'])
     with open(args['file_list']) as files:
         changedFiles = set([line.strip('\n') for line in files.readlines()])
 
-    owd = os.getcwd()
-    os.chdir(args['build_dir'])
-
     # clean build directory
-    subprocess.run(["make", "clean"])
-    subprocess.run(["make"])
+    subprocess.run(["make", "clean"], cwd=buildDir)
+    subprocess.run(["make", "all"], cwd=buildDir)
 
     # create cache folder
-    os.makedirs("TestTargets", exist_ok=True)
+    os.makedirs(os.path.join(buildDir, "TestTargets"), exist_ok=True)
 
     # detect affected tests
     print("Detecting affected tests:")
     affectedTests = {}
-    tests = glob("TestMetaData/*json")
+    tests = glob(os.path.join(buildDir, "TestMetaData") + "/*json")
 
     numProcesses = max(1, args['num_processes'])
-    findAffectedTest = partial(isAffectedTest, changedFiles=changedFiles)
+    findAffectedTest = partial(isAffectedTest,
+                               changedFiles=changedFiles,
+                               buildTreeRoot=buildDir)
     with Pool(processes=numProcesses) as p:
         for affected, name, target in p.imap_unordered(findAffectedTest, tests, chunksize=4):
             if affected:
@@ -125,5 +126,3 @@ if __name__ == '__main__':
 
     with open(targetFile, 'w') as jsonFile:
         json.dump(affectedTests, jsonFile)
-
-    os.chdir(owd)
-- 
GitLab