Skip to content

Commit

Permalink
Merge pull request #14 from thaJeztah/fix_golang
Browse files Browse the repository at this point in the history
golang: add test coverage, and fix handling of files with multiple build-tags
  • Loading branch information
kunalkushwaha authored Oct 2, 2023
2 parents 780aee2 + 9f81529 commit b0cfa33
Show file tree
Hide file tree
Showing 13 changed files with 280 additions and 31 deletions.
63 changes: 32 additions & 31 deletions golang.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,18 @@ func (g *golangApplier) CheckHeader(target *os.File, t *TagContext) (bool, error
}
target.Seek(0, 0)

if cFlags == AutoGenerated {
return true, nil
}

tbuf, err := os.ReadFile(filepath.Join(t.templatePath, "go.txt"))
if err != nil {
return false, err
}

if cFlags == AutoGenerated {
return true, nil
}

var templateBuf string
if cFlags == CompilerFlags {
for _, cbuf := range cbufs {
templateBuf += string(cbuf) + "\n"
}
templateBuf = strings.Join(cbufs, "\n") + "\n"
templateBuf += "\n"
templateBuf += string(tbuf)
} else {
Expand Down Expand Up @@ -95,10 +93,10 @@ func (g *golangApplier) ApplyHeader(path string, t *TagContext) error {
reader := bufio.NewReader(file)
if sFlags == CompilerFlags {
for _, f := range flags {
tFile.Write(f)
tFile.Write([]byte("\n"))
tFile.WriteString(f + "\n")
_, _, err = reader.ReadLine()
}
tFile.WriteString("\n")
_, _, err = reader.ReadLine()
}

Expand All @@ -124,37 +122,40 @@ func (g *golangApplier) ApplyHeader(path string, t *TagContext) error {
return nil
}

func (g *golangApplier) checkSpecialConditions(target *os.File) (uint8, [][]byte, error) {
func (g *golangApplier) checkSpecialConditions(target *os.File) (uint8, []string, error) {
reader := bufio.NewReader(target)
buf, _, err := reader.ReadLine()
if err != nil {
return NormalFiles, nil, err
}
line := string(buf)

// Go 1.17 compiler flags (e.g., `//go:build !windows`)
if strings.HasPrefix(string(buf), "//go:") {
// read next line too: (`// +build !windows`)
if buf2, _, err := reader.ReadLine(); err == nil && strings.HasPrefix(string(buf2), "// +build ") {
return CompilerFlags, [][]byte{buf, buf2}, nil
}
return CompilerFlags, [][]byte{buf}, nil
if strings.HasPrefix(line, "//") && strings.Contains(line, "DO NOT EDIT") {
return AutoGenerated, nil, nil
}

// Old compiler flags (e.g., `// +build !windows`)
var buildTags []string

// Go 1.17 compiler flags (e.g., `//go:build !windows`), or old compiler
// flags (e.g., `// +build !windows`).
// checks for Package comments as per https://blog.golang.org/godoc-documenting-go-code
if strings.HasPrefix(string(buf), "//") &&
(strings.Contains(string(buf), "build") ||
strings.Contains(string(buf), "unix") ||
strings.Contains(string(buf), "linux") ||
strings.Contains(string(buf), "windows") ||
strings.Contains(string(buf), "darwin") ||
strings.Contains(string(buf), "freebsd")) &&
!strings.Contains(string(buf), "Package") {
return CompilerFlags, [][]byte{buf}, nil
}
if strings.HasPrefix(string(buf), "//") &&
(strings.Contains(string(buf), "DO NOT EDIT")) {
return AutoGenerated, nil, nil
if strings.HasPrefix(line, "//go:") || strings.HasPrefix(line, "// +build") {
buildTags = append(buildTags, line)

// read next line too: (`// +build !windows`)
for {
buf2, _, err := reader.ReadLine()
if err != nil {
break
}
if strings.HasPrefix(string(buf2), "// +build ") {
buildTags = append(buildTags, string(buf2))
} else {
break
}
}
return CompilerFlags, buildTags, nil
}

return NormalFiles, nil, nil
}
118 changes: 118 additions & 0 deletions golang_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package main

import (
"os"
"path/filepath"
"testing"
)

func TestGolangApplier_ApplyHeader(t *testing.T) {
tc := newGolangTagContext(t)
defer func() { _ = tc.templateFiles.dTemplateFile.Close() }()

tmpDir := t.TempDir()

files := []string{
"go.basic",
"go.generated",
"go.single-buildtag",
"go.multiple-buildtags",
"go.multiple-buildtags-legacy",
}

g := golangApplier{}
for _, f := range files {
fileName := f
t.Run(fileName, func(t *testing.T) {
testfile := filepath.Join(tmpDir, fileName)
copyFile(t, "./testdata/"+fileName, testfile)

err := g.ApplyHeader(testfile, &tc)
if err != nil {
t.Fatalf("failed to apply header to %s: %+v", testfile, err)
}
compareFiles(t, testfile, "./testdata/"+fileName+".golden")
})
}
}

func TestGolangApplier_CheckHeader(t *testing.T) {
tests := []struct {
fileName string
expected bool
}{
{
fileName: "go.basic",
expected: false,
},
{
fileName: "go.generated",
expected: true, // Generated files are not checked, and always "ok"
},
{
fileName: "go.single-buildtag",
expected: false,
},
{
fileName: "go.multiple-buildtags",
expected: false,
},
{
fileName: "go.multiple-buildtags-legacy",
expected: false,
},
{
fileName: "go.basic.golden",
expected: true, // Generated files are not checked, and always "ok"
},
{
fileName: "go.generated.golden",
expected: true, // Generated files are not checked, and always "ok"
},
{
fileName: "go.single-buildtag.golden",
expected: true,
},
{
fileName: "go.multiple-buildtags.golden",
expected: true,
},
{
fileName: "go.multiple-buildtags-legacy.golden",
expected: true,
},
}
g := golangApplier{}
tagContext := newGolangTagContext(t)
defer func() { _ = tagContext.templateFiles.dTemplateFile.Close() }()
for _, tc := range tests {
tc := tc
t.Run(tc.fileName, func(t *testing.T) {
f, err := os.Open("./testdata/" + tc.fileName)
if err != nil {
t.Fatalf("failed to open file %s: %+v", tc.fileName, err)
}
defer func() { _ = f.Close() }()
found, err := g.CheckHeader(f, &tagContext)

if err != nil {
t.Fatalf("failed to check header: %+v", err)
}
if found != tc.expected {
t.Fail()
}
})
}
}

func newGolangTagContext(t *testing.T) TagContext {
t.Helper()
templateFile, err := loadTemplate("./testdata/templates/", "go.txt")
if err != nil {
t.Fatalf("failed to load Go template")
}
return TagContext{
templateFiles: TemplateFiles{goTemplateFile: templateFile},
templatePath: "./testdata/templates/",
}
}
1 change: 1 addition & 0 deletions testdata/go.basic
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package plugin
17 changes: 17 additions & 0 deletions testdata/go.basic.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
Copyright <YYYY> <Organization Name>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package plugin
7 changes: 7 additions & 0 deletions testdata/go.generated
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// protoc-gen-go v1.28.1
// protoc v3.20.1
// source: github.com/containerd/containerd/api/types/metrics.proto

package types
7 changes: 7 additions & 0 deletions testdata/go.generated.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// protoc-gen-go v1.28.1
// protoc v3.20.1
// source: github.com/containerd/containerd/api/types/metrics.proto

package types
7 changes: 7 additions & 0 deletions testdata/go.multiple-buildtags
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//go:build (amd64 || arm64) && !static_build && !gccgo
// +build amd64 arm64
// +build !static_build
// +build !gccgo

// Package plugin does something.
package plugin
6 changes: 6 additions & 0 deletions testdata/go.multiple-buildtags-legacy
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// +build amd64 arm64
// +build !static_build
// +build !gccgo

// Package plugin does something.
package plugin
22 changes: 22 additions & 0 deletions testdata/go.multiple-buildtags-legacy.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// +build amd64 arm64
// +build !static_build
// +build !gccgo

/*
Copyright <YYYY> <Organization Name>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// Package plugin does something.
package plugin
23 changes: 23 additions & 0 deletions testdata/go.multiple-buildtags.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//go:build (amd64 || arm64) && !static_build && !gccgo
// +build amd64 arm64
// +build !static_build
// +build !gccgo

/*
Copyright <YYYY> <Organization Name>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// Package plugin does something.
package plugin
4 changes: 4 additions & 0 deletions testdata/go.single-buildtag
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
//go:build amd64
// +build amd64

package main
20 changes: 20 additions & 0 deletions testdata/go.single-buildtag.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//go:build amd64
// +build amd64

/*
Copyright <YYYY> <Organization Name>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main
16 changes: 16 additions & 0 deletions testdata/templates/go.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
Copyright <YYYY> <Organization Name>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

0 comments on commit b0cfa33

Please sign in to comment.