Skip to content

Commit

Permalink
[Core] Fix test teardown on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
mpkorstanje committed Mar 9, 2023
1 parent 7d90abd commit e596c99
Show file tree
Hide file tree
Showing 15 changed files with 155 additions and 159 deletions.
14 changes: 9 additions & 5 deletions .github/workflows/test-java.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,23 @@ on:

jobs:
build:
name: 'Build'
runs-on: ubuntu-latest
strategy:
matrix:
os: [ ubuntu-latest, windows-latest ]
version: [ 17, 19 ]
name: 'Build Java ${{ matrix.version }} - ${{ matrix.os }}'
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
- uses: actions/setup-java@v3
with:
distribution: 'zulu'
java-version: '17'
java-version: ${{ matrix.version }}
cache: 'maven'
- name: Install dependencies
run: mvn install -Pinclude-extra-modules -DskipTests=true -DskipITs=true -Darchetype.test.skip=true -Dmaven.javadoc.skip=true --batch-mode -Dstyle.color=always --show-version
run: mvn install -Pinclude-extra-modules -DskipTests=true -DskipITs=true -D"archetype.test.skip=true" -D"maven.javadoc.skip=true" --batch-mode -D"style.color=always" --show-version
- name: Test
run: mvn verify -Pinclude-extra-modules -Dstyle.color=always
run: mvn verify -Pinclude-extra-modules -D"style.color=always"
env:
CUCUMBER_PUBLISH_TOKEN: ${{ secrets.CUCUMBER_PUBLISH_TOKEN }}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;

import static io.cucumber.core.plugin.BytesEqualTo.isBytesEqualTo;
import static io.cucumber.core.plugin.Bytes.bytes;
import static java.util.Arrays.asList;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

class BannerTest {
Expand All @@ -28,12 +28,12 @@ void printsAnsiBanner() throws UnsupportedEncodingException {
new Banner.Span("Bla", AnsiEscapes.RED)),
new Banner.Line("Bla Bla")), AnsiEscapes.CYAN);

assertThat(bytes, isBytesEqualTo("" +
assertThat(bytes, bytes(equalTo("" +
"\u001B[36m┌─────────────┐\u001B[0m\n" +
"\u001B[36m│\u001B[0m Bla \u001B[36m│\u001B[0m\n" +
"\u001B[36m│\u001B[0m Bla \u001B[34mBla\u001B[0m \u001B[31mBla\u001B[0m \u001B[36m│\u001B[0m\n" +
"\u001B[36m│\u001B[0m Bla Bla \u001B[36m│\u001B[0m\n" +
"\u001B[36m└─────────────┘\u001B[0m\n"));
"\u001B[36m└─────────────┘\u001B[0m\n")));
}

@Test
Expand All @@ -50,12 +50,12 @@ void printsMonochromeBanner() throws Exception {
new Banner.Span("Bla", AnsiEscapes.RED)),
new Banner.Line("Bla Bla")), AnsiEscapes.CYAN);

assertThat(bytes, isBytesEqualTo("" +
assertThat(bytes, bytes(equalTo("" +
"┌─────────────┐\n" +
"│ Bla │\n" +
"│ Bla Bla Bla │\n" +
"│ Bla Bla │\n" +
"└─────────────┘\n"));
"└─────────────┘\n")));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

import org.hamcrest.Description;
import org.hamcrest.DiagnosingMatcher;
import org.hamcrest.Matcher;

import java.io.ByteArrayOutputStream;

import static java.nio.charset.StandardCharsets.UTF_8;

