-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
124 lines (105 loc) · 2.6 KB
/
main_test.go
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package main
import (
"bytes"
_ "embed"
"encoding/json"
"io"
"os"
"reflect"
"testing"
)
//go:embed testdata/pip-inspect.golden
var inspectGold []byte
func TestPipInspect(t *testing.T) {
t.Run("decodes pip inspect output", func(t *testing.T) {
pi := new(PipInspection)
dec := json.NewDecoder(bytes.NewReader(inspectGold))
if err := dec.Decode(pi); err != nil {
t.Error(err)
}
const want = "starlette"
got := pi.Installed[0].Metadata.Name
if got != want {
t.Errorf("got %v want %v", got, want)
}
})
}
func TestSavePkgs(t *testing.T) {
pi := new(PipInspection)
dec := json.NewDecoder(bytes.NewReader(inspectGold))
if err := dec.Decode(pi); err != nil {
t.Error(err)
}
t.Run("saves installed package", func(t *testing.T) {
f, err := os.CreateTemp("", "_pips_test")
if err != nil {
t.Fatal(err)
}
defer os.Remove(f.Name())
err = savePkgs(f.Name(), pi, map[string]string{"starlette": ""})
if err != nil {
t.Fatal(err)
}
buf, err := io.ReadAll(f)
if err != nil {
t.Fatal(err)
}
got := string(buf)
want := "starlette==0.25.0\n"
if got != want {
t.Errorf("got %v want %v", got, want)
}
})
t.Run("saves installed package with extras", func(t *testing.T) {
f, err := os.CreateTemp("", "_pips_test")
if err != nil {
t.Fatal(err)
}
defer os.Remove(f.Name())
err = savePkgs(f.Name(), pi, map[string]string{"starlette": "[full]"})
if err != nil {
t.Fatal(err)
}
buf, err := io.ReadAll(f)
if err != nil {
t.Fatal(err)
}
got := string(buf)
want := "starlette[full]==0.25.0\n"
if got != want {
t.Errorf("got %v want %v", got, want)
}
})
t.Run("errors if package is not installed", func(t *testing.T) {
f, err := os.CreateTemp("", "_pips_test")
if err != nil {
t.Fatal(err)
}
defer os.Remove(f.Name())
err = savePkgs(f.Name(), pi, map[string]string{"ezekiel": ""})
if err == nil {
t.Error("wanted error, got nil")
}
})
}
func TestParseSpecifier(t *testing.T) {
tests := []struct {
name string
input string
want []string
}{
{"parses plain name", "loki", []string{"loki", "", ""}},
{"parses name with extras", "loki[doc,security]", []string{"loki", "[doc,security]", ""}},
{"parses name with version", "loki==0.0.1", []string{"loki", "", "0.0.1"}},
{"parses name with extras and version", "loki[doc,security]==0.0.1", []string{"loki", "[doc,security]", "0.0.1"}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
n, e, v := parseSpecifier(tt.input)
got := []string{n, e, v}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("got %#v want %#v", got, tt.want)
}
})
}
}