-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathtool-utils_test.go
More file actions
154 lines (123 loc) · 4.48 KB
/
tool-utils_test.go
File metadata and controls
154 lines (123 loc) · 4.48 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
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)
// 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)
}
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)
// 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)
}