Skip to content

Commit

Permalink
tests: add tests for env var checking
Browse files Browse the repository at this point in the history
  • Loading branch information
ejseqera committed Nov 1, 2023
1 parent 3cd910d commit b03f416
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions tests/unit/test_seqeraplatform.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from seqerakit import seqeraplatform
import json
import subprocess
import os


class TestSeqeraPlatform(unittest.TestCase):
Expand Down Expand Up @@ -179,5 +180,46 @@ def test_dryrun_call(self, mock_subprocess):
mock_subprocess.assert_not_called()


class TestCheckEnvVars(unittest.TestCase):
def setUp(self):
self.sp = seqeraplatform.SeqeraPlatform()
self.original_environ = dict(os.environ)

def tearDown(self):
# Restore the original environment after each test
os.environ.clear()
os.environ.update(self.original_environ)

def test_with_set_env_vars(self):
# Set environment variables for the test
os.environ["VAR1"] = "value1"

command = ["tw", "pipelines", "list", "-w", "$VAR1"]
expected = "tw pipelines list -w $VAR1"
result = self.sp._check_env_vars(command)
self.assertEqual(result, expected)

def test_without_env_vars(self):
# Test case where there are no environment variables in the command
command = ["tw", "info"]
expected = "tw info" # shlex.quote() will not alter these
result = self.sp._check_env_vars(command)
self.assertEqual(result, expected)

def test_error_raised_for_unset_env_vars(self):
# Unset environment variables for this test
if "UNSET_VAR" in os.environ:
del os.environ["UNSET_VAR"]

command = ["tw", "pipelines", "list", "-w", "$UNSET_VAR"]

# Assert that EnvironmentError is raised
with self.assertRaises(EnvironmentError) as context:
self.sp._check_env_vars(command)
self.assertIn(
" Environment variable '$UNSET_VAR' not found!", str(context.exception)
)


if __name__ == "__main__":
unittest.main()

0 comments on commit b03f416

Please sign in to comment.