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

ci: add golangci linting + fixed deprecations #212

Merged
merged 8 commits into from
Jan 25, 2023
Merged
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
12 changes: 12 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,24 @@ jobs:
- windows-latest
- macos-latest
steps:
# Related issue: https://github.com/golangci/golangci-lint/issues/580
- name: Windows - ensure LF line endings for gofmt
if: matrix.os == 'windows-latest'
run: |
git config --global core.autocrlf false
git config --global core.eol lf

- name: Check out code
uses: actions/checkout@ac593985615ec2ede58e132d2e21d2b1cbd6127c # v3.3.0
- name: Setup Go
uses: actions/setup-go@6edd4406fa81c3da01a34fa6f6343087c207a568 # v3.5.0
with:
go-version-file: 'go.mod'
- name: Run linters
uses: golangci/golangci-lint-action@08e2f20817b15149a52b5b3ebe7de50aff2ba8c5 # v3.4.0
with:
version: latest
args: --timeout=3m
- name: go vet
run: go vet ./...
- name: Run tests
Expand Down
25 changes: 25 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
issues:
max-per-linter: 0
max-same-issues: 0

linters:
disable-all: true
enable:
- durationcheck
- errcheck
- exportloopref
- forcetypeassert
- gofmt
- gosimple
- ineffassign
- makezero
- misspell
- nilerr
- paralleltest
- predeclared
- staticcheck
- tenv
- unconvert
- unparam
- unused
- vet
2 changes: 1 addition & 1 deletion internal/cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (cmd *generateCmd) Help() string {
}
})

