From 5408a986aba56b20a765fcbccefc25558ad59150 Mon Sep 17 00:00:00 2001 From: Jordan Ribbink Date: Thu, 26 Sep 2024 13:53:10 -0700 Subject: [PATCH] Add command for generating test files (#1773) --- internal/super/generate.go | 25 +++++++++++++++++++ internal/super/generator/contract_template.go | 3 ++- .../generator/templates/empty_test.cdc.tmpl | 8 ++++++ internal/super/generator/test_template.go | 2 +- 4 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 internal/super/generator/templates/empty_test.cdc.tmpl 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/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