|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 3 | + |
| 4 | +namespace Microsoft.Testing.Platform.Acceptance.IntegrationTests; |
| 5 | + |
| 6 | +[TestClass] |
| 7 | +public sealed class TestAssetTests |
| 8 | +{ |
| 9 | + public TestContext TestContext { get; set; } = null!; |
| 10 | + |
| 11 | + [TestMethod] |
| 12 | + public async Task GenerateAssetAsync_WithoutDirectory_PreparesOnlyTheOwnedRoot() |
| 13 | + { |
| 14 | + string assetName = $"{nameof(GenerateAssetAsync_WithoutDirectory_PreparesOnlyTheOwnedRoot)}_{Guid.NewGuid():N}"; |
| 15 | + using TestAsset testAsset = await TestAsset.GenerateAssetAsync( |
| 16 | + assetName, |
| 17 | + """ |
| 18 | + #file Data.txt |
| 19 | + asset |
| 20 | + """); |
| 21 | + |
| 22 | + string[] preparedRoots = [.. Directory.EnumerateDirectories(TempDirectory.TestSuiteDirectory) |
| 23 | + .Where(root => Directory.Exists(Path.Combine(root, assetName)))]; |
| 24 | + |
| 25 | + Assert.HasCount(1, preparedRoots); |
| 26 | + Assert.AreEqual(Path.GetDirectoryName(testAsset.TargetAssetPath), preparedRoots[0]); |
| 27 | + Assert.AreEqual(assetName, testAsset.AssetId); |
| 28 | + Assert.AreSequenceEqual( |
| 29 | + ["asset"], |
| 30 | + await File.ReadAllLinesAsync(Path.Combine(testAsset.TargetAssetPath, "Data.txt"), TestContext.CancellationToken)); |
| 31 | + Assert.IsTrue(File.Exists(Path.Combine(testAsset.TargetAssetPath, "NuGet.config"))); |
| 32 | + } |
| 33 | + |
| 34 | + [TestMethod] |
| 35 | + public async Task GenerateAssetAsync_WithSameName_KeepsAssetsIndependent() |
| 36 | + { |
| 37 | + string assetName = $"{nameof(GenerateAssetAsync_WithSameName_KeepsAssetsIndependent)}_{Guid.NewGuid():N}"; |
| 38 | + using TestAsset first = await TestAsset.GenerateAssetAsync( |
| 39 | + assetName, |
| 40 | + """ |
| 41 | + #file Data.txt |
| 42 | + first |
| 43 | + """, |
| 44 | + addDefaultNuGetConfigFile: false); |
| 45 | + using TestAsset second = await TestAsset.GenerateAssetAsync( |
| 46 | + assetName, |
| 47 | + """ |
| 48 | + #file Data.txt |
| 49 | + second |
| 50 | + """, |
| 51 | + addDefaultNuGetConfigFile: false); |
| 52 | + |
| 53 | + Assert.AreNotEqual(first.TargetAssetPath, second.TargetAssetPath); |
| 54 | + Assert.AreSequenceEqual( |
| 55 | + ["first"], |
| 56 | + await File.ReadAllLinesAsync(Path.Combine(first.TargetAssetPath, "Data.txt"), TestContext.CancellationToken)); |
| 57 | + Assert.AreSequenceEqual( |
| 58 | + ["second"], |
| 59 | + await File.ReadAllLinesAsync(Path.Combine(second.TargetAssetPath, "Data.txt"), TestContext.CancellationToken)); |
| 60 | + } |
| 61 | + |
| 62 | + [TestMethod] |
| 63 | + public async Task Dispose_WithBorrowedDirectory_LeavesCallerOwnedFiles() |
| 64 | + { |
| 65 | + using TempDirectory tempDirectory = new(); |
| 66 | + await TempDirectory.WriteFileAsync(tempDirectory.Path, "Caller.txt", "caller"); |
| 67 | + using TestAsset testAsset = await TestAsset.GenerateAssetAsync( |
| 68 | + nameof(Dispose_WithBorrowedDirectory_LeavesCallerOwnedFiles), |
| 69 | + """ |
| 70 | + #file Data.txt |
| 71 | + asset |
| 72 | + """, |
| 73 | + tempDirectory, |
| 74 | + addDefaultNuGetConfigFile: false); |
| 75 | + |
| 76 | + testAsset.Dispose(); |
| 77 | + |
| 78 | + Assert.AreSequenceEqual( |
| 79 | + ["caller"], |
| 80 | + await File.ReadAllLinesAsync(Path.Combine(tempDirectory.Path, "Caller.txt"), TestContext.CancellationToken)); |
| 81 | + Assert.AreSequenceEqual( |
| 82 | + ["asset"], |
| 83 | + await File.ReadAllLinesAsync(Path.Combine(testAsset.TargetAssetPath, "Data.txt"), TestContext.CancellationToken)); |
| 84 | + } |
| 85 | +} |
0 commit comments