10000 Allow [SemanticVersion] to be constructed with just major or major+minor by SteveL-MSFT · Pull Request #3696 · PowerShell/PowerShell · GitHub
[go: up one dir, main page]

Skip to content

Allow [SemanticVersion] to be constructed with just major or major+minor #3696

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

Merged
merged 1 commit into from
May 8, 2017

Conversation

SteveL-MSFT
Copy link
Member

[SemanticVersion]::new() required major, minor, and patch to be specfied which broke scenarios like:

$psversiontable.psversion -gt "3.0" which is used by PowerShellGet to determine if a module is compatible with the current version of PowerShell.

Change is to allow specifying only major or major+minor where the missing segments default to zero.

Addresses #1618

@@ -544,24 +544,24 @@ private static bool TryParseVersion(string version, ref VersionResult result)

var versionSansLabel = (dashIndex < 0) ? version : version.Substring(0, dashIndex);
string[] parsedComponents = versionSansLabel.Split(Utils.Separators.Dot);
if (parsedComponents.Length != 3)
if (parsedComponents.Length == 0 || parsedComponents.Length > 3)
Copy link
Collaborator
@JamesWTruher JamesWTruher May 4, 2017

Choose a reason for hiding this comment

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

arsedComponents.Length == 0 [](start = 17, length = 28)

i don't know if this condition can exist. Even [string]::Empty.Split('.').Length will return 1

Copy link
Member Author

Choose a reason for hiding this comment

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

I believe that's the ETS .Length returning 1 where we treat scalars as arrays and add both .Length and .Count properties. If you do [string]::Empty.Split('.')[0].Length, you get 0 as expected

Copy link
Contributor

Choose a reason for hiding this comment

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

This is C# - PowerShell ETS is not relevant, and you are inspecting the length of the first element, not the number of elements in the array. msdn does say:

If this instance does not contain any of the characters in separator, the returned array consists of a single element that contains this instance.

Copy link
Member Author

Choose a reason for hiding this comment

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

So it's a one element array that contains an empty string. In any case, there is already code that causes an empty string to fail before the split() statement.

Copy link
Contributor

Choose a reason for hiding this comment

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

@JamesWTruher pointed out that the length can't be 0 and that part of the test should be removed. I agreed. Are saying that it is possible?

Copy link
Member Author

Choose a reason for hiding this comment

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

Now I see what you are referring to. I'll remove that part.

