Skip to content

Commit

Permalink
fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gr0vity committed May 9, 2023
1 parent 3f28dbb commit 221c69c
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 44 deletions.
5 changes: 4 additions & 1 deletion nanolab/src/snippet_manager.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from nanomock.modules.nl_parse_config import ConfigReadWrite
from pathlib import Path
import logging

logger = logging.getLogger(__name__)


class SnippetManager:
Expand All @@ -19,7 +22,7 @@ def _load_snippets(self):
for snippet_key, snippet_value in snippets.items():
self.snippets[snippet_key] = snippet_value
except Exception as exc:
print(f"Failed to load snippets from {snippet_file}")
logger.info(f"Failed to load snippets from {snippet_file}")

def get_snippets(self):
return self.snippets
Expand Down
30 changes: 0 additions & 30 deletions unit_tests/test_nanomock.py

This file was deleted.

36 changes: 24 additions & 12 deletions unit_tests/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,27 @@
import os
from unittest.mock import patch
import nanolab.main as run
from unit_tests.utils import match_expected_error, match_expected_output
import shutil
from unit_tests.utils import match_expected_error, match_expected_output, contains_expected_substring, remove_nanolab_resources

from pathlib import Path
import pytest

os.environ["NL_PATH"] = "unit_tests"

class TestRun(unittest.TestCase):

def setUp(self):
os.environ["NL_PATH"] = "unit_tests"
@pytest.fixture(scope="module", autouse=True)
def setup_and_teardown():
# Setup code
remove_nanolab_resources()
print("Setup for all tests in the module")
yield

def tearDown(self):
snippets_dir = Path.cwd() / 'unit_tests' / 'snippets'
resources_dir = Path.cwd() / 'unit_tests' / 'resources'
# Teardown code
remove_nanolab_resources()
print("Teardown for all tests in the module")

if snippets_dir.exists():
shutil.rmtree(str(snippets_dir))

if resources_dir.exists():
shutil.rmtree(str(resources_dir))
class TestRun(unittest.TestCase):

@patch('sys.argv', [
'nanolab', 'run', '--testcase',
Expand Down Expand Up @@ -117,6 +119,16 @@ def test_docker_tags(self):
expected_output = "tag1\nnanocurrency/nano:V24.0"
match_expected_output(expected_output)

@patch('sys.argv', [
'nanolab', 'run', '--testcase',
'unit_tests/test_configs/nanomock_create_down.json'
])
def test_nanolocal_create(self):
os.environ["NL_CONF_DIR"] = "unit_tests/nanomock"
os.environ["NL_CONF_FILE"] = "nl_config.toml"
expected_output = "SUCCESS: 2 containers have been removed"
contains_expected_substring(expected_output)


if __name__ == '__main__':
unittest.main()
10 changes: 9 additions & 1 deletion unit_tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from unittest.mock import patch
import unittest
import nanolab.main as run
import shutil

tc = unittest.TestCase()

Expand Down Expand Up @@ -35,4 +36,11 @@ def contains_expected_substring(expected_substring):
# Get the captured output as a string
output = captured_output.getvalue().replace("\r", "").replace("\n", "")

tc.assertIn(expected_substring, output)
tc.assertIn(expected_substring, output)


def remove_nanolab_resources():
if os.path.exists("./unit_tests/snippets"):
shutil.rmtree("./unit_tests/snippets")
if os.path.exists("./unit_tests/resources"):
shutil.rmtree("./unit_tests/resources")

0 comments on commit 221c69c

Please sign in to comment.