Fix tests

This commit is contained in:
Francois Lebreau 2021-01-21 10:07:00 +00:00
parent 44675d38f5
commit 785e9a3ce7
3 changed files with 22 additions and 23 deletions

1
.gitignore vendored
View file

@ -22,3 +22,4 @@ venv/
# VSCode # VSCode
.vscode/ .vscode/
pyrightconfig.json

View file

@ -1,2 +1,8 @@
from .terraform import Terraform, VariableFiles from .terraform import (
IsFlagged,
IsNotFlagged,
Terraform,
TerraformCommandError,
VariableFiles,
)
from .tfstate import Tfstate from .tfstate import Tfstate

View file

@ -5,8 +5,10 @@ import re
import shutil import shutil
from contextlib import contextmanager from contextlib import contextmanager
from io import StringIO from io import StringIO
from typing import Callable
import pytest import pytest
from python_terraform import IsFlagged, IsNotFlagged, Terraform, TerraformCommandError from python_terraform import IsFlagged, IsNotFlagged, Terraform, TerraformCommandError
logging.basicConfig(level=logging.DEBUG) logging.basicConfig(level=logging.DEBUG)
@ -113,7 +115,7 @@ def fmt_test_file(request):
@pytest.fixture() @pytest.fixture()
def string_logger(request): def string_logger(request) -> Callable[..., str]:
log_stream = StringIO() log_stream = StringIO()
handler = logging.StreamHandler(log_stream) handler = logging.StreamHandler(log_stream)
root_logger.addHandler(handler) root_logger.addHandler(handler)
@ -150,13 +152,13 @@ def workspace_setup_teardown():
class TestTerraform(object): class TestTerraform(object):
def teardown_method(self, method): def teardown_method(self, _) -> None:
""" teardown any state that was previously setup with a setup_method """ teardown any state that was previously setup with a setup_method
call. call.
""" """
exclude = ["test_tfstate_file", "test_tfstate_file2", "test_tfstate_file3"] exclude = ["test_tfstate_file", "test_tfstate_file2", "test_tfstate_file3"]
def purge(dir, pattern): def purge(dir: str, pattern: str) -> None:
for root, dirnames, filenames in os.walk(dir): for root, dirnames, filenames in os.walk(dir):
dirnames[:] = [d for d in dirnames if d not in exclude] dirnames[:] = [d for d in dirnames if d not in exclude]
for filename in fnmatch.filter(filenames, pattern): for filename in fnmatch.filter(filenames, pattern):
@ -172,7 +174,7 @@ class TestTerraform(object):
purge(".", FILE_PATH_WITH_SPACE_AND_SPACIAL_CHARS) purge(".", FILE_PATH_WITH_SPACE_AND_SPACIAL_CHARS)
@pytest.mark.parametrize(["method", "expected"], STRING_CASES) @pytest.mark.parametrize(["method", "expected"], STRING_CASES)
def test_generate_cmd_string(self, method, expected): def test_generate_cmd_string(self, method: Callable[..., str], expected: str):
tf = Terraform(working_dir=current_path) tf = Terraform(working_dir=current_path)
result = method(tf) result = method(tf)
@ -183,36 +185,26 @@ class TestTerraform(object):
@pytest.mark.parametrize(*CMD_CASES) @pytest.mark.parametrize(*CMD_CASES)
def test_cmd( def test_cmd(
self, self,
method, method: Callable[..., str],
expected_output, expected_output: str,
expected_ret_code, expected_ret_code: int,
expected_exception, expected_exception: bool,
expected_logs, expected_logs: str,
string_logger, string_logger: Callable[..., str],
folder, folder,
): ):
tf = Terraform(working_dir=current_path) tf = Terraform(working_dir=current_path)
tf.init(folder) tf.init(folder)
try: try:
ret, out, err = method(tf) ret, out, _ = method(tf)
assert not expected_exception assert not expected_exception
except TerraformCommandError as e: except TerraformCommandError as e:
assert expected_exception assert expected_exception
ret = e.returncode ret = e.returncode
out = e.out out = e.out
err = e.err
logs = string_logger() logs = string_logger()
logs = logs.replace("\n", "") logs = logs.replace("\n", "")
if isinstance(expected_output, list):
ok = False
for xo in expected_output:
if xo in out:
ok = True
break
if not ok:
assert expected_output[0] in out
else:
assert expected_output in out assert expected_output in out
assert expected_ret_code == ret assert expected_ret_code == ret
assert expected_logs in logs assert expected_logs in logs