Skip to content

Commit ff41e27

Browse files
nunnatsaonsi
authored andcommitted
[build] Allow custom name for binaries.
Added the go build flag `-o` for custom path and name of the binary output of `ginkgo build`.
1 parent 28fb5d6 commit ff41e27

File tree

4 files changed

+67
-5
lines changed

4 files changed

+67
-5
lines changed

ginkgo/build/build_command.go

+14-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package build
22

33
import (
44
"fmt"
5+
"os"
6+
"path"
57

68
"github.com/onsi/ginkgo/v2/ginkgo/command"
79
"github.com/onsi/ginkgo/v2/ginkgo/internal"
@@ -53,7 +55,18 @@ func buildSpecs(args []string, cliConfig types.CLIConfig, goFlagsConfig types.Go
5355
if suite.State.Is(internal.TestSuiteStateFailedToCompile) {
5456
fmt.Println(suite.CompilationError.Error())
5557
} else {
56-
fmt.Printf("Compiled %s.test\n", suite.PackageName)
58+
if len(goFlagsConfig.O) == 0 {
59+
goFlagsConfig.O = path.Join(suite.Path, suite.PackageName+".test")
60+
} else {
61+
stat, err := os.Stat(goFlagsConfig.O)
62+
if err != nil {
63+
panic(err)
64+
}
65+
if stat.IsDir() {
66+
goFlagsConfig.O += "/" + suite.PackageName + ".test"
67+
}
68+
}
69+
fmt.Printf("Compiled %s\n", goFlagsConfig.O)
5770
}
5871
}
5972

ginkgo/internal/compile.go

+13-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,18 @@ func CompileSuite(suite TestSuite, goFlagsConfig types.GoFlagsConfig) TestSuite
2525
return suite
2626
}
2727

