Skip to content

Commit

Permalink
moved boilerplate-template to tests/data
Browse files Browse the repository at this point in the history
  • Loading branch information
psihachina committed Nov 3, 2022
1 parent ba3659f commit 055bc6e
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 4 deletions.
7 changes: 4 additions & 3 deletions internal/commands/ci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 36 additions & 1 deletion internal/commands/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}
File renamed without changes.
8 changes: 8 additions & 0 deletions tests/data/embed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package data

import "embed"

var (
//go:embed */*
TestData embed.FS
)

0 comments on commit 055bc6e

Please sign in to comment.