test: cover Printer and the floci update flow (#57)

* test: cover Printer and the floci update flow

PrinterTest pins where text, errors and warnings go, what --quiet hides, and the JSON and YAML rendering. UpdateCommandTest drives floci update against a stub release server: a verified replacement, a checksum mismatch, --check, up to date, a Homebrew install and a non-version API response. UpdateCommand takes the binary path and the release URLs through a constructor for this, replacing a package-private field.

Signed-off-by: Hector Ventura <hectorvent@gmail.com>

* test: check the exact release files requested and the version check itself

The stub release server serves only the expected release's files, the replacement test asserts the two requests made, valid JSON without a usable version is covered, and the Printer assertions use the platform line separator.

Signed-off-by: Hector Ventura <hectorvent@gmail.com>

---------

Signed-off-by: Hector Ventura <hectorvent@gmail.com>
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-10-10 22:30:07 -05:00
1 parent a9edb57aa6
commit 6a74d7d2d5
4 files changed
+380 -8

No files matched your search

@@ -74,8 +74,21 @@ public class UpdateCommand implements Callable<Integer> {
return http;
}
/** The binary to replace; {@code null} means "resolve the running executable". Test seam. */
Path selfPath;
/** The binary to replace; {@code null} means "resolve the running executable". */
private final Path selfPath;
private final String latestReleaseUrl;
private final String downloadBase;
public UpdateCommand() {
this(null, ReleaseChannel.latestReleaseUrl(), ReleaseChannel.downloadBase());
}
/** Test seam: replace {@code selfPath}, resolving releases and assets from the given URLs. */
public UpdateCommand(Path selfPath, String latestReleaseUrl, String downloadBase) {
this.selfPath = selfPath;
this.latestReleaseUrl = latestReleaseUrl;
this.downloadBase = downloadBase;
}
@Override
public Integer call() {
@@ -117,8 +130,8 @@ public class UpdateCommand implements Callable<Integer> {
try {
printer.println("downloading " + asset + " " + target + "...");
Path fresh = work.resolve(asset);
fetchFile(ReleaseChannel.assetUrl(target, asset), fresh);
verifyChecksum(fresh, asset, fetchString(ReleaseChannel.assetUrl(target, "sha256sums.txt")));
fetchFile(ReleaseChannel.assetUrl(downloadBase, target, asset), fresh);
verifyChecksum(fresh, asset, fetchString(ReleaseChannel.assetUrl(downloadBase, target, "sha256sums.txt")));
printer.println(Ansi.green("✓") + " checksum verified");
replaceAtomically(fresh, binary);
} finally {
@@ -141,7 +154,7 @@ public class UpdateCommand implements Callable<Integer> {
private String fetchLatestVersion() {
try {
JsonNode release = JSON.readTree(fetchString(ReleaseChannel.latestReleaseUrl()));
JsonNode release = JSON.readTree(fetchString(latestReleaseUrl));
String tag = release.path("tag_name").asText("");
// Same shape-check as UpdateNotifier: a captive portal or proxy error page must
// not end up spliced into a download URL.
@@ -25,8 +25,16 @@ public final class ReleaseChannel {
/** Download URL for one asset of one release, e.g. {@code floci-darwin-arm64}. */
public static String assetUrl(String version, String asset) {
String base = System.getenv().getOrDefault(
DOWNLOAD_ENV, "https://github.com/" + REPO + "/releases/download");
return base + "/" + version + "/" + asset;
return assetUrl(downloadBase(), version, asset);
}
/** Same, under an explicit download base (see {@link #downloadBase()}). */
public static String assetUrl(String downloadBase, String version, String asset) {
return downloadBase + "/" + version + "/" + asset;
}
/** Where release assets are downloaded from: GitHub, or {@code FLOCI_UPDATE_DOWNLOAD_URL}. */
public static String downloadBase() {
return System.getenv().getOrDefault(DOWNLOAD_ENV, "https://github.com/" + REPO + "/releases/download");
}
}