-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
76 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Copyright (c) 2013 The Go Authors. All rights reserved. | ||
// | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file or at | ||
// https://developers.google.com/open-source/licenses/bsd. | ||
|
||
// Taken from https://github.com/golang/lint/blob/85993ffd0a6cd043291f3f63d45d656d97b165bd/lint.go | ||
|
||
// Once https://github.com/golang/go/issues/28089 is implemented (proposal is accepted), | ||
// this can be replaced with the official implementation. | ||
|
||
package scanner | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
) | ||
|
||
var ( | ||
genHdr = []byte("// Code generated ") | ||
genFtr = []byte(" DO NOT EDIT.") | ||
) | ||
|
||
// isGenerated reports whether the source file is generated code | ||
// according the rules from https://golang.org/s/generatedcode. | ||
// | ||
func isGenerated(src []byte) bool { | ||
sc := bufio.NewScanner(bytes.NewReader(src)) | ||
for sc.Scan() { | ||
b := sc.Bytes() | ||
if bytes.HasPrefix(b, genHdr) && bytes.HasSuffix(b, genFtr) && len(b) >= len(genHdr)+len(genFtr) { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
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,38 @@ | ||
// Copyright (c) 2013 The Go Authors. All rights reserved. | ||
// | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file or at | ||
// https://developers.google.com/open-source/licenses/bsd. | ||
|
||
// Taken from https://github.com/golang/lint/blob/85993ffd0a6cd043291f3f63d45d656d97b165bd/lint_test.go | ||
|
||
package scanner | ||
|
||
import "testing" | ||
|
||
func TestIsGenerated(t *testing.T) { | ||
tests := []struct { | ||
source string | ||
generated bool | ||
}{ | ||
{"// Code Generated by some tool. DO NOT EDIT.", false}, | ||
{"// Code generated by some tool. DO NOT EDIT.", true}, | ||
{"// Code generated by some tool. DO NOT EDIT", false}, | ||
{"// Code generated DO NOT EDIT.", true}, | ||
{"// Code generated DO NOT EDIT.", false}, | ||
{"\t\t// Code generated by some tool. DO NOT EDIT.\npackage foo\n", false}, | ||
{"// Code generated by some tool. DO NOT EDIT.\npackage foo\n", true}, | ||
{"package foo\n// Code generated by some tool. DO NOT EDIT.\ntype foo int\n", true}, | ||
{"package foo\n // Code generated by some tool. DO NOT EDIT.\ntype foo int\n", false}, | ||
{"package foo\n// Code generated by some tool. DO NOT EDIT. \ntype foo int\n", false}, | ||
{"package foo\ntype foo int\n// Code generated by some tool. DO NOT EDIT.\n", true}, | ||
{"package foo\ntype foo int\n// Code generated by some tool. DO NOT EDIT.", true}, | ||
} | ||
|
||
for i, test := range tests { | ||
got := isGenerated([]byte(test.source)) | ||
if got != test.generated { | ||
t.Errorf("test %d, isGenerated() = %v, want %v", i, got, test.generated) | ||
} | ||
} | ||
} |
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