Skip to content

Commit 1bd6e3e

Browse files
authored
Merge pull request #92 from github/lint-action
Set up and configure actions-based linting, and clean up some more warnings
2 parents b43d62d + 3faeb53 commit 1bd6e3e

File tree

10 files changed

+539
-35
lines changed

10 files changed

+539
-35
lines changed

.github/workflows/lint.yml

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
name: Lint
2+
on:
3+
push:
4+
paths:
5+
- "**.go"
6+
- go.mod
7+
- go.sum
8+
pull_request:
9+
paths:
10+
- "**.go"
11+
- go.mod
12+
- go.sum
13+
14+
jobs:
15+
lint:
16+
runs-on: ubuntu-latest
17+
18+
steps:
19+
- name: Set up Go
20+
uses: actions/setup-go@v2
21+
with:
22+
go-version: 1.17
23+
24+
- name: Check out code
25+
uses: actions/checkout@v2
26+
27+
- name: Verify dependencies
28+
run: |
29+
go mod verify
30+
go mod download
31+
32+
LINT_VERSION=1.43.0
33+
curl -fsSL https://github.com/golangci/golangci-lint/releases/download/v${LINT_VERSION}/golangci-lint-${LINT_VERSION}-linux-amd64.tar.gz | \
34+
tar xz --strip-components 1 --wildcards \*/golangci-lint
35+
mkdir -p bin && mv golangci-lint bin/
36+
37+
- name: Run checks
38+
run: |
39+
STATUS=0
40+
assert-nothing-changed() {
41+
local diff
42+
"$@" >/dev/null || return 1
43+
if ! diff="$(git diff -U1 --color --exit-code)"; then
44+
printf '\e[31mError: running `\e[1m%s\e[22m` results in modifications that you must check into version control:\e[0m\n%s\n\n' "$*" "$diff" >&2
45+
git checkout -- .
46+
STATUS=1
47+
fi
48+
}
49+
50+
assert-nothing-changed go fmt ./...
51+
assert-nothing-changed go mod tidy
52+
53+
bin/golangci-lint run --out-format=github-actions --timeout=3m || STATUS=$?
54+
55+
exit $STATUS

.github/workflows/test.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ jobs:
88
fail-fast: false
99
runs-on: ${{ matrix.os }}
1010
steps:
11-
- name: Setup go
11+
- name: Set up Go
1212
uses: actions/setup-go@v2
1313
with:
1414
go-version: '1.17'
1515

16-
- name: Checkout code
16+
- name: Check out code
1717
uses: actions/checkout@v2
1818

1919
- name: Get full repo history

.golangci.toml

Lines changed: 443 additions & 0 deletions
Large diffs are not rendered by default.

