How to use Skylib unittest to test cpp compiler command line?

85 views
Skip to first unread message

Konstantin

unread,
Oct 16, 2022, 1:50:15 PM10/16/22
to bazel-discuss
We have custom C++ compilation rule which invokes cc_common.compile and try to add a test that it properly translates its attributes to the compiler command line parameters. 
My attempt at the test code looks like this:

load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts")

def _local_includes_test_impl(ctx):
    env = analysistest.begin(ctx)
    target_under_test = analysistest.target_under_test(env)
    actions = analysistest.target_actions(env)

    compile_action = None
    for action in actions:  # action has type Action. See https://bazel.build/rules/lib/Action
        if action.mnemonic == "CppCompile":
            compile_action = action
            break
    if not compile_action:
        fail("target_under_test %s does not produce expected action CppCompile" % target_under_test.label)

    asserts.equals(env, 1, len(compile_action.args))
    args = compile_action.args[0]  # args has type Args.
    print(args)

    return analysistest.end(env)

local_includes_test = analysistest.make(_local_includes_test_impl)

At this stage we get variable "args" of type Args, but there is absolutely no way to check its contents! I even can print it and see that it it contains correct representation of the compiler command line, but even that string representation cannot be obtained in the test!
Documentation recommends to try "argv" instead of "argc", but the experiment shows that it is None in this case. 
I am stumped. How we supposed to use unittest to analyse the command lines of the generated actions?

Thank you!
Konstantin

Alex Humesky

unread,
Oct 17, 2022, 9:05:05 PM10/17/22
to Konstantin, bazel-discuss
Unfortunately it looks like the CppCompile action was only relatively recently updated to report argv:

Luckily, the baseline for bazel 6.0 hasn't been selected yet, so I would expect both of those to be in the next release:

The commit from February is in bazel pre-release versions, so you could try the test with a pre-release. The commit from October deals with making argv from CppCompile report the real args when a param file is used (guarded by a flag), and that's not in a pre-release yet. So if you need that, you could build bazel from head, or wait for another pre-release to try this out.

--
You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/5adf9016-6c82-4865-b03b-a5fa3eb037ecn%40googlegroups.com.

Konstantin

unread,
Oct 17, 2022, 9:07:53 PM10/17/22
to bazel-discuss
Uh-huh. Good to know!

Neal Norwitz

unread,
Oct 17, 2022, 11:18:09 PM10/17/22
to Alex Humesky, bazel-discuss
On Mon, Oct 17, 2022 at 6:05 PM 'Alex Humesky' via bazel-discuss <bazel-...@googlegroups.com> wrote:
Luckily, the baseline for bazel 6.0 hasn't been selected yet, so I would expect both of those to be in the next release:

Can you give us any updates/plans about 6.0? When might it be cut?  When is the earliest it could be fully released?  A list of blockers?  An issue we could track to get updates? etc

Thanks,
n

Alex Humesky

unread,
Oct 20, 2022, 8:38:23 PM10/20/22
to Neal Norwitz, bazel-discuss
That info can be found at the bazel 6.0 tracking issue here:
Reply all
Reply to author
Forward
0 new messages