def test_sources_field(self): self.create_file('foo/bar/a.txt', 'a_contents') self.create_file('foo/bar/b.txt', 'b_contents') self.assertNotEqual( SourcesField( sources_rel_path='foo/bar', sources=['a.txt'], ).fingerprint(), SourcesField( sources_rel_path='foo/bar', sources=['b.txt'], ).fingerprint(), ) self.assertEqual( SourcesField( sources_rel_path='foo/bar', sources=['a.txt'], ).fingerprint(), SourcesField( sources_rel_path='foo/bar', sources=['a.txt'], ).fingerprint(), ) self.assertEqual( SourcesField( sources_rel_path='foo/bar', sources=['a.txt'], ).fingerprint(), SourcesField( sources_rel_path='foo/bar', sources=['a.txt'], ).fingerprint(), ) self.assertEqual( SourcesField( sources_rel_path='foo/bar', sources=['a.txt', 'b.txt'], ).fingerprint(), SourcesField( sources_rel_path='foo/bar', sources=['b.txt', 'a.txt'], ).fingerprint(), ) fp1 = SourcesField( sources_rel_path='foo/bar', sources=['a.txt'], ).fingerprint() self.create_file('foo/bar/a.txt', 'a_contents_different') fp2 = SourcesField( sources_rel_path='foo/bar', sources=['a.txt'], ).fingerprint() self.assertNotEqual(fp1, fp2)
def test_passes_eager_fileset_with_spec_through(self): self.create_file('foo/a.txt', 'a_contents') fileset = EagerFilesetWithSpec('foo', {'globs': ['foo/a.txt']}, ['foo/a.txt'], {'foo/a.txt': b'12345'}) sf = SourcesField(sources=fileset) self.assertIs(fileset, sf.sources) self.assertEqual(['foo/a.txt'], list(sf.source_paths)) self.assertEqual(['foo/foo/a.txt'], list(sf.relative_to_buildroot()))
def test_passes_eager_fileset_with_spec_through(self): self.create_file('foo/a.txt', 'a_contents') fileset = EagerFilesetWithSpec(rel_root='foo', # Glob spec is relative to build root filespec={'globs': ['foo/foo/a.txt']}, # files are relative to `rel_root` files=['foo/a.txt'], files_hash={'foo/a.txt': b'12345'}) sf = SourcesField(sources=fileset) self.assertIs(fileset, sf.sources) self.assertEqual(['foo/a.txt'], list(sf.source_paths)) self.assertEqual(['foo/foo/a.txt'], list(sf.relative_to_buildroot()))
def test_sources_field(self) -> None: self.create_file("foo/bar/a.txt", "a_contents") self.create_file("foo/bar/b.txt", "b_contents") self.assertNotEqual( SourcesField(sources=self.sources("foo/bar", "a.txt"), ).fingerprint(), SourcesField(sources=self.sources("foo/bar", "b.txt"), ).fingerprint(), ) self.assertEqual( SourcesField(sources=self.sources("foo/bar", "a.txt"), ).fingerprint(), SourcesField(sources=self.sources("foo/bar", "a.txt"), ).fingerprint(), ) self.assertEqual( SourcesField(sources=self.sources("foo/bar", "a.txt", "b.txt"), ).fingerprint(), SourcesField(sources=self.sources("foo/bar", "a.txt", "b.txt"), ).fingerprint(), ) fp1 = SourcesField(sources=self.sources("foo/bar", "a.txt"), ).fingerprint() self.create_file("foo/bar/a.txt", "a_contents_different") fp2 = SourcesField(sources=self.sources("foo/bar", "a.txt"), ).fingerprint() self.assertNotEqual(fp1, fp2)
def test_passes_eager_fileset_with_spec_through(self): self.create_file('foo/a.txt', 'a_contents') fileset = EagerFilesetWithSpec( rel_root='foo', # Glob spec is relative to build root filespec={'globs': ['foo/foo/a.txt']}, # files are relative to `rel_root` files=['foo/a.txt'], files_hash={'foo/a.txt': b'12345'}) sf = SourcesField(sources=fileset) self.assertIs(fileset, sf.sources) self.assertEqual(['foo/a.txt'], list(sf.source_paths)) self.assertEqual(['foo/foo/a.txt'], list(sf.relative_to_buildroot()))
def create_sources_field(self, sources, sources_rel_path, address=None, key_arg=None): """Factory method to create a SourcesField appropriate for the type of the sources object. Note that this method is called before the call to Target.__init__ so don't expect fields to be populated! :API: public :return: a payload field object representing the sources parameter :rtype: SourcesField """ if isinstance(sources, Addresses): # Currently, this is only created by the result of from_target() which takes a single argument if len(sources.addresses) != 1: raise self.WrongNumberOfAddresses( "Expected a single address to from_target() as argument to {spec}" .format(spec=address.spec)) referenced_address = Address.parse(sources.addresses[0], relative_to=sources.rel_path) return DeferredSourcesField(ref_address=referenced_address) elif sources is None: sources = FilesetWithSpec.empty(sources_rel_path) elif not isinstance(sources, FilesetWithSpec): # Received a literal sources list: convert to a FilesetWithSpec via Files. sources = Files.create_fileset_with_spec(sources_rel_path, *sources) return SourcesField(sources=sources)
def create_sources_field(self, sources, sources_rel_path, address=None, key_arg=None): """Factory method to create a SourcesField appropriate for the type of the sources object. Note that this method is called before the call to Target.__init__ so don't expect fields to be populated! :API: public :return: a payload field object representing the sources parameter :rtype: SourcesField """ if isinstance(sources, Addresses): # Currently, this is only created by the result of from_target() which takes a single argument if len(sources.addresses) != 1: key_arg_section = "'{}' to be ".format(key_arg) if key_arg else "" spec_section = " to '{}'".format(address.spec) if address else "" raise self.WrongNumberOfAddresses( "Expected {key_arg_section}a single address to from_target() as argument{spec_section}" .format(key_arg_section=key_arg_section, spec_section=spec_section)) referenced_address = Address.parse(sources.addresses[0], relative_to=sources.rel_path) return DeferredSourcesField(ref_address=referenced_address) elif sources is None: sources = FilesetWithSpec.empty(sources_rel_path) elif isinstance(sources, FilesetWithSpec): pass elif isinstance(sources, (set, list, tuple)): # Received a literal sources list: convert to a FilesetWithSpec via Files. sources = Files.create_fileset_with_spec(sources_rel_path, *sources) else: key_arg_section = "'{}' to be ".format(key_arg) if key_arg else "" raise TargetDefinitionException(self, "Expected {}a glob, an address or a list, but was {}" .format(key_arg_section, type(sources))) return SourcesField(sources=sources)
def create_sources_field(self, sources, sources_rel_path, address=None, key_arg=None): """Factory method to create a SourcesField appropriate for the type of the sources object. Note that this method is called before the call to Target.__init__ so don't expect fields to be populated! :API: public :return: a payload field object representing the sources parameter :rtype: SourcesField """ if sources is None: sources = FilesetWithSpec.empty(sources_rel_path) elif isinstance(sources, FilesetWithSpec): pass elif isinstance(sources, (set, list, tuple)): # Received a literal sources list: convert to a FilesetWithSpec via Files. sources = Files.create_fileset_with_spec(sources_rel_path, *sources) else: key_arg_section = "'{}' to be ".format(key_arg) if key_arg else "" raise TargetDefinitionException(self, "Expected {}a glob, an address or a list, but was {}" .format(key_arg_section, type(sources))) return SourcesField(sources=sources)
def create_sources_field(self, sources, sources_rel_path, key_arg=None): """Factory method to create a SourcesField appropriate for the type of the sources object. Note that this method is called before the call to Target.__init__ so don't expect fields to be populated! :API: public :return: a payload field object representing the sources parameter :rtype: SourcesField """ if not sources: sources = FilesetWithSpec.empty(sources_rel_path) elif not isinstance(sources, FilesetWithSpec): key_arg_section = "'{}' to be ".format(key_arg) if key_arg else "" raise TargetDefinitionException( self, "Expected {}a glob, an address or a list, but was {}".format( key_arg_section, type(sources))) elif not isinstance(sources, EagerFilesetWithSpec): deprecated_conditional(lambda: True, '1.12.0.dev0', ( 'FilesetWithSpec sources values are deprecated except for EagerFilesetWithSpec values. ' 'Saw value of type {}').format(type(sources))) return SourcesField(sources=sources)
def create_sources_field(self, sources, sources_rel_path, key_arg=None): """Factory method to create a SourcesField appropriate for the type of the sources object. Note that this method is called before the call to Target.__init__ so don't expect fields to be populated! :API: public :return: a payload field object representing the sources parameter :rtype: SourcesField """ if sources is None: # Make sure we don't apply the defaulting to uses of this method other than for # creating a sources= field (e.g., we also use this for creating resources= fields). # Note that the check for supports_default_sources() precedes the subsystem check. # This is so that tests don't need to set up the subsystem when creating targets that # legitimately do not require sources. if ((key_arg is None or key_arg == 'sources') and self.supports_default_sources() and self.Arguments. global_instance().get_options().implicit_sources): sources = self.default_sources(sources_rel_path) else: sources = FilesetWithSpec.empty(sources_rel_path) elif isinstance(sources, (set, list, tuple)): # Received a literal sources list: convert to a FilesetWithSpec via Files. sources = Files.create_fileset_with_spec(sources_rel_path, *sources) elif not isinstance(sources, FilesetWithSpec): key_arg_section = "'{}' to be ".format(key_arg) if key_arg else "" raise TargetDefinitionException( self, "Expected {}a glob, an address or a list, but was {}".format( key_arg_section, type(sources))) return SourcesField(sources=sources)
def __init__(self, script=None, sources=None, address=None, payload=None, **kwargs): if sources is not None: raise TargetDefinitionException( self, "sources must be None: was {!r}.".format(sources)) elif script and script.endswith(self.script_file_ext): script_relpath_buildroot = os.path.join(address.spec_path, script) base_globs = BaseGlobs.from_sources_field( [script_relpath_buildroot], address.spec_path) fileset_with_spec = LazyFilesetWithSpec(address.spec_path, base_globs.filespecs, lambda: [script]) else: raise TargetDefinitionException( self, "script argument '{}' to {}() must end in '{}'.".format( script, self.alias(), self.script_file_ext)) payload = payload or Payload() payload.add_fields({ 'sources': SourcesField(fileset_with_spec, address), }) super(Perl6Binary, self).__init__(address=address, payload=payload, **kwargs)
def test_passes_eager_fileset_with_spec_through(self): self.create_file('foo/foo/a.txt', 'a_contents') fileset = self.sources_for(['foo/a.txt'], 'foo') sf = SourcesField(sources=fileset) self.assertIs(fileset, sf.sources) self.assertEqual(['foo/a.txt'], list(sf.source_paths)) self.assertEqual(['foo/foo/a.txt'], list(sf.relative_to_buildroot())) digest = '56001a7e48555f156420099a99da60a7a83acc90853046709341bf9f00a6f944' want_snapshot = Snapshot(Digest(digest, 77), ('foo/foo/a.txt', ), ()) # We explicitly pass a None scheduler because we expect no scheduler lookups to be required # in order to get a Snapshot. self.assertEqual(sf.snapshot(scheduler=None), want_snapshot)
def test_passes_eager_fileset_with_spec_through(self): self.create_file('foo/foo/a.txt', 'a_contents') fileset = self.sources_for(['foo/a.txt'], 'foo') sf = SourcesField(sources=fileset) self.assertIs(fileset, sf.sources) self.assertEqual(['foo/a.txt'], list(sf.source_paths)) self.assertEqual(['foo/foo/a.txt'], list(sf.relative_to_buildroot())) digest = '56001a7e48555f156420099a99da60a7a83acc90853046709341bf9f00a6f944' want_snapshot = Snapshot(Digest(text_type(digest), 77), ('foo/foo/a.txt',), ()) # We explicitly pass a None scheduler because we expect no scheduler lookups to be required # in order to get a Snapshot. self.assertEqual(sf.snapshot(scheduler=None), want_snapshot)
def test_passes_eager_fileset_with_spec_through(self): self.create_file('foo/a.txt', 'a_contents') fileset = EagerFilesetWithSpec('foo', 'a.txt', {'a.txt': b'12345'}) sf = SourcesField(sources=fileset) self.assertIs(fileset, sf.sources) self.assertEqual(['a.txt'], list(sf.source_paths))
def test_passes_lazy_fileset_with_spec_through(self): self.create_file('foo/a.txt', 'a_contents') fileset = LazyFilesetWithSpec('foo', {'globs':['foo/a.txt']}, lambda: ['foo/a.txt']) sf = SourcesField(sources=fileset) self.assertIs(fileset, sf.sources) self.assertEqual(['foo/a.txt'], list(sf.source_paths))
def test_passes_lazy_fileset_with_spec_through(self) -> None: self.create_file("foo/a.txt", "a_contents") fileset = LazyFilesetWithSpec("foo", {"globs": ["foo/a.txt"]}, lambda: ["foo/a.txt"]) sf = SourcesField(sources=fileset) self.assertIs(fileset, sf.sources) self.assertEqual(["foo/a.txt"], list(sf.source_paths))
def create_sources_field(self, sources, sources_rel_path, key_arg=None): """Factory method to create a SourcesField appropriate for the type of the sources object. Note that this method is called before the call to Target.__init__ so don't expect fields to be populated! :API: public :return: a payload field object representing the sources parameter :rtype: SourcesField """ if sources is None: # Make sure we don't apply the defaulting to uses of this method other than for # creating a sources= field (e.g., we also use this for creating resources= fields). # Note that the check for supports_default_sources() precedes the subsystem check. # This is so that tests don't need to set up the subsystem when creating targets that # legitimately do not require sources. if (key_arg is None or key_arg == 'sources') and self.supports_default_sources(): deprecated_conditional( lambda: True, '1.11.0.dev0', 'Default sources should always be parsed through the engine not by create_sources_field. ' 'This code should be unreachable, and this message should never be displayed. ' 'If you see this message, please contact pants-dev. ' 'Class which caused this message: {}'.format( self.__class__.__name__)) sources = self.default_sources(sources_rel_path) else: sources = FilesetWithSpec.empty(sources_rel_path) elif isinstance(sources, (set, list, tuple)): if sources: # Received a literal sources list: convert to a FilesetWithSpec via Files. deprecated_conditional(lambda: True, '1.11.0.dev0', ( 'Passing collections as the value of the sources argument to create_sources_field is ' 'deprecated, and now takes a slow path. Instead, class {} should have its sources ' 'argument populated by the engine, either by using the standard parsing pipeline, or by ' 'requesting a SourcesField product from the v2 engine.' ).format(self.__class__.__name__)) sources = Files.create_fileset_with_spec( sources_rel_path, *sources) else: sources = FilesetWithSpec.empty(sources_rel_path) elif not isinstance(sources, FilesetWithSpec): key_arg_section = "'{}' to be ".format(key_arg) if key_arg else "" raise TargetDefinitionException( self, "Expected {}a glob, an address or a list, but was {}".format( key_arg_section, type(sources))) elif not isinstance(sources, EagerFilesetWithSpec): deprecated_conditional(lambda: True, '1.11.0.dev0', ( 'FilesetWithSpec sources values are deprecated except for EagerFilesetWithSpec values. ' 'Saw value of type {}').format(type(sources))) return SourcesField(sources=sources)
def create_sources_field(self, sources, sources_rel_path, address=None, key_arg=None): """Factory method to create a SourcesField appropriate for the type of the sources object. Note that this method is called before the call to Target.__init__ so don't expect fields to be populated! :return: a payload field object representing the sources parameter :rtype: SourcesField """ if isinstance(sources, Addresses): # Currently, this is only created by the result of from_target() which takes a single argument if len(sources.addresses) != 1: raise self.WrongNumberOfAddresses( "Expected a single address to from_target() as argument to {spec}" .format(spec=address.spec)) referenced_address = Address.parse(sources.addresses[0], relative_to=sources.rel_path) return DeferredSourcesField(ref_address=referenced_address) elif isinstance(sources, FilesetWithSpec): filespec = sources.filespec else: sources = sources or [] assert_list(sources, key_arg=key_arg) filespec = { 'globs': [ os.path.join(sources_rel_path, src) for src in (sources or []) ] } return SourcesField(sources=sources, sources_rel_path=sources_rel_path, filespec=filespec)
def test_fails_on_invalid_sources_kwarg(self): with self.assertRaises(ValueError): SourcesField(sources='not-a-list')
def test_fails_on_invalid_sources_kwarg(self) -> None: with self.assertRaises(ValueError): SourcesField(sources='not-a-list') # type: ignore[arg-type]
def _sources_field(self): sources_field = self.payload.get_field('sources') if sources_field is not None: return sources_field return SourcesField( sources=FilesetWithSpec.empty(self.address.spec_path))
def _sources_field(self): sources_field = self.payload.get_field('sources') return sources_field if sources_field else SourcesField( self.address.spec_path, sources=())