3

I want to test if file contain a pattern (skipped jasmine tests that should make my build fail, when run from travis).

I have code like this:

skipped_tests:
    $(if $(shell grep -E 'iit|ddescribe' spec/terminalSpec.js), @false, @true)

the problem is that I don't get output from grep, when it find the lines that match the pattern. I want this so I can see what line caused the issue in travis build log. Is there a way to reverse a command error code (return 0 when non 0 is returned and vice versa)?

I've also tried this:

skipped_tests:
    grep -E 'iit|ddescribe' spec/terminalSpec.js
    @test $$? -eq 0 # should it be -ne

but this echos one line that have iit and doesn't fail, and if it doesn't find the line, then it fails on grep.

3

How about this?

skipped_tests:
    @! grep -E 'iit|ddescribe' spec/terminalSpec.js
  • I've tried !grep but it turns out you need to put space before command. thanks. – jcubic May 29 '18 at 11:47
1

I've resolved the issue by calling grep two times

skipped_tests:
    @grep -E 'iit|ddescribe' spec/terminalSpec.js || true
    $(if $(shell grep -E 'iit|ddescribe' spec/terminalSpec.js), @false, @true)

is there a better way?

I've tried few other things but it seems that make is operating on strings (use str.replace on Makefile or something) because if output of grep is line of source code it give syntax error from other possible solutions.

1

Possible solution:

Define variable at top of Makefile and use ifeq. For example:

RESULT = $(shell grep ... 2>/dev/null && echo 0 || echo 1)
. . .
skipped_tests:
ifeq ($(RESULT),0)
    . . .
endif

But grep will be executed in every call of make, even if skipped_tests isn't calling. If that target is called every time, then it's also solution.


Considering you wishes. If you want to use grep result in target several times you can use multiline expression. Suppose I have next Makefile:

all: target

target:
    @res=`grep -E 'test|pttern' test.js` || { res="" ; : ; } ; \
        [ ! -z "$$res" ] && echo "Grep result: $$res" ; \
        ls ; \
        [ ! -z "$$res" ] && echo "Grep result: $$res" ; \
    :

.PHONY: target

and I have next test.js in the same directory:

$ cat test.js
pattern

If pattern isn't match there is will be next result:

$ make
Makefile  test.js

If the pattern is exists there is will be next result:

$ make
Grep result: pattern
Makefile  test.js
Grep result: pattern

For readability you can change `` with res=$$(grep -E ...) || ....

Of course it's not very convenient to make multiline commands in one call to system (this multiline is one session of interpreter) but sometimes might be useful in cases where you need to operate one variable (environment variable too).

  • it would require to run grep each time I call make not only for skipped_tests. and If I would like to have output of grep in terminal I will need to call grep once again. – jcubic May 28 '18 at 17:46
  • @jcubic: I update my answer. Please review. – Yurij Goncharuk May 28 '18 at 21:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.