diff --git a/.circleci/config.yml b/.circleci/config.yml index ae461b8b3..8d19a920f 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -3,7 +3,7 @@ version: 2 jobs: build: docker: - - image: circleci/golang:1.11.5 + - image: circleci/golang:1.13.3 environment: - DEP_VERSION: 0.5.4 diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 000000000..c906c577b --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,25 @@ +run: + deadline: 5m + +linters: + disable-all: true + enable: + - deadcode + - errcheck + - unused + - gosimple + - structcheck + - varcheck + - typecheck + - govet + - staticcheck + - ineffassign + - deadcode + - stylecheck + - interfacer + - unconvert + - goconst + - misspell + - maligned + - gocyclo + - gofmt diff --git a/Makefile b/Makefile index 533152c93..c464dfd12 100644 --- a/Makefile +++ b/Makefile @@ -10,6 +10,7 @@ all:format clean dir gen build-linux build-darwin build-windows copy test GOCMD=go GOBUILD=$(GOCMD) build +GOLANGCI_VERSION = 1.21.0 # Binary names BINARY_NAME=mbt @@ -19,13 +20,17 @@ format : go fmt ./... tools: - curl -L https://git.io/vp6lP | bash -s -- -b $(GOPATH)/bin/ v3.0.0 - gometalinter --version +tools: + @echo "download golangci-lint" + curl -sLO https://github.com/golangci/golangci-lint/releases/download/v${GOLANGCI_VERSION}/golangci-lint-${GOLANGCI_VERSION}-linux-amd64.tar.gz + tar -xzvf golangci-lint-${GOLANGCI_VERSION}-linux-amd64.tar.gz + cp golangci-lint-${GOLANGCI_VERSION}-linux-amd64/golangci-lint $(GOPATH)/bin + @echo "done" lint: @echo "Start project linting" - gometalinter --config=gometalinter.json ./... - @echo "Done" + golangci-lint run --config .golangci.yml + @echo "done linting" # execute general tests test: diff --git a/cmd/init_test.go b/cmd/init_test.go index 23cd37404..577ada06b 100644 --- a/cmd/init_test.go +++ b/cmd/init_test.go @@ -1,6 +1,7 @@ package commands import ( + "fmt" "os" . "github.com/onsi/ginkgo" @@ -10,7 +11,10 @@ import ( var _ = Describe("Init", func() { BeforeEach(func() { - os.Mkdir(getTestPath("result"), os.ModePerm) + err := os.Mkdir(getTestPath("result"), os.ModePerm) + if err != nil { + fmt.Println("error occurred during dir creation") + } }) AfterEach(func() { os.RemoveAll(getTestPath("result")) @@ -26,10 +30,16 @@ var _ = Describe("Init", func() { var _ = Describe("Build", func() { BeforeEach(func() { - os.Mkdir(getTestPath("result"), os.ModePerm) + err := os.Mkdir(getTestPath("result"), os.ModePerm) + if err != nil { + fmt.Println("error occurred during dir creation") + } }) AfterEach(func() { - os.RemoveAll(getTestPath("result")) + err := os.RemoveAll(getTestPath("result")) + if err != nil { + fmt.Println("error occurred during dir cleanup") + } }) It("Failure - wrong platform", func() { buildProjectCmdSrc = getTestPath("mta") diff --git a/cmd/root_test.go b/cmd/root_test.go index 17d706a78..cc44beead 100644 --- a/cmd/root_test.go +++ b/cmd/root_test.go @@ -1,6 +1,7 @@ package commands import ( + "fmt" "os" "path/filepath" @@ -38,7 +39,10 @@ var _ = Describe("Root", func() { Describe("Execute", func() { It("Sanity", func() { out := executeAndProvideOutput(func() { - Execute() + e := Execute() + if e != nil { + fmt.Println("error occurred during execute cmd process") + } }) Ω(out).Should(ContainSubstring("help")) Ω(out).Should(ContainSubstring("version")) diff --git a/internal/archive/fsops_test.go b/internal/archive/fsops_test.go index 7ec78387e..adc5c3061 100644 --- a/internal/archive/fsops_test.go +++ b/internal/archive/fsops_test.go @@ -56,7 +56,10 @@ var _ = Describe("FSOPS", func() { Entry("DirectoryExists", getFullPath("testdata", "level2", "level3")), ) It("Fails because file with the same name exists", func() { - CreateDirIfNotExist(getFullPath("testdata", "level2", "result")) + err := CreateDirIfNotExist(getFullPath("testdata", "level2", "result")) + if err != nil { + fmt.Println("error occurred during directory creation") + } file, _ := os.Create(getFullPath("testdata", "level2", "result", "file")) file.Close() Ω(CreateDirIfNotExist(getFullPath("testdata", "level2", "result", "file"))).Should(HaveOccurred()) @@ -325,7 +328,10 @@ var _ = Describe("FSOPS", func() { It("Sanity", func() { sourcePath := getFullPath("testdata", "level2", "level3") targetPath := getFullPath("testdata", "result") - CreateDirIfNotExist(targetPath) + err := CreateDirIfNotExist(targetPath) + if err != nil { + fmt.Println("error occurred during dir creation") + } files, _ := ioutil.ReadDir(sourcePath) // Files wrapped to overwrite their methods var filesWrapped []os.FileInfo @@ -340,7 +346,10 @@ var _ = Describe("FSOPS", func() { It("Sanity - copy in parallel", func() { sourcePath := getFullPath("testdata", "level2", "level3") targetPath := getFullPath("testdata", "result") - CreateDirIfNotExist(targetPath) + err := CreateDirIfNotExist(targetPath) + if err != nil { + fmt.Println("error occurred during dir creation") + } files, _ := ioutil.ReadDir(sourcePath) // Files wrapped to overwrite their methods var filesWrapped []os.FileInfo diff --git a/internal/artifacts/assembly_test.go b/internal/artifacts/assembly_test.go index 904326b50..2c6c4dadf 100644 --- a/internal/artifacts/assembly_test.go +++ b/internal/artifacts/assembly_test.go @@ -3,6 +3,7 @@ package artifacts import ( "archive/zip" "fmt" + "io" "io/ioutil" "os" "strings" @@ -64,10 +65,11 @@ func getFileContentFromZip(path string, filename string) ([]byte, error) { return nil, err } defer zipFile.Close() + var fc io.ReadCloser for _, file := range zipFile.File { if strings.Contains(file.Name, filename) { - fc, err := file.Open() - defer fc.Close() + fc, err = file.Open() + if err != nil { return nil, err } @@ -78,5 +80,6 @@ func getFileContentFromZip(path string, filename string) ([]byte, error) { return c, nil } } + fc.Close() return nil, fmt.Errorf(`file "%s" not found`, filename) } diff --git a/internal/artifacts/cleanup_test.go b/internal/artifacts/cleanup_test.go index c824cc58c..6f79fdc0d 100644 --- a/internal/artifacts/cleanup_test.go +++ b/internal/artifacts/cleanup_test.go @@ -2,9 +2,11 @@ package artifacts import ( "errors" - dir "github.com/SAP/cloud-mta-build-tool/internal/archive" + "fmt" "os" + dir "github.com/SAP/cloud-mta-build-tool/internal/archive" + . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" ) @@ -12,7 +14,10 @@ import ( var _ = Describe("Cleanup", func() { BeforeEach(func() { - dir.CreateDirIfNotExist(getTestPath("result", ".mtahtml5_mta_build_tmp")) + err := dir.CreateDirIfNotExist(getTestPath("result", ".mtahtml5_mta_build_tmp")) + if err != nil { + fmt.Println("error occurred during directory creation") + } }) AfterEach(func() { diff --git a/internal/artifacts/meta_test.go b/internal/artifacts/meta_test.go index 7205a61b1..d6ca5043a 100644 --- a/internal/artifacts/meta_test.go +++ b/internal/artifacts/meta_test.go @@ -274,11 +274,11 @@ func createMtahtml5TmpFolder() { func compareMTAContent(expectedFileName string, actualFileName string) { actual, err := ioutil.ReadFile(expectedFileName) Ω(err).Should(Succeed()) - actualMta, err := mta.Unmarshal([]byte(actual)) + actualMta, err := mta.Unmarshal(actual) Ω(err).Should(Succeed()) expected, err := ioutil.ReadFile(actualFileName) Ω(err).Should(Succeed()) - expectedMta, err := mta.Unmarshal([]byte(expected)) + expectedMta, err := mta.Unmarshal(expected) Ω(err).Should(Succeed()) Ω(actualMta).Should(Equal(expectedMta)) } diff --git a/internal/artifacts/module_arch_test.go b/internal/artifacts/module_arch_test.go index 9d2d832cc..9dee82026 100644 --- a/internal/artifacts/module_arch_test.go +++ b/internal/artifacts/module_arch_test.go @@ -471,7 +471,10 @@ module-types: createFileInGivenPath(filepath.Join(source, defaultDeploymentDescriptorName)) mta := generateTestMta(source, 2, 0, map[string]string{}, map[string]string{"test-module-0": "zip", "test-module-1": "folder"}) mtaBytes, _ := yaml.Marshal(mta) - ioutil.WriteFile(filepath.Join(source, defaultDeploymentDescriptorName), mtaBytes, os.ModePerm) + e := ioutil.WriteFile(filepath.Join(source, defaultDeploymentDescriptorName), mtaBytes, os.ModePerm) + if e != nil { + fmt.Println("error occurred during file creation") + } err := CopyMtaContent(source, source, nil, true, os.Getwd) Ω(err).Should(Succeed()) info, _ := os.Stat(source) @@ -482,7 +485,10 @@ module-types: createFileInGivenPath(filepath.Join(source, defaultDeploymentDescriptorName)) mta := generateTestMta(source, 1, 1, map[string]string{}, map[string]string{"test-resource-0": "zip", "test-module-0": "folder"}) mtaBytes, _ := yaml.Marshal(mta) - ioutil.WriteFile(filepath.Join(source, defaultDeploymentDescriptorName), mtaBytes, os.ModePerm) + e := ioutil.WriteFile(filepath.Join(source, defaultDeploymentDescriptorName), mtaBytes, os.ModePerm) + if e != nil { + fmt.Println("error occurred during file creation") + } err := CopyMtaContent(source, source, nil, true, os.Getwd) Ω(err).Should(Succeed()) info, _ := os.Stat(source) @@ -493,7 +499,10 @@ module-types: createFileInGivenPath(filepath.Join(source, defaultDeploymentDescriptorName)) mta := generateTestMta(source, 0, 2, map[string]string{}, map[string]string{"test-resource-0": "zip", "test-resource-1": "folder"}) mtaBytes, _ := yaml.Marshal(mta) - ioutil.WriteFile(filepath.Join(source, defaultDeploymentDescriptorName), mtaBytes, os.ModePerm) + e := ioutil.WriteFile(filepath.Join(source, defaultDeploymentDescriptorName), mtaBytes, os.ModePerm) + if e != nil { + fmt.Println("error occurred during file creation") + } err := CopyMtaContent(source, source, nil, true, os.Getwd) Ω(err).Should(Succeed()) info, _ := os.Stat(source) @@ -504,7 +513,10 @@ module-types: createFileInGivenPath(filepath.Join(source, defaultDeploymentDescriptorName)) mta := generateTestMta(source, 2, 2, map[string]string{}, map[string]string{"test-resource-0": "zip", "test-resource-1": "zip", "test-module-0": "zip", "test-module-1": "zip"}) mtaBytes, _ := yaml.Marshal(mta) - ioutil.WriteFile(filepath.Join(source, defaultDeploymentDescriptorName), mtaBytes, os.ModePerm) + e := ioutil.WriteFile(filepath.Join(source, defaultDeploymentDescriptorName), mtaBytes, os.ModePerm) + if e != nil { + fmt.Println("error occurred during file write process") + } err := CopyMtaContent(source, source, nil, false, os.Getwd) Ω(err).Should(Succeed()) info, _ := os.Stat(source) @@ -516,7 +528,10 @@ module-types: createFileInGivenPath(filepath.Join(source, defaultDeploymentDescriptorName)) mta := generateTestMta(source, 1, 0, map[string]string{"test-module-0": "test-required"}, map[string]string{"test-module-0": "folder", "test-required": "zip"}) mtaBytes, _ := yaml.Marshal(mta) - ioutil.WriteFile(filepath.Join(source, defaultDeploymentDescriptorName), mtaBytes, os.ModePerm) + e := ioutil.WriteFile(filepath.Join(source, defaultDeploymentDescriptorName), mtaBytes, os.ModePerm) + if e != nil { + fmt.Println("error occurred during file write process") + } err := CopyMtaContent(source, source, nil, false, os.Getwd) Ω(err).Should(Succeed()) info, _ := os.Stat(source) @@ -528,7 +543,10 @@ module-types: mta := generateTestMta(source, 1, 0, map[string]string{"test-module-0": "test-required"}, map[string]string{"test-module-0": "folder", "test-required": "zip"}) mta.Modules[0].Requires[0].Parameters["path"] = "zip1" mtaBytes, _ := yaml.Marshal(mta) - ioutil.WriteFile(filepath.Join(source, defaultDeploymentDescriptorName), mtaBytes, os.ModePerm) + e := ioutil.WriteFile(filepath.Join(source, defaultDeploymentDescriptorName), mtaBytes, os.ModePerm) + if e != nil { + fmt.Println("error occurred during file write process") + } err := CopyMtaContent(source, source, nil, true, os.Getwd) Ω(err).Should(HaveOccurred()) }) @@ -537,7 +555,10 @@ module-types: createFileInGivenPath(filepath.Join(source, defaultDeploymentDescriptorName)) mta := generateTestMta(source, 1, 0, map[string]string{}, map[string]string{"test-module-0": "not-existing-contet"}) mtaBytes, _ := yaml.Marshal(mta) - ioutil.WriteFile(filepath.Join(source, defaultDeploymentDescriptorName), mtaBytes, os.ModePerm) + e := ioutil.WriteFile(filepath.Join(source, defaultDeploymentDescriptorName), mtaBytes, os.ModePerm) + if e != nil { + fmt.Println("error occurred during file write process") + } err := CopyMtaContent(source, source, nil, false, os.Getwd) checkError(err, pathNotExistsMsg, "not-existing-content") info, _ := os.Stat(source) @@ -549,7 +570,10 @@ module-types: createFileInGivenPath(filepath.Join(source, defaultDeploymentDescriptorName)) mta := generateTestMta(source, 2, 0, map[string]string{}, map[string]string{"test-module-0": "not-existing-contet", "test-module-1": "zip"}) mtaBytes, _ := yaml.Marshal(mta) - ioutil.WriteFile(filepath.Join(source, defaultDeploymentDescriptorName), mtaBytes, os.ModePerm) + e := ioutil.WriteFile(filepath.Join(source, defaultDeploymentDescriptorName), mtaBytes, os.ModePerm) + if e != nil { + fmt.Println("error occurred during file write process") + } err := CopyMtaContent(source, source, nil, false, os.Getwd) checkError(err, pathNotExistsMsg, "not-existing-content") info, _ := os.Stat(source) @@ -565,7 +589,10 @@ module-types: } mta := generateTestMta(source, 10, 0, map[string]string{}, modulesWithSameContent) mtaBytes, _ := yaml.Marshal(mta) - ioutil.WriteFile(filepath.Join(source, defaultDeploymentDescriptorName), mtaBytes, os.ModePerm) + e := ioutil.WriteFile(filepath.Join(source, defaultDeploymentDescriptorName), mtaBytes, os.ModePerm) + if e != nil { + fmt.Println("error occurred during file write process") + } err := CopyMtaContent(source, source, nil, false, os.Getwd) Ω(err).Should(Succeed()) info, _ := os.Stat(source) @@ -668,12 +695,18 @@ func generateTestModule(moduleName, contentType, source string) *mta.Module { func getContentPath(contentType, source string) string { if contentType == "zip" { - dir.CopyFile(getTestPath("mta_content_copy_test", "test.zip"), filepath.Join(source, "test.zip")) + e := dir.CopyFile(getTestPath("mta_content_copy_test", "test.zip"), filepath.Join(source, "test.zip")) + if e != nil { + fmt.Println("error occurred during file copy process") + } return "test.zip" } if contentType == "folder" { - dir.CopyDir(getTestPath("mta_content_copy_test", "test-content"), + e := dir.CopyDir(getTestPath("mta_content_copy_test", "test-content"), filepath.Join(source, "test-content"), true, dir.CopyEntries) + if e != nil { + fmt.Println("error occurred during copy dir process") + } return "test-content" } diff --git a/internal/artifacts/mtad_test.go b/internal/artifacts/mtad_test.go index b152eb157..ae9d4b9fd 100644 --- a/internal/artifacts/mtad_test.go +++ b/internal/artifacts/mtad_test.go @@ -1,6 +1,7 @@ package artifacts import ( + "fmt" "os" "github.com/SAP/cloud-mta/mta" @@ -17,7 +18,10 @@ import ( var _ = Describe("Mtad", func() { BeforeEach(func() { - dir.CreateDirIfNotExist(getTestPath("result")) + e := dir.CreateDirIfNotExist(getTestPath("result")) + if e != nil { + fmt.Println("error occurred during dir creation process") + } }) AfterEach(func() { @@ -67,7 +71,10 @@ var _ = Describe("Mtad", func() { ep := dir.Loc{SourcePath: getTestPath("mta"), TargetPath: getTestPath("result")} metaPath := ep.GetMetaPath() tmpDir := ep.GetTargetTmpDir() - dir.CreateDirIfNotExist(tmpDir) + e := dir.CreateDirIfNotExist(tmpDir) + if e != nil { + fmt.Println("error occurred during dir creation process") + } file, err := os.Create(metaPath) Ω(err).Should(Succeed()) mtaBytes, err := dir.Read(&ep) diff --git a/internal/buildops/build_params_test.go b/internal/buildops/build_params_test.go index d73a45615..d4911836c 100644 --- a/internal/buildops/build_params_test.go +++ b/internal/buildops/build_params_test.go @@ -276,7 +276,10 @@ var _ = Describe("Process complex list of requirements", func() { for _, m := range mtaObj.Modules { if m.Name == "node" { for _, r := range getBuildRequires(m) { - ProcessRequirements(&lp, mtaObj, &r, "node") + err := ProcessRequirements(&lp, mtaObj, &r, "node") + if err != nil { + fmt.Println("error occurred during build process requirements ") + } } } } diff --git a/internal/buildtools/embed.go b/internal/buildtools/embed.go index 247d1065f..c6feb10d3 100644 --- a/internal/buildtools/embed.go +++ b/internal/buildtools/embed.go @@ -8,9 +8,8 @@ import ( "path/filepath" "text/template" - "github.com/pkg/errors" - "github.com/SAP/cloud-mta-build-tool/internal/archive" + "github.com/pkg/errors" ) type configInfo struct { diff --git a/internal/buildtools/embed_test.go b/internal/buildtools/embed_test.go index 0e593cec7..cd45569d0 100644 --- a/internal/buildtools/embed_test.go +++ b/internal/buildtools/embed_test.go @@ -1,6 +1,7 @@ package main import ( + "fmt" "io/ioutil" "os" "path/filepath" @@ -14,12 +15,19 @@ import ( var _ = Describe("Embed", func() { BeforeEach(func() { templatePath = "" - os.Mkdir("./testdata/result", os.ModePerm) + err := os.Mkdir("./testdata/result", os.ModePerm) + if err != nil { + fmt.Println("error occurred while trying to create new folder") + } + }) AfterEach(func() { wd, _ := os.Getwd() - os.RemoveAll(filepath.Join(wd, "testdata", "result")) + err := os.RemoveAll(filepath.Join(wd, "testdata", "result")) + if err != nil { + fmt.Println("error occurred while trying to remove test artifacts") + } }) It("sanity", func() { diff --git a/internal/commands/commands_test.go b/internal/commands/commands_test.go index 236d431d8..0b61ab694 100644 --- a/internal/commands/commands_test.go +++ b/internal/commands/commands_test.go @@ -2,10 +2,11 @@ package commands import ( "fmt" - "gopkg.in/yaml.v2" "os" "path/filepath" + "gopkg.in/yaml.v2" + . "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo/extensions/table" . "github.com/onsi/gomega" diff --git a/internal/logs/logger.go b/internal/logs/logger.go index 28685e50d..54fe5798a 100755 --- a/internal/logs/logger.go +++ b/internal/logs/logger.go @@ -5,7 +5,7 @@ import ( "os" "github.com/sirupsen/logrus" - "github.com/x-cray/logrus-prefixed-formatter" + pref "github.com/x-cray/logrus-prefixed-formatter" ) const ( @@ -28,7 +28,7 @@ func NewLogger() *logrus.Logger { logger := &logrus.Logger{ Out: os.Stdout, Level: level, - Formatter: &prefixed.TextFormatter{ + Formatter: &pref.TextFormatter{ DisableColors: false, TimestampFormat: "2006-01-02 15:04:05", FullTimestamp: true, diff --git a/internal/version/version_suite_test.go b/internal/version/version_suite_test.go index 9a9775249..29504c6d8 100644 --- a/internal/version/version_suite_test.go +++ b/internal/version/version_suite_test.go @@ -1,6 +1,7 @@ package version import ( + "fmt" "testing" . "github.com/onsi/ginkgo" @@ -19,7 +20,10 @@ var _ = Describe("Version", func() { cli_version: 5.2 makefile_version: 10.5.3 `) - yaml.Unmarshal([]byte("cli_version:5.2"), &VersionConfig) + err := yaml.Unmarshal([]byte("cli_version:5.2"), &VersionConfig) + if err != nil { + fmt.Println("error occurred during the unmarshal process") + } Ω(GetVersion()).Should(Equal(Version{CliVersion: "5.2", MakeFile: "10.5.3"})) }) }) diff --git a/main.go b/main.go index 29dddc40b..9546fb3e2 100755 --- a/main.go +++ b/main.go @@ -3,12 +3,12 @@ package main import ( "os" - "github.com/SAP/cloud-mta-build-tool/cmd" + cmd "github.com/SAP/cloud-mta-build-tool/cmd" ) func main() { // Execute CLI Root commands - err := commands.Execute() + err := cmd.Execute() if err != nil { os.Exit(1) }