@@ -743,3 +743,110 @@ func TestAliasedImportHandling(t *testing.T) {
743743 assert .NotNil (t , fileContent )
744744 })
745745}
746+
747+ func TestDependencyInstallerWithAlias (t * testing.T ) {
748+ logger := output .NewStdoutLogger (output .NoneLog )
749+ _ , state , _ := util .TestMocks (t )
750+
751+ serviceAcc , _ := state .EmulatorServiceAccount ()
752+ serviceAddress := serviceAcc .Address
753+
754+ t .Run ("AddBySourceStringWithName" , func (t * testing.T ) {
755+ gw := mocks .DefaultMockGateway ()
756+
757+ gw .GetAccount .Run (func (args mock.Arguments ) {
758+ addr := args .Get (1 ).(flow.Address )
759+ assert .Equal (t , addr .String (), serviceAddress .String ())
760+ acc := tests .NewAccountWithAddress (addr .String ())
761+ acc .Contracts = map [string ][]byte {
762+ "NumberFormatter" : []byte ("access(all) contract NumberFormatter {}" ),
763+ }
764+ gw .GetAccount .Return (acc , nil )
765+ })
766+
767+ di := & DependencyInstaller {
768+ Gateways : map [string ]gateway.Gateway {
769+ config .EmulatorNetwork .Name : gw .Mock ,
770+ },
771+ Logger : logger ,
772+ State : state ,
773+ SaveState : true ,
774+ TargetDir : "" ,
775+ SkipDeployments : true ,
776+ SkipAlias : true ,
777+ Name : "NumberFormatterCustom" ,
778+ dependencies : make (map [string ]config.Dependency ),
779+ }
780+
781+ err := di .AddBySourceString (fmt .Sprintf ("%s://%s.%s" , config .EmulatorNetwork .Name , serviceAddress .String (), "NumberFormatter" ))
782+ assert .NoError (t , err , "Failed to add dependency with import alias" )
783+
784+ // Check that the dependency was added with the import alias name
785+ dep := state .Dependencies ().ByName ("NumberFormatterCustom" )
786+ assert .NotNil (t , dep , "Dependency should exist with import alias name" )
787+ assert .Equal (t , "NumberFormatter" , dep .Source .ContractName , "Source ContractName should be the actual contract name" )
788+ assert .Equal (t , "NumberFormatter" , dep .Canonical , "Canonical should be set to the actual contract name for import aliasing" )
789+
790+ // Check that the contract was added with canonical field for Cadence import aliasing
791+ contract , err := state .Contracts ().ByName ("NumberFormatterCustom" )
792+ assert .NoError (t , err , "Contract should exist" )
793+ assert .Equal (t , "NumberFormatter" , contract .Canonical , "Contract Canonical should be set for import aliasing" )
794+
795+ // Check that the file was created with the actual contract name
796+ filePath := fmt .Sprintf ("imports/%s/NumberFormatter.cdc" , serviceAddress .String ())
797+ fileContent , err := state .ReaderWriter ().ReadFile (filePath )
798+ assert .NoError (t , err , "Contract file should exist at imports/address/NumberFormatter.cdc" )
799+ assert .NotNil (t , fileContent )
800+ })
801+
802+ t .Run ("AddByCoreContractNameWithName" , func (t * testing.T ) {
803+ // Mock the gateway to return FlowToken contract
804+ gw := mocks .DefaultMockGateway ()
805+ gw .GetAccount .Run (func (args mock.Arguments ) {
806+ addr := args .Get (1 ).(flow.Address )
807+ acc := tests .NewAccountWithAddress (addr .String ())
808+ acc .Contracts = map [string ][]byte {
809+ "FlowToken" : []byte ("access(all) contract FlowToken {}" ),
810+ }
811+ gw .GetAccount .Return (acc , nil )
812+ })
813+
814+ di := & DependencyInstaller {
815+ Gateways : map [string ]gateway.Gateway {
816+ config .MainnetNetwork .Name : gw .Mock ,
817+ },
818+ Logger : logger ,
819+ State : state ,
820+ SaveState : true ,
821+ TargetDir : "" ,
822+ SkipDeployments : true ,
823+ SkipAlias : true ,
824+ Name : "FlowTokenCustom" ,
825+ dependencies : make (map [string ]config.Dependency ),
826+ }
827+
828+ err := di .AddByCoreContractName ("FlowToken" )
829+ assert .NoError (t , err , "Failed to add core contract with import alias" )
830+
831+ // Check that the dependency was added with the import alias name
832+ dep := state .Dependencies ().ByName ("FlowTokenCustom" )
833+ assert .NotNil (t , dep , "Dependency should exist with import alias name" )
834+ assert .Equal (t , "FlowToken" , dep .Source .ContractName , "Source ContractName should be FlowToken" )
835+ assert .Equal (t , "FlowToken" , dep .Canonical , "Canonical should be set to FlowToken for import aliasing" )
836+ })
837+
838+ t .Run ("AddAllByNetworkAddressWithNameError" , func (t * testing.T ) {
839+ // This test doesn't need gateways since it returns an error before making any gateway calls
840+ di := & DependencyInstaller {
841+ Logger : logger ,
842+ State : state ,
843+ SaveState : true ,
844+ TargetDir : "" ,
845+ Name : "SomeName" ,
846+ }
847+
848+ err := di .AddAllByNetworkAddress (fmt .Sprintf ("%s://%s" , config .EmulatorNetwork .Name , serviceAddress .String ()))
849+ assert .Error (t , err , "Should error when using --name with network://address format" )
850+ assert .Contains (t , err .Error (), "--name flag is not supported when installing all contracts" , "Error message should mention name flag limitation" )
851+ })
852+ }
0 commit comments