diff --git a/README.md b/README.md index 4e749e1..9dde700 100644 --- a/README.md +++ b/README.md @@ -103,6 +103,13 @@ By default, the following conventions are assumed See [conventions & configuration](./docs/convention-and-configuration.md) how this can be changed. +## Tips & Tricks +### Force overwrite +Use system property `-Dio.github.goatfryed.assert_baseline.forceBaselineUpdate` to force baseline overwrites. +Use with care! This will skip all assertions and overwrite your baselines. +We recommend to git commit before and git diff after to verify your new baselines. This can ease test updates after larger updates. + + ## Goals & Non-Goals See our [project scope](./docs/project-scope.md) to for more diff --git a/docs/extend-assert-baseline.md b/docs/extend-assert-baseline.md index e2bcdaf..57a5af8 100644 --- a/docs/extend-assert-baseline.md +++ b/docs/extend-assert-baseline.md @@ -8,14 +8,19 @@ Compare [JsonBaselineAssertion](../src/main/java/io/github/goatfryed/assert_base (!) This internal API is considered unstable. ## Extending from AbstractBaselineAssertion +You can extend from [AbstractBaselineAssertion](../src/main/java/io/github/goatfryed/assert_baseline/core/AbstractBaselineAssertion.java) -### 1. implement write -Implement `AbstractBaselineAssertion::saveActual(BaselineContext)`. -Serialize your subject and write it to `BaselineContext::getActualOutputStream` +### implement assertion logic +First implement `AbstractBaselineAssertion::getAdapter(BaselineContext)`. -### 2. implement verification -Implement `verifyIsEqualToBaseline(BaselineContext context)`. -Usually, you'll want to read from `BaselineContext::getBaselineAsString(), deserialize the baseline, -and then compare the java beans. +Compare [JsonBaselineAssertion](../src/main/java/io/github/goatfryed/assert_baseline/json/JsonBaselineAssertion.java) +for an example. + +The adapter is used to decouple operations from the test strategy used. +Effectively, atm we just implement the adapter on the assertion itself and don't use more complex context. +This is likely to change in the future, when more complex strategies are required. ### implement further conventional methods +Usually, you want to implement at least +- `usingYourFormatComparator(...)` +- `yourFormatSatisfies(...)` \ No newline at end of file diff --git a/src/main/java/io/github/goatfryed/assert_baseline/core/AbstractBaselineAssertion.java b/src/main/java/io/github/goatfryed/assert_baseline/core/AbstractBaselineAssertion.java index 540775f..8aaad25 100644 --- a/src/main/java/io/github/goatfryed/assert_baseline/core/AbstractBaselineAssertion.java +++ b/src/main/java/io/github/goatfryed/assert_baseline/core/AbstractBaselineAssertion.java @@ -4,6 +4,7 @@ import io.github.goatfryed.assert_baseline.core.convention.ConventionLocator; import io.github.goatfryed.assert_baseline.core.storage.StorageFactory; import org.assertj.core.api.AbstractAssert; +import org.jetbrains.annotations.NotNull; import java.util.function.Function; @@ -24,10 +25,10 @@ public SELF using(Function config } public final SELF isEqualToBaseline(String baseline) { - var context = getContextFactory().build(baseline); - saveActual(context); - verifyIsEqualToBaseline(context); + getContextFactory() + .build(baseline) + .assertWithAdapter(getAssertionAdapter()); return myself; } @@ -39,15 +40,8 @@ public final SELF usingStorage(Configurer configurer) { return myself; } - /** - * Expects you to write your subject in serialized form to {@link BaselineContext#getActualOutputStream()} - */ - abstract protected void saveActual(BaselineContext context); - - /** - * Usually, you'll want to read {@link BaselineContext#getBaselineAsString()}, deserialize the baseline and compare the models. - */ - abstract protected void verifyIsEqualToBaseline(BaselineContext context); + @NotNull + abstract protected BaselineAssertionAdapter getAssertionAdapter(); private BaselineContextFactory getContextFactory() { if (contextFactory == null) { diff --git a/src/main/java/io/github/goatfryed/assert_baseline/core/BaselineAssertionAdapter.java b/src/main/java/io/github/goatfryed/assert_baseline/core/BaselineAssertionAdapter.java new file mode 100644 index 0000000..eec67cc --- /dev/null +++ b/src/main/java/io/github/goatfryed/assert_baseline/core/BaselineAssertionAdapter.java @@ -0,0 +1,27 @@ +package io.github.goatfryed.assert_baseline.core; + +/** + * Adapter interface to perform the real serialization and assertion + *

