Skip to content

Commit

Permalink
Don't exit the JUnitRunner with number of failures because Java will …
Browse files Browse the repository at this point in the history
…mod the exit code. (pantsbuild#4106)

### Problem

If you have a test with 256 failures it will return success

### Solution

Don't System.exit(failureCount)
  • Loading branch information
cheister authored and Stu Hood committed Dec 2, 2016
1 parent 3af3144 commit 5423eee
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ void run(Collection<String> tests) {
createUnexpectedExitHook(shutdownListener, swappableOut.getOriginal());
Runtime.getRuntime().addShutdownHook(unexpectedExitHook);

int failures = 0;
int failures = 1;
try {
Collection<Spec> parsedTests = new SpecParser(tests).parse();
if (useExperimentalRunner) {
Expand All @@ -458,17 +458,15 @@ void run(Collection<String> tests) {
failures = runLegacy(parsedTests, core);
}
} catch (SpecException e) {
failures = 1;
swappableErr.getOriginal().println("Error parsing specs: " + e.getMessage());
} catch (InitializationError e) {
failures = 1;
swappableErr.getOriginal().println("Error initializing JUnit: " + e.getMessage());
} finally {
// If we're exiting via a thrown exception, we'll get a better message by letting it
// propagate than by halt()ing.
Runtime.getRuntime().removeShutdownHook(unexpectedExitHook);
}
exit(failures);
exit(failures == 0 ? 0 : 1);
}

/**
Expand Down
10 changes: 10 additions & 0 deletions testprojects/tests/java/org/pantsbuild/testproject/fail256/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copyright 2016 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).


junit_tests(name='fail256',
sources=globs('*'),
dependencies=[
'3rdparty:junit'
],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2016 Pants project contributors (see CONTRIBUTORS.md).
// Licensed under the Apache License, Version 2.0 (see LICENSE).

package org.pantsbuild.testproject.testrule;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;

@RunWith(Parameterized.class)
public class Fail256Test {

@Parameters
public static Object[] data() {
Object[] parameters = new Object[256];
for (int i = 0; i < 256; i++) {
parameters[i] = i;
}
return parameters;
}

@Parameter
public int input;

@Test
public void testFails() {
Assert.assertTrue(input < 0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,14 @@ def test_junit_test_annotation_processor(self):
'testprojects/tests/java/org/pantsbuild/testproject/annotation'])
self.assert_success(pants_run)

# Uncomment this test after the junit_runner is updated with exit code fix
# def test_junit_test_256_failures(self):
# pants_run = self.run_pants([
# 'test',
# 'testprojects/tests/java/org/pantsbuild/testproject/fail256'])
# self.assert_failure(pants_run)
# self.assertIn('Failures: 256', pants_run.stdout_data)
#
def test_junit_test_duplicate_resources(self):
pants_run = self.run_pants([
'test',
Expand Down

0 comments on commit 5423eee

Please sign in to comment.