Skip to content

Commit 32272e9

Browse files
authored
Always refresh after Package Source changes in VS Options (#7092)
1 parent a893f4a commit 32272e9

2 files changed

Lines changed: 16 additions & 34 deletions

File tree

src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/Options/PackageSourcesPage.cs

Lines changed: 11 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public override async Task<ExternalSettingOperationResult<T>> GetValueAsync<T>(s
106106

107107
public override async Task<ExternalSettingOperationResult> SetValueAsync<T>(string moniker, T value, CancellationToken cancellationToken)
108108
{
109-
bool hasAnyHiddenPropertyChanged = false;
109+
bool isRefreshNeeded = false;
110110

111111
try
112112
{
@@ -122,19 +122,18 @@ public override async Task<ExternalSettingOperationResult> SetValueAsync<T>(stri
122122
return await Task.Run(
123123
() =>
124124
{
125-
(ExternalSettingOperationResult result, bool hasAnyHiddenPropertyChanged) savePackageSourcesResult = SavePackageSources(packageSourcesList, cancellationToken);
126-
hasAnyHiddenPropertyChanged = savePackageSourcesResult.hasAnyHiddenPropertyChanged;
127-
return savePackageSourcesResult.result;
125+
ExternalSettingOperationResult savePackageSourcesResult = SavePackageSources(packageSourcesList, cancellationToken);
126+
isRefreshNeeded = savePackageSourcesResult == ExternalSettingOperationResult.Success.Instance;
127+
return savePackageSourcesResult;
128128
},
129129
cancellationToken);
130130
case MonikerAuditSources:
131131
var auditSourceList = (IReadOnlyList<IDictionary<string, object>>)value;
132132
return await Task.Run(
133133
() =>
134134
{
135-
(ExternalSettingOperationResult result, bool hasAnyHiddenPropertyChanged) saveAuditSourcesResult = SaveAuditSources(auditSourceList, cancellationToken);
136-
hasAnyHiddenPropertyChanged = saveAuditSourcesResult.hasAnyHiddenPropertyChanged;
137-
return saveAuditSourcesResult.result;
135+
ExternalSettingOperationResult saveAuditSourcesResult = SaveAuditSources(auditSourceList, cancellationToken);
136+
return saveAuditSourcesResult;
138137
},
139138
cancellationToken);
140139
case MonikerMachineWideSources:
@@ -152,7 +151,7 @@ public override async Task<ExternalSettingOperationResult> SetValueAsync<T>(stri
152151
// Resume listening to setting changes after saving.
153152
_suppressSettingValuesChanged = false;
154153

155-
if (hasAnyHiddenPropertyChanged)
154+
if (isRefreshNeeded)
156155
{
157156
VsSettings_SettingsChanged(this, EventArgs.Empty);
158157
}
@@ -206,18 +205,16 @@ private ExternalSettingOperationResult SetIsEnabledOnMachineWidePackageSources(
206205
return result;
207206
}
208207

209-
private (ExternalSettingOperationResult result, bool hasAnyHiddenPropertyChanged) SavePackageSources(
208+
private ExternalSettingOperationResult SavePackageSources(
210209
IReadOnlyList<IDictionary<string, object>> packageSourceDictionaryList,
211210
CancellationToken cancellationToken)
212211
{
213-
bool hasAnyHiddenPropertyChanged = false;
214212
ExternalSettingOperationResult result;
215213

216214
try
217215
{
218216
List<PackageSource> packageSources = new List<PackageSource>(capacity: packageSourceDictionaryList.Count);
219217
IReadOnlyList<PackageSource> existingPackageSources = LoadPackageSources(isMachineWide: false);
220-
bool hasAnyPackageSourceNameChanged = false;
221218

222219
foreach (Dictionary<string, object> packageSourceDictionary in packageSourceDictionaryList)
223220
{
@@ -230,12 +227,6 @@ private ExternalSettingOperationResult SetIsEnabledOnMachineWidePackageSources(
230227
if (packageSourceDictionary.TryGetValue(MonikerPackageSourceId, out object packageSourceIdObj))
231228
{
232229
lookupName = packageSourceIdObj.ToString();
233-
234-
if (!string.Equals(lookupName, name, StringComparison.CurrentCultureIgnoreCase))
235-
{
236-
// Changing the ID needs to refresh Unified Settings since the ID is a hidden property.
237-
hasAnyPackageSourceNameChanged = true;
238-
}
239230
}
240231
else // Newly added Package Sources will not have an ID yet.
241232
{
@@ -260,8 +251,6 @@ private ExternalSettingOperationResult SetIsEnabledOnMachineWidePackageSources(
260251

261252
_packageSourceProvider.SavePackageSources(packageSources);
262253

263-
hasAnyHiddenPropertyChanged = hasAnyPackageSourceNameChanged;
264-
265254
result = ExternalSettingOperationResult.Success.Instance;
266255
}
267256
#pragma warning disable CA1031 // Do not catch general exception types
@@ -272,21 +261,19 @@ private ExternalSettingOperationResult SetIsEnabledOnMachineWidePackageSources(
272261
ActivityLog.LogError(ExceptionHelper.LogEntrySource, ex.ToString());
273262
}
274263

275-
return (result, hasAnyHiddenPropertyChanged);
264+
return result;
276265
}
277266

278-
private (ExternalSettingOperationResult result, bool hasAnyHiddenPropertyChanged) SaveAuditSources(
267+
private ExternalSettingOperationResult SaveAuditSources(
279268
IReadOnlyList<IDictionary<string, object>> auditSourceDictionaryList,
280269
CancellationToken cancellationToken)
281270
{
282-
bool hasAnyHiddenPropertyChanged = false;
283271
ExternalSettingOperationResult result;
284272

285273
try
286274
{
287275
List<PackageSource> auditSources = new List<PackageSource>(capacity: auditSourceDictionaryList.Count);
288276
IReadOnlyList<PackageSource> existingAuditSources = LoadAuditSources();
289-
bool hasAnyPackageSourceNameChanged = false;
290277

291278
foreach (Dictionary<string, object> packageSourceDictionary in auditSourceDictionaryList)
292279
{
@@ -299,12 +286,6 @@ private ExternalSettingOperationResult SetIsEnabledOnMachineWidePackageSources(
299286
if (packageSourceDictionary.TryGetValue(MonikerPackageSourceId, out object packageSourceIdObj))
300287
{
301288
lookupName = packageSourceIdObj.ToString();
302-
303-
if (!string.Equals(lookupName, name, StringComparison.CurrentCultureIgnoreCase))
304-
{
305-
// Changing the ID needs to refresh Unified Settings since the ID is a hidden property.
306-
hasAnyPackageSourceNameChanged = true;
307-
}
308289
}
309290
else // Newly added Package Sources will not have an ID yet.
310291
{
@@ -327,8 +308,6 @@ private ExternalSettingOperationResult SetIsEnabledOnMachineWidePackageSources(
327308

328309
_packageSourceProvider.SaveAuditSources(auditSources);
329310

330-
hasAnyHiddenPropertyChanged = hasAnyPackageSourceNameChanged;
331-
332311
result = ExternalSettingOperationResult.Success.Instance;
333312
}
334313
#pragma warning disable CA1031 // Do not catch general exception types
@@ -339,7 +318,7 @@ private ExternalSettingOperationResult SetIsEnabledOnMachineWidePackageSources(
339318
ActivityLog.LogError(ExceptionHelper.LogEntrySource, ex.ToString());
340319
}
341320

342-
return (result, hasAnyHiddenPropertyChanged);
321+
return result;
343322
}
344323

345324

test/NuGet.Clients.Tests/NuGet.PackageManagement.VisualStudio.Test/Options/PackageSourcesPageTests.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ protected override PackageSourcesPage CreateInstance(VSSettings? vsSettings)
7171
[Theory]
7272
[InlineData(true)]
7373
[InlineData(false)]
74-
public async Task SetValueAsync_AuditSources_WhenNameChanging_RaisesSettingValuesChangedAsync(bool isNameChanging)
74+
public async Task SetValueAsync_AuditSources_Never_RaisesSettingValuesChangedAsync(bool isNameChanging)
7575
{
7676
// Arrange
7777
string originalSourceName = "auditTestingSourceName";
@@ -224,6 +224,9 @@ public async Task GetValueAsync_WhenNuGetConfigHasInvalidSource_ReturnsFailureRe
224224
failure.ErrorMessage.Should().StartWith(Strings.Error_NuGetConfig_InvalidState);
225225
}
226226

227+
/// <summary>
228+
/// Any edit will cause the event to be raised. Here we test changing the source's name followed by the source's URL.
229+
/// </summary>
227230
[Theory]
228231
[InlineData(true)]
229232
[InlineData(false)]
@@ -273,7 +276,7 @@ public async Task SetValueAsync_WhenPackageNameChanging_RaisesSettingValuesChang
273276
// Assert
274277
result.Should().NotBeNull();
275278
result.Should().BeOfType<ExternalSettingOperationResult.Success>();
276-
wasVsSettingsSettingsChangedCalled.Should().Be(isPackageNameChanging);
279+
wasVsSettingsSettingsChangedCalled.Should().Be(true);
277280
}
278281

279282
[Theory]

0 commit comments

Comments
 (0)