Include template name in the client test screenshot missing error ()

* Include template name in the error message if possible

* Update javadoc
This commit is contained in:
Kevin 2025-02-15 22:37:58 +08:00 committed by GitHub
parent 640e77ae59
commit 6816ccd43b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 19 additions and 5 deletions
fabric-client-gametest-api-v1/src/client/java/net/fabricmc/fabric

View file

@ -77,6 +77,7 @@ public interface TestScreenshotComparisonOptions extends TestScreenshotCommonOpt
* 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();

View file

@ -361,7 +361,7 @@ public final class ClientGameTestContextImpl implements ClientGameTestContext {
}
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());
@ -418,7 +418,7 @@ public final class ClientGameTestContextImpl implements ClientGameTestContext {
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());

View file

@ -21,6 +21,7 @@ import java.io.InputStream;
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;
@ -52,7 +53,7 @@ public final class TestScreenshotComparisonOptionsImpl extends TestScreenshotCom
@Override
public TestScreenshotComparisonOptions save() {
return saveWithFileName(getTemplateImagePath());
return saveWithFileName(getTemplateImagePathOrThrow());
}
@Override
@ -89,8 +90,20 @@ public final class TestScreenshotComparisonOptionsImpl extends TestScreenshotCom
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