import java.io.*; import java.nio.file.*; import java.time.*; import java.util.*; public class dotfiles_integration_test { static Path root; public static void main(String[] args) throws Exception { if (args.length > 0 && args[0].equals("--inside")) inside(); else host(); } static void host() throws Exception { root = Path.of(run(null, "git", "rev-parse", "--show-toplevel").trim()); var common = Path.of(run(root, "git", "rev-parse", "--git-common-dir").trim()); var key = (common.isAbsolute() ? common : root.resolve(common)).resolve("dotfiles-aes.key"); if (!Files.isReadable(key)) fail("missing " + key); var container = "dotfiles-integration-" + LocalDateTime.now() + "-" + ProcessHandle.current().pid(); container = container.replaceAll("[^A-Za-z0-9_.-]", "-"); try { step("start container"); run(null, "podman", "run", "--name", container, "--rm", "-d", "--hostname", "desktop", "-v", root + ":/src:ro", "-v", key + ":/dotfiles-aes.key:ro", "registry.fedoraproject.org/fedora:latest", "sleep", "infinity"); ok("container-started"); step("install bootstrap packages"); run(null, "podman", "exec", container, "dnf", "-qy", "--setopt=install_weak_deps=False", "install", "git-core", "libatomic"); ok("bootstrap-packages-installed"); step("install mise"); installMise(container); ok("mise-installed"); run(null, "podman", "exec", container, "git", "config", "--global", "--add", "safe.directory", "/src"); run(null, "podman", "exec", container, "mkdir", "-p", "/root/work"); step("clone repo"); run(null, "podman", "exec", container, "git", "clone", "--quiet", "--no-local", "/src", "/root/work/dotfiles"); run(null, "podman", "exec", container, "install", "-m", "600", "/dotfiles-aes.key", "/root/work/dotfiles/.git/dotfiles-aes.key"); step("trust mise config"); run(null, "podman", "exec", "-w", "/root/work/dotfiles", container, "mise", "trust", "--yes"); ok("mise-trusted"); step("install mise tools"); runInherit(null, "podman", "exec", "-w", "/root/work/dotfiles", container, "mise", "install"); step("run ci check"); runInherit(null, "podman", "exec", "-w", "/root/work/dotfiles", container, "mise", "exec", "--", "java", "./scripts/dotfiles_integration_test.java", "--inside"); } finally { runAllowFail(null, "podman", "rm", "-f", container); } } static void installMise(String container) throws Exception { var version = run(null, "mise", "--version").trim().split("\\s+")[0]; var arch = run(null, "podman", "exec", container, "uname", "-m").trim(); var platform = switch (arch) { case "x86_64", "amd64" -> "x64"; case "aarch64", "arm64" -> "arm64"; default -> throw new IllegalStateException("unsupported container arch: " + arch); }; var url = "https://github.com/jdx/mise/releases/download/v" + version + "/mise-v" + version + "-linux-" + platform; run(null, "podman", "exec", container, "curl", "-fsSL", "-o", "/usr/local/bin/mise", url); run(null, "podman", "exec", container, "chmod", "755", "/usr/local/bin/mise"); System.out.println(run(null, "podman", "exec", container, "mise", "--version").trim()); } static void inside() throws Exception { root = Path.of(".").toAbsolutePath().normalize(); System.out.println("rootless=" + rootless()); ok("deps"); System.out.println(run(root, "git", "--version").trim()); System.out.println(run(root, "mise", "--version").trim()); System.out.println(run(root, "java", "-version").lines().findFirst().orElse("java")); miseShellSmoke(); run(root, "mise", "run", "hook"); var common = Path.of(run(root, "git", "rev-parse", "--git-common-dir").trim()); var hook = (common.isAbsolute() ? common : root.resolve(common)).resolve("hooks/pre-commit"); if (!Files.isExecutable(hook)) fail("hook-executable"); if (!Files.readString(hook).contains("exec java bootstripper.java pre-commit")) fail("hook-content"); ok("install-hooks"); if (!run(root, "git", "show", "HEAD:nushell/secrets.nu").startsWith("DFAES1")) fail("committed-encrypted"); ok("committed-encrypted"); var hosts = allHosts(); System.out.println("manifests=" + String.join(" ", hosts)); for (var host : hosts) runManifest(host); ok("symlink-manifests:" + hosts.size()); if (Files.readString(root.resolve("nushell/secrets.nu")).startsWith("DFAES1")) fail("worktree-still-encrypted"); ok("worktree-decrypted"); run(root, "mise", "run", "check"); ok("check-task"); run(root, ".git/hooks/pre-commit"); ok("pre-commit-hook"); freshCryptoSmoke(); System.out.println("RESULT:ok"); } static void miseShellSmoke() throws Exception { var smoke = "mise-shell-smoke"; var snippet = """ mkdir(\"%1$s/tree\"); write(\"%1$s/a.txt\", \"alpha\"); IO.println(read(\"%1$s/a.txt\")); IO.println(out(\"java\", \"-version\").contains(\"openjdk\")); rm(\"%1$s/a.txt\"); write(\"%1$s/tree/b.txt\", \"beta\"); rmTree(\"%1$s/tree\"); rmdir(\"%1$s\"); """.formatted(smoke); var out = run(root, "java", "scripts/mise_shell.java", snippet); if (!out.contains("alpha") || !out.contains("true")) fail("mise-shell-helpers"); if (!runExpectFail(root, Map.of(), "java", "scripts/mise_shell.java", "bad();").contains("cannot find symbol")) fail("mise-shell-errors"); ok("mise-shell"); } static List allHosts() throws IOException { var hosts = new ArrayList(); try (var s = Files.list(root)) { s.filter(p -> p.getFileName().toString().endsWith(".symlinks")) .map(p -> p.getFileName().toString().replaceFirst("\\.symlinks$", "")) .sorted().forEach(hosts::add); } return hosts; } static void runManifest(String host) throws Exception { var manifest = root.resolve(host + ".symlinks"); if (!Files.exists(manifest)) fail("missing-manifest:" + manifest.getFileName()); var home = Path.of(System.getProperty("user.home"), "home-" + host); System.out.println("manifest=" + manifest.getFileName()); deleteTree(home); Files.createDirectories(home); var env = Map.of("HOSTNAME", host, "JAVA_TOOL_OPTIONS", "-Duser.home=" + home); run(root, env, "mise", "run", "link"); ok("link:" + host); run(root, env, "mise", "run", "check"); ok("check:" + host); forEachMapping(manifest, (source, target) -> { var path = targetPath(home, target); if (!Files.isSymbolicLink(path)) throw new RuntimeException("FAIL:link:" + host + ":" + target); }); ok("links:" + host); run(root, env, "mise", "run", "clean"); ok("clean:" + host); } static void freshCryptoSmoke() throws Exception { var home = Path.of(System.getProperty("user.home"), "fresh-crypto-home"); var repo = home.resolve("repo"); deleteTree(home); Files.createDirectories(repo.resolve("tools")); Files.createDirectories(home); Files.copy(root.resolve("bootstripper.java"), repo.resolve("bootstripper.java")); Files.copy(root.resolve("tools/aes.java"), repo.resolve("tools/aes.java")); Files.writeString(repo.resolve(".secrets"), "secret.txt\n"); Files.writeString(repo.resolve("mini.symlinks"), "dummy.txt\n~/dummy.txt\n"); Files.writeString(repo.resolve("secret.txt"), "alpha=base\nmiddle=stable\nomega=base\n"); Files.writeString(repo.resolve("dummy.txt"), "dummy\n"); run(repo, "git", "init", "--quiet"); run(repo, "git", "config", "user.name", "Dotfiles Test"); run(repo, "git", "config", "user.email", "dotfiles@example.invalid"); var env = Map.of("DOTFILES_AES_PASSPHRASE", "test", "JAVA_TOOL_OPTIONS", "-Duser.home=" + home); run(repo, env, "java", "bootstripper.java", "init-crypto"); run(repo, env, "java", "bootstripper.java", "check"); run(repo, env, "git", "add", ".dotfiles-aes-salt", ".secrets", "bootstripper.java", "tools/aes.java", "mini.symlinks", "dummy.txt", "secret.txt"); run(repo, env, "java", "bootstripper.java", "check"); if (!run(repo, "git", "show", ":secret.txt").startsWith("DFAES1")) fail("fresh-staged-encrypted"); run(repo, "git", "commit", "--quiet", "-m", "base"); var base = run(repo, "git", "rev-parse", "HEAD").trim(); run(repo, "git", "switch", "--quiet", "-c", "secret-other"); Files.writeString(repo.resolve("secret.txt"), "alpha=other\nmiddle=stable\nomega=base\n"); run(repo, "git", "add", "secret.txt"); run(repo, "git", "commit", "--quiet", "-m", "other secret change"); run(repo, "git", "switch", "--quiet", "-c", "secret-current", base); Files.writeString(repo.resolve("secret.txt"), "alpha=base\nmiddle=stable\nomega=current\n"); run(repo, "git", "add", "secret.txt"); run(repo, "git", "commit", "--quiet", "-m", "current secret change"); run(repo, "git", "merge", "--quiet", "--no-edit", "secret-other"); var merged = Files.readString(repo.resolve("secret.txt")); if (!merged.contains("alpha=other") || !merged.contains("omega=current") || merged.startsWith("DFAES1")) fail("secret-clean-merge"); if (!run(repo, "git", "show", "HEAD:secret.txt").startsWith("DFAES1")) fail("secret-merge-committed-encrypted"); ok("secret-clean-merge"); var conflictBase = run(repo, "git", "rev-parse", "HEAD").trim(); run(repo, "git", "switch", "--quiet", "-c", "secret-conflict-other"); Files.writeString(repo.resolve("secret.txt"), "alpha=conflict-other\nmiddle=stable\nomega=current\n"); run(repo, "git", "add", "secret.txt"); run(repo, "git", "commit", "--quiet", "-m", "other conflicting secret change"); run(repo, "git", "switch", "--quiet", "-c", "secret-conflict-current", conflictBase); Files.writeString(repo.resolve("secret.txt"), "alpha=conflict-current\nmiddle=stable\nomega=current\n"); run(repo, "git", "add", "secret.txt"); run(repo, "git", "commit", "--quiet", "-m", "current conflicting secret change"); runExpectFail(repo, env, "git", "merge", "--quiet", "--no-edit", "secret-conflict-other"); var conflicted = Files.readString(repo.resolve("secret.txt")); if (!conflicted.contains("<<<<<<< current") || !conflicted.contains("alpha=conflict-current") || !conflicted.contains("alpha=conflict-other") || conflicted.startsWith("DFAES1")) fail("secret-conflict-plaintext"); run(repo, "git", "merge", "--abort"); ok("secret-conflict-plaintext"); run(repo, env, "java", "tools/aes.java", "encrypt", "--key-file", ".git/dotfiles-aes.key", "secret.txt", "encrypted-secret.txt"); Files.write(repo.resolve("secret.txt"), Files.readAllBytes(repo.resolve("encrypted-secret.txt"))); Files.delete(repo.resolve("encrypted-secret.txt")); if (!runExpectFail(repo, env, "git", "add", "secret.txt").contains("input is already a dotfiles AES blob")) fail("encrypted-worktree-clean-not-rejected"); Files.writeString(repo.resolve("secret.txt"), "not a real secret\n"); var wslHome = home.resolve("fresh-wsl-home"); Files.createDirectories(wslHome); var wslEnv = new HashMap<>(env); wslEnv.put("JAVA_TOOL_OPTIONS", "-Duser.home=" + wslHome); wslEnv.put("COMPUTERNAME", ""); wslEnv.put("WSL_DISTRO_NAME", "mini"); wslEnv.put("HOSTNAME", "ignored"); run(repo, wslEnv, "java", "bootstripper.java", "bootstrap"); if (!Files.isSymbolicLink(wslHome.resolve("dummy.txt"))) fail("fresh-wsl-distro-manifest"); run(repo, wslEnv, "java", "bootstripper.java", "clean"); var env2 = new HashMap<>(env); env2.put("HOSTNAME", "mini"); Files.createSymbolicLink(home.resolve("dummy.txt"), Path.of("/usr/lib/outside-home")); if (!runExpectFail(repo, env2, "java", "bootstripper.java", "bootstrap").contains("outside HOME")) fail("outside-home-symlink-not-rejected"); Files.delete(home.resolve("dummy.txt")); run(repo, env2, "java", "bootstripper.java", "bootstrap"); if (!Files.isSymbolicLink(home.resolve("dummy.txt"))) fail("fresh-link"); run(repo, env2, "java", "bootstripper.java", "clean"); ok("fresh-crypto"); } interface Mapping { void accept(String source, String target) throws Exception; } static void forEachMapping(Path manifest, Mapping mapping) throws Exception { String source = null; for (var raw : Files.readAllLines(manifest)) { var line = raw.strip(); if (line.isEmpty()) continue; if (source == null) source = line; else { mapping.accept(source, line); source = null; } } if (source != null) fail("missing target after " + source); } static Path targetPath(Path home, String target) { return target.startsWith("~/") ? home.resolve(target.substring(2)) : Path.of(target); } static String rootless() throws IOException { var p = Path.of("/proc/self/uid_map"); if (!Files.exists(p)) return "unknown"; var parts = Files.readString(p).trim().split("\\s+"); return parts.length >= 2 && parts[0].equals("0") && !parts[1].equals("0") ? "yes" : "unknown"; } static void deleteTree(Path p) throws IOException { if (!Files.exists(p)) return; try (var s = Files.walk(p)) { for (var x : s.sorted(Comparator.reverseOrder()).toList()) Files.deleteIfExists(x); } } static void step(String what) { log("step:" + what); } static void ok(String what) { log("ok:" + what); } static void log(String message) { System.out.println(message); System.out.flush(); } static void fail(String what) { throw new RuntimeException("FAIL:" + what); } static String run(Path dir, String... cmd) throws Exception { return run(dir, Map.of(), cmd); } static String run(Path dir, Map env, String... cmd) throws Exception { var pb = new ProcessBuilder(cmd); if (dir != null) pb.directory(dir.toFile()); pb.environment().putAll(env); var p = pb.redirectErrorStream(true).start(); var out = new String(p.getInputStream().readAllBytes()); var code = p.waitFor(); if (code != 0) throw new RuntimeException(String.join(" ", cmd) + "\n" + out); return out; } static void runInherit(Path dir, String... cmd) throws Exception { var pb = new ProcessBuilder(cmd); if (dir != null) pb.directory(dir.toFile()); var code = pb.inheritIO().start().waitFor(); if (code != 0) throw new RuntimeException(String.join(" ", cmd) + " exited " + code); } static String runExpectFail(Path dir, Map env, String... cmd) throws Exception { try { var out = run(dir, env, cmd); throw new AssertionError("expected failure: " + String.join(" ", cmd) + "\n" + out); } catch (RuntimeException error) { return error.getMessage(); } } static void runAllowFail(Path dir, String... cmd) { try { run(dir, cmd); } catch (Exception ignored) {} } }