forked from dotnet/msbuild
/
XMake_Tests.cs
2508 lines (2134 loc) · 111 KB
/
XMake_Tests.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
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) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading;
using Microsoft.Build.CommandLine;
using Microsoft.Build.Framework;
using Microsoft.Build.Shared;
using Microsoft.Build.UnitTests.Shared;
using Xunit;
using Xunit.Abstractions;
using Shouldly;
using System.IO.Compression;
using System.Reflection;
using Microsoft.Build.Utilities;
namespace Microsoft.Build.UnitTests
{
public class XMakeAppTests
{
#if USE_MSBUILD_DLL_EXTN
private const string MSBuildExeName = "MSBuild.dll";
#else
private const string MSBuildExeName = "MSBuild.exe";
#endif
private readonly ITestOutputHelper _output;
public XMakeAppTests(ITestOutputHelper output)
{
_output = output;
}
private const string AutoResponseFileName = "MSBuild.rsp";
[Fact]
public void GatherCommandLineSwitchesTwoProperties()
{
CommandLineSwitches switches = new CommandLineSwitches();
var arguments = new List<string>();
arguments.AddRange(new string[] { "/p:a=b", "/p:c=d" });
MSBuildApp.GatherCommandLineSwitches(arguments, switches);
string[] parameters = switches[CommandLineSwitches.ParameterizedSwitch.Property];
parameters[0].ShouldBe("a=b");
parameters[1].ShouldBe("c=d");
}
[Fact]
public void GatherCommandLineSwitchesMaxCpuCountWithArgument()
{
CommandLineSwitches switches = new CommandLineSwitches();
var arguments = new List<string>();
arguments.AddRange(new string[] { "/m:2" });
MSBuildApp.GatherCommandLineSwitches(arguments, switches);
string[] parameters = switches[CommandLineSwitches.ParameterizedSwitch.MaxCPUCount];
parameters[0].ShouldBe("2");
parameters.Length.ShouldBe(1);
switches.HaveErrors().ShouldBeFalse();
}
[Fact]
public void GatherCommandLineSwitchesMaxCpuCountWithoutArgument()
{
CommandLineSwitches switches = new CommandLineSwitches();
var arguments = new List<string>();
arguments.AddRange(new string[] { "/m:3", "/m" });
MSBuildApp.GatherCommandLineSwitches(arguments, switches);
string[] parameters = switches[CommandLineSwitches.ParameterizedSwitch.MaxCPUCount];
parameters[1].ShouldBe(Convert.ToString(NativeMethodsShared.GetLogicalCoreCount()));
parameters.Length.ShouldBe(2);
switches.HaveErrors().ShouldBeFalse();
}
/// <summary>
/// /m: should be an error, unlike /m:1 and /m
/// </summary>
[Fact]
public void GatherCommandLineSwitchesMaxCpuCountWithoutArgumentButWithColon()
{
CommandLineSwitches switches = new CommandLineSwitches();
var arguments = new List<string>();
arguments.AddRange(new string[] { "/m:" });
MSBuildApp.GatherCommandLineSwitches(arguments, switches);
string[] parameters = switches[CommandLineSwitches.ParameterizedSwitch.MaxCPUCount];
parameters.Length.ShouldBe(0);
switches.HaveErrors().ShouldBeTrue();
}
/*
* Quoting Rules:
*
* A string is considered quoted if it is enclosed in double-quotes. A double-quote can be escaped with a backslash, or it
* is automatically escaped if it is the last character in an explicitly terminated quoted string. A backslash itself can
* be escaped with another backslash IFF it precedes a double-quote, otherwise it is interpreted literally.
*
* e.g.
* abc"cde"xyz --> "cde" is quoted
* abc"xyz --> "xyz" is quoted (the terminal double-quote is assumed)
* abc"xyz" --> "xyz" is quoted (the terminal double-quote is explicit)
*
* abc\"cde"xyz --> "xyz" is quoted (the terminal double-quote is assumed)
* abc\\"cde"xyz --> "cde" is quoted
* abc\\\"cde"xyz --> "xyz" is quoted (the terminal double-quote is assumed)
*
* abc"""xyz --> """ is quoted
* abc""""xyz --> """ and "xyz" are quoted (the terminal double-quote is assumed)
* abc"""""xyz --> """ is quoted
* abc""""""xyz --> """ and """ are quoted
* abc"cde""xyz --> "cde"" is quoted
* abc"xyz"" --> "xyz"" is quoted (the terminal double-quote is explicit)
*
* abc""xyz --> nothing is quoted
* abc""cde""xyz --> nothing is quoted
*/
[Fact]
public void SplitUnquotedTest()
{
List<string> sa;
int emptySplits;
// nothing quoted
sa = QuotingUtilities.SplitUnquoted("abcdxyz");
sa.Count.ShouldBe(1);
sa[0].ShouldBe("abcdxyz");
// nothing quoted
sa = QuotingUtilities.SplitUnquoted("abcc dxyz");
sa.Count.ShouldBe(2);
sa[0].ShouldBe("abcc");
sa[1].ShouldBe("dxyz");
// nothing quoted
sa = QuotingUtilities.SplitUnquoted("abcc;dxyz", ';');
sa.Count.ShouldBe(2);
sa[0].ShouldBe("abcc");
sa[1].ShouldBe("dxyz");
// nothing quoted
sa = QuotingUtilities.SplitUnquoted("abc,c;dxyz", ';', ',');
sa.Count.ShouldBe(3);
sa[0].ShouldBe("abc");
sa[1].ShouldBe("c");
sa[2].ShouldBe("dxyz");
// nothing quoted
sa = QuotingUtilities.SplitUnquoted("abc,c;dxyz", 2, false, false, out emptySplits, ';', ',');
emptySplits.ShouldBe(0);
sa.Count.ShouldBe(2);
sa[0].ShouldBe("abc");
sa[1].ShouldBe("c;dxyz");
// nothing quoted
sa = QuotingUtilities.SplitUnquoted("abc,,;dxyz", int.MaxValue, false, false, out emptySplits, ';', ',');
emptySplits.ShouldBe(2);
sa.Count.ShouldBe(2);
sa[0].ShouldBe("abc");
sa[1].ShouldBe("dxyz");
// nothing quoted
sa = QuotingUtilities.SplitUnquoted("abc,,;dxyz", int.MaxValue, true, false, out emptySplits, ';', ',');
emptySplits.ShouldBe(0);
sa.Count.ShouldBe(4);
sa[0].ShouldBe("abc");
sa[1].ShouldBe(String.Empty);
sa[2].ShouldBe(String.Empty);
sa[3].ShouldBe("dxyz");
// "c d" is quoted
sa = QuotingUtilities.SplitUnquoted("abc\"c d\"xyz");
sa.Count.ShouldBe(1);
sa[0].ShouldBe("abc\"c d\"xyz");
// "x z" is quoted (the terminal double-quote is assumed)
sa = QuotingUtilities.SplitUnquoted("abc\"x z");
sa.Count.ShouldBe(1);
sa[0].ShouldBe("abc\"x z");
// "x z" is quoted (the terminal double-quote is explicit)
sa = QuotingUtilities.SplitUnquoted("abc\"x z\"");
sa.Count.ShouldBe(1);
sa[0].ShouldBe("abc\"x z\"");
// "x z" is quoted (the terminal double-quote is assumed)
sa = QuotingUtilities.SplitUnquoted("abc\\\"cde\"x z");
sa.Count.ShouldBe(1);
sa[0].ShouldBe("abc\\\"cde\"x z");
// "x z" is quoted (the terminal double-quote is assumed)
// "c e" is not quoted
sa = QuotingUtilities.SplitUnquoted("abc\\\"c e\"x z");
sa.Count.ShouldBe(2);
sa[0].ShouldBe("abc\\\"c");
sa[1].ShouldBe("e\"x z");
// "c e" is quoted
sa = QuotingUtilities.SplitUnquoted("abc\\\\\"c e\"xyz");
sa.Count.ShouldBe(1);
sa[0].ShouldBe("abc\\\\\"c e\"xyz");
// "c e" is quoted
// "x z" is not quoted
sa = QuotingUtilities.SplitUnquoted("abc\\\\\"c e\"x z");
sa.Count.ShouldBe(2);
sa[0].ShouldBe("abc\\\\\"c e\"x");
sa[1].ShouldBe("z");
// "x z" is quoted (the terminal double-quote is assumed)
sa = QuotingUtilities.SplitUnquoted("abc\\\\\\\"cde\"x z");
sa.Count.ShouldBe(1);
sa[0].ShouldBe("abc\\\\\\\"cde\"x z");
// "xyz" is quoted (the terminal double-quote is assumed)
// "c e" is not quoted
sa = QuotingUtilities.SplitUnquoted("abc\\\\\\\"c e\"x z");
sa.Count.ShouldBe(2);
sa[0].ShouldBe("abc\\\\\\\"c");
sa[1].ShouldBe("e\"x z");
// """ is quoted
sa = QuotingUtilities.SplitUnquoted("abc\"\"\"xyz");
sa.Count.ShouldBe(1);
sa[0].ShouldBe("abc\"\"\"xyz");
// " "" is quoted
sa = QuotingUtilities.SplitUnquoted("abc\" \"\"xyz");
sa.Count.ShouldBe(1);
sa[0].ShouldBe("abc\" \"\"xyz");
// "x z" is quoted (the terminal double-quote is assumed)
sa = QuotingUtilities.SplitUnquoted("abc\"\" \"x z");
sa.Count.ShouldBe(2);
sa[0].ShouldBe("abc\"\"");
sa[1].ShouldBe("\"x z");
// " "" and "xyz" are quoted (the terminal double-quote is assumed)
sa = QuotingUtilities.SplitUnquoted("abc\" \"\"\"x z");
sa.Count.ShouldBe(1);
sa[0].ShouldBe("abc\" \"\"\"x z");
// """ is quoted
sa = QuotingUtilities.SplitUnquoted("abc\"\"\"\"\"xyz");
sa.Count.ShouldBe(1);
sa[0].ShouldBe("abc\"\"\"\"\"xyz");
// """ is quoted
// "x z" is not quoted
sa = QuotingUtilities.SplitUnquoted("abc\"\"\"\"\"x z");
sa.Count.ShouldBe(2);
sa[0].ShouldBe("abc\"\"\"\"\"x");
sa[1].ShouldBe("z");
// " "" is quoted
sa = QuotingUtilities.SplitUnquoted("abc\" \"\"\"\"xyz");
sa.Count.ShouldBe(1);
sa[0].ShouldBe("abc\" \"\"\"\"xyz");
// """ and """ are quoted
sa = QuotingUtilities.SplitUnquoted("abc\"\"\"\"\"\"xyz");
sa.Count.ShouldBe(1);
sa[0].ShouldBe("abc\"\"\"\"\"\"xyz");
// " "" and " "" are quoted
sa = QuotingUtilities.SplitUnquoted("abc\" \"\"\" \"\"xyz");
sa.Count.ShouldBe(1);
sa[0].ShouldBe("abc\" \"\"\" \"\"xyz");
// """ and """ are quoted
sa = QuotingUtilities.SplitUnquoted("abc\"\"\" \"\"\"xyz");
sa.Count.ShouldBe(2);
sa[0].ShouldBe("abc\"\"\"");
sa[1].ShouldBe("\"\"\"xyz");
// """ and """ are quoted
sa = QuotingUtilities.SplitUnquoted("abc\"\"\" \"\"\"x z");
sa.Count.ShouldBe(3);
sa[0].ShouldBe("abc\"\"\"");
sa[1].ShouldBe("\"\"\"x");
sa[2].ShouldBe("z");
// "c e"" is quoted
sa = QuotingUtilities.SplitUnquoted("abc\"c e\"\"xyz");
sa.Count.ShouldBe(1);
sa[0].ShouldBe("abc\"c e\"\"xyz");
// "c e"" is quoted
// "x z" is not quoted
sa = QuotingUtilities.SplitUnquoted("abc\"c e\"\"x z");
sa.Count.ShouldBe(2);
sa[0].ShouldBe("abc\"c e\"\"x");
sa[1].ShouldBe("z");
// nothing is quoted
sa = QuotingUtilities.SplitUnquoted("a c\"\"x z");
sa.Count.ShouldBe(3);
sa[0].ShouldBe("a");
sa[1].ShouldBe("c\"\"x");
sa[2].ShouldBe("z");
// nothing is quoted
sa = QuotingUtilities.SplitUnquoted("a c\"\"c e\"\"x z");
sa.Count.ShouldBe(4);
sa[0].ShouldBe("a");
sa[1].ShouldBe("c\"\"c");
sa[2].ShouldBe("e\"\"x");
sa[3].ShouldBe("z");
}
[Fact]
public void UnquoteTest()
{
int doubleQuotesRemoved;
// "cde" is quoted
QuotingUtilities.Unquote("abc\"cde\"xyz", out doubleQuotesRemoved).ShouldBe("abccdexyz");
doubleQuotesRemoved.ShouldBe(2);
// "xyz" is quoted (the terminal double-quote is assumed)
QuotingUtilities.Unquote("abc\"xyz", out doubleQuotesRemoved).ShouldBe("abcxyz");
doubleQuotesRemoved.ShouldBe(1);
// "xyz" is quoted (the terminal double-quote is explicit)
QuotingUtilities.Unquote("abc\"xyz\"", out doubleQuotesRemoved).ShouldBe("abcxyz");
doubleQuotesRemoved.ShouldBe(2);
// "xyz" is quoted (the terminal double-quote is assumed)
QuotingUtilities.Unquote("abc\\\"cde\"xyz", out doubleQuotesRemoved).ShouldBe("abc\"cdexyz");
doubleQuotesRemoved.ShouldBe(1);
// "cde" is quoted
QuotingUtilities.Unquote("abc\\\\\"cde\"xyz", out doubleQuotesRemoved).ShouldBe("abc\\cdexyz");
doubleQuotesRemoved.ShouldBe(2);
// "xyz" is quoted (the terminal double-quote is assumed)
QuotingUtilities.Unquote("abc\\\\\\\"cde\"xyz", out doubleQuotesRemoved).ShouldBe("abc\\\"cdexyz");
doubleQuotesRemoved.ShouldBe(1);
// """ is quoted
QuotingUtilities.Unquote("abc\"\"\"xyz", out doubleQuotesRemoved).ShouldBe("abc\"xyz");
doubleQuotesRemoved.ShouldBe(2);
// """ and "xyz" are quoted (the terminal double-quote is assumed)
QuotingUtilities.Unquote("abc\"\"\"\"xyz", out doubleQuotesRemoved).ShouldBe("abc\"xyz");
doubleQuotesRemoved.ShouldBe(3);
// """ is quoted
QuotingUtilities.Unquote("abc\"\"\"\"\"xyz", out doubleQuotesRemoved).ShouldBe("abc\"xyz");
doubleQuotesRemoved.ShouldBe(4);
// """ and """ are quoted
QuotingUtilities.Unquote("abc\"\"\"\"\"\"xyz", out doubleQuotesRemoved).ShouldBe("abc\"\"xyz");
doubleQuotesRemoved.ShouldBe(4);
// "cde"" is quoted
QuotingUtilities.Unquote("abc\"cde\"\"xyz", out doubleQuotesRemoved).ShouldBe("abccde\"xyz");
doubleQuotesRemoved.ShouldBe(2);
// "xyz"" is quoted (the terminal double-quote is explicit)
QuotingUtilities.Unquote("abc\"xyz\"\"", out doubleQuotesRemoved).ShouldBe("abcxyz\"");
doubleQuotesRemoved.ShouldBe(2);
// nothing is quoted
QuotingUtilities.Unquote("abc\"\"xyz", out doubleQuotesRemoved).ShouldBe("abcxyz");
doubleQuotesRemoved.ShouldBe(2);
// nothing is quoted
QuotingUtilities.Unquote("abc\"\"cde\"\"xyz", out doubleQuotesRemoved).ShouldBe("abccdexyz");
doubleQuotesRemoved.ShouldBe(4);
}
[Fact]
public void ExtractSwitchParametersTest()
{
string commandLineArg = "\"/p:foo=\"bar";
int doubleQuotesRemovedFromArg;
string unquotedCommandLineArg = QuotingUtilities.Unquote(commandLineArg, out doubleQuotesRemovedFromArg);
MSBuildApp.ExtractSwitchParameters(commandLineArg, unquotedCommandLineArg, doubleQuotesRemovedFromArg, "p", unquotedCommandLineArg.IndexOf(':')).ShouldBe(":\"foo=\"bar");
doubleQuotesRemovedFromArg.ShouldBe(2);
commandLineArg = "\"/p:foo=bar\"";
unquotedCommandLineArg = QuotingUtilities.Unquote(commandLineArg, out doubleQuotesRemovedFromArg);
MSBuildApp.ExtractSwitchParameters(commandLineArg, unquotedCommandLineArg, doubleQuotesRemovedFromArg, "p", unquotedCommandLineArg.IndexOf(':')).ShouldBe(":foo=bar");
doubleQuotesRemovedFromArg.ShouldBe(2);
commandLineArg = "/p:foo=bar";
unquotedCommandLineArg = QuotingUtilities.Unquote(commandLineArg, out doubleQuotesRemovedFromArg);
MSBuildApp.ExtractSwitchParameters(commandLineArg, unquotedCommandLineArg, doubleQuotesRemovedFromArg, "p", unquotedCommandLineArg.IndexOf(':')).ShouldBe(":foo=bar");
doubleQuotesRemovedFromArg.ShouldBe(0);
commandLineArg = "\"\"/p:foo=bar\"";
unquotedCommandLineArg = QuotingUtilities.Unquote(commandLineArg, out doubleQuotesRemovedFromArg);
MSBuildApp.ExtractSwitchParameters(commandLineArg, unquotedCommandLineArg, doubleQuotesRemovedFromArg, "p", unquotedCommandLineArg.IndexOf(':')).ShouldBe(":foo=bar\"");
doubleQuotesRemovedFromArg.ShouldBe(3);
// this test is totally unreal -- we'd never attempt to extract switch parameters if the leading character is not a
// switch indicator (either '-' or '/') -- here the leading character is a double-quote
commandLineArg = "\"\"\"/p:foo=bar\"";
unquotedCommandLineArg = QuotingUtilities.Unquote(commandLineArg, out doubleQuotesRemovedFromArg);
MSBuildApp.ExtractSwitchParameters(commandLineArg, unquotedCommandLineArg, doubleQuotesRemovedFromArg, "/p", unquotedCommandLineArg.IndexOf(':')).ShouldBe(":foo=bar\"");
doubleQuotesRemovedFromArg.ShouldBe(3);
commandLineArg = "\"/pr\"operty\":foo=bar";
unquotedCommandLineArg = QuotingUtilities.Unquote(commandLineArg, out doubleQuotesRemovedFromArg);
MSBuildApp.ExtractSwitchParameters(commandLineArg, unquotedCommandLineArg, doubleQuotesRemovedFromArg, "property", unquotedCommandLineArg.IndexOf(':')).ShouldBe(":foo=bar");
doubleQuotesRemovedFromArg.ShouldBe(3);
commandLineArg = "\"/pr\"op\"\"erty\":foo=bar\"";
unquotedCommandLineArg = QuotingUtilities.Unquote(commandLineArg, out doubleQuotesRemovedFromArg);
MSBuildApp.ExtractSwitchParameters(commandLineArg, unquotedCommandLineArg, doubleQuotesRemovedFromArg, "property", unquotedCommandLineArg.IndexOf(':')).ShouldBe(":foo=bar");
doubleQuotesRemovedFromArg.ShouldBe(6);
commandLineArg = "/p:\"foo foo\"=\"bar bar\";\"baz=onga\"";
unquotedCommandLineArg = QuotingUtilities.Unquote(commandLineArg, out doubleQuotesRemovedFromArg);
MSBuildApp.ExtractSwitchParameters(commandLineArg, unquotedCommandLineArg, doubleQuotesRemovedFromArg, "p", unquotedCommandLineArg.IndexOf(':')).ShouldBe(":\"foo foo\"=\"bar bar\";\"baz=onga\"");
doubleQuotesRemovedFromArg.ShouldBe(6);
}
[Fact]
public void GetLengthOfSwitchIndicatorTest()
{
var commandLineSwitchWithSlash = "/Switch";
var commandLineSwitchWithSingleDash = "-Switch";
var commandLineSwitchWithDoubleDash = "--Switch";
var commandLineSwitchWithNoneOrIncorrectIndicator = "zSwitch";
MSBuildApp.GetLengthOfSwitchIndicator(commandLineSwitchWithSlash).ShouldBe(1);
MSBuildApp.GetLengthOfSwitchIndicator(commandLineSwitchWithSingleDash).ShouldBe(1);
MSBuildApp.GetLengthOfSwitchIndicator(commandLineSwitchWithDoubleDash).ShouldBe(2);
MSBuildApp.GetLengthOfSwitchIndicator(commandLineSwitchWithNoneOrIncorrectIndicator).ShouldBe(0);
}
[Theory]
[InlineData("-?")]
[InlineData("-h")]
[InlineData("--help")]
[InlineData(@"/h")]
public void Help(string indicator)
{
MSBuildApp.Execute(
#if FEATURE_GET_COMMANDLINE
@$"c:\bin\msbuild.exe {indicator} "
#else
new [] {@"c:\bin\msbuild.exe", indicator}
#endif
).ShouldBe(MSBuildApp.ExitType.Success);
}
[Fact]
public void ErrorCommandLine()
{
#if FEATURE_GET_COMMANDLINE
MSBuildApp.Execute(@"c:\bin\msbuild.exe -junk").ShouldBe(MSBuildApp.ExitType.SwitchError);
MSBuildApp.Execute(@"msbuild.exe -t").ShouldBe(MSBuildApp.ExitType.SwitchError);
MSBuildApp.Execute(@"msbuild.exe @bogus.rsp").ShouldBe(MSBuildApp.ExitType.InitializationError);
#else
MSBuildApp.Execute(new[] { @"c:\bin\msbuild.exe", "-junk" }).ShouldBe(MSBuildApp.ExitType.SwitchError);
MSBuildApp.Execute(new[] { @"msbuild.exe", "-t" }).ShouldBe(MSBuildApp.ExitType.SwitchError);
MSBuildApp.Execute(new[] { @"msbuild.exe", "@bogus.rsp" }).ShouldBe(MSBuildApp.ExitType.InitializationError);
#endif
}
[Fact]
public void ValidVerbosities()
{
MSBuildApp.ProcessVerbositySwitch("Q").ShouldBe(LoggerVerbosity.Quiet);
MSBuildApp.ProcessVerbositySwitch("quiet").ShouldBe(LoggerVerbosity.Quiet);
MSBuildApp.ProcessVerbositySwitch("m").ShouldBe(LoggerVerbosity.Minimal);
MSBuildApp.ProcessVerbositySwitch("minimal").ShouldBe(LoggerVerbosity.Minimal);
MSBuildApp.ProcessVerbositySwitch("N").ShouldBe(LoggerVerbosity.Normal);
MSBuildApp.ProcessVerbositySwitch("normal").ShouldBe(LoggerVerbosity.Normal);
MSBuildApp.ProcessVerbositySwitch("d").ShouldBe(LoggerVerbosity.Detailed);
MSBuildApp.ProcessVerbositySwitch("detailed").ShouldBe(LoggerVerbosity.Detailed);
MSBuildApp.ProcessVerbositySwitch("diag").ShouldBe(LoggerVerbosity.Diagnostic);
MSBuildApp.ProcessVerbositySwitch("DIAGNOSTIC").ShouldBe(LoggerVerbosity.Diagnostic);
}
[Fact]
public void InvalidVerbosity()
{
Should.Throw<CommandLineSwitchException>(() =>
{
MSBuildApp.ProcessVerbositySwitch("loquacious");
}
);
}
[Fact]
public void ValidMaxCPUCountSwitch()
{
MSBuildApp.ProcessMaxCPUCountSwitch(new string[] { "1" }).ShouldBe(1);
MSBuildApp.ProcessMaxCPUCountSwitch(new string[] { "2" }).ShouldBe(2);
MSBuildApp.ProcessMaxCPUCountSwitch(new string[] { "3" }).ShouldBe(3);
MSBuildApp.ProcessMaxCPUCountSwitch(new string[] { "4" }).ShouldBe(4);
MSBuildApp.ProcessMaxCPUCountSwitch(new string[] { "8" }).ShouldBe(8);
MSBuildApp.ProcessMaxCPUCountSwitch(new string[] { "63" }).ShouldBe(63);
// Should pick last value
MSBuildApp.ProcessMaxCPUCountSwitch(new string[] { "8", "4" }).ShouldBe(4);
}
[Fact]
public void InvalidMaxCPUCountSwitch1()
{
Should.Throw<CommandLineSwitchException>(() =>
{
MSBuildApp.ProcessMaxCPUCountSwitch(new string[] { "-1" });
}
);
}
[Fact]
public void InvalidMaxCPUCountSwitch2()
{
Should.Throw<CommandLineSwitchException>(() =>
{
MSBuildApp.ProcessMaxCPUCountSwitch(new string[] { "0" });
}
);
}
[Fact]
public void InvalidMaxCPUCountSwitch3()
{
Should.Throw<CommandLineSwitchException>(() =>
{
// Too big
MSBuildApp.ProcessMaxCPUCountSwitch(new string[] { "foo" });
}
);
}
[Fact]
public void InvalidMaxCPUCountSwitch4()
{
Should.Throw<CommandLineSwitchException>(() =>
{
MSBuildApp.ProcessMaxCPUCountSwitch(new string[] { "1025" });
}
);
}
#if FEATURE_CULTUREINFO_CONSOLE_FALLBACK
/// <summary>
/// Regression test for bug where the MSBuild.exe command-line app
/// would sometimes set the UI culture to just "en" which is considered a "neutral" UI
/// culture, which didn't allow for certain kinds of string formatting/parsing.
/// </summary>
/// <remarks>
/// fr-FR, de-DE, and fr-CA are guaranteed to be available on all BVTs, so we must use one of these
/// </remarks>
[Fact]
public void SetConsoleUICulture()
{
Thread thisThread = Thread.CurrentThread;
// Save the current UI culture, so we can restore it at the end of this unit test.
CultureInfo originalUICulture = thisThread.CurrentUICulture;
thisThread.CurrentUICulture = new CultureInfo("fr-FR");
MSBuildApp.SetConsoleUI();
// Make sure this doesn't throw an exception.
string bar = String.Format(CultureInfo.CurrentUICulture, "{0}", 1);
// Restore the current UI culture back to the way it was at the beginning of this unit test.
thisThread.CurrentUICulture = originalUICulture;
}
#endif
#if FEATURE_SYSTEM_CONFIGURATION
/// <summary>
/// Invalid configuration file should not dump stack.
/// </summary>
[Fact]
public void ConfigurationInvalid()
{
string startDirectory = null;
string output = null;
string oldValueForMSBuildOldOM = null;
try
{
oldValueForMSBuildOldOM = Environment.GetEnvironmentVariable("MSBuildOldOM");
Environment.SetEnvironmentVariable("MSBuildOldOM", "");
startDirectory = CopyMSBuild();
var msbuildExeName = Path.GetFileName(RunnerUtilities.PathToCurrentlyRunningMsBuildExe);
var newPathToMSBuildExe = Path.Combine(startDirectory, msbuildExeName);
var pathToConfigFile = Path.Combine(startDirectory, msbuildExeName + ".config");
string configContent = @"<?xml version =""1.0""?>
<configuration>
<configSections>
<section name=""msbuildToolsets"" type=""Microsoft.Build.Evaluation.ToolsetConfigurationSection, Microsoft.Build, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"" />
<foo/>
</configSections>
<startup>
<supportedRuntime version=""v4.0""/>
</startup>
<foo/>
<msbuildToolsets default=""X"">
<foo/>
<toolset toolsVersion=""X"">
<foo/>
<property name=""MSBuildBinPath"" value=""Y""/>
<foo/>
</toolset>
<foo/>
</msbuildToolsets>
<foo/>
</configuration>";
File.WriteAllText(pathToConfigFile, configContent);
var pathToProjectFile = Path.Combine(startDirectory, "foo.proj");
string projectString =
"<?xml version='1.0' encoding='utf-8'?>" +
"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003' ToolsVersion='X'>" +
"<Target Name='t'></Target>" +
"</Project>";
File.WriteAllText(pathToProjectFile, projectString);
var msbuildParameters = "\"" + pathToProjectFile + "\"";
bool successfulExit;
output = RunnerUtilities.ExecMSBuild(newPathToMSBuildExe, msbuildParameters, out successfulExit);
successfulExit.ShouldBeFalse();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
throw;
}
finally
{
if (output != null)
{
Console.WriteLine(output);
}
try
{
// Process does not let go its lock on the exe file until about 1 millisecond after
// p.WaitForExit() returns. Do I know why? No I don't.
RobustDelete(startDirectory);
}
finally
{
Environment.SetEnvironmentVariable("MSBuildOldOM", oldValueForMSBuildOldOM);
}
}
// If there's a space in the %TEMP% path, the config file is read in the static constructor by the URI class and we catch there;
// if there's not, we will catch when we try to read the toolsets. Either is fine; we just want to not crash.
(output.Contains("MSB1043") || output.Contains("MSB4136")).ShouldBeTrue("Output should contain 'MSB1043' or 'MSB4136'");
}
#endif
/// <summary>
/// Try hard to delete a file or directory specified
/// </summary>
private void RobustDelete(string path)
{
if (path != null)
{
for (int i = 0; i < 10; i++)
{
try
{
if (Directory.Exists(path))
{
FileUtilities.DeleteWithoutTrailingBackslash(path, true /*and files*/);
}
else if (File.Exists(path))
{
File.SetAttributes(path, File.GetAttributes(path) & ~FileAttributes.ReadOnly); // make writable
File.Delete(path);
}
}
catch (UnauthorizedAccessException)
{
Thread.Sleep(10);
break;
}
}
}
}
/// <summary>
/// Tests that the environment gets passed on to the node during build.
/// </summary>
[Fact]
public void TestEnvironment()
{
string projectString = ObjectModelHelpers.CleanupFileContents(
@"<?xml version=""1.0"" encoding=""utf-8""?>
<Project ToolsVersion=""msbuilddefaulttoolsversion"" xmlns=""msbuildnamespace"">
<Target Name=""t""><Error Text='Error' Condition=""'$(MyEnvVariable)' == ''""/></Target>
</Project>");
string tempdir = Path.GetTempPath();
string projectFileName = Path.Combine(tempdir, "msbEnvironmenttest.proj");
string quotedProjectFileName = "\"" + projectFileName + "\"";
try
{
Environment.SetEnvironmentVariable("MyEnvVariable", "1");
using (StreamWriter sw = FileUtilities.OpenWrite(projectFileName, false))
{
sw.WriteLine(projectString);
}
//Should pass
#if FEATURE_GET_COMMANDLINE
MSBuildApp.Execute(@"c:\bin\msbuild.exe " + quotedProjectFileName).ShouldBe(MSBuildApp.ExitType.Success);
#else
MSBuildApp.Execute(new[] { @"c:\bin\msbuild.exe", quotedProjectFileName }).ShouldBe(MSBuildApp.ExitType.Success);
#endif
}
finally
{
Environment.SetEnvironmentVariable("MyEnvVariable", null);
File.Delete(projectFileName);
}
}
[Fact]
public void MSBuildEngineLogger()
{
string projectString =
"<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<Project ToolsVersion=\"4.0\" xmlns=\"http://schemas.microsoft.com/developer/msbuild/2003\">" +
"<Target Name=\"t\"><Message Text=\"[Hello]\"/></Target>" +
"</Project>";
string tempdir = Path.GetTempPath();
string projectFileName = Path.Combine(tempdir, "msbLoggertest.proj");
string logFile = Path.Combine(tempdir, "logFile");
string quotedProjectFileName = "\"" + projectFileName + "\"";
try
{
using (StreamWriter sw = FileUtilities.OpenWrite(projectFileName, false))
{
sw.WriteLine(projectString);
}
#if FEATURE_GET_COMMANDLINE
//Should pass
MSBuildApp.Execute(@$"c:\bin\msbuild.exe /logger:FileLogger,""Microsoft.Build, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"";""LogFile={logFile}"" /verbosity:detailed " + quotedProjectFileName).ShouldBe(MSBuildApp.ExitType.Success);
#else
//Should pass
MSBuildApp.Execute(
new[]
{
NativeMethodsShared.IsWindows ? @"c:\bin\msbuild.exe" : "/msbuild.exe",
@$"/logger:FileLogger,""Microsoft.Build, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"";""LogFile={logFile}""",
"/verbosity:detailed",
quotedProjectFileName
}).ShouldBe(MSBuildApp.ExitType.Success);
#endif
File.Exists(logFile).ShouldBeTrue();
var logFileContents = File.ReadAllText(logFile);
logFileContents.ShouldContain("Process = ");
logFileContents.ShouldContain("MSBuild executable path = ");
logFileContents.ShouldContain("Command line arguments = ");
logFileContents.ShouldContain("Current directory = ");
logFileContents.ShouldContain("MSBuild version = ");
logFileContents.ShouldContain("[Hello]");
}
finally
{
File.Delete(projectFileName);
File.Delete(logFile);
}
}
private string _pathToArbitraryBogusFile = NativeMethodsShared.IsWindows // OK on 64 bit as well
? Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "notepad.exe")
: "/bin/cat";
/// <summary>
/// Basic case
/// </summary>
[Fact]
public void GetCommandLine()
{
var msbuildParameters = "\"" + _pathToArbitraryBogusFile + "\"" + (NativeMethodsShared.IsWindows ? " /v:diag" : " -v:diag");
File.Exists(_pathToArbitraryBogusFile).ShouldBeTrue();
bool successfulExit;
string output = RunnerUtilities.ExecMSBuild(msbuildParameters, out successfulExit);
successfulExit.ShouldBeFalse();
output.ShouldContain(RunnerUtilities.PathToCurrentlyRunningMsBuildExe + (NativeMethodsShared.IsWindows ? " /v:diag " : " -v:diag ") + _pathToArbitraryBogusFile, Case.Insensitive);
}
/// <summary>
/// Quoted path
/// </summary>
[Fact]
public void GetCommandLineQuotedExe()
{
var msbuildParameters = "\"" + _pathToArbitraryBogusFile + "\"" + (NativeMethodsShared.IsWindows ? " /v:diag" : " -v:diag");
File.Exists(_pathToArbitraryBogusFile).ShouldBeTrue();
bool successfulExit;
string pathToMSBuildExe = RunnerUtilities.PathToCurrentlyRunningMsBuildExe;
// This @pathToMSBuildExe is used directly with Process, so don't quote it on
// Unix
if (NativeMethodsShared.IsWindows)
{
pathToMSBuildExe = "\"" + pathToMSBuildExe + "\"";
}
string output = RunnerUtilities.ExecMSBuild(pathToMSBuildExe, msbuildParameters, out successfulExit);
successfulExit.ShouldBeFalse();
output.ShouldContain(RunnerUtilities.PathToCurrentlyRunningMsBuildExe + (NativeMethodsShared.IsWindows ? " /v:diag " : " -v:diag ") + _pathToArbitraryBogusFile, Case.Insensitive);
}
/// <summary>
/// On path
/// </summary>
[Fact]
public void GetCommandLineQuotedExeOnPath()
{
string output = null;
string current = Directory.GetCurrentDirectory();
try
{
Directory.SetCurrentDirectory(Path.GetDirectoryName(RunnerUtilities.PathToCurrentlyRunningMsBuildExe));
var msbuildParameters = "\"" + _pathToArbitraryBogusFile + "\"" + (NativeMethodsShared.IsWindows ? " /v:diag" : " -v:diag");
bool successfulExit;
output = RunnerUtilities.ExecMSBuild(msbuildParameters, out successfulExit);
successfulExit.ShouldBeFalse();
}
finally
{
Directory.SetCurrentDirectory(current);
}
output.ShouldContain(RunnerUtilities.PathToCurrentlyRunningMsBuildExe + (NativeMethodsShared.IsWindows ? " /v:diag " : " -v:diag ") + _pathToArbitraryBogusFile, Case.Insensitive);
}
/// <summary>
/// Any msbuild.rsp in the directory of the specified project/solution should be read, and should
/// take priority over any other response files.
/// </summary>
[Fact]
public void ResponseFileInProjectDirectoryFoundImplicitly()
{
string directory = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N"));
string projectPath = Path.Combine(directory, "my.proj");
string rspPath = Path.Combine(directory, AutoResponseFileName);
string currentDirectory = Directory.GetCurrentDirectory();
try
{
Directory.CreateDirectory(directory);
string content = ObjectModelHelpers.CleanupFileContents("<Project ToolsVersion='msbuilddefaulttoolsversion' xmlns='msbuildnamespace'><Target Name='t'><Warning Text='[A=$(A)]'/></Target></Project>");
File.WriteAllText(projectPath, content);
string rspContent = "/p:A=1";
File.WriteAllText(rspPath, rspContent);
// Find the project in the current directory
Directory.SetCurrentDirectory(directory);
bool successfulExit;
string output = RunnerUtilities.ExecMSBuild(String.Empty, out successfulExit);
successfulExit.ShouldBeTrue();
output.ShouldContain("[A=1]");
}
finally
{
Directory.SetCurrentDirectory(currentDirectory);
File.Delete(projectPath);
File.Delete(rspPath);
FileUtilities.DeleteWithoutTrailingBackslash(directory);
}
}
/// <summary>
/// Any msbuild.rsp in the directory of the specified project/solution should be read, and should
/// take priority over any other response files.
/// </summary>
[Fact]
public void ResponseFileInProjectDirectoryExplicit()
{
string directory = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N"));
string projectPath = Path.Combine(directory, "my.proj");
string rspPath = Path.Combine(directory, AutoResponseFileName);
try
{
Directory.CreateDirectory(directory);
string content = ObjectModelHelpers.CleanupFileContents("<Project ToolsVersion='msbuilddefaulttoolsversion' xmlns='msbuildnamespace'><Target Name='t'><Warning Text='[A=$(A)]'/></Target></Project>");
File.WriteAllText(projectPath, content);
string rspContent = "/p:A=1";
File.WriteAllText(rspPath, rspContent);
var msbuildParameters = "\"" + projectPath + "\"";
bool successfulExit;
string output = RunnerUtilities.ExecMSBuild(msbuildParameters, out successfulExit);
successfulExit.ShouldBeTrue();
output.ShouldContain("[A=1]");
}
finally
{
File.Delete(projectPath);
File.Delete(rspPath);
FileUtilities.DeleteWithoutTrailingBackslash(directory);
}
}
/// <summary>
/// Any msbuild.rsp in the directory of the specified project/solution should be read, and not any random .rsp
/// </summary>
[Fact]
public void ResponseFileInProjectDirectoryRandomName()
{
string directory = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N"));
string projectPath = Path.Combine(directory, "my.proj");
string rspPath = Path.Combine(directory, "foo.rsp");
try
{
Directory.CreateDirectory(directory);
string content = ObjectModelHelpers.CleanupFileContents("<Project ToolsVersion='msbuilddefaulttoolsversion' xmlns='msbuildnamespace'><Target Name='t'><Warning Text='[A=$(A)]'/></Target></Project>");
File.WriteAllText(projectPath, content);
string rspContent = "/p:A=1";
File.WriteAllText(rspPath, rspContent);
var msbuildParameters = "\"" + projectPath + "\"";
bool successfulExit;
string output = RunnerUtilities.ExecMSBuild(msbuildParameters, out successfulExit);
successfulExit.ShouldBeTrue();
output.ShouldContain("[A=]");
}
finally
{
File.Delete(projectPath);
File.Delete(rspPath);
FileUtilities.DeleteWithoutTrailingBackslash(directory);
}
}
/// <summary>
/// Any msbuild.rsp in the directory of the specified project/solution should be read,
/// but lower precedence than the actual command line
/// </summary>
[Fact]
public void ResponseFileInProjectDirectoryCommandLineWins()
{
string directory = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N"));
string projectPath = Path.Combine(directory, "my.proj");
string rspPath = Path.Combine(directory, AutoResponseFileName);
try