-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathcommons_test.go
129 lines (113 loc) · 3.95 KB
/
commons_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
package jas
import (
"fmt"
"os"
"path/filepath"
"testing"
jfrogappsconfig "github.com/jfrog/jfrog-apps-config/go"
clientTestUtils "github.com/jfrog/jfrog-client-go/utils/tests"
"github.com/jfrog/jfrog-cli-security/utils/jasutils"
"github.com/stretchr/testify/assert"
)
var createJFrogAppsConfigCases = []struct {
workingDirs []string
}{
{workingDirs: []string{}},
{workingDirs: []string{"working-dir"}},
{workingDirs: []string{"working-dir-1", "working-dir-2"}},
}
func TestCreateJFrogAppsConfig(t *testing.T) {
wd, err := os.Getwd()
assert.NoError(t, err)
for _, testCase := range createJFrogAppsConfigCases {
t.Run(fmt.Sprintf("%v", testCase.workingDirs), func(t *testing.T) {
jfrogAppsConfig, err := CreateJFrogAppsConfig(testCase.workingDirs)
assert.NoError(t, err)
assert.NotNil(t, jfrogAppsConfig)
if len(testCase.workingDirs) == 0 {
assert.Len(t, jfrogAppsConfig.Modules, 1)
assert.Equal(t, wd, jfrogAppsConfig.Modules[0].SourceRoot)
return
}
assert.Len(t, jfrogAppsConfig.Modules, len(testCase.workingDirs))
for i, workingDir := range testCase.workingDirs {
assert.Equal(t, filepath.Join(wd, workingDir), jfrogAppsConfig.Modules[i].SourceRoot)
}
})
}
}
func TestCreateJFrogAppsConfigWithConfig(t *testing.T) {
wd, err := os.Getwd()
assert.NoError(t, err)
chdirCallback := clientTestUtils.ChangeDirWithCallback(t, wd, "testdata")
defer chdirCallback()
jfrogAppsConfig, err := CreateJFrogAppsConfig([]string{})
assert.NoError(t, err)
assert.NotNil(t, jfrogAppsConfig)
assert.Equal(t, "1.0", jfrogAppsConfig.Version)
assert.Len(t, jfrogAppsConfig.Modules, 1)
}
func TestShouldSkipScanner(t *testing.T) {
module := jfrogappsconfig.Module{}
assert.False(t, ShouldSkipScanner(module, jasutils.IaC))
module = jfrogappsconfig.Module{ExcludeScanners: []string{"sast"}}
assert.False(t, ShouldSkipScanner(module, jasutils.IaC))
assert.True(t, ShouldSkipScanner(module, jasutils.Sast))
}
var getSourceRootsCases = []struct {
scanner *jfrogappsconfig.Scanner
}{
{scanner: nil},
{&jfrogappsconfig.Scanner{WorkingDirs: []string{"working-dir"}}},
{&jfrogappsconfig.Scanner{WorkingDirs: []string{"working-dir-1", "working-dir-2"}}},
}
func TestGetSourceRoots(t *testing.T) {
testGetSourceRoots(t, "source-root")
}
func TestGetSourceRootsEmptySourceRoot(t *testing.T) {
testGetSourceRoots(t, "")
}
func testGetSourceRoots(t *testing.T, sourceRoot string) {
sourceRoot, err := filepath.Abs(sourceRoot)
assert.NoError(t, err)
module := jfrogappsconfig.Module{SourceRoot: sourceRoot}
for _, testCase := range getSourceRootsCases {
t.Run("", func(t *testing.T) {
scanner := testCase.scanner
actualSourceRoots, err := GetSourceRoots(module, scanner)
assert.NoError(t, err)
if scanner == nil {
assert.ElementsMatch(t, []string{module.SourceRoot}, actualSourceRoots)
return
}
expectedWorkingDirs := []string{}
for _, workingDir := range scanner.WorkingDirs {
expectedWorkingDirs = append(expectedWorkingDirs, filepath.Join(module.SourceRoot, workingDir))
}
assert.ElementsMatch(t, actualSourceRoots, expectedWorkingDirs)
})
}
}
var getExcludePatternsCases = []struct {
scanner *jfrogappsconfig.Scanner
}{
{scanner: nil},
{&jfrogappsconfig.Scanner{WorkingDirs: []string{"exclude-dir"}}},
{&jfrogappsconfig.Scanner{WorkingDirs: []string{"exclude-dir-1", "exclude-dir-2"}}},
}
func TestGetExcludePatterns(t *testing.T) {
module := jfrogappsconfig.Module{ExcludePatterns: []string{"exclude-root"}}
for _, testCase := range getExcludePatternsCases {
t.Run("", func(t *testing.T) {
scanner := testCase.scanner
actualExcludePatterns := GetExcludePatterns(module, scanner)
if scanner == nil {
assert.ElementsMatch(t, module.ExcludePatterns, actualExcludePatterns)
return
}
expectedExcludePatterns := module.ExcludePatterns
expectedExcludePatterns = append(expectedExcludePatterns, scanner.ExcludePatterns...)
assert.ElementsMatch(t, actualExcludePatterns, expectedExcludePatterns)
})
}
}