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
|
//go:build integration
// +build integration
package main
import (
"bytes"
_ "embed"
"fmt"
"os"
"os/exec"
"strings"
"testing"
"github.com/alecthomas/assert/v2"
)
//go:embed testdata/package.nix
var packageNix []byte
//go:embed testdata/configuration.nix
var configurationNix []byte
func TestDiffNixPackage(t *testing.T) {
tempDir := t.TempDir()
os.WriteFile(tempDir+"/default.nix", packageNix, 0644)
runIn(tempDir, "nix-build", "--argstr", "version", "1.2.3")
copySymlink(tempDir+"/result", tempDir+"/old-result")
runIn(tempDir, "nix-build", "--argstr", "version", "1.2.4")
var sb strings.Builder
innerMain(&sb, tempDir+"/old-result", tempDir+"/result")
assert.Equal(t, sb.String(), strings.TrimLeft(`
Version changed:
test-dependency: 1.2.3 -> 1.2.4
test-indirect-dependency: 1.2.3 -> 1.2.4
`, "\n"))
}
func TestDiffNixPackageDrv(t *testing.T) {
tempDir := t.TempDir()
os.WriteFile(tempDir+"/default.nix", packageNix, 0644)
runIn(tempDir, "nix-build", "--argstr", "version", "1.2.3")
copySymlink(tempDir+"/result", tempDir+"/old-result")
runIn(tempDir, "nix-build", "--argstr", "version", "1.2.4")
out, _ := runIn(tempDir, "nix-store", "--query", "--deriver", "old-result", "result")
lines := strings.Split(out, "\n")
var sb strings.Builder
innerMain(&sb, lines[0], lines[1])
assert.Equal(t, sb.String(), strings.TrimLeft(`
Version changed:
test-dependency: 1.2.3 -> 1.2.4
test-indirect-dependency: 1.2.3 -> 1.2.4
`, "\n"))
}
func TestDiffNixSystem(t *testing.T) {
tempDir := t.TempDir()
os.WriteFile(tempDir+"/package.nix", packageNix, 0644)
os.WriteFile(tempDir+"/configuration.nix", configurationNix, 0644)
os.WriteFile(tempDir+"/version.nix", []byte("\"1.2.3\""), 0644)
runIn(tempDir, "nixos-rebuild", "build", "-I", "nixos-config=configuration.nix", "-I", "pkg-version=./version.nix")
err := copySymlink(tempDir+"/result", tempDir+"/old-result")
assert.NoError(t, err)
os.WriteFile(tempDir+"/version.nix", []byte("\"1.2.4\""), 0644)
runIn(tempDir, "nixos-rebuild", "build", "-I", "nixos-config=configuration.nix", "-I", "pkg-version=./version.nix")
var sb strings.Builder
innerMain(&sb, tempDir+"/old-result", tempDir+"/result")
assert.Equal(t, sb.String(), strings.TrimLeft(`
Version changed:
test-package: 1.2.3 -> 1.2.4
user-package: 1.2.3 -> 1.2.4
test-dependency: 1.2.3 -> 1.2.4
test-indirect-dependency: 1.2.3 -> 1.2.4
`, "\n"))
}
func runIn(dir string, name string, args ...string) (string, error) {
cmd := exec.Command(name, args...)
cmd.Dir = dir
var stderr bytes.Buffer
cmd.Stderr = &stderr
out, err := cmd.Output()
if err != nil {
return "", fmt.Errorf("%s failed: %v: %s", name, err, stderr.String())
}
return string(out), nil
}
func copySymlink(src, dst string) error {
target, err := os.Readlink(src)
if err != nil {
return err
}
return os.Symlink(target, dst)
}
|