-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathtool-utils_test.go
More file actions
203 lines (166 loc) · 5.61 KB
/
tool-utils_test.go
File metadata and controls
203 lines (166 loc) · 5.61 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package plugins
import (
"path/filepath"
"runtime"
"testing"
"github.com/stretchr/testify/assert"
)
func TestProcessTools(t *testing.T) {
// Create a list of tool configs for testing
configs := []ToolConfig{
{
Name: "eslint",
Version: "8.38.0",
},
}
// Define a test tool directory
toolDir := "/test/tools"
// Process the tools
toolInfos, err := ProcessTools(configs, toolDir, nil)
// Assert no errors occurred
assert.NoError(t, err, "ProcessTools should not return an error")
// Assert we have the expected tool in the results
assert.Contains(t, toolInfos, "eslint")
// Get the eslint tool info
eslintInfo := toolInfos["eslint"]
// Assert the basic tool info is correct
assert.Equal(t, "eslint", eslintInfo.Name)
assert.Equal(t, "8.38.0", eslintInfo.Version)
assert.Equal(t, "node", eslintInfo.Runtime)
// Assert the install directory is correct
expectedInstallDir := filepath.Join(toolDir, "eslint@8.38.0")
assert.Equal(t, expectedInstallDir, eslintInfo.InstallDir)
// Assert binary paths are correctly set
assert.NotNil(t, eslintInfo.Binaries)
assert.Greater(t, len(eslintInfo.Binaries), 0)
// Check if eslint binary is present
eslintBinary := filepath.Join(expectedInstallDir, "node_modules/.bin/eslint")
assert.Equal(t, eslintBinary, eslintInfo.Binaries["eslint"])
// Assert formatters are correctly set
assert.NotNil(t, eslintInfo.Formatters)
assert.Greater(t, len(eslintInfo.Formatters), 0)
assert.Equal(t, "-f @microsoft/eslint-formatter-sarif", eslintInfo.Formatters["sarif"])
// Assert output and analysis options are correctly set
assert.Equal(t, "-o", eslintInfo.OutputFlag)
assert.Equal(t, "--fix", eslintInfo.AutofixFlag)
assert.Equal(t, ".", eslintInfo.DefaultPath)
// Assert runtime binaries are correctly set
assert.Equal(t, "npm", eslintInfo.PackageManager)
assert.Equal(t, "node", eslintInfo.ExecutionBinary)
// Assert installation command templates are correctly set
assert.Equal(t, "install --prefix {{.InstallDir}} {{.PackageName}}@{{.Version}} @microsoft/eslint-formatter-sarif", eslintInfo.InstallCommand)
assert.Equal(t, "config set registry {{.Registry}}", eslintInfo.RegistryCommand)
assert.Equal(t, eslintInfo.NeedsSourceIDUpload, false)
}
func TestProcessToolsWithDownload(t *testing.T) {
// Create a list of tool configs for testing
configs := []ToolConfig{
{
Name: "trivy",
Version: "0.37.3",
},
}
// Define a test tool directory
toolDir := "/test/tools"
// Process the tools
toolInfos, err := ProcessTools(configs, toolDir, nil)
// Assert no errors occurred
assert.NoError(t, err, "ProcessTools should not return an error")
// Assert we have the expected tool in the results
assert.Contains(t, toolInfos, "trivy")
// Get the trivy tool info
trivyInfo := toolInfos["trivy"]
// Assert the basic tool info is correct
assert.Equal(t, "trivy", trivyInfo.Name)
assert.Equal(t, "0.37.3", trivyInfo.Version)
// Assert the install directory is correct
expectedInstallDir := filepath.Join(toolDir, "trivy@0.37.3")
assert.Equal(t, expectedInstallDir, trivyInfo.InstallDir)
// Assert download information is correctly set
assert.NotEmpty(t, trivyInfo.DownloadURL)
assert.NotEmpty(t, trivyInfo.FileName)
assert.NotEmpty(t, trivyInfo.Extension)
// Assert the correct file extension based on OS
if runtime.GOOS == "windows" {
assert.Equal(t, "zip", trivyInfo.Extension)
} else {
assert.Equal(t, "tar.gz", trivyInfo.Extension)
}
// Assert binary paths are correctly set
assert.NotNil(t, trivyInfo.Binaries)
assert.Greater(t, len(trivyInfo.Binaries), 0)
// Check if trivy binary is present
trivyBinary := filepath.Join(expectedInstallDir, "trivy")
assert.Equal(t, trivyBinary, trivyInfo.Binaries["trivy"])
// Verify URL components
assert.Contains(t, trivyInfo.DownloadURL, "aquasecurity/trivy/releases/download")
assert.Contains(t, trivyInfo.DownloadURL, trivyInfo.Version)
// Test OS mapping
var expectedOS string
if runtime.GOOS == "darwin" {
expectedOS = "macOS"
} else if runtime.GOOS == "linux" {
expectedOS = "Linux"
} else if runtime.GOOS == "windows" {
expectedOS = "Windows"
} else {
expectedOS = runtime.GOOS
}
assert.Contains(t, trivyInfo.DownloadURL, expectedOS)
// Test architecture mapping
var expectedArch string
if runtime.GOARCH == "386" {
expectedArch = "32bit"
} else if runtime.GOARCH == "amd64" {
expectedArch = "64bit"
} else if runtime.GOARCH == "arm" {
expectedArch = "ARM"
} else if runtime.GOARCH == "arm64" {
expectedArch = "ARM64"
} else {
expectedArch = runtime.GOARCH
}
assert.Contains(t, trivyInfo.DownloadURL, expectedArch)
assert.Equal(t, trivyInfo.NeedsSourceIDUpload, true)
}
func TestGetSupportedTools(t *testing.T) {
tests := []struct {
name string
expectedTools []string
expectedError bool
}{
{
name: "should return supported tools",
expectedTools: []string{
"eslint",
"pmd",
"pylint",
"trivy",
"dartanalyzer",
"opengrep",
"lizard",
"codacy-enigma-cli",
"revive",
},
expectedError: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
supportedTools, err := GetSupportedTools()
if tt.expectedError {
assert.Error(t, err)
return
}
assert.NoError(t, err)
assert.NotNil(t, supportedTools)
// Check that all expected tools are supported
for _, expectedTool := range tt.expectedTools {
_, exists := supportedTools[expectedTool]
assert.True(t, exists, "tool %s should be supported", expectedTool)
}
// Check that we have exactly the expected number of tools
assert.Equal(t, len(tt.expectedTools), len(supportedTools), "number of supported tools should match")
})
}
}