Skip to content

Commit

Permalink
Use target not make_target in some tests
Browse files Browse the repository at this point in the history
This pushes parsing of the targets through the engine, rather than
bypassing it.

This is important because I'm about to make these targets require an
EagerFilesetWithSpec as their source/sources arg, rather than being
happy with a list of strings.
  • Loading branch information
illicitonion committed Jun 8, 2018
1 parent 11723e0 commit 8dd5e6d
Show file tree
Hide file tree
Showing 6 changed files with 287 additions and 196 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import bs4
import mock

from pants.backend.docgen.targets.doc import Page
from pants.backend.docgen.register import build_file_aliases
from pants.backend.docgen.tasks import markdown_to_html_utils
from pants.backend.docgen.tasks.markdown_to_html import MarkdownToHtml
from pants.base.exceptions import TaskError
Expand Down Expand Up @@ -105,9 +105,14 @@ class MarkdownToHtmlTest(TaskTestBase):
def task_type(cls):
return MarkdownToHtml

@classmethod
def alias_groups(cls):
return build_file_aliases()

def test_rst_render_empty(self):
self.create_file('empty.rst')
empty_rst = self.make_target(':empty_rst', target_type=Page, source='empty.rst')
self.add_to_build_file('', 'page(name = "empty_rst", source = "empty.rst")')
empty_rst = self.target(':empty_rst')
task = self.create_task(self.context(target_roots=[empty_rst]))
task.execute()

