Skip to content

Commit

Permalink
Unit test BP_GO_WORK_USE
Browse files Browse the repository at this point in the history
Signed-off-by: Max Brauer <mbrauer@vmware.com>
  • Loading branch information
mamachanko committed Jan 28, 2024
1 parent 93ba5fb commit 57f4f6b
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 0 deletions.
21 changes: 21 additions & 0 deletions build_configuration_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,27 @@ func testBuildConfigurationParser(t *testing.T, context spec.G, it spec.S) {
})
})

context("when BP_GO_WORK_USE is set", func() {
it.Before(func() {
os.Setenv("BP_GO_WORK_USE", "./some/module1:./some/module2")
})

it.After(func() {
os.Unsetenv("BP_GO_WORK_USE")
})

it("uses the values in the env var", func() {
configuration, err := parser.Parse("1.2.3", workingDir)
Expect(err).NotTo(HaveOccurred())
Expect(configuration).To(Equal(gobuild.BuildConfiguration{
Targets: []string{"."},
WorkspaceUseModules: []string{"./some/module1", "./some/module2"},
}))

Expect(targetManager.GenerateDefaultsCall.Receives.WorkingDir).To(Equal(workingDir))
})
})

context("failure cases", func() {
context("when the working directory contains a buildpack.yml", func() {
it.Before(func() {
Expand Down
126 changes: 126 additions & 0 deletions go_build_process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,66 @@ func testGoBuildProcess(t *testing.T, context spec.G, it spec.S) {
})
})

context("when workspaces should be used", func() {
it.Before(func() {
Expect(os.WriteFile(filepath.Join(workspacePath, "go.mod"), nil, 0644)).To(Succeed())
Expect(os.Mkdir(filepath.Join(workspacePath, "vendor"), os.ModePerm)).To(Succeed())
})

it("inits and uses the workspaces before executing the go build process", func() {
binaries, err := buildProcess.Execute(gobuild.GoBuildConfiguration{
Workspace: workspacePath,
Output: filepath.Join(layerPath, "bin"),
GoCache: goCache,
Targets: []string{"."},
WorkspaceUseModules: []string{"./some/module1", "./some/module2"},
})
Expect(err).NotTo(HaveOccurred())
Expect(binaries).To(Equal([]string{
filepath.Join(layerPath, "bin", "some-dir"),
}))

Expect(filepath.Join(layerPath, "bin")).To(BeADirectory())

Expect(executions[0].Args).To(Equal([]string{
"work",
"init",
}))

Expect(executions[1].Args).To(Equal([]string{
"work",
"use",
"./some/module1",
"./some/module2",
}))

Expect(executions[2].Args).To(Equal([]string{
"build",
"-o", filepath.Join(layerPath, "bin"),
"-buildmode", "pie",
"-trimpath",
".",
}))

Expect(executions[3].Args).To(Equal([]string{
"list",
"--json",
".",
}))

Expect(executable.ExecuteCall.Receives.Execution.Dir).To(Equal(workspacePath))
Expect(executable.ExecuteCall.Receives.Execution.Env).To(ContainElement(fmt.Sprintf("GOCACHE=%s", goCache)))

Expect(logs).To(ContainLines(
" Executing build process",
" Running 'go work init'",
" Running 'go work use ./some/module1 ./some/module2'",
fmt.Sprintf(` Running 'go build -o %s -buildmode pie -trimpath .'`, filepath.Join(layerPath, "bin")),
" Completed in 0s",
))
})
})

context("when the GOPATH is empty", func() {
it.Before(func() {
Expect(os.WriteFile(filepath.Join(workspacePath, "go.mod"), nil, 0644)).To(Succeed())
Expand Down Expand Up @@ -244,6 +304,72 @@ func testGoBuildProcess(t *testing.T, context spec.G, it spec.S) {
})
})

context("when workspaces should be used", func() {
context("when the executable fails go work init", func() {
it.Before(func() {
executable.ExecuteCall.Stub = func(execution pexec.Execution) error {
if execution.Args[0] == "work" && execution.Args[1] == "init" {
fmt.Fprintln(execution.Stdout, "work init error stdout")
fmt.Fprintln(execution.Stderr, "work init error stderr")
return errors.New("command failed")
}

return nil
}
})

it("returns an error", func() {
_, err := buildProcess.Execute(gobuild.GoBuildConfiguration{
Workspace: workspacePath,
Output: filepath.Join(layerPath, "bin"),
GoPath: goPath,
GoCache: goCache,
Targets: []string{"."},
WorkspaceUseModules: []string{"./some/module1", "./some/module2"},
})
Expect(err).To(MatchError("failed to execute '[work init]': command failed"))

Expect(logs).To(ContainLines(
" work init error stdout",
" work init error stderr",
" Failed after 1s",
))
})
})

context("when the executable fails go work use", func() {
it.Before(func() {
executable.ExecuteCall.Stub = func(execution pexec.Execution) error {
if execution.Args[0] == "work" && execution.Args[1] == "use" {
fmt.Fprintln(execution.Stdout, "work use error stdout")
fmt.Fprintln(execution.Stderr, "work use error stderr")
return errors.New("command failed")
}

return nil
}
})

it("returns an error", func() {
_, err := buildProcess.Execute(gobuild.GoBuildConfiguration{
Workspace: workspacePath,
Output: filepath.Join(layerPath, "bin"),
GoPath: goPath,
GoCache: goCache,
Targets: []string{"."},
WorkspaceUseModules: []string{"./some/module1", "./some/module2"},
})
Expect(err).To(MatchError("failed to execute '[work use ./some/module1 ./some/module2]': command failed"))

Expect(logs).To(ContainLines(
" work use error stdout",
" work use error stderr",
" Failed after 0s",
))
})
})
})

context("when the executable fails go build", func() {
it.Before(func() {
executable.ExecuteCall.Stub = func(execution pexec.Execution) error {
Expand Down

0 comments on commit 57f4f6b

Please sign in to comment.