Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into jribbink/readme
Browse files Browse the repository at this point in the history
  • Loading branch information
jribbink committed Oct 1, 2024
2 parents e85b77e + 5e02239 commit 6e6d2e6
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 11 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
25 changes: 25 additions & 0 deletions internal/super/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,22 @@ var GenerateScriptCommand = &command.Command{
RunS: generateScript,
}

var GenerateTestCommand = &command.Command{
Cmd: &cobra.Command{
Use: "test <name>",
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(
Expand Down Expand Up @@ -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
}
3 changes: 2 additions & 1 deletion internal/super/generator/contract_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down
10 changes: 4 additions & 6 deletions internal/super/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"bytes"
"embed"
"fmt"
"path"
"path/filepath"
"text/template"

Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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)
}
Expand Down
8 changes: 8 additions & 0 deletions internal/super/generator/templates/empty_test.cdc.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import Test

access(all) let account = Test.createAccount()

access(all) fun testExample() {
// Test something
Test.expect(true, true)
}
2 changes: 1 addition & 1 deletion internal/super/generator/test_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6e6d2e6

Please sign in to comment.