strBuilder.WriteString(fmt.Sprintf("\nUsage: tfplugindocs generate [<args>]\n\n"))
strBuilder.WriteString("\nUsage: tfplugindocs generate [<args>]\n\n")
cmd.Flags().VisitAll(func(f *flag.Flag) {
if f.DefValue != "" {
strBuilder.WriteString(fmt.Sprintf(" --%s <ARG> %s%s%s (default: %q)\n",
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (cmd *validateCmd) Help() string {
}
})

strBuilder.WriteString(fmt.Sprintf("\nUsage: tfplugindocs validate [<args>]\n\n"))
strBuilder.WriteString("\nUsage: tfplugindocs validate [<args>]\n\n")
cmd.Flags().VisitAll(func(f *flag.Flag) {
if f.DefValue != "" {
strBuilder.WriteString(fmt.Sprintf(" --%s <ARG> %s%s%s (default: %q)\n",
Expand Down
37 changes: 9 additions & 28 deletions internal/mdplain/renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,26 +130,19 @@ func (options *Text) Emphasis(out *bytes.Buffer, text []byte) {
out.Write(text)
}

func (options *Text) Image(out *bytes.Buffer, link []byte, title []byte, alt []byte) {
return
}
func (options *Text) Image(out *bytes.Buffer, link []byte, title []byte, alt []byte) {}

func (options *Text) LineBreak(out *bytes.Buffer) {
return
}
func (options *Text) LineBreak(out *bytes.Buffer) {}

func (options *Text) Link(out *bytes.Buffer, link []byte, title []byte, content []byte) {
out.Write(content)
if !isRelativeLink(link) {
out.WriteString(" ")
out.Write(link)
}
return
}

func (options *Text) RawHtmlTag(out *bytes.Buffer, text []byte) {
return
}
func (options *Text) RawHtmlTag(out *bytes.Buffer, text []byte) {}

func (options *Text) TripleEmphasis(out *bytes.Buffer, text []byte) {
out.Write(text)
Expand All @@ -159,9 +152,7 @@ func (options *Text) StrikeThrough(out *bytes.Buffer, text []byte) {
out.Write(text)
}

func (options *Text) FootnoteRef(out *bytes.Buffer, ref []byte, id int) {
return
}
func (options *Text) FootnoteRef(out *bytes.Buffer, ref []byte, id int) {}

func (options *Text) Entity(out *bytes.Buffer, entity []byte) {
out.Write(entity)
Expand All @@ -171,25 +162,15 @@ func (options *Text) NormalText(out *bytes.Buffer, text []byte) {
out.Write(text)
}

func (options *Text) Smartypants(out *bytes.Buffer, text []byte) {
return
}
func (options *Text) Smartypants(out *bytes.Buffer, text []byte) {}

func (options *Text) DocumentHeader(out *bytes.Buffer) {
return
}
func (options *Text) DocumentHeader(out *bytes.Buffer) {}

func (options *Text) DocumentFooter(out *bytes.Buffer) {
return
}
func (options *Text) DocumentFooter(out *bytes.Buffer) {}

func (options *Text) TocHeader(text []byte, level int) {
return
}
func (options *Text) TocHeader(text []byte, level int) {}

func (options *Text) TocFinalize() {
return
}
func (options *Text) TocFinalize() {}

func doubleSpace(out *bytes.Buffer) {
if out.Len() > 0 {
Expand Down
11 changes: 5 additions & 6 deletions internal/provider/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package provider
import (
"context"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -128,7 +127,7 @@ func (g *generator) Generate(ctx context.Context) error {

switch {
case g.websiteTmpDir == "":
g.websiteTmpDir, err = ioutil.TempDir("", "tfws")
g.websiteTmpDir, err = os.MkdirTemp("", "tfws")
if err != nil {
return err
}
Expand Down Expand Up @@ -249,7 +248,7 @@ func (g *generator) renderMissingResourceDoc(providerName, name, typeName string
fallbackTmplPath = filepath.Join(g.websiteTmpDir, g.websiteSourceDir, fallbackTmplPath)
if fileExists(fallbackTmplPath) {
g.infof("resource %q fallback template exists", name)
tmplData, err := ioutil.ReadFile(fallbackTmplPath)
tmplData, err := os.ReadFile(fallbackTmplPath)
if err != nil {
return fmt.Errorf("unable to read file %q: %w", fallbackTmplPath, err)
}
Expand Down Expand Up @@ -377,7 +376,7 @@ func (g *generator) renderStaticWebsite(providerName string, providerSchema *tfj

g.infof("rendering templated website to static markdown")

err = filepath.Walk(g.websiteTmpDir, func(path string, info os.FileInfo, err error) error {
err = filepath.Walk(g.websiteTmpDir, func(path string, info os.FileInfo, _ error) error {
if info.IsDir() {
// skip directories
return nil
Expand Down Expand Up @@ -410,7 +409,7 @@ func (g *generator) renderStaticWebsite(providerName string, providerSchema *tfj

renderedPath = strings.TrimSuffix(renderedPath, ext)

tmplData, err := ioutil.ReadFile(path)
tmplData, err := os.ReadFile(path)
if err != nil {
return fmt.Errorf("unable to read file %q: %w", rel, err)
}
Expand Down Expand Up @@ -492,7 +491,7 @@ func (g *generator) terraformProviderSchema(ctx context.Context, providerName st

shortName := providerShortName(providerName)

tmpDir, err := ioutil.TempDir("", "tfws")
tmpDir, err := os.MkdirTemp("", "tfws")
if err != nil {
return nil, err
}
Expand Down
2 changes: 2 additions & 0 deletions internal/provider/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
)

func TestRenderStringTemplate(t *testing.T) {
t.Parallel()

template := `
Plainmarkdown: {{ plainmarkdown .Text }}
Split: {{ $arr := split .Text " "}}{{ index $arr 3 }}
Expand Down
5 changes: 2 additions & 3 deletions internal/provider/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package provider
import (
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
Expand Down Expand Up @@ -78,7 +77,7 @@ func writeFile(path string, data string) error {
return fmt.Errorf("unable to make dir %q: %w", dir, err)
}

err = ioutil.WriteFile(path, []byte(data), 0644)
err = os.WriteFile(path, []byte(data), 0644)
if err != nil {
return fmt.Errorf("unable to write file %q: %w", path, err)
}
Expand All @@ -90,7 +89,7 @@ func runCmd(cmd *exec.Cmd) ([]byte, error) {
output, err := cmd.CombinedOutput()
if err != nil {
log.Printf("error executing %q, %v", cmd.Path, cmd.Args)
log.Printf(string(output))
log.Print(string(output))
return nil, fmt.Errorf("error executing %q: %w", cmd.Path, err)
}
return output, nil
Expand Down
6 changes: 6 additions & 0 deletions internal/provider/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
)

func Test_resourceSchema(t *testing.T) {
t.Parallel()

cases := map[string]struct {
schemas map[string]*tfjson.Schema
providerShortName string
Expand Down Expand Up @@ -63,7 +65,11 @@ func Test_resourceSchema(t *testing.T) {
}

for name, c := range cases {
name := name
c := c
t.Run(name, func(t *testing.T) {
t.Parallel()

actualSchema, actualResourceName := resourceSchema(c.schemas, c.providerShortName, c.templateFileName)

if !cmp.Equal(c.expectedSchema, actualSchema) {
Expand Down
3 changes: 1 addition & 2 deletions internal/tmplfuncs/tmplfuncs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package tmplfuncs

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand All @@ -21,7 +20,7 @@ func CodeFile(format, file string) (string, error) {
}

fullPath := filepath.Join(wd, file)
content, err := ioutil.ReadFile(fullPath)
content, err := os.ReadFile(fullPath)
if err != nil {
return "", fmt.Errorf("unable to read content from %q: %w", file, err)
}
Expand Down
30 changes: 30 additions & 0 deletions schemamd/behaviors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
)

func TestChildAttributeIsRequired(t *testing.T) {
t.Parallel()

for _, c := range []struct {
name string
att *tfjson.SchemaAttribute
Expand All @@ -33,7 +35,10 @@ func TestChildAttributeIsRequired(t *testing.T) {
false,
},
} {
c := c
t.Run(c.name, func(t *testing.T) {
t.Parallel()

actual := childAttributeIsRequired(c.att)
if diff := cmp.Diff(c.expected, actual); diff != "" {
t.Fatalf("Unexpected diff (-wanted, +got): %s", diff)
Expand All @@ -43,6 +48,8 @@ func TestChildAttributeIsRequired(t *testing.T) {
}

func TestChildAttributeIsOptional(t *testing.T) {
t.Parallel()

for _, c := range []struct {
name string
att *tfjson.SchemaAttribute
Expand All @@ -67,7 +74,10 @@ func TestChildAttributeIsOptional(t *testing.T) {
true,
},
} {
c := c
t.Run(c.name, func(t *testing.T) {
t.Parallel()

actual := childAttributeIsOptional(c.att)
if diff := cmp.Diff(c.expected, actual); diff != "" {
t.Fatalf("Unexpected diff (-wanted, +got): %s", diff)
Expand All @@ -77,6 +87,8 @@ func TestChildAttributeIsOptional(t *testing.T) {
}

func TestChildAttributeIsReadOnly(t *testing.T) {
t.Parallel()

for _, c := range []struct {
name string
att *tfjson.SchemaAttribute
Expand Down Expand Up @@ -119,7 +131,10 @@ func TestChildAttributeIsReadOnly(t *testing.T) {
true,
},
} {
c := c
t.Run(c.name, func(t *testing.T) {
t.Parallel()

actual := childAttributeIsReadOnly(c.att)
if diff := cmp.Diff(c.expected, actual); diff != "" {
t.Fatalf("Unexpected diff (-wanted, +got): %s", diff)
Expand All @@ -129,6 +144,8 @@ func TestChildAttributeIsReadOnly(t *testing.T) {
}

func TestChildBlockIsRequired(t *testing.T) {
t.Parallel()

for _, c := range []struct {
name string
block *tfjson.SchemaBlockType
Expand Down Expand Up @@ -170,7 +187,10 @@ func TestChildBlockIsRequired(t *testing.T) {
false,
},
} {
c := c
t.Run(c.name, func(t *testing.T) {
t.Parallel()

actual := childBlockIsRequired(c.block)
if diff := cmp.Diff(c.expected, actual); diff != "" {
t.Fatalf("Unexpected diff (-wanted, +got): %s", diff)
Expand All @@ -180,6 +200,8 @@ func TestChildBlockIsRequired(t *testing.T) {
}

func TestChildBlockIsOptional(t *testing.T) {
t.Parallel()

for _, c := range []struct {
name string
block *tfjson.SchemaBlockType
Expand Down Expand Up @@ -335,7 +357,10 @@ func TestChildBlockIsOptional(t *testing.T) {
true,
},
} {
c := c
t.Run(c.name, func(t *testing.T) {
t.Parallel()

actual := childBlockIsOptional(c.block)
if diff := cmp.Diff(c.expected, actual); diff != "" {
t.Fatalf("Unexpected diff (-wanted, +got): %s", diff)
Expand All @@ -345,6 +370,8 @@ func TestChildBlockIsOptional(t *testing.T) {
}

func TestChildBlockIsReadOnly(t *testing.T) {
t.Parallel()

for _, c := range []struct {
name string
block *tfjson.SchemaBlockType
Expand Down Expand Up @@ -490,7 +517,10 @@ func TestChildBlockIsReadOnly(t *testing.T) {
true,
},
} {
c := c
t.Run(c.name, func(t *testing.T) {
t.Parallel()

actual := childBlockIsReadOnly(c.block)
if diff := cmp.Diff(c.expected, actual); diff != "" {
t.Fatalf("Unexpected diff (-wanted, +got): %s", diff)
Expand Down
Loading