Skip to content

Conversation

@mcnallyella
Copy link
Contributor

@mcnallyella mcnallyella commented Jul 1, 2022

Bug

Fixes: NuGet/Home#11935

Regression? Last working version:

Description

I added a method in PackageSourceMappingProvider.cs that saves changes in package source mapping to the config. The save method calls the existing AddOrUpdatePackageSourceMappingSourceItem and Remove methods. This will be useful for implementing the options page UI. I also added 4 tests for the save method. There is also a draft PR with the changes to the options UI for package source mapping: #4711

PR Checklist

  • PR has a meaningful title

  • PR has a linked issue.

  • Described changes

  • Tests

    • Automated tests added
    • OR
    • Test exception
    • OR
    • N/A
  • Documentation

    • Documentation PR or issue filled
    • OR
    • N/A

@mcnallyella mcnallyella requested a review from a team as a code owner July 1, 2022 19:07
@kartheekp-ms
Copy link
Contributor

kartheekp-ms commented Jul 2, 2022

Are there any UI related changes in this PR? If yes, please consider adding screenshots or a link to spec for reference.

Comment on lines 9 to 10
NuGet.Configuration.PackageSourceMappingProvider.SavePackageSourceMappings(System.Collections.Generic.IReadOnlyList<NuGet.Configuration.PackageSourceMappingSourceItem> mappings) -> void
NuGet.Configuration.PackageSourceMappingProvider.SavePackageSourcesMappings(System.Collections.Generic.IReadOnlyList<NuGet.Configuration.PackageSourceMappingSourceItem> packageSourceMappingsSourceItems, NuGet.Configuration.PackageSourceUpdateOptions sourceUpdateSettings) -> void
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How will customers using the package NuGet.Configuration in their own apps use the two different overloads of SavePackageSourceMappings?

Once APIs become public and we ship to customers, it becomes very difficult in the future to change/remove because we have no way to investigate how many (if any) developers we will break who use these APIs. See also our "changes to APIs" section of the NuGet SDK docs: https://github.com/NuGet/NuGet.Client/blob/dev/docs/nuget-sdk.md#changes-to-apis

I agree that there needs to be public APIs to modify/save PackageSourceMappings. I think it was a significant oversight from an SDK point of view that this was not done when the feature was added in the first place. That has the very unfortunate side-effect that the person who ends up making these public then "bears the responsibility" of considering public API design, rather than the people who implemented the feature/methods in the first place. But once these become public, we have to "support" them for a long time, so I don't want to be reckless and mechanically make existing internal APIs public without considering public API design.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing this out. Turns out I don't need two save methods. I was copying the method from PackageSourceProvider.cs (which has two save methods) and didn't look closely enough to see if I should just do one instead.

}

//Remove all old mappings not in new mappings
if (existingSettingsLookup != null)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe you may need to reload this value.
Because existingSettingsLookup is stale now, so you may not see new values here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I won't see any new values, but I don't think I need to see the mappings that were just added/updated. I really only need to see the ones that are in the config currently but aren't the current list of mappings displayed in the UI. These will still be in existingSettingsLookup even if I add new ones

@zivkan zivkan mentioned this pull request Jul 13, 2022
6 tasks
Copy link
Member

@nkolev92 nkolev92 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have some pretty good tests and code is pretty easy to follow. 👏

I think we have some duplication in what the newly public methods provider.

dominoFire
dominoFire previously approved these changes Jul 19, 2022
Copy link
Contributor

@dominoFire dominoFire left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good job. Just comments. Thank you!

tempMappings.Add(testMappingItemAdd);
tempMappings.Remove(testMappingItem2);

sourceMappingProvider.SavePackageSourceMappings(tempMappings);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this call be the 'act' part of the test?

Suggested change
sourceMappingProvider.SavePackageSourceMappings(tempMappings);
// Act
sourceMappingProvider.SavePackageSourceMappings(tempMappings);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah there is a comment on line 602 labeling it "Act & Assert". That is how the other tests in this file seem to be set up, so I just copied how everything else was labeled.

