def test_sources_generated_by_target(self): root_path = os.path.join('project', 'src', 'main', 'wire') wire_path = os.path.join(root_path, 'wire-lib') file_path = os.path.join(wire_path, 'org', 'pantsbuild', 'example', 'foo.proto') SourceRoot.register(root_path) self.add_to_build_file(wire_path, dedent(''' java_wire_library(name='wire-target', sources=['{0}'], ) '''.format(os.path.relpath(file_path, wire_path)))) self.create_dir(os.path.dirname(file_path)) self.create_file(file_path, dedent(''' package org.pantsbuild.example; message Foo { optional string bar = 1; optional string foobar = 2; } ''')) target = self.target('project/src/main/wire/wire-lib:wire-target') context = self.context(target_roots=[target]) task = self.create_task(context) previous_working_directory = os.path.abspath('.') os.chdir(os.path.abspath(self.build_root)) result = task.sources_generated_by_target(target) os.chdir(previous_working_directory) self.assertEquals(OrderedSet(['org/pantsbuild/example/Foo.java']), OrderedSet(result))
def test_exported_antlr(self): SourceRoot.register('src/antlr', PythonThriftLibrary) self.create_file(relpath='src/antlr/exported/exported.g', contents=dedent(""" grammar exported; options { language = Python; } WORD: ('a'..'z'|'A'..'Z'|'0'..'9'|'-'|'_')+; static: WORD; """)) target = self.make_target(spec='src/antlr/exported', target_type=PythonAntlrLibrary, antlr_version='3.1.3', sources=['exported.g'], module='exported', provides=PythonArtifact(name='test.exported', version='0.0.0.')) # TODO(John Sirois): Find a way to get easy access to pants option defaults. self.set_options_for_scope('ivy', ivy_profile='2.4.0', ivy_settings=None, cache_dir=os.path.expanduser('~/.ivy2/pants'), http_proxy=None, https_proxy=None) with self.run_execute(target) as created: self.assertEqual([target], created)
def test_dependees_type(self): SourceRoot.register('tests', PythonTests) self.assert_console_output( 'tests/d:d', args=['--test-type=python_tests'], targets=[self.target('common/d')] )
def test_resolve_and_inject_explicit_failure(self): SourceRoot.register(os.path.join(self.build_root, '3rdparty'), GoRemoteLibrary) r1 = self.make_target(spec='3rdparty/r1', target_type=GoRemoteLibrary) go_fetch = self.create_task(self.context()) with self.assertRaises(go_fetch.UndeclaredRemoteLibError) as cm: go_fetch._resolve(r1, self.address('3rdparty/r2'), 'r2', implict_ok=False) self.assertEqual(cm.exception.address, self.address('3rdparty/r2'))
def add_new_target(self, address, target_type, target_base=None, dependencies=None, derived_from=None, **kwargs): """Creates a new target, adds it to the context and returns it. This method ensures the target resolves files against the given target_base, creating the directory if needed and registering a source root. """ target_base = os.path.join(get_buildroot(), target_base or address.spec_path) if not os.path.exists(target_base): os.makedirs(target_base) if not SourceRoot.find_by_path(target_base): SourceRoot.register(target_base) if dependencies: dependencies = [dep.address for dep in dependencies] self.build_graph.inject_synthetic_target(address=address, target_type=target_type, dependencies=dependencies, derived_from=derived_from, **kwargs) new_target = self.build_graph.get_target(address) if derived_from: self._synthetic_targets[derived_from].append(new_target) return new_target
def test_resolve_and_inject_implicit(self): SourceRoot.register(os.path.join(self.build_root, '3rdparty'), GoRemoteLibrary) r1 = self.make_target(spec='3rdparty/r1', target_type=GoRemoteLibrary) go_fetch = self.create_task(self.context()) r2 = go_fetch._resolve(r1, self.address('3rdparty/r2'), 'r2', implict_ok=True) self.assertEqual(self.address('3rdparty/r2'), r2.address) self.assertIsInstance(r2, GoRemoteLibrary)
def test_symlink_remote_lib(self): with pushd(self.build_root): with temporary_dir() as d: SourceRoot.register('3rdparty') spec = '3rdparty/github.com/user/lib' remote_lib_src_dir = os.path.join(d, spec) self.create_file(os.path.join(remote_lib_src_dir, 'file.go')) go_remote_lib = self.make_target(spec=spec, target_type=GoRemoteLibrary) context = self.context() go_remote_lib_src = context.products.get_data('go_remote_lib_src', init_func=lambda: defaultdict(str)) go_remote_lib_src[go_remote_lib] = remote_lib_src_dir ws_task = self.create_task(context) gopath = ws_task.get_gopath(go_remote_lib) ws_task._symlink_remote_lib(gopath, go_remote_lib, set()) workspace_dir = os.path.join(gopath, 'src/github.com/user/lib') self.assertTrue(os.path.isdir(workspace_dir)) link = os.path.join(workspace_dir, 'file.go') self.assertEqual(os.readlink(link), os.path.join(remote_lib_src_dir, 'file.go'))
def test_resolve_simple(self): SourceRoot.register('3rdparty/node', NodeRemoteModule) typ = self.make_target(spec='3rdparty/node:typ', target_type=NodeRemoteModule, version='0.6.3') SourceRoot.register('src/node', NodeModule) self.create_file('src/node/util/util.js', contents=dedent(""" var typ = require('typ'); console.log("type of boolean is: " + typ.BOOLEAN); """)) target = self.make_target(spec='src/node/util', target_type=NodeModule, sources=['util.js'], dependencies=[typ]) context = self.context(target_roots=[target]) task = self.create_task(context) task.execute() node_paths = context.products.get_data(NodePaths) node_path = node_paths.node_path(target) self.assertIsNotNone(node_path) script_path = os.path.join(node_path, 'util.js') out = task.node_distribution.node_command(args=[script_path]).check_output() self.assertIn('type of boolean is: boolean', out)
def setUp(self): super(PythonReplTest, self).setUp() SourceRoot.register("3rdparty", PythonRequirementLibrary) SourceRoot.register("src", PythonBinary, PythonLibrary) self.six = self.create_python_requirement_library("3rdparty/six", "six", requirements=["six==1.9.0"]) self.requests = self.create_python_requirement_library( "3rdparty/requests", "requests", requirements=["requests==2.6.0"] ) self.library = self.create_python_library( "src/lib", "lib", { "lib.py": dedent( """ import six def go(): six.print_('go', 'go', 'go!', sep='') """ ) }, dependencies=["//3rdparty/six"], ) self.binary = self.create_python_binary("src/bin", "bin", "lib.go", dependencies=["//src/lib"]) self.non_python_target = self.create_non_python_target("src/java", "java")
def stitch_deps_local(self): SourceRoot.register('src/go', GoBinary, GoLibrary) self.create_file(relpath='src/go/jane/bar.go', contents=dedent(""" package jane var PublicConstant = 42 """)) self.create_file(relpath='src/go/fred/foo.go', contents=dedent(""" package main import ( "fmt" "jane" ) func main() { fmt.Printf("Hello %s!", jane.PublicConstant) } """)) fred = self.make_target('src/go/fred', GoBinary) context = self.context(target_roots=[fred]) self.assertEqual([fred], context.target_roots) pre_execute_files = self.buildroot_files() task = self.create_task(context) task.execute() jane = self.target('src/go/jane') self.assertIsNotNone(jane) self.assertEqual([jane], fred.dependencies) self.assertEqual({jane, fred}, set(context.targets())) return pre_execute_files
def test_transitive_download_remote_libs_complex(self): with temporary_dir() as src: with temporary_dir() as zipdir: SourceRoot.register(os.path.join(self.build_root, '3rdparty'), GoRemoteLibrary) dep_graph = { 'r1': ['r3', 'r4'], 'r2': ['r3'], 'r3': ['r4'], 'r4': [] } self._init_dep_graph_files(src, zipdir, dep_graph) r1 = self.target('3rdparty/r1') r2 = self.target('3rdparty/r2') context = self._create_fetch_context(zipdir) go_fetch = self.create_task(context) undeclared_deps = go_fetch._transitive_download_remote_libs( set([r1, r2])) self.assertEqual(undeclared_deps, {}) self._assert_dependency_graph(r1, dep_graph) self._assert_dependency_graph(r2, dep_graph)
def test_resolve_and_inject_failure(self): go_fetch = self.create_task(self.context()) SourceRoot.register(os.path.join(self.build_root, '3rdparty'), Target) r1 = self.make_target(spec='3rdparty/r1', target_type=Target) with self.assertRaises(go_fetch.UndeclaredRemoteLibError) as cm: go_fetch._resolve_and_inject(r1, 'r2') self.assertEqual(cm.exception.spec_path, '3rdparty/r2')
def test_exported_antlr(self): SourceRoot.register('src/antlr', PythonThriftLibrary) self.create_file(relpath='src/antlr/exported/exported.g', contents=dedent(""" grammar exported; options { language = Python; } WORD: ('a'..'z'|'A'..'Z'|'0'..'9'|'-'|'_')+; static: WORD; """)) target = self.make_target(spec='src/antlr/exported', target_type=PythonAntlrLibrary, antlr_version='3.1.3', sources=['exported.g'], module='exported', provides=PythonArtifact(name='test.exported', version='0.0.0')) # TODO(John Sirois): This hacks around a direct but undeclared dependency # `pants.java.distribution.distribution.Distribution` gained in # https://rbcommons.com/s/twitter/r/2657 # Remove this once proper Subsystem dependency chains are re-established. with subsystem_instance(JVM): with self.run_execute(target) as created: self.assertEqual([target], created.keys())
def test_noop_applicable_targets_complete_graph(self): SourceRoot.register('src/go', GoBinary, GoLibrary) self.create_file(relpath='src/go/jane/bar.go', contents=dedent(""" package jane var PublicConstant = 42 """)) jane = self.make_target('src/go/jane', GoLibrary) self.create_file(relpath='src/go/fred/foo.go', contents=dedent(""" package main import ( "fmt" "jane" ) func main() { fmt.Printf("Hello %s!", jane.PublicConstant) } """)) fred = self.make_target('src/go/fred', GoBinary, dependencies=[jane]) context = self.context(target_roots=[fred]) expected = context.targets() task = self.create_task(context) task.execute() self.assertEqual(expected, context.targets())
def test_protos_extracted_under_build_root(self): """This testcase shows that you can put sources for protos outside the directory where the BUILD file is defined. This will be the case for .proto files that have been extracted under .pants.d. """ # place a .proto file in a place outside of where the BUILD file is defined extracted_source_path = os.path.join(self.build_root, 'extracted-source') SourceRoot.register(extracted_source_path, JavaProtobufLibrary) safe_mkdir(os.path.join(extracted_source_path, 'sample-package')) sample_proto_path = os.path.join(extracted_source_path, 'sample-package', 'sample.proto') with open(sample_proto_path, 'w') as sample_proto: sample_proto.write(dedent(''' package com.example; message sample {} ''')) self.add_to_build_file('sample', dedent(''' java_protobuf_library(name='sample', sources=['{sample_proto_path}'], )''').format(sample_proto_path=sample_proto_path)) target = self.target("sample:sample") context = self.context(target_roots=[target]) task = self.create_task(context=context) sources_by_base = task._calculate_sources([target]) self.assertEquals(['extracted-source'], sources_by_base.keys()) self.assertEquals(OrderedSet([sample_proto_path]), sources_by_base['extracted-source'])
def test_dependees_type(self): SourceRoot.register('tests', PythonTests) self.assert_console_output( 'tests/d:d', targets=[self.target('common/d')], options={'type': ['python_tests']} )
def test_multiple_local_roots_failure(self): SourceRoot.register('src/go', GoBinary) SourceRoot.register('src2/go', GoLibrary) context = self.context(target_roots=[self.make_target('src/go/fred', GoBinary)]) task = self.create_task(context) with self.assertRaises(task.InvalidLocalRootsError): task.execute()
def test_multiple_remote_roots_failure(self): SourceRoot.register('3rdparty/go', GoRemoteLibrary) SourceRoot.register('src/go', GoLibrary, GoRemoteLibrary) context = self.context(target_roots=[self.make_target('src/go/fred', GoLibrary)]) task = self.create_task(context) with self.assertRaises(task.InvalidRemoteRootsError): task.execute()
def setUp(self): super(PythonEvalTest, self).setUp() SourceRoot.register('src', PythonBinary, PythonLibrary) self.a_library = self.create_python_library('src/a', 'a', {'a.py': dedent(""" import inspect def compile_time_check_decorator(cls): if not inspect.isclass(cls): raise TypeError('This decorator can only be applied to classes, given {}'.format(cls)) return cls """)}) self.b_library = self.create_python_library('src/b', 'b', {'b.py': dedent(""" from a.a import compile_time_check_decorator @compile_time_check_decorator class BarB(object): pass """)}) self.b_library = self.create_python_library('src/c', 'c', {'c.py': dedent(""" from a.a import compile_time_check_decorator @compile_time_check_decorator def baz_c(): pass """)}, dependencies=['//src/a']) def fix_c_source(): self.create_file('src/c/c.py', contents=dedent(""" from a.a import compile_time_check_decorator # Change from decorated function baz_c to decorated class BazC. @compile_time_check_decorator class BazC(object): pass """)) self.fix_c_source = fix_c_source self.d_library = self.create_python_library('src/d', 'd', { 'd.py': dedent(""" from a.a import compile_time_check_decorator @compile_time_check_decorator class BazD(object): pass """)}, dependencies=['//src/a']) self.e_binary = self.create_python_binary('src/e', 'e', 'a.a', dependencies=['//src/a']) self.f_binary = self.create_python_binary('src/f', 'f', 'a.a:compile_time_check_decorator', dependencies=['//src/a']) self.g_binary = self.create_python_binary('src/g', 'g', 'a.a:does_not_exist', dependencies=['//src/a']) self.h_binary = self.create_python_binary('src/h', 'h', 'a.a')
def test_exported_thrift_issues_2005(self): # Issue #2005 highlighted the fact the PythonThriftBuilder was building both a given # PythonThriftLibrary's thrift files as well as its transitive dependencies thrift files. # We test here to ensure that independently published PythonThriftLibraries each only own their # own thrift stubs and the proper dependency links exist between the distributions. SourceRoot.register('src/thrift', PythonThriftLibrary) self.create_file(relpath='src/thrift/exported/exported.thrift', contents=dedent(""" namespace py exported const set<string> VALID_IDENTIFIERS = ["Hello", "World", "!"] """)) target1 = self.make_target(spec='src/thrift/exported', target_type=PythonThriftLibrary, sources=['exported.thrift'], provides=PythonArtifact(name='test.exported', version='0.0.0')) self.create_file(relpath='src/thrift/exported_dependee/exported_dependee.thrift', contents=dedent(""" namespace py exported_dependee include "exported/exported.thrift" const set<string> ALIASED_IDENTIFIERS = exported.VALID_IDENTIFIERS """)) target2 = self.make_target(spec='src/thrift/exported_dependee', target_type=PythonThriftLibrary, sources=['exported_dependee.thrift'], dependencies=[target1], provides=PythonArtifact(name='test.exported_dependee', version='0.0.0')) with self.run_execute(target2, recursive=True) as created: self.assertEqual({target2, target1}, set(created.keys())) with self.extracted_sdist(sdist=created[target1], expected_prefix='test.exported-0.0.0') as (py_files, path): self.assertEqual({path('setup.py'), path('src/__init__.py'), path('src/exported/__init__.py'), path('src/exported/constants.py'), path('src/exported/ttypes.py')}, py_files) self.assertFalse(os.path.exists(path('src/test.exported.egg-info/requires.txt'))) with self.extracted_sdist(sdist=created[target2], expected_prefix='test.exported_dependee-0.0.0') as (py_files, path): self.assertEqual({path('setup.py'), path('src/__init__.py'), path('src/exported_dependee/__init__.py'), path('src/exported_dependee/constants.py'), path('src/exported_dependee/ttypes.py')}, py_files) requirements = path('src/test.exported_dependee.egg-info/requires.txt') self.assertTrue(os.path.exists(requirements)) with open(requirements) as fp: self.assertEqual('test.exported==0.0.0', fp.read().strip())
def test_cannot_name(self): SourceRoot.register('src/go', self.target_type) self.add_to_build_file('src/go/src/foo', dedent(""" {target_alias}(name='bob') """.format(target_alias=self.target_type.alias()))) with self.assertRaises(AddressLookupError): self.target('src/go/src/foo')
def test_multiple_local_roots_failure(self): SourceRoot.register('src/go', GoBinary) SourceRoot.register('src2/go', GoLibrary) context = self.context( target_roots=[self.make_target('src/go/fred', GoBinary)]) task = self.create_task(context) with self.assertRaises(task.InvalidLocalRootsError): task.execute()
def test_multiple_remote_roots_failure(self): SourceRoot.register('3rdparty/go', GoRemoteLibrary) SourceRoot.register('src/go', GoLibrary, GoRemoteLibrary) context = self.context( target_roots=[self.make_target('src/go/fred', GoLibrary)]) task = self.create_task(context) with self.assertRaises(task.InvalidRemoteRootsError): task.execute()
def test_cannot_sources(self): SourceRoot.register('3rdparty/go', GoRemoteLibrary) self.add_to_build_file('3rdparty/go/github.com/foo/bar', dedent(""" go_remote_library(dependencies=[]) """)) with self.assertRaises(AddressLookupError): self.target('3rdparty/go/github.com/foo/bar')
def test_reset(self): self._assert_source_root_empty() SourceRoot.register("tests", TestTarget) self.assertEquals({"tests": OrderedSet([TestTarget])}, SourceRoot.all_roots()) SourceRoot.reset() self._assert_source_root_empty()
def test_register(self): self._assert_source_root_empty() SourceRoot.register("tests", TestTarget) self.assertEquals({"tests": OrderedSet([TestTarget])}, SourceRoot.all_roots()) self.assertEquals(OrderedSet([TestTarget]), SourceRoot.types("tests")) self.assertEquals(OrderedSet(["tests"]), SourceRoot.roots(TestTarget))
def test_find(self): # When no source_root is registered, it should just return the path from the address self.assertEqual("tests/foo/bar", SourceRoot.find(TestTarget("//tests/foo/bar:baz"))) SourceRoot.register("tests/foo", TestTarget) # After the source root is registered, you should get the source root self.assertEquals("tests/foo", SourceRoot.find(TestTarget("//tests/foo/bar:baz"))) with self.assertRaises(TargetDefinitionException): SourceRoot.find(NotTestTarget("//tests/foo/foobar:qux"))
def test_resolve_and_inject(self): go_fetch = self.create_task(self.context()) SourceRoot.register(os.path.join(self.build_root, '3rdparty'), Target) r1 = self.make_target(spec='3rdparty/r1', target_type=Target) self.add_to_build_file('3rdparty/r2', 'target(name="{}")'.format('r2')) r2 = go_fetch._resolve_and_inject(r1, 'r2') self.assertEqual(r2.name, 'r2') self.assertItemsEqual(r1.dependencies, [r2])
def test_register_none(self): self._assert_source_root_empty() SourceRoot.register("tests", ) self.assertEquals({"tests": OrderedSet()}, SourceRoot.all_roots()) self.assertEquals(OrderedSet(), SourceRoot.types("tests")) self.assertEquals("tests", SourceRoot.find(TestTarget("//tests/foo/bar:baz"))) self.assertEquals("tests", SourceRoot.find_by_path("tests/foo/bar"))
def test_resolve_and_inject_explicit(self): SourceRoot.register(os.path.join(self.build_root, "3rdparty"), GoRemoteLibrary) r1 = self.make_target(spec="3rdparty/r1", target_type=GoRemoteLibrary) r2 = self.make_target(spec="3rdparty/r2", target_type=GoRemoteLibrary) go_fetch = self.create_task(self.context()) resolved = go_fetch._resolve(r1, self.address("3rdparty/r2"), "r2", implict_ok=False) self.assertEqual(r2, resolved)
def test_register_none(self): self._assert_source_root_empty() SourceRoot.register("tests") self.assertEquals({"tests": OrderedSet()}, SourceRoot.all_roots()) self.assertEquals(OrderedSet(), SourceRoot.types("tests")) self.assertEquals("tests", SourceRoot.find(TestTarget("//tests/foo/bar:baz"))) self.assertEquals("tests", SourceRoot.find_by_path("tests/foo/bar"))
def test_cannot_sources(self): SourceRoot.register('src/go', self.target_type) self.create_file('src/go/src/foo/sub/jane.go') self.add_to_build_file('src/go/src/foo', dedent(""" {target_alias}(sources=['sub/jane.go']) """.format(target_alias=self.target_type.alias()))) with self.assertRaises(AddressLookupError): self.target('src/go/src/foo')
def setUp(self): super(TestPythonThriftBuilder, self).setUp() SourceRoot.register(os.path.realpath(os.path.join(self.build_root, 'test_thrift_replacement')), PythonThriftLibrary) self.add_to_build_file('test_thrift_replacement', dedent(''' python_thrift_library(name='one', sources=['thrift/keyword.thrift'], ) '''))
def test_antlr(self): SourceRoot.register('src/antlr', PythonThriftLibrary) self.create_file(relpath='src/antlr/word/word.g', contents=dedent(""" grammar word; options { language=Python; output=AST; } WORD: ('a'..'z'|'A'..'Z'|'!')+; word_up: WORD (' ' WORD)*; """)) antlr_target = self.make_target(spec='src/antlr/word', target_type=PythonAntlrLibrary, antlr_version='3.1.3', sources=['word.g'], module='word') SourceRoot.register('src/python', PythonBinary) antlr3 = self.make_target(spec='3rdparty/python:antlr3', target_type=PythonRequirementLibrary, requirements=[PythonRequirement('antlr_python_runtime==3.1.3')]) self.create_file(relpath='src/python/test/main.py', contents=dedent(""" import antlr3 from word import wordLexer, wordParser def word_up(): input = 'Hello World!' char_stream = antlr3.ANTLRStringStream(input) lexer = wordLexer.wordLexer(char_stream) tokens = antlr3.CommonTokenStream(lexer) parser = wordParser.wordParser(tokens) def print_node(node): print(node.text) visitor = antlr3.tree.TreeVisitor() visitor.visit(parser.word_up().tree, pre_action=print_node) """)) binary = self.make_target(spec='src/python/test', target_type=PythonBinary, source='main.py', dependencies=[antlr_target, antlr3]) with self.dumped_chroot([binary]) as (pex_builder, python_chroot): pex_builder.set_entry_point('test.main:word_up') pex_builder.freeze() pex = python_chroot.pex() process = pex.run(blocking=False, stdout=subprocess.PIPE) stdout, _ = process.communicate() self.assertEqual(0, process.returncode) self.assertEqual(['Hello', ' ', 'World!'], stdout.splitlines())
def do_test_thrift(self, inspect_chroot=None): SourceRoot.register('src/thrift', PythonThriftLibrary) self.create_file(relpath='src/thrift/core/identifiers.thrift', contents=dedent(""" namespace py core const string HELLO = "Hello" const string WORLD = "World!" """)) core_const = self.make_target(spec='src/thrift/core', target_type=PythonThriftLibrary, sources=['identifiers.thrift']) self.create_file(relpath='src/thrift/test/const.thrift', contents=dedent(""" namespace py test include "core/identifiers.thrift" const list<string> MESSAGE = [identifiers.HELLO, identifiers.WORLD] """)) test_const = self.make_target(spec='src/thrift/test', target_type=PythonThriftLibrary, sources=['const.thrift'], dependencies=[core_const]) SourceRoot.register('src/python', PythonBinary) self.create_file(relpath='src/python/test/main.py', contents=dedent(""" from test.constants import MESSAGE def say_hello(): print(' '.join(MESSAGE)) """)) binary = self.make_target(spec='src/python/test', target_type=PythonBinary, source='main.py', dependencies=[test_const]) yield binary, test_const with self.dumped_chroot([binary]) as (pex_builder, python_chroot): pex_builder.set_entry_point('test.main:say_hello') pex_builder.freeze() pex = python_chroot.pex() process = pex.run(blocking=False, stdout=subprocess.PIPE) stdout, _ = process.communicate() self.assertEqual(0, process.returncode) self.assertEqual('Hello World!', stdout.strip()) if inspect_chroot: inspect_chroot(python_chroot)
def test_resources(self): SourceRoot.register('src/python', PythonLibrary, Resources) self.create_file(relpath='src/python/monster/j-function.res', contents='196884') self.create_file(relpath='src/python/monster/group.res', contents='196883') self.create_file(relpath='src/python/monster/__init__.py', contents='') self.create_file(relpath='src/python/monster/research_programme.py', contents='# Look for more off-by-one "errors"!') # NB: We have to resort to BUILD files on disk here due to the target ownership algorithm in # SetupPy needing to walk ancestors in this case which currently requires BUILD files on disk. self.add_to_build_file( 'src/python/monster', dedent(""" python_library( name='conway', sources=['__init__.py', 'research_programme.py'], resources=['group.res'], resource_targets=[ ':j-function', ], provides=setup_py( name='monstrous.moonshine', version='0.0.0', ) ) resources( name='j-function', sources=['j-function.res'] ) """)) conway = self.target('src/python/monster:conway') with self.run_execute(conway) as created: self.assertEqual([conway], created.keys()) with self.extracted_sdist( sdist=created[conway], expected_prefix='monstrous.moonshine-0.0.0', collect_suffixes=('.py', '.res')) as (py_files, path): self.assertEqual( { path('setup.py'), path('src/monster/__init__.py'), path('src/monster/research_programme.py'), path('src/monster/group.res'), path('src/monster/j-function.res') }, py_files) with open(path('src/monster/group.res')) as fp: self.assertEqual('196883', fp.read()) with open(path('src/monster/j-function.res')) as fp: self.assertEqual('196884', fp.read())
def test_cannot_name(self): SourceRoot.register('src/go', self.target_type) self.add_to_build_file( 'src/go/src/foo', dedent(""" {target_alias}(name='bob') """.format(target_alias=self.target_type.alias()))) with self.assertRaises(AddressLookupError): self.target('src/go/src/foo')
def test_resolve_preserves_package_json(self): SourceRoot.register('src/node', NodeModule) util = self.make_target(spec='src/node/util', target_type=NodeModule, sources=[], dependencies=[]) self.create_file('src/node/scripts_project/package.json', contents=dedent(""" { "name": "scripts_project", "version": "1.2.3", "dependencies": { "A": "file://A" }, "devDependencies": { "B": "file://B" }, "peerDependencies": { "C": "file://C" }, "optionalDependencies": { "D": "file://D" }, "scripts": { "test": "mocha */dist.js" } } """)) scripts_project = self.make_target(spec='src/node/scripts_project', target_type=NodeModule, sources=['package.json'], dependencies=[util]) context = self.context(target_roots=[scripts_project]) task = self.create_task(context) task.execute() node_paths = context.products.get_data(NodePaths) node_path = node_paths.node_path(scripts_project) self.assertIsNotNone(node_paths.node_path(scripts_project)) package_json_path = os.path.join(node_path, 'package.json') with open(package_json_path) as fp: package = json.load(fp) self.assertEqual( 'scripts_project', package['name'], 'Expected to find package name of `scripts_project`, but found: {}' .format(package['name'])) self.assertEqual( '1.2.3', package['version'], 'Expected to find package version of `1.2.3`, but found: {}'. format(package['version'])) self.assertEqual( 'mocha */dist.js', package['scripts']['test'], 'Expected to find package test script of `mocha */dist.js`, but found: {}' .format(package['scripts']['test'])) self.assertEqual(node_paths.node_path(util), package['dependencies']['util']) self.assertNotIn('A', package['dependencies']) self.assertNotIn('devDependencies', package) self.assertNotIn('peerDependencies', package) self.assertNotIn('optionalDependencies', package)
def check_buildroot(self, buildroot_path): self._assert_source_root_empty() SourceRoot.register(buildroot_path, TestTarget) self.assertEquals({".": OrderedSet([TestTarget])}, SourceRoot.all_roots()) self.assertEquals(OrderedSet([TestTarget]), SourceRoot.types(".")) self.assertEquals(OrderedSet(["."]), SourceRoot.roots(TestTarget)) target = TestTarget("//mock/foo/bar:baz") self.assertEqual("", SourceRoot.find(target))
def test_sub_package(self): SourceRoot.register('3rdparty/go', GoRemoteLibrary) self.add_to_build_file('3rdparty/go/github.com/foo/bar', dedent(""" go_remote_library(pkg='baz') """)) go_remote_library = self.target('3rdparty/go/github.com/foo/bar:baz') self.assertIsNotNone(go_remote_library) self.assertEqual('github.com/foo/bar/baz', go_remote_library.import_path) self.assertEqual('github.com/foo/bar', go_remote_library.remote_root) self.assertEqual('baz', go_remote_library.pkg)
def test_cannot_sources(self): SourceRoot.register('src/go', self.target_type) self.create_file('src/go/src/foo/sub/jane.go') self.add_to_build_file( 'src/go/src/foo', dedent(""" {target_alias}(sources=['sub/jane.go']) """.format(target_alias=self.target_type.alias()))) with self.assertRaises(AddressLookupError): self.target('src/go/src/foo')
def test_compiler_wire2_with_writer_errors(self): self._create_fake_wire_tool(version='2.0.0') SourceRoot.register('wire-src') wire_targetv1 = self.make_target( 'wire-src:wire-targetv1', JavaWireLibrary, sources=['bar.proto'], service_writer='org.pantsbuild.DummyServiceWriter', service_writer_options=['opt1', 'opt2']) task = self.create_task(self.context(target_roots=[wire_targetv1])) with self.assertRaises(TaskError): task.format_args_for_target(wire_targetv1)
def test_compiler_wire1_with_factory_errors(self): self._create_fake_wire_tool() SourceRoot.register('wire-src') wire_targetv2 = self.make_target( 'wire-src:wire-targetv2', JavaWireLibrary, sources=['baz.proto'], service_factory='org.pantsbuild.DummyServiceFactory', service_factory_options=['v2opt1', 'v2opt2']) task = self.create_task(self.context(target_roots=[wire_targetv2])) with self.assertRaises(TaskError): task.format_args_for_target(wire_targetv2)
def test_calculate_sources_with_source_root(self): SourceRoot.register('project/src/main/proto') self.add_to_build_file('project/src/main/proto/proto-lib', dedent(''' java_protobuf_library(name='proto-target', sources=['foo.proto'], ) ''')) target = self.target('project/src/main/proto/proto-lib:proto-target') context = self.context(target_roots=[target]) task = self.create_task(context) result = task._calculate_sources([target]) self.assertEquals(1, len(result.keys())) self.assertEquals(OrderedSet(['project/src/main/proto/proto-lib/foo.proto']), result['project/src/main/proto'])
def test_compiler_args(self): self._create_fake_wire_tool() SourceRoot.register('wire-src') simple_wire_target = self.make_target('wire-src:simple-wire-target', JavaWireLibrary, sources=['foo.proto']) context = self.context(target_roots=[simple_wire_target]) task = self.create_task(context) self.assertEquals([ '--java_out={}/{}/wire-src.simple-wire-target'.format( self.build_root, self.EXPECTED_TASK_PATH), '--proto_path={}/wire-src'.format(self.build_root), 'foo.proto' ], task.format_args_for_target(simple_wire_target))
def test_find_by_path(self): # No source_root is registered yet query = "tests/foo/bar:baz" self.assertIsNone(SourceRoot.find_by_path(query), msg="Query {query} Failed for tree: {dump}" .format(query=query, dump=SourceRoot._dump())) SourceRoot.register("tests/foo", TestTarget) self.assertEquals("tests/foo", SourceRoot.find_by_path(query), msg="Query {query} Failed for tree: {dump}" .format(query=query, dump=SourceRoot._dump())) self.assertIsNone(SourceRoot.find_by_path("tests/bar/foobar:qux"), msg="Failed for tree: {dump}" .format(dump=SourceRoot._dump()))
def setUp(self): super(JarCreateExecuteTest, self).setUp() def get_source_root_fs_path(path): return os.path.realpath(os.path.join(self.build_root, path)) SourceRoot.register(get_source_root_fs_path('src/resources'), Resources) SourceRoot.register(get_source_root_fs_path('src/java'), JavaLibrary, JvmBinary) SourceRoot.register(get_source_root_fs_path('src/scala'), ScalaLibrary) SourceRoot.register(get_source_root_fs_path('src/thrift'), JavaThriftLibrary) self.res = self.create_resources('src/resources/com/twitter', 'spam', 'r.txt') self.jl = self.java_library('src/java/com/twitter', 'foo', ['a.java'], resources='src/resources/com/twitter:spam') self.sl = self.scala_library('src/scala/com/twitter', 'bar', ['c.scala']) self.jtl = self.java_thrift_library('src/thrift/com/twitter', 'baz', 'd.thrift') self.java_lib_foo = self.java_library('src/java/com/twitter/foo', 'java_foo', ['java_foo.java']) self.scala_lib = self.scala_library( 'src/scala/com/twitter/foo', 'scala_foo', ['scala_foo.scala'], java_sources=['src/java/com/twitter/foo:java_foo']) self.binary = self.jvm_binary( 'src/java/com/twitter/baz', 'baz', source='b.java', resources='src/resources/com/twitter:spam')
def stitch_deps_remote(self): self.set_options_for_scope(Fetchers.options_scope, mapping={ r'pantsbuild.org/.*': '{}.{}'.format(FakeFetcher.__module__, FakeFetcher.__name__) }) SourceRoot.register('3rdparty/go', GoRemoteLibrary) SourceRoot.register('src/go', GoBinary, GoLibrary) self.create_file(relpath='src/go/jane/bar.go', contents=dedent(""" package jane import "pantsbuild.org/fake/prod" var PublicConstant = prod.DoesNotExistButWeShouldNotCareWhenCheckingDepsAndNotInstalling """)) self.create_file(relpath='src/go/fred/foo.go', contents=dedent(""" package main import ( "fmt" "jane" ) func main() { fmt.Printf("Hello %s!", jane.PublicConstant) } """)) fred = self.make_target('src/go/fred', GoBinary) context = self.context(target_roots=[fred]) self.assertEqual([fred], context.target_roots) pre_execute_files = self.buildroot_files() task = self.create_task(context) task.execute() jane = self.target('src/go/jane') self.assertIsNotNone(jane) self.assertEqual([jane], fred.dependencies) prod = self.target('3rdparty/go/pantsbuild.org/fake:prod') self.assertIsNotNone(prod) self.assertEqual([prod], jane.dependencies) self.assertEqual({prod, jane, fred}, set(context.targets())) return pre_execute_files
def test_exported_thrift(self): SourceRoot.register('src/thrift', PythonThriftLibrary) self.create_file(relpath='src/thrift/exported/exported.thrift', contents=dedent(""" namespace py pants.constants_only const set<string> VALID_IDENTIFIERS = ["Hello", "World", "!"] """)) target = self.make_target(spec='src/thrift/exported', target_type=PythonThriftLibrary, sources=['exported.thrift'], provides=PythonArtifact(name='test.exported', version='0.0.0')) with self.run_execute(target) as created: self.assertEqual([target], created.keys())
def test_resolve_remote(self): SourceRoot.register('3rdparty/node', NodeRemoteModule) typ = self.make_target(spec='3rdparty/node:typ', target_type=NodeRemoteModule, version='0.6.3') context = self.context(target_roots=[typ]) task = self.create_task(context) task.execute() node_paths = context.products.get_data(NodePaths) node_path = node_paths.node_path(typ) self.assertIsNotNone(node_path) script = 'var typ = require("typ"); console.log("type of boolean is: " + typ.BOOLEAN)' out = task.node_distribution.node_command(args=['--eval', script]).check_output(cwd=node_path) self.assertIn('type of boolean is: boolean', out)
def test_default_name_and_sources(self): SourceRoot.register('src/go', self.target_type) self.create_file('src/go/src/foo/jake.go') self.create_file('src/go/src/foo/sub/jane.go') self.add_to_build_file( 'src/go/src/foo', dedent(""" {target_alias}() """.format(target_alias=self.target_type.alias()))) go_local_source_target = self.target('src/go/src/foo') self.assertIsNotNone(go_local_source_target) self.assertEqual('src/foo', go_local_source_target.import_path) self.assertEqual( ['src/foo/jake.go'], list(go_local_source_target.sources_relative_to_source_root()))