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

render initial file even when template is empty #1397

Merged
merged 1 commit into from
Jul 24, 2020
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
5 changes: 3 additions & 2 deletions renderer/renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,12 @@ type RenderResult struct {
// whether it would have rendered and actually did render.
func Render(i *RenderInput) (*RenderResult, error) {
existing, err := ioutil.ReadFile(i.Path)
if err != nil && !os.IsNotExist(err) {
fileExists := !os.IsNotExist(err)
if err != nil && fileExists {
return nil, errors.Wrap(err, "failed reading file")
}

if bytes.Equal(existing, i.Contents) {
if bytes.Equal(existing, i.Contents) && fileExists {
return &RenderResult{
DidRender: false,
WouldRender: true,
Expand Down
117 changes: 117 additions & 0 deletions renderer/renderer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"io/ioutil"
"os"
"path"
"path/filepath"
"testing"
)
Expand Down Expand Up @@ -198,3 +199,119 @@ func TestAtomicWrite(t *testing.T) {
contains(outFile.Name(), "second")
})
}

func TestRender(t *testing.T) {
t.Run("file-exists-same-content", func(t *testing.T) {
outDir, err := ioutil.TempDir("", "")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(outDir)
outFile, err := ioutil.TempFile(outDir, "")
if err != nil {
t.Fatal(err)
}
contents := []byte("first")
if _, err := outFile.Write(contents); err != nil {
t.Fatal(err)
}
path := outFile.Name()
if err = outFile.Close(); err != nil {
t.Fatal(err)
}

rr, err := Render(&RenderInput{
Path: path,
Contents: contents,
})
if err != nil {
t.Fatal(err)
}
switch {
case rr.WouldRender && !rr.DidRender:
default:
t.Fatalf("Bad render results; would: %v, did: %v",
rr.WouldRender, rr.DidRender)
}
})
t.Run("file-exists-diff-content", func(t *testing.T) {
outDir, err := ioutil.TempDir("", "")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(outDir)
outFile, err := ioutil.TempFile(outDir, "")
if err != nil {
t.Fatal(err)
}
contents := []byte("first")
if _, err := outFile.Write(contents); err != nil {
t.Fatal(err)
}
path := outFile.Name()
if err = outFile.Close(); err != nil {
t.Fatal(err)
}

diff_contents := []byte("not-first")
rr, err := Render(&RenderInput{
Path: path,
Contents: diff_contents,
})
if err != nil {
t.Fatal(err)
}
switch {
case rr.WouldRender && rr.DidRender:
default:
t.Fatalf("Bad render results; would: %v, did: %v",
rr.WouldRender, rr.DidRender)
}
})
t.Run("file-no-exists", func(t *testing.T) {
outDir, err := ioutil.TempDir("", "")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(outDir)
path := path.Join(outDir, "no-exists")
contents := []byte("first")

rr, err := Render(&RenderInput{
Path: path,
Contents: contents,
})
if err != nil {
t.Fatal(err)
}
switch {
case rr.WouldRender && rr.DidRender:
default:
t.Fatalf("Bad render results; would: %v, did: %v",
rr.WouldRender, rr.DidRender)
}
})
t.Run("empty-file-no-exists", func(t *testing.T) {
outDir, err := ioutil.TempDir("", "")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(outDir)
path := path.Join(outDir, "no-exists")
contents := []byte{}

rr, err := Render(&RenderInput{
Path: path,
Contents: contents,
})
if err != nil {
t.Fatal(err)
}
switch {
case rr.WouldRender && rr.DidRender:
default:
t.Fatalf("Bad render results; would: %v, did: %v",
rr.WouldRender, rr.DidRender)
}
})
}