Skip to content

Commit

Permalink
Merge pull request #1354 from onflow/coverage-report-map-contract-files
Browse files Browse the repository at this point in the history
Coverage report map contract files
  • Loading branch information
chasefleming authored Jan 26, 2024
2 parents 4c90a9e + c7eadf0 commit f634cc1
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
18 changes: 17 additions & 1 deletion flowkit/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"os"
"path/filepath"

"github.com/onflow/cadence/runtime"
"github.com/onflow/flow-go-sdk/crypto"
"github.com/pkg/errors"
"golang.org/x/exp/slices"
Expand All @@ -49,6 +50,22 @@ type State struct {
accounts *accounts.Accounts
}

func (p *State) CreateCoverageReport(network string) *runtime.CoverageReport {
coverageReport := runtime.NewCoverageReport()
contractsConfig := *p.Contracts()
locationMappings := make(map[string]string, len(contractsConfig))
for _, contract := range contractsConfig {
alias := contract.Aliases.ByNetwork(network)
if alias != nil {
locationMappings[contract.Name] = contract.Location
}
}

coverageReport.WithLocationMappings(locationMappings)

return coverageReport
}

// ReaderWriter retrieve current file reader writer.
func (p *State) ReaderWriter() ReaderWriter {
return p.readerWriter
Expand Down Expand Up @@ -87,7 +104,6 @@ func (p *State) SaveEdited(paths []string) error {
func (p *State) Save(path string) error {
p.conf.Accounts = accounts.ToConfig(*p.accounts)
err := p.confLoader.Save(p.conf, path)

if err != nil {
return fmt.Errorf("failed to save project configuration to: %s", path)
}
Expand Down
9 changes: 4 additions & 5 deletions internal/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ func run(
testFiles := make(map[string][]byte, 0)
for _, filename := range args {
code, err := state.ReadFile(filename)

if err != nil {
return nil, fmt.Errorf("error loading script file: %w", err)
}
Expand Down Expand Up @@ -153,7 +152,7 @@ func testCode(

var coverageReport *runtime.CoverageReport
if flags.Cover {
coverageReport = runtime.NewCoverageReport()
coverageReport = state.CreateCoverageReport("testing")
if flags.CoverCode == contractsCoverCode {
coverageReport.WithLocationFilter(
func(location common.Location) bool {
Expand All @@ -177,8 +176,9 @@ func testCode(
runner = runner.WithRandomSeed(seed)
}

contracts := make(map[string]common.Address, 0)
for _, contract := range *state.Contracts() {
contractsConfig := *state.Contracts()
contracts := make(map[string]common.Address, len(contractsConfig))
for _, contract := range contractsConfig {
alias := contract.Aliases.ByNetwork("testing")
if alias != nil {
contracts[contract.Name] = common.Address(alias.Address)
Expand Down Expand Up @@ -239,7 +239,6 @@ func importResolver(scriptPath string, state *flowkit.State) cdcTests.ImportReso
}

return func(location common.Location) (string, error) {

contract := config.Contract{}

switch location := location.(type) {
Expand Down
12 changes: 12 additions & 0 deletions internal/test/test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,12 @@ func TestExecutingTests(t *testing.T) {
result.String(),
"Coverage: 91.8% of statements",
)

lcovReport, _ := coverageReport.MarshalLCOV()
assert.Contains(t, string(lcovReport), "TN:\nSF:FooContract.cdc\n")

jsonReport, _ := coverageReport.MarshalJSON()
assert.Contains(t, string(jsonReport), `{"coverage":{"FooContract.cdc":{`)
})

t.Run("with code coverage for contracts only", func(t *testing.T) {
Expand Down Expand Up @@ -447,6 +453,12 @@ func TestExecutingTests(t *testing.T) {
result.String(),
"Coverage: 100.0% of statements",
)

lcovReport, _ := coverageReport.MarshalLCOV()
assert.Contains(t, string(lcovReport), "TN:\nSF:FooContract.cdc\n")

jsonReport, _ := coverageReport.MarshalJSON()
assert.Contains(t, string(jsonReport), `{"coverage":{"FooContract.cdc":{`)
})

t.Run("with random test case execution", func(t *testing.T) {
Expand Down

0 comments on commit f634cc1

Please sign in to comment.