Skip to content

Commit

Permalink
Improvements to persister_test.go
Browse files Browse the repository at this point in the history
  • Loading branch information
pdecks committed Jan 16, 2024
1 parent bfb05e2 commit f338591
Showing 1 changed file with 62 additions and 15 deletions.
77 changes: 62 additions & 15 deletions persister_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package pgs

import (
"errors"
"html/template"
"testing"

"errors"

"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
)
Expand All @@ -29,6 +28,7 @@ func TestPersister_Persist_GeneratorFile(t *testing.T) {
fs := afero.NewMemMapFs()
p.SetFS(fs)

p.SetOutputPath("no/effect") // only used for CustomFile and CustomTemplateFile
resp := p.Persist(
GeneratorFile{
Name: "foo",
Expand All @@ -47,6 +47,8 @@ func TestPersister_Persist_GeneratorFile(t *testing.T) {
assert.Len(t, resp.File, 2)
assert.Equal(t, "foo", resp.File[0].GetName())
assert.Equal(t, "fizz", resp.File[0].GetContent())
assert.Equal(t, "quux", resp.File[1].GetName())
assert.Equal(t, "baz", resp.File[1].GetContent())
}

var genTpl = template.Must(template.New("good").Parse("{{ . }}"))
Expand Down Expand Up @@ -86,7 +88,9 @@ func TestPersister_Persist_GeneratorTemplateFile(t *testing.T) {

assert.Len(t, resp.File, 2)
assert.Equal(t, "foo", resp.File[0].GetName())
assert.Equal(t, "fizz", resp.File[0].GetContent())
assert.Equal(t, "fizz", resp.File[0].GetContent()) // overwritten
assert.Equal(t, "quux", resp.File[1].GetName())
assert.Equal(t, "baz", resp.File[1].GetContent())
}

func TestPersister_Persist_GeneratorAppend(t *testing.T) {
Expand All @@ -111,8 +115,12 @@ func TestPersister_Persist_GeneratorAppend(t *testing.T) {
)

assert.Len(t, resp.File, 4)
assert.Equal(t, "foo", resp.File[0].GetName())
assert.Equal(t, "", resp.File[0].GetContent())
assert.Equal(t, "", resp.File[1].GetName())
assert.Equal(t, "baz", resp.File[1].GetContent())
assert.Equal(t, "bar", resp.File[2].GetName())
assert.Equal(t, "", resp.File[2].GetContent())
assert.Equal(t, "", resp.File[3].GetName())
assert.Equal(t, "quux", resp.File[3].GetContent())

Expand Down Expand Up @@ -145,8 +153,11 @@ func TestPersister_Persist_GeneratorAppendSeveral(t *testing.T) {
)

assert.Len(t, resp.File, 3)
assert.Equal(t, "file", resp.File[0].GetName())
assert.Equal(t, "foo", resp.File[0].GetContent())
assert.Equal(t, "", resp.File[1].GetName())
assert.Equal(t, "bar", resp.File[1].GetContent())
assert.Equal(t, "", resp.File[2].GetName())
assert.Equal(t, "baz", resp.File[2].GetContent())
}

Expand Down Expand Up @@ -177,8 +188,12 @@ func TestPersister_Persist_GeneratorTemplateAppend(t *testing.T) {
)

assert.Len(t, resp.File, 4)
assert.Equal(t, "foo", resp.File[0].GetName())
assert.Equal(t, "", resp.File[0].GetContent())
assert.Equal(t, "", resp.File[1].GetName())
assert.Equal(t, "baz", resp.File[1].GetContent())
assert.Equal(t, "bar", resp.File[2].GetName())
assert.Equal(t, "", resp.File[2].GetContent())
assert.Equal(t, "", resp.File[3].GetName())
assert.Equal(t, "quux", resp.File[3].GetContent())

Expand Down Expand Up @@ -236,6 +251,8 @@ func TestPersister_Persist_GeneratorTemplateInjection(t *testing.T) {
assert.Equal(t, "baz", resp.File[0].GetContent())
}

// The persister writes these artifacts directly to disk, so there should
// not be any entries in CodeGeneratorResponse.File.
func TestPersister_Persist_CustomFile(t *testing.T) {
t.Parallel()

Expand All @@ -244,38 +261,54 @@ func TestPersister_Persist_CustomFile(t *testing.T) {
fs := afero.NewMemMapFs()
p.SetFS(fs)

p.Persist(CustomFile{
f := CustomFile{
Name: "foo/bar/baz.txt",
Perms: 0655,
Contents: "fizz",
})
}

// without OutputPath
resp := p.Persist(f)
assert.Len(t, resp.File, 0)
b, err := afero.ReadFile(fs, "foo/bar/baz.txt")
assert.NoError(t, err)
assert.Equal(t, "fizz", string(b))

p.Persist(CustomFile{
// with OutputPath
p.SetOutputPath("some/path")
resp = p.Persist(f)
assert.Len(t, resp.File, 0)
ok, err := afero.Exists(fs, "some/path/foo/bar/baz.txt")
assert.NoError(t, err)
assert.True(t, ok)

// change data, overwrite = false
p.SetOutputPath("")
resp = p.Persist(CustomFile{
Name: "foo/bar/baz.txt",
Perms: 0655,
Contents: "buzz",
})

assert.Len(t, resp.File, 0)
b, err = afero.ReadFile(fs, "foo/bar/baz.txt")
assert.NoError(t, err)
assert.Equal(t, "fizz", string(b))

p.Persist(CustomFile{
// change data, overwrite = true
resp = p.Persist(CustomFile{
Name: "foo/bar/baz.txt",
Perms: 0655,
Contents: "buzz",
Overwrite: true,
})

assert.Len(t, resp.File, 0)
b, err = afero.ReadFile(fs, "foo/bar/baz.txt")
assert.NoError(t, err)
assert.Equal(t, "buzz", string(b))
}

// The persister writes these artifacts directly to disk, so there should
// not be any entries in CodeGeneratorResponse.File.
func TestPersister_Persist_CustomTemplateFile(t *testing.T) {
t.Parallel()

Expand All @@ -284,33 +317,47 @@ func TestPersister_Persist_CustomTemplateFile(t *testing.T) {
fs := afero.NewMemMapFs()
p.SetFS(fs)

p.Persist(CustomTemplateFile{
f := CustomTemplateFile{
Name: "foo/bar/baz.txt",
Perms: 0655,
TemplateArtifact: TemplateArtifact{
Template: genTpl,
Data: "fizz",
},
})
}

// without OutputPath
resp := p.Persist(f)
assert.Equal(t, len(resp.File), 0)
b, err := afero.ReadFile(fs, "foo/bar/baz.txt")
assert.NoError(t, err)
assert.Equal(t, "fizz", string(b))

p.Persist(CustomTemplateFile{
// with OutputPath
p.SetOutputPath("some/path")
resp = p.Persist(f)
assert.Equal(t, len(resp.File), 0)
ok, err := afero.Exists(fs, "some/path/foo/bar/baz.txt")
assert.NoError(t, err)
assert.True(t, ok)

// change data, overwrite = false
p.SetOutputPath("")
resp = p.Persist(CustomTemplateFile{
Name: "foo/bar/baz.txt",
Perms: 0655,
TemplateArtifact: TemplateArtifact{
Template: genTpl,
Data: "buzz",
},
})

assert.Equal(t, len(resp.File), 0)
b, err = afero.ReadFile(fs, "foo/bar/baz.txt")
assert.NoError(t, err)
assert.Equal(t, "fizz", string(b))

p.Persist(CustomTemplateFile{
// change data, overwrite = true
resp = p.Persist(CustomTemplateFile{
Name: "foo/bar/baz.txt",
Perms: 0655,
TemplateArtifact: TemplateArtifact{
Expand All @@ -319,7 +366,7 @@ func TestPersister_Persist_CustomTemplateFile(t *testing.T) {
},
Overwrite: true,
})

assert.Equal(t, len(resp.File), 0)
b, err = afero.ReadFile(fs, "foo/bar/baz.txt")
assert.NoError(t, err)
assert.Equal(t, "buzz", string(b))
Expand Down

0 comments on commit f338591

Please sign in to comment.