diff options
Diffstat (limited to 'main_test.go')
-rw-r--r-- | main_test.go | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..438ce7f --- /dev/null +++ b/main_test.go @@ -0,0 +1,110 @@ +package main + +import ( + "fmt" + "strings" + "testing" + + "github.com/alecthomas/assert/v2" +) + +var prefix = "/nix/store/x2kkw05xdx9g0aqvgl5lnnypq9fvv2sn-" + +func TestParseStorePathWithoutOutput(t *testing.T) { + path := prefix + "some-name-01.23.45" + out, err := parseStorePath(outputNameRe, path) + assert.NoError(t, err) + assert.Equal(t, out, &Dependency{ + Name: "some-name", + Version: "01.23.45", + Output: "", + Path: path, + }) +} + +func TestParseStorePathWithOutput(t *testing.T) { + path := prefix + "some-name-01.23.45-some123output" + out, err := parseStorePath(outputNameRe, path) + assert.NoError(t, err) + assert.Equal(t, out, &Dependency{ + Name: "some-name", + Version: "01.23.45", + Output: "some123output", + Path: path, + }) +} + +func TestParseStorePathNoName(t *testing.T) { + path := "/nix/store/l7rjijvn6vx8njaf95vviw5krn3i9nnx" + _, err := parseStorePath(nil, path) + assert.Equal(t, err, fmt.Errorf("path %s did not start with digest", path)) +} + +func versionChange(name string, output string, oldVersions, newVersions []string) VersionChange { + return VersionChange{ + Name: name, + Output: output, + OldVersions: mapSlice(oldVersions, func(v string) Version { + return Version{v, []string{fmt.Sprintf("%s-%s-%s", name, v, output)}} + }), + NewVersions: mapSlice(newVersions, func(v string) Version { + return Version{v, []string{fmt.Sprintf("%s-%s-%s", name, v, output)}} + }), + } +} + +func TestPrintChanges(t *testing.T) { + added := []Dependency{ + { + Name: "new-foo", + Version: "1.2.3", + Output: "", + Path: "/nix/store/digest-new-foo-1.2.3", + }, + { + Name: "new-bar", + Version: "1.2.3", + Output: "some-output", + Path: "/nix/store/digest-new-bar-1.2.3", + }, + } + removed := []Dependency{ + { + Name: "removed-foo", + Version: "1.2.3", + Output: "", + Path: "/nix/store/digest-removed-foo-1.2.3", + }, + { + Name: "removed-bar", + Version: "1.2.3", + Output: "some-output", + Path: "/nix/store/digest-removed-bar-1.2.3", + }, + } + changed := []VersionChange{ + versionChange("changed-foo", "", []string{"1.2.3"}, []string{"1.2.4"}), + versionChange("changed-foo", "man", []string{"1.2.3"}, []string{"1.2.4"}), // will be omitted + versionChange("changed-foo", "bar", []string{"1.2.3"}, []string{"1.2.5"}), + versionChange("changed-bar", "some-output", []string{"1.2.3"}, []string{"1.2.4"}), + } + + var sb strings.Builder + printChanges(&sb, added, removed, changed, map[string]bool{"changed-bar-1.2.4-some-output": true}) + + assert.Equal(t, sb.String(), strings.TrimLeft(` +Version changed: + changed-bar(some-output): 1.2.3 -> 1.2.4 + + changed-foo: 1.2.3 -> 1.2.4 + changed-foo(bar): 1.2.3 -> 1.2.5 + +Added packages: + new-bar(some-output) 1.2.3 + new-foo 1.2.3 + +Removed packages: + removed-bar(some-output) 1.2.3 + removed-foo 1.2.3 +`, "\n")) +} |