Skip to content

Commit

Permalink
Copy, do not move, binary when generating profiles and output-dir is …
Browse files Browse the repository at this point in the history
…set and using a precompiled binary
  • Loading branch information
onsi committed Nov 11, 2021
1 parent c5d38cf commit 3d28a52
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 12 deletions.
11 changes: 8 additions & 3 deletions ginkgo/internal/profiles_and_reports.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,14 @@ func FinalizeProfilesAndReportsForSuites(suites TestSuites, cliConfig types.CLIC
if goFlagsConfig.BinaryMustBePreserved() {
src := suite.PathToCompiledTest
dst := filepath.Join(cliConfig.OutputDir, suite.NamespacedName()+".test")
err := os.Rename(src, dst)
if err != nil {
return messages, err
if suite.Precompiled {
if err := CopyFile(src, dst); err != nil {
return messages, err
}
} else {
if err := os.Rename(src, dst); err != nil {
return messages, err
}
}
}
profiles := []string{goFlagsConfig.BlockProfile, goFlagsConfig.CPUProfile, goFlagsConfig.MemProfile, goFlagsConfig.MutexProfile}
Expand Down
32 changes: 32 additions & 0 deletions ginkgo/internal/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package internal

import (
"fmt"
"io"
"os"
"os/exec"

Expand All @@ -14,6 +15,37 @@ func FileExists(path string) bool {
return err == nil
}

func CopyFile(src string, dest string) error {
srcFile, err := os.Open(src)
if err != nil {
return err
}

srcStat, err := srcFile.Stat()
if err != nil {
return err
}

if _, err := os.Stat(dest); err == nil {
os.Remove(dest)
}

destFile, err := os.OpenFile(dest, os.O_WRONLY|os.O_CREATE, srcStat.Mode())
if err != nil {
return err
}

_, err = io.Copy(destFile, srcFile)
if err != nil {
return err
}

if err := srcFile.Close(); err != nil {
return err
}
return destFile.Close()
}

func GoFmt(path string) {
out, err := exec.Command("go", "fmt", path).CombinedOutput()
if err != nil {
Expand Down
49 changes: 42 additions & 7 deletions ginkgo/internal/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,7 @@ var _ = Describe("Utils", func() {
var tmpDir string

BeforeEach(func() {
var err error
tmpDir, err = os.MkdirTemp("/tmp", "ginkgo")
Ω(err).ShouldNot(HaveOccurred())
})

AfterEach(func() {
Ω(os.RemoveAll(tmpDir)).Should(Succeed())
tmpDir = GinkgoT().TempDir()
})

It("returns true if the path exists", func() {
Expand All @@ -37,6 +31,47 @@ var _ = Describe("Utils", func() {
})
})

Describe("Copying Files", func() {
var tmpDirA, tmpDirB string
var j = filepath.Join

BeforeEach(func() {
tmpDirA = GinkgoT().TempDir()
tmpDirB = GinkgoT().TempDir()

os.WriteFile(j(tmpDirA, "file_a"), []byte("FILE_A"), 0666)
os.WriteFile(j(tmpDirA, "file_b"), []byte("FILE_B"), 0777)
os.WriteFile(j(tmpDirB, "file_c"), []byte("FILE_C"), 0666)
})

DescribeTable("it copies files, overwriting existing content and preserve permissions",
func(src string, dest string) {
src, dest = j(tmpDirA, src), j(tmpDirB, dest)
Ω(internal.CopyFile(src, dest)).Should(Succeed())
expectedContent, err := os.ReadFile(src)
Ω(err).ShouldNot(HaveOccurred())
Ω(os.ReadFile(dest)).Should(Equal(expectedContent))
expectedStat, err := os.Stat(src)
stat, err := os.Stat(dest)
Ω(stat.Mode()).Should(Equal(expectedStat.Mode()))
},
Entry(nil, "file_a", "file_a"),
Entry(nil, "file_b", "file_b"),
Entry(nil, "file_b", "file_c"),
)

It("fails when src does not exist", func() {
err := internal.CopyFile(j(tmpDirA, "file_c"), j(tmpDirB, "file_c"))
Ω(err).Should(HaveOccurred())
Ω(os.ReadFile(j(tmpDirB, "file_c"))).Should(Equal([]byte("FILE_C")))
})

It("fails when dest's directory does not exist", func() {
err := internal.CopyFile(j(tmpDirA, "file_a"), j(tmpDirB, "foo", "file_a"))
Ω(err).Should(HaveOccurred())
})
})

Describe("PluralizedWord", func() {
It("returns singular when count is 1", func() {
Ω(internal.PluralizedWord("s", "p", 1)).Should(Equal("s"))
Expand Down
3 changes: 2 additions & 1 deletion ginkgo/performance/performance_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ func (f PerformanceFixtureManager) PathTo(pkg string, target ...string) string {
if len(target) == 0 {
return filepath.Join(f.TmpDir, pkg)
}
return filepath.Join(f.TmpDir, pkg, target[0])
components := append([]string{f.TmpDir, pkg}, target...)
return filepath.Join(components...)
}

/* GoModDownload runs go mod download for a given package */
Expand Down
3 changes: 2 additions & 1 deletion integration/integration_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ func (f FixtureManager) PathTo(pkg string, target ...string) string {
if len(target) == 0 {
return filepath.Join(f.TmpDir, pkg)
}
return filepath.Join(f.TmpDir, pkg, target[0])
components := append([]string{f.TmpDir, pkg}, target...)
return filepath.Join(components...)
}

func (f FixtureManager) PathToFixtureFile(pkg string, target string) string {
Expand Down
14 changes: 14 additions & 0 deletions integration/profiling_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,5 +261,19 @@ var _ = Describe("Profiling Specs", func() {
"-nodes=3", "--output-dir=./profiles",
),
)

Context("when profiling a precompiled binary and output-dir is set", func() {
It("copies (not moves) the binary to output-dir", func() {
Eventually(startGinkgo(fm.PathTo("profile"), "build", "-r")).Should(gexec.Exit(0))
session := startGinkgo(fm.PathTo("profile"), "--cpuprofile=cpu.out", "--output-dir=./profiles", "./slow_memory_hog/slow_memory_hog.test", "./lock_contest/lock_contest.test", "./block_contest/block_contest.test")
Eventually(session).Should(gexec.Exit(0))

for _, pkg := range []string{"slow_memory_hog", "block_contest", "lock_contest"} {
Ω(fm.PathTo("profile", pkg, pkg+".test")).Should(BeAnExistingFile(), "preserve the precompiled binary in the package directory")
Ω(fm.PathTo("profile", "profiles", pkg+".test")).Should(BeAnExistingFile(), "copy the precompiled binary to the output-dir")
Ω(fm.PathTo("profile", "profiles", pkg+"_cpu.out")).Should(BeAnExistingFile(), "generate a correctly named cpu profile")
}
})
})
})
})

0 comments on commit 3d28a52

Please sign in to comment.