-
-
Notifications
You must be signed in to change notification settings - Fork 636
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3d22e95
commit a962fb6
Showing
12 changed files
with
344 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
python_library() |
Empty file.
32 changes: 32 additions & 0 deletions
32
src/python/pants/backend/python/rules/python_test_runner.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# coding=utf-8 | ||
# Copyright 2018 Pants project contributors (see CONTRIBUTORS.md). | ||
# Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
from __future__ import absolute_import, division, print_function, unicode_literals | ||
|
||
from builtins import str | ||
|
||
from pants.engine.legacy.graph import HydratedTarget | ||
from pants.engine.rules import rule | ||
from pants.engine.selectors import Select | ||
from pants.rules.core.core_test_model import Status, TestResult | ||
|
||
|
||
# This class currently exists so that other rules could be added which turned a HydratedTarget into | ||
# a language-specific test result, and could be installed alongside run_python_test. | ||
# Hopefully https://github.com/pantsbuild/pants/issues/4535 should help resolve this. | ||
class PyTestResult(TestResult): | ||
pass | ||
|
||
|
||
@rule(PyTestResult, [Select(HydratedTarget)]) | ||
def run_python_test(target): | ||
# TODO: Actually run tests (https://github.com/pantsbuild/pants/issues/6003) | ||
|
||
if 'fail' in target.address.reference(): | ||
noun = 'failed' | ||
status = Status.FAILURE | ||
else: | ||
noun = 'passed' | ||
status = Status.SUCCESS | ||
return PyTestResult(status=status, stdout=str('I am a python test which {}'.format(noun))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# coding=utf-8 | ||
# Copyright 2018 Pants project contributors (see CONTRIBUTORS.md). | ||
# Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
from __future__ import absolute_import, division, print_function, unicode_literals | ||
|
||
from builtins import str | ||
|
||
from pants.util.objects import datatype | ||
|
||
|
||
class Status(object): | ||
SUCCESS = str('SUCCESS') | ||
FAILURE = str('FAILURE') | ||
|
||
|
||
# This would be called TestResult, but then PyTest outputs a warning that it detected TestResult | ||
# was a test class, but couldn't treat it like a test class, because it has a constructor. | ||
class TestResult(datatype([ | ||
# One of the Status pseudo-enum values capturing whether the run was successful. | ||
('status', str), | ||
# The stdout of the test runner (which may or may not include actual testcase output). | ||
('stdout', str) | ||
])): | ||
# Prevent this class from being detected by pytest as a test class. | ||
__test__ = False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# coding=utf-8 | ||
# Copyright 2018 Pants project contributors (see CONTRIBUTORS.md). | ||
# Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
from __future__ import absolute_import, division, print_function, unicode_literals | ||
|
||
from builtins import str | ||
|
||
from pants.backend.python.rules.python_test_runner import PyTestResult | ||
from pants.build_graph.address import Address | ||
from pants.engine.addressable import BuildFileAddresses | ||
from pants.engine.console import Console | ||
from pants.engine.legacy.graph import HydratedTarget | ||
from pants.engine.rules import console_rule, rule | ||
from pants.engine.selectors import Get, Select | ||
from pants.rules.core.core_test_model import Status, TestResult | ||
from pants.rules.core.exceptions import GracefulTerminationException | ||
|
||
|
||
@console_rule('test', [Select(Console), Select(BuildFileAddresses)]) | ||
def fast_test(console, addresses): | ||
test_results = yield [Get(TestResult, Address, address.to_address()) for address in addresses] | ||
wrote_any_stdout = False | ||
did_any_fail = False | ||
for test_result in test_results: | ||
wrote_any_stdout |= bool(test_result.stdout) | ||
# Assume \n-terminated | ||
console.write_stdout(test_result.stdout) | ||
if test_result.stdout and not test_result.stdout[-1] == '\n': | ||
console.write_stdout(str('\n')) | ||
if test_result.status == Status.FAILURE: | ||
did_any_fail = True | ||
|
||
if wrote_any_stdout: | ||
console.write_stdout(str('\n')) | ||
|
||
for address, test_result in zip(addresses, test_results): | ||
console.print_stdout(str('{0:80}.....{1:>10}'.format(address.reference(), test_result.status))) | ||
|
||
if did_any_fail: | ||
raise GracefulTerminationException("Tests failed", exit_code=1) | ||
|
||
|
||
@rule(TestResult, [Select(HydratedTarget)]) | ||
def coordinator_of_tests(target): | ||
# This should do an instance match, or canonicalise the adaptor type, or something | ||
#if isinstance(target.adaptor, PythonTestsAdaptor): | ||
# See https://github.com/pantsbuild/pants/issues/4535 | ||
if target.adaptor.type_alias == 'python_tests': | ||
result = yield Get(PyTestResult, HydratedTarget, target) | ||
yield TestResult(status=result.status, stdout=result.stdout) | ||
else: | ||
raise Exception("Didn't know how to run tests for type {}".format(target.adaptor.type_alias)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
python_tests( | ||
name='test', | ||
sources=['test_test.py'], | ||
dependencies=[ | ||
'3rdparty/python:future', | ||
'src/python/pants/backend/python/rules', | ||
'src/python/pants/util:objects', | ||
'tests/python/pants_test:test_base', | ||
'tests/python/pants_test/engine:scheduler_test_base', | ||
'tests/python/pants_test/engine/examples:scheduler_inputs', | ||
] | ||
) | ||
|
||
python_tests( | ||
name='test_integration', | ||
sources=['test_test_integration.py'], | ||
dependencies=[ | ||
'tests/python/pants_test:int-test', | ||
], | ||
tags={'integration'}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
# coding=utf-8 | ||
# Copyright 2018 Pants project contributors (see CONTRIBUTORS.md). | ||
# Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
from __future__ import absolute_import, division, print_function, unicode_literals | ||
|
||
from builtins import str | ||
|
||
from pants.backend.python.rules.python_test_runner import PyTestResult | ||
from pants.build_graph.address import Address, BuildFileAddress | ||
from pants.engine.legacy.graph import HydratedTarget | ||
from pants.engine.legacy.structs import PythonTestsAdaptor | ||
from pants.rules.core.exceptions import GracefulTerminationException | ||
from pants.rules.core.test import Status, TestResult, coordinator_of_tests, fast_test | ||
from pants.util.meta import AbstractClass | ||
from pants_test.engine.scheduler_test_base import SchedulerTestBase | ||
from pants_test.engine.util import MockConsole, run_rule | ||
from pants_test.test_base import TestBase | ||
|
||
|
||
class TestTest(TestBase, SchedulerTestBase, AbstractClass): | ||
def single_target_test(self, result, expected_console_output): | ||
console = MockConsole() | ||
|
||
run_rule(fast_test, console, (self.make_build_target_address("some/target"),), { | ||
(TestResult, Address): lambda _: result, | ||
}) | ||
|
||
self.assertEquals(console.stdout.getvalue(), expected_console_output) | ||
|
||
def make_build_target_address(self, spec): | ||
address = Address.parse(spec) | ||
return BuildFileAddress( | ||
build_file=None, | ||
target_name=address.target_name, | ||
rel_path='{}/BUILD'.format(address.spec_path), | ||
) | ||
|
||
def test_outputs_success(self): | ||
self.single_target_test( | ||
TestResult(status=Status.SUCCESS, stdout=str('Here is some output from a test')), | ||
"""Here is some output from a test | ||
some/target ..... SUCCESS | ||
""" | ||
) | ||
|
||
def test_output_failure(self): | ||
with self.assertRaises(GracefulTerminationException) as cm: | ||
self.single_target_test( | ||
TestResult(status=Status.FAILURE, stdout=str('Here is some output from a test')), | ||
"""Here is some output from a test | ||
some/target ..... FAILURE | ||
""" | ||
) | ||
self.assertEqual(1, cm.exception.exit_code) | ||
|
||
def test_output_no_trailing_newline(self): | ||
self.single_target_test( | ||
TestResult(status=Status.SUCCESS, stdout=str('Here is some output from a test')), | ||
"""Here is some output from a test | ||
some/target ..... SUCCESS | ||
""" | ||
) | ||
|
||
def test_output_training_newline(self): | ||
self.single_target_test( | ||
TestResult(status=Status.SUCCESS, stdout=str('Here is some output from a test\n')), | ||
"""Here is some output from a test | ||
some/target ..... SUCCESS | ||
""" | ||
) | ||
|
||
def test_output_mixed(self): | ||
console = MockConsole() | ||
target1 = self.make_build_target_address("testprojects/tests/python/pants/passes") | ||
target2 = self.make_build_target_address("testprojects/tests/python/pants/fails") | ||
|
||
def make_result(target): | ||
if target == target1: | ||
return TestResult(status=Status.SUCCESS, stdout=str('I passed')) | ||
elif target == target2: | ||
return TestResult(status=Status.FAILURE, stdout=str('I failed')) | ||
else: | ||
raise Exception("Unrecognised target") | ||
|
||
with self.assertRaises(GracefulTerminationException) as cm: | ||
run_rule(fast_test, console, (target1, target2), { | ||
(TestResult, Address): make_result, | ||
}) | ||
|
||
self.assertEqual(1, cm.exception.exit_code) | ||
self.assertEquals(console.stdout.getvalue(), """I passed | ||
I failed | ||
testprojects/tests/python/pants/passes ..... SUCCESS | ||
testprojects/tests/python/pants/fails ..... FAILURE | ||
""") | ||
|
||
def test_coordinator_python_test(self): | ||
target_adaptor = PythonTestsAdaptor(type_alias='python_tests') | ||
|
||
result = run_rule(coordinator_of_tests, HydratedTarget(Address.parse("some/target"), target_adaptor, ()), { | ||
(PyTestResult, HydratedTarget): lambda _: PyTestResult(status=Status.FAILURE, stdout=str('foo')), | ||
}) | ||
|
||
self.assertEqual(result, TestResult(status=Status.FAILURE, stdout=str('foo'))) | ||
|
||
def test_coordinator_unknown_test(self): | ||
target_adaptor = PythonTestsAdaptor(type_alias='unknown_tests') | ||
|
||
with self.assertRaises(Exception) as cm: | ||
run_rule(coordinator_of_tests, HydratedTarget(Address.parse("some/target"), target_adaptor, ()), { | ||
(PyTestResult, HydratedTarget): lambda _: PyTestResult(status=Status.FAILURE, stdout=str('foo')), | ||
}) | ||
|
||
self.assertEqual(str(cm.exception), "Didn't know how to run tests for type unknown_tests") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# coding=utf-8 | ||
# Copyright 2018 Pants project contributors (see CONTRIBUTORS.md). | ||
# Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
from __future__ import absolute_import, division, print_function, unicode_literals | ||
|
||
from pants_test.pants_run_integration_test import PantsRunIntegrationTest | ||
|
||
|
||
class TestIntegrationTest(PantsRunIntegrationTest): | ||
|
||
def test_passing_python_test(self): | ||
args = [ | ||
'--no-v1', | ||
'--v2', | ||
'test', | ||
'testprojects/tests/python/pants/dummies:passing_target', | ||
] | ||
pants_run = self.run_pants(args) | ||
self.assert_success(pants_run) | ||
self.assertEqual("""I am a python test which passed | ||
testprojects/tests/python/pants/dummies:passing_target ..... SUCCESS | ||
""", pants_run.stdout_data) | ||
self.assertEqual("", pants_run.stderr_data) | ||
self.assertEqual(pants_run.returncode, 0) | ||
|
||
def test_failing_python_test(self): | ||
args = [ | ||
'--no-v1', | ||
'--v2', | ||
'test', | ||
'testprojects/tests/python/pants/dummies:failing_target', | ||
] | ||
pants_run = self.run_pants(args) | ||
self.assert_failure(pants_run) | ||
self.assertEqual("""I am a python test which failed | ||
testprojects/tests/python/pants/dummies:failing_target ..... FAILURE | ||
""", pants_run.stdout_data) | ||
self.assertEqual("", pants_run.stderr_data) | ||
self.assertEqual(pants_run.returncode, 1) | ||
|
||
def test_mixed_python_tests(self): | ||
args = [ | ||
'--no-v1', | ||
'--v2', | ||
'test', | ||
'testprojects/tests/python/pants/dummies:failing_target', | ||
'testprojects/tests/python/pants/dummies:passing_target', | ||
] | ||
pants_run = self.run_pants(args) | ||
self.assert_failure(pants_run) | ||
self.assertEqual("""I am a python test which failed | ||
I am a python test which passed | ||
testprojects/tests/python/pants/dummies:failing_target ..... FAILURE | ||
testprojects/tests/python/pants/dummies:passing_target ..... SUCCESS | ||
""", pants_run.stdout_data) | ||
self.assertEqual("", pants_run.stderr_data) | ||
self.assertEqual(pants_run.returncode, 1) |