From 4b6b1af7fd31dea4926e6b20c52b54b71ed7a0a8 Mon Sep 17 00:00:00 2001 From: Steve Desmond Date: Wed, 24 Nov 2021 16:25:57 -0500 Subject: [PATCH] Show more helpful message when package version parsing fails --- src/LibYear.Core/FileTypes/XmlProjectFile.cs | 13 +++++-- src/LibYear.Core/PackageVersion.cs | 35 +++++-------------- .../FileTypes/CsProjFileTests.cs | 23 ++++++++++++ .../FileTypes/DirectoryBuildPropsTests.cs | 23 ++++++++++++ .../FileTypes/DirectoryBuildTargetsTests.cs | 23 ++++++++++++ .../FileTypes/PackagesConfigFileTests.cs | 23 ++++++++++++ 6 files changed, 111 insertions(+), 29 deletions(-) diff --git a/src/LibYear.Core/FileTypes/XmlProjectFile.cs b/src/LibYear.Core/FileTypes/XmlProjectFile.cs index 8e0086e..3e6d2e3 100644 --- a/src/LibYear.Core/FileTypes/XmlProjectFile.cs +++ b/src/LibYear.Core/FileTypes/XmlProjectFile.cs @@ -39,10 +39,19 @@ public string Update(IEnumerable 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) diff --git a/src/LibYear.Core/PackageVersion.cs b/src/LibYear.Core/PackageVersion.cs index 1094320..dce5c36 100644 --- a/src/LibYear.Core/PackageVersion.cs +++ b/src/LibYear.Core/PackageVersion.cs @@ -13,7 +13,6 @@ public PackageVersion(string version) { IsWildcard = true; } - } public PackageVersion(NuGetVersion? version) @@ -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(); } diff --git a/test/LibYear.Core.Tests/FileTypes/CsProjFileTests.cs b/test/LibYear.Core.Tests/FileTypes/CsProjFileTests.cs index d0ae3d6..5caf9e4 100644 --- a/test/LibYear.Core.Tests/FileTypes/CsProjFileTests.cs +++ b/test/LibYear.Core.Tests/FileTypes/CsProjFileTests.cs @@ -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); + } + } } \ No newline at end of file diff --git a/test/LibYear.Core.Tests/FileTypes/DirectoryBuildPropsTests.cs b/test/LibYear.Core.Tests/FileTypes/DirectoryBuildPropsTests.cs index 118cd95..04bed10 100644 --- a/test/LibYear.Core.Tests/FileTypes/DirectoryBuildPropsTests.cs +++ b/test/LibYear.Core.Tests/FileTypes/DirectoryBuildPropsTests.cs @@ -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); + } + } } \ No newline at end of file diff --git a/test/LibYear.Core.Tests/FileTypes/DirectoryBuildTargetsTests.cs b/test/LibYear.Core.Tests/FileTypes/DirectoryBuildTargetsTests.cs index 0402d4b..4a82a6f 100644 --- a/test/LibYear.Core.Tests/FileTypes/DirectoryBuildTargetsTests.cs +++ b/test/LibYear.Core.Tests/FileTypes/DirectoryBuildTargetsTests.cs @@ -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); + } + } } \ No newline at end of file diff --git a/test/LibYear.Core.Tests/FileTypes/PackagesConfigFileTests.cs b/test/LibYear.Core.Tests/FileTypes/PackagesConfigFileTests.cs index 0a15170..a8a1882 100644 --- a/test/LibYear.Core.Tests/FileTypes/PackagesConfigFileTests.cs +++ b/test/LibYear.Core.Tests/FileTypes/PackagesConfigFileTests.cs @@ -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); + } + } } \ No newline at end of file