Skip to content

Commit 46b45af

Browse files
committed
Cosmetic and documentation fixes.
Signed-off-by: apostasie <[email protected]>
1 parent 90fcad6 commit 46b45af

File tree

9 files changed

+73
-27
lines changed

9 files changed

+73
-27
lines changed

mod/tigron/expect/comparators_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
//revive:disable:add-constant
1818
package expect_test
1919

20+
// TODO: add a lot more tests including failure conditions with mimicry
21+
2022
import (
2123
"encoding/json"
2224
"regexp"

mod/tigron/require/doc.md

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Requirements
2+
3+
Any `test.Case` has a `Require` property that allow enforcing specific, per-test requirements.
4+
5+
A `Requirement` is expected to make you `Skip` tests when the environment does not match
6+
expectations.
7+
8+
A number of ready-made requirements are provided:
9+
10+
```go
11+
require.Windows // a test runs only on Windows (or Not(Windows))
12+
require.Linux // a test runs only on Linux
13+
test.Darwin // a test runs only on Darwin
14+
test.OS(name string) // a test runs only on the OS `name`
15+
require.Binary(name string) // a test requires the binary `name` to be in the PATH
16+
require.Not(req Requirement) // a test runs only if the opposite of the requirement `req` is fulfilled
17+
require.All(req ...Requirement) // a test runs only if all requirements are fulfilled
18+
```
19+
20+
## Custom requirements
21+
22+
A requirement is a simple struct (`test.Requirement`), that must provide a `Check` function.
23+
24+
`Check` function signature is `func(data Data, helpers Helpers) (bool, string)`, that is expected
25+
to return a boolean indicating (true) that the environment is fine to run that test, or `false` of course if not.
26+
The second parameter should return a meaningful message explaining what the requirement is.
27+
28+
For example: "found kernel version > 5.0".
29+
30+
Given requirements can be negated with `require.Not(req)`, your message should describe the state of the environment
31+
and not whether the conditions are met (or not met).
32+
33+
A `test.Requirement` can optionally provide custom `Setup` and `Cleanup` routines, in case you need to perform
34+
specific operations before the test run (and cleanup some stuff after).
35+
36+
`Setup/Cleanup` will only run if the requirement `Check` returns true.
37+
38+
Here is for example how the `require.Binary` method is implemented:
39+
40+
```go
41+
package thing
42+
43+
func Binary(name string) *test.Requirement {
44+
return &test.Requirement{
45+
Check: func(_ test.Data, _ test.Helpers) (bool, string) {
46+
mess := fmt.Sprintf("executable %q has been found in PATH", name)
47+
ret := true
48+
if _, err := exec.LookPath(name); err != nil {
49+
ret = false
50+
mess = fmt.Sprintf("executable %q doesn't exist in PATH", name)
51+
}
52+
53+
return ret, mess
54+
},
55+
}
56+
}
57+
```
58+
59+
## Gotcha
60+
61+
Obviously, `test.Not(otherreq)` will NOT perform any `Setup/Cleanup` provided by `otherreq`.
62+
63+
Ambient requirements are currently undocumented.

mod/tigron/require/requirement_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func TestRequire(t *testing.T) {
5757
pass, _ = require.Arch(runtime.GOARCH).Check(nil, nil)
5858
}
5959

60-
assertive.IsEqual(t, pass, true)
60+
assertive.IsEqual(t, pass, true, "Require works as expected")
6161
}
6262

6363
func TestNot(t *testing.T) {
@@ -76,7 +76,7 @@ func TestNot(t *testing.T) {
7676
pass, _ = require.Not(require.Linux).Check(nil, nil)
7777
}
7878

79-
assertive.IsEqual(t, pass, true)
79+
assertive.IsEqual(t, pass, true, "require.Not works as expected")
8080
}
8181

8282
func TestAllSuccess(t *testing.T) {
@@ -99,7 +99,7 @@ func TestAllSuccess(t *testing.T) {
9999
require.Not(require.Darwin)).Check(nil, nil)
100100
}
101101

102-
assertive.IsEqual(t, pass, true)
102+
assertive.IsEqual(t, pass, true, "require.All works as expected")
103103
}
104104

105105
func TestAllOneFail(t *testing.T) {
@@ -122,5 +122,5 @@ func TestAllOneFail(t *testing.T) {
122122
require.Not(require.Darwin)).Check(nil, nil)
123123
}
124124

125-
assertive.IsEqual(t, pass, true)
125+
assertive.IsEqual(t, pass, true, "mixing require.All and require.Not works as expected")
126126
}

