Skip to content

Commit

Permalink
feat: adds tools package and refactors mocks
Browse files Browse the repository at this point in the history
  • Loading branch information
jmgilman committed Sep 7, 2024
1 parent a088004 commit 477535a
Show file tree
Hide file tree
Showing 43 changed files with 1,483 additions and 260 deletions.
2 changes: 1 addition & 1 deletion blueprint/Earthfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ deps:
ENV GOMODCACHE=/go/modcache
CACHE --persist --sharing shared /go

COPY ../cuetools+src/src /cuetools
COPY ../tools+src/src /tools

COPY go.mod go.sum .
RUN go mod download
Expand Down
6 changes: 3 additions & 3 deletions blueprint/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ module github.com/input-output-hk/catalyst-forge/blueprint
require (
cuelang.org/go v0.10.0
github.com/Masterminds/semver/v3 v3.2.1
github.com/input-output-hk/catalyst-forge/cuetools v0.0.0
github.com/spf13/afero v1.11.0
github.com/input-output-hk/catalyst-forge/tools v0.0.0
)

require (
github.com/cockroachdb/apd/v3 v3.2.1 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
golang.org/x/mod v0.20.0 // indirect
golang.org/x/net v0.28.0 // indirect
golang.org/x/text v0.17.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

replace github.com/input-output-hk/catalyst-forge/cuetools => ../cuetools
replace github.com/input-output-hk/catalyst-forge/tools => ../tools

go 1.22.3
6 changes: 6 additions & 0 deletions blueprint/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ github.com/Masterminds/semver/v3 v3.2.1 h1:RN9w6+7QoMeJVGyfmbcgs28Br8cvmnucEXnY0
github.com/Masterminds/semver/v3 v3.2.1/go.mod h1:qvl/7zhW3nngYb5+80sSMF+FG2BjYrf8m9wsX0PNOMQ=
github.com/cockroachdb/apd/v3 v3.2.1 h1:U+8j7t0axsIgvQUqthuNm82HIrYXodOV2iWLWtEaIwg=
github.com/cockroachdb/apd/v3 v3.2.1/go.mod h1:klXJcjp+FffLTHlhIG69tezTDvdP065naDsHzKhYSqc=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/emicklei/proto v1.13.2 h1:z/etSFO3uyXeuEsVPzfl56WNgzcvIr42aQazXaQmFZY=
github.com/emicklei/proto v1.13.2/go.mod h1:rn1FgRS/FANiZdD2djyH7TMA9jdRDcYQ9IEN9yvjX0A=
github.com/go-quicktest/qt v1.101.0 h1:O1K29Txy5P2OK0dGo59b7b0LR6wKfIhttaAhHUyn7eI=
Expand All @@ -28,12 +30,16 @@ github.com/opencontainers/image-spec v1.1.0 h1:8SG7/vwALn54lVB/0yZ/MMwhFrPYtpEHQ
github.com/opencontainers/image-spec v1.1.0/go.mod h1:W4s4sFTMaBeK1BQLXbG4AdM2szdn85PY75RI83NrTrM=
github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM=
github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/protocolbuffers/txtpbfmt v0.0.0-20230328191034-3462fbc510c0 h1:sadMIsgmHpEOGbUs6VtHBXRR1OHevnj7hLx9ZcdNGW4=
github.com/protocolbuffers/txtpbfmt v0.0.0-20230328191034-3462fbc510c0/go.mod h1:jgxiZysxFPM+iWKwQwPR+y+Jvo54ARd4EisXxKYpB5c=
github.com/rogpeppe/go-internal v1.12.1-0.20240709150035-ccf4b4329d21 h1:igWZJluD8KtEtAgRyF4x6lqcxDry1ULztksMJh2mnQE=
github.com/rogpeppe/go-internal v1.12.1-0.20240709150035-ccf4b4329d21/go.mod h1:RMRJLmBOqWacUkmJHRMiPKh1S1m3PA7Zh4W80/kWPpg=
github.com/spf13/afero v1.11.0 h1:WJQKhtpdm3v2IzqG8VMqrr6Rf3UYpEF239Jy9wNepM8=
github.com/spf13/afero v1.11.0/go.mod h1:GH9Y3pIexgf1MTIWtNGyogA5MwRIDXGUr+hbWNoBjkY=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/mod v0.20.0 h1:utOm6MM3R3dnawAiJgn0y+xvuYRsm1RKM/4giyfDgV0=
golang.org/x/mod v0.20.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/net v0.28.0 h1:a9JDOJc5GMUJ0+UDqmLT86WiEy7iWyIhz8gz8E4e5hE=
Expand Down
2 changes: 1 addition & 1 deletion blueprint/pkg/blueprint/blueprint.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/Masterminds/semver/v3"
"github.com/input-output-hk/catalyst-forge/blueprint/pkg/injector"
"github.com/input-output-hk/catalyst-forge/blueprint/pkg/version"
cuetools "github.com/input-output-hk/catalyst-forge/cuetools/pkg"
cuetools "github.com/input-output-hk/catalyst-forge/tools/pkg/cue"
)

