summaryrefslogtreecommitdiff
path: root/main_test.go
blob: 438ce7f2b6ec625312fdce6a064a791a07bf772d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
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"))
}