Skip to content

Commit

Permalink
Do not modify files if parsing fails
Browse files Browse the repository at this point in the history
  • Loading branch information
neuroid committed Mar 5, 2024
1 parent b42fbf9 commit f58fe9c
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 @@ -117,6 +117,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 @@ -144,10 +148,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 f58fe9c

Please sign in to comment.