Skip to content

Commit

Permalink
Support policy test junit (#845)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmdm authored Feb 17, 2023
1 parent 901243e commit 634103a
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 17 deletions.
17 changes: 13 additions & 4 deletions cmd/policy/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,7 @@ This group of commands allows the management of polices to be verified against b
verbose bool
debug bool
useJSON bool
format string
)

cmd := &cobra.Command{
Expand Down Expand Up @@ -465,10 +466,17 @@ This group of commands allows the management of polices to be verified against b
}

handler := func() tester.ResultHandler {
if useJSON {
switch strings.ToLower(format) {
case "json":
return tester.MakeJSONResultHandler(handlerOpts)
case "junit":
return tester.MakeJUnitResultHandler(handlerOpts)
default:
if useJSON {
return tester.MakeJSONResultHandler(handlerOpts)
}
return tester.MakeDefaultResultHandler(handlerOpts)
}
return tester.MakeDefaultResultHandler(handlerOpts)
}()

if !runner.RunAndHandleResults(handler) {
Expand All @@ -484,8 +492,9 @@ This group of commands allows the management of polices to be verified against b
cmd.Flags().StringVar(&run, "run", "", "select which tests to run based on regular expression")
cmd.Flags().BoolVarP(&verbose, "verbose", "v", false, "print all tests instead of only failed tests")
cmd.Flags().BoolVar(&debug, "debug", false, "print test debug context. Sets verbose to true")
cmd.Flags().BoolVar(&useJSON, "json", false, "print json test results instead of standard output format")

cmd.Flags().BoolVar(&useJSON, "json", false, "sprints json test results instead of standard output format")
_ = cmd.Flags().MarkDeprecated("json", "use --format=json to print json test results")
cmd.Flags().StringVar(&format, "format", "", "select desired format between json or junit")
return cmd
}()

Expand Down
31 changes: 31 additions & 0 deletions cmd/policy/policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1047,13 +1047,16 @@ func TestGetSetSettings(t *testing.T) {
}
}

const jsonDeprecationMessage = "Flag --json has been deprecated, use --format=json to print json test results\n"

