|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using FluentAssertions; |
| 5 | +using System; |
| 6 | +using System.Collections.Generic; |
| 7 | +using System.Diagnostics; |
| 8 | +using System.IO; |
| 9 | +using System.Linq; |
| 10 | +using Xunit; |
| 11 | +using Xunit.Abstractions; |
| 12 | + |
| 13 | +namespace Microsoft.DotNet.MacOsPkg.Tests |
| 14 | +{ |
| 15 | + public class UnpackPackTests |
| 16 | + { |
| 17 | + private readonly ITestOutputHelper output; |
| 18 | + private static readonly string simplePkg = GetResourceFilePath("Simple.pkg"); |
| 19 | + private static readonly string withAppPkg = GetResourceFilePath("WithApp.pkg"); |
| 20 | + private static readonly string simpleInstallerPkg = GetResourceFilePath("SimpleInstaller.pkg"); |
| 21 | + private static readonly string withAppInstallerPkg = GetResourceFilePath("WithAppInstaller.pkg"); |
| 22 | + |
| 23 | + private static readonly string pkgToolPath = Path.Combine( |
| 24 | + Path.GetDirectoryName(typeof(UnpackPackTests).Assembly.Location)!, |
| 25 | + "tools", |
| 26 | + "macospkg", |
| 27 | + "Microsoft.Dotnet.MacOsPkg.dll"); |
| 28 | + |
| 29 | + private static readonly string[] simplePkgFiles = |
| 30 | + [ |
| 31 | + "Bom", |
| 32 | + "PackageInfo", |
| 33 | + Path.Combine("Payload", "Sample.txt") |
| 34 | + ]; |
| 35 | + |
| 36 | + private static readonly string[] withAppPkgFiles = |
| 37 | + [ |
| 38 | + "Bom", |
| 39 | + "PackageInfo", |
| 40 | + Path.Combine("Payload", "test.app") |
| 41 | + ]; |
| 42 | + |
| 43 | + private static readonly string[] appFiles = |
| 44 | + [ |
| 45 | + Path.Combine("Contents", "Info.plist"), |
| 46 | + Path.Combine("Contents", "MacOS", "main"), |
| 47 | + Path.Combine("Contents", "Resources", "libexample.dylib") |
| 48 | + ]; |
| 49 | + |
| 50 | + private static readonly string[] simpleInstallerFiles = |
| 51 | + [ |
| 52 | + "Distribution", |
| 53 | + "Simple.pkg" |
| 54 | + ]; |
| 55 | + |
| 56 | + private static readonly string[] withAppInstallerFiles = |
| 57 | + [ |
| 58 | + "Distribution", |
| 59 | + "WithApp.pkg" |
| 60 | + ]; |
| 61 | + |
| 62 | + public UnpackPackTests(ITestOutputHelper output) => this.output = output; |
| 63 | + |
| 64 | + [MacOSOnlyFact] |
| 65 | + public void UnpackPackSimplePkg() |
| 66 | + { |
| 67 | + string unpackPath = Path.GetTempFileName(); |
| 68 | + string packPath = GetTempPkgPath(); |
| 69 | + |
| 70 | + ExecuteWithCleanup(() => |
| 71 | + { |
| 72 | + Unpack(simplePkg, unpackPath, simplePkgFiles); |
| 73 | + Pack(unpackPath, packPath, simplePkgFiles); |
| 74 | + }, [ unpackPath, packPath ]); |
| 75 | + } |
| 76 | + |
| 77 | + [MacOSOnlyFact] |
| 78 | + public void UnpackPackWithAppPkg() |
| 79 | + { |
| 80 | + string unpackPath = Path.GetTempFileName(); |
| 81 | + string packPath = GetTempPkgPath(); |
| 82 | + |
| 83 | + ExecuteWithCleanup(() => |
| 84 | + { |
| 85 | + Unpack(withAppPkg, unpackPath, withAppPkgFiles); |
| 86 | + Pack(unpackPath, packPath, withAppPkgFiles); |
| 87 | + }, [ unpackPath, packPath ]); |
| 88 | + } |
| 89 | + |
| 90 | + [MacOSOnlyFact] |
| 91 | + public void UnpackPackAppBundle() |
| 92 | + { |
| 93 | + string unpackPkgPath = Path.GetTempFileName(); |
| 94 | + string unpackAppPath = Path.GetTempFileName(); |
| 95 | + string packAppPath = GetTempAppPath(); |
| 96 | + |
| 97 | + ExecuteWithCleanup(() => |
| 98 | + { |
| 99 | + Unpack(withAppPkg, unpackPkgPath, withAppPkgFiles); |
| 100 | + Unpack(Path.Combine(unpackPkgPath, "Payload", "test.app"), unpackAppPath, appFiles); |
| 101 | + Pack(unpackAppPath, packAppPath, appFiles); |
| 102 | + }, [ unpackPkgPath, unpackAppPath ]); |
| 103 | + } |
| 104 | + |
| 105 | + [MacOSOnlyFact] |
| 106 | + public void UnpackPackSimpleInstallerPkg() |
| 107 | + { |
| 108 | + string unpackPath = Path.GetTempFileName(); |
| 109 | + string packPath = GetTempPkgPath(); |
| 110 | + |
| 111 | + ExecuteWithCleanup(() => |
| 112 | + { |
| 113 | + Unpack(simpleInstallerPkg, unpackPath, simpleInstallerFiles); |
| 114 | + Pack(unpackPath, packPath, simpleInstallerFiles); |
| 115 | + }, [ unpackPath, packPath ]); |
| 116 | + } |
| 117 | + |
| 118 | + [MacOSOnlyFact] |
| 119 | + public void UnpackPackSimplePkgInSimpleInstallerPkg() |
| 120 | + { |
| 121 | + string unpackInstallerPath = Path.GetTempFileName(); |
| 122 | + string unpackComponentPath = Path.GetTempFileName(); |
| 123 | + string packInstallerPath = GetTempPkgPath(); |
| 124 | + |
| 125 | + string componentPkgPath = Path.Combine(unpackInstallerPath, "Simple.pkg"); |
| 126 | + |
| 127 | + ExecuteWithCleanup(() => |
| 128 | + { |
| 129 | + Unpack(simpleInstallerPkg, unpackInstallerPath, simpleInstallerFiles); |
| 130 | + Unpack(componentPkgPath, unpackComponentPath, simplePkgFiles); |
| 131 | + Pack(unpackComponentPath, componentPkgPath, simplePkgFiles); |
| 132 | + Pack(unpackInstallerPath, packInstallerPath, simpleInstallerFiles); |
| 133 | + }, [ unpackInstallerPath, unpackComponentPath, packInstallerPath ]); |
| 134 | + } |
| 135 | + |
| 136 | + [MacOSOnlyFact] |
| 137 | + public void UnpackPackAppBundleAndWithAppPkgInWithAppInstallerPkg() |
| 138 | + { |
| 139 | + string unpackInstallerPath = Path.GetTempFileName(); |
| 140 | + string unpackComponentPath = Path.GetTempFileName(); |
| 141 | + string unpackAppPath = Path.GetTempFileName(); |
| 142 | + string packInstallerPath = GetTempPkgPath(); |
| 143 | + |
| 144 | + string componentPkgPath = Path.Combine(unpackInstallerPath, "WithApp.pkg"); |
| 145 | + string appPath = Path.Combine(unpackComponentPath, "Payload", "test.app"); |
| 146 | + |
| 147 | + ExecuteWithCleanup(() => |
| 148 | + { |
| 149 | + Unpack(withAppInstallerPkg, unpackInstallerPath, withAppInstallerFiles); |
| 150 | + Unpack(componentPkgPath, unpackComponentPath, withAppPkgFiles); |
| 151 | + Unpack(appPath, unpackAppPath, appFiles); |
| 152 | + Pack(unpackAppPath, appPath, appFiles); |
| 153 | + Pack(unpackComponentPath, componentPkgPath, withAppPkgFiles); |
| 154 | + Pack(unpackInstallerPath, packInstallerPath, withAppInstallerFiles); |
| 155 | + }, [ unpackInstallerPath, unpackComponentPath, unpackAppPath, packInstallerPath ]); |
| 156 | + } |
| 157 | + |
| 158 | + private static void ExecuteWithCleanup(Action action, List<string> cleanupPaths) |
| 159 | + { |
| 160 | + try |
| 161 | + { |
| 162 | + action(); |
| 163 | + } |
| 164 | + finally |
| 165 | + { |
| 166 | + foreach (string path in cleanupPaths) |
| 167 | + { |
| 168 | + if (Directory.Exists(path)) |
| 169 | + { |
| 170 | + Directory.Delete(path, true); |
| 171 | + } |
| 172 | + else if (File.Exists(path)) |
| 173 | + { |
| 174 | + File.Delete(path); |
| 175 | + } |
| 176 | + } |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + private void Unpack(string srcPath, string dstPath, string[] expectedFiles) |
| 181 | + { |
| 182 | + RunPkgProcess(srcPath, dstPath, "unpack").Should().BeTrue(); |
| 183 | + |
| 184 | + Directory.Exists(dstPath).Should().BeTrue(); |
| 185 | + |
| 186 | + CompareContent(dstPath, expectedFiles); |
| 187 | + } |
| 188 | + |
| 189 | + private void Pack(string srcPath, string dstPath, string[] expectedFiles) |
| 190 | + { |
| 191 | + RunPkgProcess(srcPath, dstPath, "pack").Should().BeTrue(); |
| 192 | + |
| 193 | + File.Exists(dstPath).Should().BeTrue(); |
| 194 | + |
| 195 | + // Unpack the packed pkg and verify the content |
| 196 | + string unpackPath = Path.GetTempFileName(); |
| 197 | + ExecuteWithCleanup(() => |
| 198 | + { |
| 199 | + Unpack(dstPath, unpackPath, expectedFiles); |
| 200 | + }, [ unpackPath ]); |
| 201 | + } |
| 202 | + |
| 203 | + private bool RunPkgProcess(string inputPath, string outputPath, string action) |
| 204 | + { |
| 205 | + var process = Process.Start(new ProcessStartInfo() |
| 206 | + { |
| 207 | + FileName = "dotnet", |
| 208 | + Arguments = $@"exec ""{pkgToolPath}"" ""{inputPath}"" ""{outputPath}"" {action}", |
| 209 | + UseShellExecute = false, |
| 210 | + RedirectStandardError = true, |
| 211 | + }); |
| 212 | + |
| 213 | + process!.WaitForExit(60000); // 60 seconds |
| 214 | + bool success = process.ExitCode == 0; |
| 215 | + if (!success) |
| 216 | + { |
| 217 | + output.WriteLine($"Error: {process.StandardError.ReadToEnd()}"); |
| 218 | + } |
| 219 | + return success; |
| 220 | + } |
| 221 | + |
| 222 | + private static string GetResourceFilePath(string resourceName) |
| 223 | + { |
| 224 | + return Path.Combine( |
| 225 | + Path.GetDirectoryName(typeof(UnpackPackTests).Assembly.Location)!, |
| 226 | + "Resources", |
| 227 | + resourceName); |
| 228 | + } |
| 229 | + |
| 230 | + private static string GetTempPkgPath() => $"{Path.GetTempFileName()}.pkg"; |
| 231 | + |
| 232 | + private static string GetTempAppPath() => $"{Path.GetTempFileName()}.app"; |
| 233 | + |
| 234 | + private static void CompareContent(string basePath, string[] expectedFiles) |
| 235 | + { |
| 236 | + string[] actualFiles = Directory.GetFiles(basePath, "*.*", SearchOption.AllDirectories) |
| 237 | + .Select(f => f.Substring(basePath.Length + 1)) |
| 238 | + .ToArray(); |
| 239 | + actualFiles.Should().BeEquivalentTo(expectedFiles); |
| 240 | + } |
| 241 | + } |
| 242 | +} |
0 commit comments