|
| 1 | +""" |
| 2 | +Check that "gnatcov extract-base64-trace" can extract trace data from outputs |
| 3 | +with arbitrary line prefixes. |
| 4 | +""" |
| 5 | + |
| 6 | +import itertools |
| 7 | + |
| 8 | +from SCOV.instr import xcov_convert_base64, xcov_instrument |
| 9 | +from SCOV.minicheck import check_xcov_reports |
| 10 | +from SUITE.context import thistest |
| 11 | +from SUITE.cutils import Wdir |
| 12 | +from SUITE.gprutils import GPRswitches |
| 13 | +from SUITE.tutils import exepath_to, gprbuild, gprfor, run_cov_program, xcov |
| 14 | + |
| 15 | +tmp = Wdir("tmp_") |
| 16 | + |
| 17 | +# Instrument and build the test project |
| 18 | +p = GPRswitches( |
| 19 | + root_project=gprfor(srcdirs="..", mains=["main1.adb", "main2.adb"]), |
| 20 | + units=["pkg"], |
| 21 | +) |
| 22 | +xcov_instrument( |
| 23 | + gprsw=p, |
| 24 | + covlevel="stmt", |
| 25 | + dump_trigger="main-end", |
| 26 | + dump_channel="base64-stdout", |
| 27 | +) |
| 28 | +gprbuild(p.root_project, trace_mode="src") |
| 29 | + |
| 30 | +# Run the two programs, saving their output |
| 31 | +mains = ["main1", "main2"] |
| 32 | +out_files = [f"{m}_output.txt" for m in mains] |
| 33 | + |
| 34 | +for main, out_file in zip(mains, out_files): |
| 35 | + run_cov_program(exepath_to(main), out=out_file) |
| 36 | + |
| 37 | +# Expected coverage report for both program executions: |
| 38 | +main1_cov = {"pkg.adb.xcov": {"+": {11, 14}, "-": {12}}} |
| 39 | +main2_cov = {"pkg.adb.xcov": {"+": {11, 12}, "-": {14}}} |
| 40 | + |
| 41 | +# Now combine them with different prefixes and check that "gnatcov |
| 42 | +# extract-base64-trace" correctly loads the last one. |
| 43 | +for label, interleaved_outputs, trailing_output, cov in [ |
| 44 | + ("main1_first", out_files, None, main1_cov), |
| 45 | + ("main2_first", [out_files[1], out_files[0]], None, main2_cov), |
| 46 | + ("main2_trail", [out_files[0]], out_files[1], main2_cov), |
| 47 | +]: |
| 48 | + thistest.log(f"== {label} ==") |
| 49 | + |
| 50 | + tracedata_filename = f"{label}_trace_data.txt" |
| 51 | + trace_filename = f"{label}.srctrace" |
| 52 | + |
| 53 | + traces_lines = [] |
| 54 | + for i, filename in enumerate(interleaved_outputs): |
| 55 | + with open(filename) as f: |
| 56 | + traces_lines.append((f"DATA{i}> ", f.readlines())) |
| 57 | + |
| 58 | + if trailing_output is None: |
| 59 | + trailing_lines = [] |
| 60 | + else: |
| 61 | + with open(trailing_output) as f: |
| 62 | + trailing_lines = f.readlines() |
| 63 | + |
| 64 | + # Interleave all lines to make sure that once gnatcov detects a source |
| 65 | + # trace with some prefix, it filters that prefix only until the end of the |
| 66 | + # source trace. |
| 67 | + with open(tracedata_filename, "w") as f: |
| 68 | + for i in itertools.count(0): |
| 69 | + had_line = False |
| 70 | + for prefix, lines in traces_lines: |
| 71 | + if i >= len(lines): |
| 72 | + continue |
| 73 | + had_line = True |
| 74 | + f.write(prefix) |
| 75 | + f.write(lines[i]) |
| 76 | + if not had_line: |
| 77 | + break |
| 78 | + |
| 79 | + for line in trailing_lines: |
| 80 | + f.write("TRAILING> ") |
| 81 | + f.write(line) |
| 82 | + |
| 83 | + # Now run the source trace conversion and create a coverage report from it |
| 84 | + xcov_convert_base64(tracedata_filename, trace_filename) |
| 85 | + xcov_dir = f"xcov-{label}" |
| 86 | + xcov( |
| 87 | + [ |
| 88 | + "coverage", |
| 89 | + "-P", |
| 90 | + p.root_project, |
| 91 | + "--level=stmt", |
| 92 | + "--annotate=xcov", |
| 93 | + "--output-dir", |
| 94 | + xcov_dir, |
| 95 | + trace_filename, |
| 96 | + ] |
| 97 | + ) |
| 98 | + check_xcov_reports("*.xcov", cov, cwd=xcov_dir) |
| 99 | + |
| 100 | +thistest.result() |
0 commit comments