-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpkg_test.go
125 lines (118 loc) · 3.49 KB
/
pkg_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
package pkgboundaries_test
import (
"encoding/json"
"fmt"
"os"
"testing"
"github.com/aereal/pkgboundaries"
"github.com/aereal/pkgboundaries/internal/sets"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
)
func TestConfig_CanDepend(t *testing.T) {
cfg := &pkgboundaries.Config{
Layers: pkgboundaries.NewLayersSet(
&pkgboundaries.Layer{Name: "a", PackageNames: pkgboundaries.NewPackagesSet("pkg/1", "pkg/2")},
&pkgboundaries.Layer{Name: "b", PackageNames: pkgboundaries.NewPackagesSet("pkg/3", "pkg/4")},
&pkgboundaries.Layer{Name: "c", PackageNames: pkgboundaries.NewPackagesSet("pkg/5", "pkg/6")},
),
Rules: []*pkgboundaries.Rule{
{Layer: "a", Allowed: []string{"b"}, Denied: []string{"c"}},
},
}
type args struct {
dependantLayerName string
dependency pkgboundaries.Package
}
testCases := []struct {
name string
args args
wantEffect pkgboundaries.Decision
}{
{"ok", args{"a", "pkg/3"}, pkgboundaries.DecisionAllow},
{"ng", args{"a", "pkg/5"}, pkgboundaries.DecisionDeny},
{"ng (unknown)", args{"a", "pkg/x"}, pkgboundaries.DecisionAllow},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
got := cfg.CanDepend(tc.args.dependantLayerName, tc.args.dependency)
if got != tc.wantEffect {
t.Errorf("want=%s got=%s", tc.wantEffect, got)
}
})
}
}
func TestEffect_And(t *testing.T) {
testCases := []struct {
x pkgboundaries.Decision
y pkgboundaries.Decision
want pkgboundaries.Decision
}{
{pkgboundaries.DecisionAllow, pkgboundaries.DecisionAllow, pkgboundaries.DecisionAllow},
{pkgboundaries.DecisionAllow, pkgboundaries.DecisionDeny, pkgboundaries.DecisionDeny},
}
for _, tc := range testCases {
t.Run(fmt.Sprintf("x=%s y=%s", tc.x, tc.y), func(t *testing.T) {
got := tc.x.And(tc.y)
if got != tc.want {
t.Errorf("got=%s want=%s", got, tc.want)
}
})
}
}
func TestConfig_Marshaling(t *testing.T) {
f, err := os.Open("./testdata/config.json")
if err != nil {
t.Fatal(err)
}
defer f.Close()
var fromData pkgboundaries.Config
if err := json.NewDecoder(f).Decode(&fromData); err != nil {
t.Fatal(err)
}
wantBytes, err := json.MarshalIndent(fromData, "", " ")
if err != nil {
t.Fatal(err)
}
want := &pkgboundaries.Config{
Rules: []*pkgboundaries.Rule{
{
Layer: "App",
Allowed: []string{"Errors"},
Denied: []string{"Print", "Encoding"},
},
},
Layers: pkgboundaries.NewLayersSet(
&pkgboundaries.Layer{
Name: "App",
PackageNames: pkgboundaries.NewPackagesSet("github.com/aereal/a"),
},
&pkgboundaries.Layer{
Name: "Errors",
PackageNames: pkgboundaries.NewPackagesSet("errors"),
},
&pkgboundaries.Layer{
Name: "Print",
PackageNames: pkgboundaries.NewPackagesSet("fmt", "log"),
},
&pkgboundaries.Layer{
Name: "Encoding",
PackageNamePatterns: pkgboundaries.NewPackagePatternSet(pkgboundaries.PackagePattern("^encoding/")),
},
),
}
marshaled, err := json.MarshalIndent(want, "", " ")
if err != nil {
t.Fatal(err)
}
if diff := cmp.Diff(string(wantBytes), string(marshaled)); diff != "" {
t.Errorf("json.Marshal (-want, +got):\n%s", diff)
}
var got pkgboundaries.Config
if err := json.Unmarshal(marshaled, &got); err != nil {
t.Fatal(err)
}
if diff := cmp.Diff(want, &got, cmpopts.IgnoreUnexported(sets.OrderedSet[pkgboundaries.Package]{}, sets.OrderedSet[*pkgboundaries.Layer]{})); diff != "" {
t.Errorf("json.Unmarshal (-want, +got):\n%s", diff)
}
}