Skip to content

Bug 1943284: Fix nil function in buildContext return values #150

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions staging/operator-registry/pkg/lib/indexer/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,9 @@ func copyDatabaseTo(databaseFile, targetDir string) (string, error) {
}

func buildContext(generate bool, requestedDockerfile string) (buildDir, outDockerfile string, cleanup func(), err error) {
// set cleanup to a no-op until explicitly set
cleanup = func() {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clients should probably be checking return values for nil before using them.


if generate {
buildDir = "./"
if len(requestedDockerfile) == 0 {
Expand Down
58 changes: 58 additions & 0 deletions staging/operator-registry/pkg/lib/indexer/indexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,61 @@ func TestGeneratePackageYaml(t *testing.T) {

_ = os.RemoveAll("./package.yaml")
}

func TestBuildContext(t *testing.T) {
// TODO(): Test does not currently have a clean way
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

those functions could be improved by using the new fs.FS abstractions -- then it would be easier to test

// of testing the generated returned values such as
// outDockerfile and buildDir.

defaultBuildDirOnGenerate := "./"
fooDockerfile := "foo.Dockerfile"
defaultDockerfile := defaultDockerfileName

cases := []struct {
generate bool
requestedDockerfile string
expectedBuildDir *string // return values not checked if nil
expectedOutDockerfile *string // return values not checked if nil
}{
{
generate: true,
requestedDockerfile: "",
expectedOutDockerfile: &defaultDockerfile,
expectedBuildDir: &defaultBuildDirOnGenerate,
},
{
generate: false,
requestedDockerfile: "foo.Dockerfile",
expectedOutDockerfile: &fooDockerfile,
expectedBuildDir: nil,
},
{
generate: false,
requestedDockerfile: "",
expectedOutDockerfile: nil,
expectedBuildDir: nil,
},
}

for _, testCase := range cases {
actualBuildDir, actualOutDockerfile, actualCleanup, _ := buildContext(
testCase.generate, testCase.requestedDockerfile)

if actualCleanup == nil {
// prevent regression - cleanup should never be nil
t.Fatal("buildContext returned nil cleanup function")
}

if testCase.expectedOutDockerfile != nil && actualOutDockerfile != *testCase.expectedOutDockerfile {
t.Fatalf("comparing outDockerfile: expected %v actual %v",
*testCase.expectedOutDockerfile,
actualOutDockerfile)
}

if testCase.expectedBuildDir != nil && actualBuildDir != *testCase.expectedBuildDir {
t.Fatalf("comparing buildDir: expected %v actual %v",
*testCase.expectedBuildDir,
actualBuildDir)
}
}
}

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