class TestGNATstackSupport(TestCase): def setUp(self): self.longMessage = True self.project = Project.simple() self.project.build() self.gnathub = GNAThub(self.project, plugins=['gnatstack']) def testDatabaseContent(self): script_output_file = os.path.abspath('script.out') self.gnathub.run(script=Script.db2cfg(), output=script_output_file) parser = ConfigParser() parser.optionxform = str parser.read(script_output_file) sections = parser.sections() for s in SECTIONS: self.assertTrue(s in sections, "Missing section: " + s) self.assertTrue(parser.has_option(TEXT_IO[1], EXTERNAL_CALL[0]), "Should be an external call") self.assertEqual(parser.get(TEXT_IO[1], EXTERNAL_CALL[0]), EXTERNAL_CALL[1], 'Wrong message') self.assertTrue(parser.has_option(B_SIMPLE_ADB[1], ENTRY_POINT[0]), "Entry point not found") self.assertTrue( parser.get(B_SIMPLE_ADB[1], ENTRY_POINT[0]).endswith(ENTRY_POINT[1]), 'unexpected value for the entry point') for metric in METRICS_SIMPLE_4_1: self.assertTrue(parser.has_option(SIMPLE_4_1[0], metric), 'missing entry for "%s"' % metric)
class TestGNATstackSupport(TestCase): def setUp(self): self.longMessage = True self.project = Project.simple_gnatstack() self.project.build() self.gnathub = GNAThub(self.project, plugins=['gnatstack']) script_output_file = os.path.abspath('script.out') self.gnathub.run(script=Script.db2cfg(), output=script_output_file) self.parser = SafeConfigParser() self.parser.optionxform = str self.parser.read(script_output_file) def testDatabaseContent(self): self.assertTrue(self.parser.has_option(MATRIX[0], UNBOUNDED_FRAME[0]), "Should be an unbounded frame") self.assertTrue(self.parser.has_option(B_MAIN[0], ENTRY_POINT[0]), "Entry point not found") for metric in METRICS_MATRIX: self.assertTrue(self.parser.has_option(MATRIX[0], metric), 'missing entry for "%s"' % metric)
def testGNATcheckHideExempted(self): PROJECT = Project.simple_with_hide_exempted() # Define 'obj' folder name and path OBJ_FOLDER = 'obj' OBJ_PATH = os.path.join(PROJECT.install_dir, OBJ_FOLDER) # 1st step: Run GNAThub without '--gnatcheck-hide-exempted' switch assert not os.path.exists(OBJ_PATH), 'Object folder should not exist' gnathub = GNAThub(PROJECT, plugins=['gnatcheck']) assert os.path.exists(OBJ_PATH), 'Object folder should exist' # Check that results are generated in /obj GNATCHECK_FILES_COUNT = 0 GNATCHECK_FILES_FOUND = False GNATHUB_DIR_FOUND = False GNATHUB_DIR_LEN = 0 for entry in os.listdir(OBJ_PATH): if entry.startswith('gnatcheck') and entry.endswith('.out'): GNATCHECK_FILES_COUNT += 1 elif os.path.isdir(os.path.join(OBJ_PATH, entry)): crt_path = os.path.join(OBJ_PATH, entry) if entry == 'gnathub': GNATHUB_DIR_FOUND = True GNATHUB_DIR_LEN = len(os.listdir(crt_path)) GNATCHECK_FILE_FOUND = GNATCHECK_FILES_COUNT != 0 assert GNATCHECK_FILE_FOUND, 'GNATcheck files should be generated in /obj folder' assert GNATHUB_DIR_FOUND, 'GNAThub directory should be generated in /obj folder' # Check DB content after gnatcheck run gnathub.run(script='check-db-gnatcheck.py') # 2nd step: Run GNAThub with '--gnatcheck-hide-exempted' switch gnathub = GNAThub(PROJECT, plugins=['gnatcheck'], gnatcheck_hide_exempted=True) GNATHUB_DIR_FOUND = False GNATHUB_DIR_LEN = 0 for entry in os.listdir(OBJ_PATH): if entry.startswith('gnatcheck') and entry.endswith('.out'): GNATCHECK_FILES_COUNT += 1 elif os.path.isdir(os.path.join(OBJ_PATH, entry)): crt_path = os.path.join(OBJ_PATH, entry) if entry == 'gnathub': GNATHUB_DIR_FOUND = True GNATHUB_DIR_LEN = len(os.listdir(crt_path)) elif entry == 'codepeer': CODEPEER_DIR_FOUND = True CODEPEER_DIR_LEN = len(os.listdir(crt_path)) GNATCHECK_FILE_FOUND = GNATCHECK_FILES_COUNT != 0 assert GNATCHECK_FILE_FOUND, 'GNATcheck files should be generated in /obj folder' assert GNATHUB_DIR_FOUND, 'GNAThub directory should be generated in /obj folder' # Check DB content after gnatcheck run with '--gnatcheck-hide-exempted' switch gnathub.run(script='check-db-gnatcheck-hide.py')
class TestGNATmetricSupport(TestCase): def setUp(self): self.longMessage = True self.gnathub = GNAThub(Project.simple(), plugins=['gnatmetric']) def testDatabaseContent(self): script_output_file = os.path.abspath('script.out') self.gnathub.run(script=Script.db2cfg(), output=script_output_file) parser = ConfigParser() parser.optionxform = str parser.read(script_output_file) self.assertListEqual(sorted(parser.sections()), ELEMENTS) for metric, value in EXPECTED_METRICS.items(): self.assertTrue(parser.has_option(SECTION, metric), 'missing entry for "%s"' % metric) self.assertEqual(parser.getfloat(SECTION, metric), value, 'unexpected value for "%s"' % metric) for emetric, value in EXPECTED_EMETRICS.items(): self.assertTrue(parser.has_option(ESECTION, emetric), 'missing entry for "%s"' % emetric) self.assertEqual(parser.getfloat(ESECTION, emetric), value, 'unexpected value for "%s"' % emetric)
class TestLALmetricSupport(TestCase): def setUp(self): self.longMessage = True os.environ['USE_LIBADALANG_TOOLS'] = '1' self.gnathub = GNAThub(Project.simple(), plugins=['gnatmetric']) @unittest.skipIf(const.skipLALToolsTests, 'requires LAL tools') def testDatabaseContent(self): script_output_file = os.path.abspath('script.out') self.gnathub.run(script='check-run.py') self.gnathub.run(script=Script.db2cfg(), output=script_output_file) parser = SafeConfigParser() parser.optionxform = str parser.read(script_output_file) self.assertListEqual(sorted(parser.sections()), FILES) for metric, value in EXPECTED_METRICS.iteritems(): self.assertTrue( parser.has_option(SECTION, metric), 'missing entry for "%s"' % metric) self.assertEqual( parser.getfloat(SECTION, metric), value, 'unexpected value for "%s"' % metric)
class TestSimpleExample(TestCase): def setUp(self): self.longMessage = True # Run GNAThub with only the sonar-config plugin self.gnathub = GNAThub(Project.simple(), plugins=['sonar-config']) def testDatabaseContent(self): # Test the Python plug-in API self.gnathub.run(script='check-api.py')
class TestSimpleExample(TestCase): def setUp(self): self.longMessage = True # Run GNAThub with only the sonar-config plugin self.gnathub = GNAThub(Project.disabled(), plugins=['sonar-config']) def testExternalRefsSwitch(self): # Pass a -Xkey=value option to GNAThub self.gnathub.run(scenario_vars={'key': 'value'})
class TestSpark2014Support(TestCase): def setUp(self): self.longMessage = True self.gnathub = GNAThub(Project.flow_analysis(), plugins=['spark2014']) def testFlowAnalysisDBContent(self): self.gnathub.run(script='check_flow_analysis_db.py') def testFlowAnalysisMultiObjDirDBContent(self): self.gnathub = GNAThub(Project.fa_multi_obj(), plugins=['spark2014']) self.gnathub.run(script='check_flow_analysis_multi_obj_db.py')
class TestSimpleExample(TestCase): def setUp(self): self.longMessage = True # Run GNAThub with only the sonar-config plugin self.gnathub = GNAThub(Project.simple(), plugins=['gnatmetric']) def testClearToolReference(self): self.gnathub.run(script='clear-gnatmetric-references.py') def testCreateMessageWithProperty(self): self.gnathub.run(script='create-message-with-property.py')
class TestSimpleExample(TestCase): def setUp(self): self.longMessage = True # Run GNAThub with only the sonar-config plugin self.gnathub = GNAThub(Project.simple(), plugins=['sonar-config']) def testPythonAPIDefined(self): # Test the core Python API self.gnathub.run(script='check-api.py', tool_args={ 'codepeer': ['-msg-output-only', '-j0', 'positional-arg'], 'codepeer_msg_reader': ['-msg-output-only'] }, runners_only=True)
def testDryRunModeNoSideEffect(self): PROJECT = Project.disabled() PLUGINS = ['sonar-config'] DB = os.path.join(PROJECT.install_dir, 'obj', 'gnathub', 'gnathub.db') # Execute GNAThub with option --dry-run # Run GNAThub with only the sonar-config plugin gnathub = GNAThub(PROJECT, plugins=PLUGINS, dry_run=True) assert not os.path.exists(DB), 'Database should not have been created' # Execute GNAThub without option --dry-run gnathub.run(plugins=PLUGINS) assert os.path.exists(DB), 'Database should have been created'
class TestCodePeerSupport(TestCase): def setUp(self): self.longMessage = True self.gnathub = GNAThub( Project.simple(), plugins=['codepeer'], mocks=[CodePeerExecutable]) def testDatabaseContent(self): script_output_file = os.path.abspath('script.out') self.gnathub.run(script=Script.db2cfg(), output=script_output_file) parser = SafeConfigParser() parser.optionxform = str parser.read(script_output_file) self.assertListEqual(sorted(parser.sections()), RESULTS)
class TestGcovMultiObjectDirExample(TestCase): def setUp(self): self.longMessage = True # Import the project, build it and run it self.project = Project.gcov_multi_object_dir() self.project.build() self.project.run() # Run GNAThub with only the gcov plugin self.gnathub = GNAThub(self.project, plugins=['gcov']) # Extract coverage information from the database script_output_file = os.path.abspath('script.out') self.gnathub.run(script=Script.db2cfg(), output=script_output_file) self.parser = SafeConfigParser() self.parser.optionxform = str self.parser.read(script_output_file) def getCoverage(self, fn): """Returns the lines covered by the tests. :param str fn: The filename of the file for which to fetch coverage results. :returns: list[str] """ return [s for s in self.parser.sections() if s.startswith('%s ' % fn)] def testDatabaseContent(self): # Test that gcov has been run and that GNAThub found coverage data to # collect self.assertTrue(not self.parser.has_option('sdc.ads', 'coverage')) self.assertEqual(len(self.getCoverage('sdc.adb')), 15) self.assertEqual(len(self.getCoverage('values.adb')), 11) self.assertEqual(len(self.getCoverage('stack.adb')), 28) self.assertEqual(len(self.getCoverage('src2.adb')), 2) self.assertEqual(len(self.getCoverage('src3.adb')), 1)
class TestGcovMultiObjectDirExample(TestCase): def setUp(self): self.longMessage = True # Import the project, build it and run it self.project = Project.gcov_multi_object_dir() self.project.build() self.project.run() # Run GNAThub with only the gcov plugin self.gnathub = GNAThub(self.project, plugins=['gcov']) # Extract coverage information from the database script_output_file = os.path.abspath('script.out') self.gnathub.run(script=Script.db2cfg(), output=script_output_file) self.parser = SafeConfigParser() self.parser.optionxform = str self.parser.read(script_output_file) def getCoverage(self, fn): """Returns the lines covered by the tests. :param str fn: The filename of the file for which to fetch coverage results. :returns: list[str] """ return [s for s in self.parser.sections() if s.startswith('%s ' % fn)] def testDatabaseContent(self): # Test that gcov has been run and that GNAThub found coverage data to # collect self.assertTrue(not self.parser.has_option('sdc.ads', 'coverage')) self.assertEqual(len(self.getCoverage('sdc.adb')), 16) self.assertEqual(len(self.getCoverage('values.adb')), 11) self.assertEqual(len(self.getCoverage('stack.adb')), 31) self.assertEqual(len(self.getCoverage('src2.adb')), 2) self.assertEqual(len(self.getCoverage('src3.adb')), 1)
class TestSimpleExample(TestCase): def setUp(self): self.longMessage = True # Run GNAThub with only the sonar-config plugin self.gnathub = GNAThub(Project.disabled(), plugins=['sonar-config']) def testDatabaseContent(self): # Dummy scenario variables scenario = { 'BUILD_MODE': 'Production', 'VERSION': 'test-0.0.0', 'BUILD_DIR': '/some/user/workspace/project/build/dir', 'PROCESSORS': '2' } # Test the Python API self.gnathub.run(script='check-api.py', scenario_vars=scenario) def testCrossAttributes(self): self.gnathub.run(script='check-cross-api.py') self.gnathub.run( script='check-cross-api-override.py', target='my-custom-target', runtime='my-custom-runtime' )
class TestLALmetricSupport(TestCase): def setUp(self): self.longMessage = True os.environ['USE_LIBADALANG_TOOLS'] = '1' self.gnathub = GNAThub(Project.simple(), plugins=['gnatmetric']) @unittest.skipIf(const.skipLALToolsTests, 'requires LAL tools') def testDatabaseContent(self): script_output_file = os.path.abspath('script.out') self.gnathub.run(script='check-run.py') self.gnathub.run(script=Script.db2cfg(), output=script_output_file) parser = SafeConfigParser() parser.optionxform = str parser.read(script_output_file) self.assertListEqual(sorted(parser.sections()), FILES) for metric, value in EXPECTED_METRICS.iteritems(): self.assertTrue(parser.has_option(SECTION, metric), 'missing entry for "%s"' % metric) self.assertEqual(parser.getfloat(SECTION, metric), value, 'unexpected value for "%s"' % metric)
class TestGNATstackSupport(TestCase): def setUp(self): self.longMessage = True self.project = Project.simple() self.project.build() self.project.run() self.gnathub = GNAThub(self.project, plugins=['gnatstack']) def testDatabaseContent(self): script_output_file = os.path.abspath('script.out') self.gnathub.run(script=Script.db2cfg(), output=script_output_file) parser = SafeConfigParser() parser.optionxform = str parser.read(script_output_file) sections = parser.sections() for s in SECTIONS: self.assertTrue(s in sections, "Missing section: " + s) self.assertTrue(parser.has_option(TEXT_IO[0], EXTERNAL_CALL[0]), "Should be an external call") self.assertEqual(parser.get(TEXT_IO[0], EXTERNAL_CALL[0]), EXTERNAL_CALL[1], 'Wrong message') self.assertTrue(parser.has_option(B_SIMPLE_ADB[0], ENTRY_POINT[0]), "Entry point not found") self.assertTrue(parser.get(B_SIMPLE_ADB[0], ENTRY_POINT[0]).endswith(ENTRY_POINT[1]), 'unexpected value for the entry point') for metric in METRICS_SIMPLE_4_1: self.assertTrue( parser.has_option(SIMPLE_4_1[0], metric), 'missing entry for "%s"' % metric)
class TestSimpleExample(TestCase): def setUp(self): self.longMessage = True # Run GNAThub with only the sonar-config plugin self.gnathub = GNAThub(Project.disabled(), plugins=['sonar-config']) def testDatabaseContent(self): # Dummy scenario variables scenario = { 'BUILD_MODE': 'Production', 'VERSION': 'test-0.0.0', 'BUILD_DIR': '/some/user/workspace/project/build/dir', 'PROCESSORS': '2' } # Test the Python API self.gnathub.run(script='check-api.py', scenario_vars=scenario) def testCrossAttributes(self): self.gnathub.run(script='check-cross-api.py') self.gnathub.run(script='check-cross-api-override.py', target='my-custom-target', runtime='my-custom-runtime')
def testDatabaseContent(self): PROJECT = Project.simple() # Create path to /obj and /codepeer folders OBJ_FOLDER = 'obj' OBJ_PATH = os.path.join(Project.simple().install_dir, OBJ_FOLDER) CODEPEER_FOLDER = 'codepeer' CODEPEER_PATH = os.path.join(OBJ_PATH, CODEPEER_FOLDER) # Run GNAThub with Codepeer gnathub = GNAThub(PROJECT, plugins=['codepeer']) # Check that /obj and /codepeer folders exists assert os.path.exists(OBJ_PATH), 'Object folder should exist' assert os.path.exists(CODEPEER_PATH), 'Codepeer folder should exist' # Check the existence of expected simple.csv file CSV_COUNT = 0 CSV_PATH = CODEPEER_PATH for entry in os.listdir(CODEPEER_PATH): if entry.endswith('.csv') and entry.startswith('simple'): CSV_COUNT += 1 CSV_PATH = os.path.join(CSV_PATH, entry) assert CSV_COUNT == 1, 'simple.csv should be generated in /obj/codepeer folder' assert os.path.isfile(CSV_PATH), 'simple.csv is a file' # Check .csv content columns = defaultdict(list) # each value in each column is appended to a list with open(CSV_PATH) as f: # read rows into a dictionary format reader = csv.DictReader(f) # read a row as {column1: value1, column2: value2,...} for row in reader: # go over each column name and value for (k,v) in row.items(): # append the value into the appropriate list based on column name k columns[k].append(v) # Check column 'Line' self.assertListEqual(columns['Line'], EXPECTED_LINES_FROM_CSV) # Check column 'Column' self.assertListEqual(columns['Column'], EXPECTED_COLUMNS_FROM_CSV) # Check column 'Category' self.assertListEqual(columns['Category'], EXPECTED_CATEGORIES_FROM_CSV) # Check column 'Ranking' self.assertListEqual(columns['Ranking'], EXPECTED_RANKINGS_FROM_CSV) # Check column 'Kind' self.assertListEqual(columns['Kind'], EXPECTED_KINDS_FROM_CSV) # Check column 'Message' self.assertListEqual(columns['Message'], EXPECTED_MESSAGES_FROM_CSV) # Checks the analysis report results are as expected script_output_file = os.path.abspath('script.out') gnathub.run(script=Script.db2cfg(), output=script_output_file) parser = SafeConfigParser() parser.optionxform = str parser.read(script_output_file) self.assertListEqual(sorted(parser.sections()), RESULTS)
class TestCoverageExhaustiveExample(TestCase): Violation = namedtuple('Violation', 'level column message') CoverageRecord = namedtuple('CoverageRecord', 'file rule line column message') EXPECTED_COVERAGE = { 'do_nothing.adb': { 1: Violation('coverage', 0, 'NO_CODE'), 2: Violation('coverage', 0, 'NO_CODE'), 3: Violation('coverage', 0, 'COVERED'), 4: Violation('coverage', 0, 'NO_CODE') }, 'main.adb': { 1: Violation('coverage', 0, 'NO_CODE'), 2: Violation('coverage', 0, 'NO_CODE'), 3: Violation('coverage', 0, 'NO_CODE'), 4: Violation('coverage', 0, 'NO_CODE'), 5: Violation('coverage', 0, 'NO_CODE'), 6: Violation('coverage', 0, 'NO_CODE'), 7: Violation('coverage', 0, 'COVERED'), 8: Violation('coverage', 0, 'COVERED'), 9: Violation('coverage', 0, 'COVERED'), 10: Violation('coverage', 0, 'COVERED'), 11: Violation('coverage', 0, 'NO_CODE') }, 'test_stmt.adb': { 1: Violation('coverage', 0, 'NO_CODE'), 2: Violation('coverage', 0, 'NO_CODE'), 3: Violation('coverage', 0, 'NO_CODE'), 4: Violation('coverage', 0, 'NO_CODE'), 5: Violation('coverage', 0, 'NO_CODE'), 6: Violation('coverage', 0, 'COVERED'), 7: Violation('coverage', 0, 'NO_CODE'), 8: Violation('coverage', 0, 'COVERED'), 9: Violation('coverage', 0, 'NO_CODE'), 10: [ Violation('statement', 7, 'statement not executed'), Violation('coverage', 0, 'NOT_COVERED') ], 11: Violation('coverage', 0, 'NO_CODE'), 12: Violation('coverage', 0, 'NO_CODE'), 13: Violation('coverage', 0, 'NO_CODE'), 14: [ Violation('statement', 4, 'statement not executed'), Violation('coverage', 0, 'PARTIALLY_COVERED') ], 15: Violation('coverage', 0, 'NO_CODE'), 16: Violation('coverage', 0, 'NO_CODE'), 17: Violation('coverage', 0, 'COVERED'), 18: [ Violation('statement', 7, 'statement not executed'), Violation('coverage', 0, 'NOT_COVERED') ], 19: Violation('coverage', 0, 'NO_CODE'), 20: Violation('coverage', 0, 'NO_CODE') }, 'test_decision.adb': { 1: Violation('coverage', 0, 'NO_CODE'), 2: Violation('coverage', 0, 'NO_CODE'), 3: Violation('coverage', 0, 'NO_CODE'), 4: Violation('coverage', 0, 'NO_CODE'), 5: Violation('coverage', 0, 'NO_CODE'), 6: [ Violation('decision', 4, 'decision outcome FALSE never exercised'), Violation('coverage', 0, 'PARTIALLY_COVERED') ], 7: Violation('coverage', 0, 'COVERED'), 8: Violation('coverage', 0, 'NO_CODE'), 9: Violation('coverage', 0, 'NO_CODE'), 10: Violation('coverage', 0, 'NO_CODE'), 11: [ Violation('decision', 4, 'decision outcome TRUE never exercised'), Violation('coverage', 0, 'PARTIALLY_COVERED'), ], 12: [ Violation('statement', 7, 'statement not executed'), Violation('coverage', 0, 'NOT_COVERED') ], 13: Violation('coverage', 0, 'NO_CODE'), 14: Violation('coverage', 0, 'NO_CODE'), 15: [ Violation('statement', 7, 'statement not executed'), Violation('coverage', 0, 'NOT_COVERED') ], 16: [ Violation('statement', 10, 'statement not executed'), Violation('coverage', 0, 'NOT_COVERED') ], 17: Violation('coverage', 0, 'NO_CODE'), 18: Violation('coverage', 0, 'NO_CODE'), 19: Violation('coverage', 0, 'NO_CODE'), 20: Violation('coverage', 0, 'COVERED'), 21: Violation('coverage', 0, 'NO_CODE'), 22: Violation('coverage', 0, 'COVERED'), 23: Violation('coverage', 0, 'COVERED'), 24: Violation('coverage', 0, 'NO_CODE'), 25: Violation('coverage', 0, 'NO_CODE'), 26: Violation('coverage', 0, 'NO_CODE') }, 'test_mcdc.adb': { 1: Violation('coverage', 0, 'NO_CODE'), 2: Violation('coverage', 0, 'NO_CODE'), 3: Violation('coverage', 0, 'NO_CODE'), 4: Violation('coverage', 0, 'NO_CODE'), 5: Violation('coverage', 0, 'NO_CODE'), 6: [ Violation( 'condition', 4, 'condition has no independent influence pair, MC/DC not achieved' ), Violation('coverage', 0, 'PARTIALLY_COVERED') ], 7: Violation('coverage', 0, 'COVERED'), 8: Violation('coverage', 0, 'NO_CODE'), 9: Violation('coverage', 0, 'NO_CODE') }, } def setUp(self): self.longMessage = True # Just import the project. Do no try to build it nor to run it: we # don't expect GNATcoverage to be available during testsuite runs, so # use the precomputed coverage reports instead. self.project = Project.coverage_exhaustive() # Run GNAThub with only the GNATcoverage plugin self.gnathub = GNAThub(self.project, plugins=['gnatcoverage']) # Extract coverage information from the database script_output_file = os.path.abspath('script.out') self.gnathub.run(script=Script.db2cfg(), output=script_output_file) self.parser = ConfigParser() self.parser.optionxform = str self.parser.read(script_output_file) def structured_to_list_coverage(self, struct): """ Turn structured coverage expectations into a list of CoverageRecord. See TestCoverag.ExhaustiveExample.EXPECTED_COVERAGE for a structured coverage example. """ result = [] for filename, record in struct.items(): for line, record in record.items(): # Shortcut: if there is only one message on a line, do not # require a sequence of messages. if isinstance(record, (str, self.Violation)): record = [record] for content in record: def get_rec(rule, column, message): return self.CoverageRecord(filename, rule, line, column, message) if isinstance(content, self.Violation): result.append( get_rec(content.level, content.column, content.message)) result.sort() return result def config_to_list_coverage(self, config): """Turn a ConfigParser instance into a list of CoverageRecord.""" result = [] for section in config.sections(): # There are no file-wide sections we are interested in: skip them try: filename, sloc = section.rsplit(None, 1) line, column = sloc.split(':') except ValueError: continue line = int(line) column = int(column) for name, value in config.items(section): result.append( self.CoverageRecord(filename, name, line, column, value)) result.sort() return result def testDatabaseContent(self): # Test that GNAThub extract the coverage information we expect it to self.assertEqual( self.config_to_list_coverage(self.parser), self.structured_to_list_coverage(self.EXPECTED_COVERAGE))
class TestGNATcheckSupport(TestCase): def setUp(self): self.longMessage = True self.gnathub = GNAThub(Project.simple_gnatcheck(), plugins=['gnatcheck']) def testDatabaseContent(self): script_output_file = os.path.abspath('script.out') self.gnathub.run(script=Script.db2cfg(), output=script_output_file) parser = ConfigParser() parser.optionxform = str parser.read(script_output_file) self.assertListEqual(sorted(parser.sections()), SECTIONS) self.assertTrue(parser.has_option(F_ADB, 'warnings'), 'missing "warnings" entry') self.assertTrue(parser.has_option(F_ADB, 'identifier_casing'), 'missing "identifier_casing" entry') self.assertTrue( parser.has_option(P_ADB, 'unconstrained_array_returns'), 'missing "unconstrained_array_returns" entry') self.assertTrue( parser.has_option(P_ADS_1, 'unconstrained_array_returns'), 'missing "unconstrained_array_returns" entry') self.assertTrue(parser.has_option(PG_ADB_1, 'improper_returns'), 'missing "improper_returns" entry') self.assertTrue(parser.has_option(PG_ADB_2, 'improper_returns'), 'missing "improper_returns" entry') self.assertTrue( parser.has_option(PG_ADB_3, 'unconstrained_array_returns'), 'missing "unconstrained_array_returns" entry') self.assertTrue(parser.has_option(PG_ADB_4, 'warnings'), 'missing "warnings" entry') self.assertTrue(parser.has_option(PG_ADB_5, 'warnings'), 'missing "warnings" entry') self.assertTrue( parser.has_option(PG_ADS_1, 'unconstrained_array_returns'), 'missing "unconstrained_array_returns" entry') self.assertTrue( parser.has_option(PG_ADS_2, 'unconstrained_array_returns'), 'missing "unconstrained_array_returns" entry') self.assertTrue( parser.has_option(PG_ADS_3, 'unconstrained_array_returns'), 'missing "unconstrained_array_returns" entry') self.assertTrue(parser.has_option(PG_ADS_4, 'recursive_subprograms'), 'missing "recursive_subprograms" entry') self.assertTrue( parser.has_option(PGG_ADS_1, 'unconstrained_array_returns'), 'missing "unconstrained_array_returns" entry') self.assertTrue(parser.has_option(PGG_ADS_2, 'warnings'), 'missing "warnings" entry') self.assertTrue(parser.has_option(SIMPLE_ADB_1, 'improper_returns'), 'missing "improper_returns" entry') self.assertTrue(parser.has_option(SIMPLE_ADB_3, 'identifier_suffixes'), 'missing "identifier_suffixes" entry') self.assertTrue(parser.has_option(SIMPLE_ADB_3, 'identifier_prefixes'), 'missing "identifier_prefixes" entry')
class TestGNATcheckSupport(TestCase): def setUp(self): self.longMessage = True self.gnathub = GNAThub(Project.simple_gnatcheck(), plugins=['gnatcheck']) def testDatabaseContent(self): script_output_file = os.path.abspath('script.out') self.gnathub.run(script=Script.db2cfg(), output=script_output_file) parser = SafeConfigParser() parser.optionxform = str parser.read(script_output_file) self.assertListEqual(sorted(parser.sections()), SECTIONS) self.assertTrue( parser.has_option(F_ADB, 'warnings'), 'missing "warnings" entry') self.assertTrue( parser.has_option(F_ADB, 'identifier_casing'), 'missing "identifier_casing" entry') self.assertTrue( parser.has_option(P_ADB, 'unconstrained_array_returns'), 'missing "unconstrained_array_returns" entry') self.assertTrue( parser.has_option(P_ADS_1, 'unconstrained_array_returns'), 'missing "unconstrained_array_returns" entry') self.assertTrue( parser.has_option(PG_ADB_1, 'improper_returns'), 'missing "improper_returns" entry') self.assertTrue( parser.has_option(PG_ADB_2, 'improper_returns'), 'missing "improper_returns" entry') self.assertTrue( parser.has_option(PG_ADB_3, 'unconstrained_array_returns'), 'missing "unconstrained_array_returns" entry') self.assertTrue( parser.has_option(PG_ADB_4, 'warnings'), 'missing "warnings" entry') self.assertTrue( parser.has_option(PG_ADB_5, 'unconstrained_array_returns'), 'missing "unconstrained_array_returns" entry') self.assertTrue( parser.has_option(PG_ADB_6, 'warnings'), 'missing "warnings" entry') self.assertTrue( parser.has_option(PG_ADS_1, 'unconstrained_array_returns'), 'missing "unconstrained_array_returns" entry') self.assertTrue( parser.has_option(PG_ADS_2, 'unconstrained_array_returns'), 'missing "unconstrained_array_returns" entry') self.assertTrue( parser.has_option(PG_ADS_3, 'unconstrained_array_returns'), 'missing "unconstrained_array_returns" entry') self.assertTrue( parser.has_option(PG_ADS_4, 'unconstrained_array_returns'), 'missing "unconstrained_array_returns" entry') self.assertTrue( parser.has_option(PG_ADS_5, 'recursive_subprograms'), 'missing "recursive_subprograms" entry') self.assertTrue( parser.has_option(PG_ADS_6, 'recursive_subprograms'), 'missing "recursive_subprograms" entry') self.assertTrue( parser.has_option(PGG_ADS_1, 'unconstrained_array_returns'), 'missing "unconstrained_array_returns" entry') self.assertTrue( parser.has_option(PGG_ADS_2, 'warnings'), 'missing "warnings" entry') self.assertTrue( parser.has_option(SIMPLE_ADB_1, 'improper_returns'), 'missing "improper_returns" entry') self.assertTrue( parser.has_option(SIMPLE_ADB_3, 'identifier_suffixes'), 'missing "identifier_suffixes" entry') self.assertTrue( parser.has_option(SIMPLE_ADB_3, 'identifier_prefixes'), 'missing "identifier_prefixes" entry')
def testDatabaseContent(self): PROJECT = Project.simple() # Create path to /obj and /codepeer folders OBJ_FOLDER = 'obj' OBJ_PATH = os.path.join(Project.simple().install_dir, OBJ_FOLDER) CODEPEER_FOLDER = 'codepeer' CODEPEER_PATH = os.path.join(OBJ_PATH, CODEPEER_FOLDER) # Run GNAThub with Codepeer gnathub = GNAThub(PROJECT, plugins=['codepeer']) # Check that /obj and /codepeer folders exists assert os.path.exists(OBJ_PATH), 'Object folder should exist' assert os.path.exists(CODEPEER_PATH), 'Codepeer folder should exist' # Check the existence of expected simple.csv file CSV_COUNT = 0 CSV_PATH = CODEPEER_PATH for entry in os.listdir(CODEPEER_PATH): if entry.endswith('.csv') and entry.startswith('simple'): CSV_COUNT += 1 CSV_PATH = os.path.join(CSV_PATH, entry) assert CSV_COUNT == 1, 'simple.csv should be generated in /obj/codepeer folder' assert os.path.isfile(CSV_PATH), 'simple.csv is a file' # Check .csv content columns = defaultdict( list) # each value in each column is appended to a list with open(CSV_PATH) as f: # read rows into a dictionary format reader = csv.DictReader(f) # read a row as {column1: value1, column2: value2,...} for row in reader: # go over each column name and value for (k, v) in row.items(): # append the value into the appropriate list based on column name k columns[k].append(v) # Check column 'Line' self.assertListEqual(columns['Line'], EXPECTED_LINES_FROM_CSV) # Check column 'Column' self.assertListEqual(columns['Column'], EXPECTED_COLUMNS_FROM_CSV) # Check column 'Category' self.assertListEqual(columns['Category'], EXPECTED_CATEGORIES_FROM_CSV) # Check column 'Ranking' self.assertListEqual(columns['Ranking'], EXPECTED_RANKINGS_FROM_CSV) # Check column 'Kind' self.assertListEqual(columns['Kind'], EXPECTED_KINDS_FROM_CSV) # Check column 'Message' self.assertListEqual(columns['Message'], EXPECTED_MESSAGES_FROM_CSV) # Checks the analysis report results are as expected script_output_file = os.path.abspath('script.out') gnathub.run(script=Script.db2cfg(), output=script_output_file) parser = SafeConfigParser() parser.optionxform = str parser.read(script_output_file) self.assertListEqual(sorted(parser.sections()), RESULTS)
def testIncrementalSwitch(self): PROJECT = Project.simple() # Define 'obj' folder name and path OBJ_FOLDER = 'obj' OBJ_PATH = os.path.join(PROJECT.install_dir, OBJ_FOLDER) # 1st step: Run GNAThub without '--incremental' switch assert not os.path.exists(OBJ_PATH), 'Object folder should not exist' gnathub = GNAThub(PROJECT, plugins=['codepeer']) assert os.path.exists(OBJ_PATH), 'Object folder should exist' # Check that results are generated in /obj METRIX_FILES_COUNT = 0 METRIX_FILES_FOUND = False GNATHUB_DIR_FOUND = False GNATHUB_DIR_LEN = 0 CODEPEER_DIR_FOUND = False CODEPEER_DIR_LEN = 0 for entry in os.listdir(OBJ_PATH): if entry.endswith('.metrix') or entry.startswith('metrix'): METRIX_FILES_COUNT += 1 elif os.path.isdir(os.path.join(OBJ_PATH, entry)): crt_path = os.path.join(OBJ_PATH, entry) if entry == 'gnathub': GNATHUB_DIR_FOUND = True GNATHUB_DIR_LEN = len(os.listdir(crt_path)) elif entry == 'codepeer': CODEPEER_DIR_FOUND = True CODEPEER_DIR_LEN = len(os.listdir(crt_path)) METRIX_FILE_FOUND = METRIX_FILES_COUNT != 0 assert not METRIX_FILE_FOUND, 'No metrix should be generated in /obj folder' assert GNATHUB_DIR_FOUND, 'GNAThub directory should be generated in /obj folder' assert CODEPEER_DIR_FOUND, 'Codepeer directory should be generated in /obj folder' # Check DB content gnathub.run(script='check-db-codepeer.py') # 2nd step: Run GNAThub without '--incremental' switch gnathub = GNAThub(PROJECT, plugins=['gnatmetric']) GNATHUB_DIR_FOUND = False GNATHUB_DIR_LEN = 0 CODEPEER_DIR_FOUND = False CODEPEER_DIR_LEN = 0 for entry in os.listdir(OBJ_PATH): if entry.endswith('.metrix') or entry.startswith('metrix'): METRIX_FILES_COUNT += 1 elif os.path.isdir(os.path.join(OBJ_PATH, entry)): crt_path = os.path.join(OBJ_PATH, entry) if entry == 'gnathub': GNATHUB_DIR_FOUND = True GNATHUB_DIR_LEN = len(os.listdir(crt_path)) elif entry == 'codepeer': CODEPEER_DIR_FOUND = True CODEPEER_DIR_LEN = len(os.listdir(crt_path)) METRIX_FILE_FOUND = METRIX_FILES_COUNT != 0 assert METRIX_FILE_FOUND, 'Metrix should be generated in /obj folder' assert GNATHUB_DIR_FOUND, 'GNAThub directory should be generated in /obj folder' assert CODEPEER_DIR_FOUND, 'Codepeer directory should be generated in /obj folder' # Check DB content after gnatmetric run gnathub.run(script='check-db-metrics.py') # 3rd step: Run GNAThub with '--incremental' switch gnathub = GNAThub(PROJECT, plugins=['codepeer'], incremental=True) # Check DB content after gnatmetric run gnathub.run(script='check-db-incremental.py') # 4th step: Run GNAThub with '--incremental' switch gnathub = GNAThub(PROJECT, plugins=['codepeer'], incremental=True) # Check DB content after gnatmetric run gnathub.run(script='check-db-incremental.py') # 5th step: Run GNAThub with '--incremental' switch gnathub = GNAThub(PROJECT, plugins=['gnatmetric'], incremental=True) # Check DB content after gnatmetric run gnathub.run(script='check-db-incremental.py')
class TestCoverageExhaustiveExample(TestCase): Violation = namedtuple('Violation', 'level column message') CoverageRecord = namedtuple('CoverageRecord', 'file rule line column message') EXPECTED_COVERAGE = { 'test_stmt.adb': { 10: Violation('stmt', 7, 'not executed'), 14: Violation('stmt', 42, 'not executed'), 18: [ Violation('stmt', 7, 'not executed'), Violation('stmt', 19, 'not executed') ], }, 'test_decision.adb': { 6: Violation('decision', 7, 'outcome FALSE never exercised'), 11: Violation('decision', 11, 'outcome TRUE never exercised'), 12: Violation('stmt', 7, 'not executed'), 15: Violation('stmt', 7, 'not executed'), 16: Violation('stmt', 10, 'not executed'), }, 'test_mcdc.adb': { 6: Violation( 'mcdc', 7, 'has no independent influence pair, MC/DC not achieved' ) }, } def setUp(self): self.longMessage = True # Just import the project. Do no try to build it nor to run it: we # don't expect GNATcoverage to be available during testsuite runs, so # use the precomputed coverage reports instead. self.project = Project.coverage_exhaustive() # Run GNAThub with only the GNATcoverage plugin self.gnathub = GNAThub(self.project, plugins=['gnatcoverage']) # Extract coverage information from the database script_output_file = os.path.abspath('script.out') self.gnathub.run(script=Script.db2cfg(), output=script_output_file) self.parser = SafeConfigParser() self.parser.optionxform = str self.parser.read(script_output_file) def structured_to_list_coverage(self, struct): """ Turn structured coverage expectations into a list of CoverageRecord. See TestCoverag.ExhaustiveExample.EXPECTED_COVERAGE for a structured coverage example. """ result = [] for filename, record in struct.iteritems(): for line, record in record.iteritems(): # Shortcut: if there is only one message on a line, do not # require a sequence of messages. if isinstance(record, (basestring, self.Violation)): record = [record] for content in record: def get_rec(rule, column, message): return self.CoverageRecord( filename, rule, line, column, message) if isinstance(content, self.Violation): result.append(get_rec( content.level, content.column, content.message )) result.sort() return result def config_to_list_coverage(self, config): """Turn a ConfigParser instance into a list of CoverageRecord.""" result = [] for section in config.sections(): # There are no file-wide sections we are interested in: skip them try: filename, sloc = section.rsplit(None, 1) line, column = sloc.split(':') except ValueError: continue line = int(line) column = int(column) for name, value in config.items(section): result.append(self.CoverageRecord( filename, name, line, column, value )) result.sort() return result def testDatabaseContent(self): # Test that GNAThub extract the coverage information we expect it to self.assertEqual( self.config_to_list_coverage(self.parser), self.structured_to_list_coverage(self.EXPECTED_COVERAGE) )
class TestSonarScannerSupport(TestCase): def setUp(self): self.longMessage = True plugins = ['gnatcheck'] if os.environ["WITH_SONAR"]: plugins = ['gnatcheck', 'sonar-config', 'sonar-scanner'] self.gnathub = GNAThub(Project.simple_sonar(), plugins=plugins) def testDatabaseContent(self): script_output_file = os.path.abspath('script.out') self.gnathub.run(script=Script.db2cfg(), output=script_output_file) parser = ConfigParser() parser.optionxform = str parser.read(script_output_file) self.assertEqual(len(parser.sections()), 26) if os.environ["WITH_SONAR"]: user = '******' pwd = 'admin' def check_Ada_language(): ''' Check if Ada language is supported ''' SONAR_LANGUAGES_URL = 'http://localhost:9000/api/languages/list' session = requests.Session() session.auth = user, pwd call = getattr(session, 'get') response = call(SONAR_LANGUAGES_URL) self.assertTrue( response.status_code < 300, 'Expected < 300 return HTT response (OK).' + 'Got %s' % str(response.status_code)) binary = response.content output = json.loads(binary) # pprint.pprint(output) ADA_LANG = False for record in output['languages']: if record['key'] == 'ada' and record['name'] == 'Ada': ADA_LANG = True break else: continue self.assertTrue(ADA_LANG, 'Ada language is not supported') session.close() def check_Ada_quality_profile(): ''' Check if Ada quality profile is supported ''' SONAR_QP_URL = 'http://localhost:9000/api/qualityprofiles/search' session = requests.Session() session.auth = user, pwd call = getattr(session, 'get') response = call(SONAR_QP_URL) self.assertTrue( response.status_code < 300, 'Expected < 300 return HTT response (OK).' + 'Got %s' % str(response.status_code)) binary = response.content output = json.loads(binary) # pprint.pprint(output) GNATDASHBOARD_WAY = False LANGUAGE_ADA = False ACTIVE_RULES_COUNT = 0 for record in output['profiles']: if 'name' in record and record[ 'name'] == 'GNATdashboard way': GNATDASHBOARD_WAY = True if 'language' in record and record['language'] == 'ada': LANGUAGE_ADA = (record['languageName'] == 'Ada') ACTIVE_RULES_COUNT = record['activeRuleCount'] else: continue self.assertTrue( GNATDASHBOARD_WAY, 'missing GNATdashboard way from Quality Profile') self.assertEqual( ACTIVE_RULES_COUNT, 476, 'unexpected value for Ada active rules.' + ' Found "%s" instead of 476' % str(ACTIVE_RULES_COUNT)) session.close() def check_Ada_project_uploaded(): ''' Check if Ada project is uploaded The exected uploaded project name is 'Simple_Sonar' ''' SONAR_SEARCH_URL = 'http://localhost:9000/api/projects/search' EXPECTED_NAME = 'Simple_Sonar' session = requests.Session() session.auth = user, pwd call = getattr(session, 'get') response = call(SONAR_SEARCH_URL) self.assertTrue( response.status_code < 300, 'Expected < 300 return HTT response (OK).' + 'Got %s' % str(response.status_code)) binary = response.content output = json.loads(binary) # pprint.pprint(output) for record in output['components']: self.assertTrue( 'key' in record, 'missing entry for name in record %s' % str(record)) self.assertEqual( record['key'], EXPECTED_NAME, 'unexpected value for "%s"' % record['key']) self.assertTrue( 'name' in record, 'missing entry for name in record %s' % str(record)) self.assertEqual( record['name'], EXPECTED_NAME, 'unexpected value for "%s"' % record['name']) session.close() def check_Ada_project_analysis(): ''' Check if Ada project has expected number of code_smells For the dashboard project 'Simple_Sonar' 17 code smells are expected The request should be http://localhost:9000/api/measures/component? component=Simple_Sonar&metricKeys=reliability_rating,bugs,code_smells The expected response is {"component":{"id":"AXfP0Sfu2OokptDLjqm9", "key":"Simple_Sonar", "name":"Simple_Sonar", "qualifier":"TRK", "measures":[{"metric":"reliability_rating", "value":"1.0","bestValue":true}, {"metric":"bugs","value":"0","bestValue":true}, {"metric":"code_smells","value":"17","bestValue":false}]}} ''' PROJECT_KEY = 'Simple_Sonar' METRICS_LIST = ['reliability_rating', 'code_smells', 'bugs'] RQ_PARAMS = { 'component': PROJECT_KEY, 'metricKeys': ','.join(METRICS_LIST) } SONAR_MEASURES_URL = 'http://localhost:9000/api/measures/component' session = requests.Session() session.auth = user, pwd call = getattr(session, 'get') # Sleep for 30 seconds to be sure that the analysis results are uploaded time.sleep(30) response = call(SONAR_MEASURES_URL, params=RQ_PARAMS) self.assertTrue( response.status_code < 300, 'Expected < 300 return HTT response (OK).' + 'Got %s' % str(response.status_code)) print(response.url) print(response.text) json_data = response.json( ) if response and response.status_code == 200 else None self.assertTrue(json_data and 'component' in json_data, 'unexpected value for returned data') self.assertTrue('measures' in json_data['component'], 'unexpected value for returned data') self.assertTrue( len(json_data['component']['measures']) != 0, 'no metric is found in measures') # Gather the informations from the response reliability_rating = 0.0 bugs = 0 code_smells = 0 for record in json_data['component']['measures']: if record['metric'] == 'reliability_rating': reliability_rating = record['value'] if record['metric'] == 'bugs': bugs = int(record['value']) if record['metric'] == 'code_smells': code_smells = int(record['value']) self.assertEqual(bugs, 0, 'Not expected metric %s' % str(bugs)) self.assertEqual(code_smells, 17, 'Not expected metric %s' % str(code_smells)) session.close() # Check if the Sonar Ada plugin is used # 1. Check if the Ada language is supported check_Ada_language() # 2. Check if the Ada quality profile is present check_Ada_quality_profile() # 3. Check if the uploaded project name is 'Simple_Sonar' check_Ada_project_uploaded() # 4. Check if the expected metrics are uploaded for 'Simple_Sonar' check_Ada_project_analysis()