Skip to content

Commit

Permalink
Merge pull request #1581 from onflow/cf/merge-master-5-6
Browse files Browse the repository at this point in the history
Merge master into feature branch
  • Loading branch information
chasefleming authored May 6, 2024
2 parents 07d288b + ca6fce3 commit 3a4f939
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ jobs:
go-version: "1.20"
- name: generate
run: make generate
- uses: golangci/golangci-lint-action@v5.1.0
- uses: golangci/golangci-lint-action@v5.3.0
with:
version: v1.52.2
only-new-issues: true
Expand Down
13 changes: 9 additions & 4 deletions internal/super/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import (
"strings"
"syscall"

flowGo "github.com/onflow/flow-go/model/flow"

"github.com/onflow/cadence/runtime/parser"
"github.com/onflow/flow-emulator/server"
"github.com/onflow/flowkit"
Expand Down Expand Up @@ -82,10 +84,13 @@ func dev(
log := zerolog.New(consoleWriter).With().Timestamp().Logger()

emulatorServer := server.NewEmulatorServer(&log, &server.Config{
ServicePublicKey: (*privateKey).PublicKey(),
ServicePrivateKey: *privateKey,
ServiceKeySigAlgo: serviceAccount.Key.SigAlgo(),
ServiceKeyHashAlgo: serviceAccount.Key.HashAlgo(),
ChainID: flowGo.Emulator,
ServicePublicKey: (*privateKey).PublicKey(),
ServicePrivateKey: *privateKey,
ServiceKeySigAlgo: serviceAccount.Key.SigAlgo(),
ServiceKeyHashAlgo: serviceAccount.Key.HashAlgo(),
ScriptGasLimit: 100000,
TransactionMaxGasLimit: 9999,
})

emuErr := emulatorServer.Listen()
Expand Down
69 changes: 56 additions & 13 deletions internal/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ const helperScriptSubstr = "_helper"
// scripts and transactions are excluded from coverage report.
const contractsCoverCode = "contracts"

// The default glob pattern to find test files.
const defaultTestSuffix = "_test.cdc"

type flagsTests struct {
Cover bool `default:"false" flag:"cover" info:"Use the cover flag to calculate coverage report"`
CoverProfile string `default:"coverage.json" flag:"coverprofile" info:"Filename to write the calculated coverage report. Supported extensions are .json and .lcov"`
Expand All @@ -74,10 +77,14 @@ var testFlags = flagsTests{}

var TestCommand = &command.Command{
Cmd: &cobra.Command{
Use: "test <filename>",
Short: "Run Cadence tests",
Example: `flow test script.cdc`,
Args: cobra.MinimumNArgs(1),
Use: "test [files...]",
Short: "Run Cadence tests",
Example: `# Run tests in files matching default pattern **/*_test.cdc
flow test
# Run tests in the specified files
flow test test1.cdc test2.cdc`,
Args: cobra.ArbitraryArgs,
GroupID: "tools",
},
Flags: &testFlags,
Expand All @@ -101,8 +108,19 @@ func run(
)
}

var filenames []string
if len(args) == 0 {
var err error
filenames, err = findAllTestFiles(".")
if err != nil {
return nil, fmt.Errorf("error loading script files: %w", err)
}
} else {
filenames = args
}

testFiles := make(map[string][]byte, 0)
for _, filename := range args {
for _, filename := range filenames {
code, err := state.ReadFile(filename)
if err != nil {
return nil, fmt.Errorf("error loading script file: %w", err)
Expand Down Expand Up @@ -341,14 +359,18 @@ func (r *result) String() string {
var b bytes.Buffer
writer := util.CreateTabWriter(&b)

for scriptPath, testResult := range r.Results {
_, _ = fmt.Fprint(writer, cdcTests.PrettyPrintResults(testResult, scriptPath))
}
if r.CoverageReport != nil {
_, _ = fmt.Fprint(writer, r.CoverageReport.String())
}
if r.RandomSeed > 0 {
_, _ = fmt.Fprintf(writer, "\nSeed: %d", r.RandomSeed)
if len(r.Results) == 0 {
_, _ = fmt.Fprint(writer, "No tests found")
} else {
for scriptPath, testResult := range r.Results {
_, _ = fmt.Fprint(writer, cdcTests.PrettyPrintResults(testResult, scriptPath))
}
if r.CoverageReport != nil {
_, _ = fmt.Fprint(writer, r.CoverageReport.String())
}
if r.RandomSeed > 0 {
_, _ = fmt.Fprintf(writer, "\nSeed: %d", r.RandomSeed)
}
}

_ = writer.Flush()
Expand Down Expand Up @@ -382,3 +404,24 @@ func (r *result) Oneliner() string {
func (r *result) ExitCode() int {
return r.exitCode
}

func findAllTestFiles(baseDir string) ([]string, error) {
var filenames []string
err := filepath.Walk(baseDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}

if !strings.HasSuffix(path, defaultTestSuffix) {
return nil
}

filenames = append(filenames, path)
return nil
})
if err != nil {
return nil, err
}

return filenames, nil
}

0 comments on commit 3a4f939

Please sign in to comment.