Skip to content
This repository has been archived by the owner on Dec 18, 2017. It is now read-only.

Commit

Permalink
fix #2390 by forcing SemanticVersion.toString() to normalize version
Browse files Browse the repository at this point in the history
- removed references to OriginalString
- removed DnuRestore test for unnormalized directory on disk
- added DnuPublish test for version normalization
- added DnuPack test for version normalization
  • Loading branch information
JunTaoLuo committed Aug 29, 2015
1 parent 9e7a621 commit 3824423
Show file tree
Hide file tree
Showing 14 changed files with 239 additions and 135 deletions.
2 changes: 1 addition & 1 deletion src/Microsoft.Dnx.Compilation/ProjectExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public static CompilationProjectContext ToCompilationContext(this Project self,
new CompilationTarget(self.Name, frameworkName, configuration, aspect),
self.ProjectDirectory,
self.ProjectFilePath,
self.Version.GetNormalizedVersionString(),
self.Version.ToString(),
self.AssemblyFileVersion,
self.EmbedInteropTypes,
new CompilationFiles(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ internal Library ToLibrary()
{
return new Library(
Identity.Name,
Identity.Version?.GetNormalizedVersionString(),
Identity.Version?.ToString(),
Path,
Type,
Dependencies.Select(d => d.Name),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,17 @@ public string GetManifestFilePath(string packageId, SemanticVersion version)
public string GetHashPath(string packageId, SemanticVersion version)
{
return Path.Combine(GetInstallPath(packageId, version),
$"{packageId}.{version.GetNormalizedVersionString()}{Constants.HashFileExtension}");
$"{packageId}.{version}{Constants.HashFileExtension}");
}

public virtual string GetPackageDirectory(string packageId, SemanticVersion version)
{
return Path.Combine(packageId, version.GetNormalizedVersionString());
return Path.Combine(packageId, version.ToString());
}

public virtual string GetPackageFileName(string packageId, SemanticVersion version)
{
return $"{packageId}.{version.GetNormalizedVersionString()}{Constants.PackageExtension}";
return $"{packageId}.{version}{Constants.PackageExtension}";
}

public virtual string GetManifestFileName(string packageId, SemanticVersion version)
Expand Down
81 changes: 32 additions & 49 deletions src/Microsoft.Dnx.Runtime/NuGet/SemanticVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Globalization;
using System.Text;
using NuGet.Resources;

namespace NuGet
Expand All @@ -13,12 +14,11 @@ namespace NuGet
/// </summary>
public sealed class SemanticVersion : IComparable, IComparable<SemanticVersion>, IEquatable<SemanticVersion>
{
private string _normalizedVersionString;

public SemanticVersion(string version)
: this(Parse(version))
{
// The constructor normalizes the version string so that it we do not need to normalize it every time we need to operate on it.
// The original string represents the original form in which the version is represented to be used when printing.
OriginalString = version;
}

public SemanticVersion(int major, int minor, int build, int revision)
Expand All @@ -37,24 +37,17 @@ public SemanticVersion(Version version)
}

public SemanticVersion(Version version, string specialVersion)
: this(version, specialVersion, null)
{
}

private SemanticVersion(Version version, string specialVersion, string originalString)
{
if (version == null)
{
throw new ArgumentNullException(nameof(version));
}
Version = NormalizeVersionValue(version);
SpecialVersion = specialVersion ?? string.Empty;
OriginalString = string.IsNullOrEmpty(originalString) ? version.ToString() + (!string.IsNullOrEmpty(specialVersion) ? '-' + specialVersion : null) : originalString;
}

internal SemanticVersion(SemanticVersion semVer)
{
OriginalString = semVer.ToString();
Version = semVer.Version;
SpecialVersion = semVer.SpecialVersion;
}
Expand All @@ -77,43 +70,6 @@ public string SpecialVersion
private set;
}

public string OriginalString { get; }

public string GetNormalizedVersionString()
{
var revision = Version.Revision > 0 ? ("." + Version.Revision.ToString(CultureInfo.InvariantCulture)) : string.Empty;
var specialVer = !string.IsNullOrEmpty(SpecialVersion) ? ("-" + SpecialVersion) : string.Empty;

// SemanticVersion normalizes the missing components to 0.
return $"{Version.Major}.{Version.Minor}.{Version.Build}{revision}{specialVer}";
}

public string[] GetOriginalVersionComponents()
{
if (!string.IsNullOrEmpty(OriginalString))
{
string original;

// search the start of the SpecialVersion part, if any
int dashIndex = OriginalString.IndexOf('-');
if (dashIndex != -1)
{
// remove the SpecialVersion part
original = OriginalString.Substring(0, dashIndex);
}
else
{
original = OriginalString;
}

return SplitAndPadVersionString(original);
}
else
{
return SplitAndPadVersionString(Version.ToString());
}
}

private static string[] SplitAndPadVersionString(string version)
{
string[] a = version.Split('.');
Expand Down Expand Up @@ -207,7 +163,7 @@ private static bool TryParseInternal(string version, bool strict, out SemanticVe
}
}

semVer = new SemanticVersion(NormalizeVersionValue(versionValue), specialVersion, version.Replace(" ", ""));
semVer = new SemanticVersion(NormalizeVersionValue(versionValue), specialVersion);
return true;
}

Expand Down Expand Up @@ -319,7 +275,34 @@ public int CompareTo(SemanticVersion other)

public override string ToString()
{
return OriginalString;
if (_normalizedVersionString == null)
{
var builder = new StringBuilder();
builder
.Append(Version.Major)
.Append('.')
.Append(Version.Minor)
.Append('.')
.Append(Math.Max(0, Version.Build));

if (Version.Revision > 0)
{
builder
.Append('.')
.Append(Version.Revision);
}

if (!string.IsNullOrEmpty(SpecialVersion))
{
builder
.Append('-')
.Append(SpecialVersion);
}

_normalizedVersionString = builder.ToString();
}

return _normalizedVersionString;
}

public bool Equals(SemanticVersion other)
Expand Down
21 changes: 0 additions & 21 deletions src/Microsoft.Dnx.Runtime/NuGet/SemanticVersionExtensions.cs

This file was deleted.

2 changes: 1 addition & 1 deletion src/Microsoft.Dnx.Tooling/Building/BuildManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ private static string Normalize(string projectDir)

private static string GetPackagePath(Runtime.Project project, string outputPath, bool symbols = false)
{
string fileName = project.Name + "." + project.Version + (symbols ? ".symbols" : "") + ".nupkg";
string fileName = $"{project.Name}.{project.Version}{(symbols ? ".symbols" : string.Empty)}{NuGet.Constants.PackageExtension}";
return Path.Combine(outputPath, fileName);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static XElement ToXElement(this ManifestMetadata metadata, XNamespace ns)
}

elem.Add(new XElement(ns + "id", metadata.Id));
elem.Add(new XElement(ns + "version", metadata.Version.OriginalString));
elem.Add(new XElement(ns + "version", metadata.Version.ToString()));
AddElementIfNotNull(elem, ns, "title", metadata.Title);
elem.Add(new XElement(ns + "requireLicenseAcceptance", metadata.RequireLicenseAcceptance));
AddElementIfNotNull(elem, ns, "authors", metadata.Authors, authors => string.Join(",", authors));
Expand Down
9 changes: 0 additions & 9 deletions src/Microsoft.Dnx.Tooling/Restore/NuGet/PackageRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,6 @@ public IEnumerable<PackageInfo> FindPackagesById(string packageId)
continue;
}
if (!version.IsOriginalStringNormalized())
{
// For a non-http match, if the OriginalVersion string is not normalized that means name of the folder which contains
// the package is not a normalized string. It will cause trouble for file searching in later stage. By invalidating this
// match, it ensures the package will be reinstalled under a correct folder. This change ensures a package installed
// by older version of DNX won't prevent new DNX to install correct package.
continue;
}
var manifestFilePath = _repositoryRoot.GetFiles(versionDir, "*" + Constants.ManifestExtension)
.FirstOrDefault();
if (string.IsNullOrEmpty(manifestFilePath))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static IEnumerable<object[]> NormalizedVersionStringTestData
[MemberData(nameof(NormalizedVersionStringTestData))]
public void TestGetNormalizeVersionString(SemanticVersion version, string expectation)
{
Assert.Equal(expectation, version.GetNormalizedVersionString());
Assert.Equal(expectation, version.ToString());
}
}
}
81 changes: 81 additions & 0 deletions test/Microsoft.Dnx.Tooling.FunctionalTests/DnuPackTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,87 @@ public void DnuPack_OutPathSpecified(string flavor, string os, string architectu
}
}

