github.com/onsi/ginkgo@v1.16.6-0.20211118180735-4e1925ba4c95/ginkgo/internal/utils_test.go (about) 1 package internal_test 2 3 import ( 4 "os" 5 "path/filepath" 6 "strings" 7 8 . "github.com/onsi/ginkgo" 9 "github.com/onsi/ginkgo/formatter" 10 "github.com/onsi/ginkgo/ginkgo/internal" 11 . "github.com/onsi/gomega" 12 ) 13 14 var _ = Describe("Utils", func() { 15 Describe("FileExists", func() { 16 var tmpDir string 17 18 BeforeEach(func() { 19 tmpDir = GinkgoT().TempDir() 20 }) 21 22 It("returns true if the path exists", func() { 23 path := filepath.Join(tmpDir, "foo") 24 Ω(os.WriteFile(path, []byte("foo"), 0666)).Should(Succeed()) 25 Ω(internal.FileExists(path)).Should(BeTrue()) 26 }) 27 28 It("returns false if the path does not exist", func() { 29 path := filepath.Join(tmpDir, "foo") 30 Ω(internal.FileExists(path)).Should(BeFalse()) 31 }) 32 }) 33 34 Describe("Copying Files", func() { 35 var tmpDirA, tmpDirB string 36 var j = filepath.Join 37 38 BeforeEach(func() { 39 tmpDirA = GinkgoT().TempDir() 40 tmpDirB = GinkgoT().TempDir() 41 42 os.WriteFile(j(tmpDirA, "file_a"), []byte("FILE_A"), 0666) 43 os.WriteFile(j(tmpDirA, "file_b"), []byte("FILE_B"), 0777) 44 os.WriteFile(j(tmpDirB, "file_c"), []byte("FILE_C"), 0666) 45 }) 46 47 DescribeTable("it copies files, overwriting existing content and preserve permissions", 48 func(src string, dest string) { 49 src, dest = j(tmpDirA, src), j(tmpDirB, dest) 50 Ω(internal.CopyFile(src, dest)).Should(Succeed()) 51 expectedContent, err := os.ReadFile(src) 52 Ω(err).ShouldNot(HaveOccurred()) 53 Ω(os.ReadFile(dest)).Should(Equal(expectedContent)) 54 expectedStat, err := os.Stat(src) 55 stat, err := os.Stat(dest) 56 Ω(stat.Mode()).Should(Equal(expectedStat.Mode())) 57 }, 58 Entry(nil, "file_a", "file_a"), 59 Entry(nil, "file_b", "file_b"), 60 Entry(nil, "file_b", "file_c"), 61 ) 62 63 It("fails when src does not exist", func() { 64 err := internal.CopyFile(j(tmpDirA, "file_c"), j(tmpDirB, "file_c")) 65 Ω(err).Should(HaveOccurred()) 66 Ω(os.ReadFile(j(tmpDirB, "file_c"))).Should(Equal([]byte("FILE_C"))) 67 }) 68 69 It("fails when dest's directory does not exist", func() { 70 err := internal.CopyFile(j(tmpDirA, "file_a"), j(tmpDirB, "foo", "file_a")) 71 Ω(err).Should(HaveOccurred()) 72 }) 73 }) 74 75 Describe("PluralizedWord", func() { 76 It("returns singular when count is 1", func() { 77 Ω(internal.PluralizedWord("s", "p", 1)).Should(Equal("s")) 78 }) 79 80 It("returns plural when count is not 1", func() { 81 Ω(internal.PluralizedWord("s", "p", 0)).Should(Equal("p")) 82 Ω(internal.PluralizedWord("s", "p", 2)).Should(Equal("p")) 83 Ω(internal.PluralizedWord("s", "p", 10)).Should(Equal("p")) 84 }) 85 }) 86 87 Describe("FailedSuiteReport", func() { 88 var f formatter.Formatter 89 BeforeEach(func() { 90 f = formatter.New(formatter.ColorModePassthrough) 91 }) 92 93 It("generates a nicely frormatter report", func() { 94 suites := []internal.TestSuite{ 95 TS("path-A", "package-A", true, internal.TestSuiteStateFailed), 96 TS("path-B", "B", true, internal.TestSuiteStateFailedToCompile), 97 TS("path-to/package-C", "the-C-package", true, internal.TestSuiteStateFailedDueToTimeout), 98 TS("path-D", "D", true, internal.TestSuiteStatePassed), 99 TS("path-F", "E", true, internal.TestSuiteStateSkippedByFilter), 100 TS("path-F", "E", true, internal.TestSuiteStateSkippedDueToPriorFailures), 101 } 102 103 Ω(internal.FailedSuitesReport(suites, f)).Should(HavePrefix(strings.Join([]string{ 104 "There were failures detected in the following suites:", 105 " {{red}} package-A {{gray}}path-A{{/}}", 106 " {{red}} B {{gray}}path-B {{magenta}}[Compilation failure]{{/}}", 107 " {{red}}the-C-package {{gray}}path-to/package-C {{orange}}[Suite did not run because the timeout elapsed]{{/}}", 108 }, "\n"))) 109 }) 110 }) 111 })