-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: replace
interface
with function type
Signed-off-by: Bird <aflybird0@gmail.com>
- Loading branch information
Showing
7 changed files
with
126 additions
and
159 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package file | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("renderGitRepoDir func", func() { | ||
var ( | ||
vars map[string]interface{} | ||
srcPath, contentDir, rawContent, tplContent, renderdContent string | ||
) | ||
|
||
createFile := func(filePath, content string) { | ||
f, err := os.Create(filePath) | ||
Expect(err).Error().ShouldNot(HaveOccurred()) | ||
defer f.Close() | ||
err = os.WriteFile(filePath, []byte(content), 0755) | ||
Expect(err).Error().ShouldNot(HaveOccurred()) | ||
} | ||
createDir := func(dirPath string) { | ||
err := os.Mkdir(dirPath, 0755) | ||
Expect(err).Error().ShouldNot(HaveOccurred()) | ||
} | ||
BeforeEach(func() { | ||
rawContent = "This is a file without template variable" | ||
tplContent = ` | ||
metadata: | ||
name: "[[ .App.Name ]]" | ||
namespace: "[[ .App.NameSpace ]]"` | ||
renderdContent = ` | ||
metadata: | ||
name: "test" | ||
namespace: "test_namespace"` | ||
|
||
}) | ||
|
||
When("srcPath is not exist", func() { | ||
BeforeEach(func() { | ||
srcPath = "not_exist_path" | ||
}) | ||
It("should return err", func() { | ||
_, err := renderGitRepoDir("test", srcPath, vars) | ||
Expect(err).Error().Should(HaveOccurred()) | ||
}) | ||
}) | ||
When("all config is right", func() { | ||
BeforeEach(func() { | ||
contentDir = "content" | ||
vars = map[string]interface{}{ | ||
"App": map[string]interface{}{ | ||
"Name": "test", | ||
"NameSpace": "test_namespace", | ||
}, | ||
} | ||
srcPath = GinkgoT().TempDir() | ||
gitPath := filepath.Join(srcPath, ".git") | ||
createDir(gitPath) | ||
createFile(filepath.Join(gitPath, "gitFile"), tplContent) | ||
createFile(filepath.Join(srcPath, "README.md"), "") | ||
contentDirPath := filepath.Join(srcPath, contentDir) | ||
createDir(contentDirPath) | ||
createFile(filepath.Join(contentDirPath, "test.yaml.tpl"), tplContent) | ||
createFile(filepath.Join(contentDirPath, "raw.txt"), rawContent) | ||
}) | ||
It("should render all dir", func() { | ||
dstPath, err := renderGitRepoDir("test", srcPath, vars) | ||
Expect(err).Error().ShouldNot(HaveOccurred()) | ||
files, err := os.ReadDir(dstPath) | ||
Expect(err).Error().ShouldNot(HaveOccurred()) | ||
// test README.md dir is not copied | ||
Expect(len(files)).Should(Equal(2)) | ||
// test git dir files should not copied | ||
gitDirFiles, err := os.ReadDir(filepath.Join(dstPath, ".git")) | ||
Expect(err).Error().ShouldNot(HaveOccurred()) | ||
Expect(len(gitDirFiles)).Should(Equal(0)) | ||
// test content dir files is copied | ||
contentDirLoc := filepath.Join(dstPath, contentDir) | ||
contentFiles, err := os.ReadDir(contentDirLoc) | ||
Expect(err).Error().ShouldNot(HaveOccurred()) | ||
Expect(len(contentFiles)).Should(Equal(2)) | ||
// test file content | ||
tplFileContent, err := os.ReadFile(filepath.Join(contentDirLoc, "test.yaml")) | ||
Expect(err).Error().ShouldNot(HaveOccurred()) | ||
Expect(string(tplFileContent)).Should(Equal(renderdContent)) | ||
rawFileContent, err := os.ReadFile(filepath.Join(contentDirLoc, "raw.txt")) | ||
Expect(err).Error().ShouldNot(HaveOccurred()) | ||
Expect(string(rawFileContent)).Should(Equal(rawContent)) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters