diff --git a/go.mod b/go.mod index 2e11fe52b..d2df89d07 100644 --- a/go.mod +++ b/go.mod @@ -22,7 +22,7 @@ require ( github.com/onflow/flixkit-go/v2 v2.0.0 github.com/onflow/flow-core-contracts/lib/go/templates v1.3.1 github.com/onflow/flow-emulator v1.0.0 - github.com/onflow/flow-evm-gateway v0.36.3 + github.com/onflow/flow-evm-gateway v0.36.4 github.com/onflow/flow-go v0.37.10 github.com/onflow/flow-go-sdk v1.0.0-preview.56 github.com/onflow/flowkit/v2 v2.0.0 diff --git a/go.sum b/go.sum index 8aa4ffd73..a9ea56892 100644 --- a/go.sum +++ b/go.sum @@ -2178,8 +2178,8 @@ github.com/onflow/flow-core-contracts/lib/go/templates v1.3.1 h1:FfhMBAb78p6VAWk github.com/onflow/flow-core-contracts/lib/go/templates v1.3.1/go.mod h1:NgbMOYnMh0GN48VsNKZuiwK7uyk38Wyo8jN9+C9QE30= github.com/onflow/flow-emulator v1.0.0 h1:CCE9mFUYidb4YPQWFSBHzcBGggs5bXVqIh02wF2wRr0= github.com/onflow/flow-emulator v1.0.0/go.mod h1:sHbe9e1RG7Y6LA/dFyLEoBnKyjJ4iHeOdkXIobMjjrE= -github.com/onflow/flow-evm-gateway v0.36.3 h1:bk08W5bYTSKRmgR/TRwO9LbTp0c3rybH8QpZr8j/LuM= -github.com/onflow/flow-evm-gateway v0.36.3/go.mod h1:LWeu5hyXWU9mWPlKgeJOGcHRO67/hMtiyxtMFxRTZY4= +github.com/onflow/flow-evm-gateway v0.36.4 h1:AvPahikqfSN8SCMHn16ZkPz5Vvg60F1HkMHsFsQzkus= +github.com/onflow/flow-evm-gateway v0.36.4/go.mod h1:LWeu5hyXWU9mWPlKgeJOGcHRO67/hMtiyxtMFxRTZY4= github.com/onflow/flow-ft/lib/go/contracts v1.0.0 h1:mToacZ5NWqtlWwk/7RgIl/jeKB/Sy/tIXdw90yKHcV0= github.com/onflow/flow-ft/lib/go/contracts v1.0.0/go.mod h1:PwsL8fC81cjnUnTfmyL/HOIyHnyaw/JA474Wfj2tl6A= github.com/onflow/flow-ft/lib/go/templates v1.0.0 h1:6cMS/lUJJ17HjKBfMO/eh0GGvnpElPgBXx7h5aoWJhs= diff --git a/internal/super/generate.go b/internal/super/generate.go index 389c9ffcc..9a3d2e9e0 100644 --- a/internal/super/generate.go +++ b/internal/super/generate.go @@ -76,10 +76,22 @@ var GenerateScriptCommand = &command.Command{ RunS: generateScript, } +var GenerateTestCommand = &command.Command{ + Cmd: &cobra.Command{ + Use: "test ", + Short: "Generate a Cadence test template", + Example: "flow generate test SomeTest", + Args: cobra.ExactArgs(1), + }, + Flags: &generateFlags, + RunS: generateTest, +} + func init() { GenerateContractCommand.AddToParent(GenerateCommand) GenerateTransactionCommand.AddToParent(GenerateCommand) GenerateScriptCommand.AddToParent(GenerateCommand) + GenerateTestCommand.AddToParent(GenerateCommand) } func generateContract( @@ -120,3 +132,16 @@ func generateScript( err = g.Create(generator.ScriptTemplate{Name: name}) return nil, err } + +func generateTest( + args []string, + _ command.GlobalFlags, + logger output.Logger, + _ flowkit.Services, + state *flowkit.State, +) (result command.Result, err error) { + g := generator.NewGenerator("", state, logger, false, true) + name := util.StripCDCExtension(args[0]) + err = g.Create(generator.TestTemplate{Name: name}) + return nil, err +} diff --git a/internal/super/generator/contract_template.go b/internal/super/generator/contract_template.go index ae78f0263..f8bbb5dd9 100644 --- a/internal/super/generator/contract_template.go +++ b/internal/super/generator/contract_template.go @@ -110,7 +110,8 @@ func (c ContractTemplate) GetChildren() []TemplateItem { return []TemplateItem{ TestTemplate{ - Name: fmt.Sprintf("%s_test", c.Name), + Name: fmt.Sprintf("%s_test", c.Name), + TemplatePath: "contract_init_test.cdc.tmpl", Data: map[string]interface{}{ "ContractName": c.Name, }, diff --git a/internal/super/generator/generator.go b/internal/super/generator/generator.go index d31ea5c34..aa05216f4 100644 --- a/internal/super/generator/generator.go +++ b/internal/super/generator/generator.go @@ -22,7 +22,6 @@ import ( "bytes" "embed" "fmt" - "path" "path/filepath" "text/template" @@ -114,8 +113,7 @@ func (g *Generator) generate(item TemplateItem) error { outputContent, err := g.processTemplate(templatePath, fileData) if err != nil { - // TODO, better error based on template type - return fmt.Errorf("error generating template: %w", err) + return fmt.Errorf("error generating %s template: %w", item.GetType(), err) } targetPath := filepath.Join(rootDir, targetRelativeToRoot) @@ -138,8 +136,7 @@ func (g *Generator) generate(item TemplateItem) error { } if !g.disableLogs { - // TODO: Add more detailed logging - g.logger.Info(fmt.Sprintf("Generated %s", targetPath)) + g.logger.Info(fmt.Sprintf("Generated new %s: %s", item.GetType(), targetPath)) } // Call template state update function if it exists @@ -156,7 +153,8 @@ func (g *Generator) generate(item TemplateItem) error { // processTemplate reads a template file from the embedded filesystem and processes it with the provided data // If you don't need to provide data, pass nil func (g *Generator) processTemplate(templatePath string, data map[string]interface{}) (string, error) { - templateData, err := templatesFS.ReadFile(path.Join("templates", templatePath)) + resolvedPath := filepath.Join("templates", templatePath) + templateData, err := templatesFS.ReadFile(filepath.ToSlash(resolvedPath)) if err != nil { return "", fmt.Errorf("failed to read template file: %w", err) } diff --git a/internal/super/generator/templates/empty_test.cdc.tmpl b/internal/super/generator/templates/empty_test.cdc.tmpl new file mode 100644 index 000000000..98ea2508a --- /dev/null +++ b/internal/super/generator/templates/empty_test.cdc.tmpl @@ -0,0 +1,8 @@ +import Test + +access(all) let account = Test.createAccount() + +access(all) fun testExample() { + // Test something + Test.expect(true, true) +} \ No newline at end of file diff --git a/internal/super/generator/test_template.go b/internal/super/generator/test_template.go index de936ea27..49e13e718 100644 --- a/internal/super/generator/test_template.go +++ b/internal/super/generator/test_template.go @@ -48,7 +48,7 @@ func (o TestTemplate) GetName() string { // GetTemplate returns an empty string for scripts and transactions func (o TestTemplate) GetTemplatePath() string { if o.TemplatePath == "" { - return "contract_init_test.cdc.tmpl" + return "empty_test.cdc.tmpl" } return o.TemplatePath