-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathExecute.SimulatedPath.cs
607 lines (502 loc) · 15.5 KB
/
Execute.SimulatedPath.cs
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
using System;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
#if FEATURE_FILESYSTEM_NET7
using Testably.Abstractions.Testing.Storage;
#endif
namespace Testably.Abstractions.Testing.Helpers;
internal partial class Execute
{
private abstract class SimulatedPath(MockFileSystem fileSystem) : IPath
{
#region IPath Members
/// <inheritdoc cref="IPath.AltDirectorySeparatorChar" />
public abstract char AltDirectorySeparatorChar { get; }
/// <inheritdoc cref="IPath.DirectorySeparatorChar" />
public abstract char DirectorySeparatorChar { get; }
/// <inheritdoc cref="IFileSystemEntity.FileSystem" />
public IFileSystem FileSystem => fileSystem;
/// <inheritdoc cref="IPath.PathSeparator" />
public abstract char PathSeparator { get; }
/// <inheritdoc cref="IPath.VolumeSeparatorChar" />
public abstract char VolumeSeparatorChar { get; }
/// <inheritdoc cref="IPath.ChangeExtension(string, string)" />
[return: NotNullIfNotNull("path")]
public string? ChangeExtension(string? path, string? extension)
{
if (path == null)
{
return null;
}
if (path == string.Empty)
{
return string.Empty;
}
if (extension == null)
{
extension = "";
}
else if (!extension.StartsWith('.'))
{
extension = "." + extension;
}
if (!TryGetExtensionIndex(path, out int? dotIndex))
{
return path + extension;
}
return path.Substring(0, dotIndex.Value) + extension;
}
/// <inheritdoc cref="IPath.Combine(string, string)" />
public string Combine(string path1, string path2)
{
if (path1 == null)
{
throw new ArgumentNullException(nameof(path1));
}
if (path2 == null)
{
throw new ArgumentNullException(nameof(path2));
}
return CombineInternal([path1, path2]);
}
/// <inheritdoc cref="IPath.Combine(string, string, string)" />
public string Combine(string path1, string path2, string path3)
{
if (path1 == null)
{
throw new ArgumentNullException(nameof(path1));
}
if (path2 == null)
{
throw new ArgumentNullException(nameof(path2));
}
if (path3 == null)
{
throw new ArgumentNullException(nameof(path3));
}
return CombineInternal([path1, path2, path3]);
}
/// <inheritdoc cref="IPath.Combine(string, string, string, string)" />
public string Combine(string path1, string path2, string path3, string path4)
{
if (path1 == null)
{
throw new ArgumentNullException(nameof(path1));
}
if (path2 == null)
{
throw new ArgumentNullException(nameof(path2));
}
if (path3 == null)
{
throw new ArgumentNullException(nameof(path3));
}
if (path4 == null)
{
throw new ArgumentNullException(nameof(path4));
}
return CombineInternal([path1, path2, path3, path4]);
}
/// <inheritdoc cref="IPath.Combine(string[])" />
public string Combine(params string[] paths)
=> CombineInternal(paths);
#if FEATURE_PATH_ADVANCED
/// <inheritdoc cref="IPath.EndsInDirectorySeparator(ReadOnlySpan{char})" />
public bool EndsInDirectorySeparator(ReadOnlySpan<char> path)
=> EndsInDirectorySeparator(path.ToString());
#endif
#if FEATURE_PATH_ADVANCED
/// <inheritdoc cref="IPath.EndsInDirectorySeparator(string)" />
public bool EndsInDirectorySeparator(string path)
=> !string.IsNullOrEmpty(path) && IsDirectorySeparator(path[path.Length - 1]);
#endif
#if FEATURE_FILESYSTEM_NET7
/// <inheritdoc cref="IPath.Exists(string)" />
public bool Exists([NotNullWhen(true)] string? path)
{
if (string.IsNullOrEmpty(path))
{
return false;
}
return fileSystem.Storage.GetContainer(fileSystem.Storage.GetLocation(path))
is not NullContainer;
}
#endif
#if FEATURE_SPAN
/// <inheritdoc cref="IPath.GetDirectoryName(ReadOnlySpan{char})" />
public ReadOnlySpan<char> GetDirectoryName(ReadOnlySpan<char> path)
=> GetDirectoryName(path.ToString());
#endif
/// <inheritdoc cref="IPath.GetDirectoryName(string)" />
public string? GetDirectoryName(string? path)
{
if (path == null || IsEffectivelyEmpty(path))
{
return null;
}
int rootLength = GetRootLength(path);
if (path.Length <= rootLength)
{
return null;
}
int end = path.Length;
while (end > rootLength && !IsDirectorySeparator(path[end - 1]))
{
end--;
}
while (end > rootLength && IsDirectorySeparator(path[end - 1]))
{
end--;
}
return NormalizeDirectorySeparators(path.Substring(0, end));
}
#if FEATURE_SPAN
/// <inheritdoc cref="IPath.GetExtension(ReadOnlySpan{char})" />
public ReadOnlySpan<char> GetExtension(ReadOnlySpan<char> path)
=> GetExtension(path.ToString());
#endif
/// <inheritdoc cref="IPath.GetExtension(string)" />
[return: NotNullIfNotNull("path")]
public string? GetExtension(string? path)
{
if (path == null)
{
return null;
}
if (TryGetExtensionIndex(path, out int? dotIndex))
{
return dotIndex != path.Length - 1
? path.Substring(dotIndex.Value)
: string.Empty;
}
return string.Empty;
}
#if FEATURE_SPAN
/// <inheritdoc cref="IPath.GetFileName(ReadOnlySpan{char})" />
public ReadOnlySpan<char> GetFileName(ReadOnlySpan<char> path)
=> GetFileName(path.ToString());
#endif
/// <inheritdoc cref="IPath.GetFileName(string)" />
[return: NotNullIfNotNull("path")]
public string? GetFileName(string? path)
{
if (path == null)
{
return null;
}
for (int i = path.Length - 1; i >= 0; i--)
{
if (IsDirectorySeparator(path[i]))
{
return path.Substring(i + 1);
}
}
return path;
}
#if FEATURE_SPAN
/// <inheritdoc cref="IPath.GetFileNameWithoutExtension(ReadOnlySpan{char})" />
public ReadOnlySpan<char> GetFileNameWithoutExtension(ReadOnlySpan<char> path)
=> GetFileNameWithoutExtension(path.ToString());
#endif
/// <inheritdoc cref="IPath.GetFileNameWithoutExtension(string)" />
[return: NotNullIfNotNull("path")]
public string? GetFileNameWithoutExtension(string? path)
{
if (path == null)
{
return null;
}
string fileName = GetFileName(path);
int lastPeriod = fileName.LastIndexOf('.');
return lastPeriod < 0 ? fileName : fileName.Substring(0, lastPeriod);
}
/// <inheritdoc cref="IPath.GetFullPath(string)" />
public string GetFullPath(string path)
{
path.EnsureValidArgument(fileSystem, nameof(path));
string? pathRoot = System.IO.Path.GetPathRoot(path);
string? directoryRoot =
System.IO.Path.GetPathRoot(fileSystem.Storage.CurrentDirectory);
if (!string.IsNullOrEmpty(pathRoot) && !string.IsNullOrEmpty(directoryRoot))
{
if (char.ToUpperInvariant(pathRoot[0]) != char.ToUpperInvariant(directoryRoot[0]))
{
return System.IO.Path.GetFullPath(path);
}
if (pathRoot.Length < directoryRoot.Length)
{
path = path.Substring(pathRoot.Length);
}
}
return System.IO.Path.GetFullPath(System.IO.Path.Combine(
fileSystem.Storage.CurrentDirectory,
path));
}
#if FEATURE_PATH_RELATIVE
/// <inheritdoc cref="IPath.GetFullPath(string, string)" />
public string GetFullPath(string path, string basePath)
=> System.IO.Path.GetFullPath(path, basePath);
#endif
/// <inheritdoc cref="IPath.GetInvalidFileNameChars()" />
public abstract char[] GetInvalidFileNameChars();
/// <inheritdoc cref="IPath.GetInvalidPathChars()" />
public abstract char[] GetInvalidPathChars();
#if FEATURE_SPAN
/// <inheritdoc cref="IPath.GetPathRoot(ReadOnlySpan{char})" />
public ReadOnlySpan<char> GetPathRoot(ReadOnlySpan<char> path)
=> GetPathRoot(path.ToString());
#endif
/// <inheritdoc cref="IPath.GetPathRoot(string?)" />
public abstract string? GetPathRoot(string? path);
/// <inheritdoc cref="IPath.GetRandomFileName()" />
public string GetRandomFileName()
=> $"{RandomString(8)}.{RandomString(3)}";
#if FEATURE_PATH_RELATIVE
/// <inheritdoc cref="IPath.GetRelativePath(string, string)" />
public string GetRelativePath(string relativeTo, string path)
{
relativeTo.EnsureValidArgument(fileSystem, nameof(relativeTo));
path.EnsureValidArgument(fileSystem, nameof(path));
relativeTo = fileSystem.Execute.Path.GetFullPath(relativeTo);
path = fileSystem.Execute.Path.GetFullPath(path);
return System.IO.Path.GetRelativePath(relativeTo, path);
}
#endif
/// <inheritdoc cref="IPath.GetTempFileName()" />
#if !NETSTANDARD2_0
[Obsolete(
"Insecure temporary file creation methods should not be used. Use `Path.Combine(Path.GetTempPath(), Path.GetRandomFileName())` instead.")]
#endif
public string GetTempFileName()
=> System.IO.Path.GetTempFileName();
/// <inheritdoc cref="IPath.GetTempPath()" />
public abstract string GetTempPath();
#if FEATURE_SPAN
/// <inheritdoc cref="IPath.HasExtension(ReadOnlySpan{char})" />
public bool HasExtension(ReadOnlySpan<char> path)
=> HasExtension(path.ToString());
#endif
/// <inheritdoc cref="IPath.HasExtension(string)" />
public bool HasExtension([NotNullWhen(true)] string? path)
=> System.IO.Path.HasExtension(path);
#if FEATURE_SPAN
/// <inheritdoc cref="IPath.IsPathFullyQualified(ReadOnlySpan{char})" />
public bool IsPathFullyQualified(ReadOnlySpan<char> path)
=> IsPathFullyQualified(path.ToString());
#endif
#if FEATURE_PATH_RELATIVE
/// <inheritdoc cref="IPath.IsPathFullyQualified(string)" />
public bool IsPathFullyQualified(string path)
=> System.IO.Path.IsPathFullyQualified(path);
#endif
#if FEATURE_SPAN
/// <inheritdoc cref="IPath.IsPathRooted(ReadOnlySpan{char})" />
public bool IsPathRooted(ReadOnlySpan<char> path)
=> IsPathRooted(path.ToString());
#endif
/// <inheritdoc cref="IPath.IsPathRooted(string)" />
public abstract bool IsPathRooted(string? path);
#if FEATURE_PATH_JOIN
/// <inheritdoc cref="IPath.Join(ReadOnlySpan{char}, ReadOnlySpan{char})" />
public string Join(ReadOnlySpan<char> path1, ReadOnlySpan<char> path2)
=> JoinInternal([path1.ToString(), path2.ToString()]);
#endif
#if FEATURE_PATH_JOIN
/// <inheritdoc cref="IPath.Join(ReadOnlySpan{char}, ReadOnlySpan{char}, ReadOnlySpan{char})" />
public string Join(ReadOnlySpan<char> path1,
ReadOnlySpan<char> path2,
ReadOnlySpan<char> path3)
=> JoinInternal([path1.ToString(), path2.ToString(), path3.ToString()]);
#endif
#if FEATURE_PATH_ADVANCED
/// <inheritdoc cref="IPath.Join(ReadOnlySpan{char}, ReadOnlySpan{char}, ReadOnlySpan{char}, ReadOnlySpan{char})" />
public string Join(ReadOnlySpan<char> path1,
ReadOnlySpan<char> path2,
ReadOnlySpan<char> path3,
ReadOnlySpan<char> path4)
=> JoinInternal(
[path1.ToString(), path2.ToString(), path3.ToString(), path4.ToString()]);
#endif
#if FEATURE_PATH_ADVANCED
/// <inheritdoc cref="IPath.Join(string, string)" />
public string Join(string? path1, string? path2)
=> JoinInternal([path1, path2]);
#endif
#if FEATURE_PATH_ADVANCED
/// <inheritdoc cref="IPath.Join(string, string, string)" />
public string Join(string? path1, string? path2, string? path3)
=> JoinInternal([path1, path2, path3]);
#endif
#if FEATURE_PATH_ADVANCED
/// <inheritdoc cref="IPath.Join(string, string, string, string)" />
public string Join(string? path1, string? path2, string? path3, string? path4)
=> JoinInternal([path1, path2, path3, path4]);
#endif
#if FEATURE_PATH_ADVANCED
/// <inheritdoc cref="IPath.Join(string[])" />
public string Join(params string?[] paths)
=> JoinInternal(paths);
#endif
#if FEATURE_PATH_ADVANCED
/// <inheritdoc cref="IPath.TrimEndingDirectorySeparator(ReadOnlySpan{char})" />
public ReadOnlySpan<char> TrimEndingDirectorySeparator(ReadOnlySpan<char> path)
=> TrimEndingDirectorySeparator(path.ToString());
#endif
#if FEATURE_PATH_ADVANCED
/// <inheritdoc cref="IPath.TrimEndingDirectorySeparator(string)" />
public string TrimEndingDirectorySeparator(string path)
=> System.IO.Path.TrimEndingDirectorySeparator(path);
#endif
#if FEATURE_PATH_JOIN
/// <inheritdoc cref="IPath.TryJoin(ReadOnlySpan{char}, ReadOnlySpan{char}, Span{char}, out int)" />
public bool TryJoin(ReadOnlySpan<char> path1,
ReadOnlySpan<char> path2,
Span<char> destination,
out int charsWritten)
{
string result = Join(path1, path2);
if (destination.Length < result.Length)
{
charsWritten = 0;
return false;
}
result.AsSpan().CopyTo(destination);
charsWritten = result.Length;
return true;
}
#endif
#if FEATURE_PATH_JOIN
/// <inheritdoc cref="IPath.TryJoin(ReadOnlySpan{char}, ReadOnlySpan{char}, ReadOnlySpan{char}, Span{char}, out int)" />
public bool TryJoin(ReadOnlySpan<char> path1,
ReadOnlySpan<char> path2,
ReadOnlySpan<char> path3,
Span<char> destination,
out int charsWritten)
{
string result = Join(path1, path2, path3);
if (destination.Length < result.Length)
{
charsWritten = 0;
return false;
}
result.AsSpan().CopyTo(destination);
charsWritten = result.Length;
return true;
}
#endif
#endregion
private string CombineInternal(string[] paths)
{
string NormalizePath(string path, bool ignoreStartingSeparator)
{
if (!ignoreStartingSeparator && (
path[0] == DirectorySeparatorChar ||
path[0] == AltDirectorySeparatorChar))
{
path = path.Substring(1);
}
if (path[path.Length - 1] == DirectorySeparatorChar ||
path[path.Length - 1] == AltDirectorySeparatorChar)
{
path = path.Substring(0, path.Length - 1);
}
return NormalizeDirectorySeparators(path);
}
if (paths == null)
{
throw new ArgumentNullException(nameof(paths));
}
StringBuilder sb = new();
bool isFirst = true;
bool endsWithDirectorySeparator = false;
foreach (string path in paths)
{
if (path == null)
{
throw new ArgumentNullException(nameof(paths));
}
if (string.IsNullOrEmpty(path))
{
continue;
}
if (IsPathRooted(path))
{
sb.Clear();
isFirst = true;
}
sb.Append(NormalizePath(path, isFirst));
sb.Append(DirectorySeparatorChar);
endsWithDirectorySeparator = path.EndsWith(DirectorySeparatorChar) ||
path.EndsWith(AltDirectorySeparatorChar);
}
if (!endsWithDirectorySeparator)
{
return sb.ToString(0, sb.Length - 1);
}
return sb.ToString();
}
protected abstract int GetRootLength(string path);
protected abstract bool IsDirectorySeparator(char c);
protected abstract bool IsEffectivelyEmpty(string path);
#if FEATURE_PATH_JOIN || FEATURE_PATH_ADVANCED
private string JoinInternal(string?[] paths)
{
if (paths == null)
{
throw new ArgumentNullException(nameof(paths));
}
if (paths.Length == 0)
{
return string.Empty;
}
StringBuilder sb = new();
foreach (string? path in paths)
{
if (string.IsNullOrEmpty(path))
{
continue;
}
if (sb.Length == 0)
{
sb.Append(path);
}
else
{
if (!IsDirectorySeparator(sb[sb.Length - 1]) && !IsDirectorySeparator(path[0]))
{
sb.Append(DirectorySeparatorChar);
}
sb.Append(path);
}
}
return sb.ToString();
}
#endif
protected abstract string NormalizeDirectorySeparators(string path);
protected string RandomString(int length)
{
const string chars = "abcdefghijklmnopqrstuvwxyz0123456789";
return new string(Enumerable.Repeat(chars, length)
.Select(s => s[fileSystem.RandomSystem.Random.Shared.Next(s.Length)]).ToArray());
}
private bool TryGetExtensionIndex(string path, [NotNullWhen(true)] out int? dotIndex)
{
for (int i = path.Length - 1; i >= 0; i--)
{
char ch = path[i];
if (ch == '.')
{
dotIndex = i;
return true;
}
if (IsDirectorySeparator(ch))
{
break;
}
}
dotIndex = null;
return false;
}
}
}