Skip to content

Commit 9dd98cf

Browse files
committed
Add a restore integration tests for FileSystemProvider
Coverage includes fileMappings and the rename case (library name is a file, files list includes a different name).
1 parent 86329c3 commit 9dd98cf

File tree

7 files changed

+92
-0
lines changed

7 files changed

+92
-0
lines changed

test/libman.IntegrationTest/CliBaseTest.cs

+23
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System;
5+
using System.Collections.Generic;
56
using System.Diagnostics;
67
using System.IO;
8+
using System.Linq;
79
using System.Threading.Tasks;
810

911
namespace Microsoft.Web.LibraryManager.Cli.IntegrationTest;
1012

1113
[TestClass]
1214
[DeploymentItem(@"TestPackages", "TestPackages")]
15+
[DeploymentItem("TestFiles", "TestFiles")]
1316
public class CliTestBase
1417
{
1518
private const string CliPackageName = "Microsoft.Web.LibraryManager.Cli";
@@ -117,4 +120,24 @@ protected void AssertFileExists(string relativeFilePath)
117120
string filePath = Path.Combine(_testDirectory, relativeFilePath);
118121
Assert.IsTrue(File.Exists(filePath), $"Expected file '{relativeFilePath}' does not exist.");
119122
}
123+
124+
protected void AssertDirectoryContents(string directoryPath, IEnumerable<string> expectedFiles, bool failOnExtraFiles = false)
125+
{
126+
string fullPath = Path.Combine(_testDirectory, directoryPath);
127+
Assert.IsTrue(Directory.Exists(fullPath), $"Expected directory '{directoryPath}' does not exist.");
128+
HashSet<string> actualFiles = Directory.GetFiles(fullPath, "*", SearchOption.AllDirectories)
129+
.Select(file => Path.GetRelativePath(fullPath, file))
130+
.ToHashSet();
131+
132+
foreach (string file in expectedFiles)
133+
{
134+
Assert.IsTrue(actualFiles.Contains(file), $"Directory contents do not match. Expected: {string.Join(", ", expectedFiles)}. Actual: {string.Join(", ", actualFiles)}");
135+
}
136+
137+
if (failOnExtraFiles)
138+
{
139+
List<string> extraFiles = actualFiles.Except(expectedFiles).Order().ToList();
140+
Assert.IsFalse(extraFiles.Any(), $"Unexpected files found in directory '{directoryPath}': {string.Join(", ", extraFiles)}");
141+
}
142+
}
120143
}

test/libman.IntegrationTest/RestoreTests.cs

+58
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System;
5+
using System.IO;
46
using System.Threading.Tasks;
57

68
namespace Microsoft.Web.LibraryManager.Cli.IntegrationTest;
@@ -99,4 +101,60 @@ public async Task Restore_WithFileMapping_FileGlobs_SetRootPath()
99101
AssertFileExists("wwwroot/lib/jquery/jquery.min.js");
100102
AssertFileExists("wwwroot/lib/jquery/jquery.min.map");
101103
}
104+
105+
[TestMethod]
106+
public async Task Restore_FileSystemProvider_WithFileMapping()
107+
{
108+
string testFilesPath = Path.Combine(Environment.CurrentDirectory, "TestFiles");
109+
string manifest = $$"""
110+
{
111+
"version": "3.0",
112+
"libraries": [
113+
{
114+
"provider": "filesystem",
115+
"library": "{{testFilesPath.Replace('\\', '/')}}",
116+
"destination": "wwwroot/lib/testFiles",
117+
"fileMappings": [
118+
{
119+
"files": [
120+
"*.min.js",
121+
]
122+
}
123+
]
124+
}
125+
]
126+
}
127+
""";
128+
await CreateManifestFileAsync(manifest);
129+
130+
await ExecuteCliToolAsync("restore");
131+
132+
AssertDirectoryContents("wwwroot/lib/testFiles/", ["EmptyFile.min.js"], failOnExtraFiles: true);
133+
}
134+
135+
[TestMethod]
136+
public async Task Restore_FileSystemProvider_WithFileRename()
137+
{
138+
string testFilesPath = Path.Combine(Environment.CurrentDirectory, "TestFiles");
139+
string manifest = $$"""
140+
{
141+
"version": "3.0",
142+
"libraries": [
143+
{
144+
"provider": "filesystem",
145+
"library": "{{testFilesPath.Replace('\\', '/')}}/EmptyFile.min.js",
146+
"destination": "wwwroot/lib/testFiles",
147+
"files": [
148+
"TheOnlyFile.js",
149+
]
150+
}
151+
]
152+
}
153+
""";
154+
await CreateManifestFileAsync(manifest);
155+
156+
await ExecuteCliToolAsync("restore");
157+
158+
AssertDirectoryContents("wwwroot/lib/testFiles/", ["TheOnlyFile.js"], failOnExtraFiles: true);
159+
}
102160
}

test/libman.IntegrationTest/TestFiles/EmptyFile.css

Whitespace-only changes.

test/libman.IntegrationTest/TestFiles/EmptyFile.js

Whitespace-only changes.

test/libman.IntegrationTest/TestFiles/EmptyFile.min.css

Whitespace-only changes.

test/libman.IntegrationTest/TestFiles/EmptyFile.min.js

Whitespace-only changes.

test/libman.IntegrationTest/libman.IntegrationTest.csproj

+11
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,17 @@
2020
<Using Include="Microsoft.VisualStudio.TestTools.UnitTesting" />
2121
</ItemGroup>
2222

23+
<ItemGroup>
24+
<Compile Remove="TestFiles\**\*" />
25+
<None Remove="TestFiles\**\*" />
26+
<Content Remove="TestFiles\**\*" />
27+
</ItemGroup>
28+
<ItemGroup>
29+
<Content Include="TestFiles\**\*">
30+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
31+
</Content>
32+
</ItemGroup>
33+
2334
<!-- Copy the nupkgs needed to run CLI and Build package integration tests -->
2435
<Target Name="CopyNupkgFiles" AfterTargets="Build" DependsOnTargets="GetBuildVersion">
2536
<PropertyGroup>

0 commit comments

Comments
 (0)