Skip to content

Commit

Permalink
Merge pull request #1648 from onflow/cf/fix-counter-path
Browse files Browse the repository at this point in the history
Fix counter path from `init`
  • Loading branch information
chasefleming authored Jun 11, 2024
2 parents 7fb7ec7 + f6eaffe commit ca4e1b5
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 32 deletions.
10 changes: 5 additions & 5 deletions internal/super/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,9 +283,9 @@ func (g *Generator) generate(templateType, templateName, name, account string, d

var fileToWrite string
var testFileToWrite string
var rootDir = DefaultCadenceDirectory
var rootDir string
var basePath string
var testsBasePath = "tests"
var testsBasePath = filepath.Join(DefaultCadenceDirectory, "tests")
var err error

if g.directory != "" {
Expand All @@ -296,7 +296,7 @@ func (g *Generator) generate(templateType, templateName, name, account string, d

switch templateType {
case ContractType:
basePath = "contracts"
basePath = filepath.Join(DefaultCadenceDirectory, "contracts")
fileData := map[string]interface{}{"Name": name}
for k, v := range data {
fileData[k] = v
Expand All @@ -311,7 +311,7 @@ func (g *Generator) generate(templateType, templateName, name, account string, d
return fmt.Errorf("error generating contract test template: %w", err)
}
case ScriptType:
basePath = "scripts"
basePath = filepath.Join(DefaultCadenceDirectory, "scripts")
fileData := map[string]interface{}{}
for k, v := range data {
fileData[k] = v
Expand All @@ -321,7 +321,7 @@ func (g *Generator) generate(templateType, templateName, name, account string, d
return fmt.Errorf("error generating script template: %w", err)
}
case TransactionType:
basePath = "transactions"
basePath = filepath.Join(DefaultCadenceDirectory, "transactions")
fileData := map[string]interface{}{}
for k, v := range data {
fileData[k] = v
Expand Down
48 changes: 24 additions & 24 deletions internal/super/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,17 @@ func TestGenerateNewContract(t *testing.T) {
logger := output.NewStdoutLogger(output.NoneLog)
_, state, _ := util.TestMocks(t)

generator := NewGenerator("cadence", state, logger, false, true)
generator := NewGenerator("", state, logger, false, true)

// Test contract generation
err := generator.Create(TemplateMap{"contract": []TemplateItem{Contract{Name: "TestContract", Account: ""}}})
assert.NoError(t, err, "Failed to generate contract")

fileContent, err := state.ReaderWriter().ReadFile("cadence/contracts/TestContract.cdc")
fileContent, err := state.ReaderWriter().ReadFile(filepath.FromSlash("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")
testContent, err := state.ReaderWriter().ReadFile(filepath.FromSlash("cadence/tests/TestContract_test.cdc"))
assert.NoError(t, err, "Failed to read generated file")
assert.NotNil(t, testContent)

Expand Down Expand Up @@ -72,7 +72,7 @@ access(all) fun testContract() {
assert.Equal(t, expectedTestContent, util.NormalizeLineEndings(string(testContent)))

// Test file already exists scenario
generatorTwo := NewGenerator("cadence", state, logger, false, true)
generatorTwo := NewGenerator("", state, logger, false, true)
err = generatorTwo.Create(TemplateMap{"contract": []TemplateItem{Contract{Name: "TestContract", Account: ""}}})
assert.Error(t, err)
expectedError := fmt.Sprintf("file already exists: %s", filepath.FromSlash("cadence/contracts/TestContract.cdc"))
Expand All @@ -83,17 +83,17 @@ func TestGenerateContractWithAccount(t *testing.T) {
logger := output.NewStdoutLogger(output.NoneLog)
_, state, _ := util.TestMocks(t)

generator := NewGenerator("cadence", state, logger, false, true)
generator := NewGenerator("", state, logger, false, true)

// Test contract generation
err := generator.Create(TemplateMap{"contract": []TemplateItem{Contract{Name: "TestContract", Account: "example-account"}}})
assert.NoError(t, err, "Failed to generate contract")

fileContent, err := state.ReaderWriter().ReadFile("cadence/contracts/example-account/TestContract.cdc")
fileContent, err := state.ReaderWriter().ReadFile(filepath.FromSlash("cadence/contracts/example-account/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")
testContent, err := state.ReaderWriter().ReadFile(filepath.FromSlash("cadence/tests/TestContract_test.cdc"))
assert.NoError(t, err, "Failed to read generated file")
assert.NotNil(t, testContent)
}
Expand All @@ -104,7 +104,7 @@ func TestGenerateNewContractSkipTests(t *testing.T) {

generateFlags.SkipTests = true

generator := NewGenerator("cadence", state, logger, false, true)
generator := NewGenerator("", state, logger, false, true)
t.Cleanup(func() {
generateFlags.SkipTests = false
})
Expand All @@ -113,11 +113,11 @@ func TestGenerateNewContractSkipTests(t *testing.T) {
err := generator.Create(TemplateMap{"contract": []TemplateItem{Contract{Name: "TestContract", Account: ""}}})
assert.NoError(t, err, "Failed to generate contract")

fileContent, err := state.ReaderWriter().ReadFile("cadence/contracts/TestContract.cdc")
fileContent, err := state.ReaderWriter().ReadFile(filepath.FromSlash("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")
testContent, err := state.ReaderWriter().ReadFile(filepath.FromSlash("cadence/tests/TestContract_test.cdc"))
assert.Error(t, err, "Failed to read generated file")
assert.Nil(t, testContent)
}
Expand All @@ -127,15 +127,15 @@ func TestGenerateNewContractWithCDCExtension(t *testing.T) {
_, state, _ := util.TestMocks(t)

// Test contract generation
generator := NewGenerator("cadence", state, logger, false, true)
generator := NewGenerator("", state, logger, false, true)
err := generator.Create(TemplateMap{"contract": []TemplateItem{Contract{Name: "Tester.cdc", Account: ""}}})
assert.NoError(t, err, "Failed to generate contract")

fileContent, err := state.ReaderWriter().ReadFile("cadence/contracts/Tester.cdc")
fileContent, err := state.ReaderWriter().ReadFile(filepath.FromSlash("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")
testContent, err := state.ReaderWriter().ReadFile(filepath.FromSlash("cadence/tests/Tester_test.cdc"))
assert.NoError(t, err, "Failed to read generated file")
assert.NotNil(t, testContent)
}
Expand All @@ -145,17 +145,17 @@ func TestGenerateNewContractFileAlreadyExists(t *testing.T) {
_, state, _ := util.TestMocks(t)

// Test contract generation
generator := NewGenerator("cadence", state, logger, false, true)
generator := NewGenerator("", state, logger, false, true)
err := generator.Create(TemplateMap{"contract": []TemplateItem{Contract{Name: "TestContract", Account: ""}}})
assert.NoError(t, err, "Failed to generate contract")

//// Check if the file exists in the correct directory
content, err := state.ReaderWriter().ReadFile("cadence/contracts/TestContract.cdc")
content, err := state.ReaderWriter().ReadFile(filepath.FromSlash("cadence/contracts/TestContract.cdc"))
assert.NoError(t, err, "Failed to read generated file")
assert.NotNil(t, content)

// Test file already exists scenario
generatorTwo := NewGenerator("cadence", state, logger, false, true)
generatorTwo := NewGenerator("", state, logger, false, true)
err = generatorTwo.Create(TemplateMap{"contract": []TemplateItem{Contract{Name: "TestContract", Account: ""}}})
assert.Error(t, err)
expectedError := fmt.Sprintf("file already exists: %s", filepath.FromSlash("cadence/contracts/TestContract.cdc"))
Expand All @@ -166,12 +166,12 @@ func TestGenerateNewContractWithFileExtension(t *testing.T) {
logger := output.NewStdoutLogger(output.NoneLog)
_, state, _ := util.TestMocks(t)

generator := NewGenerator("cadence", state, logger, false, true)
generator := NewGenerator("", state, logger, false, true)
err := generator.Create(TemplateMap{"contract": []TemplateItem{Contract{Name: "TestContract.cdc", Account: ""}}})
assert.NoError(t, err, "Failed to generate contract")

// Check file exists
content, err := state.ReaderWriter().ReadFile("cadence/contracts/TestContract.cdc")
content, err := state.ReaderWriter().ReadFile(filepath.FromSlash("cadence/contracts/TestContract.cdc"))
assert.NoError(t, err, "Failed to read generated file")
assert.NotNil(t, content)
}
Expand All @@ -180,11 +180,11 @@ func TestGenerateNewScript(t *testing.T) {
logger := output.NewStdoutLogger(output.NoneLog)
_, state, _ := util.TestMocks(t)

generator := NewGenerator("cadence", state, logger, false, true)
generator := NewGenerator("", state, logger, false, true)
err := generator.Create(TemplateMap{"script": []TemplateItem{ScriptTemplate{Name: "TestScript"}}})
assert.NoError(t, err, "Failed to generate contract")

content, err := state.ReaderWriter().ReadFile("cadence/scripts/TestScript.cdc")
content, err := state.ReaderWriter().ReadFile(filepath.FromSlash("cadence/scripts/TestScript.cdc"))
assert.NoError(t, err, "Failed to read generated file")
assert.NotNil(t, content)

Expand All @@ -199,11 +199,11 @@ func TestGenerateNewTransaction(t *testing.T) {
logger := output.NewStdoutLogger(output.NoneLog)
_, state, _ := util.TestMocks(t)

generator := NewGenerator("cadence", state, logger, false, true)
generator := NewGenerator("", state, logger, false, true)
err := generator.Create(TemplateMap{"transaction": []TemplateItem{TransactionTemplate{Name: "TestTransaction"}}})
assert.NoError(t, err, "Failed to generate contract")

content, err := state.ReaderWriter().ReadFile("cadence/transactions/TestTransaction.cdc")
content, err := state.ReaderWriter().ReadFile(filepath.FromSlash("cadence/transactions/TestTransaction.cdc"))
assert.NoError(t, err, "Failed to read generated file")
assert.NotNil(t, content)

Expand All @@ -223,11 +223,11 @@ func TestGenerateNewWithDirFlag(t *testing.T) {
err := generator.Create(TemplateMap{"contract": []TemplateItem{Contract{Name: "TestContract", Account: ""}}})
assert.NoError(t, err, "Failed to generate contract")

content, err := state.ReaderWriter().ReadFile("customDir/contracts/TestContract.cdc")
content, err := state.ReaderWriter().ReadFile(filepath.FromSlash("customDir/cadence/contracts/TestContract.cdc"))
assert.NoError(t, err, "Failed to read generated file")
assert.NotNil(t, content)

testContent, err := state.ReaderWriter().ReadFile("customDir/tests/TestContract_test.cdc")
testContent, err := state.ReaderWriter().ReadFile(filepath.FromSlash("customDir/cadence/tests/TestContract_test.cdc"))
assert.NoError(t, err, "Failed to read generated file")
assert.NotNil(t, testContent)

Expand Down
4 changes: 1 addition & 3 deletions internal/super/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,6 @@ func startInteractiveSetup(
// cadence/transactions/DefaultTransaction.cdc
// cadence/tests/DefaultContract_test.cdc

directoryPath := filepath.Join(tempDir, "cadence")

templates := TemplateMap{
"contract": []TemplateItem{
Contract{
Expand All @@ -218,7 +216,7 @@ func startInteractiveSetup(
},
}

generator := NewGenerator(directoryPath, state, logger, true, false)
generator := NewGenerator(tempDir, state, logger, true, false)
err = generator.Create(templates)
if err != nil {
return "", err
Expand Down

0 comments on commit ca4e1b5

Please sign in to comment.