Skip to content

Commit

Permalink
Merge pull request #1391 from onflow/chasefleming/remove-os-dir
Browse files Browse the repository at this point in the history
Remove os dir code in `generate` tests
  • Loading branch information
chasefleming authored Feb 7, 2024
2 parents 49d80e4 + a316956 commit ac54f1c
Showing 1 changed file with 6 additions and 55 deletions.
61 changes: 6 additions & 55 deletions internal/super/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package super

import (
"os"
"testing"

"github.com/onflow/flow-cli/internal/util"
Expand All @@ -30,19 +29,11 @@ import (
)

func TestGenerateNewContract(t *testing.T) {
dir, err := os.MkdirTemp("", "test")
assert.NoError(t, err, "Failed to create temp dir")
defer os.RemoveAll(dir)

if err := os.Chdir(dir); err != nil {
t.Fatalf("Failed to change directory: %v", err)
}

logger := output.NewStdoutLogger(output.NoneLog)
_, state, _ := util.TestMocks(t)

// Test contract generation
_, err = generateNew([]string{"TestContract"}, "contract", logger, state)
_, err := generateNew([]string{"TestContract"}, "contract", logger, state)
assert.NoError(t, err, "Failed to generate contract")

fileContent, err := state.ReaderWriter().ReadFile("cadence/contracts/TestContract.cdc")
Expand All @@ -64,19 +55,11 @@ contract TestContract {
}

func TestGenerateNewContractFileAlreadyExists(t *testing.T) {
dir, err := os.MkdirTemp("", "test")
assert.NoError(t, err, "Failed to create temp dir")
defer os.RemoveAll(dir)

if err := os.Chdir(dir); err != nil {
t.Fatalf("Failed to change directory: %v", err)
}

logger := output.NewStdoutLogger(output.NoneLog)
_, state, _ := util.TestMocks(t)

// Test contract generation
_, err = generateNew([]string{"TestContract"}, "contract", logger, state)
_, err := generateNew([]string{"TestContract"}, "contract", logger, state)
assert.NoError(t, err, "Failed to generate contract")

//// Check if the file exists in the correct directory
Expand All @@ -91,18 +74,10 @@ func TestGenerateNewContractFileAlreadyExists(t *testing.T) {
}

func TestGenerateNewContractWithFileExtension(t *testing.T) {
dir, err := os.MkdirTemp("", "test")
assert.NoError(t, err, "Failed to create temp dir")
defer os.RemoveAll(dir)

if err := os.Chdir(dir); err != nil {
t.Fatalf("Failed to change directory: %v", err)
}

logger := output.NewStdoutLogger(output.NoneLog)
_, state, _ := util.TestMocks(t)

_, err = generateNew([]string{"TestContract.cdc"}, "contract", logger, state)
_, err := generateNew([]string{"TestContract.cdc"}, "contract", logger, state)
assert.NoError(t, err, "Failed to generate contract")

// Check file exists
Expand All @@ -112,18 +87,10 @@ func TestGenerateNewContractWithFileExtension(t *testing.T) {
}

func TestGenerateNewScript(t *testing.T) {
dir, err := os.MkdirTemp("", "test")
assert.NoError(t, err, "Failed to create temp dir")
defer os.RemoveAll(dir)

if err := os.Chdir(dir); err != nil {
t.Fatalf("Failed to change directory: %v", err)
}

logger := output.NewStdoutLogger(output.NoneLog)
_, state, _ := util.TestMocks(t)

_, err = generateNew([]string{"TestScript"}, "script", logger, state)
_, err := generateNew([]string{"TestScript"}, "script", logger, state)
assert.NoError(t, err, "Failed to generate contract")

content, err := state.ReaderWriter().ReadFile("cadence/scripts/TestScript.cdc")
Expand All @@ -138,18 +105,10 @@ fun main() {
}

func TestGenerateNewTransaction(t *testing.T) {
dir, err := os.MkdirTemp("", "test")
assert.NoError(t, err, "Failed to create temp dir")
defer os.RemoveAll(dir)

if err := os.Chdir(dir); err != nil {
t.Fatalf("Failed to change directory: %v", err)
}

logger := output.NewStdoutLogger(output.NoneLog)
_, state, _ := util.TestMocks(t)

_, err = generateNew([]string{"TestTransaction"}, "transaction", logger, state)
_, err := generateNew([]string{"TestTransaction"}, "transaction", logger, state)
assert.NoError(t, err, "Failed to generate contract")

content, err := state.ReaderWriter().ReadFile("cadence/transactions/TestTransaction.cdc")
Expand All @@ -165,21 +124,13 @@ func TestGenerateNewTransaction(t *testing.T) {
}

func TestGenerateNewWithDirFlag(t *testing.T) {
dir, err := os.MkdirTemp("", "test")
assert.NoError(t, err, "Failed to create temp dir")
defer os.RemoveAll(dir)

if err := os.Chdir(dir); err != nil {
t.Fatalf("Failed to change directory: %v", err)
}

logger := output.NewStdoutLogger(output.NoneLog)
_, state, _ := util.TestMocks(t)

// Set a custom directory
generateFlags.Directory = "customDir"

_, err = generateNew([]string{"TestContract"}, "contract", logger, state)
_, err := generateNew([]string{"TestContract"}, "contract", logger, state)
assert.NoError(t, err, "Failed to generate contract")

content, err := state.ReaderWriter().ReadFile("customDir/TestContract.cdc")
Expand Down

0 comments on commit ac54f1c

Please sign in to comment.