-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
108 lines (98 loc) · 2.72 KB
/
main_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
package main
import (
"fmt"
"testing"
"github.com/stretchr/testify/require"
"github.com/tateexon/go-change-delta/golang"
"github.com/tateexon/go-change-delta/utils"
)
func verifySliceItemsPresent(t *testing.T, expected, actual []string) {
require.Equal(t, len(expected), len(actual), fmt.Sprintf("Expected: %+v\nActual: %+v\n", expected, actual))
for _, item := range expected {
require.Contains(t, actual, item)
}
}
func TestSetConfig(t *testing.T) {
config := setConfig(utils.Ptr("main"), utils.Ptr("abc"), utils.Ptr("abc,123"), utils.Ptr(1), utils.Ptr(true))
require.NotNil(t, config)
require.Equal(t, "main", config.Branch)
require.Equal(t, "abc", config.ProjectPath)
require.Equal(t, 2, len(config.Excludes))
require.Equal(t, "abc", config.Excludes[0])
require.Equal(t, "123", config.Excludes[1])
require.Equal(t, 1, config.Levels)
require.True(t, config.IncludeTestDeps)
}
func TestFindAllAffectedPackages(t *testing.T) {
config := &Config{
Branch: "main",
ProjectPath: "",
Excludes: []string{},
Levels: 0,
IncludeTestDeps: true,
}
packages := []golang.Package{
{
Dir: "/User/t/git/go-change-delta/test",
ImportPath: "github.com/tateexon/go-change-delta/test",
Root: "/User/t/git/go-change-delta",
Deps: []string{
"bytes",
},
TestImports: []string{
"unicode",
},
GoFiles: []string{
"test.go",
},
TestGoFiles: []string{
"cmd_test.go",
},
EmbedFiles: []string{
"testdata/blarg.json",
},
},
{
Dir: "/User/t/git/go-change-delta/two",
ImportPath: "github.com/tateexon/go-change-delta/two",
Root: "/User/t/git/go-change-delta",
Deps: []string{
"github.com/tateexon/go-change-delta/test",
},
TestImports: []string{
"fmt",
},
GoFiles: []string{
"test.go",
},
TestGoFiles: []string{
"cmd_test.go",
},
EmbedFiles: []string{
"testdata/blarg.json",
},
},
}
depMap := golang.GetGoDepMap(packages)
t.Run("only changed files", func(t *testing.T) {
changedPackages := []string{
"github.com/tateexon/go-change-delta/test",
}
changedModPackages := []string{}
pkgs := findAllAffectedPackages(config, changedPackages, changedModPackages, depMap)
require.Equal(t, 2, len(pkgs))
verifySliceItemsPresent(t, []string{
"github.com/tateexon/go-change-delta/test",
"github.com/tateexon/go-change-delta/two",
}, pkgs)
})
t.Run("only changed go mod file", func(t *testing.T) {
changedPackages := []string{}
changedModPackages := []string{
"fmt",
}
pkgs := findAllAffectedPackages(config, changedPackages, changedModPackages, depMap)
require.Equal(t, 1, len(pkgs))
require.Equal(t, "github.com/tateexon/go-change-delta/two", pkgs[0])
})
}