repositories / dotfiles
dotfiles
bugabingas dorkfiles
owned by bugabinga
scripts/dotfiles_integration_test.java
RawFile outline
- dotfiles_integration_test line 6
- root line 7
- main line 9
- host line 17
- installMise line 53
- inside line 67
- miseShellSmoke line 104
- allHosts line 122
- runManifest line 132
- freshCryptoChecks line 153
- freshCryptoChecks line 162
- linkOnlyChecks line 266
- cryptoRestoreChecks line 292
- Mapping line 338
- accept line 338
- forEachMapping line 339
- targetPath line 350
- rootless line 354
- deleteTree line 361
- step line 368
- ok line 369
- log line 370
- fail line 371
- run line 373
- run line 374
- runInherit line 384
- runExpectFail line 391
- runAllowFail line 400
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 if (args.length > 0 && args[0].equals("--crypto")) {
root = Path.of(run(null, "git", "rev-parse", "--show-toplevel").trim());
freshCryptoChecks();
} 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");
run(root, "mise", "run", "setup-crypto");
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");
freshCryptoChecks();
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<String> allHosts() throws IOException {
var hosts = new ArrayList<String>();
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 freshCryptoChecks() throws Exception {
var home = Files.createTempDirectory(Path.of(System.getProperty("user.home")), "dotfiles-crypto-test-");
try {
freshCryptoChecks(home);
} finally {
deleteTree(home);
}
}
static void freshCryptoChecks(Path home) throws Exception {
var repo = home.resolve("repo");
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.copy(root.resolve("mise.toml"), repo.resolve("mise.toml"));
Files.createDirectories(repo.resolve("scripts"));
Files.copy(root.resolve("scripts/mise_shell.java"), repo.resolve("scripts/mise_shell.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");
Files.writeString(repo.resolve(".git/info/attributes"), "*.bin -diff\n# BEGIN bootstripper secrets\nsecret.txt -text filter=dotfiles-aes -diff merge=dotfiles-aes\n# END bootstripper secrets\n");
var env = Map.of("DOTFILES_AES_PASSPHRASE", "test", "JAVA_TOOL_OPTIONS", "-Duser.home=" + home);
run(repo, "mise", "trust", "--yes");
linkOnlyChecks(repo, home, env);
run(repo, env, "java", "bootstripper.java", "init-crypto");
run(repo, env, "mise", "run", "setup-crypto");
run(repo, env, "java", "bootstripper.java", "check");
if (!Files.readString(repo.resolve(".gitattributes")).contains("secret.txt -text filter=dotfiles-aes -diff merge=dotfiles-aes")) fail("tracked-attributes-generated");
if (!Files.readString(repo.resolve(".git/info/attributes")).equals("*.bin -diff\n")) fail("local-attributes-migrated");
if (!runExpectFail(repo, env, "java", "bootstripper.java", "pre-commit").contains(".secrets must match the staged version")) fail("unstaged-secret-manifest-not-rejected");
run(repo, env, "git", "add", ".dotfiles-aes-salt", ".gitattributes", ".secrets", "bootstripper.java", "tools/aes.java", "mini.symlinks", "dummy.txt", "secret.txt");
run(repo, env, "java", "bootstripper.java", "pre-commit");
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();
Files.writeString(repo.resolve(".gitattributes"), Files.readString(repo.resolve(".gitattributes")) + "*.tmp -diff\n");
if (!runExpectFail(repo, env, "java", "bootstripper.java", "pre-commit").contains(".gitattributes must match the staged version")) fail("unstaged-attributes-not-rejected");
run(repo, "git", "restore", ".gitattributes");
Files.writeString(repo.resolve(".secrets"), "secret.txt\nincoming-secret.txt\n");
Files.writeString(repo.resolve("incoming-secret.txt"), "incoming=plain\n");
run(repo, env, "java", "bootstripper.java", "check");
run(repo, "git", "add", ".secrets", ".gitattributes", "incoming-secret.txt");
run(repo, env, "java", "bootstripper.java", "pre-commit");
run(repo, "git", "commit", "--quiet", "-m", "add encrypted secret");
var incoming = run(repo, "git", "rev-parse", "HEAD").trim();
run(repo, "git", "switch", "--quiet", "--detach", base);
run(repo, "git", "merge", "--quiet", "--ff-only", incoming);
if (!Files.readString(repo.resolve("incoming-secret.txt")).equals("incoming=plain\n")) fail("same-pull-secret-not-smudged");
ok("same-pull-secret-smudged");
base = incoming;
cryptoRestoreChecks(repo, env);
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.readString(repo.resolve("secret.txt")).equals("not a real secret\n")) fail("bootstrap-overwrote-local-secret");
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");
}
static void linkOnlyChecks(Path repo, Path home, Map<String, String> env) throws Exception {
var linkEnv = new HashMap<>(env);
linkEnv.put("DOTFILES_SYMLINKS", "mini");
var config = Files.readString(repo.resolve(".git/config"));
var attributes = Files.readString(repo.resolve(".git/info/attributes"));
var secret = Files.readString(repo.resolve("secret.txt"));
for (var attempt = 0; attempt < 2; attempt++) run(repo, linkEnv, "mise", "run", "link");
if (!Files.isSymbolicLink(home.resolve("dummy.txt")) || !Files.isSameFile(home.resolve("dummy.txt"), repo.resolve("dummy.txt"))) fail("link-only-target");
if (!Files.readString(repo.resolve("secret.txt")).equals(secret)) fail("link-only-secret-changed");
if (!Files.readString(repo.resolve(".git/config")).equals(config)) fail("link-only-config-changed");
if (!Files.readString(repo.resolve(".git/info/attributes")).equals(attributes)) fail("link-only-attributes-changed");
for (var path : List.of(".git/dotfiles-aes.key", ".gitattributes", ".git/hooks/pre-commit"))
if (Files.exists(repo.resolve(path))) fail("link-only-created:" + path);
Files.delete(home.resolve("dummy.txt"));
var manifest = repo.resolve("mini.symlinks");
var original = Files.readString(manifest);
for (var invalid : List.of("missing.txt\n~/missing.txt\n", "dummy.txt\n", "dummy.txt\n~/dummy.txt\n", "dummy.txt\n/etc/dotfiles-test\n")) {
Files.writeString(manifest, original + invalid);
runExpectFail(repo, linkEnv, "mise", "run", "link");
if (Files.exists(home.resolve("dummy.txt"), LinkOption.NOFOLLOW_LINKS)) fail("link-before-validation");
}
Files.writeString(manifest, original);
ok("link-only-and-manifest-validation");
}
static void cryptoRestoreChecks(Path repo, Map<String, String> env) throws Exception {
var first = repo.resolve("secret.txt");
var second = repo.resolve("incoming-secret.txt");
var firstPlain = Files.readAllBytes(first);
var secondPlain = Files.readAllBytes(second);
run(repo, env, "java", "tools/aes.java", "encrypt", "--key-file", ".git/dotfiles-aes.key", "secret.txt", "encrypted-secret.txt");
var encrypted = Files.readAllBytes(repo.resolve("encrypted-secret.txt"));
Files.delete(repo.resolve("encrypted-secret.txt"));
Files.write(first, encrypted);
var index = Files.readAllBytes(repo.resolve(".git/index"));
run(repo, env, "mise", "run", "setup-crypto");
if (!Arrays.equals(Files.readAllBytes(first), firstPlain)) fail("explicit-crypto-decryption");
if (!Arrays.equals(Files.readAllBytes(repo.resolve(".git/index")), index)) fail("crypto-changed-index");
var modified = Files.getLastModifiedTime(first);
run(repo, env, "mise", "run", "setup-crypto");
if (!Files.getLastModifiedTime(first).equals(modified)) fail("crypto-rewrote-plaintext");
for (var change : List.of("plaintext", "ciphertext", "deleted", "staged", "staged-deletion")) {
Files.write(first, encrypted);
Files.write(second, secondPlain);
switch (change) {
case "plaintext", "staged" -> Files.writeString(second, "local change\n");
case "ciphertext" -> Files.write(second, encrypted);
case "deleted" -> Files.delete(second);
case "staged-deletion" -> run(repo, "git", "rm", "--cached", "--quiet", "incoming-secret.txt");
}
if (change.equals("staged")) run(repo, "git", "add", "incoming-secret.txt");
var before = Files.exists(second) ? Files.readAllBytes(second) : null;
index = Files.readAllBytes(repo.resolve(".git/index"));
if (!runExpectFail(repo, env, "mise", "run", "setup-crypto").contains("Refusing to restore locally changed secret")) fail("crypto-accepted:" + change);
if (!Arrays.equals(Files.readAllBytes(first), encrypted)) fail("crypto-partial-restore:" + change);
if (before == null ? Files.exists(second) : !Arrays.equals(Files.readAllBytes(second), before)) fail("crypto-overwrote:" + change);
if (!Arrays.equals(Files.readAllBytes(repo.resolve(".git/index")), index)) fail("crypto-changed-index:" + change);
run(repo, "git", "reset", "--quiet", "HEAD", "--", "incoming-secret.txt");
ok("crypto-refuses:" + change);
}
var linkEnv = new HashMap<>(env);
linkEnv.put("DOTFILES_SYMLINKS", "mini");
run(repo, linkEnv, "mise", "run", "link");
if (!Arrays.equals(Files.readAllBytes(first), encrypted)) fail("link-decrypted-secret");
run(repo, linkEnv, "java", "bootstripper.java", "clean");
Files.write(first, firstPlain);
Files.write(second, secondPlain);
ok("explicit-crypto-preserves-local-changes");
}
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<String, String> 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<String, String> 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) {}
}
}