/
coverage.go
169 lines (135 loc) · 4.54 KB
/
coverage.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
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
package cmd
import (
"fmt"
"net/url"
"os"
"github.com/gookit/color"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/Permify/permify/pkg/cmd/flags"
cov "github.com/Permify/permify/pkg/development/coverage"
"github.com/Permify/permify/pkg/development/file"
"github.com/Permify/permify/pkg/schema"
)
// NewCoverageCommand - creates a new coverage command
func NewCoverageCommand() *cobra.Command {
command := &cobra.Command{
Use: "coverage <file>",
Short: "coverage analysis of authorization model and assertions",
RunE: coverage(),
Args: cobra.ExactArgs(1),
}
f := command.Flags()
f.Int("coverage-relationships", 0, "the min coverage for relationships")
f.Int("coverage-attributes", 0, "the min coverage for attributes")
f.Int("coverage-assertions", 0, "the min coverage for assertions")
// register flags for coverage
command.PreRun = func(cmd *cobra.Command, args []string) {
flags.RegisterCoverageFlags(f)
}
return command
}
// coverage - coverage analysis of authorization model and assertions
func coverage() func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) error {
// parse the url from the first argument
u, err := url.Parse(args[0])
if err != nil {
return err
}
// get min coverage from viper
coverageRelationships := viper.GetInt("coverage-relationships")
coverageAttributes := viper.GetInt("coverage-attributes")
coverageAssertions := viper.GetInt("coverage-assertions")
if err != nil {
return err
}
// create a new decoder from the url
decoder, err := file.NewDecoderFromURL(u)
if err != nil {
return err
}
// create a new shape
s := &file.Shape{}
// decode the schema from the decoder
err = decoder.Decode(s)
if err != nil {
return err
}
loader := schema.NewSchemaLoader()
loaded, err := loader.LoadSchema(s.Schema)
if err != nil {
return err
}
s.Schema = loaded
color.Notice.Println("initiating validation... 🚀")
validator := validate()
err = validator(cmd, args)
if err != nil {
color.Danger.Println("failed to validate given file\n")
color.Danger.Println("FAILED")
return err
}
color.Notice.Println("initiating coverage analysis... 🚀")
schemaCoverageInfo := cov.Run(*s)
DisplayCoverageInfo(schemaCoverageInfo)
if schemaCoverageInfo.TotalAssertionsCoverage < coverageAssertions {
color.Danger.Printf("assertions coverage < %d%%\n", coverageAssertions)
// print FAILED with color danger
color.Danger.Println("FAILED")
os.Exit(1)
}
if schemaCoverageInfo.TotalRelationshipsCoverage < coverageRelationships {
color.Danger.Printf("relationships coverage < %d%%\n", coverageRelationships)
// print FAILED with color danger
color.Danger.Println("FAILED")
os.Exit(1)
}
if schemaCoverageInfo.TotalAttributesCoverage < coverageAttributes {
color.Danger.Printf("attributes coverage < %d%%\n", coverageAttributes)
// print FAILED with color danger
color.Danger.Println("FAILED")
os.Exit(1)
}
return nil
}
}
// DisplayCoverageInfo - Display the schema coverage information
func DisplayCoverageInfo(schemaCoverageInfo cov.SchemaCoverageInfo) {
color.Notice.Println("schema coverage information:")
for _, entityCoverageInfo := range schemaCoverageInfo.EntityCoverageInfo {
color.Notice.Printf("entity: %s\n", entityCoverageInfo.EntityName)
fmt.Printf(" uncovered relationships:\n")
for _, value := range entityCoverageInfo.UncoveredRelationships {
fmt.Printf(" - %v\n", value)
}
fmt.Printf(" uncovered assertions:\n")
for key, value := range entityCoverageInfo.UncoveredAssertions {
fmt.Printf(" %s:\n", key)
for _, v := range value {
fmt.Printf(" %v\n", v)
}
}
fmt.Printf(" coverage relationships percentage:")
if entityCoverageInfo.CoverageRelationshipsPercent <= 50 {
color.Danger.Printf(" %d%%\n", entityCoverageInfo.CoverageRelationshipsPercent)
} else {
color.Success.Printf(" %d%%\n", entityCoverageInfo.CoverageRelationshipsPercent)
}
fmt.Printf(" coverage attributes percentage:")
if entityCoverageInfo.CoverageAttributesPercent <= 50 {
color.Danger.Printf(" %d%%\n", entityCoverageInfo.CoverageAttributesPercent)
} else {
color.Success.Printf(" %d%%\n", entityCoverageInfo.CoverageAttributesPercent)
}
fmt.Printf(" coverage assertions percentage: \n")
for key, value := range entityCoverageInfo.CoverageAssertionsPercent {
fmt.Printf(" %s:", key)
if value <= 50 {
color.Danger.Printf(" %d%%\n", value)
} else {
color.Success.Printf(" %d%%\n", value)
}
}
}
}