This repository was archived by the owner on Dec 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 765
feature: add boilerplate check script #807
Merged
allencloud
merged 1 commit into
dragonflyoss:master
from
SataQiu:feature-add-boilerplate-check
Aug 14, 2019
Merged
Changes from all commits
Commits
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,129 @@ | ||
| /* | ||
| * Copyright The Dragonfly Authors. | ||
| * | ||
| * 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 | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "io/ioutil" | ||
| "os" | ||
| "strings" | ||
| ) | ||
|
|
||
| var ( | ||
| start = " * Copyright " | ||
| end = " * limitations under the License." | ||
| boilerplate = []string{ | ||
| ` * Copyright The Dragonfly Authors.`, | ||
| ` *`, | ||
| ` * 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.`, | ||
| } | ||
| ) | ||
|
|
||
| // checkBoilerplate checks if the input string contains the boilerplate | ||
| func checkBoilerplate(content string) error { | ||
| // ignore generated files | ||
| if strings.Contains(content, "DO NOT EDIT") { | ||
| return nil | ||
| } | ||
|
|
||
| index := 0 | ||
| foundStart := false | ||
| lines := strings.Split(content, "\n") | ||
| for _, line := range lines { | ||
| // find the start of the boilerplate | ||
| bpLine := boilerplate[index] | ||
| if strings.Contains(line, start) { | ||
| foundStart = true | ||
| } | ||
|
|
||
| // match line by line | ||
| if foundStart { | ||
| if line != bpLine { | ||
| return fmt.Errorf("boilerplate line %d does not match\nexpected: %q\ngot: %q", index+1, bpLine, line) | ||
| } | ||
| index++ | ||
| // exit after the last line is found | ||
| if strings.Index(line, end) == 0 { | ||
| break | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if !foundStart { | ||
| return fmt.Errorf("the file is missing a boilerplate") | ||
| } | ||
| if index < len(boilerplate) { | ||
| return errors.New("boilerplate has missing lines") | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // verifyFile verifies if a file contains the boilerplate | ||
| func verifyFile(filePath string) error { | ||
| if len(filePath) == 0 { | ||
| return errors.New("empty file name") | ||
| } | ||
|
|
||
| // check file extension is go | ||
| idx := strings.LastIndex(filePath, ".") | ||
| if idx == -1 { | ||
| return nil | ||
| } | ||
|
|
||
| // check if the file has a supported extension | ||
| ext := filePath[idx : idx+len(filePath)-idx] | ||
| if ext != ".go" { | ||
| return nil | ||
| } | ||
|
|
||
| // read the file | ||
| b, err := ioutil.ReadFile(filePath) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return checkBoilerplate(string(b)) | ||
| } | ||
|
|
||
| func main() { | ||
| if len(os.Args) < 2 { | ||
| fmt.Println("usage: go run check-boilerplate.go <path-to-file> <path-to-file> ...") | ||
| os.Exit(1) | ||
| } | ||
|
|
||
| hasErr := false | ||
| for _, filePath := range os.Args[1:] { | ||
| if err := verifyFile(filePath); err != nil { | ||
| fmt.Printf("error validating %q: %v\n", filePath, err) | ||
| hasErr = true | ||
| } | ||
| } | ||
| if hasErr { | ||
| os.Exit(1) | ||
| } | ||
| } |
This file contains hidden or 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,65 @@ | ||
| /* | ||
| * Copyright The Dragonfly Authors. | ||
| * | ||
| * 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 | ||
|
|
||
| import ( | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestVerifyBoilerPlate(t *testing.T) { | ||
| testcases := []struct { | ||
| name string | ||
| bp string | ||
| expectedError bool | ||
| }{ | ||
| { | ||
| name: "valid: boilerplate is valid", | ||
| bp: `/* | ||
| * Copyright The Dragonfly Authors. | ||
| * | ||
| * 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. | ||
| */`, | ||
| expectedError: false, | ||
| }, | ||
| { | ||
| name: "invalid: missing lines", | ||
| bp: ` | ||
| * Copyright The Dragonfly Authors. | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| `, | ||
| expectedError: true, | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range testcases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| if err := checkBoilerplate(tc.bp); err != nil != tc.expectedError { | ||
| t.Errorf("expected error: %v, got: %v, error: %v", tc.expectedError, err != nil, err) | ||
| } | ||
| }) | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
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.
It works with the check file.
While I am afraid that
make checkfails since there is no docker daemon installed on my machine. I thinkmake fileshould be friendly with developer's local machine, like windows, mac and so on. And there is no need to depend on the docker engine.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.
Yes. I am also curious whether the
./hack/check-docker.shcan be stripped out frommake check.But
hack/check.shis OK, right?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 we should strip that from
make check.hack/hack.shis OK.