@@ -178,10 +199,6 @@ Describe "SemanticVersion api tests" -Tags 'CI' {
# Revision isn't supported
{ [SemanticVersion]::new([Version]::new(0, 0, 0, 4)) } | Should Throw # PSArgumentException
{ [SemanticVersion]::new([Version]::new("1.2.3.4")) } | Should Throw # PSArgumentException

# Build is required
Copy link
Collaborator

Choose a reason for hiding this comment

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

is there a reason why [SemanticVersion]::new() is not supported? It's supported for System.Version

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't know if that was an explicit decision or accidentally left out. I don't see any reason why it shouldn't be supported as it seems 0.0.0 is a valid semver. I'll add support since it's a small change even if not related to this change :)

Copy link
Contributor

Choose a reason for hiding this comment

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

Under what scenario would we want a default constructor? If you want 0.0.0, be explicit.

Copy link
Member Author

Choose a reason for hiding this comment

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

For now, let's error on the side of simplicity and we can revisit this if there's customer demand for it

Copy link
Contributor
@lzybkr lzybkr left a comment

Choose a reason for hiding this comment

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

Note that the original implementation matched the specification, specifically: http://semver.org/#spec-item-2

We may want to discuss how best to diverge from the specification in a way that the community finds acceptable.

@@ -413,7 +413,7 @@ public SemanticVersion(int major, int minor, int patch, string label)
/// <exception cref="PSArgumentException">
/// If <paramref name="major"/>, <paramref name="minor"/>, or <paramref name="patch"/> is less than 0.
/// </exception> 8000 ;
public SemanticVersion(int major, int minor, int patch)
public SemanticVersion(int major, int minor = 0, int patch = 0)
Copy link
Contributor

Choose a reason for hiding this comment

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

We should not use default arguments in public apis. The guidance is to provide multiple overloads for languages that may not support default arguments.

Copy link
Member Author

Choose a reason for hiding this comment

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

Will change

@@ -544,24 +544,24 @@ private static bool TryParseVersion(string version, ref VersionResult result)

var versionSansLabel = (dashIndex < 0) ? version : version.Substring(0, dashIndex);
string[] parsedComponents = versionSansLabel.Split(Utils.Separators.Dot);
if (parsedComponents.Length != 3)
if (parsedComponents.Length == 0 || parsedComponents.Length > 3)
Copy link
Contributor

Choose a reason for hiding this comment

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

This is C# - PowerShell ETS is not relevant, and you are inspecting the length of the first element, not the number of elements in the array. msdn does say:

If this instance does not contain any of the characters in separator, the returned array consists of a single element that contains this instance.

@@ -178,10 +199,6 @@ Describe "SemanticVersion api tests" -Tags 'CI' {
# Revision isn't supported
{ [SemanticVersion]::new([Version]::new(0, 0, 0, 4)) } | Should Throw # PSArgumentException
{ [SemanticVersion]::new([Version]::new("1.2.3.4")) } | Should Throw # PSArgumentException

# Build is required
Copy link
Contributor

Choose a reason for hiding this comment

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

Under what scenario would we want a default constructor? If you want 0.0.0, be explicit.

@SteveL-MSFT
Copy link
Member Author

Regarding the semver.org spec, we are not diverging. The resulting semver is conformant to semver.

@SteveL-MSFT
Copy link
Member Author

@lzybkr I believe I've addressed all of your concerns

@lzybkr
Copy link
Contributor
lzybkr commented May 4, 2017

I disagree - the bnf for parsing a semantic version is pretty clear - 3 parts: https://github.com/mojombo/semver/blob/master/semver.md

I found another example where we aren't matching the spec:

PS> [System.Management.Automation.SemanticVersion]"01.01.01"

Major Minor Patch Label
----- ----- ----- -----
    1     1     1

If we're more permissive than the spec, people will rely on it, and where those uses occur, we can expect issues with more conforming tools.

@SteveL-MSFT
Copy link
Member Author

Leading zeros aren't allowed so the resulting semver conforms. I guess the question is whether the resulting object is what matters for conformance or the input to the constructor. Seems you prefer a more strict approach and reject 01.0.0 vs producing a valid semver. This also means that you can't compare a semver like psversion against "3.0" since the latter is not a semver.

@lzybkr
Copy link
Contributor
lzybkr commented May 5, 2017

Comparing with System.Version is no problem, but comparing with "3.0" might be, based on my reading of the spec.

It might be worth opening an issue in the repo I linked to.

@SteveL-MSFT
Copy link
Member Author

I've opened semver/semver#368

@SteveL-MSFT
Copy link
Member Author
SteveL-MSFT commented May 5, 2017

The response I got from the maintainer of semver indicates it is reasonable and not explicitly defined by semver spec. Also, this thread semver/semver#344 has supporters for both sides with no official stance, but examples of other libraries that have taken both approaches (strict and loose). I think it's reasonable to allow constructing a semver with defaults to zero. Cc @PowerShell/powershell-committee

Copy link
Contributor
@lzybkr lzybkr left a comment

Choose a reason for hiding this comment

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

OK - changes look fine except for what looks like an unnecessary test.

…fied which broke scenarios like:

`$psversiontable.psversion -gt "3.0"` which is used by PowerShellGet to determine if a module is compatible with the current version of PowerShell.

Change is to allow specifying only major or major+minor where the missing segments default to zero by providing overloaded constructors and allow
the string parsing method to not require major, minor, and patch segments to all be specified (only major is required).

Based on the [response](semver/semver#368) from the maintainer of semver, there is no requirement to have strict
conformance for the inputs to the constructor and allowing "3.0" to result in a semver of 3.0.0 is reasonable.
@SteveL-MSFT
Copy link
Member Author

@lzybkr all fixed

@SteveL-MSFT SteveL-MSFT assigned lzybkr and unassigned mirichmo May 8, 2017
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.

5 participants
0