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 DnuPack test for version normalization
-amending PR comments
  • Loading branch information
JunTaoLuo committed Aug 28, 2015
1 parent 226a158 commit 69f4017
Show file tree
Hide file tree
Showing 14 changed files with 53 additions and 141 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
55 changes: 6 additions & 49 deletions src/Microsoft.Dnx.Runtime/NuGet/SemanticVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ public sealed class SemanticVersion : IComparable, IComparable<SemanticVersion>,
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 +34,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 +67,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 +160,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 +272,11 @@ public int CompareTo(SemanticVersion other)

public override string ToString()
{
return OriginalString;
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 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 @@ -413,7 +413,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.GetNormalizedVersionString() + (symbols ? ".symbols" : "") + ".nupkg";
string fileName = project.Name + "." + project.Version.ToString() + (symbols ? ".symbols" : "") + ".nupkg";
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());
}
}
}
34 changes: 34 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,40 @@ public void DnuPack_OutPathSpecified(string flavor, string os, string architectu
}
}

[Theory]
[MemberData(nameof(RuntimeComponents))]
public void DnuPack_NormalizesVersionNumber(string flavor, string os, string architecture)
{
string expectedNupkg =
@"{0} -> {1}/bin/Debug/{0}.1.0.0.nupkg".Replace('/', Path.DirectorySeparatorChar);
string expectedSymbol =
@"{0} -> {1}/bin/Debug/{0}.1.0.0.symbols.nupkg".Replace('/', Path.DirectorySeparatorChar);
string stdOut;
string stdErr;
var runtimeHomeDir = TestUtils.GetRuntimeHomeDir(flavor, os, architecture);
int exitCode;

using (var testEnv = new DnuTestEnvironment(runtimeHomeDir))
{
File.WriteAllText($"{testEnv.RootDir}/project.json",
@"{
""version"": ""1.0.0.0"",
""frameworks"": {
""dnx451"": {}
}
}");

exitCode = DnuTestUtils.ExecDnu(runtimeHomeDir, "restore", "", out stdOut, out stdErr, workingDir: testEnv.RootDir);
Assert.Equal(0, exitCode);

exitCode = DnuTestUtils.ExecDnu(runtimeHomeDir, "pack", "", out stdOut, out stdErr, workingDir: testEnv.RootDir);
Assert.Empty(stdErr);
Assert.Contains(string.Format(expectedNupkg, Path.GetFileName(testEnv.RootDir), testEnv.RootDir), stdOut);
Assert.Contains(string.Format(expectedSymbol, Path.GetFileName(testEnv.RootDir), testEnv.RootDir), stdOut);
Assert.Equal(0, exitCode);
}
}

[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
9 changes: 3 additions & 6 deletions test/Microsoft.Dnx.Tooling.FunctionalTests/DnuPublishTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -468,9 +468,7 @@ public void DnuPublishConsoleAppWithNoSourceOptionNormalizesVersionNumber(string
'PROJECT_NAME': {
'1.0.0': {
'.': ['PROJECT_NAME.1.0.0.nupkg', 'PROJECT_NAME.1.0.0.nupkg.sha512', 'PROJECT_NAME.nuspec'],
'root': {
'.': ['Config.json', 'project.json', 'project.lock.json']
},
'root': ['Config.json', 'project.json', 'project.lock.json'],
'lib': {
'dnx451': ['PROJECT_NAME.dll', 'PROJECT_NAME.xml']
}
Expand All @@ -494,15 +492,14 @@ public void DnuPublishConsoleAppWithNoSourceOptionNormalizesVersionNumber(string
var exitCode = DnuTestUtils.ExecDnu(
runtimeHomeDir,
subcommand: "restore",
arguments: "",
arguments: string.Empty,
workingDir: testEnv.ProjectPath);
Assert.Equal(0, exitCode);

exitCode = DnuTestUtils.ExecDnu(
runtimeHomeDir,
subcommand: "publish",
arguments: string.Format("--no-source --out {0}",
testEnv.PublishOutputDirPath),
arguments: $"--no-source --out {testEnv.PublishOutputDirPath}",
workingDir: testEnv.ProjectPath);
Assert.Equal(0, exitCode);

Expand Down
46 changes: 0 additions & 46 deletions test/Microsoft.Dnx.Tooling.FunctionalTests/DnuRestoreTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,51 +189,5 @@ public void DnuRestore_ReinstallsCorruptedPackage(string flavor, string os, stri
Assert.True(File.Exists(nuspecPath));
}
}

[Theory]
[MemberData(nameof(RuntimeComponents))]
public void DnuRestore_ReinstallsPackageWithNormalizedVersion(string flavor, string os, string architecture)
{
var runtimeHomePath = _fixture.GetRuntimeHomeDir(flavor, os, architecture);
using (var tempDir = new DisposableDir())
{
var projectDir = Path.Combine(tempDir, "project");
var packagesDir = Path.Combine(tempDir, "packages");
var projectJson = Path.Combine(projectDir, Runtime.Project.ProjectFileName);

Directory.CreateDirectory(projectDir);
File.WriteAllText(projectJson, @"
{
""dependencies"": {
""alpha"": ""0.1.0""
}
}");
DnuTestUtils.ExecDnu(
runtimeHomePath,
subcommand: "restore",
arguments: $"{projectDir} -s {_fixture.PackageSource} --packages {packagesDir}");

// rename package folder to an unnormalized string
Directory.Move(Path.Combine(packagesDir, "alpha", "0.1.0"),
Path.Combine(packagesDir, "alpha", "0.1.0.0"));

// ensure the directory is renamed
Assert.False(Directory.Exists(Path.Combine(packagesDir, "alpha", "0.1.0")));

string stdOut, stdErr;
var exitCode = DnuTestUtils.ExecDnu(
runtimeHomePath,
subcommand: "restore",
arguments: $"{projectDir} -s {_fixture.PackageSource} --packages {packagesDir}",
stdOut: out stdOut,
stdErr: out stdErr);

Assert.Equal(0, exitCode);
Assert.Empty(stdErr);
Assert.Contains($"Installing alpha.0.1.0", stdOut);
Assert.True(Directory.Exists(Path.Combine(packagesDir, "alpha", "0.1.0")));
Assert.True(File.Exists(Path.Combine(packagesDir, "alpha", "0.1.0", $"alpha{Constants.ManifestExtension}")));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void Save()

var xElemVersion = xElemMetadata.Element(ns + "version");
Assert.NotNull(xElemVersion);
Assert.Equal(version.OriginalString, xElemVersion.Value);
Assert.Equal(version.ToString(), xElemVersion.Value);

var xElemRequireLicense = xElemMetadata.Element(ns + "requireLicenseAcceptance");
Assert.NotNull(xElemRequireLicense);
Expand Down

0 comments on commit 69f4017

Please sign in to comment.