Skip to content

Commit

Permalink
feat: allow relative file paths (#25)
Browse files Browse the repository at this point in the history
* feat: allow relative file paths

Signed-off-by: Martin Chodur <m.chodur@seznam.cz>

* feat: add tests for template paths

Signed-off-by: Martin Chodur <m.chodur@seznam.cz>

* fix: optimize test file handling

Signed-off-by: Martin Chodur <m.chodur@seznam.cz>

* Update template_test.go

Co-authored-by: Rudo Thomas <rudolf.thomas@firma.seznam.cz>

Co-authored-by: Rudo Thomas <rudolf.thomas@firma.seznam.cz>
  • Loading branch information
FUSAKLA and rudo-thomas authored Aug 18, 2021
1 parent 868550e commit d2f039f
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 9 deletions.
3 changes: 2 additions & 1 deletion debian/changelog
Original file line number Diff line number Diff line change
Expand Up @@ -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 <martin.chodur@firma.seznam.cz> Wed, 11 Aug 2021 13:56:32 +0200

Expand Down
8 changes: 0 additions & 8 deletions template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
60 changes: 60 additions & 0 deletions template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit d2f039f

Please sign in to comment.