def try_one(subdir, extra_covargs, xreports, xwarnings): """ Setup a temp ``subdir`` and perform a build/run/coverage sequence for our example, passing ``extra_covargs`` in addition to gnatcov coverage. Verify that we obtain the reports stated as expected in ``xreports``, and that possible warnings on units-of-interest discrepancies (induced by the extra covargs), stated as expected in ``xwarnings``, are found in the logs. """ wd.to_subdir(subdir) gpr = gprfor(srcdirs="../src", mains="test_t.adb", extra='\n'.join( ['for Source_Files use', ' ("test_t.adb","flip.ads", "flip.adb");'])) build_run_and_coverage( gprsw=GPRswitches(root_project=gpr), covlevel='stmt', mains=['test_t'], extra_coverage_args=['--annotate=xcov'] + extra_covargs) check_xcov_reports('obj/*.xcov', xreports) wlog = contents_of('coverage.log') for xw in xwarnings: thistest.fail_if( xw not in wlog, 'expected warning "%s" not found in log' % xw) wd.to_homedir()
def run_test(label, slug, main, helper, recursive, projects=[], units=[], projects_warned=[], expected_cov_list=[]): """ Produce a coverage report for the given parameters and check the emitted warnings. :param str label: Label for this test. :param str slug: Unique short string for this test (used to create directories). :param ProjectConfig main: Configuration for the "main" project. :param ProjectConfig helper: Configuration for the "helper" project. :param bool recursive: Whether to not pass --no-subprojects. :param list[str] projects: List of projects to pass with --projects. :param list[str] units: List of units to pass with --units. :param list[str] projects_warned: List of projects for which we expected warnings. :param expected_cov: List of expected coverage reports. """ thistest.log('== [{}] {} =='.format(slug, label)) tmp.to_subdir('wd_/{}'.format(slug)) expected_output = '\n'.join( 'warning: project {} provides no unit of interest' .format(project) for project in projects_warned) # Generate projects for this test (see below for the description of each # project). ProjectConfig().generate('empty') helper.generate('helper') main_prj = main.generate('main', deps=['empty', 'helper'], mains=['main.adb']) mkdir('obj-empty') mkdir('obj-helper') mkdir('obj-main') # Generate a coverage report for them build_run_and_coverage( gprsw=GPRswitches(root_project=main_prj, projects=projects, units=units, no_subprojects=not recursive), covlevel='stmt', mains=['main'], gpr_obj_dir='obj-main', extra_coverage_args=['-axcov']) log_file = ('coverage.log' if thistest.options.trace_mode == 'bin' else 'instrument.log') thistest.fail_if_not_equal( '[{}/{}] gnatcov output'.format(label, slug), expected_output, contents_of(log_file).strip()) expected_cov = {} for c in expected_cov_list: expected_cov.update(c) check_xcov_reports('obj-*/*.xcov', expected_cov)
def run(extra_args, covlevel=None): """ Build and run the single test program, which volontarily performs stmt and decision coverage violations. """ xcov_args = build_and_run(gprsw=GPRswitches(root_project=gpr), covlevel=covlevel, mains=[pgm], extra_coverage_args=[]) xcov(xcov_args + extra_args)
def try_one_gpr(gpr, no_such): label = os.path.basename(os.getcwd()) dump = 'xcov.out' build_run_and_coverage( gprsw=GPRswitches(root_project=gpr), covlevel='stmt', mains=['p'], extra_coverage_args=['-axcov'], out=dump, register_failure=False) dump = contents_of(dump) expected_warning = ( 'no unit {} in project gen (coverage.units attribute)'.format(no_such) if no_such else 'no unit of interest') thistest.fail_if( expected_warning not in dump, '[{}] missing warning on absence of ALI for unit'.format(label))
def mode_build(self): # We first need to instrument, with proper selection of the units of # interest. Expect we are to provide this through a project file as # we have no LI file at hand: assert self.gprmode # If we have a request for specific options, honor that. Otherwise, # use the already computed project file for this test: if self.covctl and self.covctl.gprsw: instrument_gprsw = self.covctl.gprsw else: instrument_gprsw = GPRswitches(root_project=self.gpr) xcov_instrument(covlevel=self.xcovlevel, checkpoint='instr.ckpt', gprsw=instrument_gprsw) # Now we can build, instructing gprbuild to fetch the instrumented # sources in their dedicated subdir: gprbuild(self.gpr, extracargs=self.extracargs, gargs='--src-subdirs=gnatcov-instr')
def mode_build(self): # We first need to instrument, with proper selection of the units of # interest. Expect we are to provide this through a project file as # we have no LI file at hand: assert self.gprmode # If we have a request for specific options, honor that. Otherwise, # use the already computed project file for this test: if self.covctl and self.covctl.gprsw: instrument_gprsw = self.covctl.gprsw else: instrument_gprsw = GPRswitches(root_project=self.gpr) out = 'xinstr.out' xcov_instrument( covlevel=self.xcovlevel, isi_file=self.ISI_FILE, extra_args=to_list(self.covctl.covoptions) if self.covctl else [], gprsw=instrument_gprsw, gpr_obj_dir=self.gpr_obj_dir, out=out) # Standard output might contain warnings indicating instrumentation # issues. This should not happen, so simply fail as soon as the output # file is not empty. thistest.fail_if( os.path.getsize(out) > 0, 'xcov instrument standard output not empty ({}):' '\n--' '\n{}'.format(out, contents_of(out))) # Now we can build, instructing gprbuild to fetch the instrumented # sources in their dedicated subdir: gprbuild(self.gpr, extracargs=self.extracargs, gargs='--src-subdirs=gnatcov-instr')
def run(subdir, extra_args, covlevel=None): """ Build and run the single test program, which volontarily performs stmt and decision coverage violations. """ dirname = f"tmp_{subdir}" wd = Wdir(dirname) gpr = gprfor(mains=[pgm + '.adb'], srcdirs=['../src'], extra=gprcov_for(switches=[ Csw('*', ['--level=stmt']), Csw('coverage', ['--annotate=report']) ])) xcov_args = build_and_run( gprsw=GPRswitches(root_project=gpr), covlevel=covlevel, mains=[pgm], extra_coverage_args=[] if covlevel is None else ['--level', covlevel]) xcov(xcov_args + extra_args) wd.to_homedir() return dirname
tmp = Wdir('wd_') # The "orig" project contains two units: "main" and "helper". The "ext" project # extends "orig" and overrides only the "helper" unit. # # Previously, gnatcov used to consider that the only unit of interest was # "helper". It now also consider that "main" is a unit of interest. orig_prj = gprfor(prjid='orig', mains=['main.adb'], srcdirs='..') ext_prj = 'ext.gpr' with open(ext_prj, 'w') as f: f.write(""" project Ext extends "{}" is for Source_Dirs use ("../src-ext"); for Object_Dir use "obj-ext"; end Ext; """.format(orig_prj)) build_run_and_coverage( gprsw=GPRswitches(root_project=ext_prj), covlevel='stmt', mains=['main'], extra_coverage_args=['-axcov'], gpr_exe_dir='obj-ext') check_xcov_reports('obj-ext/*.xcov', { 'obj-ext/main.adb.xcov': {'+': {5}}, 'obj-ext/helper.ads.xcov': {}, 'obj-ext/helper.adb.xcov': {'+': {4}}}) thistest.result()
""" Check that invalid units passed as project attributes are properly reported. """ from SCOV.minicheck import build_run_and_coverage, check_xcov_reports from SUITE.context import thistest from SUITE.gprutils import GPRswitches, gprcov_for from SUITE.cutils import Wdir, contents_of from SUITE.tutils import gprfor tmp = Wdir('wd_') build_run_and_coverage(gprsw=GPRswitches( root_project=gprfor('main.adb', srcdirs='..', extra=gprcov_for(units_in=['no_such_unit', 'main']))), covlevel='stmt', mains=['main'], extra_coverage_args=['-axcov']) log_file = ('coverage.log' if thistest.options.trace_mode == 'bin' else 'instrument.log') thistest.fail_if_not_equal( 'gnatcov output', 'warning: no unit no_such_unit in project gen (coverage.units attribute)', contents_of(log_file).strip()) check_xcov_reports('obj/*.xcov', {'obj/main.adb.xcov': {'+': {5}}}) thistest.result()
pgm = 'test_lt0' wd = Wdir('wd_') # Build and run the single test program, which volontarily performs stmt and # decision coverage violations. # # Enforce --level=stmt+decision here. Check that Default_Switches in # subprojects are ignored gpr = gprfor(mains=[pgm + '.adb'], srcdirs=['../src'], deps=['../App/app'], extra=gprcov_for(switches=[ Csw('*', ['--level=stmt+decision']), Csw('coverage', ['--annotate=report']) ])) build_run_and_coverage(gprsw=GPRswitches(root_project=gpr, no_subprojects=False), covlevel=None, mains=[pgm], extra_coverage_args=['-o', 'def.rep']) # Check that we get results corresponding to the root project file despite # "overrides" (other Switches) in subprojects. rep = contents_of('def.rep') thistest.fail_if(not os.path.exists('def.rep'), "couldn't find default report") thistest.fail_if(not re.search('statement not executed', rep), 'missing expected stmt coverage failure indication') thistest.fail_if(
from SCOV.minicheck import build_run_and_coverage from SUITE.context import thistest from SUITE.cutils import Wdir from SUITE.gprutils import GPRswitches from SUITE.tutils import gprfor wd = Wdir('wd_', clean=True) gprname = 'p' mainunit = 'foo.adb' subdir = 'gnatcov' mainunit_xcov = os.path.join('obj', subdir, mainunit + '.xcov') # Arrange to build, run and perform coverage analysis passing # --subdirs to all gprbuild and gnatcov commands, then verify that # we find a report in the designated subdir afterwards. build_run_and_coverage(gprsw=GPRswitches(root_project=gprfor(prjid=gprname, mains=[mainunit], srcdirs=['..']), subdirs=subdir), covlevel='stmt', mains=['foo'], extra_coverage_args=['-a', 'xcov']) thistest.fail_if(not os.path.exists(mainunit_xcov), 'The coverage report is missing: {}'.format(mainunit_xcov)) thistest.result()
from SUITE.cutils import Wdir, contents_of from SUITE.tutils import gprfor tmp = Wdir('wd_', clean=True) mkdir('obj-helper') mkdir('obj-main') helper_prj = gprfor(prjid='helper', mains=[], langs=['Ada'], srcdirs='../src-helper', objdir='obj-helper') main_prj = gprfor(prjid='main', mains=['main.adb'], langs=['Ada'], deps=['helper'], srcdirs='../src-main', objdir='obj-main') build_run_and_coverage( gprsw=GPRswitches(root_project=main_prj, projects=['helper'], units=['helper', 'main']), covlevel='stmt', mains=['main'], gpr_obj_dir='obj-main', extra_coverage_args=['-axcov']) log_file = ('coverage.log' if thistest.options.trace_mode == 'bin' else 'instrument.log') thistest.fail_if_not_equal( 'gnatcov output', 'warning: no unit main (from --units) in the projects of interest', contents_of(log_file).strip()) check_xcov_reports('obj-*/*.xcov', {'obj-main/helper.adb.xcov': {'+': {3}}})
def check(root_project, recurse, projects=None, units=None, xreports=None): """ Check that running our test with -P`root_project` [--projects=... for `projects`] [--units=... for `units`] [--no-subprojects] (if `recurse` is False) we obtain reports for the units attached to the projects listed in `xreports`. If not None, `projects` and `xreports` are expected to be lists of shortcut names like 'boolops', 'intops' or 'counters'. This function takes care of converting them to relative project file names actually expected on the command line or in real project file dependencies. `root_project` may be either a .gpr filename, in which case it is used as-is, or a project short name. `recurse` None means "arrange not to pass any option influencing recursiveness". """ projects = to_list(projects) units = to_list(units) # root_project, projects, and units arguments we will provide to the # GPRswitches class: gprsw_root_project = (root_project if root_project.endswith('.gpr') else _gpr_for(root_project)) gprsw_projects = [_gpr_for(prj) for prj in projects] gprsw_units = units # Arrange to execute each check in its own tmp dir and # passing a unique --subdirs prevent mixups across test variants # within the shared projects. # Start with 'wd_foo' from .../.../foo.gpr or a project short # name intended for -P. tmpdir = 'wd_' + os.path.basename(root_project).split('.')[0] # Append the first letter of each project name will pass through # --project, if any: if projects: tmpdir += '-' + ''.join(prj[0] for prj in projects) # Append indication on recursion request: if recurse: tmpdir += '-rt' elif recurse is None: tmpdir += '-rn' else: tmpdir += '-rf' # For the --subdirs argument, relative to each subproject's object dir, # prepend our testcase local directory name: gprsw_subdirs = os.path.basename(os.getcwd()) + '_' + tmpdir # If a list of expected reports is provided, convert into list of # corresponding sources, which the CovControl class expects: if xreports is not None: ctl_xreports = [] for xr in xreports: ctl_xreports.extend(_xreports[xr] if xr in _xreports else [xr]) else: ctl_xreports = None # Getting the default behavior wrt recursiveness consists # in requesting not to pass --no-subprojects. gprsw_no_subprojects = False if recurse is None else not recurse wd = Wdir(clean=True) wd.to_subdir(tmpdir) TestCase(category=None).run(covcontrol=CovControl( # The programs we build and exercise alway depend on # the three subprojects: deps=[_gpr_for('boolops'), _gpr_for('intops'), _gpr_for('counters')], # What we analyse and check depends on our arguments: gprsw=GPRswitches(root_project=gprsw_root_project, projects=gprsw_projects, units=gprsw_units, no_subprojects=gprsw_no_subprojects, subdirs=gprsw_subdirs, xvars=[('BOARD', env.target.machine)]), xreports=ctl_xreports, # The test driver and the likes are never of interest units_in=[])) wd.to_homedir()
import os import e3.fs root_project = os.path.abspath('root.gpr') board_arg = '-XBOARD={}'.format(env.target.machine) wd = Wdir('wd_') # Every project of interest is setup to feature a single unit named after the # project. Dumping the units of interest for a given analysis, with # --dump-units-to then indirectly gives us the list of projects of interest. # --dump-units-to is a gnatcov coverage option. We just need to build and run # the dummy "root" program once, and we can then proceed with all the coverage # --dump-units-to tests we need. cov_cmdline = build_and_run(gprsw=GPRswitches(root_project=root_project), mains=['root'], covlevel='stmt', gpr_obj_dir='../obj', gpr_exe_dir='../obj', extra_args=[board_arg], extra_coverage_args=[]) trace = cov_cmdline[-1] def check(options, xunits): """ Check that running gnatcov coverage with the provided list of `options` for project selection yields `xunits` as the set of units of interest, conveyed as a list. Verify the list of units
mkdir('obj-main') helper_prj = gprfor(prjid='helper', mains=[], langs=['Ada'], srcdirs='../src-helper', objdir='obj-helper') main_prj = gprfor(prjid='main', mains=['main.adb'], langs=['Ada'], deps=['helper'], srcdirs='../src-main', objdir='obj-main') build_run_and_coverage(gprsw=GPRswitches(root_project=main_prj, projects=['helper'], units=['helper', 'main']), covlevel='stmt', mains=['main'], gpr_obj_dir='obj-main', extra_coverage_args=['-axcov']) log_file = ('coverage.log' if thistest.options.trace_mode == 'bin' else 'instrument.log') thistest.fail_if_not_equal( 'gnatcov output', 'warning: no unit main (from --units) in the projects of interest', contents_of(log_file).strip()) check_xcov_reports('obj-*/*.xcov', {'obj-main/helper.adb.xcov': {'+': {3}}})
import os.path from SCOV.tc import TestCase from SCOV.tctl import CovControl from SUITE.context import thistest from SUITE.gprutils import GPRswitches libdep = os.path.abspath('mylib/mylib.gpr') TestCase().run(covcontrol=CovControl( deps=[libdep], gprsw=GPRswitches(root_project='gen.gpr', units=['foo', 'bar', 'klunk']))) thistest.result()
from SCOV.minicheck import build_run_and_coverage, check_xcov_reports from SUITE.context import thistest from SUITE.gprutils import GPRswitches from SUITE.cutils import Wdir from SUITE.tutils import gprfor wd = Wdir('wd_', clean=True) build_run_and_coverage(gprsw=GPRswitches(root_project=gprfor( mains=['test_lt0.adb'], srcdirs=['../src'], deps=['../App/app'])), covlevel='stmt', mains=['test_lt0'], extra_args=['--projects=app_base'], extra_coverage_args=['-axcov']) # App_Base is extended by App; App overrides Coverage'Units so that only Values # (not Values.Aux) is selected. check_xcov_reports('obj/*.xcov', { 'obj/values.ads.xcov': {}, 'obj/values.adb.xcov': { '+': {5, 6}, '-': {8} } }) thistest.result()
""" Check that invalid units passed as --units are properly reported. """ from SCOV.minicheck import build_run_and_coverage, check_xcov_reports from SUITE.context import thistest from SUITE.gprutils import GPRswitches from SUITE.cutils import Wdir, contents_of from SUITE.tutils import gprfor tmp = Wdir('wd_') build_run_and_coverage(gprsw=GPRswitches( root_project=gprfor('main.adb', srcdirs='..'), units=['no_such_unit', 'main', 'helper.say_hello']), covlevel='stmt', mains=['main'], extra_coverage_args=['-axcov']) log_file = ('coverage.log' if thistest.options.trace_mode == 'bin' else 'instrument.log') # Split and re-join lines to avoid spurious CR/LF diffs on Windows. Also sort # lines, as the order in which these warnings is emitted is not deterministic. log_lines = '\n'.join( sorted(line.rstrip() for line in contents_of(log_file).splitlines())).rstrip() thistest.fail_if_not_equal( 'gnatcov output', 'warning: no unit helper.say_hello (from --units) in the projects of'
import os.path from SCOV.tc import TestCase from SCOV.tctl import CovControl from SUITE.context import thistest from SUITE.gprutils import GPRswitches libdep = os.path.abspath('mylib/mylib.gpr') TestCase().run(covcontrol=CovControl( deps=[libdep], gprsw=GPRswitches(root_project='gen.gpr', recursive=True))) thistest.result()