github.com/thediveo/gons@v0.9.9/reexec/testing/coverage_test.go (about) 1 // Copyright 2020 Harald Albrecht. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package testing 16 17 import ( 18 "fmt" 19 "io" 20 "io/ioutil" 21 "os" 22 23 "github.com/thediveo/gons/reexec" 24 25 . "github.com/onsi/ginkgo/v2" 26 . "github.com/onsi/gomega" 27 ) 28 29 func init() { 30 reexec.Register("foo", func() { 31 fmt.Println(`"foo done"`) 32 }) 33 } 34 35 func copyfile(from, to string) { 36 ffrom, err := os.Open(from) 37 ExpectWithOffset(1, err).NotTo(HaveOccurred()) 38 defer ffrom.Close() 39 fto, err := os.Create(to) 40 ExpectWithOffset(1, err).NotTo(HaveOccurred()) 41 defer fto.Close() 42 _, err = io.Copy(fto, ffrom) 43 ExpectWithOffset(1, err).NotTo(HaveOccurred()) 44 } 45 46 var _ = Describe("coveraging re-execution", func() { 47 48 It("re-executes action foo self-test", func() { 49 var result string 50 Expect(reexec.RunReexecAction( 51 "foo", 52 reexec.Result(&result), 53 )).To(Succeed()) 54 }) 55 56 It("outputs to directory", func() { 57 oldod := outputDir 58 defer func() { outputDir = oldod }() 59 Expect(toOutputDir("foo")).To(Equal("foo")) 60 Expect(toOutputDir("/foo")).To(Equal("/foo")) 61 outputDir = "bar" 62 Expect(toOutputDir("foo")).To(Equal("bar/foo")) 63 Expect(toOutputDir("/foo")).To(Equal("/foo")) 64 }) 65 66 It("panics on read-only coverage report", func() { 67 if os.Getegid() == 0 { 68 Skip("only non-root") 69 } 70 tmpdir, err := ioutil.TempDir("", "covreport") 71 Expect(err).NotTo(HaveOccurred()) 72 defer os.RemoveAll(tmpdir) 73 copyfile("test/cov1.cov", tmpdir+"/main.cov") 74 Expect(os.Chmod(tmpdir+"/main.cov", 0400)) 75 defer func() { _ = os.Chmod(tmpdir+"/main.cov", 0600) }() 76 Expect(func() { 77 mergeAndReportCoverages(tmpdir+"/main.cov", []string{}) 78 }).To(Panic()) 79 }) 80 81 It("parses coverage-related CLI args", func() { 82 oldod, oldcp := outputDir, coverProfile 83 defer func() { outputDir, coverProfile = oldod, oldcp }() 84 arghs := []string{ 85 "abc", 86 "-test.outputdir=bar", 87 "-test.coverprofile=foo", 88 "-args", 89 "-test.outputdir=xxx", 90 } 91 parseCoverageArgs(arghs) 92 Expect(outputDir).To(Equal("bar")) 93 Expect(coverProfile).To(Equal("foo")) 94 }) 95 96 It("merges coverage reports and writes merged report", func() { 97 tmpdir, err := ioutil.TempDir("", "covreport") 98 Expect(err).NotTo(HaveOccurred()) 99 defer os.RemoveAll(tmpdir) 100 copyfile("test/cov1.cov", tmpdir+"/main.cov") 101 102 mergeAndReportCoverages( 103 tmpdir+"/main.cov", 104 []string{"test/cov2.cov"}) 105 actualfinalreport, err := ioutil.ReadFile(tmpdir + "/main.cov") 106 Expect(err).NotTo(HaveOccurred()) 107 finalreport, err := ioutil.ReadFile("test/final.cov") 108 Expect(err).NotTo(HaveOccurred()) 109 Expect(string(actualfinalreport)).To(Equal(string(finalreport))) 110 }) 111 112 })