Skip to content

Commit

Permalink
Fix error handling for missing contracts in config file
Browse files Browse the repository at this point in the history
  • Loading branch information
m-Peter committed Oct 8, 2023
1 parent f0c78cb commit cd51049
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 7 deletions.
17 changes: 17 additions & 0 deletions flowkit/tests/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,23 @@ var TestScriptWithImport = Resource{
`),
}

var TestScriptWithMissingContract = Resource{
Filename: "testScriptWithImport.cdc",
Source: []byte(`
import Test
import "ApprovalVoting"
pub fun setup() {
let err = Test.deployContract(
name: "ApprovalVoting",
path: "ApprovalVoting.cdc",
arguments: []
)
Test.expect(err, Test.beNil())
}
`),
}

var TestScriptWithHelperImport = Resource{
Filename: "testScriptWithHelperImport.cdc",
Source: []byte(`
Expand Down
6 changes: 4 additions & 2 deletions internal/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,10 +229,12 @@ func importResolver(scriptPath string, state *flowkit.State) cdcTests.ImportReso
}
}

default:
}

if contract == nil {
return "", fmt.Errorf(
"cannot find contract with location '%s' in configuration",
scriptPath,
location,
)
}

Expand Down
34 changes: 29 additions & 5 deletions internal/test/test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,16 +160,40 @@ func TestExecutingTests(t *testing.T) {
assert.NoError(t, result.Results[script.Filename][0].Error)
})

t.Run("with missing contract location from config", func(t *testing.T) {
t.Run("with missing contract in config", func(t *testing.T) {
t.Parallel()

// Setup
_, state, _ := util.TestMocks(t)

// Execute script
script := tests.TestScriptWithMissingContract
testFiles := map[string][]byte{
script.Filename: script.Source,
}
_, err := testCode(testFiles, state, flagsTests{})

require.Error(t, err)
assert.ErrorContains(
t,
err,
"cannot find contract with location 'ApprovalVoting' in configuration",
)
})

t.Run("with missing testing alias in config", func(t *testing.T) {
t.Parallel()

// Setup
_, state, _ := util.TestMocks(t)

c := config.Contract{
Name: tests.ContractHelloString.Name,
Location: "SomeHelloContract.cdc",
Aliases: aliases,
Location: tests.ContractHelloString.Filename,
Aliases: config.Aliases{{
Network: "emulator",
Address: flow.HexToAddress("0x0000000000000007"),
}},
}
state.Contracts().AddOrUpdate(c)

Expand All @@ -181,10 +205,10 @@ func TestExecutingTests(t *testing.T) {
_, err := testCode(testFiles, state, flagsTests{})

require.Error(t, err)
assert.Error(
assert.ErrorContains(
t,
err,
"cannot find contract with location 'contractHello.cdc' in configuration",
"unable to find 'testing' alias for contract: Hello",
)
})

Expand Down

0 comments on commit cd51049

Please sign in to comment.