IReadOnlyList<PackageSourceMappingSourceItem> existingSettingsLookup = GetPackageSourceMappingItems();
if (existingSettingsLookup == null)
{
return;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this mean packageSourceMapping should exist before this action?
Technically prevent from onboarding. Instead, should it create a new one?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed this if statement because it was a bit redundant. GetPackageSourceMappingItems() will return an empty list of mappings if Package source mappings has not yet been enabled, so existingSettingsLookup will never be null. The Remove() and AddorUpdate() methods have logic that will create a new config if there are no previous mappings

}
}

if (removeMappings != null && removeMappings.Count > 0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (removeMappings != null && removeMappings.Count > 0)
if (removeMappings.Count > 0)

You already initialized the list at line 75 so it's not going to be null.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one is not resolved, so I reverted back to unresolved state.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops sorry. I clicked the commit suggestion button, so I thought it was resolved. I guess the commit suggestion button does not actually work? I changed it manually

@mcnallyella mcnallyella force-pushed the dev-mcnallyella-PackageSourceMappingOptionsAPI branch from cbc26b2 to 6b3ae6d Compare July 22, 2022 17:02
Copy link
Contributor

@erdembayar erdembayar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work, almost ready if you address my comment regarding reduce number of new public apis.

<packageSourceMapping>
<clear />
<packageSource key=""nuget.org"">
<package pattern=""stuff"" />
Copy link
Contributor

@erdembayar erdembayar Jul 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have test just adding new pattern into existing nuget.org package sourceKey section?
Before:

 <packageSource key=""nuget.org"">
            <package pattern=""stuff1"" />
</packageSource>

After: Add stufff2

<packageSource key=""nuget.org"">
            <package pattern=""stuff1"" />
            <package pattern=""stuff2"" />
</packageSource>

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just made a test. SavePackageSourceMappings_NewPatternExistingSource_Add()

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's wait fingers crossed for CI test results. If CI tests are passing, then I'm ready to approve the PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks CI tests for your old commit "Remove WhiteSpace" still running, probably cancelling it manually better, because it'll trigger another CI build with latest changes.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mcnallyella
Few End2end tests are failing due to some reason, please rebase your branch with latest dev branch. Those failing tests are skipped here this morning.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I rebased it. I had a green build a moment ago but had to change a test, so hopefully the next build will be green as well

<packageSourceMapping>
<clear />
<packageSource key=""nuget.org"">
<package pattern=""stuff"" />
Copy link
Contributor

@erdembayar erdembayar Jul 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have test just removing 1 pattern from existing nuget.org package sourceKey section?
Before:

<packageSource key=""nuget.org"">
            <package pattern=""stuff1"" />
            <package pattern=""stuff2"" />
</packageSource>

After: Remove stuff1

<packageSource key=""nuget.org"">
            <package pattern=""stuff2"" />
</packageSource>

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just made a test for that. SavePackageSourceMappings_RemovePatternExistingSource_Remove()

Copy link
Member

@nkolev92 nkolev92 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implementation looks great!

I really like how detailed the tests are.

One idea for another test case, and some suggestions for removing duplicate tests.

result.Replace("\r\n", "\n")
.Should().BeEquivalentTo(
File.ReadAllText(configPath1).Replace("\r\n", "\n"));
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to do one or two more tests.
Multi config tests, where say you have 2 configs, A & B, A is further and has the source mapping clear, but B has a source mapping config for one source, but A has for another one.

I don't expect you to do the same matrix of tests, that'd be an overkill, but at least have a smoke test that ensures the correct one is being updated.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay I added a new test called SavePackageSourceMappings_WithTwoConfigs_UseCorrectMapping() to test with two configs.

@mcnallyella mcnallyella force-pushed the dev-mcnallyella-PackageSourceMappingOptionsAPI branch from baa6076 to 5739717 Compare July 25, 2022 19:11
@mcnallyella mcnallyella requested a review from nkolev92 July 25, 2022 22:33
Copy link
Member

@nkolev92 nkolev92 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👏
Great job

Copy link
Member

@zivkan zivkan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for optimizing the public API surface area.

Copy link
Contributor

@erdembayar erdembayar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work. 👏 👏 👏

@mcnallyella mcnallyella merged commit 1ad7503 into dev Jul 26, 2022
@mcnallyella mcnallyella deleted the dev-mcnallyella-PackageSourceMappingOptionsAPI branch July 26, 2022 17:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Package Source Mapping API does not support saving

8 participants