From 055bc6eec3cbabe474cb105466e1be3d098e03bb Mon Sep 17 00:00:00 2001 From: Nikita Podshivalov Date: Thu, 3 Nov 2022 09:11:09 +0300 Subject: [PATCH] moved boilerplate-template to tests/data --- internal/commands/ci_test.go | 7 ++-- internal/commands/testing.go | 37 ++++++++++++++++++- .../data}/boilerplate-template/{{env}}.yml | 0 .../{{namespace}}/{{env}}.js | 0 tests/data/embed.go | 8 ++++ 5 files changed, 48 insertions(+), 4 deletions(-) rename {examples => tests/data}/boilerplate-template/{{env}}.yml (100%) rename {examples => tests/data}/boilerplate-template/{{namespace}}/{{env}}.js (100%) create mode 100644 tests/data/embed.go diff --git a/internal/commands/ci_test.go b/internal/commands/ci_test.go index 90610918e..9c41b9974 100644 --- a/internal/commands/ci_test.go +++ b/internal/commands/ci_test.go @@ -2,17 +2,18 @@ package commands import ( _ "embed" - "github.com/hazelops/ize/internal/config" - "github.com/hazelops/ize/internal/generate" "os" "path/filepath" "testing" "text/template" + + "github.com/hazelops/ize/internal/config" + "github.com/hazelops/ize/tests/data" ) func TestTemplate_Execute(t *testing.T) { tempDir, _ := os.MkdirTemp("", "test") - _, err := generate.GenerateFiles("boilerplate-template", filepath.Join(tempDir, "template")) + err := copyEmbedData(data.TestData, "boilerplate-template", filepath.Join(tempDir, "template")) if err != nil { t.Error(err) return diff --git a/internal/commands/testing.go b/internal/commands/testing.go index aeb9157cb..a852fdf95 100644 --- a/internal/commands/testing.go +++ b/internal/commands/testing.go @@ -4,13 +4,16 @@ import ( "crypto/rand" "crypto/rsa" "crypto/x509" + "embed" "encoding/pem" - "golang.org/x/crypto/ssh" "io/ioutil" "os" + "path" "path/filepath" "strings" "testing" + + "golang.org/x/crypto/ssh" ) func setConfigFile(path, data string, t *testing.T) { @@ -66,3 +69,35 @@ func resetEnv(environ []string) { os.Setenv(kv[0], kv[1]) } } + +func copyEmbedData(fsys embed.FS, sourceDir string, targetDir string) error { + subdirs, err := fsys.ReadDir(sourceDir) + if err != nil { + return err + } + for _, d := range subdirs { + sourcePath := path.Join(sourceDir, d.Name()) + if d.IsDir() { + err = copyEmbedData(fsys, path.Join(sourceDir, d.Name()), path.Join(targetDir, d.Name())) + if err != nil { + return err + } + } else { + localPath := filepath.Join(targetDir, d.Name()) + + content, err := fsys.ReadFile(sourcePath) + if err != nil { + return err + } + err = os.MkdirAll(filepath.Dir(localPath), 0755) + if err != nil { + return err + } + err = os.WriteFile(localPath, content, 0755) + if err != nil { + return err + } + } + } + return nil +} diff --git a/examples/boilerplate-template/{{env}}.yml b/tests/data/boilerplate-template/{{env}}.yml similarity index 100% rename from examples/boilerplate-template/{{env}}.yml rename to tests/data/boilerplate-template/{{env}}.yml diff --git a/examples/boilerplate-template/{{namespace}}/{{env}}.js b/tests/data/boilerplate-template/{{namespace}}/{{env}}.js similarity index 100% rename from examples/boilerplate-template/{{namespace}}/{{env}}.js rename to tests/data/boilerplate-template/{{namespace}}/{{env}}.js diff --git a/tests/data/embed.go b/tests/data/embed.go new file mode 100644 index 000000000..d5773a813 --- /dev/null +++ b/tests/data/embed.go @@ -0,0 +1,8 @@ +package data + +import "embed" + +var ( + //go:embed */* + TestData embed.FS +)