mod/tigron/test/command.go

-3
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ type CustomizableCommand interface {
7171
read(key ConfigKey) ConfigValue
7272
}
7373

74-
//nolint:ireturn
7574
func NewGenericCommand() CustomizableCommand {
7675
genericCom := &GenericCommand{
7776
Env: map[string]string{},
@@ -263,7 +262,6 @@ func (gc *GenericCommand) withConfig(config Config) {
263262
gc.Config = config
264263
}
265264

266-
//nolint:ireturn
267265
func (gc *GenericCommand) Clone() TestableCommand {
268266
// Copy the command and return a new one - with almost everything from the parent command
269267
clone := *gc
@@ -287,7 +285,6 @@ func (gc *GenericCommand) T() *testing.T {
287285
return gc.t
288286
}
289287

290-
//nolint:ireturn
291288
func (gc *GenericCommand) clear() TestableCommand {
292289
comcopy := *gc
293290
// Reset internal command

mod/tigron/test/config.go

+1-6
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,14 @@
1717
package test
1818

1919
// WithConfig returns a config object with a certain config property set.
20-
//
21-
//nolint:ireturn
2220
func WithConfig(key ConfigKey, value ConfigValue) Config {
2321
cfg := &config{}
2422
cfg.Write(key, value)
2523

2624
return cfg
2725
}
2826

29-
// Contains the implementation of the Config interface
30-
31-
//nolint:ireturn
27+
// Contains the implementation of the Config interface.
3228
func configureConfig(cfg, parent Config) Config {
3329
if cfg == nil {
3430
cfg = &config{
@@ -49,7 +45,6 @@ type config struct {
4945
config map[ConfigKey]ConfigValue
5046
}
5147

52-
//nolint:ireturn
5348
func (cfg *config) Write(key ConfigKey, value ConfigValue) Config {
5449
if cfg.config == nil {
5550
cfg.config = make(map[ConfigKey]ConfigValue)

mod/tigron/test/data.go

+1-6
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,14 @@ const (
3131
)
3232

3333
// WithData returns a data object with a certain key value set.
34-
//
35-
//nolint:ireturn
3634
func WithData(key, value string) Data {
3735
dat := &data{}
3836
dat.Set(key, value)
3937

4038
return dat
4139
}
4240

43-
// Contains the implementation of the Data interface
44-
45-
//nolint:ireturn
41+
// Contains the implementation of the Data interface.
4642
func configureData(t *testing.T, seedData, parent Data) Data {
4743
t.Helper()
4844

@@ -79,7 +75,6 @@ func (dt *data) Get(key string) string {
7975
return dt.labels[key]
8076
}
8177

82-
//nolint:ireturn
8378
func (dt *data) Set(key, value string) Data {
8479
if dt.labels == nil {
8580
dt.labels = map[string]string{}

mod/tigron/test/helpers.go

-4
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,6 @@ func (help *helpersInternal) Err(args ...string) string {
7474
}
7575

7676
// Command will return a clone of your base command without running it.
77-
//
78-
//nolint:ireturn,nolintlint
7977
func (help *helpersInternal) Command(args ...string) TestableCommand {
8078
cc := help.cmdInternal.Clone()
8179
cc.WithArgs(args...)
@@ -85,8 +83,6 @@ func (help *helpersInternal) Command(args ...string) TestableCommand {
8583

8684
// Custom will return a command for the requested binary and args, with the environment of your test
8785
// (eg: Env, Cwd, etc.)
88-
//
89-
//nolint:ireturn,nolintlint
9086
func (help *helpersInternal) Custom(binary string, args ...string) TestableCommand {
9187
cc := help.cmdInternal.clear()
9288
cc.WithBinary(binary)

mod/tigron/test/types.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ type (
2323
ConfigValue string
2424
)
2525

26-
// A Requirement offers a way to verify random conditions to decide if a test should be skipped or
27-
// run.
26+
// A Requirement offers a way to verify random conditions to decide if a test should be skipped or run.
2827
// It can also (optionally) provide custom Setup and Cleanup routines.
2928
type Requirement struct {
3029
// Check is expected to verify if the requirement is fulfilled, and return a boolean and an

mod/tigron/tig/doc.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616

1717
// Package tig defines interfaces for third-party packages that tigron needs to interact with.
18-
// The main upside of expressing our expectations instead of depending directly on concrete
19-
// implementations is evidently
18+
// The main upside of expressing our expectations instead of depending directly on concrete implementations is evidently
2019
// the ability to mock easily, which in turn makes testing much easier.
2120
package tig

0 commit comments

Comments
 (0)