开发者

code coverage tools for validating the scripts

开发者 https://www.devze.com 2023-03-30 21:09 出处:网络
Is there any way to validate the coverage of the shell scripts? I have project having lots of shell scripting, and need to ensure static analysis can be performed on the coverage for the shell scripts

Is there any way to validate the coverage of the shell scripts? I have project having lots of shell scripting, and need to ensure static analysis can be performed on the coverage for the shell scripts. Is ther开发者_开发百科e any tool available?


I seriously doubt that there could be any static code analysis performed on shell scripts - especially due to the fact that shell scripts are supposed to call external programs and based on what these external programs return - and there are myriads of external programs and external environment states. It's similar to the problem of static analysis of code heavily relying on eval-like mechanism, but shell scripting is all about eval-style programming.

However, there are some general pointers that could prove useful for "proper" validation, code coverage and documenting of shell scripts as major languages do:

  • You can always run a script with -x (AKA xtrace) option - it will output trace looking like that to stderr:

    + log_end_msg 0
    + [ -z 0 ]
    + retval=0
    + log_end_msg_pre 0
    + :
    + log_use_fancy_output
    + TPUT=/usr/bin/tput
    + EXPR=/usr/bin/expr
    + [ -t 1 ]
    + [ xxterm != x ]
    + [ xxterm != xdumb ]
    
  • Bash makes it possible to redirect this stream to external FD (using BASH_XTRACEFD variable) - that's much easier to parse in practice.

  • It's not trivial, but it's possible to write a program that will find relevant pieces of code being executed using xtrace output and make you a fancy "code coverage" report - like what was called how many times, which pieces of code weren't run at all and thus lack test coverage.

  • In fact, there's a wonderful tool named shcov already written that uses this process - albeit it's somewhat simplistic and doesn't handle all possible cases very well (especially when we're talking about long and complex lines)

  • Last, but not least - there's minimalistic shelldoc project (akin to javadoc) that helps generating documentation based on comments in shell scripts. Yep, that's a shameless plug :)


I don't think there are COTS tools available for test coverage, regardless of the scripting language and there are lots.

Another poster suggested an ad hoc approach that might work with some tools: get them to dump some trace data, and try to match that up with the actual code to get your coverage. He says it sort of works... that's the problem with most hueristics.

Another approach one might take to construct a test coverage for your favorite scripting language is covered by my technical paper on a general approach for building test coverage tools using program transformations. My company builds a line of such tools for more popular languages this way.


You might try looking at shcov, a GPL v2-licensed Python-based tool. It seems to perhaps have been abandoned by the author, but does produce HTML-based graphical reports and seemed (in my limited testing) to be reasonably accurate in terms of coverage analysis.


I have written the tool, that can do the coverage for the shell scripts, the name is shAge means shell script coverage, the project is hosted here

To do the Coverage if any shell scripts do the following

  1. First Download the shAge.jar
  2. Make sure you have you have jdk1.6 update 65 onwards
  3. run the programe like

    java -jar shAge.jar hello.sh

  4. the content in the shell script will get executed and finally a report will be get generated

  5. Report will be genared with the name of file in .html format hello.sh.html
  6. Here is the sample output

code coverage tools for validating the scripts

0

精彩评论

暂无评论...
验证码 换一张
取 消