Skip to content

Commit

Permalink
test: Add IDP Harness
Browse files Browse the repository at this point in the history
Resolves issues with idp test namespace persisting across tests, instead
loading/removing when relevant
  • Loading branch information
techman83 committed Mar 21, 2024
1 parent 7761e6b commit 2e70cf3
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 26 deletions.
34 changes: 34 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import json
import os
import sys
from importlib import import_module, reload
from pathlib import Path
from tempfile import TemporaryDirectory
from unittest import TestCase, mock
Expand Down Expand Up @@ -56,3 +58,35 @@ def synth_stack(self, stack: CallyStack) -> dict:
stack.synth_stack(self.working.name)
output_file = Path(self.working.name, 'stacks', stack.name, 'cdk.tf.json')
return json.loads(output_file.read_text())


class CallyIdpTestHarness(CallyTfTestHarness):
def setUp(self):
sys.path.append(Path(Path(__file__).parent, 'testdata/test_cls').as_posix())
self.reload_cally_modules()
super().setUp()

def tearDown(self):
super().tearDown()
sys.path.remove(Path(Path(__file__).parent, 'testdata/test_cls').as_posix())
for mod in [
'cally.idp',
'cally.idp.defaults',
'cally.idp.stacks',
'cally.idp.commands',
]:
if mod in sys.modules:
del sys.modules[mod]
self.reload_cally_modules()

@staticmethod
def reload_cally_modules():
modules = [
'cally.cli',
'cally.cdk.stacks',
'cally.cli.config.loader',
'cally.cli.config',
]
for module in modules:
mod = import_module(module)
reload(mod)
4 changes: 2 additions & 2 deletions tests/cli/config/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import os
from unittest import mock

from cally.cli.config import CallyConfig
from cally.cli.config import types as cally_types
from dynaconf import ValidationError

from cally.cli.config import CallyConfig
from cally.cli.config import types as cally_types
from ... import CallyTestHarness


Expand Down
1 change: 0 additions & 1 deletion tests/cli/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@


class ConfigCliTests(CallyTestHarness):

@mock.patch.dict(os.environ, {"CALLY_STACK_TYPE": "CallyStack"})
def test_print_service_basic(self):
result = CliRunner().invoke(
Expand Down
13 changes: 2 additions & 11 deletions tests/cli/test_idp.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
import sys
from importlib import reload
from pathlib import Path

sys.path.append(Path(Path(__file__).parent, '../testdata/test_cls').as_posix())

from cally import cli
from cally.cli.config import CallyConfig
from click.testing import CliRunner

from .. import CallyTestHarness
from .. import CallyIdpTestHarness


class CliTests(CallyTestHarness):
def setUp(self):
super().setUp()
reload(cli)
class CliTests(CallyIdpTestHarness):

def test_example(self):
result = CliRunner().invoke(
Expand Down
14 changes: 2 additions & 12 deletions tests/stacks/test_idp.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,9 @@
import sys
from importlib import reload
from pathlib import Path

sys.path.append(Path(Path(__file__).parent, '../testdata/test_cls').as_posix())

from cally.cdk import stacks

from .. import CallyTfTestHarness

from .. import CallyIdpTestHarness

class IdpStackTests(CallyTfTestHarness):
def setUp(self):
super().setUp()
reload(stacks)

class IdpStackTests(CallyIdpTestHarness):
def test_class_load(self):
self.assertEqual(stacks.MinimalStack.__name__, 'MinimalStack')

Expand Down

0 comments on commit 2e70cf3

Please sign in to comment.