Kacper
Kacper

Reputation: 1199

How to awk with condition / ommit some lines

The goal: I want to know how many test do I have during running my test suite.

I'm trying to get overall number of test scenarios written in BDD style and print that number in console. All test files are in ./features directory. There are subdirectories (so I need to search recursively) Some of tests are commented (using #)

Simple scenario looks like that:

  Scenario: blah-blah-blah
    Given blah
    When blah bloh
    Then bluh bluh

That's pretty simple and works as a charm: grep -r "^\s*Scenario:" ./features/ | wc -l

The tricky part is there could be tests which have "Scenario Outline" keyword. On the bottom of test there's a table with examples. Scenario may look like that:

  Scenario Outline: blah
    Given blah <score>
    When bloh <performance>
    Then blah-bloh-bluh <completion>

    Examples:
      | score | performance | completion |
      | 4/4   | 100         | 50         |
      | 3/4   | 75          | 50         |
      | 4/4   | 100         | 5          |

On the first line we've got headers - we shouldn't count them. So, in this case we have 3 tests. So far I've done something like that:

find ./features -type f -exec sed -n -e '/Scenario Outline/,/Scenario/ p' {} + | grep \| | wc -l

Unfortunately there are two issues I couldn't figure out:

  1. We're checking tables which exists between "Scenario Outline" and "Scenario". My solution does not match case when "Scenario Outline" is the very last test in file (there is no more scenario, just EOF).
  2. I count every line with '|' no matter there are real data or header of the table.

Thanks for every advice!

Upvotes: 0

Views: 61

Answers (1)

mdom
mdom

Reputation: 26

I'm not sure if pipes can occur anywhere else in the files, but if not the following command will count all lines starting with Scenario and all tables rows while skipping the first row.

find ./features -type f -print0 | \
   xargs -0 awk '/^\s*Scenario:/; /\|/ { if (intable) print; intable=1 } intable && $0 !~ /\|/ { intable=0 }' | \
   wc -l

Upvotes: 1

Related Questions