Skip to content

Commit

Permalink
Windows path fix (#1190)
Browse files Browse the repository at this point in the history
  • Loading branch information
jribbink authored Sep 15, 2023
1 parent f7756b4 commit cf41263
Show file tree
Hide file tree
Showing 18 changed files with 85 additions and 75 deletions.
5 changes: 3 additions & 2 deletions flowkit/config/json/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"regexp"
"strings"

Expand Down Expand Up @@ -224,7 +225,7 @@ func transformAdvancedToConfig(accountName string, a advancedAccount) (*config.A
if a.Key.Location == "" {
return nil, fmt.Errorf("missing location to a file containing the private key value for the account %s", accountName)
}
key.Location = a.Key.Location
key.Location = filepath.FromSlash(a.Key.Location)
}

return &config.Account{
Expand Down Expand Up @@ -326,7 +327,7 @@ func transformAdvancedKeyToJSON(key config.AccountKey) advanceKey {
case config.KeyTypeGoogleKMS:
advancedKey.ResourceID = key.ResourceID
case config.KeyTypeFile:
advancedKey.Location = key.Location
advancedKey.Location = filepath.ToSlash(key.Location)
}

return advancedKey
Expand Down
3 changes: 2 additions & 1 deletion flowkit/config/json/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"testing"

"github.com/onflow/flow-go-sdk"
Expand Down Expand Up @@ -114,7 +115,7 @@ func Test_ConfigAccountKeysAdvancedFile(t *testing.T) {
assert.Equal(t, "f8d6e0586b0a20c7", account.Address.String())
assert.Equal(t, "SHA3_256", key.HashAlgo.String())
assert.Equal(t, "ECDSA_P256", key.SigAlgo.String())
assert.Equal(t, "./test.pkey", key.Location)
assert.Equal(t, filepath.FromSlash("./test.pkey"), key.Location)
assert.Equal(t, "", key.ResourceID)

jsonAccs := transformAccountsToJSON(accounts)
Expand Down
8 changes: 5 additions & 3 deletions flowkit/config/json/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package json
import (
"encoding/json"
"fmt"
"path/filepath"

"github.com/invopop/jsonschema"
"github.com/onflow/flow-go-sdk"
Expand Down Expand Up @@ -70,7 +71,7 @@ func transformContractsToJSON(contracts config.Contracts) jsonContracts {
// if simple case
if !c.IsAliased() {
jsonContracts[c.Name] = jsonContract{
Simple: c.Location,
Simple: filepath.ToSlash(c.Location),
}
} else { // if advanced config
// check if we already created for this name then add or create
Expand All @@ -81,7 +82,7 @@ func transformContractsToJSON(contracts config.Contracts) jsonContracts {

jsonContracts[c.Name] = jsonContract{
Advanced: jsonContractAdvanced{
Source: c.Location,
Source: filepath.ToSlash(c.Location),
Aliases: aliases,
},
}
Expand Down Expand Up @@ -110,14 +111,15 @@ func (j *jsonContract) UnmarshalJSON(b []byte) error {
// simple
err := json.Unmarshal(b, &source)
if err == nil {
j.Simple = source
j.Simple = filepath.FromSlash(source)
return nil
}

// advanced
err = json.Unmarshal(b, &advancedFormat)
if err == nil {
j.Advanced = advancedFormat
j.Advanced.Source = filepath.FromSlash(j.Advanced.Source)
} else {
return err
}
Expand Down
15 changes: 8 additions & 7 deletions flowkit/config/json/contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package json

import (
"encoding/json"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -43,8 +44,8 @@ func Test_ConfigContractsSimple(t *testing.T) {
marketContract, _ := contracts.ByName("KittyItemsMarket")
assert.NotNil(t, marketContract)

assert.Equal(t, "./cadence/kittyItems/contracts/KittyItems.cdc", contract.Location)
assert.Equal(t, "./cadence/kittyItems/contracts/KittyItemsMarket.cdc", marketContract.Location)
assert.Equal(t, filepath.FromSlash("./cadence/kittyItems/contracts/KittyItems.cdc"), contract.Location)
assert.Equal(t, filepath.FromSlash("./cadence/kittyItems/contracts/KittyItemsMarket.cdc"), marketContract.Location)
}

func Test_ConfigContractsComplex(t *testing.T) {
Expand Down Expand Up @@ -73,8 +74,8 @@ func Test_ConfigContractsComplex(t *testing.T) {
kittyItemsMarket, _ := contracts.ByName("KittyItemsMarket")
assert.NotNil(t, kittyItemsMarket)

assert.Equal(t, "./cadence/kittyItems/contracts/KittyItems.cdc", kittyItems.Location)
assert.Equal(t, "./cadence/kittyItemsMarket/contracts/KittyItemsMarket.cdc", kittyItemsMarket.Location)
assert.Equal(t, filepath.FromSlash("./cadence/kittyItems/contracts/KittyItems.cdc"), kittyItems.Location)
assert.Equal(t, filepath.FromSlash("./cadence/kittyItemsMarket/contracts/KittyItemsMarket.cdc"), kittyItemsMarket.Location)

assert.True(t, kittyItemsMarket.Aliases.ByNetwork("emulator") == nil)

Expand Down Expand Up @@ -115,19 +116,19 @@ func Test_ConfigContractsAliases(t *testing.T) {
assert.NotNil(t, fungibleToken)
assert.True(t, fungibleToken.IsAliased())
assert.Equal(t, "e5a8b7f23e8b548f", fungibleToken.Aliases.ByNetwork("emulator").Address.String())
assert.Equal(t, "../hungry-kitties/cadence/contracts/FungibleToken.cdc", fungibleToken.Location)
assert.Equal(t, filepath.FromSlash("../hungry-kitties/cadence/contracts/FungibleToken.cdc"), fungibleToken.Location)

kibble, _ := contracts.ByName("Kibble")
assert.NotNil(t, kibble)
assert.True(t, kibble.IsAliased())
assert.Equal(t, "../hungry-kitties/cadence/contracts/Kibble.cdc", kibble.Location)
assert.Equal(t, filepath.FromSlash("../hungry-kitties/cadence/contracts/Kibble.cdc"), kibble.Location)
assert.Equal(t, "ead892083b3e2c6c", kibble.Aliases.ByNetwork("testnet").Address.String())
assert.Equal(t, "f8d6e0586b0a20c7", kibble.Aliases.ByNetwork("emulator").Address.String())

nft, _ := contracts.ByName("NonFungibleToken")
assert.NotNil(t, nft)
assert.False(t, nft.IsAliased())
assert.Equal(t, nft.Location, "../hungry-kitties/cadence/contracts/NonFungibleToken.cdc")
assert.Equal(t, nft.Location, filepath.FromSlash("../hungry-kitties/cadence/contracts/NonFungibleToken.cdc"))
}

func Test_TransformContractToJSON(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion flowkit/config/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package config_test
import (
"fmt"
"os"
"path/filepath"
"testing"

"github.com/onflow/flow-cli/flowkit/config"
Expand Down Expand Up @@ -459,5 +460,5 @@ func Test_LoadAccountFileType(t *testing.T) {
assert.NoError(t, err)

assert.Len(t, conf.Accounts, 1)
assert.Equal(t, "./test.pkey", acc.Key.Location)
assert.Equal(t, filepath.FromSlash("./test.pkey"), acc.Key.Location)
}
3 changes: 2 additions & 1 deletion flowkit/project/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package project

import (
"fmt"
"path/filepath"

"gonum.org/v1/gonum/graph"
"gonum.org/v1/gonum/graph/simple"
Expand Down Expand Up @@ -86,7 +87,7 @@ func (d *Deployment) add(contract *Contract) error {
}

d.contracts = append(d.contracts, c)
d.contractsByLocation[c.Location()] = c
d.contractsByLocation[filepath.Clean(c.Location())] = c
d.contractsByName[c.Name] = c

return nil
Expand Down
8 changes: 4 additions & 4 deletions flowkit/project/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type testContract struct {
var addresses = test.AddressGenerator()

var testContractA = testContract{
location: "ContractA.cdc",
location: "foobar/ContractA.cdc",
code: []byte(`pub contract ContractA {}`),
accountAddress: addresses.New(),
}
Expand All @@ -50,7 +50,7 @@ var testContractB = testContract{
}

var testContractC = testContract{
location: "ContractC.cdc",
location: "foobar/ContractC.cdc",
code: []byte(`
import ContractA from "ContractA.cdc"
Expand All @@ -62,7 +62,7 @@ var testContractC = testContract{
var testContractD = testContract{
location: "ContractD.cdc",
code: []byte(`
import ContractC from "ContractC.cdc"
import ContractC from "foobar/ContractC.cdc"
pub contract ContractD {}
`),
Expand Down Expand Up @@ -91,7 +91,7 @@ var testContractF = testContract{
var testContractG = testContract{
location: "ContractG.cdc",
code: []byte(`
import ContractA from "ContractA.cdc"
import ContractA from "foobar/ContractA.cdc"
import ContractB from "ContractB.cdc"
pub contract ContractG {}
Expand Down
10 changes: 5 additions & 5 deletions flowkit/project/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ package project

import (
"fmt"
"path"
"path/filepath"

"github.com/onflow/flow-go-sdk"
)
Expand Down Expand Up @@ -49,7 +49,7 @@ func (i *ImportReplacer) Replace(program *Program) (*Program, error) {

for _, imp := range imports {
// check if import by path exists (e.g. import X from ["./X.cdc"])
importLocation := path.Clean(absolutePath(program.Location(), imp))
importLocation := filepath.Clean(absolutePath(program.Location(), imp))
address, isPath := contractsLocations[importLocation]
if isPath {
program.replaceImport(imp, address)
Expand All @@ -72,18 +72,18 @@ func (i *ImportReplacer) Replace(program *Program) (*Program, error) {
func (i *ImportReplacer) getContractsLocations() map[string]string {
locationAddress := make(map[string]string)
for _, contract := range i.contracts {
locationAddress[path.Clean(contract.Location())] = contract.AccountAddress.String()
locationAddress[filepath.Clean(contract.Location())] = contract.AccountAddress.String()
// add also by name since we might use the new import schema
locationAddress[contract.Name] = contract.AccountAddress.String()
}

for source, target := range i.aliases {
locationAddress[path.Clean(source)] = flow.HexToAddress(target).String()
locationAddress[filepath.Clean(source)] = flow.HexToAddress(target).String()
}

return locationAddress
}

func absolutePath(basePath, relativePath string) string {
return path.Join(path.Dir(basePath), relativePath)
return filepath.Join(filepath.Dir(basePath), relativePath)
}
5 changes: 2 additions & 3 deletions flowkit/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ package flowkit
import (
"fmt"
"os"
"path"
"path/filepath"

"github.com/onflow/flow-go-sdk/crypto"
Expand Down Expand Up @@ -173,7 +172,7 @@ func (p *State) DeploymentContractsByNetwork(network config.Network) ([]*project

contract := project.NewContract(
c.Name,
path.Clean(location),
filepath.Clean(location),
code,
account.Address,
account.Name,
Expand Down Expand Up @@ -213,7 +212,7 @@ func (p *State) AliasesForNetwork(network config.Network) project.LocationAliase
for _, contract := range p.conf.Contracts {
if contract.IsAliased() && contract.Aliases.ByNetwork(network.Name) != nil {
alias := contract.Aliases.ByNetwork(network.Name).Address.String()
aliases[path.Clean(contract.Location)] = alias // alias for import by file location
aliases[filepath.Clean(contract.Location)] = alias // alias for import by file location
aliases[contract.Name] = alias // alias for import by name
}
}
Expand Down
42 changes: 22 additions & 20 deletions flowkit/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package flowkit
import (
"fmt"
"os"
"path/filepath"
"sort"
"testing"

Expand Down Expand Up @@ -343,7 +344,7 @@ func generateAliasesComplexProject() State {

func Test_GetContractsByNameSimple(t *testing.T) {
p := generateSimpleProject()
path := "../hungry-kitties/cadence/contracts/NonFungibleToken.cdc"
path := filepath.FromSlash("../hungry-kitties/cadence/contracts/NonFungibleToken.cdc")
af.WriteFile(path, []byte("pub contract{}"), os.ModePerm)

contracts, err := p.DeploymentContractsByNetwork(config.EmulatorNetwork)
Expand Down Expand Up @@ -399,21 +400,22 @@ func Test_GetContractsByNameComplex(t *testing.T) {
require.NoError(t, err)
require.Equal(t, 7, len(contracts))

//sort names so tests are deterministic
//sort contracts by name so tests are deterministic
sort.Slice(contracts, func(i, j int) bool {
return contracts[i].Name < contracts[j].Name
})

contractNames := funk.Map(contracts, func(c *project.Contract) string {
return c.Name
}).([]string)
sort.Strings(contractNames)

sources := funk.Map(contracts, func(c *project.Contract) string {
return c.Location()
}).([]string)
sort.Strings(sources)

targets := funk.Map(contracts, func(c *project.Contract) string {
return c.AccountAddress.String()
}).([]string)
sort.Strings(targets)

assert.Equal(t, contractNames[0], "FungibleToken")
assert.Equal(t, contractNames[1], "Kibble")
Expand All @@ -423,21 +425,21 @@ func Test_GetContractsByNameComplex(t *testing.T) {
assert.Equal(t, contractNames[5], "KittyItemsMarket")
assert.Equal(t, contractNames[6], "NonFungibleToken")

assert.Equal(t, sources[0], "../hungry-kitties/cadence/contracts/FungibleToken.cdc")
assert.Equal(t, sources[1], "../hungry-kitties/cadence/contracts/NonFungibleToken.cdc")
assert.Equal(t, sources[2], "cadence/kibble/contracts/Kibble.cdc")
assert.Equal(t, sources[3], "cadence/kittyItems/contracts/KittyItems.cdc")
assert.Equal(t, sources[4], "cadence/kittyItems/contracts/KittyItems.cdc")
assert.Equal(t, sources[5], "cadence/kittyItemsMarket/contracts/KittyItemsMarket.cdc")
assert.Equal(t, sources[6], "cadence/kittyItemsMarket/contracts/KittyItemsMarket.cdc")
assert.Equal(t, sources[0], filepath.FromSlash("../hungry-kitties/cadence/contracts/FungibleToken.cdc"))
assert.Equal(t, sources[1], filepath.FromSlash("cadence/kibble/contracts/Kibble.cdc"))
assert.Equal(t, sources[2], filepath.FromSlash("cadence/kittyItems/contracts/KittyItems.cdc"))
assert.Equal(t, sources[3], filepath.FromSlash("cadence/kittyItems/contracts/KittyItems.cdc"))
assert.Equal(t, sources[4], filepath.FromSlash("cadence/kittyItemsMarket/contracts/KittyItemsMarket.cdc"))
assert.Equal(t, sources[5], filepath.FromSlash("cadence/kittyItemsMarket/contracts/KittyItemsMarket.cdc"))
assert.Equal(t, sources[6], filepath.FromSlash("../hungry-kitties/cadence/contracts/NonFungibleToken.cdc"))

assert.Equal(t, targets[0], "f8d6e0586b0a20c1")
assert.Equal(t, targets[1], "f8d6e0586b0a20c1")
assert.Equal(t, targets[2], "f8d6e0586b0a20c1")
assert.Equal(t, targets[2], "f8d6e0586b0a20c7")
assert.Equal(t, targets[3], "f8d6e0586b0a20c1")
assert.Equal(t, targets[4], "f8d6e0586b0a20c1")
assert.Equal(t, targets[5], "f8d6e0586b0a20c7")
assert.Equal(t, targets[6], "f8d6e0586b0a20c7")
assert.Equal(t, targets[4], "f8d6e0586b0a20c7")
assert.Equal(t, targets[5], "f8d6e0586b0a20c1")
assert.Equal(t, targets[6], "f8d6e0586b0a20c1")
}

func Test_EmulatorConfigComplex(t *testing.T) {
Expand Down Expand Up @@ -485,7 +487,7 @@ func Test_GetAliases(t *testing.T) {
contracts, _ := p.DeploymentContractsByNetwork(config.EmulatorNetwork)

assert.Len(t, aliases, 2)
assert.Equal(t, aliases["../hungry-kitties/cadence/contracts/FungibleToken.cdc"], "ee82856bf20e2aa6")
assert.Equal(t, aliases[filepath.FromSlash("../hungry-kitties/cadence/contracts/FungibleToken.cdc")], "ee82856bf20e2aa6")
assert.Len(t, contracts, 1)
assert.Equal(t, contracts[0].Name, "NonFungibleToken")
}
Expand All @@ -503,11 +505,11 @@ func Test_GetAliasesComplex(t *testing.T) {
assert.Equal(t, cEmulator[0].Name, "NonFungibleToken")

assert.Len(t, aEmulator, 4)
assert.Equal(t, aEmulator["../hungry-kitties/cadence/contracts/FungibleToken.cdc"], "ee82856bf20e2aa6")
assert.Equal(t, aEmulator["../hungry-kitties/cadence/contracts/Kibble.cdc"], "ee82856bf20e2aa6")
assert.Equal(t, aEmulator[filepath.FromSlash("../hungry-kitties/cadence/contracts/FungibleToken.cdc")], "ee82856bf20e2aa6")
assert.Equal(t, aEmulator[filepath.FromSlash("../hungry-kitties/cadence/contracts/Kibble.cdc")], "ee82856bf20e2aa6")

assert.Len(t, aTestnet, 2)
assert.Equal(t, aTestnet["../hungry-kitties/cadence/contracts/Kibble.cdc"], "ee82856bf20e2aa6")
assert.Equal(t, aTestnet[filepath.FromSlash("../hungry-kitties/cadence/contracts/Kibble.cdc")], "ee82856bf20e2aa6")

assert.Len(t, cTestnet, 2)
assert.Equal(t, cTestnet[0].Name, "NonFungibleToken")
Expand Down
4 changes: 2 additions & 2 deletions internal/settings/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"errors"
"fmt"
"os"
"path"
"path/filepath"

"github.com/spf13/viper"
)
Expand Down Expand Up @@ -51,7 +51,7 @@ func FileDir() string {
if err != nil {
dir = "."
}
return path.Join(dir, settingsDir)
return filepath.Join(dir, settingsDir)
}

// Set updates settings file with new value for provided key
Expand Down
Loading

0 comments on commit cf41263

Please sign in to comment.