[Theory]
[MemberData(nameof(RuntimeComponents))]
public void DnuPack_NormalizesVersionNumberWithRevisionNumberOfZero(string flavor, string os, string architecture)
{
int exitCode;
var projectName = "TestProject";
var projectStructure = @"{
'.': ['project.json']
}";
var runtimeHomeDir = TestUtils.GetRuntimeHomeDir(flavor, os, architecture);

using (var testEnv = new DnuTestEnvironment(runtimeHomeDir, projectName))
{
DirTree.CreateFromJson(projectStructure)
.WithFileContents("project.json", @"{
""version"": ""1.0.0.0"",
""frameworks"": {
""dnx451"": {}
}
}")
.WriteTo(testEnv.ProjectPath);

exitCode = DnuTestUtils.ExecDnu(
runtimeHomeDir,
subcommand: "restore",
arguments: string.Empty,
workingDir: testEnv.ProjectPath);
Assert.Equal(0, exitCode);

exitCode = DnuTestUtils.ExecDnu(
runtimeHomeDir,
subcommand: "pack",
arguments: string.Empty,
workingDir: testEnv.ProjectPath);
Assert.Equal(0, exitCode);

Assert.True(File.Exists(Path.Combine(testEnv.ProjectPath, "bin", "Debug", $"{projectName}.1.0.0.nupkg")));
Assert.True(File.Exists(Path.Combine(testEnv.ProjectPath, "bin", "Debug", $"{projectName}.1.0.0.symbols.nupkg")));
}
}
[Theory]
[MemberData(nameof(RuntimeComponents))]
public void DnuPack_NormalizesVersionNumberWithNoBuildNumber(string flavor, string os, string architecture)
{
int exitCode;
var projectName = "TestProject";
var projectStructure = @"{
'.': ['project.json']
}";
var runtimeHomeDir = TestUtils.GetRuntimeHomeDir(flavor, os, architecture);

