3

In a Makefile target I have a list of files that I want to split into x parts and work only with one part y, then pass those file names as argument to a test runner. I have no control over the external arguments as they are provided by a CI system. So I need to manually make it 0-based. Example call: test_group_count=10 test_group=1 make foo

Here is my non-working attempt:

foo:
     group_number=$(shell echo $$(( $(test_group) - 1 )))
     tests="$(shell ls *.feature | awk 'NR%$(test_group_count)==${group_number}')"
     run_tests $${tests}
  • the -1 works
  • listing the files works but not reducing it to every y-th line
  • storing the file names in a variable and then use it for the next command does not work

So I have not figured out how to make the commands see both variables: The ones I define in the target and the ones given from the calling command.

Update:

I can make it work as a one-liner, but I would strongly prefer something more readable as my real run_tests is a long ugly command by itself:

run_tests $$(ls *.feature | awk 'NR%$(test_group_count)==( $(test_group) - 1 )')
2
  • @pizdelect Great, thank you a lot! Do you want to post it as an answer? I would then add the working code to it.
    – user640916
    Commented Jul 16, 2019 at 14:04
  • done it. I don't know what the rules are -- I hope you can edit community wiki posts without approval.
    – user313992
    Commented Jul 16, 2019 at 14:17

1 Answer 1

1

Your ${group_number} will be replaced by the make macro with that name. But the line above it sets it as a shell variable (in a different shell than the one you're using it in; simply doubling the $ will not work).

You should define it as a macro -- ie not in a rule, in a non tab indented line. Same thing with tests; each line is run in a different shell, you cannot share variables between them.

Working solution:

foo: group_number=$(shell echo $$(( $(test_group) - 1 )))
foo: tests=$(shell ls *.feature | awk 'NR%$(test_group_count)==${group_number}')
foo:
    run_tests ${tests}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .