def test_skip_no_pre_from_parse(self): """Keep everything pre analysis needs it in ctu or statistics mode.""" cmp_cmd_json = [{ "directory": "/tmp/lib1", "command": "g++ /tmp/lib1/a.cpp", "file": "a.cpp" }, { "directory": "/tmp/lib1", "command": "g++ /tmp/lib1/b.cpp", "file": "b.cpp" }, { "directory": "/tmp/lib2", "command": "g++ /tmp/lib2/a.cpp", "file": "a.cpp" }] skip_list = """ -*/lib1/* """ analysis_skip = skiplist_handler.SkipListHandler(skip_list) pre_analysis_skip = skiplist_handler.SkipListHandler("") build_actions, _ = log_parser.\ parse_unique_log(cmp_cmd_json, self.__this_dir, analysis_skip_handler=analysis_skip, ctu_or_stats_enabled=True, pre_analysis_skip_handler=pre_analysis_skip) self.assertEqual(len(build_actions), 3)
def test_no_skip_from_parse(self): """Keep everything for analysis, no skipping there.""" cmp_cmd_json = [{ "directory": "/tmp/lib1", "command": "g++ /tmp/lib1/a.cpp", "file": "a.cpp" }, { "directory": "/tmp/lib1", "command": "g++ /tmp/lib1/b.cpp", "file": "b.cpp" }, { "directory": "/tmp/lib2", "command": "g++ /tmp/lib2/a.cpp", "file": "a.cpp" }] skip_list = """ -*/lib1/* """ analysis_skip = skiplist_handler.SkipListHandler("") pre_analysis_skip = skiplist_handler.SkipListHandler(skip_list) build_actions, _ = log_parser.\ parse_unique_log(cmp_cmd_json, self.__this_dir, analysis_skip_handler=analysis_skip, pre_analysis_skip_handler=pre_analysis_skip) self.assertEqual(len(build_actions), 3)
def test_skip_everything_from_parse(self): """Same skip file for pre analysis and analysis. Skip everything.""" cmp_cmd_json = [{ "directory": "/tmp/lib1", "command": "g++ /tmp/lib1/a.cpp", "file": "a.cpp" }, { "directory": "/tmp/lib1", "command": "g++ /tmp/lib1/b.cpp", "file": "b.cpp" }, { "directory": "/tmp/lib2", "command": "g++ /tmp/lib2/a.cpp", "file": "a.cpp" }] skip_list = """ -*/lib1/* -*/lib2/* """ analysis_skip = skiplist_handler.SkipListHandler(skip_list) pre_analysis_skip = skiplist_handler.SkipListHandler(skip_list) build_actions, _ = log_parser.\ parse_unique_log(cmp_cmd_json, self.__this_dir, analysis_skip_handler=analysis_skip, pre_analysis_skip_handler=pre_analysis_skip) self.assertEqual(len(build_actions), 0)
def test_skip_all_in_pre_from_parse(self): """Pre analysis skips everything but keep build action for analysis.""" cmp_cmd_json = [ {"directory": "/tmp/lib1", "command": "g++ /tmp/lib1/a.cpp", "file": "a.cpp"}, {"directory": "/tmp/lib1", "command": "g++ /tmp/lib1/b.cpp", "file": "b.cpp"}, {"directory": "/tmp/lib2", "command": "g++ /tmp/lib2/a.cpp", "file": "a.cpp"}] keep = cmp_cmd_json[2] skip_list = """ -*/lib1/* """ pre_skip_list = """ -* """ analysis_skip = skiplist_handler.SkipListHandler(skip_list) pre_analysis_skip = skiplist_handler.SkipListHandler(pre_skip_list) build_actions, _ = log_parser.\ parse_unique_log(cmp_cmd_json, self.__this_dir, analysis_skip_handler=analysis_skip, pre_analysis_skip_handler=pre_analysis_skip) self.assertEqual(len(build_actions), 1) source_file = os.path.join(keep['directory'], keep['file']) self.assertEqual(build_actions[0].source, source_file) self.assertEqual(build_actions[0].original_command, keep['command'])
def test_skip_everything_from_parse_relative_path(self): """ Same skip file for pre analysis and analysis. Skip everything. Source file contains relative path. """ cmp_cmd_json = [ {"directory": "/tmp/lib1/Debug", "command": "g++ ../a.cpp", "file": "../a.cpp"}, {"directory": "/tmp/lib1/Debug/rel", "command": "g++ ../../b.cpp", "file": "../../b.cpp"}, {"directory": "/tmp/lib1/Debug", "command": "g++ ../d.cpp", "file": "../d.cpp"}, {"directory": "/tmp/lib2/Debug", "command": "g++ ../a.cpp", "file": "../a.cpp"}] skip_list = """ +/tmp/lib1/d.cpp -*/lib1/Debug/rel/../../* -*/lib1/a.cpp -/tmp/lib2/a.cpp """ analysis_skip = skiplist_handler.SkipListHandler(skip_list) pre_analysis_skip = skiplist_handler.SkipListHandler(skip_list) build_actions, _ = log_parser.\ parse_unique_log(cmp_cmd_json, self.__this_dir, analysis_skip_handler=analysis_skip, pre_analysis_skip_handler=pre_analysis_skip) self.assertEqual(len(build_actions), 1) self.assertEqual(build_actions[0].source, '/tmp/lib1/d.cpp')
def get_skip_handler( report_dir: str) -> skiplist_handler.SkipListHandler: """ Get a skip list handler based on the given report directory.""" skip_file_path = os.path.join(report_dir, 'skip_file') if not os.path.exists(skip_file_path): return skiplist_handler.SkipListHandler() LOG.debug("Pocessing skip file %s", skip_file_path) try: with open(skip_file_path, encoding="utf-8", errors="ignore") as f: skip_content = f.read() LOG.debug(skip_content) return skiplist_handler.SkipListHandler(skip_content) except (IOError, OSError) as err: LOG.error("Failed to open skip file: %s", err) raise
def __get_skip_handler(args): """ Initialize and return a skiplist handler if there is a skip list file in the arguments. """ try: if args.skipfile: LOG.debug_analyzer("Creating skiplist handler.") with open(args.skipfile) as skip_file: return skiplist_handler.SkipListHandler(skip_file.read()) except AttributeError: LOG.debug_analyzer('Skip file was not set in the command line')
def test_skip_all_header(self): """ Test skipping all header files. """ with open('skip_all_header.txt', encoding="utf-8", errors="ignore") as skip_file: skip_handler = skiplist_handler.SkipListHandler(skip_file.read()) with open('x.plist', 'r') as plist_data: data = remove_report_from_plist(plist_data, skip_handler) with open('skip_all_header.expected.plist', 'rb') as plist_file: expected = plist_file.read() self.assertEqual(data, expected)
def test_keep_only_empty(self): """ Test skipping all files except empty. """ with open('keep_only_empty.txt', encoding="utf-8", errors="ignore") as skip_file: skip_handler = skiplist_handler.SkipListHandler(skip_file.read()) with open('x.plist', 'rb') as plist_data: data = remove_report_from_plist(plist_data, skip_handler) with open('keep_only_empty.expected.plist', 'rb') as plist_file: expected = plist_file.read() self.assertEqual(data, expected)
def __get_skip_handler(args): """ Initialize and return a skiplist handler if there is a skip list file in the arguments or files options is provided. """ skip_file_content = "" if 'files' in args: # Creates a skip file where all source files will be skipped except # the given source files. skip_files = ['+{0}'.format(f) for f in args.files] skip_files.append('-*') skip_file_content = "\n".join(skip_files) elif 'skipfile' in args: with open(args.skipfile, encoding="utf-8", errors="ignore") as skip_file: skip_file_content = skip_file.read() if skip_file_content: LOG.debug_analyzer("Creating skiplist handler.") return skiplist_handler.SkipListHandler(skip_file_content)
def __get_skip_handler(args, compile_commands): """ Initialize and return a skiplist handler if there is a skip list file in the arguments or files options is provided. """ skip_file_content = "" if 'files' in args: # Creates a skip file where all source files will be skipped except # the given source files. skip_files = ['+{0}'.format(f) for f in args.files] skip_files.append('-*') skip_file_content = "\n".join(skip_files) elif 'skipfile' in args: with open(args.skipfile, encoding="utf-8", errors="ignore") as skip_file: skip_file_content = skip_file.read() if skip_file_content: LOG.debug_analyzer("Creating skiplist handler.") handler = skiplist_handler.SkipListHandler(skip_file_content) analyze_headers = [] # Check whether the skip file contains a header file which is not # skipped. for skip_line in handler.skip_file_lines: if skip_line[0] == '+' and \ skip_line.lower().endswith((".h", ".hh", ".hpp")): norm_skip_path = os.path.normpath(skip_line[1:].strip()) rexpr = re.compile(fnmatch.translate(norm_skip_path + '*')) analyze_headers.append((skip_line, rexpr)) # Get source files which depend on the previously collected header # files and create a new skip list handler where we include these # files in the beginning. if analyze_headers: LOG.info("Get source files which depend on some header files and " "should be analyzed by your skip file.") dependencies = __get_source_dependencies(compile_commands) analyze_header_deps = [] for f, deps in dependencies.items(): for _, rexpr in analyze_headers: if rexpr.match(f): analyze_header_deps.extend(["+" + d for d in deps]) if analyze_header_deps: LOG.info( "Your skip file contained some header files (%s) to " "be analyzed. Analysis can not be executed on header " "files only. For this reason CodeChecker will " "analyze the following source files which include " "the header files:\n%s", ', '.join([f for (f, _) in analyze_headers]), '\n'.join([" " + f for f in analyze_header_deps])) skip_file_content = \ "\n".join(analyze_header_deps) + "\n" + skip_file_content return skiplist_handler.SkipListHandler(skip_file_content) return handler