using (var testEnv = new DnuTestEnvironment(runtimeHomeDir, projectName))
{
DirTree.CreateFromJson(projectStructure)
.WithFileContents("project.json", @"{
""version"": ""1.0-beta"",
""frameworks"": {
""dnx451"": {}
}
}")
.WriteTo(testEnv.ProjectPath);

exitCode = DnuTestUtils.ExecDnu(
runtimeHomeDir,
subcommand: "restore",
arguments: string.Empty,
workingDir: testEnv.ProjectPath);
Assert.Equal(0, exitCode);

exitCode = DnuTestUtils.ExecDnu(
runtimeHomeDir,
subcommand: "pack",
arguments: string.Empty,
workingDir: testEnv.ProjectPath);
Assert.Equal(0, exitCode);

Assert.True(File.Exists(Path.Combine(testEnv.ProjectPath, "bin", "Debug", $"{projectName}.1.0.0-beta.nupkg")));
Assert.True(File.Exists(Path.Combine(testEnv.ProjectPath, "bin", "Debug", $"{projectName}.1.0.0-beta.symbols.nupkg")));
}
}

[Theory]
[MemberData(nameof(RuntimeComponents))]
public void DnuPack_DoesNotExecutePostBuildScriptWhenBuildFails(string flavor, string os, string architecture)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Microsoft.Dnx.Tooling
public class DnuPackagesAddTests
{
private static readonly string ProjectName = "HelloWorld";
private static readonly SemanticVersion ProjectVersion = new SemanticVersion("0.1-beta");
private static readonly SemanticVersion ProjectVersion = new SemanticVersion("0.1.0-beta");
private static readonly string Configuration = "Release";
private static readonly string PackagesDirName = "packages";
private static readonly string OutputDirName = "output";
Expand Down
Loading

0 comments on commit 3824423

Please sign in to comment.