1

I've defined If-Else in the jenkins script section. But it doesn't get executed at all. Any idea what's wrong.

script {
    unit_test_result = sh (
        script: ''' #!/bin/bash 
                    mvn clean test | grep \'Tests run:\' | grep -v \'Time elapsed\' 
                    if [[ $? == 1 ]]; 
                        then echo "No Unit tests to run!";
                    fi
                ''',
        returnStdout: true
    ).trim()
}

But the if section doesn't run at all..

[Pipeline] script
[Pipeline] 
[Pipeline] 
+ grep Tests run:
+ mvn clean test
+ grep -v Time elapsed
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // node
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
Failed in branch Unit Tests
Alireza
  • 2,103
  • 1
  • 6
  • 19
user630702
  • 2,529
  • 5
  • 35
  • 98
  • The value of "?" Is the result of the last statement in the prior line "grep -v \'Time elapsed\'", which you must understand the [Exit Code](https://www.gnu.org/savannah-checkouts/gnu/grep/manual/grep.html#Exit-Status) for an inverse grep. Also consider all in one: "grep -e -e -v " – Ian W Sep 26 '21 at 09:09
  • yes. The `mvn | grep` statement should run and then the if condition should run to check if it was successful or not – user630702 Sep 26 '21 at 09:10
  • @IanW `grep -e … -e -v …` won't work at all. First of all, the `-v` is interpreted as a regex to search, because you prefixed it with `-e`. But even if you used `grep -e … -v -e …` it wouldn't work, as `-v` is global / affects all patterns. – Socowi Sep 26 '21 at 12:37
  • @socowi, what can say, it was 2am. The main point was "grep -v" exit code is not inuitive I also hate seeing grep trains, but right, inverse is a separate global flag. But the whole construct is poor since mvn can fail for many reasons or the 1st grep can. Not sure why OP is doing this, but I'd suggest a 3-step mvn; "mvn clean compile"; "mvn process-test-classes", then "mvn test", followed by "$?" test expression. Then you really know where the error was, but it's the "process-test-classes error would indicate "No unit test to run", I think. Use the life cycle phases; it's cumulative. – Ian W Sep 26 '21 at 21:30

1 Answers1

2

grep -v 'Time elapsed' exits with status code 1 only if every input line did not contain Time elapsed, but this will never happen as mvn seems to always print a summary.

According to this website, the output of mvn clean test looks as follows if there are tests

-------------------------------------------------------
T E S T S
-------------------------------------------------------
[...]
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.547 sec
[...]
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.018 sec

Results :

Tests run: 2, Failures: 0, Errors: 0, Skipped: 0

and according to this question, as follows if there are no tests

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
There are no tests to run.

Results :

Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

In both cases, grep 'Tests run:' will print at least the summary in the last line. The summary never contains Time elapsed. Therefore grep -v 'Time elapsed' always exits with status code 0.

Try the following script instead:

#!/bin/bash
if ! mvn clean test | grep -q '^Tests run: [^0]'; then
    echo "No Unit tests to run!";
fi

For the outputs shown in this question, we could use if mvn clean test | grep -Fxq 'There are no tests to run.' instead. But I found other sample outputs where mvn did not print that string; sometimes even no summary. The first script handles all of these cases too.

Socowi
  • 25,550
  • 3
  • 32
  • 54
  • I want to also filter the `Time elapsed` and print only the last line if there is result. Can you help with that?? I really want to use grep. The `There are no tests to run.` is something that if there are test java files with no code. But in some cases there are no java files so technically it returns empty line and hence using grep. I would be also fine to print `no tests to run` using echo if the line count is 0 after grep filter – user630702 Sep 27 '21 at 06:31
  • I have difficulties understanding your comment. But I feel like you are asking a slightly different question again. This is already the third time and very frustrating, so I will *not* write another answer for you. When you ask one question, accept the answer or comment on why they do not work for you. Then, if you have a follow-up question, open a new question (although it would be better to pack your final goal into a single question to begin with). – Socowi Sep 28 '21 at 21:43