def test_creates_directory(self): dir_name = None with sut.temporary_directory() as tmpdir: self.assertTrue(os.path.isdir(tmpdir)) dir_name = tmpdir self.assertIsNotNone(dir_name) self.assertFalse(os.path.exists(dir_name))
def run_crash_parse(content, prefix): with libear.temporary_directory() as tmp_dir: file_name = os.path.join(tmp_dir, prefix + '.info.txt') with open(file_name, 'w') as handle: lines = (line + os.linesep for line in content) handle.writelines(lines) return sut.parse_crash(file_name)
def test_parse_real_crash(self): import libscanbuild.analyze as sut2 with libear.temporary_directory() as tmp_dir: filename = os.path.join(tmp_dir, 'test.c') with open(filename, 'w') as handle: handle.write('int main() { return 0') # produce failure report opts = { 'clang': 'clang', 'directory': os.getcwd(), 'flags': [], 'source': filename, 'output_dir': tmp_dir, 'language': 'c', 'error_output': 'some output', 'exit_code': 13 } sut2.report_failure(opts) # verify crashes = list(sut.Crash.read(tmp_dir)) self.assertEqual(1, len(crashes)) crash = crashes[0] self.assertEqual(filename, crash.source) self.assertEqual('Other Error', crash.problem) self.assertEqual(crash.file + '.info.txt', crash.info) self.assertEqual(crash.file + '.stderr.txt', crash.stderr)
def test_directory_name_comparison(self): with libear.temporary_directory() as tmp_dir, \ sut.report_directory(tmp_dir, False) as report_dir1, \ sut.report_directory(tmp_dir, False) as report_dir2, \ sut.report_directory(tmp_dir, False) as report_dir3: self.assertLess(report_dir1, report_dir2) self.assertLess(report_dir2, report_dir3)
def test_parse_real_crash(self): import libscanbuild.analyze as sut2 with libear.temporary_directory() as tmp_dir: filename = os.path.join(tmp_dir, 'test.c') with open(filename, 'w') as handle: handle.write('int main() { return 0') # produce failure report opts = { 'clang': 'clang', 'directory': os.getcwd(), 'flags': [], 'source': filename, 'output_dir': tmp_dir, 'language': 'c', 'error_output': 'some output', 'exit_code': 13 } sut2.report_failure(opts) # verify crashes = list(sut.Crash.read(tmp_dir)) self.assertEqual(1, len(crashes)) crash = crashes[0] self.assertEqual(filename, crash.source) self.assertEqual('Other Error', crash.problem) self.assertEqual(crash.file + '.info.txt', crash.info) self.assertEqual(crash.file + '.stderr.txt', crash.stderr)
def libscanbuild_capture( args: argparse.Namespace) -> Tuple[int, Iterable[Compilation]]: """ Implementation of compilation database generation. :param args: the parsed and validated command line arguments :return: the exit status of build process. """ from libscanbuild.intercept import setup_environment, run_build, exec_trace_files, parse_exec_trace, \ compilations from libear import temporary_directory with temporary_directory(prefix='intercept-') as tmp_dir: # run the build command environment = setup_environment(args, tmp_dir) if os.environ.get('PROS_TOOLCHAIN'): environment['PATH'] = os.path.join(os.environ.get('PROS_TOOLCHAIN'), 'bin') + os.pathsep + \ environment['PATH'] if sys.platform == 'darwin': environment['PATH'] = os.path.dirname(os.path.abspath(sys.executable)) + os.pathsep + \ environment['PATH'] exit_code = run_build(args.build, env=environment) # read the intercepted exec calls calls = (parse_exec_trace(file) for file in exec_trace_files(tmp_dir)) current = compilations(calls, args.cc, args.cxx) return exit_code, iter(set(current))
def test_directory_name_comparison(self): with libear.temporary_directory() as tmp_dir, \ sut.report_directory(tmp_dir, False) as report_dir1, \ sut.report_directory(tmp_dir, False) as report_dir2, \ sut.report_directory(tmp_dir, False) as report_dir3: self.assertLess(report_dir1, report_dir2) self.assertLess(report_dir2, report_dir3)
def test_write_exec_trace_with_response(self): with libear.temporary_directory() as tmp_dir: response_file_one = os.path.join(tmp_dir, 'response1.jom') response_file_two = os.path.join(tmp_dir, 'response2.jom') input_one = Execution(pid=123, cwd='/path/to/here', cmd=[ 'clang-cl', '-c', '@' + response_file_one, '-Idoes_not_exists', '@' + response_file_two ]) output_one = Execution(pid=123, cwd='/path/to/here', cmd=[ 'clang-cl', '-c', '-DSOMETHING_HERE', '-Idoes_not_exists', 'that.cpp' ]) with open(response_file_one, 'w') as response_file_one_handle: response_file_one_handle.write(' -DSOMETHING_HERE\n') with open(response_file_two, 'w') as response_file_two_handle: response_file_two_handle.write(' that.cpp\n') temp_file = os.path.join(tmp_dir, 'single_report.cmd') sut.write_exec_trace(temp_file, input_one) result = sut.parse_exec_trace(temp_file) self.assertEqual(output_one, result)
def test_parse_real_crash(self): import libscanbuild.analyze as sut2 with libear.temporary_directory() as tmp_dir: filename = os.path.join(tmp_dir, 'test.c') with open(filename, 'w') as handle: handle.write('int main() { return 0') # produce failure report opts = { 'clang': 'clang', 'directory': os.getcwd(), 'flags': [], 'source': filename, 'output_dir': tmp_dir, 'language': 'c', 'error_output': 'some output', 'exit_code': 13 } sut2.report_failure(opts) # find the info file pp_files = glob.glob(os.path.join(tmp_dir, 'failures', '*.i')) self.assertIsNot(pp_files, []) pp_file = pp_files[0] # read the failure report back result = sut.parse_crash(pp_file + '.info.txt') self.assertEqual(result['source'], filename) self.assertEqual(result['problem'], 'Other Error') self.assertEqual(result['file'], pp_file) self.assertEqual(result['info'], pp_file + '.info.txt') self.assertEqual(result['stderr'], pp_file + '.stderr.txt')
def run_bug_parse(content): with libear.temporary_directory() as tmp_dir: file_name = os.path.join(tmp_dir, 'test.html') with open(file_name, 'w') as handle: lines = (line + os.linesep for line in content) handle.writelines(lines) for bug in sut.parse_bug_html(file_name): return bug
def run_bug_parse(content): with libear.temporary_directory() as tmp_dir: file_name = os.path.join(tmp_dir, 'test.html') with open(file_name, 'w') as handle: lines = (line + os.linesep for line in content) handle.writelines(lines) for bug in sut.parse_bug_html(file_name): return bug
def test_read_write_exec_trace(self): input_one = Execution(pid=123, cwd='/path/to/here', cmd=['cc', '-c', 'this.c']) with libear.temporary_directory() as tmp_dir: temp_file = os.path.join(tmp_dir, 'single_report.cmd') sut.write_exec_trace(temp_file, input_one) result = sut.parse_exec_trace(temp_file) self.assertEqual(input_one, result)
def test_removes_directory_when_exception(self): dir_name = None try: with sut.temporary_directory() as tmpdir: self.assertTrue(os.path.isdir(tmpdir)) dir_name = tmpdir raise RuntimeError('message') except Exception: self.assertIsNotNone(dir_name) self.assertFalse(os.path.exists(dir_name))
def test_expand_cmd_with_response_files(self): with libear.temporary_directory() as tmp_dir: response_file = os.path.join(tmp_dir, 'response.jom') with open(response_file, 'w') as response_file_handle: response_file_handle.write(' Hello\n') response_file_handle.write(' World!\n') cmd_input = ['echo', '@'+response_file] cmd_output = ['echo', 'Hello', 'World!'] self.assertEqual(cmd_output, sut.expand_cmd_with_response_files(cmd_input))
def test_read_write_exec_trace(self): input_one = Execution( pid=123, cwd='/path/to/here', cmd=['cc', '-c', 'this.c']) with libear.temporary_directory() as tmp_dir: temp_file = os.path.join(tmp_dir, 'single_report.cmd') sut.write_exec_trace(temp_file, input_one) result = sut.parse_exec_trace(temp_file) self.assertEqual(input_one, result)
def test_expand_cmd_with_response_files(self): with libear.temporary_directory() as tmp_dir: response_file = os.path.join(tmp_dir, 'response.jom') with open(response_file, 'w') as response_file_handle: response_file_handle.write(' Hello\n') response_file_handle.write(' World!\n') cmd_input = ['echo', '@' + response_file] cmd_output = ['echo', 'Hello', 'World!'] self.assertEqual(cmd_output, sut.expand_cmd_with_response_files(cmd_input))
def test_parse_crash(self): content = [ "/some/path/file.c", "Some very serious Error", "bla", "bla-bla" ] with libear.temporary_directory() as tmp_dir: file_name = os.path.join(tmp_dir, 'file.i.info.txt') with open(file_name, 'w') as handle: handle.write(os.linesep.join(content)) source, problem = sut.Crash._parse_info_file(file_name) self.assertEqual(source, content[0].rstrip()) self.assertEqual(problem, content[1].rstrip())
def test_get_clang_arguments(self): with libear.temporary_directory() as tmpdir: filename = os.path.join(tmpdir, 'test.c') with open(filename, 'w') as handle: handle.write('') result = sut.get_arguments( ['clang', '-c', filename, '-DNDEBUG', '-Dvar="this is it"'], tmpdir) self.assertTrue('NDEBUG' in result) self.assertTrue('var="this is it"' in result)
def test_get_clang_arguments(self): with libear.temporary_directory() as tmpdir: filename = os.path.join(tmpdir, 'test.c') with open(filename, 'w') as handle: handle.write('') result = sut.get_arguments( ['clang', '-c', filename, '-DNDEBUG', '-Dvar="this is it"'], tmpdir) self.assertTrue('NDEBUG' in result) self.assertTrue('var="this is it"' in result)
def test_parse_crash(self): content = [ "/some/path/file.c", "Some very serious Error", "bla", "bla-bla"] with libear.temporary_directory() as tmp_dir: file_name = os.path.join(tmp_dir, 'file.i.info.txt') with open(file_name, 'w') as handle: handle.write(os.linesep.join(content)) source, problem = sut.Crash._parse_info_file(file_name) self.assertEqual(source, content[0].rstrip()) self.assertEqual(problem, content[1].rstrip())
def capture(args): """ Implementation of compilation database generation. :param args: the parsed and validated command line arguments :return: the exit status of build process. """ with temporary_directory(prefix='intercept-', dir=tempdir()) as tmp_dir: # run the build command environment = setup_environment(args, tmp_dir) exit_code = run_build(args.build, env=environment) # read the intercepted exec calls calls = (parse_exec_trace(file) for file in exec_trace_files(tmp_dir)) current = compilations(calls, args.cc, args.cxx) return exit_code, iter(set(current))
def capture(args): """ Implementation of compilation database generation. :param args: the parsed and validated command line arguments :return: the exit status of build process. """ with temporary_directory(prefix='intercept-') as tmp_dir: # run the build command environment = setup_environment(args, tmp_dir) exit_code = run_build(args.build, env=environment) # read the intercepted exec calls calls = (parse_exec_trace(file) for file in exec_trace_files(tmp_dir)) current = compilations(calls, args.cc, args.cxx) return exit_code, iter(set(current))
def test_sip(self): def create_status_report(filename, message): content = """#!/usr/bin/env sh echo 'sa-la-la-la' echo 'la-la-la' echo '{0}' echo 'sa-la-la-la' echo 'la-la-la' """.format(message) lines = [line.strip() for line in content.split(os.linesep)] with open(filename, 'w') as handle: handle.write(os.linesep.join(lines)) handle.close() os.chmod(filename, 0x1ff) def create_csrutil(dest_dir, status): filename = os.path.join(dest_dir, 'csrutil') message = 'System Integrity Protection status: {0}'.format(status) return create_status_report(filename, message) enabled = 'enabled' disabled = 'disabled' osx = 'darwin' saved = os.environ['PATH'] with libear.temporary_directory() as tmp_dir: try: os.environ['PATH'] = os.pathsep.join([tmp_dir, saved]) create_csrutil(tmp_dir, enabled) self.assertTrue(sut.is_preload_disabled(osx)) create_csrutil(tmp_dir, disabled) self.assertFalse(sut.is_preload_disabled(osx)) finally: os.environ['PATH'] = saved try: os.environ['PATH'] = '' # shall be false when it's not in the path self.assertFalse(sut.is_preload_disabled(osx)) self.assertFalse(sut.is_preload_disabled('unix')) finally: os.environ['PATH'] = saved
def test_sip(self): def create_status_report(filename, message): content = """#!/usr/bin/env sh echo 'sa-la-la-la' echo 'la-la-la' echo '{0}' echo 'sa-la-la-la' echo 'la-la-la' """.format(message) lines = [line.strip() for line in content.split(os.linesep)] with open(filename, 'w') as handle: handle.write(os.linesep.join(lines)) handle.close() os.chmod(filename, 0x1ff) def create_csrutil(dest_dir, status): filename = os.path.join(dest_dir, 'csrutil') message = 'System Integrity Protection status: {0}'.format(status) return create_status_report(filename, message) enabled = 'enabled' disabled = 'disabled' osx = 'darwin' saved = os.environ['PATH'] with libear.temporary_directory() as tmp_dir: try: os.environ['PATH'] = os.pathsep.join([tmp_dir, saved]) create_csrutil(tmp_dir, enabled) self.assertTrue(sut.is_preload_disabled(osx)) create_csrutil(tmp_dir, disabled) self.assertFalse(sut.is_preload_disabled(osx)) finally: os.environ['PATH'] = saved try: os.environ['PATH'] = '' # shall be false when it's not in the path self.assertFalse(sut.is_preload_disabled(osx)) self.assertFalse(sut.is_preload_disabled('unix')) finally: os.environ['PATH'] = saved
def run_analyzer(content, failures_report): with libear.temporary_directory() as tmpdir: filename = os.path.join(tmpdir, 'test.cpp') with open(filename, 'w') as handle: handle.write(content) opts = { 'clang': 'clang', 'directory': os.getcwd(), 'flags': [], 'direct_args': [], 'source': filename, 'output_dir': tmpdir, 'output_format': 'plist', 'output_failures': failures_report } spy = Spy() result = sut.run_analyzer(opts, spy.call) return result, spy.arg
def run_analyzer(content, failures_report): with libear.temporary_directory() as tmpdir: filename = os.path.join(tmpdir, 'test.cpp') with open(filename, 'w') as handle: handle.write(content) opts = { 'clang': 'clang', 'directory': os.getcwd(), 'flags': [], 'direct_args': [], 'source': filename, 'output_dir': tmpdir, 'output_format': 'plist', 'output_failures': failures_report } spy = Spy() result = sut.run_analyzer(opts, spy.call) return result, spy.arg
def test_report_failure_create_files(self): with libear.temporary_directory() as tmp_dir: # create input file filename = os.path.join(tmp_dir, 'test.c') with open(filename, 'w') as handle: handle.write('int main() { return 0') uname_msg = ' '.join(platform.uname()).strip() error_msg = 'this is my error output' # execute test opts = { 'clang': 'clang', 'directory': os.getcwd(), 'flags': [], 'source': filename, 'output_dir': tmp_dir, 'language': 'c', 'error_output': [error_msg], 'exit_code': 13 } sut.report_failure(opts) # find the info file pp_files = glob.glob(os.path.join(tmp_dir, 'failures', '*.i')) self.assertIsNot(pp_files, []) pp_file = pp_files[0] # info file generated and content dumped info_file = pp_file + '.info.txt' self.assertTrue(os.path.exists(info_file)) with open(info_file) as info_handler: lines = [ line.strip() for line in info_handler.readlines() if line.strip() ] self.assertEqual('Other Error', lines[1]) self.assertEqual(uname_msg, lines[3]) # error file generated and content dumped error_file = pp_file + '.stderr.txt' self.assertTrue(os.path.exists(error_file)) with open(error_file) as error_handle: self.assertEqual([error_msg], error_handle.readlines())
def test_write_exec_trace_with_response(self): with libear.temporary_directory() as tmp_dir: response_file_one = os.path.join(tmp_dir, 'response1.jom') response_file_two = os.path.join(tmp_dir, 'response2.jom') input_one = Execution( pid=123, cwd='/path/to/here', cmd=['clang-cl', '-c', '@'+response_file_one, '-Idoes_not_exists', '@'+response_file_two]) output_one = Execution( pid=123, cwd='/path/to/here', cmd=['clang-cl', '-c', '-DSOMETHING_HERE', '-Idoes_not_exists', 'that.cpp']) with open(response_file_one, 'w') as response_file_one_handle: response_file_one_handle.write(' -DSOMETHING_HERE\n') with open(response_file_two, 'w') as response_file_two_handle: response_file_two_handle.write(' that.cpp\n') temp_file = os.path.join(tmp_dir, 'single_report.cmd') sut.write_exec_trace(temp_file, input_one) result = sut.parse_exec_trace(temp_file) self.assertEqual(output_one, result)
def test_report_failure_create_files(self): with libear.temporary_directory() as tmp_dir: # create input file filename = os.path.join(tmp_dir, 'test.c') with open(filename, 'w') as handle: handle.write('int main() { return 0') uname_msg = ' '.join(platform.uname()).strip() error_msg = 'this is my error output' # execute test opts = { 'clang': 'clang', 'directory': os.getcwd(), 'flags': [], 'source': filename, 'output_dir': tmp_dir, 'language': 'c', 'error_output': [error_msg], 'exit_code': 13 } sut.report_failure(opts) # find the info file pp_files = glob.glob(os.path.join(tmp_dir, 'failures', '*.i')) self.assertIsNot(pp_files, []) pp_file = pp_files[0] # info file generated and content dumped info_file = pp_file + '.info.txt' self.assertTrue(os.path.exists(info_file)) with open(info_file) as info_handler: lines = [line.strip() for line in info_handler.readlines() if line.strip()] self.assertEqual('Other Error', lines[1]) self.assertEqual(uname_msg, lines[3]) # error file generated and content dumped error_file = pp_file + '.stderr.txt' self.assertTrue(os.path.exists(error_file)) with open(error_file) as error_handle: self.assertEqual([error_msg], error_handle.readlines())