From 8370de26db68d26abe84fa913c26b23b18dd5513 Mon Sep 17 00:00:00 2001 From: Chase Fleming <1666730+chasefleming@users.noreply.github.com> Date: Tue, 12 Mar 2024 16:48:50 -0700 Subject: [PATCH 01/11] Generate contract test file along with contract --- internal/super/generate.go | 60 ++++++++++++++++++++++++++++----- internal/super/generate_test.go | 20 +++++++++++ 2 files changed, 71 insertions(+), 9 deletions(-) diff --git a/internal/super/generate.go b/internal/super/generate.go index 5837819ab..68586fd38 100644 --- a/internal/super/generate.go +++ b/internal/super/generate.go @@ -20,6 +20,7 @@ package super import ( "fmt" + "github.com/onflow/flow-go-sdk" "path/filepath" "strings" @@ -116,6 +117,13 @@ func generateScript( return generateNew(args, "script", logger, state) } +func addCDCExtension(name string) string { + if strings.HasSuffix(name, ".cdc") { + return name + } + return fmt.Sprintf("%s.cdc", name) +} + func generateNew( args []string, templateType string, @@ -127,17 +135,12 @@ func generateNew( } name := args[0] - var filename string - - // Don't add .cdc extension if it's already there - if strings.HasSuffix(name, ".cdc") { - filename = name - } else { - filename = fmt.Sprintf("%s.cdc", name) - } + var filename = addCDCExtension(name) var fileToWrite string + var testFileToWrite string var basePath string + var testsBasePath = "cadence/tests" if generateFlags.Directory != "" { basePath = generateFlags.Directory @@ -161,6 +164,19 @@ access(all) contract %s { init() {} }`, name) + testFileToWrite = fmt.Sprintf(`import Test + +access(all) let account = Test.createAccount() + +access(all) fun testContract() { + let err = Test.deployContract( + name: "%s", + path: "../contracts/%s.cdc", + arguments: [], + ) + + Test.expect(err, Test.beNil()) +}`, name, name) case "script": fileToWrite = `access(all) fun main() { @@ -177,26 +193,52 @@ fun main() { } filenameWithBasePath := filepath.Join(basePath, filename) + testFilenameWithBasePath := filepath.Join(testsBasePath, addCDCExtension(fmt.Sprintf("%s_test", name))) // Check file existence if _, err := state.ReaderWriter().ReadFile(filenameWithBasePath); err == nil { return nil, fmt.Errorf("file already exists: %s", filenameWithBasePath) } + if _, err := state.ReaderWriter().ReadFile(testFilenameWithBasePath); err == nil { + return nil, fmt.Errorf("file already exists: %s", testFilenameWithBasePath) + } + // Ensure the directory exists if err := state.ReaderWriter().MkdirAll(basePath, 0755); err != nil { return nil, fmt.Errorf("error creating directories: %w", err) } + if err := state.ReaderWriter().MkdirAll(testsBasePath, 0755); err != nil { + return nil, fmt.Errorf("error creating test directory: %w", err) + } + + // Write files err = state.ReaderWriter().WriteFile(filenameWithBasePath, []byte(fileToWrite), 0644) if err != nil { return nil, fmt.Errorf("error writing file: %w", err) } + err = state.ReaderWriter().WriteFile(testFilenameWithBasePath, []byte(testFileToWrite), 0644) + if err != nil { + return nil, fmt.Errorf("error writing test file: %w", err) + } + logger.Info(fmt.Sprintf("Generated new %s: %s at %s", templateType, name, filenameWithBasePath)) + logger.Info(fmt.Sprintf("Generated new test file: %s at %s", name, testFilenameWithBasePath)) if templateType == "contract" { - state.Contracts().AddOrUpdate(config.Contract{Name: name, Location: filenameWithBasePath}) + aliases := config.Aliases{{ + Network: "testing", + Address: flow.HexToAddress("0x0000000000000007"), + }} + + contract := config.Contract{ + Name: name, + Location: filenameWithBasePath, + Aliases: aliases, + } + state.Contracts().AddOrUpdate(contract) err = state.SaveDefault() if err != nil { return nil, fmt.Errorf("error saving to flow.json: %w", err) diff --git a/internal/super/generate_test.go b/internal/super/generate_test.go index 0a9239ce4..dfcbc310b 100644 --- a/internal/super/generate_test.go +++ b/internal/super/generate_test.go @@ -40,13 +40,33 @@ func TestGenerateNewContract(t *testing.T) { assert.NoError(t, err, "Failed to read generated file") assert.NotNil(t, fileContent) + testContent, err := state.ReaderWriter().ReadFile("cadence/tests/TestContract_test.cdc") + assert.NoError(t, err, "Failed to read generated file") + assert.NotNil(t, testContent) + // Check content is correct expectedContent := ` access(all) contract TestContract { init() {} }` + + expectedTestContent := `import Test + +access(all) let account = Test.createAccount() + +access(all) fun testContract() { + let err = Test.deployContract( + name: "TestContract", + path: "../contracts/TestContract.cdc", + arguments: [], + ) + + Test.expect(err, Test.beNil()) +}` + assert.Equal(t, expectedContent, string(fileContent)) + assert.Equal(t, expectedTestContent, string(testContent)) // Test file already exists scenario _, err = generateNew([]string{"TestContract"}, "contract", logger, state) From 4d421f8bbcb814e6e40a27537edfe32511d6fa71 Mon Sep 17 00:00:00 2001 From: Chase Fleming <1666730+chasefleming@users.noreply.github.com> Date: Tue, 12 Mar 2024 16:54:01 -0700 Subject: [PATCH 02/11] Add skip tests --- internal/super/generate.go | 44 ++++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/internal/super/generate.go b/internal/super/generate.go index 68586fd38..fae8b2b53 100644 --- a/internal/super/generate.go +++ b/internal/super/generate.go @@ -37,6 +37,7 @@ import ( type generateFlagsDef struct { Directory string `default:"" flag:"dir" info:"Directory to generate files in"` + SkipTests bool `default:"false" flag:"skip-tests" info:"Skip generating test files"` } var generateFlags = generateFlagsDef{} @@ -200,38 +201,45 @@ fun main() { return nil, fmt.Errorf("file already exists: %s", filenameWithBasePath) } - if _, err := state.ReaderWriter().ReadFile(testFilenameWithBasePath); err == nil { - return nil, fmt.Errorf("file already exists: %s", testFilenameWithBasePath) - } - // Ensure the directory exists if err := state.ReaderWriter().MkdirAll(basePath, 0755); err != nil { return nil, fmt.Errorf("error creating directories: %w", err) } - if err := state.ReaderWriter().MkdirAll(testsBasePath, 0755); err != nil { - return nil, fmt.Errorf("error creating test directory: %w", err) - } - // Write files err = state.ReaderWriter().WriteFile(filenameWithBasePath, []byte(fileToWrite), 0644) if err != nil { return nil, fmt.Errorf("error writing file: %w", err) } - err = state.ReaderWriter().WriteFile(testFilenameWithBasePath, []byte(testFileToWrite), 0644) - if err != nil { - return nil, fmt.Errorf("error writing test file: %w", err) - } - logger.Info(fmt.Sprintf("Generated new %s: %s at %s", templateType, name, filenameWithBasePath)) - logger.Info(fmt.Sprintf("Generated new test file: %s at %s", name, testFilenameWithBasePath)) + + if generateFlags.SkipTests != true { + if _, err := state.ReaderWriter().ReadFile(testFilenameWithBasePath); err == nil { + return nil, fmt.Errorf("file already exists: %s", testFilenameWithBasePath) + } + + if err := state.ReaderWriter().MkdirAll(testsBasePath, 0755); err != nil { + return nil, fmt.Errorf("error creating test directory: %w", err) + } + + err = state.ReaderWriter().WriteFile(testFilenameWithBasePath, []byte(testFileToWrite), 0644) + if err != nil { + return nil, fmt.Errorf("error writing test file: %w", err) + } + + logger.Info(fmt.Sprintf("Generated new test file: %s at %s", name, testFilenameWithBasePath)) + } if templateType == "contract" { - aliases := config.Aliases{{ - Network: "testing", - Address: flow.HexToAddress("0x0000000000000007"), - }} + var aliases config.Aliases + + if generateFlags.SkipTests != true { + aliases = config.Aliases{{ + Network: "testing", + Address: flow.HexToAddress("0x0000000000000007"), + }} + } contract := config.Contract{ Name: name, From 07353bd6606366f929c27b6071fe514ee1dabff9 Mon Sep 17 00:00:00 2001 From: Chase Fleming <1666730+chasefleming@users.noreply.github.com> Date: Tue, 12 Mar 2024 16:57:38 -0700 Subject: [PATCH 03/11] Change import name --- internal/super/generate.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/super/generate.go b/internal/super/generate.go index fae8b2b53..03ad1f669 100644 --- a/internal/super/generate.go +++ b/internal/super/generate.go @@ -20,7 +20,7 @@ package super import ( "fmt" - "github.com/onflow/flow-go-sdk" + flowsdk "github.com/onflow/flow-go-sdk" "path/filepath" "strings" @@ -237,7 +237,7 @@ fun main() { if generateFlags.SkipTests != true { aliases = config.Aliases{{ Network: "testing", - Address: flow.HexToAddress("0x0000000000000007"), + Address: flowsdk.HexToAddress("0x0000000000000007"), }} } From 9caa29adde4efafaed781a87d3f3c3c3a3b166a8 Mon Sep 17 00:00:00 2001 From: Chase Fleming <1666730+chasefleming@users.noreply.github.com> Date: Tue, 12 Mar 2024 17:09:00 -0700 Subject: [PATCH 04/11] Run goimports --- internal/super/generate.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/internal/super/generate.go b/internal/super/generate.go index 03ad1f669..421c2d5c9 100644 --- a/internal/super/generate.go +++ b/internal/super/generate.go @@ -20,10 +20,11 @@ package super import ( "fmt" - flowsdk "github.com/onflow/flow-go-sdk" "path/filepath" "strings" + flowsdk "github.com/onflow/flow-go-sdk" + "github.com/onflow/flowkit/config" "github.com/onflow/flowkit" From 385493498087c1fd99cd2baaea29de87c1103c19 Mon Sep 17 00:00:00 2001 From: Chase Fleming <1666730+chasefleming@users.noreply.github.com> Date: Wed, 13 Mar 2024 10:29:43 -0700 Subject: [PATCH 05/11] Add skip tests flag test --- internal/super/generate_test.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/internal/super/generate_test.go b/internal/super/generate_test.go index dfcbc310b..4f2c47810 100644 --- a/internal/super/generate_test.go +++ b/internal/super/generate_test.go @@ -74,6 +74,25 @@ access(all) fun testContract() { assert.Equal(t, "file already exists: cadence/contracts/TestContract.cdc", err.Error()) } +func TestGenerateNewContractSkipTests(t *testing.T) { + logger := output.NewStdoutLogger(output.NoneLog) + _, state, _ := util.TestMocks(t) + + generateFlags.SkipTests = true + + // Test contract generation + _, err := generateNew([]string{"TestContract"}, "contract", logger, state) + assert.NoError(t, err, "Failed to generate contract") + + fileContent, err := state.ReaderWriter().ReadFile("cadence/contracts/TestContract.cdc") + assert.NoError(t, err, "Failed to read generated file") + assert.NotNil(t, fileContent) + + testContent, err := state.ReaderWriter().ReadFile("cadence/tests/TestContract_test.cdc") + assert.Error(t, err, "Failed to read generated file") + assert.Nil(t, testContent) +} + func TestGenerateNewContractFileAlreadyExists(t *testing.T) { logger := output.NewStdoutLogger(output.NoneLog) _, state, _ := util.TestMocks(t) From 9c3b72c5bd506e1bad50695e8502bce7fe8d1ee8 Mon Sep 17 00:00:00 2001 From: Chase Fleming Date: Wed, 13 Mar 2024 10:30:41 -0700 Subject: [PATCH 06/11] Update internal/super/generate.go Co-authored-by: Jordan Ribbink --- internal/super/generate.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/super/generate.go b/internal/super/generate.go index 421c2d5c9..96c34832f 100644 --- a/internal/super/generate.go +++ b/internal/super/generate.go @@ -215,7 +215,7 @@ fun main() { logger.Info(fmt.Sprintf("Generated new %s: %s at %s", templateType, name, filenameWithBasePath)) - if generateFlags.SkipTests != true { + if generateFlags.SkipTests != true && templateType == "contract" { if _, err := state.ReaderWriter().ReadFile(testFilenameWithBasePath); err == nil { return nil, fmt.Errorf("file already exists: %s", testFilenameWithBasePath) } From 9978cc3b30501a64e4888ca9c23ff97cd28614fd Mon Sep 17 00:00:00 2001 From: Chase Fleming Date: Wed, 13 Mar 2024 10:31:13 -0700 Subject: [PATCH 07/11] Update internal/super/generate.go Co-authored-by: Jordan Ribbink --- internal/super/generate.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/super/generate.go b/internal/super/generate.go index 96c34832f..e33c8bbf9 100644 --- a/internal/super/generate.go +++ b/internal/super/generate.go @@ -237,7 +237,7 @@ fun main() { if generateFlags.SkipTests != true { aliases = config.Aliases{{ - Network: "testing", + Network: config.TestingNetwork.Name, Address: flowsdk.HexToAddress("0x0000000000000007"), }} } From e29d00eba0984b435c3d84d27f6d05365fb8e85d Mon Sep 17 00:00:00 2001 From: Chase Fleming <1666730+chasefleming@users.noreply.github.com> Date: Wed, 13 Mar 2024 10:41:26 -0700 Subject: [PATCH 08/11] Remove line break from boilerplate contract --- internal/super/generate.go | 3 +-- internal/super/generate_test.go | 6 ++---- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/internal/super/generate.go b/internal/super/generate.go index e33c8bbf9..14aa4ee4a 100644 --- a/internal/super/generate.go +++ b/internal/super/generate.go @@ -161,8 +161,7 @@ func generateNew( switch templateType { case "contract": - fileToWrite = fmt.Sprintf(` -access(all) + fileToWrite = fmt.Sprintf(`access(all) contract %s { init() {} }`, name) diff --git a/internal/super/generate_test.go b/internal/super/generate_test.go index 4f2c47810..956de32b7 100644 --- a/internal/super/generate_test.go +++ b/internal/super/generate_test.go @@ -45,8 +45,7 @@ func TestGenerateNewContract(t *testing.T) { assert.NotNil(t, testContent) // Check content is correct - expectedContent := ` -access(all) + expectedContent := `access(all) contract TestContract { init() {} }` @@ -176,8 +175,7 @@ func TestGenerateNewWithDirFlag(t *testing.T) { assert.NoError(t, err, "Failed to read generated file") assert.NotNil(t, content) - expectedContent := ` -access(all) + expectedContent := `access(all) contract TestContract { init() {} }` From 63106c59127feaab3b9b77648bfe8e1bf5a794da Mon Sep 17 00:00:00 2001 From: Chase Fleming <1666730+chasefleming@users.noreply.github.com> Date: Wed, 13 Mar 2024 14:21:17 -0700 Subject: [PATCH 09/11] Add `stripCDCExtension` --- internal/super/generate.go | 8 ++++++-- internal/super/generate_test.go | 17 +++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/internal/super/generate.go b/internal/super/generate.go index 14aa4ee4a..7f2d0ba07 100644 --- a/internal/super/generate.go +++ b/internal/super/generate.go @@ -126,6 +126,10 @@ func addCDCExtension(name string) string { return fmt.Sprintf("%s.cdc", name) } +func stripCDCExtension(name string) string { + return strings.TrimSuffix(name, filepath.Ext(name)) +} + func generateNew( args []string, templateType string, @@ -136,8 +140,8 @@ func generateNew( return nil, fmt.Errorf("invalid number of arguments") } - name := args[0] - var filename = addCDCExtension(name) + name := stripCDCExtension(args[0]) + filename := addCDCExtension(name) var fileToWrite string var testFileToWrite string diff --git a/internal/super/generate_test.go b/internal/super/generate_test.go index 956de32b7..13e0c8884 100644 --- a/internal/super/generate_test.go +++ b/internal/super/generate_test.go @@ -92,6 +92,23 @@ func TestGenerateNewContractSkipTests(t *testing.T) { assert.Nil(t, testContent) } +func TestGenerateNewContractWithCDCExtension(t *testing.T) { + logger := output.NewStdoutLogger(output.NoneLog) + _, state, _ := util.TestMocks(t) + + // Test contract generation + _, err := generateNew([]string{"Tester.cdc"}, "contract", logger, state) + assert.NoError(t, err, "Failed to generate contract") + + fileContent, err := state.ReaderWriter().ReadFile("cadence/contracts/Tester.cdc") + assert.NoError(t, err, "Failed to read generated file") + assert.NotNil(t, fileContent) + + testContent, err := state.ReaderWriter().ReadFile("cadence/tests/Tester_test.cdc") + assert.NoError(t, err, "Failed to read generated file") + assert.NotNil(t, testContent) +} + func TestGenerateNewContractFileAlreadyExists(t *testing.T) { logger := output.NewStdoutLogger(output.NoneLog) _, state, _ := util.TestMocks(t) From 4a6cd2c09c31786f9ce77bf7555c454abf9219b5 Mon Sep 17 00:00:00 2001 From: Chase Fleming <1666730+chasefleming@users.noreply.github.com> Date: Wed, 13 Mar 2024 14:24:46 -0700 Subject: [PATCH 10/11] Move test variable instantiation --- internal/super/generate.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/internal/super/generate.go b/internal/super/generate.go index 7f2d0ba07..e2a916562 100644 --- a/internal/super/generate.go +++ b/internal/super/generate.go @@ -146,7 +146,6 @@ func generateNew( var fileToWrite string var testFileToWrite string var basePath string - var testsBasePath = "cadence/tests" if generateFlags.Directory != "" { basePath = generateFlags.Directory @@ -198,7 +197,6 @@ fun main() { } filenameWithBasePath := filepath.Join(basePath, filename) - testFilenameWithBasePath := filepath.Join(testsBasePath, addCDCExtension(fmt.Sprintf("%s_test", name))) // Check file existence if _, err := state.ReaderWriter().ReadFile(filenameWithBasePath); err == nil { @@ -219,6 +217,9 @@ fun main() { logger.Info(fmt.Sprintf("Generated new %s: %s at %s", templateType, name, filenameWithBasePath)) if generateFlags.SkipTests != true && templateType == "contract" { + testsBasePath := "cadence/tests" + testFilenameWithBasePath := filepath.Join(testsBasePath, addCDCExtension(fmt.Sprintf("%s_test", name))) + if _, err := state.ReaderWriter().ReadFile(testFilenameWithBasePath); err == nil { return nil, fmt.Errorf("file already exists: %s", testFilenameWithBasePath) } From 1b169485834502f206249722b6fe8b7a75c69d83 Mon Sep 17 00:00:00 2001 From: Chase Fleming <1666730+chasefleming@users.noreply.github.com> Date: Wed, 13 Mar 2024 14:49:14 -0700 Subject: [PATCH 11/11] Cleanup flag --- internal/super/generate_test.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/internal/super/generate_test.go b/internal/super/generate_test.go index 13e0c8884..8a7a37b12 100644 --- a/internal/super/generate_test.go +++ b/internal/super/generate_test.go @@ -79,6 +79,10 @@ func TestGenerateNewContractSkipTests(t *testing.T) { generateFlags.SkipTests = true + t.Cleanup(func() { + generateFlags.SkipTests = false + }) + // Test contract generation _, err := generateNew([]string{"TestContract"}, "contract", logger, state) assert.NoError(t, err, "Failed to generate contract")