diff --git a/internal/parser/parser.go b/internal/parser/parser.go index 7ae3983..fa4fc70 100644 --- a/internal/parser/parser.go +++ b/internal/parser/parser.go @@ -71,17 +71,26 @@ func New(dirPath string, sharedPath string, fileSuffix string) (TemplateEngine, func (te *TemplateEngine) Parse(data TemplateData) ([]TemplateData, error) { result := []TemplateData{} + for name, tmpl := range te.templates { + newData, err := parse(name, tmpl, data, te.funcs, te.sharedTemplates) if err != nil { return result, err } + fileEx := filepath.Ext(newData.To) + if !strings.Contains(fileEx, "go") { + result = append(result, newData) + continue + } + formattedOut, err := format.Source(newData.Output) if err != nil { log.Printf(chalk.Red("error formatting: %s"), err) return result, err } + newData.Output = formattedOut result = append(result, newData) } diff --git a/internal/parser/parser_test.go b/internal/parser/parser_test.go index 947dd6c..c2e446e 100644 --- a/internal/parser/parser_test.go +++ b/internal/parser/parser_test.go @@ -5,6 +5,7 @@ import ( "testing" "github.com/code-gorilla-au/odize" + "github.com/code-gorilla-au/pyrotic/internal/testfixtures" ) func Test_withTemplates(t *testing.T) { @@ -52,25 +53,68 @@ func Test_withTemplates(t *testing.T) { } } -func TestTmplEngine_Parse_should_render(t *testing.T) { - strTmp := `--- - to: elo - --- - blah - ` +func TestTmplEngine_Parse(t *testing.T) { + group := odize.NewGroup(t, nil) + expected := TemplateData{ Name: "hello", } - te := &TemplateEngine{ - templates: map[string]string{"tmp": strTmp}, - funcs: defaultFuncs, - } - data, err := te.Parse(expected) + + var te *TemplateEngine + + group.BeforeEach(func() { + strGoFile := `--- + to: elo.go + --- + blah + ` + + strNonGoFile := `--- + to: {{ "elo" | caseSnake }} + --- + blah + ` + + te = &TemplateEngine{ + templates: map[string]string{ + "tmp": strGoFile, + "tmp2": strNonGoFile, + }, + funcs: defaultFuncs, + } + }) + + err := group. + Test("should parse go file and format", func(t *testing.T) { + data, err := te.Parse(expected) + odize.AssertNoError(t, err) + + result, found := testfixtures.Find(data, func(d TemplateData) bool { + return d.To == "elo.go" + }) + odize.AssertTrue(t, found) + + odize.AssertEqual(t, expected.Name, result.Name) + odize.AssertEqual(t, "elo.go", result.To) + odize.AssertEqual(t, "blah", strings.TrimSpace(string(result.Output))) + }). + Test("should parse non go file", func(t *testing.T) { + data, err := te.Parse(expected) + + result, found := testfixtures.Find(data, func(d TemplateData) bool { + return d.To == "elo" + }) + odize.AssertTrue(t, found) + + odize.AssertNoError(t, err) + odize.AssertEqual(t, expected.Name, result.Name) + odize.AssertEqual(t, "elo", result.To) + odize.AssertEqual(t, "blah", strings.TrimSpace(string(result.Output))) + }). + Run() odize.AssertNoError(t, err) - odize.AssertEqual(t, expected.Name, data[0].Name) - odize.AssertEqual(t, "elo", data[0].To) - odize.AssertEqual(t, "blah", strings.TrimSpace(string(data[0].Output))) } + func TestTmplEngine_Parse_missing_funcs_should_fail_on_meta_parse(t *testing.T) { strTmp := `--- to: {{ "elo" | caseSnake }} diff --git a/internal/testfixtures/testfixtures.go b/internal/testfixtures/testfixtures.go new file mode 100644 index 0000000..d188d79 --- /dev/null +++ b/internal/testfixtures/testfixtures.go @@ -0,0 +1,11 @@ +package testfixtures + +func Find[T any](data []T, fn func(T) bool) (T, bool) { + for _, item := range data { + if fn(item) { + return item, true + } + } + var zero T + return zero, false +} diff --git a/scripts/tests.mk b/scripts/tests.mk index 773167b..eff112d 100644 --- a/scripts/tests.mk +++ b/scripts/tests.mk @@ -10,6 +10,9 @@ test: test-unit test-integration ## Run all tests test-unit: ## Run unit tests go test -coverprofile $(COVER_OUTPUT_RAW) --short -cover -failfast ./... +test-unit-watch: ## Run unit tests in watch mode + gow test -coverprofile $(COVER_OUTPUT_RAW) --short -cover -failfast ./... + test-integration: build ## Run integration test ENV=DEV ./pyrotic -p example/_templates generate fakr --meta foo=bar,bin=baz,enum_list=a-long-list