def test_get_xml_element_none_existent_file(self): """Test get xml element but file does not exist.""" with self.assertRaisesRegex( ValueError, r"Given xml file does not exist: does_not_exist/pom.xml"): # pylint: disable=expression-not-assigned get_xml_element('does_not_exist/pom.xml', 'version').text
def _run_step(self): """Runs the step implemented by this StepImplementer. Returns ------- StepResult Object containing the dictionary results of this step. """ step_result = StepResult.from_step_implementer(self) pom_file = self.get_value('pom-file') pom_version = None try: pom_version_element = get_xml_element(pom_file, 'version') pom_version = pom_version_element.text except ValueError: pom_version = None if not pom_version: step_result.success = False step_result.message = f'Given pom file ({pom_file})' + \ ' does not contain a \"version\" key.' return step_result step_result.add_artifact( name='app-version', value=pom_version ) return step_result
def test_get_xml_element_element_does_not_exist(self): """Test get xml element but does not exist.""" with TempDirectory() as temp_dir: pom = b'''<project> <modelVersion>4.0.0</modelVersion> <groupId>com.mycompany.app</groupId> <artifactId>my-app</artifactId> <version>42.1</version> </project>''' temp_dir.write('pom.xml', pom) pom_file_path = path.join(temp_dir.path, 'pom.xml') with self.assertRaisesRegex( ValueError, r"Given xml file \(.*\) does not have ./does-not-exist element" ): get_xml_element(pom_file_path, 'does-not-exist')
def test_get_xml_element_from_pom_file(self): """Test getting an xml element from the pom file.""" with TempDirectory() as temp_dir: pom = b'''<project> <modelVersion>4.0.0</modelVersion> <groupId>com.mycompany.app</groupId> <artifactId>my-app</artifactId> <version>42.1</version> </project>''' temp_dir.write('pom.xml', pom) pom_file_path = path.join(temp_dir.path, 'pom.xml') version = get_xml_element(pom_file_path, 'version').text artifact_id = get_xml_element(pom_file_path, 'artifactId').text group_id = get_xml_element(pom_file_path, 'groupId').text assert version == '42.1' assert artifact_id == 'my-app' assert group_id == 'com.mycompany.app'
def test_get_xml_element_from_pom_file_with_namespace(self): """Test get xml element from pom file with namespace.""" with TempDirectory() as temp_dir: pom = b'''<?xml version="1.0"?> <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <groupId>com.mycompany.app</groupId> <artifactId>my-app</artifactId> <version>42.1</version> </project>''' temp_dir.write('pom.xml', pom) pom_file_path = path.join(temp_dir.path, 'pom.xml') version = get_xml_element(pom_file_path, 'version').text artifact_id = get_xml_element(pom_file_path, 'artifactId').text group_id = get_xml_element(pom_file_path, 'groupId').text assert version == '42.1' assert artifact_id == 'my-app' assert group_id == 'com.mycompany.app'
def _run_step(self): # pylint: disable=too-many-locals """Runs the step implemented by this StepImplementer. Returns ------- StepResult Object containing the dictionary results of this step. """ step_result = StepResult.from_step_implementer(self) pom_file = self.get_value('pom-file') artifact_extensions = self.get_value('artifact-extensions') artifact_parent_dir = self.get_value('artifact-parent-dir') if not os.path.exists(pom_file): step_result.success = False step_result.message = f'Given pom file does not exist: {pom_file}' return step_result settings_file = self._generate_maven_settings() mvn_output_file_path = self.write_working_file('mvn_test_output.txt') try: with open(mvn_output_file_path, 'w') as mvn_output_file: out_callback = create_sh_redirect_to_multiple_streams_fn_callback( [sys.stdout, mvn_output_file]) err_callback = create_sh_redirect_to_multiple_streams_fn_callback( [sys.stderr, mvn_output_file]) sh.mvn( # pylint: disable=no-member 'clean', 'install', '-f', pom_file, '-s', settings_file, _out=out_callback, _err=err_callback) except sh.ErrorReturnCode as error: step_result.success = False step_result.message = "Package failures. See 'maven-output' report artifacts " \ f"for details: {error}" return step_result finally: step_result.add_artifact( description= "Standard out and standard error from 'mvn install'.", name='maven-output', value=mvn_output_file_path) # find the artifacts artifact_file_names = [] artifact_parent_dir_full_path = \ os.listdir(os.path.join( os.path.dirname(os.path.abspath(pom_file)), artifact_parent_dir)) for filename in artifact_parent_dir_full_path: if any(filename.endswith(ext) for ext in artifact_extensions): artifact_file_names.append(filename) # error if we find more then one artifact # see https://projects.engineering.redhat.com/browse/NAPSSPO-546 if len(artifact_file_names) > 1: step_result.success = False step_result.message = 'pom resulted in multiple artifacts with expected artifact ' \ f'extensions ({artifact_extensions}), this is unsupported' return step_result if len(artifact_file_names) < 1: step_result.success = False step_result.message = 'pom resulted in 0 with expected artifact extensions ' \ f'({artifact_extensions}), this is unsupported' return step_result artifact_id = get_xml_element(pom_file, 'artifactId').text group_id = get_xml_element(pom_file, 'groupId').text try: package_type = get_xml_element(pom_file, 'package').text except ValueError: package_type = 'jar' package_artifacts = { 'path': os.path.join(os.path.dirname(os.path.abspath(pom_file)), artifact_parent_dir, artifact_file_names[0]), 'artifact-id': artifact_id, 'group-id': group_id, 'package-type': package_type, 'pom-path': pom_file } # Currently, package returns ONE 'artifact', eg: one war file # However, in the future, an ARRAY could be returned, eg: several jar files step_result.add_artifact(name='package-artifacts', value=[package_artifacts]) return step_result