-
Notifications
You must be signed in to change notification settings - Fork 0
/
arg_suite.go
130 lines (108 loc) · 3.7 KB
/
arg_suite.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
// Copyright 2024 The Kanister Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package test
import (
"gopkg.in/check.v1"
"github.com/pkg/errors"
"github.com/kanisterio/safecli"
"github.com/kanisterio/safecli/command"
)
// ArgumentTest defines a test for a single argument.
type ArgumentTest struct {
// Name of the test. (required)
Name string
// Argument to test. (required)
Argument command.Applier
// Expected CLI arguments. (optional)
ExpectedCLI []string
// Expected log output. (optional)
// if empty, it will be set to ExpectedCLI joined with space.
// if empty and ExpectedCLI is empty, it will be ignored.
ExpectedLog string
// Expected error. (optional)
// If nil, no error is expected.
ExpectedErr error
// Expected error message. (optional)
// If empty, it will be ignored.
ExpectedErrMsg string
}
// comment provides a test name for the test during assertions and checks.
// This method is compatible with different checkers.
func (t *ArgumentTest) comment() any {
return check.Commentf("%s", t.Name)
}
// setDefaultExpectedLog sets the default value for ExpectedLog based on ExpectedCLI.
func (t *ArgumentTest) setDefaultExpectedLog() {
if len(t.ExpectedLog) == 0 && len(t.ExpectedCLI) > 0 {
t.ExpectedLog = RedactCLI(t.ExpectedCLI)
}
}
// assertNoError makes sure there is no error.
func (t *ArgumentTest) assertNoError(c *check.C, err error) {
c.Assert(err, check.IsNil, t.comment())
}
// assertError checks the error against ExpectedErr.
func (t *ArgumentTest) assertError(c *check.C, err error) {
actualErr := errors.Cause(err)
c.Assert(actualErr, check.Equals, t.ExpectedErr, t.comment())
}
// assertErrorMsg checks the error message against ExpectedErrMsg.
func (t *ArgumentTest) assertErrorMsg(c *check.C, err error) {
if t.ExpectedErrMsg != "" {
c.Assert(err.Error(), check.Equals, t.ExpectedErrMsg, t.comment())
}
}
// assertCLI asserts the builder's CLI output against ExpectedCLI.
func (t *ArgumentTest) assertCLI(c *check.C, b *safecli.Builder) {
if t.ExpectedCLI != nil {
c.Check(b.Build(), check.DeepEquals, t.ExpectedCLI, t.comment())
}
}
// assertLog asserts the builder's log output against ExpectedLog.
func (t *ArgumentTest) assertLog(c *check.C, b *safecli.Builder) {
if t.ExpectedCLI != nil {
t.setDefaultExpectedLog()
c.Check(b.String(), check.Equals, t.ExpectedLog, t.comment())
}
}
// Test runs the argument test.
func (t *ArgumentTest) Test(c *check.C, cmdName string) {
if t.Name == "" {
c.Fatal("Name is required")
}
cmd, err := command.New(cmdName, t.Argument)
if t.ExpectedErr == nil {
t.assertNoError(c, err)
} else {
t.assertError(c, err)
t.assertErrorMsg(c, err)
}
t.assertCLI(c, cmd)
t.assertLog(c, cmd)
}
// ArgumentSuite defines a test suite for arguments.
type ArgumentSuite struct {
Cmd string // Cmd appends to the safecli.Builder before test if not empty.
Arguments []ArgumentTest // Tests to run.
}
// TestArguments runs all tests in the argument suite.
func (s *ArgumentSuite) TestArguments(c *check.C) {
for _, arg := range s.Arguments {
arg.Test(c, s.Cmd)
}
}
// NewArgumentSuite creates a new ArgumentSuite.
func NewArgumentSuite(args []ArgumentTest) *ArgumentSuite {
return &ArgumentSuite{Arguments: args}
}