-
Notifications
You must be signed in to change notification settings - Fork 190
Expand file tree
/
Copy pathUtilTest.cs
More file actions
1225 lines (1050 loc) · 49.6 KB
/
Copy pathUtilTest.cs
File metadata and controls
1225 lines (1050 loc) · 49.6 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
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
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2017-2018, 2020-2021 Ubisoft Entertainment
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using NUnit.Framework;
namespace Sharpmake.UnitTests
{
namespace NiceTypeNameTest
{
internal class DummyClass { };
internal class DummyClass2 { };
internal class DummyGeneric<T> { };
internal class DummyGeneric2<T, U> { };
public class NiceTypeName
{
[Test]
public void NiceTypeNameOnSimpleType()
{
Assert.That(typeof(DummyClass).ToNiceTypeName(), Is.EqualTo("Sharpmake.UnitTests.NiceTypeNameTest.DummyClass"));
Assert.That(typeof(DummyClass2).ToNiceTypeName(), Is.EqualTo("Sharpmake.UnitTests.NiceTypeNameTest.DummyClass2"));
}
[Test]
public void NiceTypeNameOnGenericType()
{
Assert.That(typeof(DummyGeneric<DummyClass>).ToNiceTypeName(),
Is.EqualTo("Sharpmake.UnitTests.NiceTypeNameTest.DummyGeneric<Sharpmake.UnitTests.NiceTypeNameTest.DummyClass>"));
Assert.That(typeof(DummyGeneric2<DummyClass, DummyClass2>).ToNiceTypeName(),
Is.EqualTo("Sharpmake.UnitTests.NiceTypeNameTest.DummyGeneric2<Sharpmake.UnitTests.NiceTypeNameTest.DummyClass,Sharpmake.UnitTests.NiceTypeNameTest.DummyClass2>"));
}
}
}
public class PathMakeStandard
{
[Test]
public void LeavesEmptyStringsUntouched()
{
Assert.That(Util.PathMakeStandard(string.Empty), Is.EqualTo(string.Empty));
Assert.That(Util.PathMakeStandard(""), Is.EqualTo(string.Empty));
Assert.That(Util.PathMakeStandard(""), Is.EqualTo(""));
}
[Test]
public void LeavesVariablesUntouched()
{
string expectedResult = "$(Console_SdkPackagesRoot)";
Assert.That(Util.PathMakeStandard("$(Console_SdkPackagesRoot)"), Is.EqualTo(expectedResult));
}
[Test]
public void ProcessesPathWithTrailingBackslash()
{
string expectedResult = Path.Combine("rd", "project", "dev", "projects", "sharpmake", "..", "..", "extern", "Geometrics");
Assert.That(Util.PathMakeStandard(@"rd\project\dev\projects\sharpmake\..\..\extern\Geometrics\"), Is.EqualTo(expectedResult));
}
[Test]
public void ProcessesPathWithTrailingBackslashAndADot()
{
var expectedResult = Path.Combine("rd", "project", "dev", "projects", "sharpmake", "..", "..", "extern", "Microsoft.CNG", "Lib");
Assert.That(Util.PathMakeStandard(@"rd\project\dev\projects\sharpmake\..\..\extern\Microsoft.CNG\Lib\"), Is.EqualTo(expectedResult));
}
[Test]
public void ProcessesPathWithMultipleTrailingBackslashes()
{
var expectedResult = Path.Combine("rd", "project", "dev", "projects", "sharpmake", "..", "..", "extern", "Microsoft.CNG", "Lib");
Assert.That(Util.PathMakeStandard(@"rd\project\dev\projects\sharpmake\..\..\extern\Microsoft.CNG\Lib\\\"), Is.EqualTo(expectedResult));
}
/// <summary>
/// Verify that the strings from the list were format as path
/// <remark><c>PathMakeStandard</c> lower the path on Windows</remark>
/// </summary>
[Test]
public void ProcessesWithAListAsArgument()
{
IList<string> listString = new List<string>()
{
Path.Combine("F:","SharpMake","sharpmake","Sharpmake.Application"),
Path.Combine("F:","SharpMake","sharpmake","Sharpmake.Extensions")
};
var expectedList = listString;
Util.PathMakeStandard(listString);
Assert.AreEqual(expectedList, listString);
}
}
public class SimplifyPath
{
/// <summary>
/// Verify that an error is thrown when a path begin with three dots
/// </summary>
[Test]
public void ThrowsErrorDot()
{
Assert.Throws<ArgumentException>(() => Util.SimplifyPath(".../sharpmake/README.md"));
}
/// <summary>
/// Verify that an error is thrown when a path start with dots but no slash
/// </summary>
[Test]
public void ThrowsErrorSeparator()
{
Assert.Throws<ArgumentException>(() => Util.SimplifyPath("..sharpmake/README.md"));
}
[Test]
public void LeavesEmptyStringsUntouched()
{
Assert.That(Util.SimplifyPath(string.Empty), Is.EqualTo(string.Empty));
Assert.That(Util.SimplifyPath(""), Is.EqualTo(string.Empty));
Assert.That(Util.SimplifyPath(""), Is.EqualTo(""));
}
[Test]
public void HandlesPathRelativeToCurrentFolder()
{
Assert.That(Util.SimplifyPath(@".\project\test.cpp"),
Is.EqualTo(Path.Combine("project", "test.cpp")));
Assert.That(Util.SimplifyPath(@".\.\.\.\project\.\test.cpp"),
Is.EqualTo(Path.Combine("project", "test.cpp")));
}
[Test]
public void HandlesReturningToParentFolder()
{
Assert.That(Util.SimplifyPath(@"test\..\test.cpp"),
Is.EqualTo("test.cpp"));
}
[Test]
public void HandlesReturningToParentFolderRelativeToCurrentFolder()
{
Assert.That(Util.SimplifyPath(@".\project\..\test.cpp"),
Is.EqualTo("test.cpp"));
Assert.That(Util.SimplifyPath(@".\.\.\.\project\..\test.cpp"),
Is.EqualTo("test.cpp"));
}
[Test]
public void CollapsesMultipleFolderSeparators()
{
Assert.That(Util.SimplifyPath(@".\\\project\..\test.cpp"),
Is.EqualTo("test.cpp"));
Assert.That(Util.SimplifyPath(@"\\\folder"),
Is.EqualTo("folder"));
}
[Test]
public void HandlesSlashesInFullPath()
{
var currentDirectory = Directory.GetCurrentDirectory();
Assert.That(Util.SimplifyPath(currentDirectory + "\\main/test//t.cpp"),
Is.EqualTo(Path.Combine(currentDirectory, "main", "test", "t.cpp")));
}
[Test]
public void HandlesFolderParentsAtTheEnd()
{
Assert.That(Util.SimplifyPath(@"alpha\beta\gamma\sigma\omega\zeta\..\.."),
Is.EqualTo(Path.Combine("alpha", "beta", "gamma", "sigma")));
}
[Test]
public void LeavesCleanPathUntouched()
{
// Check that we do not change dot and dot dot
Assert.That(".", Is.EqualTo(Util.SimplifyPath(".")));
Assert.That("..", Is.EqualTo(Util.SimplifyPath("..")));
Assert.That(Util.SimplifyPath(Util.PathMakeStandard(@"alpha\beta\gamma\sigma\omega\zeta\lambda\phi\")),
Is.EqualTo(Path.Combine("alpha", "beta", "gamma", "sigma", "omega", "zeta", "lambda", "phi")));
}
}
/// <summary>
/// Mock <c>GetEnvironmentVariable</c> by reproducing the logic to make it works on Windows and Linux
/// The test cases are:
/// <list type="number">
/// <item><description>Testing when an environment variable doesn't exist the default value is returned</description></item>
/// <item><description>Testing when an environment variable exists</description></item>
/// </list>
/// </summary>
public class MockEnvironmentVariable
{
private const string DefaultValue = "Default";
private const string VariableName = "SharpmakeDoNotExists";
private const string ExpectedVariableValue = "Variable exists";
[Test]
public void GetEnvironmentVariableNotExisting()
{
string output = null;
Assert.AreEqual(DefaultValue, Util.GetEnvironmentVariable(VariableName, DefaultValue, ref output, true));
}
[Test]
public void GetEnvironmentVariableExisting()
{
string output = null;
Assert.AreEqual(DefaultValue, Util.GetEnvironmentVariable(VariableName, DefaultValue, ref output, true));
Environment.SetEnvironmentVariable(VariableName, ExpectedVariableValue);
output = null;
Assert.AreEqual(ExpectedVariableValue, Util.GetEnvironmentVariable(VariableName, DefaultValue, ref output, true));
Environment.SetEnvironmentVariable(VariableName, null);
output = null;
Assert.AreEqual(DefaultValue, Util.GetEnvironmentVariable(VariableName, DefaultValue, ref output, true));
}
}
public class MockPath
{
/// <summary>
/// Verify that the path recovered their original format after being lowered.
/// </summary>
[Test]
public void PathGetCapitalizedFile()
{
var mockPath1 = Util.GetCapitalizedPath(Path.GetTempFileName());
var mockPath2 = Util.GetCapitalizedPath(Path.GetTempFileName());
OrderableStrings paths = new OrderableStrings
{
mockPath1,
mockPath2
};
OrderableStrings pathsToLower = new OrderableStrings(paths);
OrderableStrings pathsToUpper = new OrderableStrings(paths.Select((p) => p.ToUpper()));
pathsToLower.ToLower();
Assert.AreEqual(paths, Util.PathGetCapitalized(pathsToLower));
Assert.AreEqual(paths, Util.PathGetCapitalized(pathsToUpper));
File.Delete(mockPath1);
File.Delete(mockPath2);
}
/// <summary>
/// Verify that the path recovered their original format after being lowered.
/// </summary>
[Test]
public void PathGetCapitalizedDirectory()
{
string temp = Util.GetCapitalizedPath(Path.GetTempPath());
var tempDirectory1 = Directory.CreateDirectory(temp + @"\test1");
var tempDirectory2 = Directory.CreateDirectory(temp + @"\test2");
OrderableStrings paths = new OrderableStrings
{
tempDirectory1.FullName.Replace("test1", "TEST1"),
tempDirectory2.FullName.Replace("test2", "TEST2")
};
OrderableStrings expectedOutput = new OrderableStrings
{
tempDirectory1.FullName,
tempDirectory2.FullName
};
Assert.AreEqual(expectedOutput, Util.PathGetCapitalized(paths));
Directory.Delete(tempDirectory1.FullName);
Directory.Delete(tempDirectory2.FullName);
}
}
public class MockFile
{
/// <summary>
/// Verify that it returns the name of the current file
/// </summary>
[Test]
public void GetCurrentSharpMakeFileInfo()
{
string[] listFileInfo = Util.GetCurrentSharpmakeFileInfo().FullName.Split('\\');
Assert.AreEqual("UtilTest.cs", listFileInfo[listFileInfo.Length - 1]);
}
/// <summary>
/// Verify the right extensions are returned
/// </summary>
[Test]
public void GetTextTemplateDirectiveParam()
{
var mockPath = Path.GetTempFileName();
File.WriteAllLines(mockPath, new[] { "<#@ output extension=\".txt\" #>", "<#@ log extension=\".dll\" #>" });
Assert.AreEqual(".txt", Util.GetTextTemplateDirectiveParam(mockPath, "output", "extension"));
Assert.AreEqual(".dll", Util.GetTextTemplateDirectiveParam(mockPath, "log", "extension"));
File.Delete(mockPath);
}
/// <summary>
/// <c>FileWriteIfDifferentInternal</c> verify if the MemoryStream and the file have different values
/// The test cases are:
/// <list type="number">
/// <item><description>Testing when the file is readonly</description></item>
/// <item><description>Testing when the memorystream and the file are the same</description></item>
/// <item><description>Testing when the memorystream and the file are different</description></item>
/// </list>
/// </summary>
[Test]
public void FileWriteIfDifferentInternal()
{
var mockPath1 = Path.GetTempFileName();
var mockPath2 = Path.GetTempFileName();
var mockPath3 = Path.GetTempFileName();
File.WriteAllLines(mockPath1, new[] { "test", "memory", "stream" });
File.WriteAllLines(mockPath2, new[] { "test", "file", "wrap" });
File.WriteAllLines(mockPath3, new[] { "test", "memory", "streams" });
FileInfo fileInfo = new FileInfo(mockPath1);
fileInfo.IsReadOnly = true;
MemoryStream memoryStream1 = new MemoryStream(File.ReadAllBytes(mockPath1));
MemoryStream memoryStream2 = new MemoryStream(File.ReadAllBytes(mockPath2));
MemoryStream memoryStream3 = new MemoryStream(File.ReadAllBytes(mockPath3));
Assert.False(Util.FileWriteIfDifferentInternal(fileInfo, memoryStream1, true));
fileInfo.IsReadOnly = false;
Assert.False(Util.FileWriteIfDifferentInternal(fileInfo, memoryStream1, true));
Assert.True(Util.FileWriteIfDifferentInternal(fileInfo, memoryStream2, true));
Assert.True(Util.FileWriteIfDifferentInternal(fileInfo, memoryStream3, true));
fileInfo.Delete();
File.Delete(mockPath1);
File.Delete(mockPath2);
File.Delete(mockPath3);
}
/// <summary>
/// Verify that the contains of the source mock file was copied in the destination mock file
/// </summary>
[Test]
public void ForceCopy()
{
var mockPathSource = Path.GetTempFileName();
var mockPathDest = Path.GetTempFileName();
var mockPathExpected = Path.GetTempFileName();
File.WriteAllLines(mockPathSource, new[] { "MockFile" });
File.WriteAllBytes(mockPathExpected, File.ReadAllBytes(mockPathSource));
Util.ForceCopy(mockPathSource, mockPathDest);
Assert.AreEqual(File.ReadAllText(mockPathExpected), File.ReadAllText(mockPathDest));
File.WriteAllLines(mockPathSource, new[] { "MockFile Test" });
File.WriteAllBytes(mockPathExpected, File.ReadAllBytes(mockPathSource));
Util.ForceCopy(mockPathSource, mockPathDest);
Assert.AreEqual(File.ReadAllText(mockPathExpected), File.ReadAllText(mockPathDest));
File.Delete(mockPathSource);
File.Delete(mockPathDest);
File.Delete(mockPathExpected);
}
/// <summary>
/// Verify that a normal file is delete
/// </summary>
[Test]
public void TryDeleteFile()
{
var mockPath = Path.GetTempFileName();
Assert.True(Util.TryDeleteFile(mockPath, true));
File.Delete(mockPath);
}
/// <summary>
/// Verify that a read only file is not deleted
/// <remark>Changing ReadOnly attribute in the Linux pipeline doesn't work so the test is discard on Mono</remark>
/// </summary>
[Test]
public void TryDeleteReadOnlyFile()
{
if (!Util.IsRunningInMono())
{
var mockPath = Path.GetTempFileName();
var fileInfoWrap = new FileInfo(mockPath);
fileInfoWrap.IsReadOnly = true;
Assert.True(fileInfoWrap.IsReadOnly);
Assert.False(Util.TryDeleteFile(mockPath));
fileInfoWrap.IsReadOnly = false;
fileInfoWrap.Delete();
File.Delete(mockPath);
}
}
/// <summary>
/// Verify that the custom message is returned
/// </summary>
[Test]
public void GetCompleteExceptionMessage()
{
string expectedOutput = "Exception Message";
Exception e = new Exception(expectedOutput);
Assert.True(Util.GetCompleteExceptionMessage(e, " ").Contains(expectedOutput));
}
}
public class ProjectEnvironment
{
/// <summary>
/// Verify an exception is thrown on a not supported tool version
/// </summary>
[Test]
public void GetToolVersionStringException()
{
Assert.Catch<Error>(() => Util.GetToolVersionString(DevEnv.xcode4ios));
Assert.Catch<Error>(() => Util.GetToolVersionString(DevEnv.eclipse));
Assert.Catch<Error>(() => Util.GetToolVersionString(DevEnv.make));
}
/// <summary>
/// Verify the right managed project platform name was returned depending on the platform and the project type
/// </summary>
[Test]
public void GetPlatformString()
{
Assert.AreEqual("x86", Util.GetPlatformString(Platform.win32, new CSharpProject(), null, false));
Assert.AreEqual("x64", Util.GetPlatformString(Platform.win64, new CSharpProject(), null, false));
Assert.AreEqual("AnyCPU", Util.GetPlatformString(Platform.win64, new PythonProject(), null, false));
Assert.AreEqual("Any CPU", Util.GetPlatformString(Platform.win64, new PythonProject(), null, true));
Assert.AreEqual("x64", Util.GetPlatformString(Platform.win64, new AndroidPackageProject(), null, true));
}
/// <summary>
/// Verify that an exception is thrown if a platform is not supported
/// </summary>
[Test]
public void GetPlatformStringException()
{
Assert.Catch<Exception>(() => Util.GetPlatformString(Platform.android, new CSharpProject(), null, false));
}
}
public class FakeTree
{
[SetUp]
public void Init()
{
Util.FakePathPrefix = Path.GetDirectoryName(new Uri(System.Reflection.Assembly.GetExecutingAssembly().Location).LocalPath);
string[] files =
{
"./data/mod.el",
"./code/test.h",
"code/test.cpp",
@".\code\main\main.cpp",
"./code/test/stuff.cpp"
};
foreach (string filePath in files.Select(Util.PathMakeStandard))
{
Util.AddNewFakeFile(filePath, 0);
}
}
[TearDown]
public void Shutdown()
{
Util.ClearFakeTree();
}
[Test, Repeat(2)]
public void KeepsACountOfFakeFiles()
{
// Repetition is to ensure Shutdown() is restoring the global context
// and not adding each time the Setup() is done
Assert.That(Util.CountFakeFiles(), Is.EqualTo(5));
}
[Test]
public void CanEmulateDirectories()
{
var directory = Path.Combine(Util.FakePathPrefix, "code");
Assert.That(Util.DirectoryExists(directory), Is.True);
var subDirectory = Path.Combine(Util.FakePathPrefix, "code", "main");
Assert.That(Util.DirectoryExists(subDirectory), Is.True);
var missingDirectory = Path.Combine(Util.FakePathPrefix, "doesnotexist");
Assert.That(Util.DirectoryExists(missingDirectory), Is.False);
}
[Test]
public void IsCaseInsensitive()
{
var directoryLower = Path.Combine(Util.FakePathPrefix, "code");
var directoryUpper = Path.Combine(Util.FakePathPrefix, "CODE");
Assert.That(Util.DirectoryExists(directoryLower), Is.True);
Assert.That(Util.DirectoryExists(directoryUpper), Is.True);
}
[Test]
public void CanListDirectories()
{
string[] expected =
{
Path.Combine(Util.FakePathPrefix, "code", "test"),
Path.Combine(Util.FakePathPrefix, "code", "main")
};
var result = Util.DirectoryGetDirectories(Path.Combine(Util.FakePathPrefix, "code"));
Assert.That(result, Is.EquivalentTo(expected));
}
[Test]
public void CanListDirectoriesWithFilter()
{
string[] expected =
{
Path.Combine(Util.FakePathPrefix, "data"),
Path.Combine(Util.FakePathPrefix, "code")
};
var result = Util.DirectoryGetDirectories(Path.Combine(Util.FakePathPrefix), "*d*");
Assert.That(result, Is.EquivalentTo(expected));
}
[Test]
public void CanListDirectoriesWithFilterAndSearchOption()
{
string[] expected =
{
Path.Combine(Util.FakePathPrefix, "data"),
Path.Combine(Util.FakePathPrefix, "code"),
Path.Combine(Util.FakePathPrefix, "code", "main"),
Path.Combine(Util.FakePathPrefix, "code", "test")
};
var result = Util.DirectoryGetDirectories(Path.Combine(Util.FakePathPrefix), "????", SearchOption.AllDirectories);
Assert.That(result, Is.EquivalentTo(expected));
}
[Test]
public void CanListFiles()
{
string[] expected =
{
Path.Combine(Util.FakePathPrefix, "code", "test.h"),
Path.Combine(Util.FakePathPrefix, "code", "test.cpp"),
Path.Combine(Util.FakePathPrefix, "code", "main", "main.cpp"),
Path.Combine(Util.FakePathPrefix, "code", "test", "stuff.cpp")
};
var result = Util.DirectoryGetFiles(Path.Combine(Util.FakePathPrefix, "code"));
Assert.That(result, Is.EquivalentTo(expected));
}
[Test]
public void CanListFilesWithFilter()
{
string[] expected =
{
Path.Combine(Util.FakePathPrefix, "code", "test.cpp"),
Path.Combine(Util.FakePathPrefix, "code", "main", "main.cpp"),
Path.Combine(Util.FakePathPrefix, "code", "test", "stuff.cpp")
};
var result = Util.DirectoryGetFiles(Path.Combine(Util.FakePathPrefix, "code"), "*.cpp");
Assert.That(result, Is.EquivalentTo(expected));
}
[Test]
public void CanListFilesWithFilterAndSearchOption()
{
string[] expected =
{
Path.Combine(Util.FakePathPrefix, "code", "test.cpp"),
};
var result = Util.DirectoryGetFiles(Path.Combine(Util.FakePathPrefix, "code"), "*.cpp", SearchOption.TopDirectoryOnly);
Assert.That(result, Is.EquivalentTo(expected));
}
[Test]
public void CanListFilesInSubDirectory()
{
string[] expected =
{
Path.Combine(Util.FakePathPrefix, "code", "main", "main.cpp")
};
var result = Util.DirectoryGetFiles(Path.Combine(Util.FakePathPrefix, "code", "main"));
Assert.That(result, Is.EquivalentTo(expected));
}
[Test]
public void TestPathWithWildcards()
{
Assert.IsTrue(Util.IsPathWithWildcards(Path.Combine("test", "*test", "test")));
Assert.IsTrue(Util.IsPathWithWildcards(Path.Combine("test", "*test**", "test")));
Assert.IsTrue(Util.IsPathWithWildcards(Path.Combine("test", "tes?t", "test")));
Assert.IsTrue(Util.IsPathWithWildcards(Path.Combine("test", "tes??t", "test")));
Assert.IsFalse(Util.IsPathWithWildcards(Path.Combine("test", "test", "test")));
}
[Test]
public void ErrorListFileWithWildcards()
{
Assert.Catch<ArgumentException>(() => Util.DirectoryGetFilesWithWildcards(Path.Combine(Util.FakePathPrefix, "test")));
}
[Test]
public void CanListFileWithWildcards_WithDotDot1()
{
string[] expected =
{
Path.Combine(Util.FakePathPrefix, "code", "test.h"),
Path.Combine(Util.FakePathPrefix, "code", "test.cpp")
};
var result = Util.DirectoryGetFilesWithWildcards(Path.Combine(Util.FakePathPrefix, "code", "inexistantFolder", "..", "test*"));
Assert.That(result, Is.EquivalentTo(expected));
}
[Test]
public void CanListFileWithWildcards_WithDotDot2()
{
string[] expected =
{
Path.Combine(Util.FakePathPrefix, "code", "test.cpp")
};
var result1 = Util.DirectoryGetFilesWithWildcards(Path.Combine(Util.FakePathPrefix, "code", "????", "..", "*.cpp"));
Assert.That(result1, Is.EquivalentTo(expected));
var result2 = Util.DirectoryGetFilesWithWildcards(Path.Combine(Util.FakePathPrefix, "code", "????", "..", "test.cpp"));
Assert.That(result2, Is.EquivalentTo(expected));
}
[Test]
public void CanListFileWithWildcards1()
{
string[] expected =
{
Path.Combine(Util.FakePathPrefix, "code", "test.h"),
Path.Combine(Util.FakePathPrefix, "code", "test.cpp")
};
var result = Util.DirectoryGetFilesWithWildcards(Path.Combine(Util.FakePathPrefix, "code", "test*"));
Assert.That(result, Is.EquivalentTo(expected));
}
[Test]
public void CanListFileWithWildcards2()
{
string[] expected =
{
Path.Combine(Util.FakePathPrefix, "code", "main", "main.cpp"),
Path.Combine(Util.FakePathPrefix, "code", "test", "stuff.cpp")
};
var result = Util.DirectoryGetFilesWithWildcards(Path.Combine(Util.FakePathPrefix, "code", "*", "*.cpp"));
Assert.That(result, Is.EquivalentTo(expected));
}
[Test]
public void CanListFileWithWildcards3()
{
string[] expected =
{
Path.Combine(Util.FakePathPrefix, "code", "main", "main.cpp"),
Path.Combine(Util.FakePathPrefix, "code", "test", "stuff.cpp")
};
var result = Util.DirectoryGetFilesWithWildcards(Path.Combine(Util.FakePathPrefix, "c*de", "????", "*.cpp"));
Assert.That(result, Is.EquivalentTo(expected));
}
[Test]
public void CanListFileWithWildcards4()
{
string[] expected =
{
Path.Combine(Util.FakePathPrefix, "code", "main", "main.cpp")
};
var result = Util.DirectoryGetFilesWithWildcards(Path.Combine(Util.FakePathPrefix, "code", "*", "main.cpp"));
Assert.That(result, Is.EquivalentTo(expected));
}
[Test]
public void CanListFileWithWildcards_NoMatch()
{
// Last file doesn't exist in test folder
Assert.IsEmpty(Util.DirectoryGetFilesWithWildcards(Path.Combine(Util.FakePathPrefix, "c?de", "test", "main.cpp")));
// No folder with only one character exist
Assert.IsEmpty(Util.DirectoryGetFilesWithWildcards(Path.Combine(Util.FakePathPrefix, "code", "?", "main.cpp")));
// No file with only one character exist
Assert.IsEmpty(Util.DirectoryGetFilesWithWildcards(Path.Combine(Util.FakePathPrefix, "code", "*", "?")));
}
}
[TestFixture]
public class ReferencePath
{
[Test]
public void CanBeComputedFromOutputPath()
{
const string outputFileFullPath = @"F:\OnePath\With\Output\with\a\file.cs";
const string outputPath = @"F:\OnePath\With\Output\";
const string referencePath = @"F:\OnePath\With\Reference\";
var referenceFileFullPath = outputFileFullPath.ReplaceHeadPath(outputPath, referencePath);
Assert.That(referenceFileFullPath, Is.EqualTo(
Util.PathMakeStandard(@"F:\OnePath\With\Reference\with\a\file.cs")));
}
[Test]
public void IsCaseInsensitiveButPreservesCase()
{
const string outputFileFullPath = @"F:\OnePath\With\Output\with\a\File.cs";
const string outputPath = @"f:\OnePath\with\output\";
const string referencePath = @"F:\OnePath\with\Reference\";
var referenceFileFullPath = outputFileFullPath.ReplaceHeadPath(outputPath, referencePath);
Assert.That(referenceFileFullPath, Is.EqualTo(
Util.PathMakeStandard(@"F:\OnePath\with\Reference\with\a\File.cs")));
}
[Test]
public void AcceptsOutputPathWithoutTrailingSlash()
{
const string outputFileFullPath = @"F:\OnePath\With\Output\with\a\file.cs";
const string outputPath = @"F:\OnePath\With\Output";
const string referencePath = @"F:\OnePath\With\Reference\";
var referenceFileFullPath = outputFileFullPath.ReplaceHeadPath(outputPath, referencePath);
Assert.That(referenceFileFullPath, Is.EqualTo(
Util.PathMakeStandard(@"F:\OnePath\With\Reference\with\a\file.cs")));
}
}
[TestFixture]
public class SymbolicLink
{
/// <summary>
/// <c>CreateSymbolicLink</c> create a symbolic link
/// The test cases are:
/// <list type="number">
/// <item><description>Testing that a symbolic link is not created on a temporary directory pointing itself</description></item>
/// <item><description>Testing that a symbolic link is created on a temporary directory pointing an other directory</description></item>
/// </list>
/// <remark>Implementation of create symbolic links doesn't work on linux so this test is discard on Mono</remark>
/// </summary>
[Test]
[Ignore("Test Broken")]
public void CreateSymbolicLinkOnDirectory()
{
if (!Util.IsRunningInMono())
{
var tempDirectory1 = Directory.CreateDirectory(Path.GetTempPath() + Path.DirectorySeparatorChar + "test-source");
var tempDirectory2 = Directory.CreateDirectory(Path.GetTempPath() + Path.DirectorySeparatorChar + "test-destination");
Assert.False(Util.CreateSymbolicLink(Path.GetTempPath(), Path.GetTempPath(), true));
Assert.False(tempDirectory1.Attributes.HasFlag(FileAttributes.ReparsePoint));
Assert.True(Util.CreateSymbolicLink(tempDirectory1.FullName, tempDirectory2.FullName, true));
Assert.True(tempDirectory1.Attributes.HasFlag(FileAttributes.ReparsePoint));
Directory.Delete(tempDirectory1.FullName);
Directory.Delete(tempDirectory2.FullName);
}
}
/// <summary>
/// <c>IsSymbolicLink</c> verify if a file has a symbolic link
/// The test cases are:
/// <list type="number">
/// <item><description>Testing that the method detected the absence of a symbolic link</description></item>
/// <item><description>Testing that the method detected the presence of a symbolic link</description></item>
/// </list>
/// <remark>Implementation of create symbolic links doesn't work on linux so this test is discard on Mono</remark>
/// </summary>
[Test]
[Ignore("Test Broken")]
public void IsSymbolicLink()
{
if (!Util.IsRunningInMono())
{
var mockPath1 = Path.GetTempFileName();
var mockPath2 = Path.GetTempFileName();
Assert.False(Util.IsSymbolicLink(mockPath1));
Assert.True(Util.CreateSymbolicLink(mockPath1, mockPath2, false));
Assert.True(Util.IsSymbolicLink(mockPath1));
File.Delete(mockPath1);
File.Delete(mockPath2);
}
}
}
public class StringsOperations
{
/// <summary>
/// Verify that:
/// <list type="number">
/// <item><description>Verify that -1 is returned when two versions are different</description></item>
/// <item><description>Verify that 0 is returned when two versions are equal</description></item>
/// <item><description>Verify that 1 is returned when two versions are different</description></item>
/// </list>
/// </summary>
[Test]
public void VersionStringComparer()
{
var versionArray = new[] { "10.2.0", "10.2.9", "11.2.6" };
IComparer<string> comparer = new Util.VersionStringComparer();
Assert.AreEqual(-1, comparer.Compare(versionArray[0], versionArray[1]));
Assert.AreEqual(1, comparer.Compare(versionArray[1], versionArray[0]));
Assert.AreEqual(0, comparer.Compare(versionArray[0], versionArray[0]));
Assert.AreEqual(-1, comparer.Compare(versionArray[1], versionArray[2]));
Assert.AreEqual(1, comparer.Compare(versionArray[2], versionArray[0]));
}
/// <summary>
/// Verify that the separator was added between elements and the following characters were escaped: <, > and &
/// </summary>
[Test]
public void JoinStringsCollectionSeparator()
{
List<string> list1 = new List<string>()
{
"a",
"b",
"c",
"d"
};
List<string> list2 = new List<string>()
{
"a&",
"b<",
"c>",
"d"
};
Assert.AreEqual("a-b-c-d", Util.JoinStrings(list1, "-", false));
Assert.AreEqual("a&*b<*c>*d", Util.JoinStrings(list2, "*", true));
}
/// <summary>
/// Verify that the separator and prefix was added between elements and the following characters were escaped: <, > and &
/// </summary>
[Test]
public void JoinStringsCollectionSeparatorPrefix()
{
List<string> list1 = new List<string>()
{
"a",
"b",
"c",
"d"
};
List<string> list2 = new List<string>()
{
"a&",
"b<",
"c>",
"d"
};
Assert.AreEqual("prefixa-prefixb-prefixc-prefixd", Util.JoinStrings(list1, "-", "prefix", false));
Assert.AreEqual("prefixa&*prefixb<*prefixc>*prefixd", Util.JoinStrings(list2, "*", "prefix", true));
}
/// <summary>
/// Verify that the separator, suffix and prefix was added between elements and the following characters were escaped: <, > and &
/// </summary>
[Test]
public void JoinStringsCollectionSeparatorPrefixSuffix()
{
List<string> list1 = new List<string>()
{
"a",
"b",
"c",
"d"
};
List<string> list2 = new List<string>()
{
"a&",
"b<",
"c>",
"d"
};
Assert.AreEqual("prefixasuffix-prefixbsuffix-prefixcsuffix-prefixdsuffix", Util.JoinStrings(list1, "-", "prefix", "suffix", false));
Assert.AreEqual("prefixa&suffix*prefixb<suffix*prefixc>suffix*prefixdsuffix", Util.JoinStrings(list2, "*", "prefix", "suffix", true));
}
/// <summary>
/// Verify that two paths with different separator and same separator are still considered equal and different path is not considered equal
/// </summary>
[Test]
public void PathIsSameDifferentSeparator()
{
var path1 = @"C:\Windows\System32\cmd.exe";
var path2 = @"C:/Windows/System32/cmd.exe";
var path3 = @"C:\Windows\local\cmd.exe";
Assert.True(Util.PathIsSame(path1, path2));
Assert.True(Util.PathIsSame(path1, path1));
Assert.False(Util.PathIsSame(path3, path2));
Assert.True(Util.PathIsSame(path2, path2));
}
/// <summary>
/// Verify that equal string return an empty result and unequal string returns a string with the different properties info
/// </summary>
[Test]
public void MakeDifferenceString()
{
ITarget target1 = new Target(Platform.win64, DevEnv.vs2017, Optimization.Release, OutputType.Dll, Blob.Blob, BuildSystem.FastBuild, DotNetFramework.v3_5);
ITarget target2 = new Target(Platform.win64, DevEnv.vs2017, Optimization.Release, OutputType.Dll, Blob.Blob, BuildSystem.FastBuild, DotNetFramework.v3_5);
ITarget target3 = new Target(Platform.win64, DevEnv.vs2017, Optimization.Debug, OutputType.Dll, Blob.Blob, BuildSystem.FastBuild, DotNetFramework.v4_5_2);
Assert.True(Util.MakeDifferenceString(target1, target2).Length == 0);
Assert.True(Util.MakeDifferenceString(target1, target3).Contains("\"net35\" and \"net452\""));
Assert.True(Util.MakeDifferenceString(target1, target3).Contains("\"Release\" and \"Debug\""));
}
/// <summary>
/// Verify that the resulted path of <c>EnsureTrailingSeparator</c> only have one separator at the end
/// </summary>
[Test]
public void EnsureTrailingSeparator()
{
var separator = Path.DirectorySeparatorChar;
List<string> paths = new List<string>{ Util.PathMakeStandard(@"c:\windows\system32\cmd.exe"),
Util.PathMakeStandard(@"c:\windows\system32\cmd.exe")+separator,
Util.PathMakeStandard(@"c:\windows\system32\cmd.exe")+separator+separator};
string expectedOutputPath = Util.PathMakeStandard(@"c:\windows\system32\cmd.exe\") + separator;
var results = paths.Select((p) => Util.EnsureTrailingSeparator(p));
Assert.True(results.All((p) => p.Equals(expectedOutputPath)));
}
/// <summary>
/// Verify that the returned file is the absolute path with the case that return a list
/// </summary>
[Test]
public void PathGetAbsoluteStringsReturnList()
{
var mockPath = Path.GetTempFileName();
string filename = Path.GetFileName(mockPath);
string stringsSource = Path.GetDirectoryName(mockPath);
Assert.AreEqual(Path.Combine(stringsSource, filename), Util.PathGetAbsolute(stringsSource, new Strings(filename))[0]);
File.Delete(mockPath);
}
/// <summary>
/// Verify that the returned file is the absolute path with the case that return a string
/// </summary>
[Test]