diff --git a/cmd/restree/helpers_test.go b/cmd/restree/helpers_test.go new file mode 100644 index 0000000..2c2c4b5 --- /dev/null +++ b/cmd/restree/helpers_test.go @@ -0,0 +1,29 @@ +package main + +import ( + "reflect" + "testing" +) + +func TestFilterSnapshots(t *testing.T) { + snapshots := []snapshot{ + {ID: "alpha", Hostname: "laptop", Tags: []string{"daily"}}, + {ID: "beta", Tags: []string{"weekly"}}, + {ID: "gamma", Hostname: "server"}, + } + + if got, want := filterSnapshots(snapshots, "laptop", allTagsLabel), snapshots[:1]; !reflect.DeepEqual(got, want) { + t.Fatalf("host filter = %#v, want %#v", got, want) + } + if got, want := filterSnapshots(snapshots, allHostsLabel, noTagLabel), snapshots[2:]; !reflect.DeepEqual(got, want) { + t.Fatalf("no-tag filter = %#v, want %#v", got, want) + } +} + +func TestTrimNestedPaths(t *testing.T) { + paths := []string{"/photos/2025", "/documents", "/photos", "/documents/work", "/music"} + want := []string{"/music", "/photos", "/documents"} + if got := trimNestedPaths(paths); !reflect.DeepEqual(got, want) { + t.Fatalf("trimNestedPaths() = %#v, want %#v", got, want) + } +} diff --git a/cmd/restree/restic_test.go b/cmd/restree/restic_test.go new file mode 100644 index 0000000..165acf8 --- /dev/null +++ b/cmd/restree/restic_test.go @@ -0,0 +1,33 @@ +package main + +import ( + "reflect" + "testing" +) + +func TestParseLSEntries(t *testing.T) { + out := []byte("{\"name\":\"documents\",\"path\":\"/documents\",\"type\":\"dir\"}\n{\"name\":\"notes.txt\",\"path\":\"/documents/notes.txt\",\"type\":\"file\",\"size\":42}\n") + want := []lsEntry{ + {Name: "documents", Path: "/documents", Type: "dir"}, + {Name: "notes.txt", Path: "/documents/notes.txt", Type: "file", Size: 42}, + } + got, err := parseLSEntries(out) + if err != nil || !reflect.DeepEqual(got, want) { + t.Fatalf("parseLSEntries() = %#v, %v; want %#v, nil", got, err, want) + } + if _, err := parseLSEntries([]byte("not json")); err == nil { + t.Fatal("expected malformed ls output to fail") + } +} + +func TestParseFindEntries(t *testing.T) { + out := []byte("[{\"matches\":[{\"path\":\"/documents/a.txt\",\"type\":\"file\"}]},{\"matches\":[{\"path\":\"/photos\",\"type\":\"dir\"}]}]") + want := []lsEntry{ + {Path: "/documents/a.txt", Type: "file"}, + {Path: "/photos", Type: "dir"}, + } + got, err := parseFindEntries(out) + if err != nil || !reflect.DeepEqual(got, want) { + t.Fatalf("parseFindEntries() = %#v, %v; want %#v, nil", got, err, want) + } +} diff --git a/justfile b/justfile index 23fc290..fb6c094 100644 --- a/justfile +++ b/justfile @@ -115,11 +115,21 @@ run: just build "{{bin}}/{{app}}-{{version}}-$(go env GOOS)-$(go env GOARCH)" +check: + just lint + just test + lint: goimports -w {{pkg}} go vet ./{{pkg}} staticcheck ./{{pkg}} +test: + go test ./... + +test-race: + go test -race ./... + bump new: #!/usr/bin/env bash set -euo pipefail