git-sizer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ func mainImplementation(stdout, stderr io.Writer, args []string) error {
190190

191191
err = flags.Parse(args)
192192
if err != nil {
193-
if err == pflag.ErrHelp {
193+
if errors.Is(err, pflag.ErrHelp) {
194194
return nil
195195
}
196196
return err

git_sizer_test.go

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,12 @@ import (
2727
func sizerExe(t *testing.T) string {
2828
t.Helper()
2929

30-
v := "bin/git-sizer"
30+
var v string
3131
switch runtime.GOOS {
3232
case "windows":
3333
v = `bin\git-sizer.exe`
34+
default:
35+
v = "bin/git-sizer"
3436
}
3537

3638
v, err := exec.LookPath(v)
@@ -238,19 +240,19 @@ func TestRefSelections(t *testing.T) {
238240
name: "branches-refgroup",
239241
args: []string{"--include=@mygroup"},
240242
config: []git.ConfigEntry{
241-
{"refgroup.mygroup.include", "refs/heads"},
243+
{Key: "refgroup.mygroup.include", Value: "refs/heads"},
242244
},
243245
},
244246
{ // 18
245247
name: "combination-refgroup",
246248
args: []string{"--include=@mygroup"},
247249
config: []git.ConfigEntry{
248-
{"refgroup.mygroup.include", "refs/heads"},
249-
{"refgroup.mygroup.include", "refs/tags"},
250-
{"refgroup.mygroup.exclude", "refs/heads/foo"},
251-
{"refgroup.mygroup.includeRegexp", ".*foo.*"},
252-
{"refgroup.mygroup.exclude", "refs/foo"},
253-
{"refgroup.mygroup.excludeRegexp", "refs/tags/release-.*"},
250+
{Key: "refgroup.mygroup.include", Value: "refs/heads"},
251+
{Key: "refgroup.mygroup.include", Value: "refs/tags"},
252+
{Key: "refgroup.mygroup.exclude", Value: "refs/heads/foo"},
253+
{Key: "refgroup.mygroup.includeRegexp", Value: ".*foo.*"},
254+
{Key: "refgroup.mygroup.exclude", Value: "refs/foo"},
255+
{Key: "refgroup.mygroup.excludeRegexp", Value: "refs/tags/release-.*"},
254256
},
255257
},
256258
} {
@@ -387,14 +389,14 @@ References (included references marked with '+'):
387389
config: []git.ConfigEntry{
388390
// Note that refgroup "misc" is defined implicitly.
389391

390-
{"refgroup.misc.foo.includeRegexp", ".*foo.*"},
392+
{Key: "refgroup.misc.foo.includeRegexp", Value: ".*foo.*"},
391393

392-
{"refgroup.misc.foo.oatend.includeRegexp", ".*o"},
394+
{Key: "refgroup.misc.foo.oatend.includeRegexp", Value: ".*o"},
393395

394-
{"refgroup.misc.foo.bogus.include", "bogus"},
396+
{Key: "refgroup.misc.foo.bogus.include", Value: "bogus"},
395397

396-
{"refgroup.tags.releases.name", "Releases"},
397-
{"refgroup.tags.releases.includeRegexp", "refs/tags/release-.*"},
398+
{Key: "refgroup.tags.releases.name", Value: "Releases"},
399+
{Key: "refgroup.tags.releases.includeRegexp", Value: "refs/tags/release-.*"},
398400
},
399401
stdout: `
400402
| * References | | |
@@ -420,10 +422,10 @@ References (included references marked with '+'):
420422
name: "include-refgroups",
421423
args: []string{"--include=@branches", "--include=@tags.releases", "--include=@oatend"},
422424
config: []git.ConfigEntry{
423-
{"refgroup.oatend.includeRegexp", ".*o"},
425+
{Key: "refgroup.oatend.includeRegexp", Value: ".*o"},
424426

425-
{"refgroup.tags.releases.name", "Releases"},
426-
{"refgroup.tags.releases.includeRegexp", "refs/tags/release-.*"},
427+
{Key: "refgroup.tags.releases.name", Value: "Releases"},
428+
{Key: "refgroup.tags.releases.includeRegexp", Value: "refs/tags/release-.*"},
427429
},
428430
stdout: `
429431
| * References | | |

go.mod

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/github/git-sizer
22

3-
go 1.16
3+
go 1.17
44

55
require (
66
github.com/cli/safeexec v1.0.0
@@ -10,3 +10,8 @@ require (
1010
go.uber.org/goleak v1.1.12
1111
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
1212
)
13+
14+
require (
15+
github.com/pmezard/go-difflib v1.0.0 // indirect
16+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
17+
)

internal/pipe/pipeline.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"errors"
77
"fmt"
88
"io"
9-
"io/ioutil"
109
"sync/atomic"
1110
)
1211

@@ -22,7 +21,7 @@ type Env struct {
2221
// request that the iteration be ended early (possibly without reading
2322
// all of its input). This "error" is considered a successful return,
2423
// and is not reported to the caller.
25-
//nolint:revive
24+
//nolint:errname
2625
var FinishEarly = errors.New("finish stage early")
2726

2827
// Pipeline represents a Unix-like pipe that can include multiple
@@ -134,7 +133,7 @@ func (p *Pipeline) Start(ctx context.Context) error {
134133
if p.stdin != nil {
135134
// We don't want the first stage to actually close this, and
136135
// it's not even an `io.ReadCloser`, so fake it:
137-
nextStdin = ioutil.NopCloser(p.stdin)
136+
nextStdin = io.NopCloser(p.stdin)
138137
}
139138

140139
for i, s := range p.stages {
@@ -204,7 +203,7 @@ func (p *Pipeline) Wait() error {
204203
finishedEarly = false
205204
continue
206205

207-
case err == FinishEarly:
206+
case errors.Is(err, FinishEarly):
208207
// We ignore `FinishEarly` errors because that is how a
209208
// stage informs us that it intentionally finished early.
210209
// Moreover, if we see a `FinishEarly` error, ignore any

internal/pipe/pipeline_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ func TestPipelineReadFromSlowly(t *testing.T) {
109109
go func() {
110110
time.Sleep(200 * time.Millisecond)
111111
var err error
112-
buf, err = ioutil.ReadAll(r)
112+
buf, err = io.ReadAll(r)
113113
readErr <- err
114114
}()
115115

@@ -361,7 +361,7 @@ func TestFunction(t *testing.T) {
361361
pipe.Function(
362362
"farewell",
363363
func(_ context.Context, _ pipe.Env, stdin io.Reader, stdout io.Writer) error {
364-
buf, err := ioutil.ReadAll(stdin)
364+
buf, err := io.ReadAll(stdin)
365365
if err != nil {
366366
return err
367367
}
@@ -389,7 +389,7 @@ func TestPipelineWithFunction(t *testing.T) {
389389
pipe.Function(
390390
"farewell",
391391
func(_ context.Context, _ pipe.Env, stdin io.Reader, stdout io.Writer) error {
392-
buf, err := ioutil.ReadAll(stdin)
392+
buf, err := io.ReadAll(stdin)
393393
if err != nil {
394394
return err
395395
}
@@ -419,7 +419,7 @@ func (s ErrorStartingStage) Name() string {
419419
func (s ErrorStartingStage) Start(
420420
ctx context.Context, env pipe.Env, stdin io.ReadCloser,
421421
) (io.ReadCloser, error) {
422-
return ioutil.NopCloser(&bytes.Buffer{}), s.err
422+
return io.NopCloser(&bytes.Buffer{}), s.err
423423
}
424424

425425
func (s ErrorStartingStage) Wait() error {
@@ -525,7 +525,7 @@ func TestScannerAlwaysFlushes(t *testing.T) {
525525
"compute-length",
526526
func(_ context.Context, _ pipe.Env, stdin io.Reader, _ io.Writer) error {
527527
var err error
528-
length, err = io.Copy(ioutil.Discard, stdin)
528+
length, err = io.Copy(io.Discard, stdin)
529529
return err
530530
},
531531
),
@@ -567,7 +567,7 @@ func TestScannerFinishEarly(t *testing.T) {
567567
"compute-length",
568568
func(_ context.Context, _ pipe.Env, stdin io.Reader, _ io.Writer) error {
569569
var err error
570-
length, err = io.Copy(ioutil.Discard, stdin)
570+
length, err = io.Copy(io.Discard, stdin)
571571
return err
572572
},
573573
),

internal/testutils/repoutils.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ func (repo *TestRepo) CreateObject(
196196
t.FailNow()
197197
}
198198

199-
output, err := ioutil.ReadAll(out)
199+
output, err := io.ReadAll(out)
200200
err2 = cmd.Wait()
201201
require.NoError(t, err)
202202
require.NoError(t, err2)
@@ -216,7 +216,7 @@ func (repo *TestRepo) AddFile(t *testing.T, relativePath, contents string) {
216216
if dirPath != "." {
217217
require.NoError(
218218
t,
219-
os.MkdirAll(filepath.Join(repo.Path, dirPath), 0777),
219+
os.MkdirAll(filepath.Join(repo.Path, dirPath), 0o777),
220220
"creating subdir",
221221
)
222222
}

sizes/graph.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ func (r *treeRecord) initialize(g *Graph, oid git.OID, tree *git.Tree) error {
571571
name := entry.Name
572572

573573
switch {
574-
case entry.Filemode&0170000 == 0040000:
574+
case entry.Filemode&0o170000 == 0o40000:
575575
// Tree
576576
listener := func(size TreeSize) {
577577
// This listener is called when the tree pointed to by
@@ -595,12 +595,12 @@ func (r *treeRecord) initialize(g *Graph, oid git.OID, tree *git.Tree) error {
595595
}
596596
r.entryCount.Increment(1)
597597

598-
case entry.Filemode&0170000 == 0160000:
598+
case entry.Filemode&0o170000 == 0o160000:
599599
// Commit (i.e., submodule)
600600
r.size.addSubmodule(name)
601601
r.entryCount.Increment(1)
602602

603-
case entry.Filemode&0170000 == 0120000:
603+
case entry.Filemode&0o170000 == 0o120000:
604604
// Symlink
605605
g.pathResolver.RecordTreeEntry(oid, name, entry.OID)
606606

0 commit comments

Comments
 (0)