Skip to content

Commit

Permalink
Rename temp directory for fetching templates
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Ellis (OpenFaaS Ltd) <[email protected]>
  • Loading branch information
alexellis committed Sep 22, 2023
1 parent 93037de commit b102982
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
4 changes: 2 additions & 2 deletions commands/fetch_templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func fetchTemplates(templateURL string, refName string, overwrite bool) error {
return fmt.Errorf("pass valid templateURL")
}

dir, err := os.MkdirTemp("", "openFaasTemplates")
dir, err := os.MkdirTemp("", "openfaas-templates-*")
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -127,7 +127,7 @@ func moveTemplates(repoPath string, overwrite bool) ([]string, []string, error)
func pullTemplate(repository string) error {
if _, err := os.Stat(repository); err != nil {
if !versioncontrol.IsGitRemote(repository) && !versioncontrol.IsPinnedGitRemote(repository) {
return fmt.Errorf("The repository URL must be a valid git repo uri")
return fmt.Errorf("repository URL must be a valid git repo uri")
}
}

Expand Down
21 changes: 10 additions & 11 deletions commands/fetch_templates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,18 @@ func Test_PullTemplates(t *testing.T) {
defer os.RemoveAll(localTemplateRepository)
defer tearDownFetchTemplates(t)

t.Run("simplePull", func(t *testing.T) {
t.Run("pullTemplates", func(t *testing.T) {
defer tearDownFetchTemplates(t)
if err := pullTemplates(localTemplateRepository); err != nil {
t.Error(err)
t.Fatal(err)
}
})

t.Run("fetchTemplates", func(t *testing.T) {
t.Run("fetchTemplates with master ref", func(t *testing.T) {
defer tearDownFetchTemplates(t)

err := fetchTemplates(localTemplateRepository, "master", false)
if err != nil {
t.Error(err)
if err := fetchTemplates(localTemplateRepository, "master", false); err != nil {
t.Fatal(err)
}

})
Expand All @@ -47,14 +46,15 @@ func Test_PullTemplates(t *testing.T) {
// setupLocalTemplateRepo will create a local copy of the core OpenFaaS templates, this
// can be refered to as a local git repository.
func setupLocalTemplateRepo(t *testing.T) string {
dir, err := os.MkdirTemp("", "openFaasTestTemplates")
dir, err := os.MkdirTemp("", "openfaas-templates-test-*")
if err != nil {
t.Error(err)
}

// Copy the submodule to temp directory to avoid altering it during tests
testRepoGit := filepath.Join("testdata", "templates")
builder.CopyFiles(testRepoGit, dir)

// Remove submodule .git file
os.Remove(filepath.Join(dir, ".git"))
if err := versioncontrol.GitInitRepo.Invoke(dir, map[string]string{"dir": "."}); err != nil {
Expand All @@ -68,11 +68,10 @@ func setupLocalTemplateRepo(t *testing.T) string {
func tearDownFetchTemplates(t *testing.T) {

// Remove existing templates folder, if it exist
if _, err := os.Stat("template/"); err == nil {
t.Log("Found a template/ directory, removing it...")
if _, err := os.Stat("./template/"); err == nil {
t.Log("Found a ./template/ directory, removing it.")

err := os.RemoveAll("template/")
if err != nil {
if err := os.RemoveAll("./template/"); err != nil {
t.Log(err)
}
} else {
Expand Down

0 comments on commit b102982

Please sign in to comment.