-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from thaJeztah/fix_golang
golang: add test coverage, and fix handling of files with multiple build-tags
- Loading branch information
Showing
13 changed files
with
280 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/", | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package plugin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
//go:build amd64 | ||
// +build amd64 | ||
|
||
package main |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
*/ | ||
|