From 294a815f6f44e040cc40127e152cc59f5657b5e5 Mon Sep 17 00:00:00 2001 From: "Ach.Vani Ardiansyah" Date: Sun, 15 Sep 2024 21:35:42 +0700 Subject: [PATCH 1/4] feature(generate) use go format in model generator --- pkg/generator/generator.go | 46 +++++++++++++++++++++++++++++++++++++- pkg/generator/model.go | 5 ++++- 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/pkg/generator/generator.go b/pkg/generator/generator.go index 62f15513..a5783fc7 100644 --- a/pkg/generator/generator.go +++ b/pkg/generator/generator.go @@ -1,8 +1,10 @@ package generator import ( + "bytes" "fmt" "go/ast" + "go/format" "go/parser" "go/token" "io" @@ -40,6 +42,31 @@ func DefaultWriter(filePath string) (*os.File, error) { return file, nil } +type FileWriter struct { + FilePath string + file *os.File +} + +func (fw *FileWriter) Write(p []byte) (int, error) { + if fw.file == nil { + file, err := utils.CreateFile(fw.FilePath, true) + if err != nil { + return 0, fmt.Errorf("failed create file %s : %v", fw.FilePath, err) + } + fw.file = file + + } + return fw.file.Write(p) +} + +// Close closes the underlying file +func (fw *FileWriter) Close() error { + if fw.file != nil { + return fw.file.Close() + } + return nil +} + func Generate(input GenerateInput, writer io.Writer) error { // set default writer if writer == nil { @@ -61,7 +88,24 @@ func Generate(input GenerateInput, writer io.Writer) error { return fmt.Errorf("error parsing : %v", err) } - return tmpl.Execute(writer, input.BindData) + var renderedCode bytes.Buffer + err = tmpl.Execute(&renderedCode, input.BindData) + if err != nil { + return fmt.Errorf("error execute template : %v", err) + } + + // Step 2: Format the rendered code + formattedCode, err := format.Source(renderedCode.Bytes()) + if err != nil { + return fmt.Errorf("error format code : %v", err) + } + + _, err = writer.Write(formattedCode) + if err != nil { + return err + } + + return nil } func CreateInternalFolder(basePath string) (err error) { diff --git a/pkg/generator/model.go b/pkg/generator/model.go index dad0549f..c88bbf22 100644 --- a/pkg/generator/model.go +++ b/pkg/generator/model.go @@ -147,8 +147,11 @@ func GenerateModel(folderPath string, input *GenerateModelInput, generateFn Gene OutputPath: filePath, } + // setup writer + writer := FileWriter{FilePath: filePath} + ModelLogger.Debug("generate model", "path", generateInput.OutputPath) - return generateFn(generateInput, nil) + return generateFn(generateInput, &writer) } // map table to column, map pg type to go type and get dependency import path From 7e6c5587e6fb66bfa88d165cb01edba9a0d143ee Mon Sep 17 00:00:00 2001 From: "Ach.Vani Ardiansyah" Date: Sun, 15 Sep 2024 22:02:15 +0700 Subject: [PATCH 2/4] feature(generator) fix unit test in generator package --- pkg/generator/generator.go | 18 +++++++++--------- pkg/resource/import.go | 2 +- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/pkg/generator/generator.go b/pkg/generator/generator.go index a5783fc7..7da30398 100644 --- a/pkg/generator/generator.go +++ b/pkg/generator/generator.go @@ -55,8 +55,15 @@ func (fw *FileWriter) Write(p []byte) (int, error) { } fw.file = file + defer fw.file.Close() } - return fw.file.Write(p) + + formattedCode, err := format.Source(p) + if err != nil { + return 0, fmt.Errorf("error format code : %v", err) + } + + return fw.file.Write(formattedCode) } // Close closes the underlying file @@ -74,7 +81,6 @@ func Generate(input GenerateInput, writer io.Writer) error { if err != nil { return err } - defer file.Close() writer = file } @@ -94,13 +100,7 @@ func Generate(input GenerateInput, writer io.Writer) error { return fmt.Errorf("error execute template : %v", err) } - // Step 2: Format the rendered code - formattedCode, err := format.Source(renderedCode.Bytes()) - if err != nil { - return fmt.Errorf("error format code : %v", err) - } - - _, err = writer.Write(formattedCode) + _, err = writer.Write(renderedCode.Bytes()) if err != nil { return err } diff --git a/pkg/resource/import.go b/pkg/resource/import.go index dcf6b705..f35db986 100644 --- a/pkg/resource/import.go +++ b/pkg/resource/import.go @@ -305,7 +305,7 @@ func generateImportResource(config *raiden.Config, importState *state.LocalState func ImportDecorateFunc[T any](data []T, findFunc func(T, generator.GenerateInput) bool, stateChan chan any) generator.GenerateFn { return func(input generator.GenerateInput, writer io.Writer) error { - if err := generator.Generate(input, nil); err != nil { + if err := generator.Generate(input, writer); err != nil { return err } if rs, found := FindImportResource(data, input, findFunc); found { From 556d669cab487a8120715b01c9378ada4a3a82a9 Mon Sep 17 00:00:00 2001 From: "Ach.Vani Ardiansyah" Date: Sun, 15 Sep 2024 23:45:38 +0700 Subject: [PATCH 3/4] feature(generator) increate coverage --- pkg/generator/generator.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pkg/generator/generator.go b/pkg/generator/generator.go index 7da30398..af2ea2b8 100644 --- a/pkg/generator/generator.go +++ b/pkg/generator/generator.go @@ -54,8 +54,7 @@ func (fw *FileWriter) Write(p []byte) (int, error) { return 0, fmt.Errorf("failed create file %s : %v", fw.FilePath, err) } fw.file = file - - defer fw.file.Close() + defer fw.Close() } formattedCode, err := format.Source(p) From 0e1c50c18bfce5ad387599fd917ca90f2303b789 Mon Sep 17 00:00:00 2001 From: "Ach.Vani Ardiansyah" Date: Mon, 16 Sep 2024 00:08:13 +0700 Subject: [PATCH 4/4] feature(generator) increase coverage test in generator/generator.go --- pkg/generator/generator_test.go | 76 +++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 pkg/generator/generator_test.go diff --git a/pkg/generator/generator_test.go b/pkg/generator/generator_test.go new file mode 100644 index 00000000..80183e99 --- /dev/null +++ b/pkg/generator/generator_test.go @@ -0,0 +1,76 @@ +package generator_test + +import ( + "testing" + + "github.com/sev-2/raiden/pkg/generator" + "github.com/stretchr/testify/assert" +) + +func TestGenerate_ErrorWritingToFile(t *testing.T) { + invalidPath := "/invalid_path/output.txt" + + tmpl := "{{ .Name }}" + input := generator.GenerateInput{ + BindData: struct{ Name string }{"John"}, + Template: tmpl, + TemplateName: "testTemplate", + OutputPath: invalidPath, + } + + err := generator.Generate(input, nil) + assert.Error(t, err) + assert.Contains(t, err.Error(), "failed create file") +} + +func TestGenerate_ErrorParsingTemplate(t *testing.T) { + input := generator.GenerateInput{ + BindData: nil, + Template: "{{ .invalid}", + TemplateName: "testTemplate", + OutputPath: "test_output.txt", + } + + err := generator.Generate(input, nil) + assert.Error(t, err) + assert.Contains(t, err.Error(), "error parsing") +} + +func TestGenerate_ErrorExecutingTemplate(t *testing.T) { + tmpl := "{{ .Name }}" + input := generator.GenerateInput{ + BindData: struct{}{}, + Template: tmpl, + TemplateName: "testTemplate", + OutputPath: "test_output.txt", + } + + err := generator.Generate(input, nil) + assert.Error(t, err) + assert.Contains(t, err.Error(), "error execute template") +} + +// Test for FileWriter Write function +func TestFileWriter_Write_ErrorCreatingFile(t *testing.T) { + invalidPath := "/invalid_path/output.txt" // Simulating an invalid path + + fw := &generator.FileWriter{FilePath: invalidPath} + _, err := fw.Write([]byte("test content")) + assert.Error(t, err) + assert.Contains(t, err.Error(), "failed create file") +} + +func TestFileWriter_Write_ErrorFormattingCode(t *testing.T) { + validPath := "test_err_output_formatting.txt" + + fw := &generator.FileWriter{FilePath: validPath} + + input := generator.GenerateInput{ + BindData: struct{ Name string }{"invalid code"}, + Template: "{{ .Name }}", + TemplateName: "testTemplate", + } + + err := generator.Generate(input, fw) + assert.Contains(t, err.Error(), "error format code") +}