def test_relative_to(self): """ verify how Origin.relative_to() works in various situations """ # if the source does not have relative_to method, nothing is changed origin = Origin(UnknownTextSource(), 1, 2) self.assertIs(origin.relative_to("/some/path"), origin) # otherwise the source is replaced and a new origin is returned self.assertEqual( Origin(FileTextSource("/some/path/file.txt"), 1, 2).relative_to("/some/path"), Origin(FileTextSource("file.txt"), 1, 2))
def __init__(self, filename, text, provider): """ Initialize the plug-in with the specified name text """ self._filename = filename self._job_list = [] logger.debug(_("Loading jobs definitions from %r..."), filename) try: records = load_rfc822_records(text, source=FileTextSource(filename)) except RFC822SyntaxError as exc: raise PlugInError( _("Cannot load job definitions from {!r}: {}").format( filename, exc)) for record in records: try: job = JobDefinition.from_rfc822_record(record) except ValueError as exc: raise PlugInError( _("Cannot define job from record {!r}: {}").format( record, exc)) else: job._provider = provider self._job_list.append(job) logger.debug(_("Loaded %r"), job)
def test_from_string__with_name_and_origin(self): """ verify that WhiteList.from_string() works when passing name and origin """ # construct a whitelist with some dummy data, the names, pathnames and # line ranges are arbitrary whitelist = WhiteList.from_string("\n".join(self._content), name="somefile", origin=Origin( FileTextSource("somefile.txt"), 1, 3)) # verify that the patterns are okay self.assertEqual(repr(whitelist.qualifier_list[0]), "RegExpJobQualifier('^foo$', inclusive=True)") # verify that whitelist name is copied self.assertEqual(whitelist.name, "somefile") # verify that the origin is copied self.assertEqual(whitelist.origin, Origin(FileTextSource("somefile.txt"), 1, 3))
def test_job_data(self): """ verify the contents of the loaded JobDefinition object """ job = self.plugin.plugin_object[0] self.assertEqual(job.partial_id, "test/job") self.assertEqual(job.id, "2013.com.canonical.plainbox::test/job") self.assertEqual(job.plugin, "shell") self.assertEqual(job.command, "true") self.assertEqual(job.origin, Origin(FileTextSource("/path/to/jobs.txt"), 1, 3))
def test_origin_from_filename_is_filename(self): # If the test's origin has a filename, we need a valid origin # with proper data. # We're faking the name by using a StringIO subclass with a # name property, which is how rfc822 gets that data. expected_origin = Origin(FileTextSource("file.txt"), 1, 1) with NamedStringIO("key:value", fake_filename="file.txt") as stream: records = type(self).loader(stream) self.assertEqual(len(records), 1) self.assertEqual(records[0].data, {'key': 'value'}) self.assertEqual(records[0].origin, expected_origin)
def test_gt(self): """ verify that Origin instances are ordered by their constituting components """ self.assertTrue( Origin(FileTextSource('file.txt'), 1, 1) < Origin( FileTextSource('file.txt'), 1, 2) < Origin( FileTextSource('file.txt'), 1, 3)) self.assertTrue( Origin(FileTextSource('file.txt'), 1, 10) < Origin( FileTextSource('file.txt'), 2, 10) < Origin( FileTextSource('file.txt'), 3, 10)) self.assertTrue( Origin(FileTextSource('file1.txt'), 1, 10) < Origin( FileTextSource('file2.txt'), 1, 10) < Origin( FileTextSource('file3.txt'), 1, 10))
def test_whitelist_data(self): """ verify the contents of the loaded whitelist object """ self.assertEqual( self.plugin.plugin_object.qualifier_list[0].pattern_text, "^foo$") self.assertEqual( self.plugin.plugin_object.qualifier_list[1].pattern_text, "^bar$") self.assertEqual(self.plugin.plugin_object.name, 'some') self.assertEqual( self.plugin.plugin_object.origin, Origin(FileTextSource('/path/to/some.whitelist'), 1, 2))
def setUp(self): self._full_record = RFC822Record( { 'plugin': 'plugin', 'id': 'id', 'summary': 'summary', 'requires': 'requires', 'command': 'command', 'description': 'description' }, Origin(FileTextSource('file.txt'), 1, 5)) self._full_gettext_record = RFC822Record( { '_plugin': 'plugin', '_id': 'id', '_summary': 'summary', '_requires': 'requires', '_command': 'command', '_description': 'description' }, Origin(FileTextSource('file.txt.in'), 1, 5)) self._min_record = RFC822Record({ 'plugin': 'plugin', 'id': 'id', }, Origin(FileTextSource('file.txt'), 1, 2))
def test_from_file(self): """ verify that WhiteList.from_file() works """ with self.mocked_file(self._name, self._content): whitelist = WhiteList.from_file(self._name) # verify that the patterns are okay self.assertEqual(repr(whitelist.qualifier_list[0]), "RegExpJobQualifier('^foo$', inclusive=True)") # verify that whitelist name got set self.assertEqual(whitelist.name, "whitelist") # verify that the origin got set self.assertEqual(whitelist.origin, Origin(FileTextSource("whitelist.txt"), 1, 3))
def from_file(cls, pathname, implicit_namespace=None): """ Load and initialize the WhiteList object from the specified file. :param pathname: file to load :param implicit_namespace: (optional) implicit namespace for jobs that are using partial identifiers (all jobs) :returns: a fresh WhiteList object """ pattern_list, max_lineno = cls._load_patterns(pathname) name = os.path.splitext(os.path.basename(pathname))[0] origin = Origin(FileTextSource(pathname), 1, max_lineno) return cls(pattern_list, name, origin, implicit_namespace)
def from_string(cls, text, *, filename=None, name=None, origin=None, implicit_namespace=None): """ Load and initialize the WhiteList object from the specified string. :param text: full text of the whitelist :param filename: (optional, keyword-only) filename from which text was read from. This simulates a call to :meth:`from_file()` which properly computes the name and origin of the whitelist. :param name: (optional) name of the whitelist, only used if filename is not specified. :param origin: (optional) origin of the whitelist, only used if a filename is not specified. If omitted a default origin value will be constructed out of UnknownTextSource instance :param implicit_namespace: (optional) implicit namespace for jobs that are using partial identifiers (all jobs) :returns: a fresh WhiteList object The optional filename or a pair of name and origin arguments may be provided in order to have additional meta-data. This is typically needed when the :meth:`from_file()` method cannot be used as the caller already has the full text of the intended file available. """ _logger.debug("Loaded whitelist from %r", filename) pattern_list, max_lineno = cls._parse_patterns(text) # generate name and origin if filename is provided if filename is not None: name = WhiteList.name_from_filename(filename) origin = Origin(FileTextSource(filename), 1, max_lineno) else: # otherwise generate origin if it's not specified if origin is None: origin = Origin(UnknownTextSource(), 1, max_lineno) return cls(pattern_list, name, origin, implicit_namespace)
def test_eq(self): """ verify instances of Origin are all equal to other instances with the same instance attributes but not equal to instances with different attributes """ origin1 = Origin(self.origin.source, self.origin.line_start, self.origin.line_end) origin2 = Origin(self.origin.source, self.origin.line_start, self.origin.line_end) self.assertTrue(origin1 == origin2) origin_other1 = Origin(self.origin.source, self.origin.line_start + 1, self.origin.line_end) self.assertTrue(origin1 != origin_other1) self.assertFalse(origin1 == origin_other1) origin_other2 = Origin(self.origin.source, self.origin.line_start, self.origin.line_end + 1) self.assertTrue(origin1 != origin_other2) self.assertFalse(origin1 == origin_other2) origin_other3 = Origin(FileTextSource("unrelated"), self.origin.line_start, self.origin.line_end) self.assertTrue(origin1 != origin_other3) self.assertFalse(origin1 == origin_other3)
def setUp(self): self.raw_data = {'key': ' value'} self.data = {'key': 'value'} self.origin = Origin(FileTextSource('file.txt'), 1, 1) self.record = RFC822Record(self.data, self.origin, self.raw_data)
def setUp(self): self.origin = Origin(FileTextSource("file.txt"), 10, 12)