Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not modify files if parsing fails #53

Merged
merged 1 commit into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))

}
Loading