This is an automated email from the ASF dual-hosted git repository. kezhenxu94 pushed a commit to branch bug/respect-licenserc-content in repository https://gitbox.apache.org/repos/asf/skywalking-eyes.git
commit 7d7b00546901c01d0977d3deecb307023ce3a554 Author: kezhenxu94 <[email protected]> AuthorDate: Wed Aug 25 10:43:23 2021 +0800 header fix: respect user configured license content --- .golangci.yml | 5 ++--- commands/root.go | 6 +----- pkg/config/config.go | 6 +----- pkg/deps/golang.go | 6 +----- pkg/deps/maven.go | 14 +++++--------- pkg/deps/maven_test.go | 13 ++++++++++--- pkg/header/check.go | 5 +---- pkg/header/config.go | 2 +- pkg/header/fix.go | 25 ++++++++++++++----------- pkg/header/fix_test.go | 51 ++++++++++++++++++++++++++++++++++++-------------- pkg/review/header.go | 6 +----- 11 files changed, 74 insertions(+), 65 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index d96a69f..ef39333 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -22,7 +22,7 @@ run: linters-settings: govet: check-shadowing: true - golint: + revive: min-confidence: 0 gocyclo: min-complexity: 20 @@ -106,12 +106,11 @@ linters: - gocyclo - gofmt - goimports - - golint + - revive - gosec - gosimple - govet - ineffassign - - interfacer - lll - misspell - nakedret diff --git a/commands/root.go b/commands/root.go index fbe6b01..361ce49 100644 --- a/commands/root.go +++ b/commands/root.go @@ -44,11 +44,7 @@ var root = &cobra.Command{ } logger.Log.SetLevel(level) - if err := Config.Parse(configFile); err != nil { - return err - } - - return nil + return Config.Parse(configFile) }, Version: version, } diff --git a/pkg/config/config.go b/pkg/config/config.go index afd8578..97d9df2 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -60,9 +60,5 @@ func (config *Config) Parse(file string) (err error) { return err } - if err := config.Deps.Finalize(file); err != nil { - return err - } - - return nil + return config.Deps.Finalize(file) } diff --git a/pkg/deps/golang.go b/pkg/deps/golang.go index 13a04d5..5f2ac5a 100644 --- a/pkg/deps/golang.go +++ b/pkg/deps/golang.go @@ -79,11 +79,7 @@ func (resolver *GoModResolver) Resolve(goModFile string, report *Report) error { logger.Log.Debugln("Module size:", len(modules)) - if err := resolver.ResolvePackages(modules, report); err != nil { - return err - } - - return nil + return resolver.ResolvePackages(modules, report) } // ResolvePackages resolves the licenses of the given packages. diff --git a/pkg/deps/maven.go b/pkg/deps/maven.go index 7f9d05c..868732b 100644 --- a/pkg/deps/maven.go +++ b/pkg/deps/maven.go @@ -70,11 +70,7 @@ func (resolver *MavenPomResolver) Resolve(mavenPomFile string, report *Report) e } } - if err := resolver.ResolveDependencies(deps, report); err != nil { - return err - } - - return nil + return resolver.ResolveDependencies(deps, report) } // CheckMVN check available maven tools, find local repositories and download all dependencies @@ -205,7 +201,7 @@ func (resolver *MavenPomResolver) ReadLicensesFromPom(pomFile string) (*PomFile, if err != nil { return nil, err } - defer file.Close() + defer func() { _ = file.Close() }() dec := xml.NewDecoder(file) dec.CharsetReader = charset.NewReaderLabel @@ -224,7 +220,7 @@ func (resolver *MavenPomResolver) ReadHeaderCommentsFromPom(pomFile string) (str if err != nil { return "", err } - defer file.Close() + defer func() { _ = file.Close() }() var comments string @@ -300,7 +296,7 @@ func LoadDependenciesTree(data []byte) []*Dependency { stack := []Elem{} unique := make(map[string]struct{}) - reFind := regexp.MustCompile(`(?im)^.*? ([\| ]*)(\+\-|\\\-) (\b.+):(\b.+):(\b.+):(\b.+):(\b.+)$`) + reFind := regexp.MustCompile(`(?im)^.*? ([| ]*)(\+-|\\-) (\b.+):(\b.+):(\b.+):(\b.+):(\b.+)$`) rawDeps := reFind.FindAllSubmatch(data, -1) deps := make([]*Dependency, 0, len(rawDeps)) @@ -436,7 +432,7 @@ func (dep *Dependency) String() string { } } - w.Flush() + _ = w.Flush() return buf.String() } diff --git a/pkg/deps/maven_test.go b/pkg/deps/maven_test.go index 906f7a3..b5b6881 100644 --- a/pkg/deps/maven_test.go +++ b/pkg/deps/maven_test.go @@ -22,9 +22,11 @@ import ( "fmt" "io/ioutil" "os" + "os/exec" "path/filepath" "testing" + "github.com/apache/skywalking-eyes/license-eye/internal/logger" "github.com/apache/skywalking-eyes/license-eye/pkg/deps" ) @@ -51,7 +53,7 @@ func dumpPomFile(fileName, content string) error { if err != nil { return err } - defer file.Close() + defer func() { _ = file.Close() }() write := bufio.NewWriter(file) _, err = write.WriteString(content) @@ -59,7 +61,7 @@ func dumpPomFile(fileName, content string) error { return err } - write.Flush() + _ = write.Flush() return nil } @@ -84,6 +86,11 @@ func destroyTmpDir(t *testing.T, dir string) { } func TestResolveMaven(t *testing.T) { + if _, err := exec.Command("mvn", "--version").Output(); err != nil { + logger.Log.Warnf("Failed to find mvn, the test `TestResolveMaven` was skipped") + return + } + resolver := new(deps.MavenPomResolver) path, err := tmpDir() @@ -130,7 +137,7 @@ func TestResolveMaven(t *testing.T) { </dependencies> </project>`, 107}, } { - dumpPomFile(pomFile, test.pomContent) + _ = dumpPomFile(pomFile, test.pomContent) if resolver.CanResolve(pomFile) { report := deps.Report{} diff --git a/pkg/header/check.go b/pkg/header/check.go index e10b318..5498f01 100644 --- a/pkg/header/check.go +++ b/pkg/header/check.go @@ -99,10 +99,7 @@ func checkPath(path string, result *Result, config *ConfigHeader) error { if p == path { // when p is symbolic link file, it causes infinite recursive calls return nil } - if err := checkPath(p, result, config); err != nil { - return err - } - return nil + return checkPath(p, result, config) }); err != nil { return err } diff --git a/pkg/header/config.go b/pkg/header/config.go index 6abb94a..900b726 100644 --- a/pkg/header/config.go +++ b/pkg/header/config.go @@ -133,7 +133,7 @@ func (config *ConfigHeader) Finalize() error { func (config *ConfigHeader) GetLicenseContent() string { if c := strings.TrimSpace(config.License.Content); c != "" { - return c + return config.License.Content // Do not change anything in user config } c, err := readLicenseFromSpdx(config) if err != nil { diff --git a/pkg/header/fix.go b/pkg/header/fix.go index 2cccf28..4e3d9a6 100644 --- a/pkg/header/fix.go +++ b/pkg/header/fix.go @@ -44,11 +44,7 @@ func Fix(file string, config *ConfigHeader, result *Result) error { return fmt.Errorf("unsupported file: %v", file) } - if err := InsertComment(file, style, config, result); err != nil { - return err - } - - return nil + return InsertComment(file, style, config, result) } func InsertComment(file string, style *comments.CommentStyle, config *ConfigHeader, result *Result) error { @@ -106,15 +102,22 @@ func GenerateLicenseHeader(style *comments.CommentStyle, config *ConfigHeader) ( return "", err } - middleLines := strings.Split(config.GetLicenseContent(), "\n") - for i, line := range middleLines { - middleLines[i] = strings.TrimRight(fmt.Sprintf("%v %v", style.Middle, line), " ") + lines := strings.Split(config.GetLicenseContent(), "\n") + for i, line := range lines { + if line != "" { + lines[i] = fmt.Sprintf("%v %v", style.Middle, line) + } else { + lines[i] = style.Middle + } + } + + if style.Start != style.Middle { + lines = append([]string{style.Start}, lines...) } - lines := fmt.Sprintf("%v\n%v\n", style.Start, strings.Join(middleLines, "\n")) if style.End != style.Middle { - lines += style.End + lines = append(lines, style.End) } - return strings.TrimSpace(lines) + "\n", nil + return strings.Join(lines, "\n") + "\n", nil } diff --git a/pkg/header/fix_test.go b/pkg/header/fix_test.go index 5ab9503..e8dce75 100644 --- a/pkg/header/fix_test.go +++ b/pkg/header/fix_test.go @@ -49,8 +49,7 @@ func TestFix(t *testing.T) { }, { filename: "Test.py", - comments: `# -# Apache License 2.0 + comments: `# Apache License 2.0 # http://www.apache.org/licenses/LICENSE-2.0 # Apache License 2.0 `, @@ -84,8 +83,7 @@ func TestRewriteContent(t *testing.T) { content: `print_string "hello worlds!\n";; `, licenseHeader: getLicenseHeader("test.ml", t.Error), - expectedContent: `(* -(* Apache License 2.0 + expectedContent: `(* Apache License 2.0 (* http://www.apache.org/licenses/LICENSE-2.0 (* Apache License 2.0 print_string "hello worlds!\n";; @@ -93,14 +91,12 @@ print_string "hello worlds!\n";; { name: "Python with Shebang", style: comments.FileCommentStyle("test.py"), - content: ` -#!/usr/bin/env python3 + content: `#!/usr/bin/env python3 if __name__ == '__main__': print('Hello World') `, licenseHeader: getLicenseHeader("test.py", t.Error), expectedContent: `#!/usr/bin/env python3 -# # Apache License 2.0 # http://www.apache.org/licenses/LICENSE-2.0 # Apache License 2.0 @@ -110,15 +106,36 @@ if __name__ == '__main__': { name: "Python", style: comments.FileCommentStyle("test.py"), - content: ` -if __name__ == '__main__': + content: `if __name__ == '__main__': print('Hello World') `, licenseHeader: getLicenseHeader("test.py", t.Error), - expectedContent: `# + expectedContent: `# Apache License 2.0 +# http://www.apache.org/licenses/LICENSE-2.0 # Apache License 2.0 +if __name__ == '__main__': + print('Hello World') +`}, + { + name: "Python with Blank Line", + style: comments.FileCommentStyle("test.py"), + content: `if __name__ == '__main__': + print('Hello World') +`, + licenseHeader: getLicenseHeaderCustomConfig("test.py", t.Error, &ConfigHeader{ + License: LicenseConfig{ + Content: `Apache License 2.0 + http://www.apache.org/licenses/LICENSE-2.0 +Apache License 2.0 + +`, + }, + }), + expectedContent: `# Apache License 2.0 # http://www.apache.org/licenses/LICENSE-2.0 # Apache License 2.0 +# +# if __name__ == '__main__': print('Hello World') `}, @@ -173,8 +190,7 @@ if __name__ == '__main__': style: comments.FileCommentStyle("test.sql"), content: `select * from user;`, licenseHeader: getLicenseHeader("test.sql", t.Error), - expectedContent: `-- --- Apache License 2.0 + expectedContent: `-- Apache License 2.0 -- http://www.apache.org/licenses/LICENSE-2.0 -- Apache License 2.0 select * from user;`}, @@ -195,8 +211,7 @@ import Foundation.Hashing.Hashable`}, content: `echo 'Hello' | echo 'world!' `, licenseHeader: getLicenseHeader("test.vim", t.Error), - expectedContent: `" -" Apache License 2.0 + expectedContent: `" Apache License 2.0 " http://www.apache.org/licenses/LICENSE-2.0 " Apache License 2.0 echo 'Hello' | echo 'world!' @@ -319,3 +334,11 @@ func getLicenseHeader(filename string, tError func(args ...interface{})) string } return s } + +func getLicenseHeaderCustomConfig(filename string, tError func(args ...interface{}), c *ConfigHeader) string { + s, err := GenerateLicenseHeader(comments.FileCommentStyle(filename), c) + if err != nil { + tError(err) + } + return s +} diff --git a/pkg/review/header.go b/pkg/review/header.go index a60660e..048eceb 100644 --- a/pkg/review/header.go +++ b/pkg/review/header.go @@ -169,11 +169,7 @@ func Header(result *header2.Result, config *config2.Config) error { } } - if err := tryReview(result, config, comments); err != nil { - return err - } - - return nil + return tryReview(result, config, comments) } func tryReview(result *header2.Result, config *config2.Config, comments []*github.DraftReviewComment) error {
