I have a script which needs to run npm run test and capture test coverage coverage value so here I am trying to capture the value 36.95
and output it in gitlab script which I am planning to add as a gitlab job. It seems that the runner is gnu. If I execute the below script on my local it works fine as in returns 36.95.
However if I run it on gitlab it returns the value of the npm run test
. How can I make it compattible with the runner I am using.
RES=$(npm test -- --coverage --watchAll=false)
TOTAL=$(sed -E 's/^.* All files \| ([^ ] ) \| .*$/\1/' <<< $RES)
echo "Current test coverage is : " $TOTAL
I think here-string(<<<) is not compatible with the runner which is why it works on my local machine but not on gitlab maybe there is some issue with my script too. Gitlab pipleline config
test:
stage: test
image: node:16.13.1
before_script:
- npm i
- npx node -v
- npx npm -v
script:
- echo "running test coverage"
- bash test.sh
test.sh
contains the script mentioned at the top
CodePudding user response:
You can try the alternative to <<<
TOTAL=$(echo "${RES}"|grep "All files"|sed -E 's/^.* All files \| ([^ ] ) \| .*$/\1/')
Meaning you isolate the line you want to sed
.
CodePudding user response:
You could just pipe directly to sed
total=$(npm test -- --coverage --watchAll=false | sed -En '/All files/s/^[^|]*\| ([[:digit:].] ).*$/\1/p')
echo "Current test coverage is : $total"