def test_filter_strings(self): filter = ProblemTypeFilter(['foo'], ['foo.bar.baz']) problems = [ '', 'foo', 'bar', 'foo.bar', 'foo.bar.baz', 'foo.bar.baz.x' ] filtered_problems = filter.filter(problems, key=lambda item: item) self.assertEquals(filtered_problems, ['foo', 'foo.bar'])
def test_include_and_exclude(self): filter = ProblemTypeFilter(['foo'], ['foo.bar.baz']) self.assertFalse(filter.match('')) self.assertTrue(filter.match('foo')) self.assertFalse(filter.match('bar')) self.assertTrue(filter.match('foo.bar')) self.assertFalse(filter.match('foo.bar.baz')) self.assertFalse(filter.match('foo.bar.baz.x'))
def test_filter_problems(self): filter = ProblemTypeFilter(['foo'], ['foo.bar.baz']) problems = [ api.Problem('', type=''), api.Problem('', type='foo'), api.Problem('', type='bar'), api.Problem('', type='foo.bar'), api.Problem('', type='foo.bar.baz'), api.Problem('', type='foo.bar.baz.x') ] filtered_problems = filter.filter(problems) self.assertEquals( filtered_problems, [api.Problem('', type='foo'), api.Problem('', type='foo.bar')])
def run(self): filter = ProblemTypeFilter(includes=self.include_filter, excludes=self.exclude_filter) # Open the project if it is not already opened if self.project == None: storage = api.Storage.open(self.project_name, "a", username=self.username, password=self.password) self.project = api.Project(storage) if self.config == None: if not self.configuration_name: logging.getLogger('cone').error( "No configuration given! Use -c / --configuration") return False try: self.config = self.project.get_configuration( self.configuration_name) except exceptions.NotFound: logging.getLogger('cone').error("No such configuration: %s" % self.configuration_name) return False fixers = confmlvalidation.get_fixer_classes(filter) vc = confmlvalidation.fix_configuration(self.config, None, fixers) if vc.fixes: print "Fixed %d problem(s). See log for details" % len(vc.fixes) else: print "No problems to fix found. See log for details" return True
def test_single_include(self): filter = ProblemTypeFilter(['foo.bar'], []) self.assertFalse(filter.match('')) self.assertFalse(filter.match('foo')) self.assertTrue(filter.match('foo.bar')) self.assertTrue(filter.match('foo.bar.baz')) self.assertFalse(filter.match('bar'))
def test_wildcard_in_exclude(self): filter = ProblemTypeFilter([], ['*.bar']) self.assertTrue(filter.match('foo')) self.assertTrue(filter.match('boo')) self.assertTrue(filter.match('foo.baz.bar')) self.assertFalse(filter.match('foo.bar')) self.assertFalse(filter.match('foo.bar.baz')) self.assertFalse(filter.match('boo.bar')) self.assertFalse(filter.match('boo.bar.baz')) filter = ProblemTypeFilter([], ['*.*.baz']) self.assertFalse(filter.match('foo.bar.baz')) self.assertFalse(filter.match('boo.bar.baz')) self.assertTrue(filter.match('baz')) self.assertTrue(filter.match('foo.baz'))
def test_empty_matches_everything(self): filter = ProblemTypeFilter([], []) self.assertTrue(filter.match('')) self.assertTrue(filter.match('foo')) self.assertTrue(filter.match('foo.bar')) self.assertTrue(filter.match('foo.bar.baz'))
def main(): """ Validate a configuration, or individual confml/implml files. """ shortcut_container = report_util.ReportShortcutContainer( REPORT_SHORTCUTS, 'html') parser = OptionParser(version="ConE validate %s" % VERSION) parser.add_options(cone_common.COMMON_OPTIONS) parser.add_option( "-c", "--configuration", dest="configuration", help="Defines the name of the configuration for the action", metavar="CONFIG") parser.add_option("-p", "--project", dest="project", default=".", help="defines the location of current project. Default is the "\ "current working directory.", metavar="STORAGE") group = OptionGroup(parser, 'Validate options', 'The validate action is intended for performing validation on a '\ 'configuration or individual files. ') group.add_option('--confml-file', action="append", help='Validate only the given single ConfML file.', metavar="FILE", default=None) group.add_option('--implml-file', action="append", help='Validate only the given single ImplML file.', metavar="FILE", default=None) group.add_option("--template", help="Template used in report generation. "\ "Example: --template=report_template.html.", metavar="FILE", default=None) group.add_option("--report-type", help="The type of the report to generate. This is a convenience "\ "switch for setting the used template. If --template is given, this option has no effect. "\ "Possible values: "\ + shortcut_container.get_shortcut_help_text(), metavar="TYPE",\ default=None) group.add_option("--report", help="Specifies the location of the validation report. "\ "Example --report=report.html.", metavar="FILE", default=None) group.add_option( "--dump-schema-files", help= "Dump the XML schema files used for validation into the specified directory.", metavar="DIR", default=None) group.add_option("--exclude-filter", action="append", help="Exclude validation problems by given filter. "\ "Examples: --exclude-filter=schema, --exclude-filter=schema.implml, --exclude-filter=schema.confml, --exclude-filter=schema.implml.ruleml", default=None) group.add_option("--include-filter", action="append", help="Include validation problems by given filter."\ "Examples: --include-filter=schema.implml, --include-filter=schema.implml.ruleml", default=None) parser.add_option_group(group) (options, _) = parser.parse_args() cone_common.handle_common_options(options) if not shortcut_container.is_valid_shortcut(options.report_type): parser.error("Invalid report type: %s" % options.report_type) if options.dump_schema_files: dump_dir = options.dump_schema_files print "Dumping XML schema files to '%s'" % dump_dir cone.validation.schemavalidation.dump_schema_files(dump_dir) return pt_filter = ProblemTypeFilter(includes=options.include_filter or [], excludes=options.exclude_filter or []) problems = [] if options.confml_file or options.implml_file: if options.confml_file: func = cone.validation.schemavalidation.validate_confml_data for file in options.confml_file: problems.extend(validate_file(file, func)) if options.implml_file: func = cone.validation.schemavalidation.validate_implml_data for file in options.implml_file: problems.extend(validate_file(file, func)) else: problems = validate_configuration(options.project, options.configuration, pt_filter) if problems is not None: filters = {'filter_by_severity': filter_by_severity} problems = pt_filter.filter(problems) print "Total %d problem(s) after filtering" % len(problems) print "Generating report..." problems.sort(key=lambda p: (p.severity, p.file, p.line)) template, report = shortcut_container.determine_template_and_report( options.report_type, options.template, options.report, 'validation_report') report_util.generate_report(template, report, {'problems': problems}, extra_filters=filters)