Skip to content

Commit 4737ed4

Browse files
committed
cli/command/image/build: fix linting, add sub-tests
- fix minor linting issues (unhandled errors) - rename vars to prevent shadowing - use sub-tests for tests that already prepared for it Signed-off-by: Sebastiaan van Stijn <[email protected]> (cherry picked from commit 2c539a6) Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent 8cff008 commit 4737ed4

File tree

3 files changed

+39
-29
lines changed

3 files changed

+39
-29
lines changed

cli/command/image/build/context.go

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func ValidateContextDirectory(srcPath string, excludes []string) error {
8080
if err != nil && os.IsPermission(err) {
8181
return errors.Errorf("no permission to read from '%s'", filePath)
8282
}
83-
currentFile.Close()
83+
_ = currentFile.Close()
8484
}
8585
return nil
8686
})
@@ -97,10 +97,10 @@ func filepathMatches(matcher *patternmatcher.PatternMatcher, file string) (bool,
9797

9898
// DetectArchiveReader detects whether the input stream is an archive or a
9999
// Dockerfile and returns a buffered version of input, safe to consume in lieu
100-
// of input. If an archive is detected, isArchive is set to true, and to false
100+
// of input. If an archive is detected, ok is set to true, and to false
101101
// otherwise, in which case it is safe to assume input represents the contents
102102
// of a Dockerfile.
103-
func DetectArchiveReader(input io.ReadCloser) (rc io.ReadCloser, isArchive bool, err error) {
103+
func DetectArchiveReader(input io.ReadCloser) (rc io.ReadCloser, ok bool, err error) {
104104
buf := bufio.NewReader(input)
105105

106106
magic, err := buf.Peek(archiveHeaderSize * 2)
@@ -141,12 +141,12 @@ func WriteTempDockerfile(rc io.ReadCloser) (dockerfileDir string, err error) {
141141
// Dockerfile or tar archive. Returns a tar archive used as a context and a
142142
// path to the Dockerfile inside the tar.
143143
func GetContextFromReader(rc io.ReadCloser, dockerfileName string) (out io.ReadCloser, relDockerfile string, err error) {
144-
rc, isArchive, err := DetectArchiveReader(rc)
144+
rc, ok, err := DetectArchiveReader(rc)
145145
if err != nil {
146146
return nil, "", err
147147
}
148148

149-
if isArchive {
149+
if ok {
150150
return rc, dockerfileName, nil
151151
}
152152

@@ -171,7 +171,7 @@ func GetContextFromReader(rc io.ReadCloser, dockerfileName string) (out io.ReadC
171171

172172
return newReadCloserWrapper(tarArchive, func() error {
173173
err := tarArchive.Close()
174-
os.RemoveAll(dockerfileDir)
174+
_ = os.RemoveAll(dockerfileDir)
175175
return err
176176
}), DefaultDockerfileName, nil
177177
}
@@ -242,7 +242,7 @@ func getWithStatusError(url string) (resp *http.Response, err error) {
242242
}
243243
msg := fmt.Sprintf("failed to GET %s with status %s", url, resp.Status)
244244
body, err := io.ReadAll(resp.Body)
245-
resp.Body.Close()
245+
_ = resp.Body.Close()
246246
if err != nil {
247247
return nil, errors.Wrapf(err, "%s: error reading body", msg)
248248
}
@@ -374,7 +374,7 @@ func isUNC(path string) bool {
374374
// the relative path to the dockerfile in the context.
375375
func AddDockerfileToBuildContext(dockerfileCtx io.ReadCloser, buildCtx io.ReadCloser) (io.ReadCloser, string, error) {
376376
file, err := io.ReadAll(dockerfileCtx)
377-
dockerfileCtx.Close()
377+
_ = dockerfileCtx.Close()
378378
if err != nil {
379379
return nil, "", err
380380
}
@@ -438,17 +438,19 @@ func Compress(buildCtx io.ReadCloser) (io.ReadCloser, error) {
438438
go func() {
439439
compressWriter, err := compression.CompressStream(pipeWriter, archive.Gzip)
440440
if err != nil {
441-
pipeWriter.CloseWithError(err)
441+
_ = pipeWriter.CloseWithError(err)
442442
}
443-
defer buildCtx.Close()
443+
defer func() {
444+
_ = buildCtx.Close()
445+
}()
444446

445447
if _, err := io.Copy(compressWriter, buildCtx); err != nil {
446-
pipeWriter.CloseWithError(errors.Wrap(err, "failed to compress context"))
447-
compressWriter.Close()
448+
_ = pipeWriter.CloseWithError(errors.Wrap(err, "failed to compress context"))
449+
_ = compressWriter.Close()
448450
return
449451
}
450-
compressWriter.Close()
451-
pipeWriter.Close()
452+
_ = compressWriter.Close()
453+
_ = pipeWriter.Close()
452454
}()
453455

454456
return pipeReader, nil

cli/command/image/build/context_test.go

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ func TestGetContextFromReaderString(t *testing.T) {
135135
}
136136

137137
buff := new(bytes.Buffer)
138-
buff.ReadFrom(tarReader)
138+
_, _ = buff.ReadFrom(tarReader)
139139
contents := buff.String()
140140

141141
_, err = tarReader.Next()
@@ -182,7 +182,7 @@ func TestGetContextFromReaderTar(t *testing.T) {
182182
}
183183

184184
buff := new(bytes.Buffer)
185-
buff.ReadFrom(tarReader)
185+
_, _ = buff.ReadFrom(tarReader)
186186
contents := buff.String()
187187

188188
_, err = tarReader.Next()
@@ -263,7 +263,7 @@ func chdir(t *testing.T, dir string) {
263263
}
264264

265265
func TestIsArchive(t *testing.T) {
266-
testcases := []struct {
266+
tests := []struct {
267267
doc string
268268
header []byte
269269
expected bool
@@ -289,13 +289,15 @@ func TestIsArchive(t *testing.T) {
289289
expected: false,
290290
},
291291
}
292-
for _, testcase := range testcases {
293-
assert.Check(t, is.Equal(testcase.expected, IsArchive(testcase.header)), testcase.doc)
292+
for _, tc := range tests {
293+
t.Run(tc.doc, func(t *testing.T) {
294+
assert.Check(t, is.Equal(tc.expected, IsArchive(tc.header)), tc.doc)
295+
})
294296
}
295297
}
296298

297299
func TestDetectArchiveReader(t *testing.T) {
298-
testcases := []struct {
300+
tests := []struct {
299301
file string
300302
desc string
301303
expected bool
@@ -316,14 +318,18 @@ func TestDetectArchiveReader(t *testing.T) {
316318
expected: false,
317319
},
318320
}
319-
for _, testcase := range testcases {
320-
content, err := os.Open(testcase.file)
321-
assert.NilError(t, err)
322-
defer content.Close()
323-
324-
_, isArchive, err := DetectArchiveReader(content)
325-
assert.NilError(t, err)
326-
assert.Check(t, is.Equal(testcase.expected, isArchive), testcase.file)
321+
for _, tc := range tests {
322+
t.Run(tc.desc, func(t *testing.T) {
323+
content, err := os.Open(tc.file)
324+
assert.NilError(t, err)
325+
defer func() {
326+
_ = content.Close()
327+
}()
328+
329+
_, isArchive, err := DetectArchiveReader(content)
330+
assert.NilError(t, err)
331+
assert.Check(t, is.Equal(tc.expected, isArchive), tc.file)
332+
})
327333
}
328334
}
329335

cli/command/image/build/dockerignore.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ func ReadDockerignore(contextDir string) ([]string, error) {
2121
case err != nil:
2222
return nil, err
2323
}
24-
defer f.Close()
24+
defer func() {
25+
_ = f.Close()
26+
}()
2527

2628
patterns, err := ignorefile.ReadAll(f)
2729
if err != nil {

0 commit comments

Comments
 (0)