28+
if len(goFlagsConfig.O) > 0 {
29+
userDefinedPath, err := filepath.Abs(goFlagsConfig.O)
30+
if err != nil {
31+
suite.State = TestSuiteStateFailedToCompile
32+
suite.CompilationError = fmt.Errorf("Failed to compute compilation target path %s:\n%s", goFlagsConfig.O, err.Error())
33+
return suite
34+
}
35+
path = userDefinedPath
36+
}
37+
38+
goFlagsConfig.O = path
39+
2840
ginkgoInvocationPath, _ := os.Getwd()
2941
ginkgoInvocationPath, _ = filepath.Abs(ginkgoInvocationPath)
3042
packagePath := suite.AbsPath()
@@ -34,7 +46,7 @@ func CompileSuite(suite TestSuite, goFlagsConfig types.GoFlagsConfig) TestSuite
3446
suite.CompilationError = fmt.Errorf("Failed to get relative path from package to the current working directory:\n%s", err.Error())
3547
return suite
3648
}
37-
args, err := types.GenerateGoTestCompileArgs(goFlagsConfig, path, "./", pathToInvocationPath)
49+
args, err := types.GenerateGoTestCompileArgs(goFlagsConfig, "./", pathToInvocationPath)
3850
if err != nil {
3951
suite.State = TestSuiteStateFailedToCompile
4052
suite.CompilationError = fmt.Errorf("Failed to generate go test compile flags:\n%s", err.Error())

integration/precompiled_test.go

+35-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package integration_test
22

33
import (
4+
"os"
45
"os/exec"
6+
"path"
57

6-
. "github.com/onsi/ginkgo/v2"
78
. "github.com/onsi/gomega"
89
"github.com/onsi/gomega/gbytes"
910
"github.com/onsi/gomega/gexec"
11+
12+
. "github.com/onsi/ginkgo/v2"
1013
)
1114

1215
var _ = Describe("ginkgo build", func() {
@@ -44,3 +47,34 @@ var _ = Describe("ginkgo build", func() {
4447
Ω(session).Should(gbytes.Say("Running in parallel across 2 processes"))
4548
})
4649
})
50+
51+
var _ = Describe("ginkgo build with custom output", Label("build"), func() {
52+
const customPath = "mycustomdir"
53+
var fullPath string
54+
55+
BeforeEach(func() {
56+
fm.MountFixture("passing_ginkgo_tests")
57+
fullPath = fm.PathTo("passing_ginkgo_tests", customPath)
58+
Ω(os.Mkdir(fullPath, 0777)).To(Succeed())
59+
60+
DeferCleanup(func() {
61+
Ω(os.RemoveAll(fullPath)).Should(Succeed())
62+
})
63+
})
64+
65+
It("should build with custom path", func() {
66+
session := startGinkgo(fm.PathTo("passing_ginkgo_tests"), "build", "-o", customPath+"/mytestapp")
67+
Eventually(session).Should(gexec.Exit(0))
68+
output := string(session.Out.Contents())
69+
Ω(output).Should(And(ContainSubstring("Compiled"), ContainSubstring(customPath+"/mytestapp")))
70+
Ω(path.Join(fullPath, "/mytestapp")).Should(BeAnExistingFile())
71+
})
72+
73+
It("should build with custom directory", func() {
74+
session := startGinkgo(fm.PathTo("passing_ginkgo_tests"), "build", "-o", customPath)
75+
Eventually(session).Should(gexec.Exit(0))
76+
output := string(session.Out.Contents())
77+
Ω(output).Should(And(ContainSubstring("Compiled"), ContainSubstring(customPath+"/passing_ginkgo_tests.test")))
78+
Ω(path.Join(fullPath, "/passing_ginkgo_tests.test")).Should(BeAnExistingFile())
79+
})
80+
})

types/config.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ type GoFlagsConfig struct {
219219
ToolExec string
220220
Work bool
221221
X bool
222+
O string
222223
}
223224

224225
func NewDefaultGoFlagsConfig() GoFlagsConfig {
@@ -561,6 +562,8 @@ var GoBuildFlags = GinkgoFlags{
561562
Usage: "print the name of the temporary work directory and do not delete it when exiting."},
562563
{KeyPath: "Go.X", Name: "x", SectionKey: "go-build",
563564
Usage: "print the commands."},
565+
{KeyPath: "Go.O", Name: "o", SectionKey: "go-build",
566+
Usage: "output binary path (including name)."},
564567
}
565568

566569
// GoRunFlags provides flags for the Ginkgo CLI run, and watch commands that capture go's run-time flags. These are passed to the compiled test binary by the ginkgo CLI
@@ -614,7 +617,7 @@ func VetAndInitializeCLIAndGoConfig(cliConfig CLIConfig, goFlagsConfig GoFlagsCo
614617
}
615618

616619
// GenerateGoTestCompileArgs is used by the Ginkgo CLI to generate command line arguments to pass to the go test -c command when compiling the test
617-
func GenerateGoTestCompileArgs(goFlagsConfig GoFlagsConfig, destination string, packageToBuild string, pathToInvocationPath string) ([]string, error) {
620+
func GenerateGoTestCompileArgs(goFlagsConfig GoFlagsConfig, packageToBuild string, pathToInvocationPath string) ([]string, error) {
618621
// if the user has set the CoverProfile run-time flag make sure to set the build-time cover flag to make sure
619622
// the built test binary can generate a coverprofile
620623
if goFlagsConfig.CoverProfile != "" {
@@ -637,7 +640,7 @@ func GenerateGoTestCompileArgs(goFlagsConfig GoFlagsConfig, destination string,
637640
goFlagsConfig.CoverPkg = strings.Join(adjustedCoverPkgs, ",")
638641
}
639642

640-
args := []string{"test", "-c", "-o", destination, packageToBuild}
643+
args := []string{"test", "-c", packageToBuild}
641644
goArgs, err := GenerateFlagArgs(
642645
GoBuildFlags,
643646
map[string]interface{}{

0 commit comments

Comments
 (0)