package fstree import ( "os" "path/filepath" "strings" "testing" "time" ) func TestFstreeRoundTrip(t *testing.T) { old := time.Date(2020, 1, 2, 3, 4, 5, 0, time.UTC) spec := Spec{ File("a.txt", "hello").WithMTime(old), File("bin/run.sh", "#!/bin/sh\n").WithMode(0o755), Dir("ro").WithMode(0o555).WithMTime(old), File("ro/inside.bin", "").WithSize(100000), Symlink("link", "a.txt"), Symlink("broken", "/nonexistent/zzz"), FIFO("pipe"), Dir("empty"), } root := t.TempDir() Build(t, root, spec) st, err := os.Stat(filepath.Join(root, "ro")) if err != nil || st.Mode().Perm() != 0o555 { t.Fatalf("ro dir: %v %v", st, err) } got := Snapshot(t, root) if got["ro/inside.bin"].Kind != KindFile || len(got["ro/inside.bin"].Data) != 100000 { t.Fatalf("sized file not built: %+v", got["ro/inside.bin"].Kind) } Equal(t, FromSpec(spec), got, Opts{Mode: true, MTime: true, Links: true}) // A second build of the same spec is equal, including mode and mtime. root2 := t.TempDir() Build(t, root2, spec) Equal(t, got, Snapshot(t, root2), Opts{Mode: true, Links: true}) } func TestFstreeDiffReportsDifferences(t *testing.T) { want := FromSpec(Spec{ File("a", "x"), File("b", "y").WithMode(0o600), Symlink("l", "a"), Dir("d"), }) got := FromSpec(Spec{ File("a", "z"), File("b", "y").WithMode(0o644), Symlink("l", "b"), File("d", ""), File("extra", ""), }) d := Diff(want, got, Opts{Mode: true, Links: true}) joined := strings.Join(d, "\n") for _, s := range []string{"a: content", "b: mode want 0600, got 0644", "l: link target", "d: kind want dir, got file", "unexpected file extra"} { if !strings.Contains(joined, s) { t.Errorf("diff lacks %q:\n%s", s, joined) } } if d := Diff(want, got, Opts{}); strings.Contains(strings.Join(d, "\n"), "link target") { t.Errorf("Links=false must ignore symlinks: %v", d) } }