-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathanalyze_internal_test.go
131 lines (99 loc) · 2.86 KB
/
analyze_internal_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
125
126
127
128
129
130
131
package k6deps
import (
"testing"
"github.com/stretchr/testify/require"
)
func Test_empty(t *testing.T) {
t.Parallel()
inst, err := empty()
require.NoError(t, err)
require.Empty(t, inst)
}
func Test_filterInvalid(t *testing.T) {
t.Parallel()
deps := Dependencies{
"k6": &Dependency{Name: "k6"},
"foo": &Dependency{Name: "foo"},
"bar": &Dependency{Name: "bar"},
"k6/x/faker": &Dependency{Name: "k6/x/faker"},
"xk6-foo": &Dependency{Name: "xk6-foo"},
"@grafana/xk6-bar": &Dependency{Name: "@grafana/xk6-bar"},
}
valid := filterInvalid(deps)
require.Len(t, valid, 4)
require.Contains(t, valid, "k6")
require.Contains(t, valid, "k6/x/faker")
require.Contains(t, valid, "xk6-foo")
require.Contains(t, valid, "@grafana/xk6-bar")
}
func Test_manifestAnalyzer(t *testing.T) {
t.Parallel()
src := Source{Name: "package.json"}
fn := manifestAnalyzer(src)
deps, err := fn()
require.NoError(t, err)
require.Empty(t, deps)
src.Contents = []byte(`{"dependencies":{"@grafana/xk6-faker":"*"}}`)
fn = manifestAnalyzer(src)
deps, err = fn()
require.NoError(t, err)
require.NotEmpty(t, deps)
require.Len(t, deps, 1)
require.Equal(t, deps["@grafana/xk6-faker"].Constraints.String(), "*")
src.Contents = []byte(`{`)
fn = manifestAnalyzer(src)
_, err = fn()
require.Error(t, err)
}
func Test_scriptAnalyzer(t *testing.T) {
t.Parallel()
src := Source{Name: "script.js"}
fn := scriptAnalyzer(src)
deps, err := fn()
require.NoError(t, err)
require.Empty(t, deps)
src.Contents = []byte(`"use k6 with @grafana/xk6-faker>v0.3.0";`)
fn = scriptAnalyzer(src)
deps, err = fn()
require.NoError(t, err)
require.NotEmpty(t, deps)
require.Len(t, deps, 1)
require.Equal(t, deps["@grafana/xk6-faker"].Constraints.String(), ">v0.3.0")
src.Contents = []byte(`"use k6 with k6/x/faker>>1.0";`)
fn = scriptAnalyzer(src)
_, err = fn()
require.Error(t, err)
}
func Test_envAnalyzer(t *testing.T) {
t.Parallel()
src := Source{Name: "DEPS"}
fn := envAnalyzer(src)
deps, err := fn()
require.NoError(t, err)
require.Empty(t, deps)
src.Contents = []byte(`@grafana/xk6-faker>v0.3.0`)
fn = envAnalyzer(src)
deps, err = fn()
require.NoError(t, err)
require.NotEmpty(t, deps)
require.Len(t, deps, 1)
require.Equal(t, deps["@grafana/xk6-faker"].Constraints.String(), ">v0.3.0")
src.Contents = []byte(`k6/x/faker>>1.0`)
fn = envAnalyzer(src)
_, err = fn()
require.Error(t, err)
}
func Test_mergeAnalyzers_error(t *testing.T) {
t.Parallel()
src := Source{Name: "DEPS"}
src.Contents = []byte(`@grafana/xk6-faker>v0.3.0`)
fn := envAnalyzer(src)
src.Contents = []byte(`@grafana/xk6-foo>>v0.4.0`)
fn2 := envAnalyzer(src)
_, err := mergeAnalyzers(fn, fn2)()
require.Error(t, err)
src.Contents = []byte(`@grafana/xk6-faker>v0.4.0`)
fn2 = envAnalyzer(src)
_, err = mergeAnalyzers(fn, fn2)()
require.Error(t, err)
}