def verify_tavern(project: Project, logger: Logger, reactor: Reactor): # Expand the directory to get full path test_dir = project.expand_path(f"${TAVERN_TEST_DIR}") # Run the tests in the directory if _run_tavern_tests_in_dir(test_dir, logger, project, reactor): package_artifacts(project, test_dir, "tavern", project.get_property(ROLE))
def _copy_dist_package(project: Project, logger: Logger, reactor: Reactor, dist_dir: str) -> None: dist_file_path = project.expand_path( _make_folder(project, "$dir_dist", 'dist'), project.get_property("docker_package_dist_file", f"{project.name}-{project.version}.tar.gz")) shutil.copy2(dist_file_path, dist_dir)
def verify_cypress(project: Project, logger: Logger, reactor: Reactor): # Get directories with test and cypress executable work_dir = project.expand_path(f"${CYPRESS_TEST_DIR}") if _run_cypress_tests_in_directory(work_dir=work_dir, logger=logger, project=project, reactor=reactor): package_artifacts(project, work_dir, "cypress", project.get_property(ROLE))
def _generate_artifact_manifest(project: Project, logger: Logger, reactor: Reactor, registry_path: str) -> None: artifact_path = project.expand_path(_make_folder(project, '$dir_target'), 'artifact.json') with open(artifact_path, 'w') as target: artifact_manifest = { 'artifact-type': 'container', 'artifact-path': registry_path, 'artifact-identifier': project.version, } json.dump(artifact_manifest, target, indent=4)
class PackageTest(TestCase): def setUp(self): self.tempdir = tempfile.mkdtemp(prefix="palp-") self.testdir = os.path.join(self.tempdir, "package_emr_test") self.project = Project(basedir=self.testdir, name="palp", version="123") self.source_dir = os.path.join( os.path.dirname(os.path.abspath(__file__)), "package_emr_test/") shutil.copytree(self.source_dir, self.testdir) self.project.set_property("dir_target", "target") self.project.set_property("dir_source_main_python", "src/main/python") self.project.set_property("dir_source_main_scripts", "src/main/scripts") self.project.set_property("run_unit_tests_propagate_stdout", True) self.project.set_property("run_unit_tests_propagate_stderr", True) self.dir_target = os.path.join( self.testdir, "target", emr_tasks._EMR_PACKAGE_DIR + "-" + self.project.version) self.zipfile = os.path.join(self.dir_target, "palp.zip") def tearDown(self): shutil.rmtree(self.tempdir) @mock.patch("pybuilder_emr_plugin.emr_tasks.prepare_dependencies_dir") def test_emr_package_assembles_zipfile_correctly( self, prepare_dependencies_dir_mock): emr_package(self.project, mock.MagicMock(Logger)) zf = zipfile.ZipFile(self.zipfile) expected = sorted([ "test_dependency_module.py", "test_dependency_package/__init__.py", "test_package_directory/__init__.py", "test_package_directory/package_file.py", "test_module_file.py", "resources.txt", "resources_subfolder/sub_resources.txt", "VERSION" ]) self.assertEqual(sorted(zf.namelist()), expected, "zipfile") scripts_dir = self.project.expand_path("$dir_source_main_scripts") for file in ["bash-script.sh", "python-script.py"]: self.assertTrue( filecmp.cmp(os.path.join(scripts_dir, file), os.path.join(self.dir_target, file)), "missing " + file)
class ProjectTest (unittest.TestCase): def setUp(self): self.project = Project(basedir="/imaginary", name="Unittest") def test_should_pick_directory_name_for_project_name_when_name_is_not_given(self): try: when(os.path).basename("/imaginary").thenReturn("imaginary") project = Project(basedir="/imaginary") self.assertEquals("imaginary", project.name) verify(os.path).basename("/imaginary") finally: unstub() def test_get_property_should_return_default_value_when_property_is_not_set(self): self.assertEquals("spam", self.project.get_property("spam", "spam")) def test_get_property_should_return_property_value_when_property_is_set(self): self.project.set_property("spam", "eggs") self.assertEquals("eggs", self.project.get_property("spam", "spam")) def test_has_property_should_return_false_when_property_is_not_set(self): self.assertFalse(self.project.has_property("spam")) def test_has_property_should_return_true_when_property_is_set(self): self.project.set_property("spam", "eggs") self.assertTrue(self.project.has_property("spam")) def test_set_property_if_unset_should_set_property_when_property_is_not_set(self): self.project.set_property_if_unset("spam", "spam") self.assertEquals("spam", self.project.get_property("spam")) def test_set_property_if_unset_should_not_set_property_when_property_is_already_set(self): self.project.set_property("spam", "eggs") self.project.set_property_if_unset("spam", "spam") self.assertEquals("eggs", self.project.get_property("spam")) def test_expand_should_raise_exception_when_property_is_not_set(self): self.assertRaises( MissingPropertyException, self.project.expand, "$spam") def test_expand_should_return_expanded_string_when_property_is_set(self): self.project.set_property("spam", "eggs") self.assertEquals("eggs", self.project.expand("$spam")) def test_expand_should_return_expanded_string_when_two_properties_are_found_and_set(self): self.project.set_property("spam", "spam") self.project.set_property("eggs", "eggs") self.assertEquals( "spam and eggs", self.project.expand("$spam and $eggs")) def test_expand_should_expand_property_with_value_being_an_property_expression(self): self.project.set_property("spam", "spam") self.project.set_property("eggs", "$spam") self.assertEquals("spam", self.project.expand("$eggs")) def test_expand_should_raise_exception_when_first_expansion_leads_to_property_reference_and_property_is_undefined(self): self.project.set_property("eggs", "$spam") self.assertRaises( MissingPropertyException, self.project.expand, "$eggs") def test_expand_path_should_return_expanded_path(self): self.project.set_property("spam", "spam") self.project.set_property("eggs", "eggs") self.assertEquals(os.path.join("/imaginary", "spam", "eggs"), self.project.expand_path("$spam/$eggs")) def test_expand_path_should_return_expanded_path_and_additional_parts_when_additional_parts_are_given(self): self.project.set_property("spam", "spam") self.project.set_property("eggs", "eggs") self.assertEquals( os.path.join("/imaginary", "spam", "eggs", "foo", "bar"), self.project.expand_path("$spam/$eggs", "foo", "bar")) def test_should_raise_exception_when_getting_mandatory_propert_and_property_is_not_found(self): self.assertRaises(MissingPropertyException, self.project.get_mandatory_property, "i_dont_exist") def test_should_return_property_value_when_getting_mandatory_propert_and_property_exists(self): self.project.set_property("spam", "spam") self.assertEquals("spam", self.project.get_mandatory_property("spam")) def test_should_add_runtime_dependency_with_name_only(self): self.project.depends_on("spam") self.assertEquals(1, len(self.project.dependencies)) self.assertEquals("spam", self.project.dependencies[0].name) self.assertEquals(None, self.project.dependencies[0].version) def test_should_add_dependency_with_name_and_version(self): self.project.depends_on("spam", "0.7") self.assertEquals(1, len(self.project.dependencies)) self.assertEquals("spam", self.project.dependencies[0].name) self.assertEquals("0.7", self.project.dependencies[0].version) def test_should_add_dependency_with_name_and_version_only_once(self): self.project.depends_on("spam", "0.7") self.project.depends_on("spam", "0.7") self.assertEquals(1, len(self.project.dependencies)) self.assertEquals("spam", self.project.dependencies[0].name) self.assertEquals("0.7", self.project.dependencies[0].version)
class ProjectTest(unittest.TestCase): def setUp(self): self.project = Project(basedir="/imaginary", name="Unittest") @patch("pybuilder.core.basename", return_value="imaginary") def test_should_pick_directory_name_for_project_name_when_name_is_not_given( self, os_path_basename): project = Project(basedir="/imaginary") self.assertEqual("imaginary", project.name) os_path_basename.assert_called_with("/imaginary") def test_get_property_should_return_default_value_when_property_is_not_set( self): self.assertEqual("spam", self.project.get_property("spam", "spam")) def test_get_property_should_return_property_value_when_property_is_set( self): self.project.set_property("spam", "eggs") self.assertEqual("eggs", self.project.get_property("spam", "spam")) def test_has_property_should_return_false_when_property_is_not_set(self): self.assertFalse(self.project.has_property("spam")) def test_has_property_should_return_true_when_property_is_set(self): self.project.set_property("spam", "eggs") self.assertTrue(self.project.has_property("spam")) def test_set_property_if_unset_should_set_property_when_property_is_not_set( self): self.project.set_property_if_unset("spam", "spam") self.assertEqual("spam", self.project.get_property("spam")) def test_set_property_if_unset_should_not_set_property_when_property_is_already_set( self): self.project.set_property("spam", "eggs") self.project.set_property_if_unset("spam", "spam") self.assertEqual("eggs", self.project.get_property("spam")) def test_expand_should_raise_exception_when_property_is_not_set(self): self.assertRaises(MissingPropertyException, self.project.expand, "$spam") def test_expand_should_return_expanded_string_when_property_is_set(self): self.project.set_property("spam", "eggs") self.assertEqual("eggs", self.project.expand("$spam")) def test_expand_should_return_expanded_string_when_two_properties_are_found_and_set( self): self.project.set_property("spam", "spam") self.project.set_property("eggs", "eggs") self.assertEqual("spam and eggs", self.project.expand("$spam and $eggs")) def test_expand_should_expand_property_with_value_being_an_property_expression( self): self.project.set_property("spam", "spam") self.project.set_property("eggs", "$spam") self.assertEqual("spam", self.project.expand("$eggs")) def test_expand_should_raise_exception_when_first_expansion_leads_to_property_reference_and_property_is_undefined( self): self.project.set_property("eggs", "$spam") self.assertRaises(MissingPropertyException, self.project.expand, "$eggs") def test_expand_path_should_return_expanded_path(self): self.project.set_property("spam", "spam") self.project.set_property("eggs", "eggs") self.assertEqual(np(jp(self.project.basedir, "spam", "eggs")), self.project.expand_path("$spam/$eggs")) def test_expand_path_should_return_expanded_path_and_additional_parts_when_additional_parts_are_given( self): self.project.set_property("spam", "spam") self.project.set_property("eggs", "eggs") self.assertEqual( np(jp(self.project.basedir, "spam", "eggs", "foo", "bar")), self.project.expand_path("$spam/$eggs", "foo", "bar")) def test_should_raise_exception_when_getting_mandatory_propert_and_property_is_not_found( self): self.assertRaises(MissingPropertyException, self.project.get_mandatory_property, "i_dont_exist") def test_should_return_property_value_when_getting_mandatory_propert_and_property_exists( self): self.project.set_property("spam", "spam") self.assertEqual("spam", self.project.get_mandatory_property("spam")) def test_should_add_runtime_dependency_with_name_only(self): self.project.depends_on("spam") self.assertEqual(1, len(self.project.dependencies)) self.assertEqual("spam", self.project.dependencies[0].name) self.assertEqual(None, self.project.dependencies[0].version) def test_should_add_dependency_with_name_and_version(self): self.project.depends_on("spam", "0.7") self.assertEqual(1, len(self.project.dependencies)) self.assertEqual("spam", self.project.dependencies[0].name) self.assertEqual(">=0.7", self.project.dependencies[0].version) def test_should_add_dependency_with_name_and_version_only_once(self): self.project.depends_on("spam", "0.7") self.project.depends_on("spam", "0.7") self.assertEqual(1, len(self.project.dependencies)) self.assertEqual("spam", self.project.dependencies[0].name) self.assertEqual(">=0.7", self.project.dependencies[0].version)
class PdocPluginTests(unittest.TestCase): def setUp(self): self.logger = Mock() self.project = Project(".") self.project.set_property("dir_target", "dir_target_value") self.project.set_property("dir_source_main_python", "dir_source_main_python_value") self.project.set_property("dir_reports", "dir_reports_value") @patch("pybuilder.plugins.python.pdoc_plugin.os.mkdir") @patch("pybuilder.plugins.python.pdoc_plugin.os.path.exists") @patch("pybuilder.plugins.python.pdoc_plugin.assert_can_execute") @patch("pybuilder.plugins.python.pdoc_plugin.execute_command") def test_pdoc_prepare_works(self, exec_command, assert_can_exec, os_path_exists, os_mkdir): pdoc_init(self.project) os_path_exists.return_value = False pdoc_prepare(self.project, self.logger) self.assertEqual(os_mkdir.call_count, 1) os_path_exists.return_value = True pdoc_prepare(self.project, self.logger) self.assertEqual(os_mkdir.call_count, 1) self.assertEqual(assert_can_exec.call_count, 2) @patch("pybuilder.plugins.python.pdoc_plugin.os.mkdir") @patch("pybuilder.plugins.python.pdoc_plugin.os.path.exists") @patch("pybuilder.plugins.python.pdoc_plugin.assert_can_execute") @patch("pybuilder.plugins.python.pdoc_plugin.execute_command") def test_pdoc_requires_module_name(self, exec_command, assert_can_exec, os_path_exists, os_mkdir): pdoc_init(self.project) self.assertRaises(BuildFailedException, pdoc_compile_docs, self.project, self.logger) @patch("pybuilder.plugins.python.pdoc_plugin.os.mkdir") @patch("pybuilder.plugins.python.pdoc_plugin.os.path.exists") @patch("pybuilder.plugins.python.pdoc_plugin.assert_can_execute") @patch("pybuilder.plugins.python.pdoc_plugin.execute_command") def test_pdoc_html_adds_html_dir(self, exec_command, assert_can_exec, os_path_exists, os_mkdir): pdoc_init(self.project) self.project.set_property("pdoc_module_name", "pdoc_module_name_value") self.project.set_property("pdoc_command_args", []) pdoc_compile_docs(self.project, self.logger) exec_command.assert_called_with( ['pdoc', "pdoc_module_name_value"], cwd=self.project.expand_path("$dir_target", "pdocs"), env={ 'PYTHONPATH': self.project.expand_path("$dir_source_main_python"), 'PATH': os.environ['PATH'] }, outfile_name=self.project.expand_path('$dir_reports', 'pdoc')) self.project.set_property("pdoc_command_args", ["--html"]) pdoc_compile_docs(self.project, self.logger) exec_command.assert_called_with( [ 'pdoc', "--html", "--html-dir", self.project.expand_path('$dir_target', 'pdocs'), "pdoc_module_name_value" ], cwd=self.project.expand_path("$dir_target", "pdocs"), env={ 'PYTHONPATH': self.project.expand_path("$dir_source_main_python"), 'PATH': os.environ['PATH'] }, outfile_name=self.project.expand_path('$dir_reports', 'pdoc'))
class PdocPluginTests(unittest.TestCase): def setUp(self): self.logger = Mock() self.project = Project(".") self.project.set_property("dir_target", "dir_target_value") self.project.set_property("dir_source_main_python", "dir_source_main_python_value") self.project.set_property("dir_reports", "dir_reports_value") self.reactor = Mock() pyb_env = Mock() pyb_env.environ = {"PATH": "a"} pyb_env.execute_command.return_value = 0 self.reactor.python_env_registry = {"pybuilder": pyb_env} self.reactor.pybuilder_venv = pyb_env @patch("pybuilder.plugins.python.pdoc_plugin.os.mkdir") @patch("pybuilder.plugins.python.pdoc_plugin.os.path.exists") def test_pdoc_prepare_works(self, os_path_exists, os_mkdir): pdoc_init(self.project) os_path_exists.return_value = False pdoc_prepare(self.project, self.logger, self.reactor) self.assertEqual(os_mkdir.call_count, 1) os_path_exists.return_value = True pdoc_prepare(self.project, self.logger, self.reactor) self.assertEqual(os_mkdir.call_count, 1) self.assertEqual( self.reactor.pybuilder_venv.verify_can_execute.call_count, 2) @patch("pybuilder.plugins.python.pdoc_plugin.tail_log") @patch("pybuilder.plugins.python.pdoc_plugin.os.mkdir") @patch("pybuilder.plugins.python.pdoc_plugin.os.path.exists") def test_pdoc_requires_module_name(self, *_): pdoc_init(self.project) self.assertRaises(BuildFailedException, pdoc_compile_docs, self.project, self.logger, self.reactor) @patch("pybuilder.plugins.python.pdoc_plugin.tail_log") @patch("pybuilder.plugins.python.pdoc_plugin.os.mkdir") @patch("pybuilder.plugins.python.pdoc_plugin.os.path.exists") def test_pdoc_html_adds_html_dir(self, *_): pdoc_init(self.project) self.project.set_property("pdoc_module_name", "pdoc_module_name_value") self.project.set_property("pdoc_command_args", []) pdoc_compile_docs(self.project, self.logger, self.reactor) pyb_env = self.reactor.pybuilder_venv pyb_env.execute_command.assert_called_with( ["pdoc", "pdoc_module_name_value"], cwd=self.project.expand_path("$dir_target", "pdocs"), env={ "PYTHONPATH": self.project.expand_path("$dir_source_main_python"), "PATH": pyb_env.environ["PATH"] }, outfile_name=self.project.expand_path("$dir_reports", "pdoc"), error_file_name=self.project.expand_path("$dir_reports", "pdoc.err")) self.project.set_property("pdoc_command_args", ["--html"]) pdoc_compile_docs(self.project, self.logger, self.reactor) pyb_env.execute_command.assert_called_with( [ "pdoc", "--html", "--html-dir", self.project.expand_path("$dir_target", "pdocs"), "pdoc_module_name_value" ], cwd=self.project.expand_path("$dir_target", "pdocs"), env={ "PYTHONPATH": self.project.expand_path("$dir_source_main_python"), "PATH": pyb_env.environ["PATH"] }, outfile_name=self.project.expand_path("$dir_reports", "pdoc"), error_file_name=self.project.expand_path("$dir_reports", "pdoc.err"))
class InitPythonDirectoriesTest(unittest.TestCase): def greedy(self, generator): return list(generator) def setUp(self): self.project = Project(".") @patch("pybuilder.plugins.python.core_plugin.listdir") @patch("pybuilder.plugins.python.core_plugin.isfile") def test_should_set_list_modules_function_with_project_modules( self, _, source_listdir): source_listdir.return_value = ["foo.py", "bar.py", "some-package"] init_python_directories(self.project) self.assertEqual(["foo", "bar"], self.greedy(self.project.list_modules())) @patch("pybuilder.plugins.python.core_plugin.walk") @patch("pybuilder.plugins.python.core_plugin.exists") def test_should_set_list_packages_function_with_project_packages( self, _, walk): self.project.set_property("dir_source_main_python", "src/main/python") init_python_directories(self.project) src_path = self.project.expand_path("$dir_source_main_python") walk.return_value = [(join(src_path, "pybuilder"), ["pluginhelper", "plugins"], ("__init__.py", "foo.py", "foo.txt")), (join(src_path, "pybuilder", "pluginhelper"), (), "__init__.py"), (join(src_path, "pybuilder", "plugins"), (), "__init__.py")] self.assertEqual( ["pybuilder", "pybuilder.pluginhelper", "pybuilder.plugins"], self.greedy(self.project.list_packages())) @patch("pybuilder.plugins.python.core_plugin.walk") @patch("pybuilder.plugins.python.core_plugin.exists") def test_should_not_cut_off_packages_when_path_ends_with_trailing_slash( self, _, walk): self.project.set_property("dir_source_main_python", "src/main/python/") init_python_directories(self.project) src_path = self.project.expand_path("$dir_source_main_python") walk.return_value = [(join(src_path, "pybuilder"), ["pluginhelper", "plugins"], ("__init__.py", "foo.py", "foo.txt")), (join(src_path, "pybuilder", "pluginhelper"), (), "__init__.py"), (join(src_path, "pybuilder", "plugins"), (), "__init__.py")] self.assertEqual( ["pybuilder", "pybuilder.pluginhelper", "pybuilder.plugins"], self.greedy(self.project.list_packages())) def test_should_set_python_sources_property(self): init_python_directories(self.project) self.assertEqual( "src/main/python", self.project.get_property(PYTHON_SOURCES_PROPERTY, "caboom")) def test_should_set_scripts_sources_property(self): init_python_directories(self.project) self.assertEqual( "src/main/scripts", self.project.get_property(SCRIPTS_SOURCES_PROPERTY, "caboom")) def test_should_set_dist_scripts_property(self): init_python_directories(self.project) self.assertEqual("scripts", self.project.get_property(SCRIPTS_TARGET_PROPERTY)) def test_should_set_dist_property(self): init_python_directories(self.project) self.assertEqual( "$dir_target/dist/.-1.0.dev0", self.project.get_property(DISTRIBUTION_PROPERTY, "caboom"))
def _make_folder(project: Project, *path: str) -> str: folder = project.expand_path(*path) if not os.path.exists(folder): os.mkdir(folder) return folder
def write_version_into_library(project: Project): version_file = Path( project.expand_path('$dir_dist', f'{project.name}/__version__.py')) version_file.write_text(version_file.read_text().replace( '%version%', project.version).replace('%hash%', VCSRevision().get_git_hash()))
class PdocPluginTests(unittest.TestCase): def setUp(self): self.logger = Mock() self.project = Project(".") self.project.set_property("dir_target", "dir_target_value") self.project.set_property("dir_source_main_python", "dir_source_main_python_value") self.project.set_property("dir_reports", "dir_reports_value") @patch("pybuilder.plugins.python.pdoc_plugin.os.mkdir") @patch("pybuilder.plugins.python.pdoc_plugin.os.path.exists") @patch("pybuilder.plugins.python.pdoc_plugin.assert_can_execute") @patch("pybuilder.plugins.python.pdoc_plugin.execute_command") def test_pdoc_prepare_works(self, exec_command, assert_can_exec, os_path_exists, os_mkdir): pdoc_init(self.project) os_path_exists.return_value = False pdoc_prepare(self.project, self.logger) self.assertEquals(os_mkdir.call_count, 1) os_path_exists.return_value = True pdoc_prepare(self.project, self.logger) self.assertEquals(os_mkdir.call_count, 1) self.assertEquals(assert_can_exec.call_count, 2) @patch("pybuilder.plugins.python.pdoc_plugin.os.mkdir") @patch("pybuilder.plugins.python.pdoc_plugin.os.path.exists") @patch("pybuilder.plugins.python.pdoc_plugin.assert_can_execute") @patch("pybuilder.plugins.python.pdoc_plugin.execute_command") def test_pdoc_requires_module_name(self, exec_command, assert_can_exec, os_path_exists, os_mkdir): pdoc_init(self.project) self.assertRaises(BuildFailedException, pdoc_compile_docs, self.project, self.logger) @patch("pybuilder.plugins.python.pdoc_plugin.os.mkdir") @patch("pybuilder.plugins.python.pdoc_plugin.os.path.exists") @patch("pybuilder.plugins.python.pdoc_plugin.assert_can_execute") @patch("pybuilder.plugins.python.pdoc_plugin.execute_command") def test_pdoc_html_adds_html_dir(self, exec_command, assert_can_exec, os_path_exists, os_mkdir): pdoc_init(self.project) self.project.set_property("pdoc_module_name", "pdoc_module_name_value") self.project.set_property("pdoc_command_args", []) pdoc_compile_docs(self.project, self.logger) exec_command.assert_called_with(['pdoc', "pdoc_module_name_value"], cwd=self.project.expand_path("$dir_target", "pdocs"), env={ 'PYTHONPATH': self.project.expand_path("$dir_source_main_python"), 'PATH': os.environ['PATH'] }, outfile_name=self.project.expand_path('$dir_reports', 'pdoc')) self.project.set_property("pdoc_command_args", ["--html"]) pdoc_compile_docs(self.project, self.logger) exec_command.assert_called_with(['pdoc', "--html", "--html-dir", self.project.expand_path('$dir_target', 'pdocs'), "pdoc_module_name_value"], cwd=self.project.expand_path("$dir_target", "pdocs"), env={ 'PYTHONPATH': self.project.expand_path("$dir_source_main_python"), 'PATH': os.environ['PATH'] }, outfile_name=self.project.expand_path('$dir_reports', 'pdoc') )
def package_cypress_artifacts(project: Project): test_dir = project.expand_path(f"${CYPRESS_TEST_DIR}") tasks.package_artifacts(project, test_dir, "cypress",project.get_property(ROLE))
def package_tavern_artifacts(project: Project): test_dir = project.expand_path(f"${TAVERN_TEST_DIR}") tasks.package_artifacts(project, test_dir, "tavern", project.get_property(ROLE))