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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
|
using NUnit.Framework;
using StardewModdingAPI.Toolkit.Utilities;
namespace SMAPI.Tests.Utilities
{
/// <summary>Unit tests for <see cref="PathUtilities"/>.</summary>
[TestFixture]
internal class PathUtilitiesTests
{
/*********
** Sample data
*********/
/// <summary>Sample paths used in unit tests.</summary>
public static readonly SamplePath[] SamplePaths = {
// Windows absolute path
new SamplePath
{
OriginalPath = @"C:\Program Files (x86)\Steam\steamapps\common\Stardew Valley",
Segments = new[] { "C:", "Program Files (x86)", "Steam", "steamapps", "common", "Stardew Valley" },
SegmentsLimit3 = new [] { "C:", "Program Files (x86)", @"Steam\steamapps\common\Stardew Valley" },
NormalizedOnWindows = @"C:\Program Files (x86)\Steam\steamapps\common\Stardew Valley",
NormalizedOnUnix = @"C:/Program Files (x86)/Steam/steamapps/common/Stardew Valley"
},
// Windows absolute path (with trailing slash)
new SamplePath
{
OriginalPath = @"C:\Program Files (x86)\Steam\steamapps\common\Stardew Valley\",
Segments = new[] { "C:", "Program Files (x86)", "Steam", "steamapps", "common", "Stardew Valley" },
SegmentsLimit3 = new [] { "C:", "Program Files (x86)", @"Steam\steamapps\common\Stardew Valley\" },
NormalizedOnWindows = @"C:\Program Files (x86)\Steam\steamapps\common\Stardew Valley\",
NormalizedOnUnix = @"C:/Program Files (x86)/Steam/steamapps/common/Stardew Valley/"
},
// Windows relative path
new SamplePath
{
OriginalPath = @"Content\Characters\Dialogue\Abigail",
Segments = new [] { "Content", "Characters", "Dialogue", "Abigail" },
SegmentsLimit3 = new [] { "Content", "Characters", @"Dialogue\Abigail" },
NormalizedOnWindows = @"Content\Characters\Dialogue\Abigail",
NormalizedOnUnix = @"Content/Characters/Dialogue/Abigail"
},
// Windows relative path (with directory climbing)
new SamplePath
{
OriginalPath = @"..\..\Content",
Segments = new [] { "..", "..", "Content" },
SegmentsLimit3 = new [] { "..", "..", "Content" },
NormalizedOnWindows = @"..\..\Content",
NormalizedOnUnix = @"../../Content"
},
// Windows UNC path
new SamplePath
{
OriginalPath = @"\\unc\path",
Segments = new [] { "unc", "path" },
SegmentsLimit3 = new [] { "unc", "path" },
NormalizedOnWindows = @"\\unc\path",
NormalizedOnUnix = "/unc/path" // there's no good way to normalize this on Unix since UNC paths aren't supported; path normalization is meant for asset names anyway, so this test only ensures it returns some sort of sane value
},
// Linux absolute path
new SamplePath
{
OriginalPath = @"/home/.steam/steam/steamapps/common/Stardew Valley",
Segments = new [] { "home", ".steam", "steam", "steamapps", "common", "Stardew Valley" },
SegmentsLimit3 = new [] { "home", ".steam", "steam/steamapps/common/Stardew Valley" },
NormalizedOnWindows = @"\home\.steam\steam\steamapps\common\Stardew Valley",
NormalizedOnUnix = @"/home/.steam/steam/steamapps/common/Stardew Valley"
},
// Linux absolute path (with trailing slash)
new SamplePath
{
OriginalPath = @"/home/.steam/steam/steamapps/common/Stardew Valley/",
Segments = new [] { "home", ".steam", "steam", "steamapps", "common", "Stardew Valley" },
SegmentsLimit3 = new [] { "home", ".steam", "steam/steamapps/common/Stardew Valley/" },
NormalizedOnWindows = @"\home\.steam\steam\steamapps\common\Stardew Valley\",
NormalizedOnUnix = @"/home/.steam/steam/steamapps/common/Stardew Valley/"
},
// Linux absolute path (with ~)
new SamplePath
{
OriginalPath = @"~/.steam/steam/steamapps/common/Stardew Valley",
Segments = new [] { "~", ".steam", "steam", "steamapps", "common", "Stardew Valley" },
SegmentsLimit3 = new [] { "~", ".steam", "steam/steamapps/common/Stardew Valley" },
NormalizedOnWindows = @"~\.steam\steam\steamapps\common\Stardew Valley",
NormalizedOnUnix = @"~/.steam/steam/steamapps/common/Stardew Valley"
},
// Linux relative path
new SamplePath
{
OriginalPath = @"Content/Characters/Dialogue/Abigail",
Segments = new [] { "Content", "Characters", "Dialogue", "Abigail" },
SegmentsLimit3 = new [] { "Content", "Characters", "Dialogue/Abigail" },
NormalizedOnWindows = @"Content\Characters\Dialogue\Abigail",
NormalizedOnUnix = @"Content/Characters/Dialogue/Abigail"
},
// Linux relative path (with directory climbing)
new SamplePath
{
OriginalPath = @"../../Content",
Segments = new [] { "..", "..", "Content" },
SegmentsLimit3 = new [] { "..", "..", "Content" },
NormalizedOnWindows = @"..\..\Content",
NormalizedOnUnix = @"../../Content"
},
// Mixed directory separators
new SamplePath
{
OriginalPath = @"C:\some/mixed\path/separators",
Segments = new [] { "C:", "some", "mixed", "path", "separators" },
SegmentsLimit3 = new [] { "C:", "some", @"mixed\path/separators" },
NormalizedOnWindows = @"C:\some\mixed\path\separators",
NormalizedOnUnix = @"C:/some/mixed/path/separators"
},
};
/*********
** Unit tests
*********/
/****
** GetSegments
****/
[Test(Description = "Assert that PathUtilities.GetSegments splits paths correctly.")]
[TestCaseSource(nameof(PathUtilitiesTests.SamplePaths))]
public void GetSegments(SamplePath path)
{
// act
string[] segments = PathUtilities.GetSegments(path.OriginalPath);
// assert
Assert.AreEqual(path.Segments, segments);
}
[Test(Description = "Assert that PathUtilities.GetSegments splits paths correctly when given a limit.")]
[TestCaseSource(nameof(PathUtilitiesTests.SamplePaths))]
public void GetSegments_WithLimit(SamplePath path)
{
// act
string[] segments = PathUtilities.GetSegments(path.OriginalPath, 3);
// assert
Assert.AreEqual(path.SegmentsLimit3, segments);
}
/****
** NormalizePathSeparators
****/
[Test(Description = "Assert that PathUtilities.NormalizePathSeparators normalizes paths correctly.")]
[TestCaseSource(nameof(PathUtilitiesTests.SamplePaths))]
public void NormalizePath(SamplePath path)
{
// act
string normalized = PathUtilities.NormalizePath(path.OriginalPath);
// assert
#if SMAPI_FOR_WINDOWS
Assert.AreEqual(path.NormalizedOnWindows, normalized);
#else
Assert.AreEqual(path.NormalizedOnUnix, normalized);
#endif
}
/****
** GetRelativePath
****/
[Test(Description = "Assert that PathUtilities.GetRelativePath returns the expected values.")]
#if SMAPI_FOR_WINDOWS
[TestCase(
@"C:\Program Files (x86)\Steam\steamapps\common\Stardew Valley",
@"C:\Program Files (x86)\Steam\steamapps\common\Stardew Valley\Mods\Automate",
ExpectedResult = @"Mods\Automate"
)]
[TestCase(
@"C:\Program Files (x86)\Steam\steamapps\common\Stardew Valley\Mods\Automate",
@"C:\Program Files (x86)\Steam\steamapps\common\Stardew Valley\Content",
ExpectedResult = @"..\..\Content"
)]
[TestCase(
@"C:\Program Files (x86)\Steam\steamapps\common\Stardew Valley\Mods\Automate",
@"D:\another-drive",
ExpectedResult = @"D:\another-drive"
)]
[TestCase(
@"\\parent\unc",
@"\\parent\unc\path\to\child",
ExpectedResult = @"path\to\child"
)]
[TestCase(
@"C:\same\path",
@"C:\same\path",
ExpectedResult = @"."
)]
[TestCase(
@"C:\parent",
@"C:\PARENT\child",
ExpectedResult = @"child"
)]
#else
[TestCase(
@"~/.steam/steam/steamapps/common/Stardew Valley",
@"~/.steam/steam/steamapps/common/Stardew Valley/Mods/Automate",
ExpectedResult = @"Mods/Automate"
)]
[TestCase(
@"~/.steam/steam/steamapps/common/Stardew Valley/Mods/Automate",
@"~/.steam/steam/steamapps/common/Stardew Valley/Content",
ExpectedResult = @"../../Content"
)]
[TestCase(
@"~/.steam/steam/steamapps/common/Stardew Valley/Mods/Automate",
@"/mnt/another-drive",
ExpectedResult = @"/mnt/another-drive"
)]
[TestCase(
@"~/same/path",
@"~/same/path",
ExpectedResult = @"."
)]
[TestCase(
@"~/parent",
@"~/PARENT/child",
ExpectedResult = @"child" // note: incorrect on Linux and sometimes macOS, but not worth the complexity of detecting whether the filesystem is case-sensitive for SMAPI's purposes
)]
#endif
public string GetRelativePath(string sourceDir, string targetPath)
{
return PathUtilities.GetRelativePath(sourceDir, targetPath);
}
/*********
** Private classes
*********/
public class SamplePath
{
public string OriginalPath { get; set; }
public string[] Segments { get; set; }
public string[] SegmentsLimit3 { get; set; }
public string NormalizedOnWindows { get; set; }
public string NormalizedOnUnix { get; set; }
public override string ToString()
{
return this.OriginalPath;
}
}
}
}
|