Skip to content
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
2 changes: 1 addition & 1 deletion internal/generate/crd/crd.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func (g crdGenerator) generateGo() (map[string][]byte, error) {
// Generate files in the generator's cache so we can modify the file name
// and annotations.
defName := "output:crd:cache"
cacheOutputDir := string(filepath.Separator) + filepath.Clean(g.OutputDir)
cacheOutputDir := filepath.Clean(g.OutputDir)
rawOpts := []string{
"crd",
fmt.Sprintf("paths=%s/...", fileutil.DotPath(g.Inputs[APIsDirKey])),
Expand Down
24 changes: 18 additions & 6 deletions internal/generate/crd/crd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
package crd

import (
"io/ioutil"
"encoding/base32"
"math/rand"
"os"
"path"
"path/filepath"
"strconv"
"testing"
"time"

gen "github.com/operator-framework/operator-sdk/internal/generate/gen"
"github.com/operator-framework/operator-sdk/internal/scaffold"
Expand All @@ -41,13 +42,24 @@ var (
testAPIVersion = path.Join(testGroup, testVersion)
)

func init() {
rand.Seed(time.Now().UnixNano())
}

// randomString returns a base32-encoded random string, reasonably sized for
// directory creation.
func randomString() string {
rb := []byte(strconv.Itoa(rand.Int() % (2 << 20)))
return base32.StdEncoding.WithPadding(base32.NoPadding).EncodeToString(rb)
}

func TestGenerate(t *testing.T) {
tmp, err := ioutil.TempDir("", "")
if err != nil {
tmp := filepath.Join(os.TempDir(), randomString())
if err := os.MkdirAll(tmp, 0755); err != nil {
t.Fatal(err)
}
defer func() {
if err = os.RemoveAll(tmp); err != nil {
if err := os.RemoveAll(tmp); err != nil {
t.Fatal(err)
}
}()
Expand All @@ -66,7 +78,7 @@ func TestGenerate(t *testing.T) {
Inputs: map[string]string{
APIsDirKey: filepath.Join(testGoDataDir, scaffold.ApisDir),
},
OutputDir: filepath.Join(tmp, strconv.Itoa(rand.Int())),
OutputDir: filepath.Join(tmp, randomString()),
}),
},
{
Expand All @@ -75,7 +87,7 @@ func TestGenerate(t *testing.T) {
Inputs: map[string]string{
APIsDirKey: filepath.Join(testGoDataDir, scaffold.ApisDir),
},
OutputDir: filepath.Join(tmp, strconv.Itoa(rand.Int())),
OutputDir: filepath.Join(tmp, randomString()),
}, *r),
},
}
Expand Down