Skip to content

Commit

Permalink
test: allow fetching CRD from a local replace statement
Browse files Browse the repository at this point in the history
Ginkgo test suites require fetching the CRDs of the custom resources
used in the tests. Those are fetched from the go.mod and any replace
statement is parsed and honoured.
Add the ability to process replace statements that point to a local
directory as well, to ease the developement process when a project
depends on a feature from another project that has not been merged yet.
  • Loading branch information
dciabrin committed Nov 9, 2023
1 parent a0ac89b commit 479a7ee
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions modules/test/crd.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"go/build"
"os"
"path/filepath"
"strings"

"golang.org/x/mod/modfile"
)
Expand Down Expand Up @@ -53,10 +54,16 @@ func getDependencyVersion(moduleName string, goModPath string) (string, string,
}
}

// replacement points to a URI + version
if version != "" {
return name, version, nil
}

// replacement points to a local path
if version == "" && strings.HasPrefix(name, ".") {
return name, version, nil
}

return name, "", fmt.Errorf("cannot find %s in %s file", moduleName, goModPath)
}

Expand All @@ -68,6 +75,15 @@ func GetCRDDirFromModule(moduleName string, goModPath string, relativeCRDPath st
if err != nil {
return "", err
}

// for a local replacement, assume the CRDs are available in the
// standard Operator SDK's layout
if version == "" && strings.HasPrefix(moduleName, ".") {
goModDir := filepath.Dir(goModPath)
path := filepath.Join(goModDir, moduleName, "..", "config", "crd", relativeCRDPath)
return path, nil
}

versionedModule := fmt.Sprintf("%s@%s", moduleName, version)
path := filepath.Join(build.Default.GOPATH, "pkg", "mod", versionedModule, relativeCRDPath)
return path, nil
Expand Down

0 comments on commit 479a7ee

Please sign in to comment.