Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --config-file param to dotnet nuget push command #4819

Closed
wants to merge 2 commits into from

Conversation

aortiz-msft
Copy link
Contributor

@aortiz-msft aortiz-msft commented Sep 24, 2022

Bug

Fixes: NuGet/Home#4879

Regression? Last working version: No.

Description

Adding support for the --config-file option in dotnet nuget push.

As part of this change, adding automated test coverage for the --config-file (-configfile) option in both NuGet.exe and dotnet.exe as follows:

  1. NuGetPackCommandTests now has test coverage for the -configfile option in NuGet.exe.
  2. XPlatPushTests validates the new --config-file option in dotnet.exe. Because the implementation in NuGet.exe and dotnet.exe is shared (see PushRunner.cs), only added an additional test for an error message that differs in both executables.
  3. TODO in this PR: Moved the CreateFile utility method in NuGet.CommandLine.Tests/Utils.cs to a project that is shared between both test suites, Test.Utility/TestFileSystemUtility.cs. (Note: I will do this once there is agreement on whether the TestFileSystemUtility class is appropriate or a new class.)

Also:

  1. Improved performance of existing tests in XPlatPushTests by avoiding instantiation and enumeration of a List. Instead, test params are added directly to a string array which is used by the execution method.

PR Checklist

@aortiz-msft aortiz-msft requested a review from a team as a code owner September 24, 2022 18:22
string.Join(" ", args),
true);

// TODO: The bad config file is ignored, so I need to confirm this is expected behavior.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@zivkan , @nkolev92 - Any input here?

Copy link
Member

Choose a reason for hiding this comment

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

As mentioned in chat, it does feel like a bug. My first guess would have been that the config file isn't actually being read, but then I'd expect the next test, PushCommand_ConfigFile_ExpectedFormat, to fail.

Anyway, this PR clearly didn't introduce that behaviour, so we should create a new issue, and skip this test until that other issue is fixed.

jeffkl
jeffkl previously approved these changes Sep 26, 2022
src/NuGet.Clients/NuGet.CommandLine/NuGetCommand.resx Outdated Show resolved Hide resolved
@aortiz-msft aortiz-msft force-pushed the origin/dev-aortiz-pushcommandconfigfile branch from 3061517 to 4b2d8b6 Compare September 26, 2022 22:34
@aortiz-msft aortiz-msft force-pushed the origin/dev-aortiz-pushcommandconfigfile branch from 1fd1699 to ef202fd Compare September 27, 2022 00:41
@@ -105,7 +110,9 @@ public static void Register(CommandLineApplication app, Func<ILogger> getLogger)
}

#pragma warning disable CS0618 // Type or member is obsolete
var sourceProvider = new PackageSourceProvider(XPlatUtility.GetSettingsForCurrentWorkingDirectory(), enablePackageSourcesChangedEvent: false);
var sourceProvider = new PackageSourceProvider(
XPlatUtility.ProcessConfigFile(configurationFile.Value()),
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should add a validation to ensure file path exists before accessing it.

Copy link
Member

Choose a reason for hiding this comment

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

Also, does XPlatUtility.ProcessConfigFile fall back to GetSettingsForCurrentWorkingDirectory when configurationFile.Value() is null or empty (was not provided on the command line)?

This code, the way it's written, makes it look like a breaking change and that passing the --config-file is becoming mandatory. edit: reading the tests, it appears that it does maintain backwards compatibility. It's just not obvious to me.

@@ -105,7 +110,9 @@ public static void Register(CommandLineApplication app, Func<ILogger> getLogger)
}

#pragma warning disable CS0618 // Type or member is obsolete
var sourceProvider = new PackageSourceProvider(XPlatUtility.GetSettingsForCurrentWorkingDirectory(), enablePackageSourcesChangedEvent: false);
var sourceProvider = new PackageSourceProvider(
XPlatUtility.ProcessConfigFile(configurationFile.Value()),
Copy link
Member

Choose a reason for hiding this comment

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

Also, does XPlatUtility.ProcessConfigFile fall back to GetSettingsForCurrentWorkingDirectory when configurationFile.Value() is null or empty (was not provided on the command line)?

This code, the way it's written, makes it look like a breaking change and that passing the --config-file is becoming mandatory. edit: reading the tests, it appears that it does maintain backwards compatibility. It's just not obvious to me.

@@ -36,6 +36,11 @@ public static void Register(CommandLineApplication app, Func<ILogger> getLogger)
Strings.SymbolSource_Description,
CommandOptionType.SingleValue);

var configurationFile = push.Option(
"--config-file <source>",
Copy link
Member

Choose a reason for hiding this comment

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

@@ -34,7 +40,7 @@ public void PushCommand_PushToV2FileSystemSource()
var packageFileName = Util.CreateTestPackage("testPackage1", "1.1.0", packageDirectory);

// Act
string[] args = new string[] { "push", packageFileName, "-Source", source };
string[] args = new string[] { Command_Push, packageFileName, Param_Source, source };
Copy link
Member

Choose a reason for hiding this comment

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

I'm sorry, but I don't find this easier. Command_Push is many more characters than just "push", similar for Param_Source. And given they're in constants, my gut feeling is that this means it's a non-obvious value. Like, maybe Command_Push is maybe not just "push", so now I feel obligated to go to the top of the file to find out what it is.

I get this this could be useful if we ever rename push to something else, then it's just a single place to change for all the tests in this file. However, renaming it would be a breaking change that I just don't see happening. So, my opinion is that we optimize for readability, which in my opinion, means prefer the string literals. But this is 100% opinionated.

@@ -2078,6 +2084,339 @@ public void PushCommand_PushToServerV3_WithSymbols_ApiKey_SymbolApiKey_BothFromC
}
}

[Theory]
[InlineData(">")]
Copy link
Member

