-
Notifications
You must be signed in to change notification settings - Fork 8
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
golang: add test coverage, and fix handling of files with multiple build-tags #14
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ef10782
golang: add tests
thaJeztah 1dc4909
golang: return early on generated files
thaJeztah 6315543
golang: checkSpecialConditions: return string-slice for convenience
thaJeztah 1329872
golang: add whitespace between build-tags and header
thaJeztah 9f81529
golang: fix files with multiple build-tags
thaJeztah File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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. | ||
*/ | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you want an else break case here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah, yes, doesn't hurt I guess 😄