// BlueprintFile represents a single blueprint file.
Expand Down
2 changes: 1 addition & 1 deletion blueprint/pkg/injector/injector.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package injector

//go:generate go run github.com/matryer/moq@latest --out ./injector_mock.go . EnvGetter
//go:generate go run github.com/matryer/moq@latest -skip-ensure --pkg mocks --out ./mocks/injector.go . EnvGetter

import (
"fmt"
Expand Down
5 changes: 3 additions & 2 deletions blueprint/pkg/injector/injector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import (

"cuelang.org/go/cue"
"cuelang.org/go/cue/cuecontext"
cuetools "github.com/input-output-hk/catalyst-forge/cuetools/pkg"
"github.com/input-output-hk/catalyst-forge/blueprint/pkg/injector/mocks"
cuetools "github.com/input-output-hk/catalyst-forge/tools/pkg/cue"
)

func TestInjectEnv(t *testing.T) {
Expand Down Expand Up @@ -96,7 +97,7 @@ func TestInjectEnv(t *testing.T) {

i := Injector{
logger: slog.New(slog.NewTextHandler(io.Discard, nil)),
getter: &EnvGetterMock{
getter: &mocks.EnvGetterMock{
GetFunc: func(key string) (string, bool) {
return tt.env[key], true
},
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

69 changes: 39 additions & 30 deletions blueprint/pkg/loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import (
"github.com/input-output-hk/catalyst-forge/blueprint/pkg/injector"
"github.com/input-output-hk/catalyst-forge/blueprint/pkg/version"
"github.com/input-output-hk/catalyst-forge/blueprint/schema"
cuetools "github.com/input-output-hk/catalyst-forge/cuetools/pkg"
cuetools "github.com/input-output-hk/catalyst-forge/tools/pkg/cue"
"github.com/input-output-hk/catalyst-forge/tools/pkg/walker"
)

const BlueprintFileName = "blueprint.cue"
Expand All @@ -29,7 +30,7 @@ type BlueprintLoader struct {
injector injector.Injector
logger *slog.Logger
rootPath string
walker ReverseWalker
walker walker.ReverseWalker
}

func (b *BlueprintLoader) Load() error {
Expand Down Expand Up @@ -137,27 +138,31 @@ func (b *BlueprintLoader) Raw() blueprint.RawBlueprint {
func (b *BlueprintLoader) findBlueprints(startPath, endPath string) (map[string][]byte, error) {
bps := make(map[string][]byte)

err := b.walker.Walk(startPath, endPath, func(path string, fileType FileType, openFile func() (FileSeeker, error)) error {
if fileType == FileTypeFile {
if filepath.Base(path) == BlueprintFileName {
reader, err := openFile()
if err != nil {
return err
err := b.walker.Walk(
startPath,
endPath,
func(path string, fileType walker.FileType, openFile func() (walker.FileSeeker, error)) error {
if fileType == walker.FileTypeFile {
if filepath.Base(path) == BlueprintFileName {
reader, err := openFile()
if err != nil {
return err
}

defer reader.Close()

data, err := io.ReadAll(reader)
if err != nil {
return err
}

bps[path] = data
}

defer reader.Close()

data, err := io.ReadAll(reader)
if err != nil {
return err
}

bps[path] = data
}
}

return nil
})
return nil
},
)

if err != nil {
return nil, err
Expand All @@ -171,16 +176,20 @@ func (b *BlueprintLoader) findBlueprints(startPath, endPath string) (map[string]
// the root is not found.
func (b *BlueprintLoader) findGitRoot(startPath string) (string, error) {
var gitRoot string
err := b.walker.Walk(startPath, "/", func(path string, fileType FileType, openFile func() (FileSeeker, error)) error {
if fileType == FileTypeDir {
if filepath.Base(path) == ".git" {
gitRoot = filepath.Dir(path)
return io.EOF
err := b.walker.Walk(
startPath,
"/",
func(path string, fileType walker.FileType, openFile func() (walker.FileSeeker, error)) error {
if fileType == walker.FileTypeDir {
if filepath.Base(path) == ".git" {
gitRoot = filepath.Dir(path)
return io.EOF
}
}
}

return nil
})
return nil
},
)

if err != nil {
return "", err
Expand All @@ -202,7 +211,7 @@ func NewDefaultBlueprintLoader(rootPath string,
logger = slog.New(slog.NewTextHandler(io.Discard, nil))
}

walker := NewDefaultFSReverseWalker(logger)
walker := walker.NewDefaultFSReverseWalker(logger)
return BlueprintLoader{
injector: injector.NewDefaultInjector(logger),
logger: logger,
Expand All @@ -214,7 +223,7 @@ func NewDefaultBlueprintLoader(rootPath string,
// NewBlueprintLoader creates a new blueprint loader
func NewBlueprintLoader(rootPath string,
logger *slog.Logger,
walker ReverseWalker,
walker walker.ReverseWalker,
injector injector.Injector,
) BlueprintLoader {
return BlueprintLoader{
Expand Down
27 changes: 15 additions & 12 deletions blueprint/pkg/loader/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import (

"cuelang.org/go/cue"
"github.com/input-output-hk/catalyst-forge/blueprint/pkg/injector"
imocks "github.com/input-output-hk/catalyst-forge/blueprint/pkg/injector/mocks"
"github.com/input-output-hk/catalyst-forge/tools/pkg/walker"
wmocks "github.com/input-output-hk/catalyst-forge/tools/pkg/walker/mocks"
)

type fieldTest struct {
Expand Down Expand Up @@ -181,11 +184,11 @@ func TestBlueprintLoaderLoad(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
walker := &ReverseWalkerMock{
WalkFunc: func(startPath string, endPath string, callback WalkerCallback) error {
walker := &wmocks.ReverseWalkerMock{
WalkFunc: func(startPath string, endPath string, callback walker.WalkerCallback) error {
// True when there is no git root, so we simulate only searching for blueprint files in the root path.
if startPath == endPath && len(tt.files) > 0 {
err := callback(filepath.Join(tt.root, "blueprint.cue"), FileTypeFile, func() (FileSeeker, error) {
err := callback(filepath.Join(tt.root, "blueprint.cue"), walker.FileTypeFile, func() (walker.FileSeeker, error) {
return NewMockFileSeeker(tt.files[filepath.Join(tt.root, "blueprint.cue")]), nil
})

Expand All @@ -201,11 +204,11 @@ func TestBlueprintLoaderLoad(t *testing.T) {
for path, content := range tt.files {
var err error
if content == "" {
err = callback(path, FileTypeDir, func() (FileSeeker, error) {
err = callback(path, walker.FileTypeDir, func() (walker.FileSeeker, error) {
return nil, nil
})
} else {
err = callback(path, FileTypeFile, func() (FileSeeker, error) {
err = callback(path, walker.FileTypeFile, func() (walker.FileSeeker, error) {
return NewMockFileSeeker(content), nil
})
}
Expand All @@ -224,7 +227,7 @@ func TestBlueprintLoaderLoad(t *testing.T) {
loader := BlueprintLoader{
injector: injector.NewInjector(
slog.New(slog.NewTextHandler(io.Discard, nil)),
&injector.EnvGetterMock{
&imocks.EnvGetterMock{
GetFunc: func(name string) (string, bool) {
return "", false
},
Expand Down Expand Up @@ -320,10 +323,10 @@ func TestBlueprintLoader_findBlueprints(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
walker := &ReverseWalkerMock{
WalkFunc: func(startPath string, endPath string, callback WalkerCallback) error {
walker := &wmocks.ReverseWalkerMock{
WalkFunc: func(startPath string, endPath string, callback walker.WalkerCallback) error {
for path, content := range tt.files {
err := callback(path, FileTypeFile, func() (FileSeeker, error) {
err := callback(path, walker.FileTypeFile, func() (walker.FileSeeker, error) {
return NewMockFileSeeker(content), nil
})

Expand Down Expand Up @@ -393,10 +396,10 @@ func TestBlueprintLoader_findGitRoot(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var lastPath string
walker := &ReverseWalkerMock{
WalkFunc: func(startPath string, endPath string, callback WalkerCallback) error {
walker := &wmocks.ReverseWalkerMock{
WalkFunc: func(startPath string, endPath string, callback walker.WalkerCallback) error {
for _, dir := range tt.dirs {
err := callback(dir, FileTypeDir, func() (FileSeeker, error) {
err := callback(dir, walker.FileTypeDir, func() (walker.FileSeeker, error) {
return nil, nil
})

Expand Down
2 changes: 1 addition & 1 deletion blueprint/pkg/version/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"cuelang.org/go/cue/cuecontext"
"github.com/Masterminds/semver/v3"
"github.com/input-output-hk/catalyst-forge/blueprint/internal/testutils"
cuetools "github.com/input-output-hk/catalyst-forge/cuetools/pkg"
cuetools "github.com/input-output-hk/catalyst-forge/tools/pkg/cue"
)

func TestGetVersion(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion blueprint/schema/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"cuelang.org/go/cue"
"github.com/Masterminds/semver/v3"
"github.com/input-output-hk/catalyst-forge/blueprint/pkg/version"
cuetools "github.com/input-output-hk/catalyst-forge/cuetools/pkg"
cuetools "github.com/input-output-hk/catalyst-forge/tools/pkg/cue"
)

// SchemaFile contains the schema for blueprint files.
Expand Down
2 changes: 1 addition & 1 deletion forge/cli/Earthfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ deps:
CACHE --persist --sharing shared /go

COPY ../../blueprint+src/src /blueprint
COPY ../../cuetools+src/src /cuetools
COPY ../../tools+src/src /tools

COPY go.mod go.sum .
RUN go mod download
Expand Down
4 changes: 2 additions & 2 deletions forge/cli/cmd/cmds/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"sort"

"github.com/input-output-hk/catalyst-forge/forge/cli/pkg/earthfile"
"github.com/input-output-hk/catalyst-forge/forge/cli/pkg/walker"
"github.com/input-output-hk/catalyst-forge/tools/pkg/walker"
)

type ScanCmd struct {
Expand All @@ -20,7 +20,7 @@ type ScanCmd struct {
}

func (c *ScanCmd) Run(logger *slog.Logger) error {
walker := walker.NewFilesystemWalker(logger)
walker := walker.NewDefaultFSWalker(logger)

var rootPath string
if c.Absolute {
Expand Down
4 changes: 2 additions & 2 deletions forge/cli/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ require (
github.com/cockroachdb/apd/v3 v3.2.1 // indirect
github.com/go-logfmt/logfmt v0.6.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/input-output-hk/catalyst-forge/cuetools v0.0.0 // indirect
github.com/input-output-hk/catalyst-forge/tools v0.0.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.18 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
Expand All @@ -55,4 +55,4 @@ require (

replace github.com/input-output-hk/catalyst-forge/blueprint => ../../blueprint

replace github.com/input-output-hk/catalyst-forge/cuetools => ../../cuetools
replace github.com/input-output-hk/catalyst-forge/tools => ../../tools
2 changes: 1 addition & 1 deletion forge/cli/pkg/earthfile/earthfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/earthly/earthly/ast"
"github.com/earthly/earthly/ast/spec"
"github.com/input-output-hk/catalyst-forge/forge/cli/pkg/walker"
"github.com/input-output-hk/catalyst-forge/tools/pkg/walker"
)

// Earthfile represents a parsed Earthfile.
Expand Down
2 changes: 1 addition & 1 deletion forge/cli/pkg/earthfile/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"path/filepath"
"strings"

w "github.com/input-output-hk/catalyst-forge/forge/cli/pkg/walker"
w "github.com/input-output-hk/catalyst-forge/tools/pkg/walker"
)

// ScanEarthfiles scans the given root path for Earthfiles and returns a map
Expand Down
Loading

0 comments on commit 477535a

Please sign in to comment.