func TestTestRunner(t *testing.T) {
cases := []struct {
Name string
Verbose bool
Debug bool
Run string
Json bool
Format string
Expected func(*testing.T, string)
}{
{
Expand Down Expand Up @@ -1093,11 +1096,36 @@ func TestTestRunner(t *testing.T) {
{
Name: "json",
Json: true,
Expected: func(t *testing.T, s string) {
assert.Check(t, strings.HasPrefix(s, jsonDeprecationMessage))
assert.Check(t, s[len(jsonDeprecationMessage)] == '[')
assert.Check(t, s[len(s)-2] == ']')
},
},
{
Name: "format:json",
Format: "json",
Expected: func(t *testing.T, s string) {
assert.Check(t, s[0] == '[')
assert.Check(t, s[len(s)-2] == ']')
},
},
{
Name: "format:junit",
Format: "junit",
Expected: func(t *testing.T, s string) {
assert.Check(t, strings.Contains(s, "<?xml"))
},
},
{
Name: "format:junit and json flag",
Format: "junit",
Json: true,
Expected: func(t *testing.T, s string) {
assert.Check(t, strings.HasPrefix(s, jsonDeprecationMessage))
assert.Check(t, strings.Contains(s, "<?xml"))
},
},
}

for _, tc := range cases {
Expand All @@ -1117,6 +1145,9 @@ func TestTestRunner(t *testing.T) {
if tc.Json {
args = append(args, "--json")
}
if tc.Format != "" {
args = append(args, "--format", tc.Format)
}

cmd.SetArgs(args)

Expand Down
8 changes: 4 additions & 4 deletions cmd/policy/testdata/policy/test-expected-usage.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ Examples:
circleci policy test ./policies/...

Flags:
--debug print test debug context. Sets verbose to true
--json print json test results instead of standard output format
--run string select which tests to run based on regular expression
-v, --verbose print all tests instead of only failed tests
--debug print test debug context. Sets verbose to true
--format string select desired format between json or junit
--run string select which tests to run based on regular expression
-v, --verbose print all tests instead of only failed tests

Global Flags:
--policy-base-url string base url for policy api (default "https://internal.circleci.com")
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module github.com/CircleCI-Public/circleci-cli

require (
github.com/AlecAivazis/survey/v2 v2.1.1
github.com/CircleCI-Public/circle-policy-agent v0.0.469
github.com/CircleCI-Public/circle-policy-agent v0.0.534
github.com/Masterminds/semver v1.4.2
github.com/araddon/dateparse v0.0.0-20210429162001-6b43995a97de
github.com/blang/semver v3.5.1+incompatible
Expand Down
10 changes: 2 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,8 @@ github.com/AlecAivazis/survey/v2 v2.1.1 h1:LEMbHE0pLj75faaVEKClEX1TM4AJmmnOh9eim
github.com/AlecAivazis/survey/v2 v2.1.1/go.mod h1:9FJRdMdDm8rnT+zHVbvQT2RTSTLq0Ttd6q3Vl2fahjk=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/CircleCI-Public/circle-policy-agent v0.0.462 h1:WYSrA9MQGsqjjg19KJ3xve7CpH7KPiYg1JLD06SK/3k=
github.com/CircleCI-Public/circle-policy-agent v0.0.462/go.mod h1:lsP7UOPtBj0+IYFdL46vdoPaPJitURRvhmhiE4uRpeg=
github.com/CircleCI-Public/circle-policy-agent v0.0.469 h1:qsMuMObnT+LVYra1ekK25ayot4qQ8j7WGv0IInT2heI=
github.com/CircleCI-Public/circle-policy-agent v0.0.469/go.mod h1:lsP7UOPtBj0+IYFdL46vdoPaPJitURRvhmhiE4uRpeg=
github.com/CircleCI-Public/circle-policy-agent v0.0.534 h1:NekWjox7QQ2+K1QtNNPRP244JwLCLPze9Km3IOWTB+I=
github.com/CircleCI-Public/circle-policy-agent v0.0.534/go.mod h1:oySWkeJOAnvT56DRaGL0n2lHmeCEFwrhGg0Bvj9jiI8=
github.com/Masterminds/semver v1.4.2 h1:WBLTQ37jOCzSLtXNdoo8bNM8876KhNqOKvrlGITgsTc=
github.com/Masterminds/semver v1.4.2/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y=
github.com/Netflix/go-expect v0.0.0-20180615182759-c93bf25de8e8 h1:xzYJEypr/85nBpB11F9br+3HUrpgb+fcm5iADzXXYEw=
Expand Down Expand Up @@ -281,8 +279,6 @@ github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7J
github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
github.com/onsi/gomega v1.17.0 h1:9Luw4uT5HTjHTN8+aNcSThgH1vdXnmdJ8xIfZ4wyTRE=
github.com/onsi/gomega v1.17.0/go.mod h1:HnhC7FXeEQY45zxNK3PPoIUhzk/80Xly9PcubAlGdZY=
github.com/open-policy-agent/opa v0.48.0 h1:s2K823yohAUu/HB4MOPWDhBh88JMKQv7uTr6S89fbM0=
github.com/open-policy-agent/opa v0.48.0/go.mod h1:CsQcksP+qGBxO9oEBj1NnZqKcjgjmTJbRNTzjZB/DXQ=
github.com/open-policy-agent/opa v0.49.0 h1:TIlpCT1B5FSm8Dqo/a4t23gKmHkQysC3+7W77F99P4k=
github.com/open-policy-agent/opa v0.49.0/go.mod h1:WTLWtu498/QNTDkiHx76Xj7jaJUPvLJAPtdMkCcst0w=
github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4 h1:49lOXmGaUpV9Fz3gd7TFZY106KVlPVa5jcYD1gaQf98=
Expand Down Expand Up @@ -371,8 +367,6 @@ golang.org/x/exp v0.0.0-20191227195350-da58074b4299/go.mod h1:2RIsYlXP63K8oxa1u0
golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4=
golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM=
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU=
golang.org/x/exp v0.0.0-20230118134722-a68e582fa157 h1:fiNkyhJPUvxbRPbCqY/D9qdjmPzfHcpK3P4bM4gioSY=
golang.org/x/exp v0.0.0-20230118134722-a68e582fa157/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
golang.org/x/exp v0.0.0-20230213192124-5e25df0256eb h1:PaBZQdo+iSDyHT053FjUCgZQ/9uqVwPOcl7KSWhKn6w=
golang.org/x/exp v0.0.0-20230213192124-5e25df0256eb/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
Expand Down

0 comments on commit 634103a

Please sign in to comment.