Skip to content

Commit

Permalink
feat(#9)!: add option to force baseline overwrite
Browse files Browse the repository at this point in the history
  • Loading branch information
goatfryed committed Oct 21, 2024
1 parent 1509f1c commit 2a5e484
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 2 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Map;
import java.util.Optional;
import java.util.function.Supplier;

public class BaselineContext {
Expand All @@ -20,13 +22,16 @@ public class BaselineContext {
private final StoredValue actual;
@NotNull
private final StoredValue baseline;
@NotNull final Map<Options, Object> options;

public BaselineContext(
@NotNull StoredValue baseline,
@NotNull StoredValue actual
@NotNull StoredValue actual,
@NotNull Map<Options, Object> options
) {
this.actual = actual;
this.baseline = baseline;
this.options = options;

actual.setName("actual :");
baseline.setName("baseline :");
Expand All @@ -52,6 +57,17 @@ public BaselineContext(
}

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);

Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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<Options, Object> collectOptions() {
Map<Options, Object> 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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, Object> normalizer;

OptionType(Function<String,Object> normalizer) {
this.normalizer = normalizer;
}

public Object normalize(String property) {
return normalizer.apply(property);
}
}
}
Original file line number Diff line number Diff line change
@@ -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> cliOption() {
return Optional.ofNullable(cliOption);
}
}
}
Original file line number Diff line number Diff line change
@@ -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");
}
}

0 comments on commit 2a5e484

Please sign in to comment.