final class BytesEqualTo {
final class Bytes {

static DiagnosingMatcher<ByteArrayOutputStream> isBytesEqualTo(String expected) {
static DiagnosingMatcher<ByteArrayOutputStream> bytes(Matcher<String> expected) {
return new DiagnosingMatcher<ByteArrayOutputStream>() {
@Override
protected boolean matches(Object actual, Description description) {
Expand All @@ -20,13 +21,13 @@ protected boolean matches(Object actual, Description description) {
}
String actualString = new String(((ByteArrayOutputStream) actual).toByteArray(), UTF_8);
description.appendValue(actualString);
return actualString.equals(expected);
return expected.matches(actualString);
}

@Override
public void describeTo(Description description) {
description.appendText("is ");
description.appendValue(expected);
description.appendDescriptionOf(expected);
}
};
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
import java.util.Collections;
import java.util.UUID;

import static io.cucumber.core.plugin.BytesContainsString.bytesContainsString;
import static io.cucumber.core.plugin.Bytes.bytes;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.MatcherAssert.assertThat;

class HtmlFormatterTest {
Expand All @@ -37,11 +38,11 @@ void writes_index_html() throws Throwable {
TestRunFinished testRunFinished = new TestRunFinished(null, true, new Timestamp(15L, 0L), null);
bus.send(Envelope.of(testRunFinished));

assertThat(bytes, bytesContainsString("" +
assertThat(bytes, bytes(containsString("" +
"window.CUCUMBER_MESSAGES = [" +
"{\"testRunStarted\":{\"timestamp\":{\"seconds\":10,\"nanos\":0}}}," +
"{\"testRunFinished\":{\"success\":true,\"timestamp\":{\"seconds\":15,\"nanos\":0}}}" +
"];\n"));
"];\n")));
}

@Test
Expand Down Expand Up @@ -84,11 +85,11 @@ void ignores_step_definitions() throws Throwable {
null);
bus.send(Envelope.of(testRunFinished));

assertThat(bytes, bytesContainsString("" +
assertThat(bytes, bytes(containsString("" +
"window.CUCUMBER_MESSAGES = [" +
"{\"testRunStarted\":{\"timestamp\":{\"seconds\":10,\"nanos\":0}}}," +
"{\"testRunFinished\":{\"success\":true,\"timestamp\":{\"seconds\":15,\"nanos\":0}}}" +
"];\n"));
"];\n")));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
import java.time.Clock;
import java.util.UUID;

import static io.cucumber.core.plugin.BytesEqualTo.isBytesEqualTo;
import static io.cucumber.core.plugin.Bytes.bytes;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

class JUnitFormatterTest {
Expand All @@ -30,10 +31,10 @@ void writes_report_xml() {
TestRunFinished testRunFinished = new TestRunFinished(null, true, new Timestamp(15L, 0L), null);
bus.send(Envelope.of(testRunFinished));

assertThat(bytes, isBytesEqualTo("" +
assertThat(bytes, bytes(equalTo("" +
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<testsuite name=\"Cucumber\" time=\"5\" tests=\"0\" skipped=\"0\" failures=\"0\" errors=\"0\">\n" +
"</testsuite>\n"));
"</testsuite>\n")));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,17 @@
import io.cucumber.messages.types.TestRunFinished;
import io.cucumber.messages.types.TestRunStarted;
import io.cucumber.messages.types.Timestamp;
import io.cucumber.plugin.event.EventHandler;
import io.cucumber.plugin.event.EventPublisher;
import org.junit.jupiter.api.Test;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.time.Clock;
import java.time.Instant;
import java.util.UUID;

import static io.cucumber.core.plugin.BytesEqualTo.isBytesEqualTo;
import static io.cucumber.core.plugin.Bytes.bytes;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

class NoPublishFormatterTest {
Expand All @@ -35,7 +33,7 @@ public void should_print_banner() throws UnsupportedEncodingException {
bus.send(Envelope.of(new TestRunStarted(new Timestamp(0L, 0L))));
bus.send(Envelope.of(new TestRunFinished(null, true, new Timestamp(0L, 0L), null)));

assertThat(bytes, isBytesEqualTo("" +
assertThat(bytes, bytes(equalTo("" +
"┌───────────────────────────────────────────────────────────────────────────────────┐\n" +
"│ Share your Cucumber Report with your team at https://reports.cucumber.io │\n" +
"│ Activate publishing with one of the following: │\n" +
Expand All @@ -51,7 +49,7 @@ public void should_print_banner() throws UnsupportedEncodingException {
"│ │\n" +
"│ src/test/resources/cucumber.properties: cucumber.publish.quiet=true │\n" +
"│ src/test/resources/junit-platform.properties: cucumber.publish.quiet=true │\n" +
"└───────────────────────────────────────────────────────────────────────────────────┘\n"));
"└───────────────────────────────────────────────────────────────────────────────────┘\n")));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import io.cucumber.core.options.PluginOption;
import io.cucumber.core.runner.ClockStub;
import io.cucumber.core.runtime.TimeServiceEventBus;
import io.cucumber.messages.types.Envelope;
import io.cucumber.plugin.ConcurrentEventListener;
import io.cucumber.plugin.EventListener;
import io.cucumber.plugin.event.EventHandler;
Expand All @@ -30,13 +31,14 @@
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Instant;
import java.util.Objects;
import java.util.UUID;

import static io.cucumber.core.options.TestPluginOption.parse;
import static io.cucumber.messages.Convertor.toMessage;
import static java.nio.file.Files.readAllLines;
import static java.time.Duration.ZERO;
import static java.time.Instant.now;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.CoreMatchers.nullValue;
Expand Down Expand Up @@ -383,6 +385,7 @@ public WantsTooMuch(String too, String much) {
private static class FakeTestRunEventsPublisher implements EventPublisher {
private EventHandler<TestRunStarted> startHandler;
private EventHandler<TestRunFinished> finishedHandler;
private EventHandler<Envelope> envelopeHandler;

@Override
public <T> void registerHandlerFor(Class<T> eventType, EventHandler<T> handler) {
Expand All @@ -392,6 +395,9 @@ public <T> void registerHandlerFor(Class<T> eventType, EventHandler<T> handler)
if (eventType == TestRunFinished.class) {
finishedHandler = ((EventHandler<TestRunFinished>) handler);
}
if (eventType == Envelope.class) {
envelopeHandler = ((EventHandler<Envelope>) handler);
}
}

@Override
Expand All @@ -400,10 +406,14 @@ public <T> void removeHandlerFor(Class<T> eventType, EventHandler<T> handler) {

public void fakeTestRunEvents() {
if (startHandler != null) {
startHandler.receive(new TestRunStarted(Instant.now()));
startHandler.receive(new TestRunStarted(now()));
}
if (finishedHandler != null) {
finishedHandler.receive(new TestRunFinished(Instant.now(), new Result(Status.PASSED, ZERO, null)));
finishedHandler.receive(new TestRunFinished(now(), new Result(Status.PASSED, ZERO, null)));
}
if (envelopeHandler != null) {
envelopeHandler.receive(
Envelope.of(new io.cucumber.messages.types.TestRunFinished("done", false, toMessage(now()), null)));
}
}

Expand Down
Loading

0 comments on commit e596c99

Please sign in to comment.