-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Only flush output frequently for ProgressFormatter (#2541)
Co-authored-by: Scott Davis <scott.davis@ticketmaster.com> Co-authored-by: M.P. Korstanje <rien.korstanje@gmail.com>
- Loading branch information
1 parent
549fe86
commit 4605c55
Showing
7 changed files
with
174 additions
and
13 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
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
53 changes: 53 additions & 0 deletions
53
core/src/test/java/io/cucumber/core/plugin/NiceAppendableTest.java
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 @@ | ||
package io.cucumber.core.plugin; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.OutputStreamWriter; | ||
|
||
import static io.cucumber.core.plugin.BytesEqualTo.isBytesEqualTo; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.mockito.Mockito.spy; | ||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
|
||
class NiceAppendableTest { | ||
|
||
@Test | ||
public void should_flush_every_call_if_configured() throws IOException { | ||
ByteArrayOutputStream out = new ByteArrayOutputStream(); | ||
OutputStreamWriter writer = spy(new OutputStreamWriter(out)); | ||
NiceAppendable appendable = new NiceAppendable(writer, true); | ||
|
||
appendable | ||
.append("First String,") | ||
.append("__Second String__", 2, 15) | ||
.append("\n") | ||
.println("Second line") | ||
.println() | ||
.close(); | ||
|
||
assertThat(out, isBytesEqualTo("First String,Second String\nSecond line\n\n")); | ||
verify(writer, times(6)).flush(); // Each method call flushes | ||
} | ||
|
||
@Test | ||
public void should_not_flush_unless_configured() throws IOException { | ||
ByteArrayOutputStream out = new ByteArrayOutputStream(); | ||
OutputStreamWriter writer = spy(new OutputStreamWriter(out)); | ||
NiceAppendable appendable = new NiceAppendable(writer); | ||
|
||
appendable | ||
.append("First String,") | ||
.append("__Second String__", 2, 15) | ||
.append("\n") | ||
.println("Second line") | ||
.println() | ||
.close(); | ||
|
||
assertThat(out, isBytesEqualTo("First String,Second String\nSecond line\n\n")); | ||
verify(writer, times(0)).flush(); | ||
} | ||
|
||
} |
85 changes: 85 additions & 0 deletions
85
core/src/test/java/io/cucumber/core/plugin/ProgressFormatterTest.java
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,85 @@ | ||
package io.cucumber.core.plugin; | ||
|
||
import io.cucumber.core.eventbus.EventBus; | ||
import io.cucumber.core.runtime.TimeServiceEventBus; | ||
import io.cucumber.plugin.event.HookTestStep; | ||
import io.cucumber.plugin.event.PickleStepTestStep; | ||
import io.cucumber.plugin.event.Result; | ||
import io.cucumber.plugin.event.TestCase; | ||
import io.cucumber.plugin.event.TestRunFinished; | ||
import io.cucumber.plugin.event.TestStepFinished; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.time.Clock; | ||
import java.time.Duration; | ||
import java.time.Instant; | ||
import java.util.UUID; | ||
|
||
import static io.cucumber.core.plugin.BytesEqualTo.isBytesEqualTo; | ||
import static io.cucumber.plugin.event.Status.FAILED; | ||
import static io.cucumber.plugin.event.Status.PASSED; | ||
import static io.cucumber.plugin.event.Status.UNDEFINED; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.mockito.Mockito.mock; | ||
|
||
class ProgressFormatterTest { | ||
|
||
final EventBus bus = new TimeServiceEventBus(Clock.systemUTC(), UUID::randomUUID); | ||
final ByteArrayOutputStream out = new ByteArrayOutputStream(); | ||
final ProgressFormatter formatter = new ProgressFormatter(out); | ||
|
||
@BeforeEach | ||
void setup() { | ||
formatter.setEventPublisher(bus); | ||
} | ||
|
||
@Test | ||
void prints_empty_line_for_empty_test_run() { | ||
Result runResult = new Result(PASSED, Duration.ZERO, null); | ||
bus.send(new TestRunFinished(Instant.now(), runResult)); | ||
assertThat(out, isBytesEqualTo("\n")); | ||
} | ||
|
||
@Test | ||
void print_green_dot_for_passing_step() { | ||
Result result = new Result(PASSED, Duration.ZERO, null); | ||
bus.send(new TestStepFinished(Instant.now(), mock(TestCase.class), mock(PickleStepTestStep.class), result)); | ||
bus.send(new TestRunFinished(Instant.now(), result)); | ||
assertThat(out, isBytesEqualTo(AnsiEscapes.GREEN + "." + AnsiEscapes.RESET + "\n")); | ||
} | ||
|
||
@Test | ||
void print_yellow_U_for_undefined_step() { | ||
Result result = new Result(UNDEFINED, Duration.ZERO, null); | ||
bus.send(new TestStepFinished(Instant.now(), mock(TestCase.class), mock(PickleStepTestStep.class), result)); | ||
bus.send(new TestRunFinished(Instant.now(), result)); | ||
assertThat(out, isBytesEqualTo(AnsiEscapes.YELLOW + "U" + AnsiEscapes.RESET + "\n")); | ||
} | ||
|
||
@Test | ||
void print_nothing_for_passed_hook() { | ||
Result result = new Result(PASSED, Duration.ZERO, null); | ||
bus.send(new TestStepFinished(Instant.now(), mock(TestCase.class), mock(HookTestStep.class), result)); | ||
bus.send(new TestRunFinished(Instant.now(), result)); | ||
assertThat(out, isBytesEqualTo("\n")); | ||
} | ||
|
||
@Test | ||
void print_red_F_for_failed_step() { | ||
Result result = new Result(FAILED, Duration.ZERO, null); | ||
bus.send(new TestStepFinished(Instant.now(), mock(TestCase.class), mock(PickleStepTestStep.class), result)); | ||
bus.send(new TestRunFinished(Instant.now(), result)); | ||
assertThat(out, isBytesEqualTo(AnsiEscapes.RED + "F" + AnsiEscapes.RESET + "\n")); | ||
} | ||
|
||
@Test | ||
void print_red_F_for_failed_hook() { | ||
Result result = new Result(FAILED, Duration.ZERO, null); | ||
bus.send(new TestStepFinished(Instant.now(), mock(TestCase.class), mock(HookTestStep.class), result)); | ||
bus.send(new TestRunFinished(Instant.now(), result)); | ||
assertThat(out, isBytesEqualTo(AnsiEscapes.RED + "F" + AnsiEscapes.RESET + "\n")); | ||
} | ||
|
||
} |