+ * This interface is used to separate the format dependent actions of baseline testing + * from the test process itself. + */ +public interface BaselineAssertionAdapter { + + /** + * Writes the serialized representation of the actual subject + * + * @param output Out parameter + * @param context + */ + void writeActual(BaselineContext.ActualOutput output, BaselineContext context); + + /** + * Perform the equality assertion against the provided baseline + * + * @param baseline the baseline input + * @param context + */ + void assertEquals(BaselineContext.BaselineInput baseline, BaselineContext context); + +} diff --git a/src/main/java/io/github/goatfryed/assert_baseline/core/BaselineContext.java b/src/main/java/io/github/goatfryed/assert_baseline/core/BaselineContext.java index 73e40c8..d68da34 100644 --- a/src/main/java/io/github/goatfryed/assert_baseline/core/BaselineContext.java +++ b/src/main/java/io/github/goatfryed/assert_baseline/core/BaselineContext.java @@ -5,7 +5,6 @@ import org.assertj.core.description.Description; import org.assertj.core.description.JoinDescription; import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; import java.io.FileNotFoundException; import java.io.IOException; @@ -13,8 +12,9 @@ import java.io.OutputStream; import java.nio.charset.StandardCharsets; import java.util.Arrays; -import java.util.Collections; -import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.function.Supplier; public class BaselineContext { @@ -22,15 +22,18 @@ public class BaselineContext { private final StoredValue actual; @NotNull private final StoredValue baseline; + @NotNull final Map options; public BaselineContext( @NotNull StoredValue baseline, - @NotNull StoredValue actual + @NotNull StoredValue actual, + @NotNull Map options ) { this.actual = actual; this.baseline = baseline; + this.options = options; - actual .setName("actual :"); + actual.setName("actual :"); baseline.setName("baseline :"); } @@ -38,32 +41,14 @@ public BaselineContext( return actual; } - public @NotNull OutputStream getActualOutputStream() { - return actual.getOutputStream(); - } public @NotNull StoredValue getBaseline() { return baseline; } - public @NotNull InputStream getBaselineInputStream() { - try { - return baseline.getInputStream(); - } catch (AssertionError e) { - if (e.getCause() instanceof FileNotFoundException) { - throw new AssertionError( - "No baseline found. Consider saving %s as baseline.".formatted( - actual.asDescription() - ), e - ); - } - throw e; - } - } - public @NotNull Description asDescription() { return new JoinDescription( - "Baseline set","", + "Baseline set", "", Arrays.asList( baseline.asDescription(), actual.asDescription() @@ -71,14 +56,87 @@ public BaselineContext( ); } - public String getBaselineAsString() { - try (var input = getBaselineInputStream()) { - return IOUtils.toString(input, StandardCharsets.UTF_8); - } catch (IOException e) { - throw new AssertionError( - "Failed to read baseline as string\n%s".formatted(getActual().asDescription()), - e - ); + public void assertWithAdapter(BaselineAssertionAdapter adapter) { + // not using .getOrDefault, because explicit null should be treated as false as well + boolean forceBaselineUpdate = Optional.ofNullable( + (Boolean) options.get(Options.StandardOptions.FORCE_BASELINE_UPDATE) + ).orElse(false); + + if (forceBaselineUpdate) { + adapter.writeActual(new ActualOutput(getBaseline()), this); + System.err.println("WARNING: Forced baseline creation or update of %s. Skipping check.".formatted(getBaseline().asDescription())); + return; + } + + var actualOutput = new ActualOutput(getActual()); + adapter.writeActual(actualOutput, this); + + var baselineInput = new BaselineInput( + getBaseline(), + () -> "Consider saving %s as baseline.".formatted( + actual.asDescription() + ) + ); + adapter.assertEquals(baselineInput, this); + } + + public static class BaselineInput { + + private final StoredValue baseline; + private final Supplier notFoundSuggestionSupplier; + + public BaselineInput( + StoredValue baseline, + Supplier notFoundSuggestionSupplier + ) { + this.baseline = baseline; + this.notFoundSuggestionSupplier = notFoundSuggestionSupplier; + } + + public @NotNull InputStream getInputStream() { + try { + return baseline.getInputStream(); + } catch (FileNotFoundException e) { + throw new AssertionError( + "No baseline found. %s".formatted( + notFoundSuggestionSupplier.get() + ), e + ); + } catch (IOException e) { + throw new AssertionError( + "Failed to create input stream for %s.".formatted( + baseline.asDescription() + ), e + ); + } + } + + public String readContentAsString() { + try (var input = getInputStream()) { + return IOUtils.toString(input, StandardCharsets.UTF_8); + } catch (IOException e) { + throw new AssertionError( + "Failed to read baseline as string\n%s".formatted(baseline.asDescription()), + e + ); + } + } + } + + public static class ActualOutput { + + private final StoredValue actual; + + public ActualOutput(StoredValue actual) { + this.actual = actual; + } + + public @NotNull OutputStream outputStream() { + try { + return actual.getOutputStream(); + } catch (IOException e) { + throw new AssertionError("failed to create output stream for " + actual.asDescription(), e); + } } } } \ No newline at end of file diff --git a/src/main/java/io/github/goatfryed/assert_baseline/core/BaselineContextFactory.java b/src/main/java/io/github/goatfryed/assert_baseline/core/BaselineContextFactory.java index bec2dd4..240eb21 100644 --- a/src/main/java/io/github/goatfryed/assert_baseline/core/BaselineContextFactory.java +++ b/src/main/java/io/github/goatfryed/assert_baseline/core/BaselineContextFactory.java @@ -1,7 +1,11 @@ package io.github.goatfryed.assert_baseline.core; import io.github.goatfryed.assert_baseline.core.storage.*; +import org.jetbrains.annotations.NotNull; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; import java.util.Objects; public class BaselineContextFactory { @@ -21,7 +25,22 @@ public BaselineContext build(String requestedKey) { validateDifferentPaths(baseline, actual); - return new BaselineContext(baseline, actual); + return new BaselineContext(baseline, actual, collectOptions()); + } + + private @NotNull Map collectOptions() { + Map options = new HashMap<>(); + + for (Options.StandardOptions option : Options.StandardOptions.values()) { + option.cliOption().ifPresent(cliOption -> { + var property = System.getProperty(cliOption.systemProperty()); + if (property != null) { + options.put(option, cliOption.optionType().normalize(property)); + } + }); + } + + return options; } private void validateDifferentPaths(StoredValue baseline, StoredValue actual) { diff --git a/src/main/java/io/github/goatfryed/assert_baseline/core/CliOption.java b/src/main/java/io/github/goatfryed/assert_baseline/core/CliOption.java new file mode 100644 index 0000000..8ff94be --- /dev/null +++ b/src/main/java/io/github/goatfryed/assert_baseline/core/CliOption.java @@ -0,0 +1,43 @@ +package io.github.goatfryed.assert_baseline.core; + +import org.jetbrains.annotations.NotNull; + +import java.util.function.Function; + +public class CliOption { + @NotNull + private final String systemProperty; + @NotNull + private final OptionType optionType; + + CliOption(@NotNull String systemProperty, @NotNull OptionType optionType) { + this.systemProperty = systemProperty; + this.optionType = optionType; + } + + public String systemProperty() { + return systemProperty; + } + + OptionType optionType() { + return optionType; + } + + enum OptionType { + Boolean(sVal -> { + if (sVal == null) return false; + if (sVal.equalsIgnoreCase("false")) return true; + return true; + }); + + private final Function normalizer; + + OptionType(Function normalizer) { + this.normalizer = normalizer; + } + + public Object normalize(String property) { + return normalizer.apply(property); + } + } +} diff --git a/src/main/java/io/github/goatfryed/assert_baseline/core/Options.java b/src/main/java/io/github/goatfryed/assert_baseline/core/Options.java new file mode 100644 index 0000000..50050b6 --- /dev/null +++ b/src/main/java/io/github/goatfryed/assert_baseline/core/Options.java @@ -0,0 +1,27 @@ +package io.github.goatfryed.assert_baseline.core; + +import org.jetbrains.annotations.Nullable; + +import java.util.Optional; + +public interface Options { + enum StandardOptions implements Options { + FORCE_BASELINE_UPDATE( + new CliOption( + "io.github.goatfryed.assert_baseline.forceBaselineUpdate", + CliOption.OptionType.Boolean + ) + ); + + @Nullable + private final CliOption cliOption; + + StandardOptions(@Nullable CliOption cliOption) { + this.cliOption = cliOption; + } + + public Optional cliOption() { + return Optional.ofNullable(cliOption); + } + } +} diff --git a/src/main/java/io/github/goatfryed/assert_baseline/core/storage/StoredValue.java b/src/main/java/io/github/goatfryed/assert_baseline/core/storage/StoredValue.java index 84c711f..4d464e6 100644 --- a/src/main/java/io/github/goatfryed/assert_baseline/core/storage/StoredValue.java +++ b/src/main/java/io/github/goatfryed/assert_baseline/core/storage/StoredValue.java @@ -28,9 +28,7 @@ public StoredValue( } public Description asDescription() { - var innerDescription = driver != null ? - driver.asDescription(descriptor).toString() - : "[NO DRIVER SET]"; + var innerDescription = driver.asDescription(descriptor).toString(); return new TextDescription(name + " " + innerDescription); } @@ -43,20 +41,12 @@ public StoredValue setName(@NotNull String name) { return this; } - public InputStream getInputStream() { - try { - return driver.getInputStream(descriptor); - } catch (IOException e) { - throw new AssertionError("failed to create input stream for " + asDescription(), e); - } + public InputStream getInputStream() throws IOException { + return driver.getInputStream(descriptor); } - public OutputStream getOutputStream() { - try { - return driver.getOutputStream(descriptor); - } catch (IOException e) { - throw new AssertionError("failed to create output stream for " + asDescription(), e); - } + public OutputStream getOutputStream() throws IOException { + return driver.getOutputStream(descriptor); } public Object getDriverDescriptor() { diff --git a/src/main/java/io/github/goatfryed/assert_baseline/core/storage/ValueDescriptor.java b/src/main/java/io/github/goatfryed/assert_baseline/core/storage/ValueDescriptor.java index 82153bd..104d146 100644 --- a/src/main/java/io/github/goatfryed/assert_baseline/core/storage/ValueDescriptor.java +++ b/src/main/java/io/github/goatfryed/assert_baseline/core/storage/ValueDescriptor.java @@ -49,4 +49,13 @@ public ValueDescriptor addTag(String tag) { tags.add(tag); return this; } + + @Override + public String toString() { + return "ValueDescriptor{" + + "contextPath=" + contextPath + + ", valuePath=" + valuePath + + ", tags=" + tags + + '}'; + } } diff --git a/src/main/java/io/github/goatfryed/assert_baseline/json/JsonBaselineAssertion.java b/src/main/java/io/github/goatfryed/assert_baseline/json/JsonBaselineAssertion.java index 92c3f34..70fa8d6 100644 --- a/src/main/java/io/github/goatfryed/assert_baseline/json/JsonBaselineAssertion.java +++ b/src/main/java/io/github/goatfryed/assert_baseline/json/JsonBaselineAssertion.java @@ -1,17 +1,19 @@ package io.github.goatfryed.assert_baseline.json; import io.github.goatfryed.assert_baseline.core.AbstractBaselineAssertion; +import io.github.goatfryed.assert_baseline.core.BaselineAssertionAdapter; import io.github.goatfryed.assert_baseline.core.BaselineContext; import io.github.goatfryed.assert_baseline.SerializableSubject; import net.javacrumbs.jsonunit.assertj.JsonAssert; import net.javacrumbs.jsonunit.core.Configuration; +import org.jetbrains.annotations.NotNull; import java.util.function.Consumer; import java.util.function.Function; import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson; -public class JsonBaselineAssertion extends AbstractBaselineAssertion { +public class JsonBaselineAssertion extends AbstractBaselineAssertion implements BaselineAssertionAdapter { private final SerializableSubject subject; private Function comparatorConfigurer = Function.identity(); @@ -45,19 +47,24 @@ public JsonBaselineAssertion usingJsonComparator( } @Override - protected void saveActual(BaselineContext context) { - subject.writeTo(context::getActualOutputStream); + public void writeActual(BaselineContext.ActualOutput actualOutput, BaselineContext context) { + subject.writeTo(actualOutput::outputStream); } @Override - protected void verifyIsEqualToBaseline(BaselineContext context) { + public void assertEquals(BaselineContext.BaselineInput baselineInput, BaselineContext context) { getJsonAssert() .describedAs(context.asDescription()) - .isEqualTo(context.getBaselineAsString()); + .isEqualTo(baselineInput.readContentAsString()); } private JsonAssert.ConfigurableJsonAssert getJsonAssert() { return assertThatJson(subject.serialized()) .withConfiguration(comparatorConfigurer); } + + @Override + protected @NotNull BaselineAssertionAdapter getAssertionAdapter() { + return this; + } } diff --git a/src/main/java/io/github/goatfryed/assert_baseline/text/TextBaselineAssertion.java b/src/main/java/io/github/goatfryed/assert_baseline/text/TextBaselineAssertion.java index 910e74d..021c7bf 100644 --- a/src/main/java/io/github/goatfryed/assert_baseline/text/TextBaselineAssertion.java +++ b/src/main/java/io/github/goatfryed/assert_baseline/text/TextBaselineAssertion.java @@ -1,17 +1,17 @@ package io.github.goatfryed.assert_baseline.text; import io.github.goatfryed.assert_baseline.core.AbstractBaselineAssertion; +import io.github.goatfryed.assert_baseline.core.BaselineAssertionAdapter; import io.github.goatfryed.assert_baseline.core.BaselineContext; import io.github.goatfryed.assert_baseline.SerializableSubject; -import net.javacrumbs.jsonunit.core.Configuration; import org.assertj.core.api.AbstractStringAssert; +import org.jetbrains.annotations.NotNull; import java.util.function.Consumer; -import java.util.function.Function; import static org.assertj.core.api.Assertions.assertThat; -public class TextBaselineAssertion extends AbstractBaselineAssertion { +public class TextBaselineAssertion extends AbstractBaselineAssertion implements BaselineAssertionAdapter { private final SerializableSubject subject; @@ -26,15 +26,20 @@ public TextBaselineAssertion textSatisfies(Consumer> ass } @Override - protected void saveActual(BaselineContext context) { - subject.writeTo(context::getActualOutputStream); + protected @NotNull BaselineAssertionAdapter getAssertionAdapter() { + return this; } @Override - protected void verifyIsEqualToBaseline(BaselineContext context) { + public void writeActual(BaselineContext.ActualOutput actualOutput, BaselineContext context) { + subject.writeTo(actualOutput::outputStream); + } + + @Override + public void assertEquals(BaselineContext.BaselineInput baselineInput, BaselineContext context) { getStringAssert() .describedAs(context.asDescription()) - .isEqualTo(context.getBaselineAsString()); + .isEqualTo(baselineInput.readContentAsString()); } private AbstractStringAssert getStringAssert() { diff --git a/src/main/java/io/github/goatfryed/assert_baseline/xml/XmlBaselineAssertion.java b/src/main/java/io/github/goatfryed/assert_baseline/xml/XmlBaselineAssertion.java index 1174be2..af8a6f0 100644 --- a/src/main/java/io/github/goatfryed/assert_baseline/xml/XmlBaselineAssertion.java +++ b/src/main/java/io/github/goatfryed/assert_baseline/xml/XmlBaselineAssertion.java @@ -1,8 +1,10 @@ package io.github.goatfryed.assert_baseline.xml; import io.github.goatfryed.assert_baseline.core.AbstractBaselineAssertion; +import io.github.goatfryed.assert_baseline.core.BaselineAssertionAdapter; import io.github.goatfryed.assert_baseline.core.BaselineContext; import io.github.goatfryed.assert_baseline.SerializableSubject; +import org.jetbrains.annotations.NotNull; import org.xmlunit.assertj.CompareAssert; import org.xmlunit.assertj.XmlAssert; @@ -11,7 +13,7 @@ import static org.xmlunit.assertj.XmlAssert.assertThat; -public class XmlBaselineAssertion extends AbstractBaselineAssertion { +public class XmlBaselineAssertion extends AbstractBaselineAssertion implements BaselineAssertionAdapter { private final SerializableSubject subject; private Function comparatorConfigurer = Function.identity(); @@ -45,14 +47,19 @@ public XmlBaselineAssertion usingXmlComparator( } @Override - protected void saveActual(BaselineContext context) { - subject.writeTo(context::getActualOutputStream); + protected @NotNull BaselineAssertionAdapter getAssertionAdapter() { + return this; } @Override - protected void verifyIsEqualToBaseline(BaselineContext context) { + public void writeActual(BaselineContext.ActualOutput actualOutput, BaselineContext context) { + subject.writeTo(actualOutput::outputStream); + } + + @Override + public void assertEquals(BaselineContext.BaselineInput baselineInput, BaselineContext context) { comparatorConfigurer.apply( - getXmlAssert().and(context.getBaselineAsString()) + getXmlAssert().and(baselineInput.readContentAsString()) ) .describedAs(context.asDescription()) .areSimilar(); diff --git a/src/test/java/io/github/goatfryed/assert_baseline/BaselineAssertionTest.java b/src/test/java/io/github/goatfryed/assert_baseline/BaselineAssertionTest.java index cedfc55..1bd1345 100644 --- a/src/test/java/io/github/goatfryed/assert_baseline/BaselineAssertionTest.java +++ b/src/test/java/io/github/goatfryed/assert_baseline/BaselineAssertionTest.java @@ -1,7 +1,9 @@ package io.github.goatfryed.assert_baseline; import io.github.goatfryed.assert_baseline.core.AbstractBaselineAssertion; +import io.github.goatfryed.assert_baseline.core.BaselineAssertionAdapter; import io.github.goatfryed.assert_baseline.core.BaselineContext; +import org.jetbrains.annotations.NotNull; import org.junit.jupiter.api.Test; import static io.github.goatfryed.assert_baseline.BaselineConfigurations.*; @@ -28,9 +30,15 @@ protected DummyBaselineAssertion() { } @Override - protected void saveActual(BaselineContext context) {} + protected @NotNull BaselineAssertionAdapter getAssertionAdapter() { + return new BaselineAssertionAdapter() { - @Override - protected void verifyIsEqualToBaseline(BaselineContext context) {} + @Override + public void writeActual(BaselineContext.ActualOutput output, BaselineContext context) {} + + @Override + public void assertEquals(BaselineContext.BaselineInput baseline, BaselineContext context) {} + }; + } } } diff --git a/src/test/java/io/github/goatfryed/assert_baseline/BaselineContextTest.java b/src/test/java/io/github/goatfryed/assert_baseline/BaselineContextTest.java index 153607e..873724e 100644 --- a/src/test/java/io/github/goatfryed/assert_baseline/BaselineContextTest.java +++ b/src/test/java/io/github/goatfryed/assert_baseline/BaselineContextTest.java @@ -1,9 +1,12 @@ package io.github.goatfryed.assert_baseline; +import io.github.goatfryed.assert_baseline.core.BaselineAssertionAdapter; +import io.github.goatfryed.assert_baseline.core.BaselineContext; import io.github.goatfryed.assert_baseline.core.convention.presets.StandardConventionProvider; import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThatCode; +import static org.assertj.core.api.Assertions.fail; class BaselineContextTest { @@ -13,7 +16,18 @@ public void itRecommendsCopyActionOnMissingBaseline() { var defaultConvention = new StandardConventionProvider().getConvention(); var context = defaultConvention.createContext("baseline.txt"); - assertThatCode(context::getBaselineInputStream) + assertThatCode(() -> { + context.assertWithAdapter(new BaselineAssertionAdapter() { + @Override + public void writeActual(BaselineContext.ActualOutput output, BaselineContext context) {} + + @Override + public void assertEquals(BaselineContext.BaselineInput baseline, BaselineContext context) { + baseline.getInputStream(); + fail("expected the above line to fail with FileNotFound"); + } + }); + }) .hasMessageContaining("No baseline found") .hasMessageMatching("(.|\\n)*Consider saving .* as .*(.|\\n)*"); } diff --git a/src/test/java/io/github/goatfryed/assert_baseline/core/BaselineOverwriteTest.java b/src/test/java/io/github/goatfryed/assert_baseline/core/BaselineOverwriteTest.java new file mode 100644 index 0000000..5ba97d4 --- /dev/null +++ b/src/test/java/io/github/goatfryed/assert_baseline/core/BaselineOverwriteTest.java @@ -0,0 +1,48 @@ +package io.github.goatfryed.assert_baseline.core; + +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; + +import static io.github.goatfryed.assert_baseline.BaselineAssertions.assertThatText; +import static io.github.goatfryed.assert_baseline.BaselineConfigurations.*; +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class BaselineOverwriteTest { + + @Test + public void itCanForceBaselineWriteFromCli() throws IOException { + var actual = "src/test/resources/CliOptionsTest.itCanForceBaselineWrite.actually.actual.test"; + var baseline = "src/test/resources/CliOptionsTest.itCanForceBaselineWrite.actual.test"; + + var baselinePath = Path.of(baseline); + Files.writeString(baselinePath, "false"); + + System.setProperty( + Options.StandardOptions.FORCE_BASELINE_UPDATE.cliOption().get().systemProperty(), + "" + ); + + try { + assertThatText("true") + .usingStorage( + commonRootPath("") + .and(baselinePath(baseline)) + .and(actualPath(actual)) + ).isEqualToBaseline("null"); + } finally { + System.clearProperty( + Options.StandardOptions.FORCE_BASELINE_UPDATE.cliOption().get().systemProperty() + ); + } + + assertEquals( + Files.readString(baselinePath), + "true" + ); + + Files.writeString(baselinePath, "false"); + } +}