4

I am using BATS testing framework with bats-assert and bats-support.

I want to ensure that the user remembered to set the ELASTIC_CREDS env variable properly. How do I do that? Here is what I've tried:

config.bash
export SYSTEM_CREDS=myuser:mypass

Then, my checkcreds.bats test fike looks like this:

checkcreds.bats
#! ./libs/bats/bin/bats
load 'libs/bats-support/load'
load 'libs/bats-assert/load'
load 'config'

@test 'assert_system_creds()' {
  run env | grep SYSTEM_CREDS | awk -F= '{print$2}'
  assert_output --regexp '^.*:.*$'
}
JamesD
  • 679
  • 10
  • 36
  • See [What's a concise way to check that environment variables are set in a Unix shell script?](https://stackoverflow.com/questions/307503/whats-a-concise-way-to-check-that-environment-variables-are-set-in-a-unix-shell) I believe this could/should be a duplicate of that. – Jonathan Leffler Apr 09 '19 at 23:47
  • Are you asking how to do this in Bash, or in Bats? – tripleee Apr 10 '19 at 04:22
  • `grep 'x' | awk '{ y }'` can usefully be refactored to `awk '/x/ { y }'`; see also [useless use of `grep`](http://www.iki.fi/era/unix/award.html#grep) – tripleee Apr 10 '19 at 04:22
  • 1
    I'd like to know how to do that concisely using Bats, but the link that J.Leffler posted is very helpful and may just be what I need. – JamesD Apr 10 '19 at 14:28

1 Answers1

4

This expression works fine:

@test 'check_env_vars()' { 
  run : ${SYSTEM_CREDS?"Need to set SYSTEM_CREDS"} 
  assert_success 
}
JamesD
  • 679
  • 10
  • 36