Skip to content

Commit

Permalink
Include template name in the client test screenshot missing error (#4434
Browse files Browse the repository at this point in the history
)

* Include template name in the error message if possible

* Update javadoc
  • Loading branch information
kevinthegreat1 authored Feb 15, 2025
1 parent 640e77a commit 6816ccd
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ static TestScreenshotComparisonOptions of(NativeImage templateImage) {
* as the one that is compared against in this screenshot comparison.
*
* @return This screenshot comparison options instance
* @throws java.util.NoSuchElementException if template image is not provided by path
*/
TestScreenshotComparisonOptions save();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ private Vector2i doAssertScreenshotContains(
}

if (result == null) {
throw new AssertionError("Screenshot does not contain template");
throw new AssertionError("Screenshot does not contain template" + optionsImpl.getTemplateImagePath().map(" '%s'"::formatted).orElse(""));
}

return result.add(region.getX(), region.getY());
Expand Down Expand Up @@ -418,7 +418,7 @@ private static Path saveScreenshot(NativeImage screenshot, String fileName, Test

private static void onTemplateImageDoesntExist(NativeImage subScreenshot, TestScreenshotComparisonOptionsImpl options) {
if (TestSystemProperties.TEST_MOD_RESOURCES_PATH != null) {
Path savePath = Path.of(TestSystemProperties.TEST_MOD_RESOURCES_PATH).resolve("templates").resolve(options.getTemplateImagePath() + ".png");
Path savePath = Path.of(TestSystemProperties.TEST_MOD_RESOURCES_PATH).resolve("templates").resolve(options.getTemplateImagePathOrThrow() + ".png");

try {
Files.createDirectories(savePath.getParent());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Optional;

import com.google.common.base.Preconditions;
import com.mojang.datafixers.util.Either;
Expand Down Expand Up @@ -52,7 +53,7 @@ public TestScreenshotComparisonOptionsImpl(NativeImage templateImage) {

@Override
public TestScreenshotComparisonOptions save() {
return saveWithFileName(getTemplateImagePath());
return saveWithFileName(getTemplateImagePathOrThrow());
}

@Override
Expand Down Expand Up @@ -89,8 +90,20 @@ public TestScreenshotComparisonOptions withRegion(int x, int y, int width, int h
return this;
}

public String getTemplateImagePath() {
return this.templateImage.left().orElseThrow();
/**
* Gets the path to the template image, relative to the {@code templates} directory, if one was provided.
*/
public Optional<String> getTemplateImagePath() {
return this.templateImage.left();
}

/**
* Gets the path to the template image, relative to the {@code templates} directory, if one was provided.
*
* @throws java.util.NoSuchElementException if template image is not provided by path
*/
public String getTemplateImagePathOrThrow() {
return this.getTemplateImagePath().orElseThrow();
}

@Nullable
Expand Down

0 comments on commit 6816ccd

Please # to comment.