-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroot_test.go
113 lines (95 loc) · 2.77 KB
/
root_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
package cmd
import (
"os"
"testing"
"github.com/cacoco/codemetagenerator/internal/utils"
"github.com/ohler55/ojg/oj"
"github.com/onsi/gomega"
"golang.org/x/exp/maps"
)
func TestGetLicenseReference(t *testing.T) {
g := gomega.NewWithT(t)
temp := t.TempDir()
// setup
os.Mkdir(utils.GetHomeDir(temp), 0755)
file, le := os.ReadFile("../testdata/spdx-licenses.json")
if le != nil {
t.Errorf("Unexpected error: %v", le)
}
we := utils.WriteFile(utils.GetLicensesFilePath(temp), file)
if we != nil {
t.Errorf("Unexpected error: %v", we)
}
writer := utils.TestWriter{}
reference, err := getLicenseReference(writer, temp, "Apache-2.0")
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
g.Ω(*reference).Should(gomega.Equal("https://spdx.org/licenses/Apache-2.0.html"))
}
func reset() {
SupportedLicenses = Licenses{}
}
func TestValidateLicenseId1(t *testing.T) {
g := gomega.NewWithT(t)
temp := t.TempDir()
// setup
os.Mkdir(utils.GetHomeDir(temp), 0755)
file, err := os.ReadFile("../testdata/spdx-licenses.json")
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
err = utils.WriteFile(utils.GetLicensesFilePath(temp), file)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
supported, err := utils.GetSupportedLicenses(temp)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
SupportedLicenses.setSupportedLicenses(*supported)
defer reset() // make sure to reset the global variable
writer := utils.TestWriter{}
var validateFn = validateLicenseId(writer, temp)
err = validateFn("Apache-2.0")
g.Expect(err).To(gomega.BeNil())
}
func TestValidateLicenseId2(t *testing.T) {
g := gomega.NewWithT(t)
temp := t.TempDir()
writer := utils.TestWriter{}
// SupportedLicenses is nil, should error
var validateFn = validateLicenseId(writer, temp)
err := validateFn("Apache-2.0")
g.Expect(err).ToNot(gomega.BeNil())
}
func TestLoadSupportedLicenses(t *testing.T) {
g := gomega.NewWithT(t)
temp := t.TempDir()
// setup
os.Mkdir(utils.GetHomeDir(temp), 0755)
file, err := os.ReadFile("../testdata/spdx-full-licenses.json")
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
var stack utils.Stack[string]
stack.Push(string(file))
httpClient := utils.NewTestHttpClient(&stack)
actual, err := loadSupportedLicenses(temp, httpClient)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
// read converted test file
var expectedMap map[string]string = make(map[string]string)
file, err = os.ReadFile("../testdata/spdx-licenses.json")
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
oj.Unmarshal(file, &expectedMap)
expected := maps.Keys(expectedMap)
// check "downloaded" file against converted test file
g.Ω(*actual).Should(gomega.ConsistOf(expected))
}