1
1
using System ;
2
2
using System . Collections . Generic ;
3
+ using System . Globalization ;
3
4
using System . Net ;
4
5
using System . Net . Http ;
5
6
using System . Threading . Tasks ;
@@ -48,7 +49,8 @@ public async Task HostProviderRegistry_GetProvider_Auto_HasProviders_ReturnsSupp
48
49
{
49
50
var context = new TestCommandContext ( ) ;
50
51
var registry = new HostProviderRegistry ( context ) ;
51
- var input = new InputArguments ( new Dictionary < string , string > ( ) ) ;
52
+ var remote = new Uri ( "https://example.com" ) ;
53
+ InputArguments input = CreateInputArguments ( remote ) ;
52
54
53
55
var provider1Mock = new Mock < IHostProvider > ( ) ;
54
56
var provider2Mock = new Mock < IHostProvider > ( ) ;
@@ -66,12 +68,69 @@ public async Task HostProviderRegistry_GetProvider_Auto_HasProviders_ReturnsSupp
66
68
Assert . Same ( provider2Mock . Object , result ) ;
67
69
}
68
70
71
+ [ Fact ]
72
+ public async Task HostProviderRegistry_GetProvider_Auto_HasProviders_SetsProviderGlobalConfig ( )
73
+ {
74
+ var context = new TestCommandContext ( ) ;
75
+ var registry = new HostProviderRegistry ( context ) ;
76
+ var remote = new Uri ( "https://example.com" ) ;
77
+ InputArguments input = CreateInputArguments ( remote ) ;
78
+
79
+ string providerId = "myProvider" ;
80
+ string configKey = string . Format ( CultureInfo . InvariantCulture ,
81
+ "{0}.https://example.com.{1}" ,
82
+ Constants . GitConfiguration . Credential . SectionName ,
83
+ Constants . GitConfiguration . Credential . Provider ) ;
84
+
85
+ var providerMock = new Mock < IHostProvider > ( ) ;
86
+ providerMock . Setup ( x => x . Id ) . Returns ( providerId ) ;
87
+ providerMock . Setup ( x => x . IsSupported ( It . IsAny < InputArguments > ( ) ) ) . Returns ( true ) ;
88
+
89
+ registry . Register ( providerMock . Object , HostProviderPriority . Normal ) ;
90
+
91
+ IHostProvider result = await registry . GetProviderAsync ( input ) ;
92
+
93
+ Assert . Same ( providerMock . Object , result ) ;
94
+ Assert . True ( context . Git . Configuration . Global . TryGetValue ( configKey , out IList < string > config ) ) ;
95
+ Assert . Equal ( 1 , config . Count ) ;
96
+ Assert . Equal ( providerId , config [ 0 ] ) ;
97
+ }
98
+
99
+ [ Fact ]
100
+ public async Task HostProviderRegistry_GetProvider_Auto_HasProviders_SetsProviderGlobalConfig_HostWithPath ( )
101
+ {
102
+ var context = new TestCommandContext ( ) ;
103
+ var registry = new HostProviderRegistry ( context ) ;
104
+ var remote = new Uri ( "https://example.com/alice/repo.git/" ) ;
105
+ InputArguments input = CreateInputArguments ( remote ) ;
106
+
107
+ string providerId = "myProvider" ;
108
+ string configKey = string . Format ( CultureInfo . InvariantCulture ,
109
+ "{0}.https://example.com/alice/repo.git.{1}" , // expect any trailing slash to be removed
110
+ Constants . GitConfiguration . Credential . SectionName ,
111
+ Constants . GitConfiguration . Credential . Provider ) ;
112
+
113
+ var providerMock = new Mock < IHostProvider > ( ) ;
114
+ providerMock . Setup ( x => x . Id ) . Returns ( providerId ) ;
115
+ providerMock . Setup ( x => x . IsSupported ( It . IsAny < InputArguments > ( ) ) ) . Returns ( true ) ;
116
+
117
+ registry . Register ( providerMock . Object , HostProviderPriority . Normal ) ;
118
+
119
+ IHostProvider result = await registry . GetProviderAsync ( input ) ;
120
+
121
+ Assert . Same ( providerMock . Object , result ) ;
122
+ Assert . True ( context . Git . Configuration . Global . TryGetValue ( configKey , out IList < string > config ) ) ;
123
+ Assert . Equal ( 1 , config . Count ) ;
124
+ Assert . Equal ( providerId , config [ 0 ] ) ;
125
+ }
126
+
69
127
[ Fact ]
70
128
public async Task HostProviderRegistry_GetProvider_Auto_MultipleValidProviders_ReturnsFirstRegistered ( )
71
129
{
72
130
var context = new TestCommandContext ( ) ;
73
131
var registry = new HostProviderRegistry ( context ) ;
74
- var input = new InputArguments ( new Dictionary < string , string > ( ) ) ;
132
+ var remote = new Uri ( "https://example.com" ) ;
133
+ InputArguments input = CreateInputArguments ( remote ) ;
75
134
76
135
var provider1Mock = new Mock < IHostProvider > ( ) ;
77
136
var provider2Mock = new Mock < IHostProvider > ( ) ;
@@ -94,7 +153,8 @@ public async Task HostProviderRegistry_GetProvider_Auto_MultipleValidProvidersMu
94
153
{
95
154
var context = new TestCommandContext ( ) ;
96
155
var registry = new HostProviderRegistry ( context ) ;
97
- var input = new InputArguments ( new Dictionary < string , string > ( ) ) ;
156
+ var remote = new Uri ( "https://example.com" ) ;
157
+ InputArguments input = CreateInputArguments ( remote ) ;
98
158
99
159
var provider1Mock = new Mock < IHostProvider > ( ) ;
100
160
var provider2Mock = new Mock < IHostProvider > ( ) ;
@@ -152,7 +212,8 @@ public async Task HostProviderRegistry_GetProvider_AutoProviderSpecified_Returns
152
212
Settings = { ProviderOverride = Constants . ProviderIdAuto }
153
213
} ;
154
214
var registry = new HostProviderRegistry ( context ) ;
155
- var input = new InputArguments ( new Dictionary < string , string > ( ) ) ;
215
+ var remote = new Uri ( "https://example.com" ) ;
216
+ InputArguments input = CreateInputArguments ( remote ) ;
156
217
157
218
var provider1Mock = new Mock < IHostProvider > ( ) ;
158
219
var provider2Mock = new Mock < IHostProvider > ( ) ;
@@ -181,7 +242,8 @@ public async Task HostProviderRegistry_GetProvider_UnknownProviderSpecified_Retu
181
242
Settings = { ProviderOverride = "provider42" }
182
243
} ;
183
244
var registry = new HostProviderRegistry ( context ) ;
184
- var input = new InputArguments ( new Dictionary < string , string > ( ) ) ;
245
+ var remote = new Uri ( "https://example.com" ) ;
246
+ InputArguments input = CreateInputArguments ( remote ) ;
185
247
186
248
var provider1Mock = new Mock < IHostProvider > ( ) ;
187
249
var provider2Mock = new Mock < IHostProvider > ( ) ;
@@ -239,7 +301,8 @@ public async Task HostProviderRegistry_GetProvider_AutoLegacyAuthoritySpecified_
239
301
Settings = { LegacyAuthorityOverride = Constants . AuthorityIdAuto }
240
302
} ;
241
303
var registry = new HostProviderRegistry ( context ) ;
242
- var input = new InputArguments ( new Dictionary < string , string > ( ) ) ;
304
+ var remote = new Uri ( "https://example.com" ) ;
305
+ InputArguments input = CreateInputArguments ( remote ) ;
243
306
244
307
var provider1Mock = new Mock < IHostProvider > ( ) ;
245
308
var provider2Mock = new Mock < IHostProvider > ( ) ;
@@ -266,13 +329,7 @@ public async Task HostProviderRegistry_GetProvider_Auto_NetworkProbe_ReturnsSupp
266
329
var context = new TestCommandContext ( ) ;
267
330
var registry = new HostProviderRegistry ( context ) ;
268
331
var remoteUri = new Uri ( "https://provider2.onprem.example.com" ) ;
269
- var input = new InputArguments (
270
- new Dictionary < string , string >
271
- {
272
- [ "protocol" ] = remoteUri . Scheme ,
273
- [ "host" ] = remoteUri . Host
274
- }
275
- ) ;
332
+ InputArguments input = CreateInputArguments ( remoteUri ) ;
276
333
277
334
var provider1Mock = new Mock < IHostProvider > ( ) ;
278
335
provider1Mock . Setup ( x => x . IsSupported ( It . IsAny < InputArguments > ( ) ) ) . Returns ( false ) ;
@@ -408,5 +465,21 @@ public async Task HostProviderRegistry_GetProvider_Auto_NetworkProbe_NoNetwork_R
408
465
httpHandler . AssertRequest ( HttpMethod . Head , remoteUri , 1 ) ;
409
466
Assert . Same ( lowProviderMock . Object , result ) ;
410
467
}
468
+
469
+ public static InputArguments CreateInputArguments ( Uri uri )
470
+ {
471
+ var dict = new Dictionary < string , string >
472
+ {
473
+ [ "protocol" ] = uri . Scheme ,
474
+ [ "host" ] = uri . IsDefaultPort ? uri . Host : $ "{ uri . Host } :{ uri . Port } "
475
+ } ;
476
+
477
+ if ( ! string . IsNullOrWhiteSpace ( uri . AbsolutePath ) && uri . AbsolutePath != "/" )
478
+ {
479
+ dict [ "path" ] = uri . AbsolutePath . TrimEnd ( '/' ) ;
480
+ }
481
+
482
+ return new InputArguments ( dict ) ;
483
+ }
411
484
}
412
485
}
0 commit comments