Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jribbink committed Sep 23, 2024
1 parent 945cd42 commit 1fb224f
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 27 deletions.
19 changes: 15 additions & 4 deletions internal/dependencymanager/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,11 @@ var addFlags = addFlagsCollection{

var addCommand = &command.Command{
Cmd: &cobra.Command{
Use: "add <source string>",
Short: "Add a single contract and its dependencies.",
Example: "flow dependencies add testnet://0afe396ebc8eee65.FlowToken",
Args: cobra.ExactArgs(1),
Use: "add <source string or core contract name>",
Short: "Add a single contract and its dependencies.",
Example: `flow dependencies add testnet://0afe396ebc8eee65.FlowToken
flow dependencies add FlowToken`,
Args: cobra.ExactArgs(1),
},
RunS: add,
Flags: &struct{}{},
Expand Down Expand Up @@ -75,6 +76,16 @@ func add(
return nil, err
}

// First check if the dependency is a core contract.
if isCoreContract(dep) {
if err := installer.AddByCoreContractName(dep, addFlags.name); err != nil {
logger.Error(fmt.Sprintf("Error: %v", err))
return nil, err
}
return nil, nil
}

// Otherwise, add the dependency by source string.
if err := installer.AddBySourceString(dep, addFlags.name); err != nil {
logger.Error(fmt.Sprintf("Error: %v", err))
return nil, err
Expand Down
53 changes: 30 additions & 23 deletions internal/dependencymanager/dependencyinstaller.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,29 +183,46 @@ func (di *DependencyInstaller) Install() error {

// AddBySourceString processes a single dependency and installs it and any dependencies it has, as well as adding it to the state
func (di *DependencyInstaller) AddBySourceString(depSource, customName string) error {
// First, check if the source string is a core contract name
depNetwork, depAddress, depContractName, err := config.ParseSourceString(depSource)
if err != nil {
return fmt.Errorf("error parsing source: %w", err)
}

name := depContractName

if customName != "" {
name = customName
}

dep := config.Dependency{
Name: name,
Source: config.Source{
NetworkName: depNetwork,
Address: flowsdk.HexToAddress(depAddress),
ContractName: depContractName,
},
}

return di.Add(dep)
}

func (di *DependencyInstaller) AddByCoreContractName(coreContractName, customName string) error {
var depNetwork, depAddress, depContractName string
sc := systemcontracts.SystemContractsForChain(flowGo.Mainnet)
for _, coreContract := range sc.All() {
if coreContract.Name == depSource {
if coreContract.Name == coreContractName {
depAddress = coreContract.Address.String()
depNetwork = config.MainnetNetwork.Name
depContractName = depSource
depContractName = coreContractName
break
}
}

// If it's not a core contract, parse the source string
if depAddress == "" {
var err error
depNetwork, depAddress, depContractName, err = config.ParseSourceString(depSource)
if err != nil {
return fmt.Errorf("error parsing source: %w", err)
}
return fmt.Errorf("contract %s not found in core contracts", coreContractName)
}

name := depContractName

if customName != "" {
name = customName
}
Expand All @@ -219,19 +236,7 @@ func (di *DependencyInstaller) AddBySourceString(depSource, customName string) e
},
}

if err := di.processDependency(dep); err != nil {
return fmt.Errorf("error processing dependency: %w", err)
}

di.checkForConflictingContracts()

if err := di.saveState(); err != nil {
return err
}

di.logs.LogAll(di.Logger)

return nil
return di.Add(dep)
}

// Add processes a single dependency and installs it and any dependencies it has, as well as adding it to the state
Expand All @@ -240,6 +245,8 @@ func (di *DependencyInstaller) Add(dep config.Dependency) error {
return fmt.Errorf("error processing dependency: %w", err)
}

di.checkForConflictingContracts()

if err := di.saveState(); err != nil {
return err
}
Expand Down
39 changes: 39 additions & 0 deletions internal/dependencymanager/dependencyinstaller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,45 @@ func TestDependencyInstallerAdd(t *testing.T) {
assert.NoError(t, err, "Failed to read generated file")
assert.NotNil(t, fileContent)
})

t.Run("Add by core contract name", func(t *testing.T) {
gw := mocks.DefaultMockGateway()

gw.GetAccount.Run(func(args mock.Arguments) {
addr := args.Get(1).(flow.Address)
assert.Equal(t, addr.String(), "1654653399040a61")
acc := tests.NewAccountWithAddress(addr.String())
acc.Contracts = map[string][]byte{
"FlowToken": []byte("access(all) contract FlowToken {}"),
}

gw.GetAccount.Return(acc, nil)
})

di := &DependencyInstaller{
Gateways: map[string]gateway.Gateway{
config.EmulatorNetwork.Name: gw.Mock,
config.TestnetNetwork.Name: gw.Mock,
config.MainnetNetwork.Name: gw.Mock,
},
Logger: logger,
State: state,
SaveState: true,
TargetDir: "",
SkipDeployments: true,
SkipAlias: true,
dependencies: make(map[string]config.Dependency),
}

err := di.AddByCoreContractName("FlowToken", "")
assert.NoError(t, err, "Failed to install dependencies")

filePath := fmt.Sprintf("imports/%s/%s.cdc", "1654653399040a61", "FlowToken")
fileContent, err := state.ReaderWriter().ReadFile(filePath)
assert.NoError(t, err, "Failed to read generated file")
assert.NotNil(t, fileContent)
assert.Contains(t, string(fileContent), "contract FlowToken")
})
}

func TestDependencyInstallerAddMany(t *testing.T) {
Expand Down

0 comments on commit 1fb224f

Please sign in to comment.