Skip to content

Commit

Permalink
Merge pull request #41 from gostaticanalysis/release-v2.0.3
Browse files Browse the repository at this point in the history
Release v2.0.3
  • Loading branch information
tenntenn authored Jul 12, 2021
2 parents 3c6248e + 4fb3815 commit 9efc43f
Show file tree
Hide file tree
Showing 17 changed files with 105 additions and 24 deletions.
52 changes: 52 additions & 0 deletions .github/workflows/testandvet.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: Test and Vet

on:
push:
branches:
- main
pull_request:
branches:
- main
- release-v2.*.*
release:
types:
- published
- created
- edited

defaults:
run:
shell: bash

jobs:
test:
runs-on: ubuntu-20.04

steps:
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: 1.16.x

- name: Checkout code
uses: actions/checkout@v2

- name: Cache Go module and build cache
uses: actions/cache@v2
with:
key: go-${{ hashFiles('**/go.sum') }}
path: |
~/go/pkg/mod
restore-keys: |
go-
- name: Install tennvet
run: |
GOBIN=$(pwd)/v2 go install github.com/tenntenn/tennvet@latest
- name: Test and vet
run: |
cd v2
go vet ./...
go vet -vettool=$(pwd)/tennvet ./...
go test -v -race ./...
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

skeleton creates skeleton codes for a modularized static analysis tool with [x/tools/go/analysis](https://golang.org/x/tools/go/analysis) package.

## x/tools/go/analysis pacakge
## x/tools/go/analysis package

[x/tools/go/analysis](https://golang.org/x/tools/go/analysis) package provides a type `analysis.Analyzer` which is unit of analyzers in modularized static analysis tool.

Expand Down
1 change: 1 addition & 0 deletions v2/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4 h1:myAQVi0cGEoqQVR5POX+8RR2mrocKqNN1hmeMqhX27k=
golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
Expand Down
2 changes: 1 addition & 1 deletion v2/skeleton/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func create(prompt *Prompt, path string, policy overwritePolicy) (io.WriteCloser
exist, err := isExist(path)
if err != nil {
return nil, err
}
}

if !exist {
return os.Create(path)
Expand Down
20 changes: 19 additions & 1 deletion v2/skeleton/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package skeleton
import (
"bytes"
"io/fs"
"path/filepath"
"text/template"

"github.com/josharian/txtarfs"
"golang.org/x/tools/imports"
"golang.org/x/tools/txtar"
)

Expand All @@ -18,7 +20,23 @@ func (g *Generator) Run(info *Info) (fs.FS, error) {
if err := g.template().Execute(&buf, info); err != nil {
return nil, err
}
return txtarfs.As(txtar.Parse(buf.Bytes())), nil
ar := txtar.Parse(buf.Bytes())
for i := range ar.Files {
if filepath.Ext(ar.Files[i].Name) != ".go" ||
len(bytes.TrimSpace(ar.Files[i].Data)) == 0 {
continue
}
opt := &imports.Options{
Comments: true,
FormatOnly: true,
}
src, err := imports.Process(ar.Files[i].Name, ar.Files[i].Data, opt)
if err != nil {
return nil, err
}
ar.Files[i].Data = src
}
return txtarfs.As(ar), nil
}

func (g *Generator) template() *template.Template {
Expand Down
12 changes: 6 additions & 6 deletions v2/skeleton/info.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package skeleton

type Info struct {
Kind Kind
Checker Checker
Pkg string
Path string
Cmd bool
Plugin bool
Kind Kind
Checker Checker
Pkg string
Path string
Cmd bool
Plugin bool
}
24 changes: 18 additions & 6 deletions v2/skeleton/skeleton.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ func (s *Skeleton) Run(version string, args []string) int {
}

info.Path = flags.Arg(0)
if prefix := os.Getenv("SKELETON_PREFIX"); prefix != "" {
info.Path = path.Join(prefix, info.Path)
}
// allow package name only
if module.CheckImportPath(info.Path) != nil {
flags.Usage()
Expand All @@ -73,13 +76,9 @@ func (s *Skeleton) parseFlag(args []string, info *Info) (*flag.FlagSet, error) {
flags.PrintDefaults()
}
flags.Var(&info.Checker, "checker", "[unit,single,multi]")
if info.Checker == "" {
info.Checker = CheckerUnit
}

flags.Var(&info.Kind, "kind", "[inspect,ssa,codegen]")
if info.Kind == "" {
info.Kind = KindInspect
}

flags.BoolVar(&info.Cmd, "cmd", true, "create main file")
flags.BoolVar(&info.Plugin, "plugin", false, "create golangci-lint plugin")
flags.StringVar(&info.Pkg, "pkg", "", "package name")
Expand All @@ -88,6 +87,19 @@ func (s *Skeleton) parseFlag(args []string, info *Info) (*flag.FlagSet, error) {
return nil, err
}

if info.Kind == "" {
info.Kind = KindInspect
}

if info.Checker == "" {
switch info.Kind {
case KindCodegen:
info.Checker = CheckerSingle
default:
info.Checker = CheckerUnit
}
}

return flags, nil
}

Expand Down
4 changes: 4 additions & 0 deletions v2/skeleton/skeleton_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ func TestSkeletonRun(t *testing.T) {
t.Errorf("exit code want %d got %d", tt.wantExitCode, gotExitCode)
}

if tt.wantExitCode == 0 && errout.String() != "" {
t.Error("exit code want 0 but error messages are outputed", errout.String())
}

if tt.wantOutput != "" && out.String() != tt.wantOutput {
t.Errorf("output want %s got %s", tt.wantOutput, out.String())
}
Expand Down
1 change: 0 additions & 1 deletion v2/skeleton/testdata/nooption.golden
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
)

func main() { unitchecker.Main(example.Analyzer) }

-- example/example.go --
package example

Expand Down
3 changes: 2 additions & 1 deletion v2/skeleton/testdata/onlypkgname.golden
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package main

import (
"example"

"golang.org/x/tools/go/analysis/unitchecker"
)

func main() { unitchecker.Main(example.Analyzer) }

-- example/example.go --
package example

Expand Down Expand Up @@ -56,6 +56,7 @@ import (
"testing"

"example"

"github.com/gostaticanalysis/testutil"
"golang.org/x/tools/go/analysis/analysistest"
)
Expand Down
1 change: 0 additions & 1 deletion v2/skeleton/testdata/overwrite-cancel.golden
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
)

func main() { unitchecker.Main(example.Analyzer) }

-- example/example.go --
package example

Expand Down
1 change: 0 additions & 1 deletion v2/skeleton/testdata/overwrite-confirm-no.golden
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
)

func main() { unitchecker.Main(example.Analyzer) }

-- example/example.go --
package example

Expand Down
1 change: 0 additions & 1 deletion v2/skeleton/testdata/overwrite-confirm-yes.golden
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
)

func main() { unitchecker.Main(example.Analyzer) }

-- example/example.go --
package example

Expand Down
1 change: 0 additions & 1 deletion v2/skeleton/testdata/overwrite-force.golden
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
)

func main() { unitchecker.Main(example.Analyzer) }

-- example/example.go --
package example

Expand Down
1 change: 0 additions & 1 deletion v2/skeleton/testdata/overwrite-newonly.golden
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
)

func main() { unitchecker.Main(example.Analyzer) }

-- example/example.go --
package example

Expand Down
1 change: 0 additions & 1 deletion v2/skeleton/testdata/plugin.golden
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
)

func main() { unitchecker.Main(example.Analyzer) }

-- example/example.go --
package example

Expand Down
2 changes: 1 addition & 1 deletion v2/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v2.0.2
v2.0.3

0 comments on commit 9efc43f

Please sign in to comment.