diff --git a/internal/util/files.go b/internal/util/files.go index 956f16316..47e3268dd 100644 --- a/internal/util/files.go +++ b/internal/util/files.go @@ -47,13 +47,13 @@ func AbsolutePath(basePath, relativePath string) string { return filepath.Join(filepath.Dir(basePath), relativePath) } -// IsPathLocation returns true if the location is a file path (contains .cdc) +// IsPathLocation returns true if the location names a Cadence source file. func IsPathLocation(location common.Location) bool { stringLocation, ok := location.(common.StringLocation) if !ok { return false } - return strings.Contains(stringLocation.String(), ".cdc") + return filepath.Ext(stringLocation.String()) == ".cdc" } // NormalizePathLocation normalizes a relative path import against a base location diff --git a/internal/util/util_test.go b/internal/util/util_test.go index 6df8f35af..aed480837 100644 --- a/internal/util/util_test.go +++ b/internal/util/util_test.go @@ -22,11 +22,28 @@ import ( "strings" "testing" + "github.com/onflow/cadence/common" "github.com/onflow/flow-go-sdk" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) +func TestIsPathLocation(t *testing.T) { + for _, tc := range []struct { + name string + want bool + }{ + {"contracts/Example.cdc", true}, + {"contracts/Example.cdc.backup", false}, + {"contracts.cdc/Example", false}, + {"Example", false}, + } { + t.Run(tc.name, func(t *testing.T) { + assert.Equal(t, tc.want, IsPathLocation(common.StringLocation(tc.name))) + }) + } +} + func TestIsAddressValidForNetwork(t *testing.T) { testnetAddr := flow.HexToAddress("8efde57e98c557fa") // Valid testnet address emulatorAddr := flow.HexToAddress("f8d6e0586b0a20c7") // Valid emulator address