diff --git a/general_sanity.py b/general_sanity.py deleted file mode 100755 index 1c28b3e..0000000 --- a/general_sanity.py +++ /dev/null @@ -1,74 +0,0 @@ -#! /usr/bin/env python3 - -from sys import argv -from pathlib import Path - -import yaml -import os - - -def validate_file(passed_file): - user_file = Path(passed_file) - - # See if the argument is a valid path. - try: - user_file.resolve(strict=True) - except FileNotFoundError: - print(f"Error: {passed_file} could not be found from the current" - + " directory.") - else: - # The passed path is valid, now check if path is a file or directory. - if user_file.is_file(): - # If the path is a file, start testing. - test_structure(user_file) - elif user_file.is_dir(): - # If path is a directory, recommend user tries again. - print(f"Warning: {passed_file} is a directory, not a file.") - print("Try again with one of the files within the directory.") - else: - # In case path is valid but inaccessible... - print(f"Error: {passed_file} is inaccessible.") - print("Please try again.") - - -def test_structure(yaml_file): - # Print the "functions" branch(es) to stdout. - with open(yaml_file, 'r') as f: - stack_yaml = yaml.safe_load(f) - - # Test for 'functions' key in YAML file. - assert 'functions' in stack_yaml, "Error: 'functions' not defined in file." - - #parent_dir = os.path.dirname(str(yaml_file)) - project_path = os.path.realpath(os.path.dirname(str(yaml_file))) - - # Test for '.gitignore' file in base directory. - assert os.path.exists(os.path.join(project_path, '.gitignore')) - - for key, value in stack_yaml['functions'].items(): - # Test for template directory named after lang value. - assert os.path.exists(os.path.join(project_path, 'template', - value['lang'])) - - # Test for 'Dockerfile' in language specific template directory. - assert os.path.exists(os.path.join(project_path, 'template', - value['lang'], 'Dockerfile')) - - # Test for 'requirements.txt' in function specific directory. - assert os.path.exists(os.path.join(project_path, key, - 'requirements.txt')) - - # Test for 'handler.py' in function specific directory. - assert os.path.exists(os.path.join(project_path, key, 'handler.py')) - - # Test for '__init__.py' in function specific directory. - assert os.path.exists(os.path.join(project_path, key, '__init__.py')) - - -if __name__ == "__main__": - try: - script, arg01 = argv - except ValueError: - print("Error: Too few arguments provided.") - else: - validate_file(arg01) diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..73c88ab --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,29 @@ +import os +import yaml + +import pytest + +_real_stack_path = os.path.realpath('./stack.yml') +with open(_real_stack_path, 'r') as stack_yaml: + _stack_data = yaml.safe_load(stack_yaml) + + +@pytest.fixture(scope='session') +def stack_dir(): + return os.path.dirname(_real_stack_path) + + +@pytest.fixture(scope='session') +def stack_data(): + return _stack_data + + +def pytest_generate_tests(metafunc): + if 'stack_function' in metafunc.fixturenames: + data = [] + for function, values in _stack_data['functions'].items(): + values.setdefault('function', function) + data.append(values) + + metafunc.parametrize('stack_function', + data) diff --git a/tests/test_stack_yaml.py b/tests/test_stack_yaml.py new file mode 100644 index 0000000..7cface1 --- /dev/null +++ b/tests/test_stack_yaml.py @@ -0,0 +1,74 @@ +import os +import pytest + + +def test_stack_functions(stack_data): + errMsg = 'Error: \'stack.yml\' file does not contain any functions.' + assert 'functions' in stack_data, errMsg + + +def test_git_ignore(stack_dir): + path = os.path.join(stack_dir, '.gitignore') + errMsg = f'Error: No \'.gitignore\' file found in {stack_dir}.' + assert os.path.exists(os.path.realpath(path)), errMsg + + +def test_stack_handlers(stack_function, stack_dir): + path = os.path.join(stack_dir, stack_function['handler']) + errMsg = f'Error: No directory for {stack_function["handler"]}.' + assert os.path.exists(os.path.realpath(path)), errMsg + + +def test_stack_handlers_file(stack_function, stack_dir): + path = os.path.join(stack_dir, stack_function['handler'], 'handler.py') + errMsg = (f'Error: \'{stack_function["handler"]}\' does not contain ' + '\'handler.py\' file.') + assert os.path.exists(os.path.realpath(path)), errMsg + + +def test_stack_handlers_requires(stack_function, stack_dir): + path = os.path.join(stack_dir, stack_function['handler'], + 'requirements.txt') + errMsg = (f'Error: \'{stack_function["handler"]}\' does not contain ' + '\'requirements.txt\' file.') + assert os.path.exists(os.path.realpath(path)), errMsg + + +def test_stack_handlers_init(stack_function, stack_dir): + path = os.path.join(stack_dir, stack_function['handler'], + '__init__.py') + errMsg = (f'Error: \'{stack_function["handler"]}\' does not contain ' + '\'__init__.py\' file.') + assert os.path.exists(os.path.realpath(path)), errMsg + + +def test_stack_langs(stack_function, stack_dir): + path = os.path.join(stack_dir, 'template', stack_function['lang']) + errMsg = (f'Error: No directory for {stack_function["lang"]} is present in ' + '\'template\' directory.') + assert os.path.exists(os.path.realpath(path)), errMsg + + +def test_stack_langs_dockerfile(stack_function, stack_dir): + path = os.path.join(stack_dir, 'template', stack_function['lang'], + 'Dockerfile') + errMsg = (f'Error: \'template/{stack_function["lang"]}/\' does not contain ' + '\'Dockerfile\'.') + assert os.path.exists(os.path.realpath(path)), errMsg + + +def test_stack_langs_requires(stack_function, stack_dir): + path = os.path.join(stack_dir, 'template', stack_function['lang'], + 'requirements.txt') + errMsg = (f'Error: \'template/{stack_function["lang"]}/\' does not contain ' + '\'requirements.txt\' file.') + assert os.path.exists(os.path.realpath(path)), errMsg + + +def test_only_langs(stack_data, stack_dir): + lang_set = {v['lang'] for k, v in stack_data['functions'].items()} + dir_set = set(os.listdir(os.path.join(stack_dir, 'template'))) + warnMsg = ('Warning: Unused language templates are present in \'template\' ' + 'directory.') + + assert len(dir_set - lang_set) == 0, warnMsg