sstephenson / bats

Bash Automated Testing System
MIT License
7.13k stars 517 forks source link

Check if $output contains a particular string #106

Closed DinisCruz closed 9 years ago

DinisCruz commented 9 years ago

Hi, I'm using bats to write tests for docker build images, and on the test bellow

@test "curl installed" { 
  run curl --version;  
  [ $(expr "$output" : ".*curl 7.37.1") -eq 11 ]    # works
  [ "$output" == *"curl 7.37.1"* ];                 # doesn't work
}

I was able to use the [ $(expr "$output" : ".*curl 7.37.1") -eq 11 ] to check that the curl version was 7.37.1, but that syntax fells too complex

Is there another way?

Maybe something like [ "$output" == *"curl 7.37.1"* ]; ?(which doesn't work)

sstephenson commented 9 years ago

You don’t need run or $output here.

@test "curl installed" {
  curl --version | grep '7\.37\.1’
}

Just run curl --version directly and pipe its output to grep with the string you want to match. grep will return a non-zero exit code if there’s no match, which will result in a failed test.

DinisCruz commented 8 years ago

@sstephenson version works great

for example:

#!/usr/bin/env bats
. ./src/api-git.sh
. ./src/api-veracode.sh

@test "load API ok and call git version" {
  result="$(git_version)"  
  echo $result | grep 'git version'
}
simbo1905 commented 5 years ago

We use double squares and double equality to do string contains:

  # Output should be verbose:
  [[ "$output" == *"cleaning"* ]]
  [[ "$output" == *"$encrypted_filename"* ]]