aboutsummaryrefslogtreecommitdiff
path: root/src/test
diff options
context:
space:
mode:
authorRob Mensching <rob@firegiant.com>2017-11-29 14:08:08 -0800
committerRob Mensching <rob@firegiant.com>2017-11-29 14:08:08 -0800
commitea3d18595a610ee07b03f07af4f03cf75b5ab420 (patch)
treeb69ac2185b05254b136051d561b189c4fda1fc5b /src/test
parent95f4f9b9b99e1a6f91f4687c2dd511a6d6fc2716 (diff)
downloadwix-ea3d18595a610ee07b03f07af4f03cf75b5ab420.tar.gz
wix-ea3d18595a610ee07b03f07af4f03cf75b5ab420.tar.bz2
wix-ea3d18595a610ee07b03f07af4f03cf75b5ab420.zip
Improved cabinet handling
Diffstat (limited to 'src/test')
-rw-r--r--src/test/WixToolsetTest.Core.Native/CabinetFixture.cs115
-rw-r--r--src/test/WixToolsetTest.Core.Native/TestData/test.cabbin0 -> 115 bytes
-rw-r--r--src/test/WixToolsetTest.Core.Native/TestData/test.txt1
-rw-r--r--src/test/WixToolsetTest.Core.Native/Utility/DisposableFileSystem.cs86
-rw-r--r--src/test/WixToolsetTest.Core.Native/Utility/Pushd.cs46
-rw-r--r--src/test/WixToolsetTest.Core.Native/Utility/TestData.cs17
-rw-r--r--src/test/WixToolsetTest.Core.Native/WixToolsetTest.Core.Native.csproj24
7 files changed, 289 insertions, 0 deletions
diff --git a/src/test/WixToolsetTest.Core.Native/CabinetFixture.cs b/src/test/WixToolsetTest.Core.Native/CabinetFixture.cs
new file mode 100644
index 00000000..baab3bee
--- /dev/null
+++ b/src/test/WixToolsetTest.Core.Native/CabinetFixture.cs
@@ -0,0 +1,115 @@
1// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.
2
3namespace WixToolsetTest.CoreNative
4{
5 using System;
6 using System.IO;
7 using System.Linq;
8 using WixToolset.Core.Native;
9 using WixToolsetTest.CoreNative.Utility;
10 using Xunit;
11
12 public class CabinetFixture
13 {
14 [Fact]
15 public void CanCreateSingleFileCabinet()
16 {
17 using (var fs = new DisposableFileSystem())
18 {
19 var intermediateFolder = fs.GetFolder(true);
20 var cabPath = Path.Combine(intermediateFolder, "testout.cab");
21
22 var files = new[] { new CabinetCompressFile(TestData.Get(@"TestData\test.txt"), "test.txt") };
23
24 var cabinet = new Cabinet(cabPath);
25 cabinet.Compress(files, CabinetCompressionLevel.Low);
26
27 Assert.True(File.Exists(cabPath));
28 }
29 }
30
31 [Fact]
32 public void CanEnumerateSingleFileCabinet()
33 {
34 var cabinetPath = TestData.Get(@"TestData\test.cab");
35
36 var cabinet = new Cabinet(cabinetPath);
37 var files = cabinet.Enumerate();
38
39 var file = files.Single();
40 Assert.Equal("test.txt", file.FileId);
41 Assert.Equal(17, file.Size);
42
43 Assert.Equal(19259, file.Date);
44 Assert.Equal(47731, file.Time);
45 Assert.True(file.SameAsDateTime(new DateTime(2017, 9, 28, 0, 19, 38)));
46 }
47
48 [Fact]
49 public void CanExtractSingleFileCabinet()
50 {
51 var cabinetPath = TestData.Get(@"TestData\test.cab");
52
53 using (var fs = new DisposableFileSystem())
54 {
55 var extractFolder = fs.GetFolder(true);
56
57 var cabinet = new Cabinet(cabinetPath);
58 cabinet.Extract(extractFolder);
59
60 var files = Directory.EnumerateFiles(extractFolder);
61
62 var file = new FileInfo(files.Single());
63 CabInterop.DateTimeToCabDateAndTime(file.CreationTime, out var date, out var time);
64
65 Assert.Equal("test.txt", file.Name);
66 Assert.Equal(17, file.Length);
67 Assert.Equal(19259, date);
68 Assert.Equal(47731, time);
69 }
70 }
71
72 [Fact]
73 public void IntegrationTest()
74 {
75 using (var fs = new DisposableFileSystem())
76 {
77 var intermediateFolder = fs.GetFolder(true);
78 var cabinetPath = Path.Combine(intermediateFolder, "testout.cab");
79 var extractFolder = fs.GetFolder(true);
80
81 // Compress.
82 {
83 var files = new[] { new CabinetCompressFile(TestData.Get(@"TestData\test.txt"), "test.txt") };
84
85 var cabinet = new Cabinet(cabinetPath);
86 cabinet.Compress(files, CabinetCompressionLevel.Low);
87 }
88
89 // Extract.
90 {
91 var cabinet = new Cabinet(cabinetPath);
92 cabinet.Extract(extractFolder);
93 }
94
95 // Enumerate to compare cabinet to extracted files.
96 {
97 var cabinet = new Cabinet(cabinetPath);
98 var enumerated = cabinet.Enumerate().OrderBy(f => f.FileId).ToArray();
99
100 var files = Directory.EnumerateFiles(extractFolder).OrderBy(f => f).ToArray();
101
102 for (var i =0; i < enumerated.Length; ++i)
103 {
104 var cabFileInfo = enumerated[i];
105 var fileInfo = new FileInfo(files[i]);
106
107 Assert.Equal(cabFileInfo.FileId, fileInfo.Name);
108 Assert.Equal(cabFileInfo.Size, fileInfo.Length);
109 Assert.True(cabFileInfo.SameAsDateTime(fileInfo.CreationTime));
110 }
111 }
112 }
113 }
114 }
115}
diff --git a/src/test/WixToolsetTest.Core.Native/TestData/test.cab b/src/test/WixToolsetTest.Core.Native/TestData/test.cab
new file mode 100644
index 00000000..ca78f632
--- /dev/null
+++ b/src/test/WixToolsetTest.Core.Native/TestData/test.cab
Binary files differ
diff --git a/src/test/WixToolsetTest.Core.Native/TestData/test.txt b/src/test/WixToolsetTest.Core.Native/TestData/test.txt
new file mode 100644
index 00000000..cd0db0e1
--- /dev/null
+++ b/src/test/WixToolsetTest.Core.Native/TestData/test.txt
@@ -0,0 +1 @@
This is test.txt. \ No newline at end of file
diff --git a/src/test/WixToolsetTest.Core.Native/Utility/DisposableFileSystem.cs b/src/test/WixToolsetTest.Core.Native/Utility/DisposableFileSystem.cs
new file mode 100644
index 00000000..c9957247
--- /dev/null
+++ b/src/test/WixToolsetTest.Core.Native/Utility/DisposableFileSystem.cs
@@ -0,0 +1,86 @@
1// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.
2
3namespace WixToolsetTest.CoreNative.Utility
4{
5 using System;
6 using System.Collections.Generic;
7 using System.IO;
8
9 public class DisposableFileSystem : IDisposable
10 {
11 protected bool Disposed { get; private set; }
12
13 private List<string> CleanupPaths { get; } = new List<string>();
14
15 public string GetFile(bool create = false)
16 {
17 var path = Path.GetTempFileName();
18
19 if (!create)
20 {
21 File.Delete(path);
22 }
23
24 this.CleanupPaths.Add(path);
25
26 return path;
27 }
28
29 public string GetFolder(bool create = false)
30 {
31 var path = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
32
33 if (create)
34 {
35 Directory.CreateDirectory(path);
36 }
37
38 this.CleanupPaths.Add(path);
39
40 return path;
41 }
42
43
44 #region // IDisposable
45
46 public void Dispose()
47 {
48 this.Dispose(true);
49 GC.SuppressFinalize(this);
50 }
51
52 protected virtual void Dispose(bool disposing)
53 {
54 if (this.Disposed)
55 {
56 return;
57 }
58
59 if (disposing)
60 {
61 foreach (var path in this.CleanupPaths)
62 {
63 try
64 {
65 if (File.Exists(path))
66 {
67 File.Delete(path);
68 }
69 else if (Directory.Exists(path))
70 {
71 Directory.Delete(path, true);
72 }
73 }
74 catch
75 {
76 // Best effort delete, so ignore any failures.
77 }
78 }
79 }
80
81 this.Disposed = true;
82 }
83
84 #endregion
85 }
86}
diff --git a/src/test/WixToolsetTest.Core.Native/Utility/Pushd.cs b/src/test/WixToolsetTest.Core.Native/Utility/Pushd.cs
new file mode 100644
index 00000000..91700c2f
--- /dev/null
+++ b/src/test/WixToolsetTest.Core.Native/Utility/Pushd.cs
@@ -0,0 +1,46 @@
1// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.
2
3namespace WixToolsetTest.CoreNative.Utility
4{
5 using System;
6 using System.IO;
7
8 public class Pushd : IDisposable
9 {
10 protected bool Disposed { get; private set; }
11
12 public Pushd(string path)
13 {
14 this.PreviousDirectory = Directory.GetCurrentDirectory();
15
16 Directory.SetCurrentDirectory(path);
17 }
18
19 public string PreviousDirectory { get; }
20
21 #region // IDisposable
22
23 public void Dispose()
24 {
25 this.Dispose(true);
26 GC.SuppressFinalize(this);
27 }
28
29 protected virtual void Dispose(bool disposing)
30 {
31 if (this.Disposed)
32 {
33 return;
34 }
35
36 if (disposing)
37 {
38 Directory.SetCurrentDirectory(this.PreviousDirectory);
39 }
40
41 this.Disposed = true;
42 }
43
44 #endregion
45 }
46}
diff --git a/src/test/WixToolsetTest.Core.Native/Utility/TestData.cs b/src/test/WixToolsetTest.Core.Native/Utility/TestData.cs
new file mode 100644
index 00000000..cd9c6318
--- /dev/null
+++ b/src/test/WixToolsetTest.Core.Native/Utility/TestData.cs
@@ -0,0 +1,17 @@
1// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.
2
3namespace WixToolsetTest.CoreNative.Utility
4{
5 using System;
6 using System.IO;
7
8 public class TestData
9 {
10 public static string LocalPath => Path.GetDirectoryName(new Uri(System.Reflection.Assembly.GetExecutingAssembly().CodeBase).LocalPath);
11
12 public static string Get(params string[] paths)
13 {
14 return Path.Combine(LocalPath, Path.Combine(paths));
15 }
16 }
17}
diff --git a/src/test/WixToolsetTest.Core.Native/WixToolsetTest.Core.Native.csproj b/src/test/WixToolsetTest.Core.Native/WixToolsetTest.Core.Native.csproj
new file mode 100644
index 00000000..c7fd89ea
--- /dev/null
+++ b/src/test/WixToolsetTest.Core.Native/WixToolsetTest.Core.Native.csproj
@@ -0,0 +1,24 @@
1<?xml version="1.0" encoding="utf-8"?>
2<!-- Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. -->
3
4<Project Sdk="Microsoft.NET.Sdk">
5 <PropertyGroup>
6 <TargetFramework>netcoreapp2.0</TargetFramework>
7 <IsPackable>false</IsPackable>
8 </PropertyGroup>
9
10 <ItemGroup>
11 <Content Include="TestData\test.cab" CopyToOutputDirectory="PreserveNewest" />
12 <Content Include="TestData\test.txt" CopyToOutputDirectory="PreserveNewest" />
13 </ItemGroup>
14
15 <ItemGroup>
16 <ProjectReference Include="..\..\WixToolset.Core.Native\WixToolset.Core.Native.csproj" />
17 </ItemGroup>
18
19 <ItemGroup>
20 <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0-preview-20170628-02" />
21 <PackageReference Include="xunit" Version="2.2.0" />
22 <PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
23 </ItemGroup>
24</Project>