Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test if env variable is set in bats-assert framework?

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 '^.*:.*$'
}
like image 670
JamesD Avatar asked Oct 26 '25 09:10

JamesD


1 Answers

This expression works fine:

@test 'check_env_vars()' { 
  run : ${SYSTEM_CREDS?"Need to set SYSTEM_CREDS"} 
  assert_success 
}
like image 103
JamesD Avatar answered Oct 28 '25 01:10

JamesD