Commit e0d784b2 authored by Imdad Sardharwalla's avatar Imdad Sardharwalla Committed by GitHub Enterprise
Browse files

Don't change working directory in scan.py (#20)

Calculate the relative path instead. This should be more stable and less prone to error.
parent 64b257c0
......@@ -2,14 +2,16 @@ import os
import sys
from pathlib import Path
def get_file_extension(_filename):
return os.path.splitext(_filename)[1]
# Creates a list of tests to be be included in CMakeList.txt. If the test file
# already exists, read it in and compare it to the generated test list. Only
# write the generated test list if it differs from the existing list. This
# prevents CMake from reconfiguring if nothing has changed.
def make_test_list(_test_set, _output_dir, _filename, _cmd):
def make_test_list(_test_set, _input_dir, _output_dir, _filename, _cmd):
# Sort tests by file size (descending). Tests with larger file sizes are
# likely to take longer, and therefore should be run first to improve
# overall test time. Note: this is only relevant for the first test run, as
......@@ -21,11 +23,13 @@ def make_test_list(_test_set, _output_dir, _filename, _cmd):
# Generate a new test list with each line of the form:
# <cmd>(<file>)
# Note: CMake requires '/' as its path separator and thus replace all
# where <file> is relative to _input_dir.
# Note: CMake requires '/' as its path separator and thus we replace all
# instances of '\' with '/' in the path.
new_test_list = ''
for file in sorted_by_size:
new_test_list += _cmd + '(' + file.replace ('\\', '/') + ')\n'
relative_file = os.path.relpath(file, _input_dir)
new_test_list += _cmd + '(' + relative_file.replace ('\\', '/') + ')\n'
# Remove some issues with the final EOL characters
new_test_list = new_test_list.rstrip()
......@@ -71,10 +75,6 @@ def scan(_input_dir, _output_dir,
if not Path(_output_dir).is_dir():
raise ValueError('Error: invalid output directory ("' + _output_dir + '").')
# Change working directory to _input_dir
initial_wd = os.getcwd()
os.chdir(_input_dir)
print("Scanning for tests...", end='', flush=True)
ignored_tests = set()
......@@ -82,20 +82,17 @@ def scan(_input_dir, _output_dir,
other_tests_all = set()
# Scan all test input directory files and sort them into different sets.
# Note: all paths are relative to the current working directory, and thus
# will start with either './' (on Unix) or '.\' (on Windows). We remove
# these characters before storing the path.
for p, d, f in os.walk('.'):
for p, d, f in os.walk(_input_dir):
for file in f:
if file.endswith('.ignore'):
# Tests to be ignored (remove .ignore suffix before storing)
ignored_tests.add(os.path.join(p, file[:-7])[2:])
ignored_tests.add(os.path.join(p, file[:-7]))
elif get_file_extension(file) in _cpp_exts:
# C++ tests
cpp_tests_all.add(os.path.join(p, file)[2:])
cpp_tests_all.add(os.path.join(p, file))
elif get_file_extension(file) in _other_exts:
# Any other tests
other_tests_all.add(os.path.join(p, file)[2:])
other_tests_all.add(os.path.join(p, file))
print(" ...complete.")
......@@ -108,8 +105,5 @@ def scan(_input_dir, _output_dir,
print("Number of other tests =", len(other_tests))
print("Total number of tests =", len(cpp_tests) + len(other_tests))
make_test_list(cpp_tests, _output_dir, _cpp_out_filename, _cpp_cmd)
make_test_list(other_tests, _output_dir, _other_out_filename, _other_cmd)
# Return to initial working directory
os.chdir(initial_wd)
make_test_list(cpp_tests, _input_dir, _output_dir, _cpp_out_filename, _cpp_cmd)
make_test_list(other_tests, _input_dir, _output_dir, _other_out_filename, _other_cmd)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment