def test_get_xml_by_path_element_none_existent_file(self): """Test get xml element by xpath 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_by_path('does_not_exist/pom.xml', 'build').text
def test_get_xml_element_by_path_exists(self): """Test to get an xml element where it exists.""" with TempDirectory() as temp_dir: pom = b'''<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> <build> <plugins> <plugin> <artifactId>maven-surefire-plugin</artifactId> <configuration> <reportsDirectory>mycompany-reports-directory</reportsDirectory> </configuration> </plugin> </plugins> </build> </project>''' temp_dir.write('pom.xml', pom) pom_file_path = path.join(temp_dir.path, 'pom.xml') element = get_xml_element_by_path( pom_file_path, 'mvn:build/mvn:plugins/mvn:plugin/[mvn:artifactId="maven-surefire-plugin"]/'\ 'mvn:configuration/mvn:reportsDirectory', default_namespace='mvn' ) assert element.text == 'mycompany-reports-directory'
def test_get_xml_element_by_path_no_exists_no_namespaces(self): """Test to get an xml element where it does not exist and no namespaces 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> <build> <plugins> <plugin> <artifactId>maven-surefire-plugin</artifactId> <configuration> <reportsDirectory>mycompany-reports-directory</reportsDirectory> </configuration> </plugin> </plugins> </build> </project>''' temp_dir.write('pom.xml', pom) pom_file_path = path.join(temp_dir.path, 'pom.xml') element = get_xml_element_by_path( pom_file_path, 'this-does-not-exist' ) assert element is None
def _run_step(self): """Runs the TSSC step implemented by this StepImplementer. Returns ------- dict Results of running this step. """ pom_file = self.get_config_value('pom-file') fail_on_no_tests = self.get_config_value('fail-on-no-tests') if not os.path.exists(pom_file): raise ValueError('Given pom file does not exist: ' + pom_file) surefire_path = 'mvn:build/mvn:plugins/mvn:plugin/[mvn:artifactId="maven-surefire-plugin"]' maven_surefire_plugin = get_xml_element_by_path( pom_file, surefire_path, default_namespace='mvn') if maven_surefire_plugin is None: raise ValueError( 'Unit test dependency "maven-surefire-plugin" missing from POM.' ) reports_dir = get_xml_element_by_path( pom_file, f'{surefire_path}/mvn:configuration/mvn:reportsDirectory', default_namespace='mvn') if reports_dir is not None: test_results_dir = reports_dir.text else: test_results_dir = os.path.join( os.path.dirname(os.path.abspath(pom_file)), 'target/surefire-reports') settings_file = self._generate_maven_settings() try: sh.mvn( # pylint: disable=no-member 'clean', 'test', '-f', pom_file, '-s', settings_file, _out=sys.stdout, _err=sys.stderr) except sh.ErrorReturnCode as error: raise RuntimeError( "Error invoking mvn: {error}".format(error=error)) from error test_results_output_path = test_results_dir if not os.path.isdir(test_results_dir) or \ len(os.listdir(test_results_dir)) == 0: if fail_on_no_tests is not True: results = { 'result': { 'success': True, 'message': 'unit test step run successfully, but no tests were found' }, 'report-artifacts': [], 'options': { 'pom-path': pom_file, 'fail-on-no-tests': False } } else: # pragma: no cover # Added 'no cover' to bypass missing unit-test step coverage error # that is covered by the following unit test: # test_unit_test_run_attempt_fails_fail_on_no_tests_flag_true raise RuntimeError('Error: No unit tests defined') else: results = { 'result': { 'success': True, 'message': 'unit test step run successfully and junit reports were generated' }, 'options': { 'pom-path': pom_file }, 'report-artifacts': [{ 'name': 'maven unit test results generated using junit', 'path': f'file://{test_results_output_path}' }] } return results
def _run_step(self): """ Runs the TSSC step implemented by this StepImplementer. Returns ------- dict Results of running this step. """ settings_file = self._generate_maven_settings() pom_file = self.get_config_value('pom-file') fail_on_no_tests = self.get_config_value('fail-on-no-tests') selenium_hub_url = self.get_config_value('selenium-hub-url') report_dir = self.get_config_value('report-dir') target_base_url = self._get_target_base_url() if not os.path.exists(pom_file): raise ValueError(f'Given pom file does not exist: {pom_file}') surefire_path = 'mvn:build/mvn:plugins/mvn:plugin/[mvn:artifactId="maven-surefire-plugin"]' maven_surefire_plugin = get_xml_element_by_path( pom_file, surefire_path, default_namespace='mvn') if maven_surefire_plugin is None: raise ValueError( 'Uat dependency "maven-surefire-plugin" missing from POM.') reports_dir = get_xml_element_by_path( pom_file, f'{surefire_path}/mvn:configuration/mvn:reportsDirectory', default_namespace='mvn') if reports_dir is not None: test_results_dir = reports_dir.text else: test_results_dir = os.path.join( os.path.dirname(os.path.abspath(pom_file)), 'target/surefire-reports') try: sh.mvn( # pylint: disable=no-member 'clean', '-Pintegration-test', f'-Dselenium.hub.url={selenium_hub_url}', f'-Dtarget.base.url={target_base_url}', f'-Dcucumber.plugin=html:target/{report_dir}/cucumber.html,' \ f'json:target/{report_dir}/cucumber.json', 'test', '-f', pom_file, '-s', settings_file, _out=sys.stdout, _err=sys.stderr ) except sh.ErrorReturnCode as error: raise RuntimeError(f'Error invoking mvn: {error}') from error if not os.path.isdir(test_results_dir) or len( os.listdir(test_results_dir)) == 0: if fail_on_no_tests is not True: results = { 'result': { 'success': True, 'message': 'Uat step run successfully, but no tests were found' }, 'report-artifacts': [], 'options': { 'pom-path': pom_file, 'fail-on-no-tests': False } } else: # pragma: no cover # Added 'no cover' to bypass missing uat step coverage error # that is covered by the following test: # test_uat_run_attempt_fails_fail_on_no_tests_flag_true raise RuntimeError('Error: No uat defined') else: results = { 'result': { 'success': True, 'message': 'Uat step run successfully and reports were generated' }, 'options': { 'pom-path': pom_file }, 'report-artifacts': [ { 'name': 'Uat results generated', 'path': f'file://{os.path.dirname(os.path.abspath(pom_file))}' \ f'/target/{report_dir}' } ] } return results