Skip to content

Commit

Permalink
Bump to Go 1.23 and latest linter (#68)
Browse files Browse the repository at this point in the history
* Bump to Go 1.23 and latest linter
* covet: Fix lint failures on Go 1.23
* gopages: Fix lint failures on Go 1.23
* datasize: Fix lint failures on Go 1.23
* dns: Fix lint failures on Go 1.23
* goop: Fix lint failures on Go 1.23
* pipe: Fix lint failures on Go 1.23
* dns: Use latest x/net
  • Loading branch information
JohnStarich authored Oct 2, 2024
1 parent 41bdf96 commit 4180821
Show file tree
Hide file tree
Showing 35 changed files with 235 additions and 184 deletions.
9 changes: 5 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ jobs:
- 1.20.x
- 1.21.x
- 1.22.x
- 1.23.x
include:
- platform: macos-latest
go: 1.22.x
go: 1.23.x
- platform: windows-latest
go: 1.22.x
go: 1.23.x
name: Test (Go ${{ matrix.go }} on ${{ matrix.platform }})
runs-on: ${{ matrix.platform }}
permissions:
Expand All @@ -45,9 +46,9 @@ jobs:
- name: Test
run: make -k test
env:
LINT_GO_VERSION: 1.19
LINT_GO_VERSION: 1.23
- name: Publish test coverage
if: "matrix.platform == 'ubuntu-latest' && matrix.go == '1.19.x'"
if: "matrix.platform == 'ubuntu-latest' && matrix.go == '1.23.x'"
run: |
# Fetch more commits for coverage diff.
commits=${{ github.event.pull_request.commits }}
Expand Down
7 changes: 4 additions & 3 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ linters:
- errname
- errorlint
- exhaustive
- exportloopref
- exportloopref # After minimum Go version hits 1.22, replace this with 'copyloopvar'
- gochecknoglobals
- gochecknoinits
- gocognit
- goconst
- gocritic
- gofmt
- gomnd
- gosec
- ireturn
- makezero
- misspell
- mnd
- nakedret
- nilerr
- noctx
Expand All @@ -41,7 +41,7 @@ issues:
- gochecknoglobals
# Reflection is slow, so some global reflect types are ok
- text: ".*Type is a global variable"
source: ".*Type = reflect.TypeOf\\(.*"
source: ".*Type *= *reflect.TypeOf\\(.*"
linters:
- gochecknoglobals
# False positive for paralleltest usage detection
Expand All @@ -56,6 +56,7 @@ linters-settings:
- anon
- error
- empty
- generic
- stdlib
# Also allow these common interfaces:
- github\.com\/go-git\/go-billy\/v5
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
SHELL := /usr/bin/env bash
LINT_VERSION=1.50.1
LINT_VERSION=1.61.0

MODULES = $(sort $(patsubst %/,%,$(dir $(wildcard */go.mod))))
GOLANGCI_FLAGS =
Expand Down
4 changes: 2 additions & 2 deletions covet/cmd/covet/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ func findFirstUncoveredLines(lines []covet.Line, startIndex int) (uncovered span
for _, l := range lines[nextLineIndex:] {
nextLineIndex++
if !l.Covered {
n := int64(l.LineNumber)
n := l.LineNumber
uncovered = span.Span{
Start: n,
End: n + 1,
Expand All @@ -241,7 +241,7 @@ func findFirstUncoveredLines(lines []covet.Line, startIndex int) (uncovered span
}
// find next line number jump or covered line
for _, l := range lines[nextLineIndex:] {
if l.Covered || int64(l.LineNumber) != uncovered.End {
if l.Covered || l.LineNumber != uncovered.End {
break
}
nextLineIndex++
Expand Down
4 changes: 2 additions & 2 deletions covet/cmd/covet/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func TestRun(t *testing.T) {
t.Parallel()
fs, err := mem.NewFS()
require.NoError(t, err)
require.NoError(t, fs.Mkdir("tmp", 0700))
require.NoError(t, fs.Mkdir("tmp", 0o700))
var output bytes.Buffer
err = run(
tc.args,
Expand Down Expand Up @@ -428,7 +428,7 @@ Diff coverage is below target. Add tests for these files:
}

if args.GitHubEndpoint == "replace-me" {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
}))
args.GitHubEndpoint = server.URL
Expand Down
37 changes: 27 additions & 10 deletions covet/covet.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"
"io/fs"
"math"
"path"
"sort"
"strings"
Expand Down Expand Up @@ -107,10 +108,26 @@ func (c *Covet) addDiff(diffFiles []*gitdiff.File) {
}
}

type signedInteger interface {
~int | ~int64
}

// uintFromBoundedSignedInt converts i to a uint.
// If i is outside 0 to [math.MaxUint32], then it is capped at those bounds.
func uintFromBoundedSignedInt[integer signedInteger](i integer) uint {
if i < 0 {
return 0
}
if i > math.MaxUint32 {
return math.MaxUint32
}
return uint(i)
}

func findDiffAddSpans(fragments []*gitdiff.TextFragment) []span.Span {
var spans []span.Span
for _, fragment := range fragments {
lineNumber := fragment.NewPosition
lineNumber := uintFromBoundedSignedInt(fragment.NewPosition)
for _, line := range fragment.Lines {
if line.Op == gitdiff.OpAdd {
if len(spans) == 0 || spans[len(spans)-1].End < lineNumber {
Expand All @@ -136,13 +153,13 @@ func (c *Covet) addCoverage(fs hackpadfs.FS, baseDir string, coverageFiles []*co
}
if block.Count > 0 {
c.coveredLines[coverageFile] = append(c.coveredLines[coverageFile], span.Span{
Start: int64(block.StartLine),
End: int64(block.EndLine + 1),
Start: uintFromBoundedSignedInt(block.StartLine),
End: uintFromBoundedSignedInt(block.EndLine + 1),
})
} else {
c.uncoveredLines[coverageFile] = append(c.uncoveredLines[coverageFile], span.Span{
Start: int64(block.StartLine),
End: int64(block.EndLine + 1),
Start: uintFromBoundedSignedInt(block.StartLine),
End: uintFromBoundedSignedInt(block.EndLine + 1),
})
}
}
Expand Down Expand Up @@ -213,19 +230,19 @@ func (c *Covet) DiffCoverageFiles() []File {
for i := s.Start; i < s.End; i++ {
coveredFile.Lines = append(coveredFile.Lines, Line{
Covered: true,
LineNumber: uint(i),
LineNumber: i,
})
}
coveredFile.Covered += uint(s.Len())
coveredFile.Covered += s.Len()
}
for _, s := range uncovered {
for i := s.Start; i < s.End; i++ {
coveredFile.Lines = append(coveredFile.Lines, Line{
Covered: false,
LineNumber: uint(i),
LineNumber: i,
})
}
coveredFile.Uncovered += uint(s.Len())
coveredFile.Uncovered += s.Len()
}
sort.Slice(coveredFile.Lines, func(a, b int) bool {
return coveredFile.Lines[a].LineNumber < coveredFile.Lines[b].LineNumber
Expand All @@ -240,7 +257,7 @@ func (c *Covet) DiffCoverageFiles() []File {
type ReportFileCoverageOptions struct{}

// ReportFileCoverage writes a diff-like plain text report with color to 'w'.
func (c *Covet) ReportFileCoverage(w io.Writer, f File, options ReportFileCoverageOptions) error {
func (c *Covet) ReportFileCoverage(w io.Writer, f File, _ ReportFileCoverageOptions) error {
name := path.Join(c.options.GoCoverageBaseDir, f.Name)
r, err := c.options.FS.Open(name)
if err != nil {
Expand Down
20 changes: 19 additions & 1 deletion covet/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/johnstarich/go/covet

go 1.16
go 1.18

require (
github.com/bluekeyes/go-gitdiff v0.6.1
Expand All @@ -14,3 +14,21 @@ require (
golang.org/x/oauth2 v0.0.0-20220411215720-9780585627b5
golang.org/x/tools v0.1.10
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/golang/protobuf v1.4.2 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/mattn/go-colorable v0.1.9 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 // indirect
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd // indirect
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.25.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
)
10 changes: 0 additions & 10 deletions covet/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,6 @@ github.com/googleapis/gax-go/v2 v2.0.5 h1:sjZBwGj9Jlw33ImPtvFviGYvseOtDM7hkSKB7+
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
github.com/hack-pad/go-indexeddb v0.1.0 h1:UzRAl6WiKxLJePkgi2uaQa9MMPWcjO29zI3pt9D+rNs=
github.com/hack-pad/go-indexeddb v0.1.0/go.mod h1:NH8CaojufPNcKYDhy5JkjfyBXE/72oJPeiywlabN/lM=
github.com/hack-pad/hackpadfs v0.1.3 h1:3t82qWNG5otQ3OCOvRZlVhxpQteyj9pG5kz1wE2lPEg=
github.com/hack-pad/hackpadfs v0.1.3/go.mod h1:8bsINHOQhQUioUUiCzCyZZNLfEXjs0RwBIf3lTG+CEg=
github.com/hack-pad/hackpadfs v0.1.5 h1:lcMmk2S1B/qma/2AMpnSXGv0RsTR4zRDLMITRm9cuu4=
github.com/hack-pad/hackpadfs v0.1.5/go.mod h1:8bsINHOQhQUioUUiCzCyZZNLfEXjs0RwBIf3lTG+CEg=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
Expand Down Expand Up @@ -198,7 +196,6 @@ golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8U
golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 h1:7I4JAnoQBe7ZtJcBaYHi5UtiO8tQHbUSXxL+pnGRANg=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
Expand Down Expand Up @@ -263,8 +260,6 @@ golang.org/x/net v0.0.0-20200520182314-0ba52f642ac2/go.mod h1:qpuaurCH72eLCgpAm/
golang.org/x/net v0.0.0-20200625001655-4c5254603344/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
golang.org/x/net v0.0.0-20200707034311-ab3426394381/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd h1:O7DYs+zxREGLKzKoMQrtrEacpb0ZVXA5rIwylE2Xchk=
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
Expand Down Expand Up @@ -311,22 +306,17 @@ golang.org/x/sys v0.0.0-20200511232937-7e40ca221e25/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20200515095857-1151b9dac4a9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200523222454-059865788121/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200803210538-64077c9b5642/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e h1:fLOSk5Q00efkSvAm+4xcoXD+RRmLmmulPn5I3Y9F2EM=
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
Expand Down
32 changes: 15 additions & 17 deletions covet/internal/coverfile/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"bufio"
"io"

"github.com/johnstarich/go/covet/internal/minmax"
"github.com/johnstarich/go/covet/internal/span"
)

Expand All @@ -26,9 +27,13 @@ func (f File) findContextSpans(contextLines uint) []span.Span {

var overlappingSpans []span.Span
for _, line := range f.Lines {
var start uint = 1
if contextLines < line.LineNumber { // prevent uint underflow
start = line.LineNumber - contextLines
}
overlappingSpans = append(overlappingSpans, span.Span{
Start: max(1, int64(line.LineNumber)-int64(contextLines)),
End: int64(line.LineNumber + contextLines + 1), // may be beyond end of file, but can stop line iteration at EOF
Start: minmax.Max(1, start),
End: line.LineNumber + contextLines + 1, // may be beyond end of file, but can stop line iteration at EOF
})
}
spans := []span.Span{overlappingSpans[0]}
Expand Down Expand Up @@ -77,7 +82,7 @@ func DiffChunks(file File, fileReader io.Reader) ([]DiffChunk, error) {
var chunks []DiffChunk
iter := newLineIterator(fileReader)
const contextLines = 2
var lineNumber int64 = 1
var lineNumber uint = 1
diffLineIndex := 0
for _, s := range file.findContextSpans(contextLines) {
if lineNumber < s.Start {
Expand All @@ -95,7 +100,7 @@ func DiffChunks(file File, fileReader io.Reader) ([]DiffChunk, error) {
op := noOpPrefix
if diffLineIndex < len(file.Lines) {
diffLine := file.Lines[diffLineIndex]
if lineNumber == int64(diffLine.LineNumber) {
if lineNumber == diffLine.LineNumber {
op = diffLine.diffOpPrefix()
diffLineIndex++
}
Expand All @@ -104,21 +109,14 @@ func DiffChunks(file File, fileReader io.Reader) ([]DiffChunk, error) {
lineNumber++
}
chunks = append(chunks, DiffChunk{
FirstLine: uint(s.Start),
LastLine: uint(lineNumber - 1),
FirstLine: s.Start,
LastLine: lineNumber - 1,
Lines: lines,
})
}
return chunks, nil
}

func max(a, b int64) int64 {
if a > b {
return a
}
return b
}

type lineIterator struct {
scanner *bufio.Scanner
}
Expand All @@ -129,17 +127,17 @@ func newLineIterator(r io.Reader) *lineIterator {
}
}

func (l *lineIterator) SkipLines(n int64) error {
func (l *lineIterator) SkipLines(n uint) error {
more := true
for i := int64(0); i < n && more; i++ {
for i := uint(0); i < n && more; i++ {
more = l.scanner.Scan()
}
return l.scanner.Err()
}

func (l *lineIterator) NextLines(n int64) ([]string, error) {
func (l *lineIterator) NextLines(n uint) ([]string, error) {
lines := make([]string, 0, n)
for i := int64(0); i < n; i++ {
for i := uint(0); i < n; i++ {
more := l.scanner.Scan()
if !more {
return lines, l.scanner.Err()
Expand Down
2 changes: 1 addition & 1 deletion covet/internal/coverstatus/coverage_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const (

// New categorizes the given percentage (between 0 and 1) as a coverage status
func New(f float64) Status {
//nolint:gomnd // These magic numbers are indeed arbitrary thresholds. As long as they are monotonically increasing from 0 to 1, we're ok.
//nolint:mnd // These magic numbers are indeed arbitrary thresholds. As long as they are monotonically increasing from 0 to 1, we're ok.
switch {
case f < 0.50:
return coverageError
Expand Down
Loading

0 comments on commit 4180821

Please sign in to comment.