github.com/snyk/vervet/v4@v4.27.2/generate/generate_test.go (about)

     1  package generate_test
     2  
     3  import (
     4  	"os"
     5  	"testing"
     6  	"testing/fstest"
     7  	"text/template"
     8  
     9  	qt "github.com/frankban/quicktest"
    10  
    11  	"github.com/snyk/vervet/v4/generate"
    12  	"github.com/snyk/vervet/v4/testdata"
    13  )
    14  
    15  func TestGenerateFS(t *testing.T) {
    16  	c := qt.New(t)
    17  	out := c.TempDir()
    18  
    19  	fs := fstest.MapFS{
    20  		"generator.yaml": &fstest.MapFile{
    21  			Data: []byte(`
    22  version-readme:
    23    scope: version
    24    filename: "` + out + `/{{.API}}/{{.Resource}}/{{.Version.DateString}}/README"
    25    template: "/README.tmpl"
    26  `),
    27  			Mode: 0666,
    28  		},
    29  		"README.tmpl": &fstest.MapFile{
    30  			Data: []byte(`
    31  This is a generated scaffold for version {{ .Version.String }} of the
    32  {{ .Resource }} resource in API {{ .API }}.
    33  
    34  {{ "hello" | testFunc }}
    35  `),
    36  			Mode: 0666,
    37  		},
    38  	}
    39  
    40  	params := generate.GeneratorParams{
    41  		ProjectDir:     testdata.Path("."),
    42  		ConfigFile:     testdata.Path(".vervet.yaml"),
    43  		GeneratorsFile: "/generator.yaml",
    44  		Generators:     []string{"version-readme"},
    45  		FS:             fs,
    46  		Functions: template.FuncMap{
    47  			"testFunc": func(s string) string {
    48  				return "j" + s[1:] + " world!"
    49  			},
    50  		},
    51  	}
    52  	err := generate.Generate(params)
    53  	c.Assert(err, qt.IsNil)
    54  
    55  	contents, err := os.ReadFile(out + "/testdata/hello-world/2021-06-01/README")
    56  	c.Assert(err, qt.IsNil)
    57  	c.Assert(string(contents), qt.Equals, `
    58  This is a generated scaffold for version 2021-06-01~experimental of the
    59  hello-world resource in API testdata.
    60  
    61  jello world!
    62  `)
    63  }