From 37027e772acbbee6516be15a6d2b9cbc1dfbd245 Mon Sep 17 00:00:00 2001 From: tal-sapan Date: Sun, 24 Nov 2019 15:24:49 +0200 Subject: [PATCH] Improve error handling in tests (#603) * Assert there are no errors instead of printing the error * Fix tests that had errors because they weren't written correctly * Remove unnecessary prints in tests --- cmd/commands_suite_test.go | 15 ++-- cmd/init_test.go | 18 ++--- cmd/module_test.go | 5 +- cmd/root_test.go | 9 +-- integration/cloud_mta_build_tool_test.go | 8 +-- internal/archive/fsops_test.go | 20 ++---- internal/artifacts/assembly_test.go | 9 +-- internal/artifacts/cleanup_test.go | 6 +- internal/artifacts/manifest_test.go | 7 -- internal/artifacts/module_arch_test.go | 92 ++++++++---------------- internal/artifacts/mtad_test.go | 11 +-- internal/buildops/build_params_test.go | 5 +- internal/buildops/modules_deps_test.go | 20 +++--- internal/buildtools/embed_test.go | 12 +--- internal/version/version_suite_test.go | 21 ++++-- 15 files changed, 90 insertions(+), 168 deletions(-) diff --git a/cmd/commands_suite_test.go b/cmd/commands_suite_test.go index 651e4ed6c..8494b5cf1 100644 --- a/cmd/commands_suite_test.go +++ b/cmd/commands_suite_test.go @@ -23,12 +23,15 @@ var _ = BeforeSuite(func() { logs.Logger = logs.NewLogger() }) -func executeAndProvideOutput(execute func()) string { +func executeAndProvideOutput(execute func() error) (string, error) { old := os.Stdout // keep backup of the real stdout - r, w, _ := os.Pipe() + r, w, err := os.Pipe() + if err != nil { + return "", err + } os.Stdout = w - execute() + err = execute() outC := make(chan string) // copy the output in a separate goroutine so printing can't block indefinitely @@ -41,11 +44,11 @@ func executeAndProvideOutput(execute func()) string { outC <- buf.String() }() - // back to normal state - w.Close() os.Stdout = old // restoring the real stdout + // back to normal state + _ = w.Close() out := <-outC - return out + return out, err } func createFileInTmpFolder(projectName string, path ...string) { diff --git a/cmd/init_test.go b/cmd/init_test.go index 07c8c7797..d7738fb63 100644 --- a/cmd/init_test.go +++ b/cmd/init_test.go @@ -1,7 +1,6 @@ package commands import ( - "fmt" "os" . "github.com/onsi/ginkgo" @@ -11,13 +10,10 @@ import ( var _ = Describe("Init", func() { BeforeEach(func() { - err := os.Mkdir(getTestPath("result"), os.ModePerm) - if err != nil { - fmt.Println("error occurred during dir creation") - } + Ω(os.MkdirAll(getTestPath("result"), os.ModePerm)).Should(Succeed()) }) AfterEach(func() { - os.RemoveAll(getTestPath("result")) + Ω(os.RemoveAll(getTestPath("result"))).Should(Succeed()) }) It("Sanity", func() { initCmdSrc = getTestPath("mta") @@ -30,16 +26,10 @@ var _ = Describe("Init", func() { var _ = Describe("Build", func() { BeforeEach(func() { - err := os.Mkdir(getTestPath("result"), os.ModePerm) - if err != nil { - fmt.Println("error occurred during dir creation") - } + Ω(os.MkdirAll(getTestPath("result"), os.ModePerm)).Should(Succeed()) }) AfterEach(func() { - err := os.RemoveAll(getTestPath("result")) - if err != nil { - fmt.Println("error occurred during dir cleanup") - } + Ω(os.RemoveAll(getTestPath("result"))).Should(Succeed()) }) It("Failure - wrong platform", func() { buildCmdSrc = getTestPath("mta") diff --git a/cmd/module_test.go b/cmd/module_test.go index 894ca8069..5b3974867 100644 --- a/cmd/module_test.go +++ b/cmd/module_test.go @@ -26,10 +26,7 @@ var _ = Describe("Commands", func() { buildModuleCmdTrg = getTestPath("result") cleanupCmdTrg = getTestPath("result") logs.Logger = logs.NewLogger() - err := os.Mkdir(mtadCmdTrg, os.ModePerm) - if err != nil { - fmt.Println(err) - } + Ω(os.MkdirAll(mtadCmdTrg, os.ModePerm)).Should(Succeed()) }) AfterEach(func() { diff --git a/cmd/root_test.go b/cmd/root_test.go index cc44beead..5c9b6bead 100644 --- a/cmd/root_test.go +++ b/cmd/root_test.go @@ -1,7 +1,6 @@ package commands import ( - "fmt" "os" "path/filepath" @@ -38,12 +37,10 @@ var _ = Describe("Root", func() { Describe("Execute", func() { It("Sanity", func() { - out := executeAndProvideOutput(func() { - e := Execute() - if e != nil { - fmt.Println("error occurred during execute cmd process") - } + out, err := executeAndProvideOutput(func() error { + return Execute() }) + Ω(err).Should(Succeed()) Ω(out).Should(ContainSubstring("help")) Ω(out).Should(ContainSubstring("version")) }) diff --git a/integration/cloud_mta_build_tool_test.go b/integration/cloud_mta_build_tool_test.go index 4c7f7d013..a1362b2af 100644 --- a/integration/cloud_mta_build_tool_test.go +++ b/integration/cloud_mta_build_tool_test.go @@ -670,12 +670,8 @@ func getFileContentFromZip(path string, filename string) ([]byte, error) { if err != nil { return nil, err } - defer fc.Close() - c, err := ioutil.ReadAll(fc) - if err != nil { - return nil, err - } - return c, nil + defer fc.Close() // If we got here there won't be another loop iteration + return ioutil.ReadAll(fc) } } return nil, fmt.Errorf(`file "%s" not found`, filename) diff --git a/internal/archive/fsops_test.go b/internal/archive/fsops_test.go index 3d1f3f83b..8fd63d3bb 100644 --- a/internal/archive/fsops_test.go +++ b/internal/archive/fsops_test.go @@ -56,12 +56,10 @@ var _ = Describe("FSOPS", func() { Entry("DirectoryExists", getFullPath("testdata", "level2", "level3")), ) It("Fails because file with the same name exists", func() { - 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"))).Should(Succeed()) + file, err := os.Create(getFullPath("testdata", "level2", "result", "file")) + Ω(err).Should(Succeed()) + Ω(file.Close()).Should(Succeed()) Ω(CreateDirIfNotExist(getFullPath("testdata", "level2", "result", "file"))).Should(HaveOccurred()) }) }) @@ -380,10 +378,7 @@ var _ = Describe("FSOPS", func() { It("Sanity", func() { sourcePath := getFullPath("testdata", "level2", "level3") targetPath := getFullPath("testdata", "result") - err := CreateDirIfNotExist(targetPath) - if err != nil { - fmt.Println("error occurred during dir creation") - } + Ω(CreateDirIfNotExist(targetPath)).Should(Succeed()) files, _ := ioutil.ReadDir(sourcePath) // Files wrapped to overwrite their methods var filesWrapped []os.FileInfo @@ -397,10 +392,7 @@ var _ = Describe("FSOPS", func() { It("Sanity - copy in parallel", func() { sourcePath := getFullPath("testdata", "level2", "level3") targetPath := getFullPath("testdata", "result") - err := CreateDirIfNotExist(targetPath) - if err != nil { - fmt.Println("error occurred during dir creation") - } + Ω(CreateDirIfNotExist(targetPath)).Should(Succeed()) 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 2c6c4dadf..c7bdc9e12 100644 --- a/internal/artifacts/assembly_test.go +++ b/internal/artifacts/assembly_test.go @@ -69,17 +69,12 @@ func getFileContentFromZip(path string, filename string) ([]byte, error) { for _, file := range zipFile.File { if strings.Contains(file.Name, filename) { fc, err = file.Open() - - if err != nil { - return nil, err - } - c, err := ioutil.ReadAll(fc) if err != nil { return nil, err } - return c, nil + defer fc.Close() // If we got here there won't be another loop iteration + return ioutil.ReadAll(fc) } } - 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 6f79fdc0d..0feacb093 100644 --- a/internal/artifacts/cleanup_test.go +++ b/internal/artifacts/cleanup_test.go @@ -2,7 +2,6 @@ package artifacts import ( "errors" - "fmt" "os" dir "github.com/SAP/cloud-mta-build-tool/internal/archive" @@ -14,10 +13,7 @@ import ( var _ = Describe("Cleanup", func() { BeforeEach(func() { - err := dir.CreateDirIfNotExist(getTestPath("result", ".mtahtml5_mta_build_tmp")) - if err != nil { - fmt.Println("error occurred during directory creation") - } + Ω(dir.CreateDirIfNotExist(getTestPath("result", ".mtahtml5_mta_build_tmp"))).Should(Succeed()) }) AfterEach(func() { diff --git a/internal/artifacts/manifest_test.go b/internal/artifacts/manifest_test.go index 25d7f2857..4623616ce 100644 --- a/internal/artifacts/manifest_test.go +++ b/internal/artifacts/manifest_test.go @@ -1,7 +1,6 @@ package artifacts import ( - "fmt" "os" "strings" "text/template" @@ -105,8 +104,6 @@ var _ = Describe("manifest", func() { golden := getFileContent(getTestPath("golden_manifestBuildResult.mf")) v, _ := version.GetVersion() golden = strings.Replace(golden, "{{cli_version}}", v.CliVersion, -1) - fmt.Println(actual) - fmt.Println(golden) Ω(actual).Should(Equal(golden)) }) It("wrong content types configuration", func() { @@ -131,8 +128,6 @@ var _ = Describe("manifest", func() { golden := getFileContent(getTestPath("golden_assembly_manifest_no_paths.mf")) v, _ := version.GetVersion() golden = strings.Replace(golden, "{{cli_version}}", v.CliVersion, -1) - fmt.Println(actual) - fmt.Println(golden) Ω(actual).Should(Equal(golden)) }) It("With resources", func() { @@ -148,8 +143,6 @@ var _ = Describe("manifest", func() { golden := getFileContent(getTestPath("golden_assembly_manifest.mf")) v, _ := version.GetVersion() golden = strings.Replace(golden, "{{cli_version}}", v.CliVersion, -1) - fmt.Println(actual) - fmt.Println(golden) Ω(actual).Should(Equal(golden)) }) It("With missing module path", func() { diff --git a/internal/artifacts/module_arch_test.go b/internal/artifacts/module_arch_test.go index 9dee82026..b8e02450a 100644 --- a/internal/artifacts/module_arch_test.go +++ b/internal/artifacts/module_arch_test.go @@ -471,13 +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) - 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) + Ω(ioutil.WriteFile(filepath.Join(source, defaultDeploymentDescriptorName), mtaBytes, os.ModePerm)).Should(Succeed()) + Ω(CopyMtaContent(source, source, nil, true, os.Getwd)).Should(Succeed()) + info, err := os.Stat(source) Ω(err).Should(Succeed()) - info, _ := os.Stat(source) Ω(dirContainsAllElements(source, map[string]bool{"." + info.Name() + dir.TempFolderSuffix: true}, false)).Should(Equal(true)) Ω(dirContainsAllElements(filepath.Join(source, "."+info.Name()+dir.TempFolderSuffix), map[string]bool{"test.zip": true, "test-content": true}, true)).Should(Equal(true)) }) @@ -485,13 +482,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) - 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) + Ω(ioutil.WriteFile(filepath.Join(source, defaultDeploymentDescriptorName), mtaBytes, os.ModePerm)).Should(Succeed()) + Ω(CopyMtaContent(source, source, nil, true, os.Getwd)).Should(Succeed()) + info, err := os.Stat(source) Ω(err).Should(Succeed()) - info, _ := os.Stat(source) Ω(dirContainsAllElements(source, map[string]bool{"." + info.Name() + dir.TempFolderSuffix: true}, false)).Should(Equal(true)) Ω(dirContainsAllElements(filepath.Join(source, "."+info.Name()+dir.TempFolderSuffix), map[string]bool{"test.zip": true, "test-content": true}, true)).Should(Equal(true)) }) @@ -499,13 +493,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) - 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) + Ω(ioutil.WriteFile(filepath.Join(source, defaultDeploymentDescriptorName), mtaBytes, os.ModePerm)).Should(Succeed()) + Ω(CopyMtaContent(source, source, nil, true, os.Getwd)).Should(Succeed()) + info, err := os.Stat(source) Ω(err).Should(Succeed()) - info, _ := os.Stat(source) Ω(dirContainsAllElements(source, map[string]bool{"." + info.Name() + dir.TempFolderSuffix: true}, false)).Should(Equal(true)) Ω(dirContainsAllElements(filepath.Join(source, "."+info.Name()+dir.TempFolderSuffix), map[string]bool{"test.zip": true, "test-content": true}, true)).Should(Equal(true)) }) @@ -513,13 +504,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) - 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) + Ω(ioutil.WriteFile(filepath.Join(source, defaultDeploymentDescriptorName), mtaBytes, os.ModePerm)).Should(Succeed()) + Ω(CopyMtaContent(source, source, nil, false, os.Getwd)).Should(Succeed()) + info, err := os.Stat(source) Ω(err).Should(Succeed()) - info, _ := os.Stat(source) Ω(dirContainsAllElements(source, map[string]bool{"." + info.Name() + dir.TempFolderSuffix: true}, false)).Should(Equal(true)) Ω(dirContainsAllElements(filepath.Join(source, "."+info.Name()+dir.TempFolderSuffix), map[string]bool{"test.zip": true}, true)).Should(Equal(true)) }) @@ -528,13 +516,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) - 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) + Ω(ioutil.WriteFile(filepath.Join(source, defaultDeploymentDescriptorName), mtaBytes, os.ModePerm)).Should(Succeed()) + Ω(CopyMtaContent(source, source, nil, false, os.Getwd)).Should(Succeed()) + info, err := os.Stat(source) Ω(err).Should(Succeed()) - info, _ := os.Stat(source) Ω(dirContainsAllElements(source, map[string]bool{"." + info.Name() + dir.TempFolderSuffix: true}, false)).Should(Equal(true)) Ω(dirContainsAllElements(filepath.Join(source, "."+info.Name()+dir.TempFolderSuffix), map[string]bool{"test.zip": true, "test-content": true}, true)).Should(Equal(true)) }) @@ -543,25 +528,19 @@ 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) - 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()) + Ω(ioutil.WriteFile(filepath.Join(source, defaultDeploymentDescriptorName), mtaBytes, os.ModePerm)).Should(Succeed()) + Ω(CopyMtaContent(source, source, nil, true, os.Getwd)).Should(HaveOccurred()) }) It("With a deployment descriptor in the source directory with only one module with non-existing content", func() { 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) - e := ioutil.WriteFile(filepath.Join(source, defaultDeploymentDescriptorName), mtaBytes, os.ModePerm) - if e != nil { - fmt.Println("error occurred during file write process") - } + Ω(ioutil.WriteFile(filepath.Join(source, defaultDeploymentDescriptorName), mtaBytes, os.ModePerm)).Should(Succeed()) err := CopyMtaContent(source, source, nil, false, os.Getwd) checkError(err, pathNotExistsMsg, "not-existing-content") - info, _ := os.Stat(source) + info, err := os.Stat(source) + Ω(err).Should(Succeed()) Ω(dirContainsAllElements(source, map[string]bool{info.Name() + dir.TempFolderSuffix: true}, false)).Should(Equal(false)) Ω(dirContainsAllElements(filepath.Join(source, info.Name()+dir.TempFolderSuffix), map[string]bool{}, true)).Should(Equal(true)) }) @@ -570,13 +549,11 @@ 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) - e := ioutil.WriteFile(filepath.Join(source, defaultDeploymentDescriptorName), mtaBytes, os.ModePerm) - if e != nil { - fmt.Println("error occurred during file write process") - } + Ω(ioutil.WriteFile(filepath.Join(source, defaultDeploymentDescriptorName), mtaBytes, os.ModePerm)).Should(Succeed()) err := CopyMtaContent(source, source, nil, false, os.Getwd) checkError(err, pathNotExistsMsg, "not-existing-content") - info, _ := os.Stat(source) + info, err := os.Stat(source) + Ω(err).Should(Succeed()) Ω(dirContainsAllElements(source, map[string]bool{info.Name() + dir.TempFolderSuffix: true}, false)).Should(Equal(false)) Ω(dirContainsAllElements(filepath.Join(source, info.Name()+dir.TempFolderSuffix), map[string]bool{}, true)).Should(Equal(true)) }) @@ -589,13 +566,10 @@ module-types: } mta := generateTestMta(source, 10, 0, map[string]string{}, modulesWithSameContent) mtaBytes, _ := yaml.Marshal(mta) - 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) + Ω(ioutil.WriteFile(filepath.Join(source, defaultDeploymentDescriptorName), mtaBytes, os.ModePerm)).Should(Succeed()) + Ω(CopyMtaContent(source, source, nil, false, os.Getwd)).Should(Succeed()) + info, err := os.Stat(source) Ω(err).Should(Succeed()) - info, _ := os.Stat(source) Ω(dirContainsAllElements(source, map[string]bool{"." + info.Name() + dir.TempFolderSuffix: true}, false)).Should(Equal(true)) Ω(dirContainsAllElements(filepath.Join(source, "."+info.Name()+dir.TempFolderSuffix), map[string]bool{"test.zip": true}, true)).Should(Equal(true)) }) @@ -695,18 +669,14 @@ func generateTestModule(moduleName, contentType, source string) *mta.Module { func getContentPath(contentType, source string) string { if contentType == "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") - } + Ω(dir.CopyFile(getTestPath("mta_content_copy_test", "test.zip"), filepath.Join(source, "test.zip"))).Should(Succeed()) return "test.zip" } if contentType == "folder" { - 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") - } + Ω(dir.CopyDir( + getTestPath("mta_content_copy_test", "test-content"), filepath.Join(source, "test-content"), + true, dir.CopyEntries, + )).Should(Succeed()) return "test-content" } diff --git a/internal/artifacts/mtad_test.go b/internal/artifacts/mtad_test.go index ae9d4b9fd..faaed38d0 100644 --- a/internal/artifacts/mtad_test.go +++ b/internal/artifacts/mtad_test.go @@ -1,7 +1,6 @@ package artifacts import ( - "fmt" "os" "github.com/SAP/cloud-mta/mta" @@ -18,10 +17,7 @@ import ( var _ = Describe("Mtad", func() { BeforeEach(func() { - e := dir.CreateDirIfNotExist(getTestPath("result")) - if e != nil { - fmt.Println("error occurred during dir creation process") - } + Ω(dir.CreateDirIfNotExist(getTestPath("result"))).Should(Succeed()) }) AfterEach(func() { @@ -71,10 +67,7 @@ var _ = Describe("Mtad", func() { ep := dir.Loc{SourcePath: getTestPath("mta"), TargetPath: getTestPath("result")} metaPath := ep.GetMetaPath() tmpDir := ep.GetTargetTmpDir() - e := dir.CreateDirIfNotExist(tmpDir) - if e != nil { - fmt.Println("error occurred during dir creation process") - } + Ω(dir.CreateDirIfNotExist(tmpDir)).Should(Succeed()) 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 d50a262f3..59670e7af 100644 --- a/internal/buildops/build_params_test.go +++ b/internal/buildops/build_params_test.go @@ -281,10 +281,7 @@ var _ = Describe("Process complex list of requirements", func() { for _, m := range mtaObj.Modules { if m.Name == "node" { for _, r := range GetBuildRequires(m) { - err := ProcessRequirements(&lp, mtaObj, &r, "node") - if err != nil { - fmt.Println("error occurred during build process requirements ") - } + Ω(ProcessRequirements(&lp, mtaObj, &r, "node")).Should(Succeed()) } } } diff --git a/internal/buildops/modules_deps_test.go b/internal/buildops/modules_deps_test.go index e1aeae066..cea951427 100644 --- a/internal/buildops/modules_deps_test.go +++ b/internal/buildops/modules_deps_test.go @@ -89,12 +89,15 @@ func readFile(file string) ([]byte, error) { return content, nil } -func executeAndProvideOutput(execute func()) string { +func executeAndProvideOutput(execute func() error) (string, error) { old := os.Stdout // keep backup of the real stdout - r, w, _ := os.Pipe() + r, w, err := os.Pipe() + if err != nil { + return "", err + } os.Stdout = w - execute() + err = execute() outC := make(chan string) // copy the output in a separate goroutine so printing can't block indefinitely @@ -107,19 +110,20 @@ func executeAndProvideOutput(execute func()) string { outC <- buf.String() }() - // back to normal state - w.Close() os.Stdout = old // restoring the real stdout + // back to normal state + _ = w.Close() out := <-outC - return out + return out, err } var _ = Describe("Provide", func() { It("Valid path to yaml", func() { - out := executeAndProvideOutput(func() { - Ω(ProvideModules(filepath.Join("testdata", "mtahtml5"), "dev", nil, os.Getwd)).Should(Succeed()) + out, err := executeAndProvideOutput(func() error { + return ProvideModules(filepath.Join("testdata", "mtahtml5"), "dev", nil, os.Getwd) }) + Ω(err).Should(Succeed()) Ω(out).Should(ContainSubstring("[ui5app ui5app2]")) }) diff --git a/internal/buildtools/embed_test.go b/internal/buildtools/embed_test.go index cd45569d0..fdb05bf7e 100644 --- a/internal/buildtools/embed_test.go +++ b/internal/buildtools/embed_test.go @@ -1,7 +1,6 @@ package main import ( - "fmt" "io/ioutil" "os" "path/filepath" @@ -15,19 +14,12 @@ import ( var _ = Describe("Embed", func() { BeforeEach(func() { templatePath = "" - err := os.Mkdir("./testdata/result", os.ModePerm) - if err != nil { - fmt.Println("error occurred while trying to create new folder") - } - + Ω(os.MkdirAll("./testdata/result", os.ModePerm)).Should(Succeed()) }) AfterEach(func() { wd, _ := os.Getwd() - err := os.RemoveAll(filepath.Join(wd, "testdata", "result")) - if err != nil { - fmt.Println("error occurred while trying to remove test artifacts") - } + Ω(os.RemoveAll(filepath.Join(wd, "testdata", "result"))).Should(Succeed()) }) It("sanity", func() { diff --git a/internal/version/version_suite_test.go b/internal/version/version_suite_test.go index 29504c6d8..c56745711 100644 --- a/internal/version/version_suite_test.go +++ b/internal/version/version_suite_test.go @@ -1,12 +1,10 @@ package version import ( - "fmt" "testing" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" - "gopkg.in/yaml.v2" ) func TestVersion(t *testing.T) { @@ -15,15 +13,24 @@ func TestVersion(t *testing.T) { } var _ = Describe("Version", func() { + var config []byte + BeforeEach(func() { + config = VersionConfig + }) + AfterEach(func() { + VersionConfig = config + }) It("Sanity", func() { VersionConfig = []byte(` cli_version: 5.2 makefile_version: 10.5.3 `) - 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"})) + version, e := GetVersion() + Ω(e).Should(Succeed()) + Ω(version, e).Should(Equal(Version{CliVersion: "5.2", MakeFile: "10.5.3"})) + }) + It("parses the real version config successfully", func() { + _, e := GetVersion() + Ω(e).Should(Succeed()) }) })