|
| 1 | +// Copyright (c) DEMA Consulting |
| 2 | +// |
| 3 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | +// of this software and associated documentation files (the "Software"), to deal |
| 5 | +// in the Software without restriction, including without limitation the rights |
| 6 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | +// copies of the Software, and to permit persons to whom the Software is |
| 8 | +// furnished to do so, subject to the following conditions: |
| 9 | +// |
| 10 | +// The above copyright notice and this permission notice shall be included in all |
| 11 | +// copies or substantial portions of the Software. |
| 12 | +// |
| 13 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 19 | +// SOFTWARE. |
| 20 | + |
| 21 | +using System.IO.Compression; |
| 22 | +using DemaConsulting.NuGetInstaller.Cli; |
| 23 | + |
| 24 | +namespace DemaConsulting.NuGetInstaller.Tests; |
| 25 | + |
| 26 | +/// <summary> |
| 27 | +/// End-to-end tests proving that <see cref="Program.Run"/> derives a <c>configRoot</c> from |
| 28 | +/// the <c>packages.config</c> location and forwards it through to NuGet settings discovery, |
| 29 | +/// so a project/repo-local <c>nuget.config</c> is found the same way <c>dotnet restore</c> |
| 30 | +/// finds it - regardless of the process's actual current working directory. |
| 31 | +/// </summary> |
| 32 | +/// <remarks> |
| 33 | +/// This is a regression test for GitHub issue #37: <c>NuGetCache.EnsureCachedAsync</c> |
| 34 | +/// previously ignored any repo-local <c>nuget.config</c> because no root directory was |
| 35 | +/// forwarded to <c>Settings.LoadDefaultSettings</c>. Each test here builds an isolated |
| 36 | +/// temporary "repository" containing a <c>nuget.config</c> at its root (defining a local |
| 37 | +/// folder package source and a private global packages folder, so no real machine state |
| 38 | +/// is touched) and a nested <c>packages.config</c> in a subdirectory - mirroring the |
| 39 | +/// <c>HiArc.LaunchCode.HardwareSim</c> layout where <c>nuget.config</c> lives several |
| 40 | +/// directories above the project's <c>packages.config</c>. |
| 41 | +/// </remarks> |
| 42 | +public class ProgramConfigRootTests |
| 43 | +{ |
| 44 | + /// <summary> |
| 45 | + /// Test that <c>Program.Run</c> installs a package from a source defined only in a |
| 46 | + /// repo-local <c>nuget.config</c> located in an ancestor of the <c>packages.config</c> |
| 47 | + /// directory, proving the derived <c>configRoot</c> is honored end-to-end. |
| 48 | + /// </summary> |
| 49 | + [Fact] |
| 50 | + public void Program_Run_WithRepoLocalNugetConfigAboveNestedPackagesConfig_InstallsFromLocalSource() |
| 51 | + { |
| 52 | + // Arrange - build an isolated "repository" layout: |
| 53 | + // repoRoot/ |
| 54 | + // nuget.config <- defines the only source that can resolve the package |
| 55 | + // feed/{id}.{version}.nupkg <- the package content |
| 56 | + // project/packages.config <- references the package; several levels of walk-up |
| 57 | + // from here reach nuget.config, matching the real |
| 58 | + // HardwareSim repo layout |
| 59 | + // packages/ <- installer output directory |
| 60 | + // global-packages/ <- private global packages folder (keeps this test |
| 61 | + // from touching the real machine-wide NuGet cache) |
| 62 | + var repoRoot = Path.Combine(Path.GetTempPath(), $"nuget_installer_config_root_test_{Guid.NewGuid():N}"); |
| 63 | + var feedDir = Path.Combine(repoRoot, "feed"); |
| 64 | + var projectDir = Path.Combine(repoRoot, "project"); |
| 65 | + var outputDir = Path.Combine(repoRoot, "packages"); |
| 66 | + var globalPackagesDir = Path.Combine(repoRoot, "global-packages"); |
| 67 | + var packageId = $"Test.ConfigRootDiscovery.{Guid.NewGuid():N}"; |
| 68 | + const string version = "1.0.0"; |
| 69 | + |
| 70 | + try |
| 71 | + { |
| 72 | + Directory.CreateDirectory(feedDir); |
| 73 | + Directory.CreateDirectory(projectDir); |
| 74 | + Directory.CreateDirectory(globalPackagesDir); |
| 75 | + |
| 76 | + // Write the package into the local folder feed |
| 77 | + File.WriteAllBytes( |
| 78 | + Path.Combine(feedDir, $"{packageId}.{version}.nupkg"), |
| 79 | + CreateMinimalNupkgBytes(packageId, version)); |
| 80 | + |
| 81 | + // Write a repo-local nuget.config at the repo root defining the local feed as the |
| 82 | + // only source and a private global packages folder |
| 83 | + File.WriteAllText(Path.Combine(repoRoot, "nuget.config"), $""" |
| 84 | + <?xml version="1.0" encoding="utf-8"?> |
| 85 | + <configuration> |
| 86 | + <config> |
| 87 | + <add key="globalPackagesFolder" value="{globalPackagesDir}" /> |
| 88 | + </config> |
| 89 | + <packageSources> |
| 90 | + <clear /> |
| 91 | + <add key="repo-local-feed" value="{feedDir}" /> |
| 92 | + </packageSources> |
| 93 | + </configuration> |
| 94 | + """); |
| 95 | + |
| 96 | + // Write packages.config nested below the repo root, matching how HardwareSim's |
| 97 | + // packages.config sits several directories below its repo-root nuget.config |
| 98 | + var configPath = Path.Combine(projectDir, "packages.config"); |
| 99 | + File.WriteAllText(configPath, $""" |
| 100 | + <?xml version="1.0" encoding="utf-8"?> |
| 101 | + <packages> |
| 102 | + <package id="{packageId}" version="{version}" /> |
| 103 | + </packages> |
| 104 | + """); |
| 105 | + |
| 106 | + var originalOut = Console.Out; |
| 107 | + try |
| 108 | + { |
| 109 | + using var outWriter = new StringWriter(); |
| 110 | + Console.SetOut(outWriter); |
| 111 | + using var context = Context.Create([configPath, "-o", outputDir]); |
| 112 | + |
| 113 | + // Act - run the program; the process's actual working directory is unrelated to |
| 114 | + // repoRoot, so success can only be explained by Program deriving configRoot from |
| 115 | + // the packages.config location and NuGetCache honoring it |
| 116 | + Program.Run(context); |
| 117 | + |
| 118 | + // Assert - installation succeeded and the package was extracted |
| 119 | + Assert.Equal(0, context.ExitCode); |
| 120 | + var expectedFolder = Path.Combine(outputDir, $"{packageId}.{version}"); |
| 121 | + Assert.True( |
| 122 | + Directory.Exists(expectedFolder), |
| 123 | + $"Expected package folder to exist at: {expectedFolder}"); |
| 124 | + } |
| 125 | + finally |
| 126 | + { |
| 127 | + Console.SetOut(originalOut); |
| 128 | + } |
| 129 | + } |
| 130 | + finally |
| 131 | + { |
| 132 | + if (Directory.Exists(repoRoot)) |
| 133 | + { |
| 134 | + Directory.Delete(repoRoot, recursive: true); |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + /// <summary> |
| 140 | + /// Test that <c>Program.Run</c> fails to resolve a package whose only source is defined |
| 141 | + /// in a <c>nuget.config</c> that does not exist anywhere above the <c>packages.config</c> |
| 142 | + /// directory, proving the previous test's success is due to genuine discovery of the |
| 143 | + /// repo-local config rather than an unrelated fallback. |
| 144 | + /// </summary> |
| 145 | + [Fact] |
| 146 | + public void Program_Run_WithoutRepoLocalNugetConfig_PackageNotFound_ThrowsInvalidOperationException() |
| 147 | + { |
| 148 | + // Arrange - same nested packages.config as above, but with no nuget.config anywhere in |
| 149 | + // the temp tree, so the package (which does not exist on any real source) cannot resolve |
| 150 | + var repoRoot = Path.Combine(Path.GetTempPath(), $"nuget_installer_config_root_test_{Guid.NewGuid():N}"); |
| 151 | + var projectDir = Path.Combine(repoRoot, "project"); |
| 152 | + var outputDir = Path.Combine(repoRoot, "packages"); |
| 153 | + var packageId = $"Test.ConfigRootDiscovery.{Guid.NewGuid():N}"; |
| 154 | + const string version = "1.0.0"; |
| 155 | + |
| 156 | + try |
| 157 | + { |
| 158 | + Directory.CreateDirectory(projectDir); |
| 159 | + |
| 160 | + var configPath = Path.Combine(projectDir, "packages.config"); |
| 161 | + File.WriteAllText(configPath, $""" |
| 162 | + <?xml version="1.0" encoding="utf-8"?> |
| 163 | + <packages> |
| 164 | + <package id="{packageId}" version="{version}" /> |
| 165 | + </packages> |
| 166 | + """); |
| 167 | + |
| 168 | + var originalOut = Console.Out; |
| 169 | + try |
| 170 | + { |
| 171 | + using var outWriter = new StringWriter(); |
| 172 | + Console.SetOut(outWriter); |
| 173 | + using var context = Context.Create([configPath, "-o", outputDir]); |
| 174 | + |
| 175 | + // Act & Assert - with no repo-local source defining this package, resolution must |
| 176 | + // fail; NuGetCache.EnsureCachedAsync surfaces this as InvalidOperationException, |
| 177 | + // which Program.Run (called directly, bypassing Main's try/catch) propagates |
| 178 | + Assert.Throws<InvalidOperationException>(() => Program.Run(context)); |
| 179 | + } |
| 180 | + finally |
| 181 | + { |
| 182 | + Console.SetOut(originalOut); |
| 183 | + } |
| 184 | + } |
| 185 | + finally |
| 186 | + { |
| 187 | + if (Directory.Exists(repoRoot)) |
| 188 | + { |
| 189 | + Directory.Delete(repoRoot, recursive: true); |
| 190 | + } |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + /// <summary> |
| 195 | + /// Builds a minimal valid .nupkg byte array for the given package identity using only |
| 196 | + /// <see cref="ZipArchive"/> (no NuGet SDK package-building types are referenced by this |
| 197 | + /// test project). NuGet's local-folder feed resolution only requires a top-level |
| 198 | + /// <c>*.nuspec</c> entry containing valid package metadata; full OPC parts |
| 199 | + /// (<c>[Content_Types].xml</c>, relationship parts) are not required for reading. |
| 200 | + /// </summary> |
| 201 | + /// <param name="packageId">The NuGet package identifier.</param> |
| 202 | + /// <param name="version">The package version string.</param> |
| 203 | + /// <returns>A byte array containing a minimal .nupkg archive.</returns> |
| 204 | + private static byte[] CreateMinimalNupkgBytes(string packageId, string version) |
| 205 | + { |
| 206 | + using var stream = new MemoryStream(); |
| 207 | + using (var archive = new ZipArchive(stream, ZipArchiveMode.Create, leaveOpen: true)) |
| 208 | + { |
| 209 | + // Top-level nuspec entry - required by NuGet's package reader to identify the package |
| 210 | + var nuspecEntry = archive.CreateEntry($"{packageId}.nuspec"); |
| 211 | + using (var entryStream = nuspecEntry.Open()) |
| 212 | + using (var writer = new StreamWriter(entryStream)) |
| 213 | + { |
| 214 | + writer.Write($""" |
| 215 | + <?xml version="1.0" encoding="utf-8"?> |
| 216 | + <package xmlns="http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd"> |
| 217 | + <metadata> |
| 218 | + <id>{packageId}</id> |
| 219 | + <version>{version}</version> |
| 220 | + <authors>Test</authors> |
| 221 | + <description>Minimal test package for configRoot discovery tests.</description> |
| 222 | + </metadata> |
| 223 | + </package> |
| 224 | + """); |
| 225 | + } |
| 226 | + |
| 227 | + // A placeholder content file so the extracted package folder is non-empty |
| 228 | + var contentEntry = archive.CreateEntry("lib/net8.0/_placeholder.txt"); |
| 229 | + using (var entryStream = contentEntry.Open()) |
| 230 | + using (var writer = new StreamWriter(entryStream)) |
| 231 | + { |
| 232 | + writer.Write("placeholder"); |
| 233 | + } |
| 234 | + } |
| 235 | + |
| 236 | + return stream.ToArray(); |
| 237 | + } |
| 238 | +} |
0 commit comments