-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(#9)!: add option to force baseline overwrite
- Loading branch information
Showing
6 changed files
with
162 additions
and
2 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
43 changes: 43 additions & 0 deletions
43
src/main/java/io/github/goatfryed/assert_baseline/core/CliOption.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,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); | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/io/github/goatfryed/assert_baseline/core/Options.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,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); | ||
} | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
src/test/java/io/github/goatfryed/assert_baseline/core/BaselineOverwriteTest.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,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"); | ||
} | ||
} |