From 0253895b3d98b7f2051f529bef98d84142d69d01 Mon Sep 17 00:00:00 2001 From: James A Sutherland <> Date: Tue, 14 Nov 2023 15:44:08 -0600 Subject: [PATCH] Update package list check logic --- .../PackageListIsCorrectTests.cs | 49 +++++++------------ 1 file changed, 18 insertions(+), 31 deletions(-) diff --git a/LoadModules.Extensions.Tests/PackageListIsCorrectTests.cs b/LoadModules.Extensions.Tests/PackageListIsCorrectTests.cs index f325ee2..d93fd50 100644 --- a/LoadModules.Extensions.Tests/PackageListIsCorrectTests.cs +++ b/LoadModules.Extensions.Tests/PackageListIsCorrectTests.cs @@ -1,9 +1,3 @@ -// Copyright (c) The University of Dundee 2018-2019 -// This file is part of the Research Data Management Platform (RDMP). -// RDMP is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. -// RDMP is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. -// You should have received a copy of the GNU General Public License along with RDMP. If not, see . - using System; using System.Collections.Generic; using System.IO; @@ -15,23 +9,19 @@ namespace LoadModules.Extensions.Tests; /// -/// Tests to confirm that the dependencies in csproj files (NuGet packages) match those in the .nuspec files and that packages.md +/// Tests to confirm that the dependencies in csproj files (NuGet packages) match those in the .nuspec files and that packages.md /// lists the correct versions (in documentation) /// public class PackageListIsCorrectTests { - private static readonly EnumerationOptions EnumerationOptions = new() - { RecurseSubdirectories = true, MatchCasing = MatchCasing.CaseInsensitive, IgnoreInaccessible = true }; + private static readonly EnumerationOptions EnumerationOptions = new() { RecurseSubdirectories = true,MatchCasing = MatchCasing.CaseInsensitive,IgnoreInaccessible = true}; // - private static readonly Regex RPackageRef = - new(@" @@ -39,17 +29,17 @@ public class PackageListIsCorrectTests /// /// [TestCase] - public void TestPackagesDocumentCorrect(string rootPath = null) + public void TestPackagesDocumentCorrect(string rootPath=null) { - var root = FindRoot(rootPath); + var root= FindRoot(rootPath); var undocumented = new StringBuilder(); // Extract the named packages from PACKAGES.md - var packagesMarkdown = File.ReadAllLines(GetPackagesMarkdown(root)) + var packagesMarkdown = GetPackagesMarkdown(root).SelectMany(File.ReadAllLines) .Select(line => RMarkdownEntry.Match(line)) - .Where(m => m.Success) - .Skip(2) // Jump over the header + .Where(m=>m.Success) .Select(m => m.Groups[1].Value) + .Except(new[]{"Package", "-------" }) .ToHashSet(StringComparer.InvariantCultureIgnoreCase); // Extract the named packages from csproj files @@ -57,14 +47,13 @@ public void TestPackagesDocumentCorrect(string rootPath = null) .Select(m => m.Groups[1].Value).ToHashSet(StringComparer.InvariantCultureIgnoreCase); // Then subtract those listed in PACKAGES.md (should be empty) - var undocumentedPackages = usedPackages.Except(packagesMarkdown).Select(BuildRecommendedMarkdownLine).ToList(); + var undocumentedPackages = usedPackages.Except(packagesMarkdown).Select(BuildRecommendedMarkdownLine); undocumented.AppendJoin(Environment.NewLine, undocumentedPackages); var unusedPackages = packagesMarkdown.Except(usedPackages).ToArray(); Assert.IsEmpty(unusedPackages, $"The following packages are listed in PACKAGES.md but are not used in any csproj file: {string.Join(", ", unusedPackages)}"); - Assert.Zero(undocumentedPackages.Count, - $"One or more packages not documented in PACKAGES.md. Recommended addition:{Environment.NewLine}{undocumented}"); + Assert.IsEmpty(undocumented.ToString()); } /// @@ -72,8 +61,7 @@ public void TestPackagesDocumentCorrect(string rootPath = null) /// /// /// - private static object BuildRecommendedMarkdownLine(string package) => - $"| {package} | [GitHub]() | LICENCE GOES HERE | |"; + private static object BuildRecommendedMarkdownLine(string package) => $"Package {package} is not documented in PACKAGES.md. Recommended line is:\r\n| {package} | [GitHub]() | LICENCE GOES HERE | |"; /// /// Find the root of this repo, which is usually the directory containing the .sln file @@ -88,7 +76,6 @@ private static DirectoryInfo FindRoot(string path = null) if (!Path.IsPathRooted(path)) path = Path.Combine(TestContext.CurrentContext.TestDirectory, path); return new DirectoryInfo(path); } - var root = new DirectoryInfo(TestContext.CurrentContext.TestDirectory); while (!root.EnumerateFiles("*.sln", SearchOption.TopDirectoryOnly).Any() && root.Parent != null) root = root.Parent; @@ -103,8 +90,7 @@ private static DirectoryInfo FindRoot(string path = null) /// private static IEnumerable GetCsprojFiles(DirectoryInfo root) { - return root.EnumerateFiles("*.csproj", EnumerationOptions).Select(f => f.FullName) - .Where(f => !f.Contains("tests", StringComparison.InvariantCultureIgnoreCase)); + return root.EnumerateFiles("*.csproj", EnumerationOptions).Select(f => f.FullName).Where(f => !f.Contains("tests", StringComparison.InvariantCultureIgnoreCase)); } /// @@ -112,10 +98,11 @@ private static IEnumerable GetCsprojFiles(DirectoryInfo root) /// /// /// - private static string GetPackagesMarkdown(DirectoryInfo root) + private static string[] GetPackagesMarkdown(DirectoryInfo root) { - var path = root.EnumerateFiles("packages.md", EnumerationOptions).Select(f => f.FullName).SingleOrDefault(); - Assert.IsNotNull(path, "Could not find packages.md"); + var path = root.EnumerateFiles("packages.md", EnumerationOptions).Select(f => f.FullName).ToArray(); + Assert.False(path.Length==0, "Could not find packages.md"); return path; } -} \ No newline at end of file + +}