Skip to content

Commit

Permalink
Merge pull request #53 from neuroid/errors
Browse files Browse the repository at this point in the history
Do not modify files if parsing fails
  • Loading branch information
enc authored Apr 26, 2024
2 parents a3dd47f + f58fe9c commit 163b12b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
8 changes: 4 additions & 4 deletions envplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ func (h *Handler) parse(file string) error {

})

if len(errors) > 0 {
return errors[0]
}

if h.DryRun {
Log(DEBUG, "Expanding all references in '%s' without doing anything (dry-run)", file)
Log(RAW, parsed)
Expand Down Expand Up @@ -143,10 +147,6 @@ func (h *Handler) parse(file string) error {

}

if len(errors) > 0 {
return errors[0]
}

return nil

}
Expand Down
23 changes: 21 additions & 2 deletions envplate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,11 +229,30 @@ func TestStrictParse(t *testing.T) {
file = "test/template4.txt"
)

defer _restore(file)

err := handler.parse(file)
assert.Error(err)

assert.Equal("'test/template4.txt' requires undeclared environment variable 'ANOTHER_DATABASE', but cannot use default 'db2.example.com' (strict-mode)", err.Error())

}

func TestAbortOnParseErrors(t *testing.T) {

assert := assert.New(t)
handler := &Handler{
Backup: true,
Strict: true,
}

var (
file = "test/template4.txt"
template = _read(t, file)
)

err := handler.parse(file)
assert.Error(err)

assert.Equal(template, _read(t, file))
assert.NoFileExists(fmt.Sprintf("%s.bak", file))

}

0 comments on commit 163b12b

Please sign in to comment.