Expand All @@ -117,7 +122,8 @@ def test_rst_render_failure_fail(self):
* `RB #2363 https://rbcommons.com/s/twitter/r/2363/>`_
"""))
bad_rst = self.make_target(':bad_rst', target_type=Page, source='bad.rst')
self.add_to_build_file('', 'page(name = "bad_rst", source = "bad.rst")')
bad_rst = self.target(':bad_rst')
task = self.create_task(self.context(target_roots=[bad_rst]))
with self.assertRaises(TaskError):
task.execute()
Expand All @@ -138,8 +144,9 @@ def test_rst_render_failure_warn(self):
* `RB #2363 https://rbcommons.com/s/twitter/r/2363/>`_
"""))
bad_rst = self.make_target(':bad_rst', target_type=Page, source='bad.rst')
self.add_to_build_file('', 'page(name = "bad_rst", source = "bad.rst")')
self.set_options(ignore_failure=True)
bad_rst = self.target(':bad_rst')
context = self.context(target_roots=[bad_rst])
context.log.warn = mock.Mock()
task = self.create_task(context)
Expand All @@ -163,7 +170,8 @@ def test_rst_render_success(self):
* `RB #2363 <https://rbcommons.com/s/twitter/r/2363/>`_
"""))
good_rst = self.make_target(':good_rst', target_type=Page, source='good.rst')
self.add_to_build_file('', 'page(name = "good_rst", source = "good.rst")')
good_rst = self.target(':good_rst')
context = self.context(target_roots=[good_rst])
task = self.create_task(context)
task.execute()
Expand Down
121 changes: 86 additions & 35 deletions tests/python/pants_test/backend/jvm/targets/test_jvm_binary.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@

import unittest

from pants.backend.jvm.targets.jvm_binary import (Duplicate, JarRules, JvmBinary, ManifestEntries,
Skip)
from pants.backend.jvm.register import build_file_aliases
from pants.backend.jvm.targets.jvm_binary import Duplicate, JarRules, ManifestEntries, Skip
from pants.base.exceptions import TargetDefinitionException
from pants.base.payload_field import FingerprintedField
from pants.build_graph.build_file_aliases import BuildFileAliases
from pants.build_graph.target import Target
from pants.java.jar.exclude import Exclude
from pants.java.jar.jar_dependency import JarDependency
from pants_test.test_base import TestBase


Expand Down Expand Up @@ -57,8 +57,22 @@ def test_set_bad_default(self):

class JvmBinaryTest(TestBase):

@classmethod
def alias_groups(cls):
return build_file_aliases().merge(
BuildFileAliases(
objects={
'duplicate': Duplicate,
},
)
)

def test_simple(self):
target = self.make_target(':foo', JvmBinary, main='com.example.Foo', basename='foo-base')
self.add_to_build_file(
'',
'jvm_binary(name = "foo", main = "com.example.Foo", basename = "foo-base")',
)
target = self.target(':foo')
self.assertEquals('com.example.Foo', target.main)
self.assertEquals('com.example.Foo', target.payload.main)
self.assertEquals('foo-base', target.basename)
Expand All @@ -70,23 +84,34 @@ def test_simple(self):
self.assertEquals({}, target.payload.manifest_entries.entries)

def test_default_base(self):
target = self.make_target(':foo', JvmBinary, main='com.example.Foo')
self.add_to_build_file('', 'jvm_binary(name = "foo", main = "com.example.Foo")')
target = self.target(':foo')
self.assertEquals('foo', target.basename)

def test_deploy_jar_excludes(self):
target = self.make_target(':foo',
JvmBinary,
main='com.example.Foo',
deploy_excludes=[Exclude(org='example.com', name='foo-lib')])
self.add_to_build_file(
'',
'''jvm_binary(
name = "foo",
main = "com.example.Foo",
deploy_excludes=[exclude(org = "example.com", name = "foo-lib")],
)''')
target = self.target(':foo')
self.assertEquals([Exclude(org='example.com', name='foo-lib')],
target.deploy_excludes)

def test_deploy_jar_rules(self):
target = self.make_target(':foo',
JvmBinary,
main='com.example.Foo',
deploy_jar_rules=JarRules([Duplicate('foo', Duplicate.SKIP)],
default_dup_action=Duplicate.FAIL))
self.add_to_build_file(
'',
'''jvm_binary(
name = "foo",
main = "com.example.Foo",
deploy_jar_rules = jar_rules(
[duplicate("foo", duplicate.SKIP)],
default_dup_action = duplicate.FAIL,
),
)''')
target = self.target(':foo')
jar_rules = target.deploy_jar_rules
self.assertEquals(1, len(jar_rules.rules))
self.assertEquals('foo', jar_rules.rules[0].apply_pattern.pattern)
Expand All @@ -95,25 +120,39 @@ def test_deploy_jar_rules(self):
self.assertEquals(Duplicate.FAIL, jar_rules.default_dup_action)

def test_bad_source_declaration(self):
self.add_to_build_file(
'',
'jvm_binary(name = "foo", main = "com.example.Foo", source = ["foo.py"])',
)
with self.assertRaisesRegexp(TargetDefinitionException,
r'Invalid target JvmBinary.*foo.*source must be a single'):
self.make_target(':foo', JvmBinary, main='com.example.Foo', source=['foo.py'])
self.target(':foo')

def test_bad_sources_declaration(self):
self.add_to_build_file(
'foo',
'jvm_binary(name = "foo", main = "com.example.Foo", sources = ["foo.py"])',
)
with self.assertRaisesRegexp(Target.IllegalArgument,
r'jvm_binary only supports a single "source" argument'):
self.make_target('foo:foo', target_type=JvmBinary, main='com.example.Foo', sources=['foo.py'])
self.target('foo:foo')

def test_bad_main_declaration(self):
self.add_to_build_file('', 'jvm_binary(name = "bar", main = ["com.example.Bar"])')
with self.assertRaisesRegexp(TargetDefinitionException,
r'Invalid target JvmBinary.*bar.*main must be a fully'):
self.make_target(':bar', JvmBinary, main=['com.example.Bar'])
self.target(':bar')

def test_bad_jar_rules(self):
self.add_to_build_file(
'',
'jvm_binary(name = "foo", main = "com.example.Foo", deploy_jar_rules="invalid")',
)
with self.assertRaisesRegexp(TargetDefinitionException,
r'Invalid target JvmBinary.*foo.*'
r'deploy_jar_rules must be a JarRules specification. got unicode'):
self.make_target(':foo', JvmBinary, main='com.example.Foo', deploy_jar_rules='invalid')
r'deploy_jar_rules must be a JarRules specification. '
r'got (str|unicode)'):
self.target(':foo')

def _assert_fingerprints_not_equal(self, fields):
for field in fields:
Expand Down Expand Up @@ -144,31 +183,43 @@ def test_jar_rules_field(self):
self._assert_fingerprints_not_equal([field1, field2, field3, field4, field5, field6, field7])

def test_manifest_entries(self):
target = self.make_target(':foo',
JvmBinary,
main='com.example.Foo',
manifest_entries={'Foo-Field': 'foo'})
self.add_to_build_file(
'',
'''jvm_binary(
name = "foo",
main = "com.example.Foo",
manifest_entries = {"Foo-Field": "foo"},
)''')
target = self.target(":foo")
self.assertTrue(isinstance(target.payload.manifest_entries, ManifestEntries))
entries = target.payload.manifest_entries.entries
self.assertEquals({'Foo-Field': 'foo'}, entries)

def test_manifest_not_dict(self):
self.add_to_build_file(
'',
'''jvm_binary(
name = "foo",
main = "com.example.Foo",
manifest_entries = "foo",
)''')
with self.assertRaisesRegexp(TargetDefinitionException,
r'Invalid target JvmBinary.*foo.*: manifest_entries must be a '
r'dict. got unicode'):
self.make_target(':foo',
JvmBinary,
main='com.example.Foo',
manifest_entries='foo')
r'dict. got (str|unicode)'):
self.target(':foo')

def test_manifest_bad_key(self):
with self.assertRaisesRegexp(ManifestEntries.ExpectedDictionaryError,
r'^entries must be dictionary of strings, got key .* '
r'type JarDependency$'):
self.make_target(':foo',
JvmBinary,
main='com.example.Foo',
manifest_entries={JarDependency('bad', 'bad', 'bad'): 'foo'})
self.add_to_build_file(
'',
'''jvm_binary(
name = "foo",
main = "com.example.Foo",
manifest_entries = {jar("bad", "bad", "bad"): "foo"},
)''')
with self.assertRaisesRegexp(TargetDefinitionException,
r'entries must be dictionary of strings, got key .* '
r'type JarDependency'):
self.target(':foo')

def test_manifest_entries_fingerprint(self):
field1 = ManifestEntries()
Expand Down
34 changes: 19 additions & 15 deletions tests/python/pants_test/backend/jvm/tasks/test_binary_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@

import os

from pants.backend.jvm.targets.jar_library import JarLibrary
from pants.backend.jvm.targets.jvm_binary import JvmBinary
from pants.backend.jvm.tasks.binary_create import BinaryCreate
from pants.java.jar.exclude import Exclude
from pants.java.jar.jar_dependency import JarDependency
from pants.util.contextutil import open_zip
from pants_test.backend.jvm.tasks.jvm_binary_task_test_base import JvmBinaryTaskTestBase

Expand All @@ -23,9 +19,8 @@ def task_type(cls):
return BinaryCreate

def test_jvm_binaries_products(self):
binary_target = self.make_target(spec='//bar:bar-binary',
target_type=JvmBinary,
source='Bar.java')
self.add_to_build_file('bar', 'jvm_binary(name = "bar-binary", source = "Bar.java")')
binary_target = self.target('//bar:bar-binary')
context = self.context(target_roots=[binary_target])
classpath_products = self.ensure_classpath_products(context)

Expand Down Expand Up @@ -56,17 +51,26 @@ def test_jvm_binaries_products(self):
sorted(jar.namelist()))

def test_jvm_binaries_deploy_excludes(self):
foo_jar_lib = self.make_target(spec='3rdparty/jvm/org/example:foo',
target_type=JarLibrary,
jars=[JarDependency(org='org.example', name='foo', rev='1.0.0')])
binary_target = self.make_target(spec='//bar:bar-binary',
target_type=JvmBinary,
source='Bar.java',
dependencies=[foo_jar_lib],
deploy_excludes=[Exclude(org='org.pantsbuild')])
self.add_to_build_file(
'3rdparty/jvm/org/example',
'jar_library(name = "foo", jars = [jar(org = "org.example", name = "foo", rev = "1.0.0")])',
)
foo_jar_lib = self.target('3rdparty/jvm/org/example:foo')

self.add_to_build_file(
'bar',
'''jvm_binary(
name = "bar-binary",
source = "Bar.java",
dependencies = ["3rdparty/jvm/org/example:foo"],
deploy_excludes = [exclude(org = "org.pantsbuild")],
)'''
)
binary_target = self.target('//bar:bar-binary')
context = self.context(target_roots=[binary_target])
classpath_products = self.ensure_classpath_products(context)


foo_artifact = self.create_artifact(org='org.example', name='foo', rev='1.0.0')
with open_zip(foo_artifact.pants_path, 'w') as jar:
jar.writestr('foo/Foo.class', '')
Expand Down
Loading

0 comments on commit 8dd5e6d

Please sign in to comment.