Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add golangci-lint GitHub Action. Add test coverage. #2

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
fetch-depth: 0

- name: Set up Go
uses: actions/setup-go@v2
uses: actions/setup-go@v4
with:
go-version: 1.19

Expand Down
53 changes: 53 additions & 0 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: golangci-lint
on:
push:
tags:
- v*
branches:
- master
- main
pull_request:

permissions:
contents: read
# Optional: allow read access to pull request. Use with `only-new-issues` option.
# pull-requests: read

jobs:
golangci:
name: lint
runs-on: ubuntu-latest
steps:
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.19'
cache: false

- name: Checkout
uses: actions/checkout@v3

- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
# Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version
version: v1.52.2

# Optional: working directory, useful for monorepos
# working-directory: somedir

# Optional: golangci-lint command line arguments.
# args: --issues-exit-code=0

# Optional: show only new issues if it's a pull request. The default value is `false`.
# only-new-issues: true

# Optional: if set to true then the all caching functionality will be complete disabled,
# takes precedence over all other caching options.
# skip-cache: true

# Optional: if set to true then the action don't cache or restore ~/go/pkg.
# skip-pkg-cache: true

# Optional: if set to true then the action don't cache or restore ~/.cache/go-build.
# skip-build-cache: true
27 changes: 27 additions & 0 deletions _testdata/foo.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,3 +200,30 @@ func F27(r *os.File) (data []byte, err error) {
func F28(r *os.File) map[int]io.Reader {
return map[int]io.Reader{7: r}
}

func F29(r *os.File) ([]byte, error) {
s := struct {
rd io.Reader
}{rd: r}
return io.ReadAll(s.rd)
}

func F30(f *os.File) ([]byte, error) {
s := struct {
file *os.File
}{file: f}
return io.ReadAll(s.file)
}

func F31(r *os.File) ([]byte, error) {
fn := func(readers ...io.Reader) ([]byte, error) {
for _, reader := range readers {
if reader != nil {
return io.ReadAll(reader)
}
}
return nil, nil
}

return fn(nil, r)
}
4 changes: 2 additions & 2 deletions debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ func (a *analyzer) debugf(format string, args ...any) {
return
}
s := fmt.Sprintf(format, args...)
strings.TrimRight(s, "\r\n")
s = strings.TrimRight(s, "\r\n")
if a.level > 0 {
fmt.Fprintf(os.Stderr, strings.Repeat(" ", a.level))
fmt.Fprint(os.Stderr, strings.Repeat(" ", a.level))
}
fmt.Fprintln(os.Stderr, s)
}
32 changes: 19 additions & 13 deletions decouple.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,21 +349,27 @@ func (a *analyzer) stmt(stmt ast.Stmt) (ok bool) {
}
for i, rhs := range stmt.Rhs {
// xxx do a recursive analysis of how this var is used!
if a.isObj(rhs) && stmt.Tok != token.DEFINE {
if stmt.Tok != token.ASSIGN {
// Reject OP=
return false
}
tv, ok := a.pkg.TypesInfo.Types[stmt.Lhs[i]]
if !ok {
panic(errf("no type info for lvalue %d in assignment at %s", i, a.pos(stmt)))
}
intf := getInterface(tv.Type)
if intf == nil {
if a.isObj(rhs) {
switch stmt.Tok {
case token.DEFINE:
continue

case token.ASSIGN:
tv, ok := a.pkg.TypesInfo.Types[stmt.Lhs[i]]
if !ok {
panic(errf("no type info for lvalue %d in assignment at %s", i, a.pos(stmt)))
}
intf := getInterface(tv.Type)
if intf == nil {
return false
}
a.addMethods(intf)
continue

default:
// Reject OP= (e.g., foo += obj)
return false
}
a.addMethods(intf)
continue
}
if !a.expr(rhs) {
return false
Expand Down
2 changes: 1 addition & 1 deletion decouple_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
// "github.com/davecgh/go-spew/spew"
)

func TestAnalyze(t *testing.T) {
func TestCheckParam(t *testing.T) {
ctx := context.Background()

conf := &packages.Config{
Expand Down
30 changes: 30 additions & 0 deletions fab.d/fab.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package fab

import (
"context"
"os"

"github.com/bobg/fab"
"github.com/bobg/fab/deps"
"github.com/pkg/errors"
)

var (
Cover = fab.Deps(showCoverage, computeCoverage)
Clean = fab.Clean("cover.out")

showCoverage = fab.Command("go tool cover -html cover.out")
)

var computeCoverage = fab.Register("computeCoverage", "compute coverage profile", fab.F(func(ctx context.Context) error {
in, err := deps.Go(".", true)
if err != nil {
return errors.Wrap(err, "in deps.Go")
}
filesTarget := fab.Files{
Target: fab.Command("go test -coverprofile cover.out ./...", fab.CmdStdout(os.Stdout)),
In: in,
Out: []string{"cover.out"},
}
return filesTarget.Run(ctx)
}))