Skip to content

Commit 8661552

Browse files
committed
golangci-lint: enable thelper linter
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent 8e98c76 commit 8661552

21 files changed

Lines changed: 139 additions & 116 deletions

File tree

.golangci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ linters:
1616
- nakedret
1717
- revive
1818
- staticcheck
19+
- thelper
1920
- typecheck
2021
- unconvert
2122
- unparam

cli/command/cli_options_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
)
99

1010
func contentTrustEnabled(t *testing.T) bool {
11+
t.Helper()
1112
var cli DockerCli
1213
assert.NilError(t, WithContentTrustFromEnv()(&cli))
1314
return cli.contentTrust

cli/command/context/create_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ func TestCreate(t *testing.T) {
104104
}
105105

106106
func assertContextCreateLogging(t *testing.T, cli *test.FakeCli, n string) {
107+
t.Helper()
107108
assert.Equal(t, n+"\n", cli.OutBuffer().String())
108109
assert.Equal(t, fmt.Sprintf("Successfully created context %q\n", n), cli.ErrBuffer().String())
109110
}

cli/command/formatter/tabwriter/tabwriter_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ func (b *buffer) Write(buf []byte) (written int, err error) {
3636
func (b *buffer) String() string { return string(b.a) }
3737

3838
func write(t *testing.T, testname string, w *Writer, src string) {
39+
t.Helper()
3940
written, err := io.WriteString(w, src)
4041
if err != nil {
4142
t.Errorf("--- test: %s\n--- src:\n%q\n--- write error: %v\n", testname, src, err)
@@ -46,6 +47,7 @@ func write(t *testing.T, testname string, w *Writer, src string) {
4647
}
4748

4849
func verify(t *testing.T, testname string, w *Writer, b *buffer, src, expected string) {
50+
t.Helper()
4951
err := w.Flush()
5052
if err != nil {
5153
t.Errorf("--- test: %s\n--- src:\n%q\n--- flush error: %v\n", testname, src, err)
@@ -58,6 +60,7 @@ func verify(t *testing.T, testname string, w *Writer, b *buffer, src, expected s
5860
}
5961

6062
func check(t *testing.T, testname string, minwidth, tabwidth, padding int, padchar byte, flags uint, src, expected string) {
63+
t.Helper()
6164
var b buffer
6265
b.init(1000)
6366

@@ -622,6 +625,7 @@ func (panicWriter) Write([]byte) (int, error) {
622625
}
623626

624627
func wantPanicString(t *testing.T, want string) {
628+
t.Helper()
625629
if e := recover(); e != nil {
626630
got, ok := e.(string)
627631
switch {

cli/command/image/build/context_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,19 @@ func prepareEmpty(_ *testing.T) string {
2323
}
2424

2525
func prepareNoFiles(t *testing.T) string {
26+
t.Helper()
2627
return createTestTempDir(t)
2728
}
2829

2930
func prepareOneFile(t *testing.T) string {
31+
t.Helper()
3032
contextDir := createTestTempDir(t)
3133
createTestTempFile(t, contextDir, DefaultDockerfileName, dockerfileContents)
3234
return contextDir
3335
}
3436

3537
func testValidateContextDirectory(t *testing.T, prepare func(t *testing.T) string, excludes []string) {
38+
t.Helper()
3639
contextDir := prepare(t)
3740
err := ValidateContextDirectory(contextDir, excludes)
3841
assert.NilError(t, err)
@@ -250,6 +253,7 @@ func createTestTempFile(t *testing.T, dir, filename, contents string) string {
250253
// This function is meant to be executed as a deferred call.
251254
// When an error occurs, it terminates the test.
252255
func chdir(t *testing.T, dir string) {
256+
t.Helper()
253257
workingDirectory, err := os.Getwd()
254258
assert.NilError(t, err)
255259
assert.NilError(t, os.Chdir(dir))

cli/command/manifest/inspect_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@ import (
2020
)
2121

2222
func ref(t *testing.T, name string) reference.Named {
23+
t.Helper()
2324
named, err := reference.ParseNamed("example.com/" + name)
2425
assert.NilError(t, err)
2526
return named
2627
}
2728

2829
func fullImageManifest(t *testing.T, ref reference.Named) types.ImageManifest {
30+
t.Helper()
2931
man, err := schema2.FromStruct(schema2.Manifest{
3032
Versioned: schema2.SchemaVersion,
3133
Config: distribution.Descriptor{

cli/command/service/inspect_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
)
1818

1919
func formatServiceInspect(t *testing.T, format formatter.Format, now time.Time) string {
20+
t.Helper()
2021
b := new(bytes.Buffer)
2122

2223
endpointSpec := &swarm.EndpointSpec{

cli/command/trust/key_load_test.go

Lines changed: 67 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -114,99 +114,91 @@ var testKeys = map[string][]byte{
114114
func TestLoadKeyFromPath(t *testing.T) {
115115
skip.If(t, runtime.GOOS == "windows")
116116
for keyID, keyBytes := range testKeys {
117-
keyID, keyBytes := keyID, keyBytes
117+
privKeyID, privKeyFixture := keyID, keyBytes
118118
t.Run(fmt.Sprintf("load-key-id-%s-from-path", keyID), func(t *testing.T) {
119-
testLoadKeyFromPath(t, keyID, keyBytes)
119+
privKeyFilepath := filepath.Join(t.TempDir(), "privkey.pem")
120+
assert.NilError(t, os.WriteFile(privKeyFilepath, privKeyFixture, notary.PrivNoExecPerms))
121+
122+
keyStorageDir := t.TempDir()
123+
124+
const passwd = "password"
125+
cannedPasswordRetriever := passphrase.ConstantRetriever(passwd)
126+
keyFileStore, err := storage.NewPrivateKeyFileStorage(keyStorageDir, notary.KeyExtension)
127+
assert.NilError(t, err)
128+
privKeyImporters := []trustmanager.Importer{keyFileStore}
129+
130+
// get the privKeyBytes
131+
privKeyBytes, err := getPrivKeyBytesFromPath(privKeyFilepath)
132+
assert.NilError(t, err)
133+
134+
// import the key to our keyStorageDir
135+
assert.Check(t, loadPrivKeyBytesToStore(privKeyBytes, privKeyImporters, privKeyFilepath, "signer-name", cannedPasswordRetriever))
136+
137+
// check that the appropriate ~/<trust_dir>/private/<key_id>.key file exists
138+
expectedImportKeyPath := filepath.Join(keyStorageDir, notary.PrivDir, privKeyID+"."+notary.KeyExtension)
139+
_, err = os.Stat(expectedImportKeyPath)
140+
assert.NilError(t, err)
141+
142+
// verify the key content
143+
from, _ := os.OpenFile(expectedImportKeyPath, os.O_RDONLY, notary.PrivExecPerms)
144+
defer from.Close()
145+
fromBytes, _ := io.ReadAll(from)
146+
keyPEM, _ := pem.Decode(fromBytes)
147+
assert.Check(t, is.Equal("signer-name", keyPEM.Headers["role"]))
148+
// the default GUN is empty
149+
assert.Check(t, is.Equal("", keyPEM.Headers["gun"]))
150+
// assert encrypted header
151+
assert.Check(t, is.Equal("ENCRYPTED PRIVATE KEY", keyPEM.Type))
152+
153+
decryptedKey, err := tufutils.ParsePKCS8ToTufKey(keyPEM.Bytes, []byte(passwd))
154+
assert.NilError(t, err)
155+
fixturePEM, _ := pem.Decode(privKeyFixture)
156+
assert.Check(t, is.DeepEqual(fixturePEM.Bytes, decryptedKey.Private()))
120157
})
121158
}
122159
}
123160

124-
func testLoadKeyFromPath(t *testing.T, privKeyID string, privKeyFixture []byte) {
125-
privKeyFilepath := filepath.Join(t.TempDir(), "privkey.pem")
126-
assert.NilError(t, os.WriteFile(privKeyFilepath, privKeyFixture, notary.PrivNoExecPerms))
127-
128-
keyStorageDir := t.TempDir()
129-
130-
const passwd = "password"
131-
cannedPasswordRetriever := passphrase.ConstantRetriever(passwd)
132-
keyFileStore, err := storage.NewPrivateKeyFileStorage(keyStorageDir, notary.KeyExtension)
133-
assert.NilError(t, err)
134-
privKeyImporters := []trustmanager.Importer{keyFileStore}
135-
136-
// get the privKeyBytes
137-
privKeyBytes, err := getPrivKeyBytesFromPath(privKeyFilepath)
138-
assert.NilError(t, err)
139-
140-
// import the key to our keyStorageDir
141-
assert.Check(t, loadPrivKeyBytesToStore(privKeyBytes, privKeyImporters, privKeyFilepath, "signer-name", cannedPasswordRetriever))
142-
143-
// check that the appropriate ~/<trust_dir>/private/<key_id>.key file exists
144-
expectedImportKeyPath := filepath.Join(keyStorageDir, notary.PrivDir, privKeyID+"."+notary.KeyExtension)
145-
_, err = os.Stat(expectedImportKeyPath)
146-
assert.NilError(t, err)
147-
148-
// verify the key content
149-
from, _ := os.OpenFile(expectedImportKeyPath, os.O_RDONLY, notary.PrivExecPerms)
150-
defer from.Close()
151-
fromBytes, _ := io.ReadAll(from)
152-
keyPEM, _ := pem.Decode(fromBytes)
153-
assert.Check(t, is.Equal("signer-name", keyPEM.Headers["role"]))
154-
// the default GUN is empty
155-
assert.Check(t, is.Equal("", keyPEM.Headers["gun"]))
156-
// assert encrypted header
157-
assert.Check(t, is.Equal("ENCRYPTED PRIVATE KEY", keyPEM.Type))
158-
159-
decryptedKey, err := tufutils.ParsePKCS8ToTufKey(keyPEM.Bytes, []byte(passwd))
160-
assert.NilError(t, err)
161-
fixturePEM, _ := pem.Decode(privKeyFixture)
162-
assert.Check(t, is.DeepEqual(fixturePEM.Bytes, decryptedKey.Private()))
163-
}
164-
165161
func TestLoadKeyTooPermissive(t *testing.T) {
166162
skip.If(t, runtime.GOOS == "windows")
167163
for keyID, keyBytes := range testKeys {
168-
keyID, keyBytes := keyID, keyBytes
164+
keyID, privKeyFixture := keyID, keyBytes
169165
t.Run(fmt.Sprintf("load-key-id-%s-too-permissive", keyID), func(t *testing.T) {
170-
testLoadKeyTooPermissive(t, keyBytes)
171-
})
172-
}
173-
}
174-
175-
func testLoadKeyTooPermissive(t *testing.T, privKeyFixture []byte) {
176-
privKeyDir := t.TempDir()
177-
privKeyFilepath := filepath.Join(privKeyDir, "privkey477.pem")
178-
assert.NilError(t, os.WriteFile(privKeyFilepath, privKeyFixture, 0o477))
166+
privKeyDir := t.TempDir()
167+
privKeyFilepath := filepath.Join(privKeyDir, "privkey477.pem")
168+
assert.NilError(t, os.WriteFile(privKeyFilepath, privKeyFixture, 0o477))
179169

180-
// import the key to our keyStorageDir
181-
_, err := getPrivKeyBytesFromPath(privKeyFilepath)
182-
expected := fmt.Sprintf("private key file %s must not be readable or writable by others", privKeyFilepath)
183-
assert.Error(t, err, expected)
170+
// import the key to our keyStorageDir
171+
_, err := getPrivKeyBytesFromPath(privKeyFilepath)
172+
expected := fmt.Sprintf("private key file %s must not be readable or writable by others", privKeyFilepath)
173+
assert.Error(t, err, expected)
184174

185-
privKeyFilepath = filepath.Join(privKeyDir, "privkey667.pem")
186-
assert.NilError(t, os.WriteFile(privKeyFilepath, privKeyFixture, 0o677))
175+
privKeyFilepath = filepath.Join(privKeyDir, "privkey667.pem")
176+
assert.NilError(t, os.WriteFile(privKeyFilepath, privKeyFixture, 0o677))
187177

188-
_, err = getPrivKeyBytesFromPath(privKeyFilepath)
189-
expected = fmt.Sprintf("private key file %s must not be readable or writable by others", privKeyFilepath)
190-
assert.Error(t, err, expected)
178+
_, err = getPrivKeyBytesFromPath(privKeyFilepath)
179+
expected = fmt.Sprintf("private key file %s must not be readable or writable by others", privKeyFilepath)
180+
assert.Error(t, err, expected)
191181

192-
privKeyFilepath = filepath.Join(privKeyDir, "privkey777.pem")
193-
assert.NilError(t, os.WriteFile(privKeyFilepath, privKeyFixture, 0o777))
182+
privKeyFilepath = filepath.Join(privKeyDir, "privkey777.pem")
183+
assert.NilError(t, os.WriteFile(privKeyFilepath, privKeyFixture, 0o777))
194184

195-
_, err = getPrivKeyBytesFromPath(privKeyFilepath)
196-
expected = fmt.Sprintf("private key file %s must not be readable or writable by others", privKeyFilepath)
197-
assert.Error(t, err, expected)
185+
_, err = getPrivKeyBytesFromPath(privKeyFilepath)
186+
expected = fmt.Sprintf("private key file %s must not be readable or writable by others", privKeyFilepath)
187+
assert.Error(t, err, expected)
198188

199-
privKeyFilepath = filepath.Join(privKeyDir, "privkey400.pem")
200-
assert.NilError(t, os.WriteFile(privKeyFilepath, privKeyFixture, 0o400))
189+
privKeyFilepath = filepath.Join(privKeyDir, "privkey400.pem")
190+
assert.NilError(t, os.WriteFile(privKeyFilepath, privKeyFixture, 0o400))
201191

202-
_, err = getPrivKeyBytesFromPath(privKeyFilepath)
203-
assert.NilError(t, err)
192+
_, err = getPrivKeyBytesFromPath(privKeyFilepath)
193+
assert.NilError(t, err)
204194

205-
privKeyFilepath = filepath.Join(privKeyDir, "privkey600.pem")
206-
assert.NilError(t, os.WriteFile(privKeyFilepath, privKeyFixture, 0o600))
195+
privKeyFilepath = filepath.Join(privKeyDir, "privkey600.pem")
196+
assert.NilError(t, os.WriteFile(privKeyFilepath, privKeyFixture, 0o600))
207197

208-
_, err = getPrivKeyBytesFromPath(privKeyFilepath)
209-
assert.NilError(t, err)
198+
_, err = getPrivKeyBytesFromPath(privKeyFilepath)
199+
assert.NilError(t, err)
200+
})
201+
}
210202
}
211203

212204
var pubKeyFixture = []byte(`-----BEGIN PUBLIC KEY-----

cli/config/config_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
)
1616

1717
func setupConfigDir(t *testing.T) string {
18+
t.Helper()
1819
tmpdir := t.TempDir()
1920
oldDir := Dir()
2021
SetDir(tmpdir)

cli/required_test.go

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ func TestRequiresNoArgs(t *testing.T) {
2121
expectedError: "accepts no arguments.",
2222
},
2323
}
24-
runTestCases(t, testCases)
24+
for _, tc := range testCases {
25+
cmd := newDummyCommand(tc.validateFunc)
26+
cmd.SetArgs(tc.args)
27+
assert.ErrorContains(t, cmd.Execute(), tc.expectedError)
28+
}
2529
}
2630

2731
func TestRequiresMinArgs(t *testing.T) {
@@ -40,7 +44,11 @@ func TestRequiresMinArgs(t *testing.T) {
4044
expectedError: "at least 2 arguments.",
4145
},
4246
}
43-
runTestCases(t, testCases)
47+
for _, tc := range testCases {
48+
cmd := newDummyCommand(tc.validateFunc)
49+
cmd.SetArgs(tc.args)
50+
assert.ErrorContains(t, cmd.Execute(), tc.expectedError)
51+
}
4452
}
4553

4654
func TestRequiresMaxArgs(t *testing.T) {
@@ -60,7 +68,11 @@ func TestRequiresMaxArgs(t *testing.T) {
6068
expectedError: "at most 2 arguments.",
6169
},
6270
}
63-
runTestCases(t, testCases)
71+
for _, tc := range testCases {
72+
cmd := newDummyCommand(tc.validateFunc)
73+
cmd.SetArgs(tc.args)
74+
assert.ErrorContains(t, cmd.Execute(), tc.expectedError)
75+
}
6476
}
6577

6678
func TestRequiresRangeArgs(t *testing.T) {
@@ -88,7 +100,11 @@ func TestRequiresRangeArgs(t *testing.T) {
88100
expectedError: "at least 1 ",
89101
},
90102
}
91-
runTestCases(t, testCases)
103+
for _, tc := range testCases {
104+
cmd := newDummyCommand(tc.validateFunc)
105+
cmd.SetArgs(tc.args)
106+
assert.ErrorContains(t, cmd.Execute(), tc.expectedError)
107+
}
92108
}
93109

94110
func TestExactArgs(t *testing.T) {
@@ -106,7 +122,11 @@ func TestExactArgs(t *testing.T) {
106122
expectedError: "exactly 2 arguments.",
107123
},
108124
}
109-
runTestCases(t, testCases)
125+
for _, tc := range testCases {
126+
cmd := newDummyCommand(tc.validateFunc)
127+
cmd.SetArgs(tc.args)
128+
assert.ErrorContains(t, cmd.Execute(), tc.expectedError)
129+
}
110130
}
111131

112132
type testCase struct {
@@ -115,17 +135,6 @@ type testCase struct {
115135
expectedError string
116136
}
117137

118-
func runTestCases(t *testing.T, testCases []testCase) {
119-
for _, tc := range testCases {
120-
cmd := newDummyCommand(tc.validateFunc)
121-
cmd.SetArgs(tc.args)
122-
cmd.SetOut(io.Discard)
123-
124-
err := cmd.Execute()
125-
assert.ErrorContains(t, err, tc.expectedError)
126-
}
127-
}
128-
129138
func newDummyCommand(validationFunc cobra.PositionalArgs) *cobra.Command {
130139
cmd := &cobra.Command{
131140
Use: "dummy",
@@ -134,5 +143,6 @@ func newDummyCommand(validationFunc cobra.PositionalArgs) *cobra.Command {
134143
return errors.New("no error")
135144
},
136145
}
146+
cmd.SetOut(io.Discard)
137147
return cmd
138148
}

0 commit comments

Comments
 (0)