From d2f039f258b3b1f16813027604c3afb4bafafff7 Mon Sep 17 00:00:00 2001 From: Martin Chodur Date: Wed, 18 Aug 2021 15:17:25 +0200 Subject: [PATCH] feat: allow relative file paths (#25) * feat: allow relative file paths Signed-off-by: Martin Chodur * feat: add tests for template paths Signed-off-by: Martin Chodur * fix: optimize test file handling Signed-off-by: Martin Chodur * Update template_test.go Co-authored-by: Rudo Thomas Co-authored-by: Rudo Thomas --- debian/changelog | 3 ++- template.go | 8 ------- template_test.go | 60 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 9 deletions(-) diff --git a/debian/changelog b/debian/changelog index d7ece40..21cd77b 100644 --- a/debian/changelog +++ b/debian/changelog @@ -2,7 +2,8 @@ goenvtemplator (2.1.0) UNRELEASED; urgency=medium * Upgrade to go 1.16 * Migrated from `glide` to `go mod` - * Upgrade sprig library to 2.22.0 + * Upgrade sprig library to 2.22.0 + * Allow file paths to be relative -- Martin Chodur Wed, 11 Aug 2021 13:56:32 +0200 diff --git a/template.go b/template.go index 3fa2acb..7a73b3f 100644 --- a/template.go +++ b/template.go @@ -68,14 +68,6 @@ func generateTemplate(source, name string, delimLeft string, delimRight string) } func generateFile(templatePath, destinationPath string, debugTemplates bool, delimLeft string, delimRight string) error { - if !filepath.IsAbs(templatePath) { - return fmt.Errorf("Template path '%s' is not absolute!", templatePath) - } - - if !filepath.IsAbs(destinationPath) { - return fmt.Errorf("Destination path '%s' is not absolute!", destinationPath) - } - var slice []byte var err error if slice, err = ioutil.ReadFile(templatePath); err != nil { diff --git a/template_test.go b/template_test.go index be2e537..6f7e4d0 100644 --- a/template_test.go +++ b/template_test.go @@ -2,13 +2,73 @@ package main import ( "github.com/joho/godotenv" + "github.com/stretchr/testify/assert" + "io/ioutil" "os" + "path/filepath" "reflect" "strings" "testing" "text/template" ) +// getTmpFile creates tmp file with a given content and returns its absolute path, relative path and function to remove the file +func getTmpFile(t *testing.T, content string) (string, string, func()) { + tmpFile, err := ioutil.TempFile("", "test-file") + if err != nil { + t.Fatal(err) + } + abs, err := filepath.Abs(tmpFile.Name()) + if err != nil { + t.Fatal(err) + } + wd, err := os.Getwd() + if err != nil { + t.Fatal(err) + } + rel, err := filepath.Rel(wd, abs) + _, err = tmpFile.WriteString(content) + if err != nil { + t.Fatal(err) + } + return rel, abs, func() { + _ := os.Remove(abs) + } +} + +func TestGenerateFile(t *testing.T) { + templatePathRel, templatePathAbs, removeTemplate := getTmpFile(t, "") + defer removeTemplate() + outputPathRel, outputPathAbs, removeOutput := getTmpFile(t, "") + defer removeOutput() + + + testCases := []struct { + name string + templatePath string + outputPath string + expectedError bool + }{ + {name: "template absolute path, output absolute path", templatePath: templatePathAbs, outputPath: outputPathAbs, expectedError: false}, + {name: "template absolute path, output relative path", templatePath: templatePathAbs, outputPath: outputPathRel, expectedError: false}, + {name: "template relative path, output absolute path", templatePath: templatePathRel, outputPath: outputPathAbs, expectedError: false}, + {name: "template relative path, output relative path", templatePath: templatePathRel, outputPath: outputPathRel, expectedError: false}, + {name: "non existing template relative path", templatePath: "./fooo", outputPath: outputPathRel, expectedError: true}, + {name: "non existing template absolute path", templatePath: "/xxx/yyy/foo/bar", outputPath: outputPathRel, expectedError: true}, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + err := generateFile(tc.templatePath, tc.outputPath, false, "{{", "}}") + if tc.expectedError { + assert.Error(t, err) + } else { + assert.NoError(t, err) + } + }) + } +} + func TestGenerateTemplate(t *testing.T) { templateName := "test" var tests = []struct {