1

Problem Description

I am trying to filter directories that we don't want to ignore for the test coverage. For this purpose we are using Lcov.

When I try to put the directories that are to be ignore in a variable __ignoreinput the command #${__lcov} ${__gcovopts} --remove MYCODE.info "${__ignoreinput}" -o MYCODE_filtered.info > /dev/null 2> /dev/null doesn't work, it doesn't filter anything. Whereas when I use the command without the __ignoreinput as in

 ${__lcov} ${__gcovopts} --remove MYCODE.info '/opt/*' '/usr/include/*' '*3rdParty/*' '*Input_API/*' '*Grammars/*' -o MYCODE_filtered.info > /dev/null 2> /dev/null
if [[ ${?} -ne 0 ]] ;then echo "Error *** lcov filtrering failed" && exit 1 ;fi

The filter works ok. What am I doing wrong. I don't understand.


Script

#!/bin/bash
__orc=/home/anybody/workspace/project
__buildtype="local"
__output=/home/anybody/workspace/lcov


#doe not work
#__ignoreinput="'/opt/*' '/usr/include/*' '*3rdParty/*' '*Input_API/*' '*Grammars/*'"
#__ignoreinput="/opt/* /usr/include/* *3rdParty/* *Input_API/* *Grammars/* "
#__ignoreinput="\"/opt/*\" \"/usr/include/*\" \"*3rdParty/*\" \"*Input_API/*\" \"*Grammars/*\""


__gcovopts=--gcov-tool=/opt/1A/x86_64-2.6.32-v2/bin/gcov
__lcov=lcov
if [[ "${__buildtype}" == "docker" ]] ;then
  __build=MYCODE/build_x86_64-2.6.32-v2_Gcov
else
  __build=MYCODE/cmake-build-coverage
fi

echo "Filter lcov tracefile"
cd ${__orc}/${__build}
#does not work
#${__lcov} ${__gcovopts} --remove MYCODE.info "${__ignoreinput}" -o MYCODE_filtered.info > /dev/null 2> /dev/null

#works
${__lcov} ${__gcovopts} --remove MYCODE.info '/opt/*' '/usr/include/*' '*3rdParty/*' '*Input_API/*' '*Grammars/*' -o MYCODE_filtered.info > /dev/null 2> /dev/null
if [[ ${?} -ne 0 ]] ;then echo "Error *** lcov filtrering failed" && exit 1 ;fi

echo "Generate HTML reports"
cd ${__orc}/${__build}
genhtml --ignore-errors source -o ${__output}/lcov_"$(git rev-list HEAD -n 1)" MYCODE_filtered.info > /dev/null 2> /dev/null
if [[ ${?} -ne 0 ]] ;then echo "Error *** lcov reports failed" && exit 1 ;fi
Community
  • 1
  • 1
Hani Gotc
  • 840
  • 11
  • 24

1 Answers1

1

One simple way to pass the __ignoreinput would be to store the glob expressions in the array under single quote expand them while passing to the lcov command. Write your ignore input as

__ignoreinput=( '/opt/*' '/usr/include/*' '*3rdParty/*' '*Input_API/*' '*Grammars/*' )

and doing below should work as expected.

"${__lcov}" "${__gcovopts}" --remove MYCODE.info "${__ignoreinput[@]}" -o MYCODE_filtered.info 2>&1 > /dev/null

For all the failure cases in your description __ignoreinput is set as one whole-string under ".." but the command expects words split up one expression for each. The array expansion "${__ignoreinput[@]}" though puts each word defined in the array as a separate word as expected by the command.

Also carefully single/double quote the words in the array during the definition, because with lack of quotes * could undergo path-name expansion and could expand to the list of filenames under each of those paths.

Also see how > /dev/null 2> /dev/null could be minimized to > /dev/null 2> /dev/null or simply &> /dev/null in bash.


Also naming variable names prefixed with __ is a bad practice. Like most of the languages out there, the character can itself could be a valid variable identifier. As shown above enclosing the variable name completely around {..} is the recommended way.

Inian
  • 80,270
  • 14
  • 142
  • 161
  • 1
    thank you so much for your help @Inian. Indeed it worked. I thought that they will be separated. I thought wrong lololol – Hani Gotc May 29 '19 at 08:33
  • 1
    Also carefully single/double quote the words in the array during the definition, because with lack of quotes * could undergo path-name expansion and could expand to the list of filenames under each of those paths. You are right I did have that problem the expansion did happen i didn't know why earlier – Hani Gotc May 29 '19 at 08:38
  • #inian is it possible to ask another question? new one – Hani Gotc May 29 '19 at 09:24
  • If its trivial I can answer here or if it’s a new one altogether, ask a separate question! – Inian May 29 '19 at 09:34