-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathCabContainerTests.cs
More file actions
77 lines (61 loc) · 2.71 KB
/
CabContainerTests.cs
File metadata and controls
77 lines (61 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE.txt file in the project root for more information.
using System.Text;
using Microsoft.Extensions.Logging;
using Moq;
using WixToolset.Dtf.Compression.Cab;
namespace Sign.Core.Test
{
public class CabContainerTests
{
[Fact]
public async Task OpenAsync_ExtractsCabToDirectory()
{
string[] expectedFileNames = [".a", "b", "c.d"];
FileInfo cabFile = CreateCabFile(expectedFileNames);
using (DirectoryServiceStub directoryService = new())
using (CabContainer container = new(cabFile, directoryService, Mock.Of<IFileMatcher>(), Mock.Of<ILogger>()))
{
await container.OpenAsync();
FileInfo[] actualFiles = directoryService.Directories[0].GetFiles("*", SearchOption.AllDirectories);
string[] actualFileNames = actualFiles
.Select(file => file.FullName.Substring(directoryService.Directories[0].FullName.Length + 1))
.ToArray();
Assert.Equal(expectedFileNames, actualFileNames);
}
}
[Fact]
public async Task SaveAsync_CompressesCabFromDirectory()
{
string[] fileNames = ["a"];
FileInfo cabFile = CreateCabFile(fileNames);
using (DirectoryServiceStub directoryService = new())
using (CabContainer container = new(cabFile, directoryService, Mock.Of<IFileMatcher>(), Mock.Of<ILogger>()))
{
await container.OpenAsync();
File.WriteAllText(Path.Combine(directoryService.Directories[0].FullName, "b"), "b");
await container.SaveAsync();
}
var cab = new CabInfo(cabFile.FullName);
var files = cab.GetFiles();
Assert.Equal(2, files.Count);
Assert.Contains(files, e => e.Name == "a");
Assert.Contains(files, e => e.Name == "b");
}
private static FileInfo CreateCabFile(params string[] entryNames)
{
FileInfo file = new(Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()));
var cab = new CabInfo(file.FullName);
var sourceFiles = new List<string>();
foreach (string entryName in entryNames)
{
var sourceFile = Path.GetTempFileName();
File.WriteAllBytes(sourceFile, Encoding.UTF8.GetBytes(entryName));
sourceFiles.Add(sourceFile);
}
cab.PackFiles(sourceDirectory: null, sourceFiles, entryNames);
return file;
}
}
}