From 75dbefb378838c951fc9480bcab46e467a8e3ec0 Mon Sep 17 00:00:00 2001 From: Robin Liang Date: Thu, 13 Jun 2019 11:33:07 +1200 Subject: [PATCH 1/3] Normalise relative paths in compilation-db `compile_commands.json` could refer to files as relative path to the specified directory, which could be different to where the script is being executed from --- lint4jsondb.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lint4jsondb.py b/lint4jsondb.py index b2a5af0..85e0708 100644 --- a/lint4jsondb.py +++ b/lint4jsondb.py @@ -326,6 +326,8 @@ def write_defines(prefix): f.write("-%s%s\n" % (prefix, define)) def write_item(): + item.file = os.path.normpath( + os.path.join(item.directory, item.file)) f.write("\n\n// for: %s \n" % item.file) f.write("-save\n") @@ -334,6 +336,8 @@ def write_item(): for include in item.invocation.includes: if include not in includes: includes.append(include) + include = os.path.normpath( + os.path.join(item.directory, include)) f.write("-i\"%s\"\n" % include) write_defines('d') From 61c9e67b78b014979f628e06fde45a06208ffb41 Mon Sep 17 00:00:00 2001 From: Robin Liang Date: Thu, 13 Jun 2019 15:54:00 +1200 Subject: [PATCH 2/3] Move path normalization to the JSON process --- lint4jsondb.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/lint4jsondb.py b/lint4jsondb.py index 85e0708..a35f4f9 100644 --- a/lint4jsondb.py +++ b/lint4jsondb.py @@ -183,6 +183,12 @@ def finish(self): continue + # apply the directory to relative paths if needed + self.file = os.path.normpath(os.path.join(self.directory, self.file)) + self.invocation.includes = [ + os.path.normpath(os.path.join(self.directory, inc)) + for inc in self.invocation.includes] + class Lint4JsonCompilationDb: def __init__(self, compilation_db, include_only=set(), exclude_all=set()): @@ -317,7 +323,8 @@ def execute_with(self, arg, json_db): class ExecuteLintForAllFilesInOneInvocation: def __init__(self): - self._tmp_file = tempfile.NamedTemporaryFile(mode='w+', delete=False, suffix='.lnt') + self._tmp_file = tempfile.NamedTemporaryFile( + mode='w+', delete=False, suffix='.lnt') self._last_invocation = None def _create_temporary_lint_config(self, json_db): @@ -326,8 +333,6 @@ def write_defines(prefix): f.write("-%s%s\n" % (prefix, define)) def write_item(): - item.file = os.path.normpath( - os.path.join(item.directory, item.file)) f.write("\n\n// for: %s \n" % item.file) f.write("-save\n") @@ -336,8 +341,6 @@ def write_item(): for include in item.invocation.includes: if include not in includes: includes.append(include) - include = os.path.normpath( - os.path.join(item.directory, include)) f.write("-i\"%s\"\n" % include) write_defines('d') From 25897d89809f61dd5e6a4071e4a769e7964a73c4 Mon Sep 17 00:00:00 2001 From: Robin Liang Date: Thu, 13 Jun 2019 15:56:32 +1200 Subject: [PATCH 3/3] Fix TemporaryFile invocation, run test from script --- test_lint4jsondb.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/test_lint4jsondb.py b/test_lint4jsondb.py index 58b6a5a..9e70fee 100644 --- a/test_lint4jsondb.py +++ b/test_lint4jsondb.py @@ -19,7 +19,7 @@ def tearDown(self): os.remove(self._json_tested) def __create_temp_json(self, content): - with tempfile.TemporaryFile('wb', delete=False) as f: + with tempfile.NamedTemporaryFile('wb', delete=False) as f: self._json_tested = f.name f.write(content) @@ -264,7 +264,8 @@ def test_invocation(self, mock_call, mock_os_path_exists, mock_os_makedirs): 'stderr': -2, 'stdout': -1}) # ensure lint is called - self.assertEqual(args.pop(0), os.path.join("", "")) + self.assertEqual(args.pop(0), os.path.join( + "", "")) # ensure lint suppresses the banner line self.assertEqual(args.pop(0), '-b') # ensure lint's "lnt" directory was added @@ -280,3 +281,7 @@ def test_invocation(self, mock_call, mock_os_path_exists, mock_os_makedirs): self.assertEqual(args.pop(0), '-i"i2"') # ensure that the file to check is passed finally self.assertEqual(args.pop(0), "") + + +if __name__ == "__main__": + unittest.main()