Skip to content

Commit

Permalink
Add formatting script (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevebarrau authored Apr 28, 2022
1 parent 5b7999d commit 2cb8467
Show file tree
Hide file tree
Showing 29 changed files with 525 additions and 298 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/workspace_snippet.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/usr/bin/env bash

set -o errexit -o nounset -o pipefail
set -eufo pipefail

# Set by GH actions, see
# https://docs.github.com/en/actions/learn-github-actions/environment-variables#default-environment-variables
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ want to force users to remember to load our own dependencies for
us. Instead, to add a new dependency to the project:

1. Update `frozen_deps` in the `WORKSPACE` file
2. Run `./bin/freeze-deps.py`
2. Run `./tools/update-dependencies.sh`
3. Commit the updated files.

### Freezing your dependencies
Expand Down Expand Up @@ -522,7 +522,7 @@ file you need to do the following:

pinned_maven_install()
```
2. Run `./bin/frozen-deps.py --repo <repo name> --zip
2. Run `./tools/frozen-deps.py --repo <repo name> --zip
<path/to/dependency.zip>`. The `<repo name>` matches the name used
for the `maven_install()` rule above. This will pin the
dependencies then collect them into the zip file.
Expand Down
2 changes: 2 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ pinned_maven_install()
# These are used for our own tests.
maven_install(
artifacts = [
"com.google.googlejavaformat:google-java-format:1.15.0",

# These can be versioned independently of the versions in `repositories.bzl`
# so long as the version numbers are higher.
"org.junit.jupiter:junit-jupiter-engine:5.8.2",
Expand Down
4 changes: 2 additions & 2 deletions docs/postfix.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ want to force users to remember to load our own dependencies for
us. Instead, to add a new dependency to the project:

1. Update `frozen_deps` in the `WORKSPACE` file
2. Run `./bin/freeze-deps.py`
2. Run `./tools/update-dependencies.sh`
3. Commit the updated files.

### Freezing your dependencies
Expand Down Expand Up @@ -40,7 +40,7 @@ file you need to do the following:

pinned_maven_install()
```
2. Run `./bin/frozen-deps.py --repo <repo name> --zip
2. Run `./tools/frozen-deps.py --repo <repo name> --zip
<path/to/dependency.zip>`. The `<repo name>` matches the name used
for the `maven_install()` rule above. This will pin the
dependencies then collect them into the zip file.
Expand Down
2 changes: 1 addition & 1 deletion java/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ checkstyle_binary(
name = "checkstyle_cli",
runtime_deps = [
artifact("com.puppycrawl.tools:checkstyle"),
]
],
)

pmd_binary(
Expand Down
Binary file modified java/private/contrib_rules_jvm_deps.zip
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package com.github.bazel_contrib.contrib_rules_jvm.junit5;

import org.junit.platform.engine.discovery.DiscoverySelectors;
import org.junit.platform.launcher.LauncherConstants;
import org.junit.platform.launcher.core.LauncherConfig;
import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder;
import org.junit.platform.launcher.core.LauncherFactory;
import static java.nio.file.StandardOpenOption.DELETE_ON_CLOSE;
import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING;
import static java.nio.file.StandardOpenOption.WRITE;
import static org.junit.platform.launcher.EngineFilter.includeEngines;

import java.io.File;
import java.io.IOException;
Expand All @@ -14,11 +13,11 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

import static java.nio.file.StandardOpenOption.DELETE_ON_CLOSE;
import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING;
import static java.nio.file.StandardOpenOption.WRITE;
import static org.junit.platform.launcher.EngineFilter.includeEngines;
import org.junit.platform.engine.discovery.DiscoverySelectors;
import org.junit.platform.launcher.LauncherConstants;
import org.junit.platform.launcher.core.LauncherConfig;
import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder;
import org.junit.platform.launcher.core.LauncherFactory;

public class ActualRunner implements RunsTest {

Expand All @@ -36,31 +35,30 @@ public boolean run(String testClassName) {
try (var bazelJunitXml = new BazelJUnitOutputListener(xmlOut)) {
var summary = new CommandLineSummary();

LauncherConfig config = LauncherConfig.builder()
.addTestExecutionListeners(bazelJunitXml, summary)
.build();
LauncherConfig config =
LauncherConfig.builder().addTestExecutionListeners(bazelJunitXml, summary).build();

var classSelector = DiscoverySelectors.selectClass(testClassName);

var request = LauncherDiscoveryRequestBuilder.request()
.selectors(List.of(classSelector))
.filters(includeEngines(
"junit-jupiter",
"junit-vintage"))
.configurationParameter(LauncherConstants.CAPTURE_STDERR_PROPERTY_NAME, "true")
.configurationParameter(LauncherConstants.CAPTURE_STDOUT_PROPERTY_NAME, "true");
var request =
LauncherDiscoveryRequestBuilder.request()
.selectors(List.of(classSelector))
.filters(includeEngines("junit-jupiter", "junit-vintage"))
.configurationParameter(LauncherConstants.CAPTURE_STDERR_PROPERTY_NAME, "true")
.configurationParameter(LauncherConstants.CAPTURE_STDOUT_PROPERTY_NAME, "true");

String filter = System.getenv("TESTBRIDGE_TEST_ONLY");
request.filters(new PatternFilter(filter));

File exitFile = getExitFile();
var originalSecurityManager = System.getSecurityManager();
TestRunningSecurityManager testSecurityManager = new TestRunningSecurityManager(originalSecurityManager);
TestRunningSecurityManager testSecurityManager =
new TestRunningSecurityManager(originalSecurityManager);
try {
System.setSecurityManager(testSecurityManager);
var launcher = LauncherFactory.create(config);
launcher.execute(request.build());
} finally {
} finally {
testSecurityManager.allowRemoval();
System.setSecurityManager(originalSecurityManager);
}
Expand Down Expand Up @@ -97,6 +95,6 @@ private void deleteExitFile(File exitFile) {
} catch (Throwable t) {
// Ignore.
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package com.github.bazel_contrib.contrib_rules_jvm.junit5;

import org.junit.platform.engine.TestExecutionResult;
import org.junit.platform.engine.reporting.ReportEntry;
import org.junit.platform.launcher.TestIdentifier;

import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
import java.time.Duration;
import java.time.Instant;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
import org.junit.platform.engine.TestExecutionResult;
import org.junit.platform.engine.reporting.ReportEntry;
import org.junit.platform.launcher.TestIdentifier;

abstract class BaseResult {
private final TestIdentifier testId;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
package com.github.bazel_contrib.contrib_rules_jvm.junit5;

import org.junit.platform.engine.TestExecutionResult;
import org.junit.platform.engine.reporting.ReportEntry;
import org.junit.platform.launcher.TestExecutionListener;
import org.junit.platform.launcher.TestIdentifier;
import org.junit.platform.launcher.TestPlan;
import static java.nio.charset.StandardCharsets.UTF_8;

import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
import java.io.BufferedWriter;
import java.io.Closeable;
import java.io.IOException;
Expand All @@ -19,8 +12,14 @@
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;

import static java.nio.charset.StandardCharsets.UTF_8;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
import org.junit.platform.engine.TestExecutionResult;
import org.junit.platform.engine.reporting.ReportEntry;
import org.junit.platform.launcher.TestExecutionListener;
import org.junit.platform.launcher.TestIdentifier;
import org.junit.platform.launcher.TestPlan;

public class BazelJUnitOutputListener implements TestExecutionListener, Closeable {

Expand Down Expand Up @@ -48,9 +47,10 @@ public void testPlanExecutionStarted(TestPlan testPlan) {
throw new IllegalStateException("Test plan is currently executing");
}

roots = testPlan.getRoots().stream()
.map(root -> new RootContainer(root, testPlan))
.collect(Collectors.toCollection(LinkedHashSet::new));
roots =
testPlan.getRoots().stream()
.map(root -> new RootContainer(root, testPlan))
.collect(Collectors.toCollection(LinkedHashSet::new));
}

@Override
Expand Down Expand Up @@ -81,7 +81,8 @@ public void executionSkipped(TestIdentifier testIdentifier, String reason) {
}

@Override
public void executionFinished(TestIdentifier testIdentifier, TestExecutionResult testExecutionResult) {
public void executionFinished(
TestIdentifier testIdentifier, TestExecutionResult testExecutionResult) {
roots.forEach(root -> root.markFinished(testIdentifier, testExecutionResult));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
package com.github.bazel_contrib.contrib_rules_jvm.junit5;

import org.junit.platform.engine.TestExecutionResult;
import org.junit.platform.launcher.TestExecutionListener;
import org.junit.platform.launcher.TestIdentifier;
import org.junit.platform.launcher.TestPlan;
import org.junit.platform.reporting.legacy.LegacyReportingUtils;
import static org.junit.platform.engine.TestExecutionResult.Status.SUCCESSFUL;

import java.io.PrintWriter;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;

import static org.junit.platform.engine.TestExecutionResult.Status.SUCCESSFUL;
import org.junit.platform.engine.TestExecutionResult;
import org.junit.platform.launcher.TestExecutionListener;
import org.junit.platform.launcher.TestIdentifier;
import org.junit.platform.launcher.TestPlan;
import org.junit.platform.reporting.legacy.LegacyReportingUtils;

public class CommandLineSummary implements TestExecutionListener {

private final Map<TestIdentifier, Failure> failures = Collections.synchronizedMap(new LinkedHashMap<>());
private final Map<TestIdentifier, Failure> failures =
Collections.synchronizedMap(new LinkedHashMap<>());
private TestPlan testPlan;

@Override
Expand All @@ -43,11 +43,7 @@ public void writeTo(PrintWriter writer) {

String className = LegacyReportingUtils.getClassName(testPlan, entry.getKey());

writer.printf(
"%d) %s (%s)%n",
count,
entry.getKey().getDisplayName(),
className);
writer.printf("%d) %s (%s)%n", count, entry.getKey().getDisplayName(), className);

Throwable cause = failure.getCause();
StackTraceElement[] stackTrace = cause.getStackTrace();
Expand Down Expand Up @@ -76,8 +72,7 @@ public int getFailureCount() {
private static class Failure {
private TestExecutionResult result;

public Failure() {
}
public Failure() {}

public void setResult(TestExecutionResult result) {
this.result = result;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package com.github.bazel_contrib.contrib_rules_jvm.junit5;

import org.junit.platform.engine.reporting.ReportEntry;

import java.util.Map;

import static org.junit.platform.launcher.LauncherConstants.STDERR_REPORT_ENTRY_KEY;
import static org.junit.platform.launcher.LauncherConstants.STDOUT_REPORT_ENTRY_KEY;

import java.util.Map;
import org.junit.platform.engine.reporting.ReportEntry;

class EntryDetails {

private EntryDetails() {
Expand All @@ -23,10 +22,9 @@ public static String getStdErr(ReportEntry entry) {

private static String getReportEntryValue(ReportEntry entry, String key) {
return entry.getKeyValuePairs().entrySet().stream()
.filter(e -> key.equals(e.getKey()))
.map(Map.Entry::getValue)
.findFirst()
.orElse(null);
.filter(e -> key.equals(e.getKey()))
.map(Map.Entry::getValue)
.findFirst()
.orElse(null);
}

}
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package com.github.bazel_contrib.contrib_rules_jvm.junit5;

/**
* Test bootstrapper. This class only depends on the JRE (java 11+) and will
* ensure that the required dependencies for a junit5 test are on the
* classpath before creating the actual runner. In this way we can offer a
* useful error message to people, which is always nice, right?
* <p>
* Most of the configuration information can be found on this page in the
* <a href="https://docs.bazel.build/versions/master/test-encyclopedia.html">
* Test Encyclopedia</a>.
* Test bootstrapper. This class only depends on the JRE (java 11+) and will ensure that the
* required dependencies for a junit5 test are on the classpath before creating the actual runner.
* In this way we can offer a useful error message to people, which is always nice, right?
*
* <p>Most of the configuration information can be found on this page in the <a
* href="https://docs.bazel.build/versions/master/test-encyclopedia.html">Test Encyclopedia</a>.
*/
public class JUnit5Runner {

private static final String JUNIT5_RUNNER_CLASS = "com.github.bazel_contrib.contrib_rules_jvm.junit5.ActualRunner";
private static final String JUNIT5_RUNNER_CLASS =
"com.github.bazel_contrib.contrib_rules_jvm.junit5.ActualRunner";

public static void main(String[] args) {
var testSuite = System.getProperty("bazel.test_suite");
Expand All @@ -25,9 +24,8 @@ public static void main(String[] args) {
detectJUnit5Classes();

try {
var constructor = Class.forName(JUNIT5_RUNNER_CLASS)
.asSubclass(RunsTest.class)
.getConstructor();
var constructor =
Class.forName(JUNIT5_RUNNER_CLASS).asSubclass(RunsTest.class).getConstructor();
var runsTest = constructor.newInstance();
if (!runsTest.run(testSuite)) {
System.exit(2);
Expand All @@ -45,29 +43,26 @@ public static void main(String[] args) {

private static void detectJUnit5Classes() {
checkClass(
"org.junit.jupiter.engine.JupiterTestEngine",
"org.junit.jupiter:junit-jupiter-engine");
"org.junit.jupiter.engine.JupiterTestEngine", "org.junit.jupiter:junit-jupiter-engine");
checkClass(
"org.junit.platform.commons.JUnitException",
"org.junit.platform:junit-platform-commons");
"org.junit.platform.commons.JUnitException", "org.junit.platform:junit-platform-commons");
checkClass(
"org.junit.platform.engine.ExecutionRequest",
"org.junit.platform:junit-platform-engine");
"org.junit.platform.engine.ExecutionRequest", "org.junit.platform:junit-platform-engine");
checkClass(
"org.junit.platform.launcher.TestPlan",
"org.junit.platform:junit-platform-launcher");
"org.junit.platform.launcher.TestPlan", "org.junit.platform:junit-platform-launcher");
checkClass(
"org.junit.platform.reporting.legacy.LegacyReportingUtils",
"org.junit.platform:junit-platform-reporting");
"org.junit.platform.reporting.legacy.LegacyReportingUtils",
"org.junit.platform:junit-platform-reporting");
}

private static void checkClass(String className, String containedInDependency) {
try {
Class.forName(className);
} catch (ReflectiveOperationException e) {
throw new IllegalStateException(
String.format("JUnit 5 test runner is missing a dependency on `artifact(\"%s\")`%n", containedInDependency));
String.format(
"JUnit 5 test runner is missing a dependency on `artifact(\"%s\")`%n",
containedInDependency));
}
}

}
Loading

0 comments on commit 2cb8467

Please sign in to comment.