forked from Unity-Technologies/UnityDataTools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebBundleSupportTests.cs
More file actions
164 lines (138 loc) · 4.93 KB
/
Copy pathWebBundleSupportTests.cs
File metadata and controls
164 lines (138 loc) · 4.93 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
using System;
using System.Text.Json;
using Microsoft.Data.Sqlite;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using NUnit.Framework;
using UnityDataTools.FileSystem;
namespace UnityDataTools.UnityDataTool.Tests;
#pragma warning disable NUnit2005, NUnit2006
public class WebBundleSupportTests
{
private string m_TestOutputFolder;
private string m_TestDataFolder;
[OneTimeSetUp]
public void OneTimeSetup()
{
m_TestOutputFolder = Path.Combine(TestContext.CurrentContext.TestDirectory, "test_folder");
m_TestDataFolder = Path.Combine(TestContext.CurrentContext.TestDirectory, "Data");
Directory.CreateDirectory(m_TestOutputFolder);
Directory.SetCurrentDirectory(m_TestOutputFolder);
}
[TearDown]
public void Teardown()
{
SqliteConnection.ClearAllPools();
var testDir = new DirectoryInfo(m_TestOutputFolder);
testDir.EnumerateFiles()
.ToList().ForEach(f => f.Delete());
testDir.EnumerateDirectories()
.ToList().ForEach(d => d.Delete(true));
}
[Test]
public void IsWebBundle_True()
{
var webBundlePath = Path.Combine(m_TestDataFolder, "WebBundles", "HelloWorld.data");
Assert.IsTrue(WebBundleHelper.IsWebBundle(webBundlePath));
}
[Test]
public void IsWebBundle_False()
{
var nonWebBundlePath = Path.Combine(m_TestDataFolder, "WebBundles", "NotAWebBundle.txt");
Assert.IsFalse(WebBundleHelper.IsWebBundle(nonWebBundlePath));
}
[Test]
public async Task ArchiveList_WebBundle_ListFilesCorrectly(
[Values(
"HelloWorld.data",
"HelloWorld.data.gz",
"HelloWorld.data.br"
)] string bundlePath)
{
var path = Path.Combine(m_TestDataFolder, "WebBundles", bundlePath);
using var sw = new StringWriter();
var currentOut = Console.Out;
try
{
Console.SetOut(sw);
Assert.AreEqual(0, await Program.Main(new string[] { "archive", "list", path }));
var actualOutput = sw.ToString();
// the expectedOutput has "lf" line endings but running on Windows
// the console output will have "crlr"
actualOutput = actualOutput.Replace("\r\n", "\n");
var expectedOutput = (
@"data.unity3d
Size: 253044
RuntimeInitializeOnLoads.json
Size: 700
ScriptingAssemblies.json
Size: 3060
boot.config
Size: 93
Il2CppData/Metadata/global-metadata.dat
Size: 1641180
Resources/unity_default_resources
Size: 607376
"
);
Assert.AreEqual(expectedOutput, actualOutput);
}
finally
{
Console.SetOut(currentOut);
}
}
[Test]
public async Task ArchiveList_WebBundle_JsonFormat(
[Values(
"HelloWorld.data",
"HelloWorld.data.gz",
"HelloWorld.data.br"
)] string bundlePath)
{
var path = Path.Combine(m_TestDataFolder, "WebBundles", bundlePath);
using var sw = new StringWriter();
var currentOut = Console.Out;
try
{
Console.SetOut(sw);
Assert.AreEqual(0, await Program.Main(new string[] { "archive", "list", path, "-f", "Json" }));
var output = sw.ToString();
var jsonArray = JsonDocument.Parse(output).RootElement;
Assert.AreEqual(JsonValueKind.Array, jsonArray.ValueKind);
Assert.AreEqual(6, jsonArray.GetArrayLength());
foreach (var element in jsonArray.EnumerateArray())
{
Assert.IsTrue(element.TryGetProperty("path", out _));
Assert.IsTrue(element.TryGetProperty("size", out _));
}
Assert.AreEqual("data.unity3d", jsonArray[0].GetProperty("path").GetString());
Assert.AreEqual(253044u, jsonArray[0].GetProperty("size").GetUInt32());
}
finally
{
Console.SetOut(currentOut);
}
}
[Test]
public async Task ArchiveExtract_WebBundle_FileExtractedSuccessfully(
[Values("", "-o archive", "--output-path archive")] string options,
[Values("HelloWorld.data", "HelloWorld.data.gz", "HelloWorld.data.br")] string bundlePath)
{
var path = Path.Combine(m_TestDataFolder, "WebBundles", bundlePath);
string[] expectedFiles = {
"boot.config",
"data.unity3d",
"RuntimeInitializeOnLoads.json",
"ScriptingAssemblies.json",
Path.Combine("Il2CppData", "Metadata", "global-metadata.dat"),
Path.Combine("Resources", "unity_default_resources"),
};
Assert.AreEqual(0, await Program.Main(new string[] { "archive", "extract", path }.Concat(options.Split(" ", StringSplitOptions.RemoveEmptyEntries)).ToArray()));
foreach (var file in expectedFiles)
{
Assert.IsTrue(File.Exists(Path.Combine(m_TestOutputFolder, "archive", file)));
}
}
}