go1.24: fix lint, unit and system tests

- development env: base on debian trixie with go1.24
- lint: run with default config
- fix lint errors
- fix unit tests
- fix system test
This commit is contained in:
André Roth
2025-04-12 17:53:48 +02:00
parent ae5379d84a
commit f7057a9517
117 changed files with 803 additions and 727 deletions
+20 -10
View File
@@ -46,8 +46,10 @@ func (s *CompressionSuite) TestDownloadTryCompression(c *C) {
d.ExpectResponse("http://example.com/file.bz2", bzipData)
r, file, err := DownloadTryCompression(s.ctx, d, s.baseURL, "file", expectedChecksums, false)
c.Assert(err, IsNil)
defer file.Close()
io.ReadFull(r, buf)
defer func() {
_ = file.Close()
}()
_, _ = io.ReadFull(r, buf)
c.Assert(string(buf), Equals, rawData)
c.Assert(d.Empty(), Equals, true)
@@ -58,8 +60,10 @@ func (s *CompressionSuite) TestDownloadTryCompression(c *C) {
d.ExpectResponse("http://example.com/file.gz", gzipData)
r, file, err = DownloadTryCompression(s.ctx, d, s.baseURL, "file", expectedChecksums, false)
c.Assert(err, IsNil)
defer file.Close()
io.ReadFull(r, buf)
defer func() {
_ = file.Close()
}()
_, _ = io.ReadFull(r, buf)
c.Assert(string(buf), Equals, rawData)
c.Assert(d.Empty(), Equals, true)
@@ -71,8 +75,10 @@ func (s *CompressionSuite) TestDownloadTryCompression(c *C) {
d.ExpectResponse("http://example.com/file.xz", xzData)
r, file, err = DownloadTryCompression(s.ctx, d, s.baseURL, "file", expectedChecksums, false)
c.Assert(err, IsNil)
defer file.Close()
io.ReadFull(r, buf)
defer func() {
_ = file.Close()
}()
_, _ = io.ReadFull(r, buf)
c.Assert(string(buf), Equals, rawData)
c.Assert(d.Empty(), Equals, true)
@@ -85,8 +91,10 @@ func (s *CompressionSuite) TestDownloadTryCompression(c *C) {
d.ExpectResponse("http://example.com/file", rawData)
r, file, err = DownloadTryCompression(s.ctx, d, s.baseURL, "file", expectedChecksums, false)
c.Assert(err, IsNil)
defer file.Close()
io.ReadFull(r, buf)
defer func() {
_ = file.Close()
}()
_, _ = io.ReadFull(r, buf)
c.Assert(string(buf), Equals, rawData)
c.Assert(d.Empty(), Equals, true)
@@ -114,8 +122,10 @@ func (s *CompressionSuite) TestDownloadTryCompressionLongestSuffix(c *C) {
d.ExpectResponse("http://example.com/subdir/file.bz2", bzipData)
r, file, err := DownloadTryCompression(s.ctx, d, s.baseURL, "subdir/file", expectedChecksums, false)
c.Assert(err, IsNil)
defer file.Close()
io.ReadFull(r, buf)
defer func() {
_ = file.Close()
}()
_, _ = io.ReadFull(r, buf)
c.Assert(string(buf), Equals, rawData)
c.Assert(d.Empty(), Equals, true)
}
+9 -5
View File
@@ -226,7 +226,7 @@ func (downloader *downloaderImpl) DownloadWithChecksum(ctx context.Context, url
err = os.Rename(temppath, destination)
if err != nil {
os.Remove(temppath)
_ = os.Remove(temppath)
return errors.Wrap(err, url)
}
@@ -239,7 +239,9 @@ func (downloader *downloaderImpl) download(req *http.Request, url, destination s
return "", errors.Wrap(err, url)
}
if resp.Body != nil {
defer resp.Body.Close()
defer func() {
_ = resp.Body.Close()
}()
}
if resp.StatusCode < 200 || resp.StatusCode > 299 {
@@ -257,7 +259,9 @@ func (downloader *downloaderImpl) download(req *http.Request, url, destination s
if err != nil {
return "", errors.Wrap(err, url)
}
defer outfile.Close()
defer func() {
_ = outfile.Close()
}()
checksummer := utils.NewChecksumWriter()
writers := []io.Writer{outfile, downloader.aggWriter}
@@ -270,7 +274,7 @@ func (downloader *downloaderImpl) download(req *http.Request, url, destination s
_, err = io.Copy(w, resp.Body)
if err != nil {
os.Remove(temppath)
_ = os.Remove(temppath)
return "", errors.Wrap(err, url)
}
@@ -295,7 +299,7 @@ func (downloader *downloaderImpl) download(req *http.Request, url, destination s
downloader.progress.Printf("WARNING: %s\n", err.Error())
}
} else {
os.Remove(temppath)
_ = os.Remove(temppath)
return "", err
}
} else {
+5 -5
View File
@@ -32,13 +32,13 @@ func (s *DownloaderSuiteBase) SetUpTest(c *C) {
mux := http.NewServeMux()
mux.HandleFunc("/test", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, %s", r.URL.Path)
_, _ = fmt.Fprintf(w, "Hello, %s", r.URL.Path)
})
s.ch = make(chan struct{})
go func() {
http.Serve(s.l, mux)
_ = http.Serve(s.l, mux)
close(s.ch)
}()
@@ -52,11 +52,11 @@ func (s *DownloaderSuiteBase) SetUpTest(c *C) {
func (s *DownloaderSuiteBase) TearDownTest(c *C) {
s.progress.Shutdown()
s.l.Close()
_ = s.l.Close()
<-s.ch
os.Remove(s.tempfile.Name())
s.tempfile.Close()
_ = os.Remove(s.tempfile.Name())
_ = s.tempfile.Close()
}
type DownloaderSuite struct {
+3 -1
View File
@@ -104,7 +104,9 @@ func (f *FakeDownloader) DownloadWithChecksum(_ context.Context, url string, fil
if err != nil {
return err
}
defer outfile.Close()
defer func() {
_ = outfile.Close()
}()
cks := utils.NewChecksumWriter()
w := io.MultiWriter(outfile, cks)
+16 -13
View File
@@ -14,7 +14,7 @@ import (
"golang.org/x/time/rate"
"github.com/aptly-dev/aptly/utils"
"github.com/cavaliergopher/grab/v3"
grab "github.com/cavaliergopher/grab/v3"
"github.com/pkg/errors"
"github.com/aptly-dev/aptly/aptly"
@@ -49,10 +49,10 @@ func (d *GrabDownloader) Download(ctx context.Context, url string, destination s
func (d *GrabDownloader) DownloadWithChecksum(ctx context.Context, url string, destination string, expected *utils.ChecksumInfo, ignoreMismatch bool) error {
maxTries := d.maxTries
const delayMax = time.Duration(5 * time.Minute)
// FIXME: const delayMax = time.Duration(5 * time.Minute)
delay := time.Duration(1 * time.Second)
const delayMultiplier = 2
err := fmt.Errorf("No tries available")
// FIXME: const delayMultiplier = 2
err := fmt.Errorf("no tries available")
for maxTries > 0 {
err = d.download(ctx, url, destination, expected, ignoreMismatch)
if err == nil {
@@ -125,7 +125,7 @@ func (d *GrabDownloader) download(_ context.Context, url string, destination str
req.RateLimiter = rate.NewLimiter(rate.Limit(d.downLimit), int(d.downLimit))
}
d.maybeSetupChecksum(req, expected)
err = d.maybeSetupChecksum(req, expected)
if err != nil {
d.log("Error setting up checksum: %v\n", err)
return errors.Wrap(err, url)
@@ -133,14 +133,17 @@ func (d *GrabDownloader) download(_ context.Context, url string, destination str
resp := d.client.Do(req)
Loop:
for {
select {
case <-resp.Done:
// download is complete
break Loop
}
}
<-resp.Done
// download is complete
// Loop:
// for {
// select {
// case <-resp.Done:
// // download is complete
// break Loop
// }
// }
err = resp.Err()
if err != nil && err == grab.ErrBadChecksum && ignoreMismatch {
fmt.Printf("Ignoring checksum mismatch for %s\n", url)
+5 -5
View File
@@ -31,13 +31,13 @@ func (s *GrabDownloaderSuiteBase) SetUpTest(c *C) {
mux := http.NewServeMux()
mux.HandleFunc("/test", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, %s", r.URL.Path)
_, _ = fmt.Fprintf(w, "Hello, %s", r.URL.Path)
})
s.ch = make(chan struct{})
go func() {
http.Serve(s.l, mux)
_ = http.Serve(s.l, mux)
close(s.ch)
}()
@@ -51,11 +51,11 @@ func (s *GrabDownloaderSuiteBase) SetUpTest(c *C) {
func (s *GrabDownloaderSuiteBase) TearDownTest(c *C) {
s.progress.Shutdown()
s.l.Close()
_ = s.l.Close()
<-s.ch
os.Remove(s.tempfile.Name())
s.tempfile.Close()
_ = os.Remove(s.tempfile.Name())
_ = s.tempfile.Close()
}
type GrabDownloaderSuite struct {
+1 -1
View File
@@ -12,7 +12,7 @@ type Error struct {
URL string
}
// Error
// Error returns HTTP error message
func (e *Error) Error() string {
return fmt.Sprintf("HTTP code %d while fetching %s", e.Code, e.URL)
}
+1 -1
View File
@@ -24,7 +24,7 @@ func DownloadTempWithChecksum(ctx context.Context, downloader aptly.Downloader,
if err != nil {
return nil, err
}
defer os.RemoveAll(tempdir)
defer func() { _ = os.RemoveAll(tempdir) }()
tempfile := filepath.Join(tempdir, "buffer")
+2 -2
View File
@@ -25,11 +25,11 @@ func (s *TempSuite) TearDownTest(c *C) {
func (s *TempSuite) TestDownloadTemp(c *C) {
f, err := DownloadTemp(s.ctx, s.d, s.url+"/test")
c.Assert(err, IsNil)
defer f.Close()
defer func() { _ = f.Close() }()
buf := make([]byte, 1)
f.Read(buf)
_, _ = f.Read(buf)
c.Assert(buf, DeepEquals, []byte("H"))
_, err = os.Stat(f.Name())