diff options
Diffstat (limited to 'tests/base.py')
-rw-r--r-- | tests/base.py | 209 |
1 files changed, 121 insertions, 88 deletions
diff --git a/tests/base.py b/tests/base.py index b239f64e..660a3af7 100644 --- a/tests/base.py +++ b/tests/base.py @@ -59,9 +59,13 @@ except ImportError: import mock # noqa -def get_scenarios(fixtures_path, in_ext='yaml', out_ext='xml', - plugins_info_ext='plugins_info.yaml', - filter_func=None): +def get_scenarios( + fixtures_path, + in_ext="yaml", + out_ext="xml", + plugins_info_ext="plugins_info.yaml", + filter_func=None, +): """Returns a list of scenarios, each scenario being described by two parameters (yaml and xml filenames by default). - content of the fixture output file (aka expected) @@ -75,8 +79,9 @@ def get_scenarios(fixtures_path, in_ext='yaml', out_ext='xml', else: files[fn] = [os.path.join(dirpath, fn)] - input_files = [files[f][0] for f in files if - re.match(r'.*\.{0}$'.format(in_ext), f)] + input_files = [ + files[f][0] for f in files if re.match(r".*\.{0}$".format(in_ext), f) + ] for input_filename in input_files: if input_filename.endswith(plugins_info_ext): @@ -85,21 +90,22 @@ def get_scenarios(fixtures_path, in_ext='yaml', out_ext='xml', if callable(filter_func) and filter_func(input_filename): continue - output_candidate = re.sub(r'\.{0}$'.format(in_ext), - '.{0}'.format(out_ext), input_filename) + output_candidate = re.sub( + r"\.{0}$".format(in_ext), ".{0}".format(out_ext), input_filename + ) # assume empty file if no output candidate found if os.path.basename(output_candidate) in files: out_filenames = files[os.path.basename(output_candidate)] else: out_filenames = None - plugins_info_candidate = re.sub(r'\.{0}$'.format(in_ext), - '.{0}'.format(plugins_info_ext), - input_filename) + plugins_info_candidate = re.sub( + r"\.{0}$".format(in_ext), ".{0}".format(plugins_info_ext), input_filename + ) if os.path.basename(plugins_info_candidate) not in files: plugins_info_candidate = None - conf_candidate = re.sub(r'\.yaml$|\.json$', '.conf', input_filename) + conf_candidate = re.sub(r"\.yaml$|\.json$", ".conf", input_filename) conf_filename = files.get(os.path.basename(conf_candidate), None) if conf_filename: @@ -108,12 +114,17 @@ def get_scenarios(fixtures_path, in_ext='yaml', out_ext='xml', # for testing purposes we want to avoid using user config files conf_filename = os.devnull - scenarios.append((input_filename, { - 'in_filename': input_filename, - 'out_filenames': out_filenames, - 'conf_filename': conf_filename, - 'plugins_info_filename': plugins_info_candidate, - })) + scenarios.append( + ( + input_filename, + { + "in_filename": input_filename, + "out_filenames": out_filenames, + "conf_filename": conf_filename, + "plugins_info_filename": plugins_info_candidate, + }, + ) + ) return scenarios @@ -121,7 +132,7 @@ def get_scenarios(fixtures_path, in_ext='yaml', out_ext='xml', class BaseTestCase(testtools.TestCase): # TestCase settings: - maxDiff = None # always dump text difference + maxDiff = None # always dump text difference longMessage = True # keep normal error message when providing our def setUp(self): @@ -137,12 +148,12 @@ class BaseTestCase(testtools.TestCase): # Read XML content, assuming it is unicode encoded xml_content = "" for f in sorted(self.out_filenames): - with io.open(f, 'r', encoding='utf-8') as xml_file: + with io.open(f, "r", encoding="utf-8") as xml_file: xml_content += u"%s" % xml_file.read() return xml_content def _read_yaml_content(self, filename): - with io.open(filename, 'r', encoding='utf-8') as yaml_file: + with io.open(filename, "r", encoding="utf-8") as yaml_file: yaml_content = yaml.load(yaml_file) return yaml_content @@ -170,10 +181,10 @@ class BaseScenariosTestCase(testscenarios.TestWithScenarios, BaseTestCase): plugins_info = None if self.plugins_info_filename: plugins_info = self._read_yaml_content(self.plugins_info_filename) - self.addDetail("plugins-info-filename", - text_content(self.plugins_info_filename)) - self.addDetail("plugins-info", - text_content(str(plugins_info))) + self.addDetail( + "plugins-info-filename", text_content(self.plugins_info_filename) + ) + self.addDetail("plugins-info", text_content(str(plugins_info))) parser = YamlParser(jjb_config) registry = ModuleRegistry(jjb_config, plugins_info) @@ -182,76 +193,89 @@ class BaseScenariosTestCase(testscenarios.TestWithScenarios, BaseTestCase): pub = self.klass(registry) project = None - if ('project-type' in yaml_content): - if (yaml_content['project-type'] == "maven"): + if "project-type" in yaml_content: + if yaml_content["project-type"] == "maven": project = project_maven.Maven(registry) - elif (yaml_content['project-type'] == "matrix"): + elif yaml_content["project-type"] == "matrix": project = project_matrix.Matrix(registry) - elif (yaml_content['project-type'] == "flow"): + elif yaml_content["project-type"] == "flow": project = project_flow.Flow(registry) - elif (yaml_content['project-type'] == "multijob"): + elif yaml_content["project-type"] == "multijob": project = project_multijob.MultiJob(registry) - elif (yaml_content['project-type'] == "multibranch"): + elif yaml_content["project-type"] == "multibranch": project = project_multibranch.WorkflowMultiBranch(registry) - elif (yaml_content['project-type'] == "multibranch-defaults"): - project = project_multibranch.WorkflowMultiBranchDefaults(registry) # noqa - elif (yaml_content['project-type'] == "externaljob"): + elif yaml_content["project-type"] == "multibranch-defaults": + project = project_multibranch.WorkflowMultiBranchDefaults( + registry + ) # noqa + elif yaml_content["project-type"] == "externaljob": project = project_externaljob.ExternalJob(registry) - if 'view-type' in yaml_content: - if yaml_content['view-type'] == "all": + if "view-type" in yaml_content: + if yaml_content["view-type"] == "all": project = view_all.All(None) - elif yaml_content['view-type'] == "list": + elif yaml_content["view-type"] == "list": project = view_list.List(None) - elif yaml_content['view-type'] == "pipeline": + elif yaml_content["view-type"] == "pipeline": project = view_pipeline.Pipeline(None) else: - raise InvalidAttributeError( - 'view-type', yaml_content['view-type']) + raise InvalidAttributeError("view-type", yaml_content["view-type"]) if project: xml_project = project.root_xml(yaml_content) else: - xml_project = XML.Element('project') + xml_project = XML.Element("project") # Generate the XML tree directly with modules/general pub.gen_xml(xml_project, yaml_content) # check output file is under correct path - if 'name' in yaml_content: + if "name" in yaml_content: prefix = os.path.dirname(self.in_filename) # split using '/' since fullname uses URL path separator - expected_folders = [os.path.normpath( - os.path.join(prefix, - '/'.join(parser._getfullname(yaml_content). - split('/')[:-1])))] + expected_folders = [ + os.path.normpath( + os.path.join( + prefix, + "/".join(parser._getfullname(yaml_content).split("/")[:-1]), + ) + ) + ] actual_folders = [os.path.dirname(f) for f in self.out_filenames] self.assertEquals( - expected_folders, actual_folders, - "Output file under wrong path, was '%s', should be '%s'" % - (self.out_filenames[0], - os.path.join(expected_folders[0], - os.path.basename(self.out_filenames[0])))) + expected_folders, + actual_folders, + "Output file under wrong path, was '%s', should be '%s'" + % ( + self.out_filenames[0], + os.path.join( + expected_folders[0], os.path.basename(self.out_filenames[0]) + ), + ), + ) # Prettify generated XML - pretty_xml = XmlJob(xml_project, 'fixturejob').output().decode('utf-8') + pretty_xml = XmlJob(xml_project, "fixturejob").output().decode("utf-8") self.assertThat( pretty_xml, - testtools.matchers.DocTestMatches(expected_xml, - doctest.ELLIPSIS | - doctest.REPORT_NDIFF) + testtools.matchers.DocTestMatches( + expected_xml, doctest.ELLIPSIS | doctest.REPORT_NDIFF + ), ) class SingleJobTestCase(BaseScenariosTestCase): - def test_yaml_snippet(self): config = self._get_config() - expected_xml = self._read_utf8_content().strip() \ - .replace('<BLANKLINE>', '').replace('\n\n', '\n') + expected_xml = ( + self._read_utf8_content() + .strip() + .replace("<BLANKLINE>", "") + .replace("\n\n", "\n") + ) parser = YamlParser(config) parser.parse(self.in_filename) @@ -259,10 +283,10 @@ class SingleJobTestCase(BaseScenariosTestCase): plugins_info = None if self.plugins_info_filename: plugins_info = self._read_yaml_content(self.plugins_info_filename) - self.addDetail("plugins-info-filename", - text_content(self.plugins_info_filename)) - self.addDetail("plugins-info", - text_content(str(plugins_info))) + self.addDetail( + "plugins-info-filename", text_content(self.plugins_info_filename) + ) + self.addDetail("plugins-info", text_content(str(plugins_info))) registry = ModuleRegistry(config, plugins_info) registry.set_parser_data(parser.data) @@ -277,53 +301,62 @@ class SingleJobTestCase(BaseScenariosTestCase): # check reference files are under correct path for folders prefix = os.path.dirname(self.in_filename) # split using '/' since fullname uses URL path separator - expected_folders = list(set([ - os.path.normpath( - os.path.join(prefix, - '/'.join(job_data['name'].split('/')[:-1]))) - for job_data in job_data_list - ])) + expected_folders = list( + set( + [ + os.path.normpath( + os.path.join(prefix, "/".join(job_data["name"].split("/")[:-1])) + ) + for job_data in job_data_list + ] + ) + ) actual_folders = [os.path.dirname(f) for f in self.out_filenames] six.assertCountEqual( self, - expected_folders, actual_folders, - "Output file under wrong path, was '%s', should be '%s'" % - (self.out_filenames[0], - os.path.join(expected_folders[0], - os.path.basename(self.out_filenames[0])))) + expected_folders, + actual_folders, + "Output file under wrong path, was '%s', should be '%s'" + % ( + self.out_filenames[0], + os.path.join( + expected_folders[0], os.path.basename(self.out_filenames[0]) + ), + ), + ) # Prettify generated XML - pretty_xml = u"\n".join(job.output().decode('utf-8') - for job in xml_jobs) \ - .strip().replace('\n\n', '\n') + pretty_xml = ( + u"\n".join(job.output().decode("utf-8") for job in xml_jobs) + .strip() + .replace("\n\n", "\n") + ) self.assertThat( pretty_xml, - testtools.matchers.DocTestMatches(expected_xml, - doctest.ELLIPSIS | - doctest.REPORT_NDIFF)) + testtools.matchers.DocTestMatches( + expected_xml, doctest.ELLIPSIS | doctest.REPORT_NDIFF + ), + ) class JsonTestCase(BaseScenariosTestCase): - def test_yaml_snippet(self): expected_json = self._read_utf8_content() yaml_content = self._read_yaml_content(self.in_filename) - pretty_json = json.dumps(yaml_content, indent=4, - separators=(',', ': ')) + pretty_json = json.dumps(yaml_content, indent=4, separators=(",", ": ")) self.assertThat( pretty_json, - testtools.matchers.DocTestMatches(expected_json, - doctest.ELLIPSIS | - doctest.REPORT_NDIFF) + testtools.matchers.DocTestMatches( + expected_json, doctest.ELLIPSIS | doctest.REPORT_NDIFF + ), ) class YamlTestCase(BaseScenariosTestCase): - def test_yaml_snippet(self): expected_yaml = self._read_utf8_content() yaml_content = self._read_yaml_content(self.in_filename) @@ -337,7 +370,7 @@ class YamlTestCase(BaseScenariosTestCase): self.assertThat( pretty_yaml, - testtools.matchers.DocTestMatches(expected_yaml, - doctest.ELLIPSIS | - doctest.REPORT_NDIFF) + testtools.matchers.DocTestMatches( + expected_yaml, doctest.ELLIPSIS | doctest.REPORT_NDIFF + ), ) |