Skip to content

Commit

Permalink
Show more helpful message when package version parsing fails
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveDesmond-ca committed Nov 24, 2021
1 parent 51377ab commit 4b6b1af
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 29 deletions.
13 changes: 11 additions & 2 deletions src/LibYear.Core/FileTypes/XmlProjectFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,19 @@ public string Update(IEnumerable<Result> results)
return _xmlContents.ToString();
}

private static PackageVersion? ParseCurrentVersion(XElement element, string versionAttributeName)
private PackageVersion? ParseCurrentVersion(XElement element, string versionAttributeName)
{
var version = element.Attribute(versionAttributeName)?.Value ?? element.Element(versionAttributeName)?.Value ?? string.Empty;
return !string.IsNullOrEmpty(version) ? new PackageVersion(version) : null;
try
{
return !string.IsNullOrEmpty(version)
? new PackageVersion(version)
: null;
}
catch (Exception e)
{
throw new ArgumentException($"Could not parse version {version} of {element} in {FileName}", e);
}
}

private void UpdateElement(XElement element, string latestVersion)
Expand Down
35 changes: 8 additions & 27 deletions src/LibYear.Core/PackageVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ public PackageVersion(string version)
{
IsWildcard = true;
}

}

public PackageVersion(NuGetVersion? version)
Expand All @@ -31,38 +30,20 @@ public PackageVersion(int major, int minor, int patch, int revision)
{
}

private new static PackageVersion? Parse(string version)
{
if (version.Equals("*"))
{
return new PackageVersion(0, 0, 0);
}

try
{
var nuGetVersion = new NuGetVersion(version);
return new PackageVersion(nuGetVersion);
}
catch
{
return null;
}
}
private new static PackageVersion Parse(string version)
=> version.Equals("*")
? new PackageVersion(0, 0, 0)
: new PackageVersion(new NuGetVersion(version));

public override string ToString()
{
if (string.IsNullOrEmpty(OriginalVersion) || IsSemVer2)
{
return ToString("N", VersionFormatter.Instance);
}

return OriginalVersion;
}
=> string.IsNullOrEmpty(OriginalVersion) || IsSemVer2
? ToString("N", VersionFormatter.Instance)
: OriginalVersion;

public override string ToString(string format, IFormatProvider? formatProvider)
{
if (formatProvider == null
|| !TryFormatter(format, formatProvider, out var formattedString))
|| !TryFormatter(format, formatProvider, out var formattedString))
{
formattedString = ToString();
}
Expand Down
23 changes: 23 additions & 0 deletions test/LibYear.Core.Tests/FileTypes/CsProjFileTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,27 @@ public async Task CanUpdateCsProjFile()
Assert.Null(newFile.Packages["test6"]);
Assert.True(newFile.Packages["test7"]!.IsWildcard);
}

[Fact]
public async Task InvalidVersionShowsInfo()
{
//arrange
var filename = Path.Combine("FileTypes", "project.csproj");
var contents = await File.ReadAllTextAsync(filename);
var newContents = contents.Replace("0.2.0", "0.2.x");

try
{
//act
_ = new CsProjFile(filename, newContents);
Assert.False(true);
}
catch (Exception e)
{
//assert
Assert.Contains("0.2.x", e.Message);
Assert.Contains("test2", e.Message);
Assert.Contains("project.csproj", e.Message);
}
}
}
23 changes: 23 additions & 0 deletions test/LibYear.Core.Tests/FileTypes/DirectoryBuildPropsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,27 @@ public async Task CanUpdateDirectoryBuildPropsFile()
Assert.Equal("2.3.4", newFile.Packages.Skip(1).First().Value!.ToString());
Assert.Equal("3.4.5", newFile.Packages.Skip(2).First().Value!.ToString());
}

[Fact]
public async Task InvalidVersionShowsInfo()
{
//arrange
var filename = Path.Combine("FileTypes", "Directory.Build.props");
var contents = await File.ReadAllTextAsync(filename);
var newContents = contents.Replace("0.2.0", "0.2.x");

try
{
//act
_ = new DirectoryBuildPropsFile(filename, newContents);
Assert.False(true);
}
catch (Exception e)
{
//assert
Assert.Contains("0.2.x", e.Message);
Assert.Contains("test2", e.Message);
Assert.Contains("Directory.Build.props", e.Message);
}
}
}
23 changes: 23 additions & 0 deletions test/LibYear.Core.Tests/FileTypes/DirectoryBuildTargetsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,27 @@ public async Task CanUpdateDirectoryTargetsFile()
Assert.Equal("2.3.4", newFile.Packages.Skip(1).First().Value!.ToString());
Assert.Equal("3.4.5", newFile.Packages.Skip(2).First().Value!.ToString());
}

[Fact]
public async Task InvalidVersionShowsInfo()
{
//arrange
var filename = Path.Combine("FileTypes", "Directory.Build.targets");
var contents = await File.ReadAllTextAsync(filename);
var newContents = contents.Replace("0.2.0", "0.2.x");

try
{
//act
_ = new DirectoryBuildTargetsFile(filename, newContents);
Assert.False(true);
}
catch (Exception e)
{
//assert
Assert.Contains("0.2.x", e.Message);
Assert.Contains("test2", e.Message);
Assert.Contains("Directory.Build.targets", e.Message);
}
}
}
23 changes: 23 additions & 0 deletions test/LibYear.Core.Tests/FileTypes/PackagesConfigFileTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,27 @@ public async Task CanUpdatePackagesConfigFile()
Assert.Equal("2.3.4", newFile.Packages.Skip(1).First().Value!.ToString());
Assert.Equal("3.4.5", newFile.Packages.Skip(2).First().Value!.ToString());
}

[Fact]
public async Task InvalidVersionShowsInfo()
{
//arrange
var filename = Path.Combine("FileTypes", "packages.config");
var contents = await File.ReadAllTextAsync(filename);
var newContents = contents.Replace("0.2.0", "0.2.x");

try
{
//act
_ = new PackagesConfigFile(filename, newContents);
Assert.False(true);
}
catch (Exception e)
{
//assert
Assert.Contains("0.2.x", e.Message);
Assert.Contains("test2", e.Message);
Assert.Contains("packages.config", e.Message);
}
}
}

0 comments on commit 4b6b1af

Please sign in to comment.