Skip to content

Commit f4757c8

Browse files
[go-migration] Refactor common context, generate mocks, fix hollow unit tests for supply/finalize (#1177)
[go-migration] Refactor context to interfaces and fix hollow unit tests for supply/finalize Introduces Go interfaces (Stager, Manifest, Installer, Command) in the common context to replace concrete libbuildpack types, enabling mock generation via gomock. Previously, the supply and finalize unit tests always passed vacuously — this PR rewrites them to actually exercise `supply.Run()` and `finalize.Run()` for Tomcat, Spring Boot, and Groovy containers using generated mocks, without performing real dependency downloads. Adds a new finalize test case for the Groovy container. Also consolidates the `Installer` interface from `cf_metrics_exporter` into the shared common context and fixes related test setup. Fixes #1172
1 parent 014701d commit f4757c8

17 files changed

Lines changed: 2054 additions & 98 deletions

File tree

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ require (
66
github.com/Dynatrace/libbuildpack-dynatrace v1.8.0
77
github.com/cloudfoundry/libbuildpack v0.0.0-20251203175254-7be530ec9fef
88
github.com/cloudfoundry/switchblade v0.9.4
9+
github.com/golang/mock v1.6.0
910
github.com/onsi/ginkgo/v2 v2.27.2
1011
github.com/onsi/gomega v1.38.2
1112
github.com/sclevine/spec v1.4.0
13+
go.yaml.in/yaml/v3 v3.0.4
1214
gopkg.in/yaml.v2 v2.4.0
1315
)
1416

@@ -42,7 +44,6 @@ require (
4244
go.opentelemetry.io/otel v1.32.0 // indirect
4345
go.opentelemetry.io/otel/metric v1.32.0 // indirect
4446
go.opentelemetry.io/otel/trace v1.32.0 // indirect
45-
go.yaml.in/yaml/v3 v3.0.4 // indirect
4647
golang.org/x/mod v0.30.0 // indirect
4748
golang.org/x/net v0.47.0 // indirect
4849
golang.org/x/sync v0.18.0 // indirect

src/internal/mocks/mocks.go

Lines changed: 308 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/java/common/context.go

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,49 @@ import (
44
"encoding/json"
55
"fmt"
66
"github.com/cloudfoundry/libbuildpack"
7+
"io"
78
"os"
89
"path/filepath"
910
"strconv"
1011
"strings"
1112
)
1213

14+
//go:generate mockgen -source=context.go --destination=../../internal/mocks/mocks.go --package=mocks
15+
16+
type Command interface {
17+
Execute(string, io.Writer, io.Writer, string, ...string) error
18+
}
19+
20+
type Stager interface {
21+
LinkDirectoryInDepDir(string, string) error
22+
BuildDir() string
23+
DepDir() string
24+
DepsIdx() string
25+
CacheDir() string
26+
WriteConfigYml(interface{}) error
27+
WriteEnvFile(string, string) error
28+
WriteProfileD(string, string) error
29+
}
30+
31+
type Manifest interface {
32+
AllDependencyVersions(string) []string
33+
DefaultVersion(string) (libbuildpack.Dependency, error)
34+
GetEntry(libbuildpack.Dependency) (*libbuildpack.ManifestEntry, error)
35+
}
36+
37+
type Installer interface {
38+
InstallDependency(libbuildpack.Dependency, string) error
39+
InstallDependencyWithStrip(libbuildpack.Dependency, string, int) error
40+
}
41+
1342
// Context holds shared dependencies for buildpack components
1443
// Used by containers, frameworks, and JREs to access buildpack infrastructure
1544
type Context struct {
16-
Stager *libbuildpack.Stager
17-
Manifest *libbuildpack.Manifest
18-
Installer *libbuildpack.Installer
45+
Stager Stager
46+
Manifest Manifest
47+
Installer Installer
1948
Log *libbuildpack.Logger
20-
Command *libbuildpack.Command
49+
Command Command
2150
}
2251

2352
// DetermineJavaVersion determines the major Java version from a Java installation

0 commit comments

Comments
 (0)