From 32478926c8a0b2659d5cee1dd87aff1383371e76 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Thu, 23 Jun 2022 21:49:44 +0200 Subject: [PATCH] Added tests for build command --- src/cpl_cli/publish/publisher_service.py | 4 +- unittests/unittests_cli/build_test_case.py | 86 +++++++++++++++++++++- unittests/unittests_cli/cli_test_suite.py | 48 ++++++------ unittests/unittests_shared/cli_commands.py | 4 + 4 files changed, 115 insertions(+), 27 deletions(-) diff --git a/src/cpl_cli/publish/publisher_service.py b/src/cpl_cli/publish/publisher_service.py index dc85215c..64988545 100644 --- a/src/cpl_cli/publish/publisher_service.py +++ b/src/cpl_cli/publish/publisher_service.py @@ -426,7 +426,7 @@ class PublisherService(PublisherABC): :return: """ self._env.set_working_directory(os.path.join(self._env.working_directory, '../')) - self.exclude(f'*/{self._config.get_configuration("ProjectName")}.json') + self.exclude(f'*/{self._project_settings.name}.json') self._output_path = os.path.abspath(os.path.join(self._output_path, self._project_settings.name, 'build')) Console.spinner('Reading source files:', self._read_sources, text_foreground_color=ForegroundColorEnum.green, @@ -449,7 +449,7 @@ class PublisherService(PublisherABC): :return: """ self._env.set_working_directory(os.path.join(self._env.working_directory, '../')) - self.exclude(f'*/{self._config.get_configuration("ProjectName")}.json') + self.exclude(f'*/{self._project_settings.name}.json') self._output_path = os.path.abspath(os.path.join(self._output_path, self._project_settings.name, 'publish')) Console.write_line('Build:') diff --git a/unittests/unittests_cli/build_test_case.py b/unittests/unittests_cli/build_test_case.py index 2456c099..e35d5cbd 100644 --- a/unittests/unittests_cli/build_test_case.py +++ b/unittests/unittests_cli/build_test_case.py @@ -1,10 +1,88 @@ +import filecmp +import json +import os +import shutil import unittest +from cpl_core.utils import String + +from unittests_cli.constants import PLAYGROUND_PATH +from unittests_shared.cli_commands import CLICommands + class BuildTestCase(unittest.TestCase): - def setUp(self): - pass + def __init__(self, methodName: str): + unittest.TestCase.__init__(self, methodName) + self._source = 'build-test-source' + self._project_file = f'src/{String.convert_to_snake_case(self._source)}/{self._source}.json' - def test_equal(self): - pass + def _get_project_settings(self): + with open(os.path.join(os.getcwd(), self._project_file), 'r', encoding='utf-8') as cfg: + # load json + project_json = json.load(cfg) + cfg.close() + + return project_json + + def _save_project_settings(self, settings: dict): + with open(os.path.join(os.getcwd(), self._project_file), 'w', encoding='utf-8') as project_file: + project_file.write(json.dumps(settings, indent=2)) + project_file.close() + + def setUp(self): + os.chdir(os.path.abspath(PLAYGROUND_PATH)) + # create projects + CLICommands.new('console', self._source, '--ab', '--s') + os.chdir(os.path.join(os.getcwd(), self._source)) + + def cleanUp(self): + # remove projects + if not os.path.exists(os.path.abspath(os.path.join(PLAYGROUND_PATH, self._source))): + return + + shutil.rmtree(os.path.abspath(os.path.join(PLAYGROUND_PATH, self._source))) + + def _are_dir_trees_equal(self, dir1, dir2): + """ + found at https://stackoverflow.com/questions/4187564/recursively-compare-two-directories-to-ensure-they-have-the-same-files-and-subdi + + Compare two directories recursively. Files in each directory are + assumed to be equal if their names and contents are equal. + + @param dir1: First directory path + @param dir2: Second directory path + + @return: True if the directory trees are the same and + there were no errors while accessing the directories or files, + False otherwise. + """ + + dirs_cmp = filecmp.dircmp(dir1, dir2) + if len(dirs_cmp.left_only) > 0 or len(dirs_cmp.right_only) > 0 or len(dirs_cmp.funny_files) > 0: + return False + + (_, mismatch, errors) = filecmp.cmpfiles(dir1, dir2, dirs_cmp.common_files, shallow=False) + + if len(mismatch) > 0 or len(errors) > 0: + return False + + for common_dir in dirs_cmp.common_dirs: + new_dir1 = os.path.join(dir1, common_dir) + new_dir2 = os.path.join(dir2, common_dir) + if not self._are_dir_trees_equal(new_dir1, new_dir2): + return False + + return True + + def test_build(self): + CLICommands.build() + dist_path = './dist' + full_dist_path = f'{dist_path}/{self._source}/build/{String.convert_to_snake_case(self._source)}' + self.assertTrue(os.path.exists(dist_path)) + self.assertTrue(os.path.exists(full_dist_path)) + self.assertFalse(self._are_dir_trees_equal(f'./src/{String.convert_to_snake_case(self._source)}', full_dist_path)) + with open(f'{full_dist_path}/{self._source}.json', 'w') as file: + file.write(json.dumps(self._get_project_settings(), indent=2)) + file.close() + self.assertTrue(self._are_dir_trees_equal(f'./src/{String.convert_to_snake_case(self._source)}', full_dist_path)) diff --git a/unittests/unittests_cli/cli_test_suite.py b/unittests/unittests_cli/cli_test_suite.py index a0e49f09..fac4682b 100644 --- a/unittests/unittests_cli/cli_test_suite.py +++ b/unittests/unittests_cli/cli_test_suite.py @@ -6,12 +6,14 @@ from typing import Optional from unittest import TestResult from unittests_cli.add_test_case import AddTestCase +from unittests_cli.build_test_case import BuildTestCase from unittests_cli.constants import PLAYGROUND_PATH from unittests_cli.generate_test_case import GenerateTestCase from unittests_cli.install_test_case import InstallTestCase from unittests_cli.new_test_case import NewTestCase from unittests_cli.remove_test_case import RemoveTestCase from unittests_cli.uninstall_test_case import UninstallTestCase +from unittests_cli.update_test_case import UpdateTestCase class CLITestSuite(unittest.TestSuite): @@ -21,36 +23,40 @@ class CLITestSuite(unittest.TestSuite): loader = unittest.TestLoader() self._result: Optional[TestResult] = None - # nothing needed - self.addTests(loader.loadTestsFromTestCase(GenerateTestCase)) - self.addTests(loader.loadTestsFromTestCase(NewTestCase)) - # compare console output - # self.addTests(loader.loadTestsFromTestCase(VersionTestCase)) + active_tests = [ + # nothing needed + GenerateTestCase, + NewTestCase, - # project needed - # compare two file states/directory content - # self.addTests(loader.loadTestsFromTestCase(BuildTestCase)) + # compare console output + # VersionTestCase, - self.addTests(loader.loadTestsFromTestCase(InstallTestCase)) + # project needed + # compare two file states/directory content + BuildTestCase, + InstallTestCase, - # compare two file states/directory content - # self.addTests(loader.loadTestsFromTestCase(PublishTestCase)) + # compare two file states/directory content + # PublishTestCase, - # check if application was executed properly - # self.addTests(loader.loadTestsFromTestCase(RunTestCase)) + # check if application was executed properly + # RunTestCase, - # check if application was executed properly and file watcher is working - # self.addTests(loader.loadTestsFromTestCase(StartTestCase)) + # check if application was executed properly and file watcher is working + # StartTestCase, - self.addTests(loader.loadTestsFromTestCase(UninstallTestCase)) + UninstallTestCase, + # check in project settings if package is updated + # UpdateTestCase, - # check in project settings if package is updated - # self.addTests(loader.loadTestsFromTestCase(UpdateTestCase)) + # workspace needed + AddTestCase, + RemoveTestCase + ] - # workspace needed - self.addTests(loader.loadTestsFromTestCase(AddTestCase)) - self.addTests(loader.loadTestsFromTestCase(RemoveTestCase)) + for test in active_tests: + self.addTests(loader.loadTestsFromTestCase(test)) def _setup(self): try: diff --git a/unittests/unittests_shared/cli_commands.py b/unittests/unittests_shared/cli_commands.py index 41ab4b4c..431e6d13 100644 --- a/unittests/unittests_shared/cli_commands.py +++ b/unittests/unittests_shared/cli_commands.py @@ -21,6 +21,10 @@ class CLICommands: def add(cls, source: str, target: str): cls._run('add', source, target) + @classmethod + def build(cls): + cls._run('build') + @classmethod def generate(cls, schematic: str, name: str, output=False): cls._run('generate', schematic, name, output=output)