Choose a reason for hiding this comment

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

If there's only a single InlineData, I think we may as well just use [Fact] instead of [Theory].

@@ -36,6 +36,11 @@ public static void Register(CommandLineApplication app, Func<ILogger> getLogger)
Strings.SymbolSource_Description,
CommandOptionType.SingleValue);

var configurationFile = push.Option(
"--config-file <source>",
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
"--config-file <source>",
"--config-file <file>",

In NuGet vocabulary, a source is a feed of packages. I don't know if this was a copy-paste issue, but source really doesn't feel right to me. I don't know if we should say path instead of file.

dotnet restore -h says --configile <FILE>, while dotnet list package -h says --config <CONFIG_FILE>. Clear as mud. As consistent as flipping a coin.

Copy link
Contributor

Choose a reason for hiding this comment

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

Just fyi --configfile <File> is used for dotnet nuget trust.

string.Join(" ", args),
true);

// TODO: The bad config file is ignored, so I need to confirm this is expected behavior.
Copy link
Member

Choose a reason for hiding this comment

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

As mentioned in chat, it does feel like a bug. My first guess would have been that the config file isn't actually being read, but then I'd expect the next test, PushCommand_ConfigFile_ExpectedFormat, to fail.

Anyway, this PR clearly didn't introduce that behaviour, so we should create a new issue, and skip this test until that other issue is fixed.

Comment on lines +2261 to +2265
FileSystemUtils.CreateFile(
configFilePath,
configFileName,
string.Format(
CultureInfo.InvariantCulture,
Copy link
Member

Choose a reason for hiding this comment

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

I dislike nested method calls, because it's harder to read (where is the end ) for string.Format? Are there any additional parameters to FileSystemUtils.CreateFile, or just the 3? Also, when debugging, maybe I want to see the result of string.Format without stepping into the outer method call. If string.Format was a method from our own code, rather than the BCL (or if I disabled "Just My Code" in the debugger), then it's also harder to step into FileSystemUtils.CreateFile without stepping into the nested method call.

Therefore, I suggest extracting the string.Format into a variable, and then have a simple method call to FileSystemUtils.CreateFile.

@@ -310,6 +310,7 @@ public static string CreateUAPProject(string directory, string projectJsonConten
return projectFile;
}

// TODO: Remove CreateFile methods from this utility class. Will require changes to all classes in NuGet.CommandLine.Test.
Copy link
Member

Choose a reason for hiding this comment

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

coding guidelines number 22: https://github.com/NuGet/NuGet.Client/blob/dev/docs/coding-guidelines.md

Adding TODOs is not recommended and should be avoided whenever possible. In cases in which they do get added each TODO needs to be linked to an issue. Use the full url to the GitHub issue.

Comment on lines +17 to +20
private const string Command_Push = "push";
private const string Param_ApiKey = "--api-key";
private const string Param_ConfigFile = "--config-file";
private const string Param_Source = "--source";
Copy link
Member

Choose a reason for hiding this comment

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

same comments as the other file. My personal opinion is that this does not aid readability, and the risk of any of these is so low that putting them all in a constant is not worth the reduction in readability.

[PackageSourceTheory]
[PackageSourceData(TestSources.MyGet)]
[PackageSourceData(TestSources.ProGet, Skip = "No such host is known")]
[PackageSourceData(TestSources.Klondike, Skip = "401 (Invalid API key)")]
[PackageSourceData(TestSources.NuGetServer, Skip = "No such host is known")]
public async Task PushToServerSucceeds(PackageSource packageSource)
{
// Arrange
// Setup
Copy link
Member

Choose a reason for hiding this comment

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

https://github.com/NuGet/NuGet.Client/blob/dev/docs/coding-guidelines.md#unit-test-structure

We use Arrange, Act, Assert as the coding guidelines unit test structure. This is known as the AAA pattern. Perhaps not well known (I never heard of it before joining NuGet), but doing a web search in multiple search engines finds many results, so I can't claim it's made up just for NuGet.

Copy link
Contributor

@donnie-msft donnie-msft left a comment

Choose a reason for hiding this comment

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

Great to see push improvements! I'm blocking on the comments/upvotes I put on coding guideline violations and several code changes or refactorings.

@@ -70,7 +76,7 @@ public void PushCommand_PushToV3FileSystemSource()
var packageFileName = Util.CreateTestPackage(packageId, version, packageDirectory);

// Act
string[] args = new string[] { "push", packageFileName, "-Source", source };
string[] args = new string[] { Command_Push, packageFileName, Param_Source, source };
Copy link
Contributor

Choose a reason for hiding this comment

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

As others have commented, I don't think introducing variables helps readability or maintainability, so should be reverted.


Assert.True(
exitCode != 0,
"The run did not fail as desired. Simply got this output:" + log.ShowMessages());
Copy link
Contributor

Choose a reason for hiding this comment

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

Another area where I'm not sure adding strings is helpful.

Suggested change
"The run did not fail as desired. Simply got this output:" + log.ShowMessages());
log.ShowMessages());

@ghost ghost added the Status:No recent activity PRs that have not had any recent activity and will be closed if the label is not removed label Oct 4, 2022
@ghost
Copy link

ghost commented Oct 4, 2022

This PR has been automatically marked as stale because it has no activity for 7 days. It will be closed if no further activity occurs within another 7 days of this comment. If it is closed, you may reopen it anytime when you're ready again, as long as you don't delete the branch.

@ghost ghost closed this Oct 11, 2022
@nkolev92 nkolev92 deleted the origin/dev-aortiz-pushcommandconfigfile branch February 23, 2023 00:06
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status:No recent activity PRs that have not had any recent activity and will be closed if the label is not removed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

dotnet-nuget-push: does not support --config-file included in examples
6 participants