forked from frerich/clcache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unittests.py
911 lines (765 loc) · 44.3 KB
/
unittests.py
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
#!/usr/bin/env python
#
# This file is part of the clcache project.
#
# The contents of this file are subject to the BSD 3-Clause License, the
# full text of which is available in the accompanying LICENSE file at the
# root directory of this project.
#
# In Python unittests are always members, not functions. Silence lint in this file.
# pylint: disable=no-self-use
#
from contextlib import contextmanager
import multiprocessing
import os
import unittest
import clcache
from clcache import (
CommandLineAnalyzer,
CompilerArtifactsRepository,
Configuration,
Manifest,
ManifestRepository,
Statistics,
)
from clcache import (
AnalysisError,
CalledForLinkError,
CalledForPreprocessingError,
InvalidArgumentError,
MultipleSourceFilesComplexError,
NoSourceFileError,
)
ASSETS_DIR = os.path.join("tests", "unittests")
@contextmanager
def cd(targetDirectory):
oldDirectory = os.getcwd()
os.chdir(os.path.expanduser(targetDirectory))
try:
yield
finally:
os.chdir(oldDirectory)
class TestHelperFunctions(unittest.TestCase):
def testBasenameWithoutExtension(self):
self.assertEqual(clcache.basenameWithoutExtension(r"README.asciidoc"), "README")
self.assertEqual(clcache.basenameWithoutExtension(r"/home/user/README.asciidoc"), "README")
self.assertEqual(clcache.basenameWithoutExtension(r"C:\Project\README.asciidoc"), "README")
self.assertEqual(clcache.basenameWithoutExtension(r"READ ME.asciidoc"), "READ ME")
self.assertEqual(clcache.basenameWithoutExtension(r"/home/user/READ ME.asciidoc"), "READ ME")
self.assertEqual(clcache.basenameWithoutExtension(r"C:\Project\READ ME.asciidoc"), "READ ME")
self.assertEqual(clcache.basenameWithoutExtension(r"README.asciidoc.tmp"), "README.asciidoc")
self.assertEqual(clcache.basenameWithoutExtension(r"/home/user/README.asciidoc.tmp"), "README.asciidoc")
self.assertEqual(clcache.basenameWithoutExtension(r"C:\Project\README.asciidoc.tmp"), "README.asciidoc")
def testNormalizeBaseDir(self):
self.assertIsNone(clcache.normalizeBaseDir(None))
self.assertIsNone(clcache.normalizeBaseDir(r""))
# Note: raw string literals cannot end in an odd number of backslashes
# https://docs.python.org/3/faq/design.html#why-can-t-raw-strings-r-strings-end-with-a-backslash
# So we consistenly use basic literals
self.assertEqual(clcache.normalizeBaseDir("c:"), "c:\\")
self.assertEqual(clcache.normalizeBaseDir("c:\\projects"), "c:\\projects\\")
self.assertEqual(clcache.normalizeBaseDir("C:\\"), "c:\\")
self.assertEqual(clcache.normalizeBaseDir("C:\\Projects\\"), "c:\\projects\\")
self.assertEqual(clcache.normalizeBaseDir("c:\\projects with space"), "c:\\projects with space\\")
self.assertEqual(clcache.normalizeBaseDir("c:\\projects with ö"), "c:\\projects with ö\\")
def testFilesBeneathSimple(self):
with cd(os.path.join(ASSETS_DIR, "files-beneath")):
files = list(clcache.filesBeneath("a"))
self.assertEqual(len(files), 2)
self.assertIn(r"a\1.txt", files)
self.assertIn(r"a\2.txt", files)
def testFilesBeneathDeep(self):
with cd(os.path.join(ASSETS_DIR, "files-beneath")):
files = list(clcache.filesBeneath("b"))
self.assertEqual(len(files), 1)
self.assertIn(r"b\c\3.txt", files)
def testFilesBeneathRecursive(self):
with cd(os.path.join(ASSETS_DIR, "files-beneath")):
files = list(clcache.filesBeneath("."))
self.assertEqual(len(files), 5)
self.assertIn(r".\a\1.txt", files)
self.assertIn(r".\a\2.txt", files)
self.assertIn(r".\b\c\3.txt", files)
self.assertIn(r".\d\4.txt", files)
self.assertIn(r".\d\e\5.txt", files)
class TestExtentCommandLineFromEnvironment(unittest.TestCase):
def testEmpty(self):
cmdLine, env = clcache.extentCommandLineFromEnvironment([], {})
self.assertEqual(cmdLine, [])
self.assertEqual(env, {})
def testSimple(self):
cmdLine, env = clcache.extentCommandLineFromEnvironment(['/nologo'], {'USER': 'ab'})
self.assertEqual(cmdLine, ['/nologo'])
self.assertEqual(env, {'USER': 'ab'})
def testPrepend(self):
cmdLine, env = clcache.extentCommandLineFromEnvironment(['/nologo'], {
'USER': 'ab',
'CL': '/MP',
})
self.assertEqual(cmdLine, ['/MP', '/nologo'])
self.assertEqual(env, {'USER': 'ab'})
def testPrependMultiple(self):
cmdLine, _ = clcache.extentCommandLineFromEnvironment(['INPUT.C'], {
'CL': r'/Zp2 /Ox /I\INCLUDE\MYINCLS \LIB\BINMODE.OBJ',
})
self.assertEqual(cmdLine, ['/Zp2', '/Ox', r'/I\INCLUDE\MYINCLS', r'\LIB\BINMODE.OBJ', 'INPUT.C'])
def testAppend(self):
cmdLine, env = clcache.extentCommandLineFromEnvironment(['/nologo'], {
'USER': 'ab',
'_CL_': 'file.c',
})
self.assertEqual(cmdLine, ['/nologo', 'file.c'])
self.assertEqual(env, {'USER': 'ab'})
def testAppendPrepend(self):
cmdLine, env = clcache.extentCommandLineFromEnvironment(['/nologo'], {
'USER': 'ab',
'CL': '/MP',
'_CL_': 'file.c',
})
self.assertEqual(cmdLine, ['/MP', '/nologo', 'file.c'])
self.assertEqual(env, {'USER': 'ab'})
class TestConfiguration(unittest.TestCase):
def testOpenClose(self):
configuration = Configuration(os.path.join(ASSETS_DIR, "configuration", "testOpenClose.json"))
with configuration:
pass
def testDefaults(self):
configuration = Configuration(os.path.join(ASSETS_DIR, "configuration", "testDefaults.json"))
with configuration as cfg:
self.assertGreaterEqual(cfg.maximumCacheSize(), 1024) # 1KiB
class TestStatistics(unittest.TestCase):
def testOpenClose(self):
stats = Statistics(os.path.join(ASSETS_DIR, "statistics", "testOpenClose.json"))
with stats:
pass
def testHitCounts(self):
stats = Statistics(os.path.join(ASSETS_DIR, "statistics", "testHitCounts.json"))
with stats as s:
self.assertEqual(s.numCallsWithInvalidArgument(), 0)
self.assertEqual(s.numCallsWithoutSourceFile(), 0)
self.assertEqual(s.numCallsWithMultipleSourceFiles(), 0)
self.assertEqual(s.numCallsWithPch(), 0)
self.assertEqual(s.numCallsForLinking(), 0)
self.assertEqual(s.numCallsForExternalDebugInfo(), 0)
self.assertEqual(s.numEvictedMisses(), 0)
self.assertEqual(s.numHeaderChangedMisses(), 0)
self.assertEqual(s.numSourceChangedMisses(), 0)
self.assertEqual(s.numCacheHits(), 0)
self.assertEqual(s.numCacheMisses(), 0)
self.assertEqual(s.numCallsForPreprocessing(), 0)
# Bump all by 1
s.registerCallWithInvalidArgument()
s.registerCallWithoutSourceFile()
s.registerCallWithMultipleSourceFiles()
s.registerCallWithPch()
s.registerCallForLinking()
s.registerCallForExternalDebugInfo()
s.registerEvictedMiss()
s.registerHeaderChangedMiss()
s.registerSourceChangedMiss()
s.registerCacheHit()
s.registerCacheMiss()
s.registerCallForPreprocessing()
self.assertEqual(s.numCallsWithInvalidArgument(), 1)
self.assertEqual(s.numCallsWithoutSourceFile(), 1)
self.assertEqual(s.numCallsWithMultipleSourceFiles(), 1)
self.assertEqual(s.numCallsWithPch(), 1)
self.assertEqual(s.numCallsForLinking(), 1)
self.assertEqual(s.numCallsForExternalDebugInfo(), 1)
self.assertEqual(s.numEvictedMisses(), 1)
self.assertEqual(s.numHeaderChangedMisses(), 1)
self.assertEqual(s.numSourceChangedMisses(), 1)
self.assertEqual(s.numCacheHits(), 1)
self.assertEqual(s.numCallsForPreprocessing(), 1)
# accumulated: headerChanged, sourceChanged, eviced, miss
self.assertEqual(s.numCacheMisses(), 4)
class TestManifestRepository(unittest.TestCase):
def _getDirectorySize(self, dirPath):
def filesize(path, filename):
return os.stat(os.path.join(path, filename)).st_size
size = 0
for path, _, filenames in clcache.WALK(dirPath):
size += sum(filesize(path, f) for f in filenames)
return size
def testPaths(self):
manifestsRootDir = os.path.join(ASSETS_DIR, "manifests")
mm = ManifestRepository(manifestsRootDir)
ms = mm.section("fdde59862785f9f0ad6e661b9b5746b7")
self.assertEqual(ms.manifestSectionDir, os.path.join(manifestsRootDir, "fd"))
self.assertEqual(ms.manifestPath("fdde59862785f9f0ad6e661b9b5746b7"),
os.path.join(manifestsRootDir, "fd", "fdde59862785f9f0ad6e661b9b5746b7.json"))
def testIncludesContentHash(self):
self.assertEqual(
ManifestRepository.getIncludesContentHashForHashes([]),
ManifestRepository.getIncludesContentHashForHashes([])
)
self.assertEqual(
ManifestRepository.getIncludesContentHashForHashes(["d88be7edbf"]),
ManifestRepository.getIncludesContentHashForHashes(["d88be7edbf"])
)
self.assertEqual(
ManifestRepository.getIncludesContentHashForHashes(["d88be7edbf", "f6c8bd5733"]),
ManifestRepository.getIncludesContentHashForHashes(["d88be7edbf", "f6c8bd5733"])
)
# Wrong number of elements
self.assertNotEqual(
ManifestRepository.getIncludesContentHashForHashes([]),
ManifestRepository.getIncludesContentHashForHashes(["d88be7edbf"])
)
# Wrong order
self.assertNotEqual(
ManifestRepository.getIncludesContentHashForHashes(["d88be7edbf", "f6c8bd5733"]),
ManifestRepository.getIncludesContentHashForHashes(["f6c8bd5733", "d88be7edbf"])
)
# Content in different elements
self.assertNotEqual(
ManifestRepository.getIncludesContentHashForHashes(["", "d88be7edbf"]),
ManifestRepository.getIncludesContentHashForHashes(["d88be7edbf", ""])
)
self.assertNotEqual(
ManifestRepository.getIncludesContentHashForHashes(["d88be", "7edbf"]),
ManifestRepository.getIncludesContentHashForHashes(["d88b", "e7edbf"])
)
def testStoreAndGetManifest(self):
manifestsRootDir = os.path.join(ASSETS_DIR, "manifests")
mm = ManifestRepository(manifestsRootDir)
manifest1 = Manifest([r'somepath\myinclude.h'], {
"fdde59862785f9f0ad6e661b9b5746b7": "a649723940dc975ebd17167d29a532f8"
})
manifest2 = Manifest([r'somepath\myinclude.h', 'moreincludes.h'], {
"474e7fc26a592d84dfa7416c10f036c6": "8771d7ebcf6c8bd57a3d6485f63e3a89"
})
ms1 = mm.section("8a33738d88be7edbacef48e262bbb5bc")
ms2 = mm.section("0623305942d216c165970948424ae7d1")
ms1.setManifest("8a33738d88be7edbacef48e262bbb5bc", manifest1)
ms2.setManifest("0623305942d216c165970948424ae7d1", manifest2)
retrieved1 = ms1.getManifest("8a33738d88be7edbacef48e262bbb5bc")
self.assertIsNotNone(retrieved1)
self.assertEqual(retrieved1.includesContentToObjectMap["fdde59862785f9f0ad6e661b9b5746b7"],
"a649723940dc975ebd17167d29a532f8")
retrieved2 = ms2.getManifest("0623305942d216c165970948424ae7d1")
self.assertIsNotNone(retrieved2)
self.assertEqual(retrieved2.includesContentToObjectMap["474e7fc26a592d84dfa7416c10f036c6"],
"8771d7ebcf6c8bd57a3d6485f63e3a89")
def testNonExistingManifest(self):
manifestsRootDir = os.path.join(ASSETS_DIR, "manifests")
mm = ManifestRepository(manifestsRootDir)
retrieved = mm.section("ffffffffffffffffffffffffffffffff").getManifest("ffffffffffffffffffffffffffffffff")
self.assertIsNone(retrieved)
def testClean(self):
manifestsRootDir = os.path.join(ASSETS_DIR, "manifests")
mm = ManifestRepository(manifestsRootDir)
# Size in (120, 240] bytes
manifest1 = Manifest([r'somepath\myinclude.h'], {
"fdde59862785f9f0ad6e661b9b5746b7": "a649723940dc975ebd17167d29a532f8"
})
# Size in (120, 240] bytes
manifest2 = Manifest([r'somepath\myinclude.h', 'moreincludes.h'], {
"474e7fc26a592d84dfa7416c10f036c6": "8771d7ebcf6c8bd57a3d6485f63e3a89"
})
mm.section("8a33738d88be7edbacef48e262bbb5bc").setManifest("8a33738d88be7edbacef48e262bbb5bc", manifest1)
mm.section("0623305942d216c165970948424ae7d1").setManifest("0623305942d216c165970948424ae7d1", manifest2)
cleaningResultSize = mm.clean(240)
# Only one of those manifests can be left
self.assertLessEqual(cleaningResultSize, 240)
self.assertLessEqual(self._getDirectorySize(manifestsRootDir), 240)
cleaningResultSize = mm.clean(240)
# The one remaining is remains alive
self.assertLessEqual(cleaningResultSize, 240)
self.assertGreaterEqual(cleaningResultSize, 120)
self.assertLessEqual(self._getDirectorySize(manifestsRootDir), 240)
self.assertGreaterEqual(self._getDirectorySize(manifestsRootDir), 120)
cleaningResultSize = mm.clean(0)
# All manifest are gone
self.assertEqual(cleaningResultSize, 0)
self.assertEqual(self._getDirectorySize(manifestsRootDir), 0)
class TestCompilerArtifactsRepository(unittest.TestCase):
def testPaths(self):
compilerArtifactsRepositoryRootDir = os.path.join(ASSETS_DIR, "compiler-artifacts-repository")
car = CompilerArtifactsRepository(compilerArtifactsRepositoryRootDir)
cas = car.section("fdde59862785f9f0ad6e661b9b5746b7")
# section path
self.assertEqual(cas.compilerArtifactsSectionDir, os.path.join(compilerArtifactsRepositoryRootDir, "fd"))
# entry path
self.assertEqual(cas.cachedObjectName("fdde59862785f9f0ad6e661b9b5746b7"), os.path.join(
compilerArtifactsRepositoryRootDir, "fd", "fdde59862785f9f0ad6e661b9b5746b7", "object"))
class TestArgumentClasses(unittest.TestCase):
def testEquality(self):
self.assertEqual(clcache.ArgumentT1('Fo'), clcache.ArgumentT1('Fo'))
self.assertEqual(clcache.ArgumentT1('W'), clcache.ArgumentT1('W'))
self.assertEqual(clcache.ArgumentT2('W'), clcache.ArgumentT2('W'))
self.assertEqual(clcache.ArgumentT3('W'), clcache.ArgumentT3('W'))
self.assertEqual(clcache.ArgumentT4('W'), clcache.ArgumentT4('W'))
self.assertNotEqual(clcache.ArgumentT1('Fo'), clcache.ArgumentT1('W'))
self.assertNotEqual(clcache.ArgumentT1('Fo'), clcache.ArgumentT1('FO'))
self.assertNotEqual(clcache.ArgumentT1('W'), clcache.ArgumentT2('W'))
self.assertNotEqual(clcache.ArgumentT2('W'), clcache.ArgumentT3('W'))
self.assertNotEqual(clcache.ArgumentT3('W'), clcache.ArgumentT4('W'))
self.assertNotEqual(clcache.ArgumentT4('W'), clcache.ArgumentT1('W'))
def testHash(self):
self.assertEqual(hash(clcache.ArgumentT1('Fo')), hash(clcache.ArgumentT1('Fo')))
self.assertEqual(hash(clcache.ArgumentT1('W')), hash(clcache.ArgumentT1('W')))
self.assertEqual(hash(clcache.ArgumentT2('W')), hash(clcache.ArgumentT2('W')))
self.assertEqual(hash(clcache.ArgumentT3('W')), hash(clcache.ArgumentT3('W')))
self.assertEqual(hash(clcache.ArgumentT4('W')), hash(clcache.ArgumentT4('W')))
self.assertNotEqual(hash(clcache.ArgumentT1('Fo')), hash(clcache.ArgumentT1('W')))
self.assertNotEqual(hash(clcache.ArgumentT1('Fo')), hash(clcache.ArgumentT1('FO')))
self.assertNotEqual(hash(clcache.ArgumentT1('W')), hash(clcache.ArgumentT2('W')))
self.assertNotEqual(hash(clcache.ArgumentT2('W')), hash(clcache.ArgumentT3('W')))
self.assertNotEqual(hash(clcache.ArgumentT3('W')), hash(clcache.ArgumentT4('W')))
self.assertNotEqual(hash(clcache.ArgumentT4('W')), hash(clcache.ArgumentT1('W')))
class TestSplitCommandsFile(unittest.TestCase):
def _genericTest(self, commandLine, expected):
self.assertEqual(clcache.splitCommandsFile(commandLine), expected)
def testEmpty(self):
self._genericTest('', [])
def testSimple(self):
self._genericTest('/nologo', ['/nologo'])
self._genericTest('/nologo /c', ['/nologo', '/c'])
self._genericTest('/nologo /c -I.', ['/nologo', '/c', '-I.'])
def testWhitespace(self):
self._genericTest('-A -B -C', ['-A', '-B', '-C'])
self._genericTest(' -A -B -C', ['-A', '-B', '-C'])
self._genericTest('-A -B -C ', ['-A', '-B', '-C'])
def testMicrosoftExamples(self):
# https://msdn.microsoft.com/en-us/library/17w5ykft.aspx
self._genericTest(r'"abc" d e', ['abc', 'd', 'e'])
self._genericTest(r'a\\b d"e f"g h', [r'a\\b', 'de fg', 'h'])
self._genericTest(r'a\\\"b c d', [r'a\"b', 'c', 'd'])
self._genericTest(r'a\\\\"b c" d e', [r'a\\b c', 'd', 'e'])
def testQuotesAroundArgument(self):
self._genericTest(r'/Fo"C:\out dir\main.obj"', [r'/FoC:\out dir\main.obj'])
self._genericTest(r'/c /Fo"C:\out dir\main.obj"', ['/c', r'/FoC:\out dir\main.obj'])
self._genericTest(r'/Fo"C:\out dir\main.obj" /nologo', [r'/FoC:\out dir\main.obj', '/nologo'])
self._genericTest(r'/c /Fo"C:\out dir\main.obj" /nologo', ['/c', r'/FoC:\out dir\main.obj', '/nologo'])
def testDoubleQuoted(self):
self._genericTest(r'"/Fo"something\main.obj""', [r'/Fosomething\main.obj'])
self._genericTest(r'/c "/Fo"something\main.obj""', ['/c', r'/Fosomething\main.obj'])
self._genericTest(r'"/Fo"something\main.obj"" /nologo', [r'/Fosomething\main.obj', '/nologo'])
self._genericTest(r'/c "/Fo"something\main.obj"" /nologo', ['/c', r'/Fosomething\main.obj', '/nologo'])
def testBackslashBeforeQuote(self):
# Pathological cases of escaping the quote incorrectly.
self._genericTest(r'/Fo"C:\out dir\"', [r'/FoC:\out dir"'])
self._genericTest(r'/c /Fo"C:\out dir\"', ['/c', r'/FoC:\out dir"'])
self._genericTest(r'/Fo"C:\out dir\" /nologo', [r'/FoC:\out dir" /nologo'])
self._genericTest(r'/c /Fo"C:\out dir\" /nologo', ['/c', r'/FoC:\out dir" /nologo'])
# Sane cases of escaping the backslash correctly.
self._genericTest(r'/Fo"C:\out dir\\"', [r'/FoC:\out dir' '\\'])
self._genericTest(r'/c /Fo"C:\out dir\\"', ['/c', r'/FoC:\out dir' '\\'])
self._genericTest(r'/Fo"C:\out dir\\" /nologo', [r'/FoC:\out dir' '\\', r'/nologo'])
self._genericTest(r'/c /Fo"C:\out dir\\" /nologo', ['/c', r'/FoC:\out dir' '\\', r'/nologo'])
def testVyachselavCase(self):
self._genericTest(
r'"-IC:\Program files\Some library" -DX=1 -DVERSION=\"1.0\" -I..\.. -I"..\..\lib" -DMYPATH=\"C:\Path\"',
[
r'-IC:\Program files\Some library',
r'-DX=1',
r'-DVERSION="1.0"',
r'-I..\..',
r'-I..\..\lib',
r'-DMYPATH="C:\Path"'
])
def testLineEndings(self):
self._genericTest('-A\n-B', ['-A', '-B'])
self._genericTest('-A\r\n-B', ['-A', '-B'])
self._genericTest('-A -B\r\n-C -D -E', ['-A', '-B', '-C', '-D', '-E'])
def testInitialBackslash(self):
self._genericTest(r'/Fo"C:\out dir\"', [r'/FoC:\out dir"'])
self._genericTest(r'\foo.cpp', [r'\foo.cpp'])
self._genericTest(r'/nologo \foo.cpp', [r'/nologo', r'\foo.cpp'])
self._genericTest(r'\foo.cpp /c', [r'\foo.cpp', r'/c'])
class TestAnalyzeCommandLine(unittest.TestCase):
def _testSourceFilesOk(self, cmdLine):
try:
CommandLineAnalyzer.analyze(cmdLine)
except AnalysisError as err:
if isinstance(err, NoSourceFileError):
self.fail("analyze() unexpectedly raised an NoSourceFileError")
else:
# We just want to know if we got a proper source file.
# Other AnalysisErrors are ignored.
pass
def _testFailure(self, cmdLine, expectedExceptionClass):
self.assertRaises(expectedExceptionClass, lambda: CommandLineAnalyzer.analyze(cmdLine))
def _testFull(self, cmdLine, expectedSourceFiles, expectedOutputFile):
sourceFiles, outputFile = CommandLineAnalyzer.analyze(cmdLine)
self.assertEqual(sourceFiles, expectedSourceFiles)
self.assertEqual(outputFile, expectedOutputFile)
def _testFo(self, foArgument, expectedObjectFilepath):
self._testFull(['/c', foArgument, 'main.cpp'],
["main.cpp"], expectedObjectFilepath)
def _testFi(self, fiArgument):
self._testPreprocessingOutfile(['/c', '/P', fiArgument, 'main.cpp'])
self._testPreprocessingOutfile(['/c', '/P', '/EP', fiArgument, 'main.cpp'])
def _testPreprocessingOutfile(self, cmdLine):
self._testFailure(cmdLine, CalledForPreprocessingError)
def _testArgInfiles(self, cmdLine, expectedArguments, expectedInputFiles):
arguments, inputFiles = CommandLineAnalyzer.parseArgumentsAndInputFiles(cmdLine)
self.assertEqual(arguments, expectedArguments)
self.assertEqual(inputFiles, expectedInputFiles)
def testEmpty(self):
self._testFailure([], NoSourceFileError)
def testSimple(self):
self._testFull(["/c", "main.cpp"], ["main.cpp"], "main.obj")
def testNoSource(self):
# No source file has priority over other errors, for consistency
# and because it's likely to be a misconfigured command line.
self._testFailure(['/c', '/nologo'], NoSourceFileError)
self._testFailure(['/c'], NoSourceFileError)
self._testFailure([], NoSourceFileError)
self._testFailure(['/Zi'], NoSourceFileError)
self._testFailure(['/E'], NoSourceFileError)
self._testFailure(['/P'], NoSourceFileError)
self._testFailure(['/EP'], NoSourceFileError)
self._testFailure(['/Yc'], NoSourceFileError)
self._testFailure(['/Yu'], NoSourceFileError)
self._testFailure(['/link'], NoSourceFileError)
def testOutputFileFromSourcefile(self):
# For object file
self._testFull(['/c', 'main.cpp'],
['main.cpp'], 'main.obj')
# For preprocessor file
self._testFailure(['/c', '/P', 'main.cpp'], CalledForPreprocessingError)
def testPreprocessIgnoresOtherArguments(self):
# All those inputs must ignore the /Fo, /Fa and /Fm argument according
# to the documentation of /E, /P and /EP
# to file (/P)
self._testPreprocessingOutfile(['/c', '/P', 'main.cpp'])
self._testPreprocessingOutfile(['/c', '/P', '/FoSome.obj', 'main.cpp'])
self._testPreprocessingOutfile(['/c', '/P', '/FaListing.asm', 'main.cpp'])
self._testPreprocessingOutfile(['/c', '/P', '/FmMapfile.map', 'main.cpp'])
# to file (/P /EP)
# Note: documentation bug in https://msdn.microsoft.com/en-us/library/becb7sys.aspx
self._testPreprocessingOutfile(['/c', '/P', '/EP', 'main.cpp'])
self._testPreprocessingOutfile(['/c', '/P', '/EP', '/FoSome.obj', 'main.cpp'])
self._testPreprocessingOutfile(['/c', '/P', '/EP', '/FaListing.asm', 'main.cpp'])
self._testPreprocessingOutfile(['/c', '/P', '/EP', '/FmMapfile.map', 'main.cpp'])
# to stdout (/E)
self._testPreprocessingOutfile(['/c', '/E', 'main.cpp'])
self._testPreprocessingOutfile(['/c', '/E', '/FoSome.obj', 'main.cpp'])
self._testPreprocessingOutfile(['/c', '/E', '/FaListing.asm', 'main.cpp'])
self._testPreprocessingOutfile(['/c', '/E', '/FmMapfile.map', 'main.cpp'])
# to stdout (/EP)
self._testPreprocessingOutfile(['/c', '/EP', 'main.cpp'])
self._testPreprocessingOutfile(['/c', '/EP', '/FoSome.obj', 'main.cpp'])
self._testPreprocessingOutfile(['/c', '/EP', '/FaListing.asm', 'main.cpp'])
self._testPreprocessingOutfile(['/c', '/EP', '/FmMapfile.map', 'main.cpp'])
def testOutputFile(self):
# Given object filename (default extension .obj)
self._testFo('/FoTheOutFile.obj', 'TheOutFile.obj')
# Given object filename (custom extension .dat)
self._testFo('/FoTheOutFile.dat', 'TheOutFile.dat')
# Given object filename (with spaces)
self._testFo('/FoThe Out File.obj', 'The Out File.obj')
# Existing directory
with cd(ASSETS_DIR):
self._testFo(r'/Fo.', r'.\main.obj')
self._testFo(r'/Fofo-build-debug', r'fo-build-debug\main.obj')
self._testFo(r'/Fofo-build-debug\\', r'fo-build-debug\main.obj')
def testOutputFileNormalizePath(self):
# Out dir does not exist, but preserve path. Compiler will complain
self._testFo(r'/FoDebug\TheOutFile.obj', r'Debug\TheOutFile.obj')
# Convert to Windows path separatores (like cl does too)
self._testFo(r'/FoDebug/TheOutFile.obj', r'Debug\TheOutFile.obj')
# Different separators work as well
self._testFo(r'/FoDe\bug/TheOutFile.obj', r'De\bug\TheOutFile.obj')
# Double slash
self._testFo(r'/FoDebug//TheOutFile.obj', r'Debug\TheOutFile.obj')
self._testFo(r'/FoDebug\\TheOutFile.obj', r'Debug\TheOutFile.obj')
def testPreprocessingFi(self):
# Given output filename
self._testFi('/FiTheOutFile.i')
self._testFi('/FiTheOutFile.dat')
self._testFi('/FiThe Out File.i')
# Existing directory
with cd(ASSETS_DIR):
self._testFi(r'/Fi.')
self._testFi(r'/Fifi-build-debug')
self._testFi(r'/Fifi-build-debug\\')
# Non-existing directory: preserve path, compiler will complain
self._testFi(r'/FiDebug\TheOutFile.i')
# Convert to single Windows path separatores (like cl does too)
self._testFi(r'/FiDebug/TheOutFile.i')
self._testFi(r'/FiDe\bug/TheOutFile.i')
self._testFi(r'/FiDebug//TheOutFile.i')
self._testFi(r'/FiDebug\\TheOutFile.i')
def testTpTcSimple(self):
# clcache can handle /Tc or /Tp as long as there is only one of them
self._testFull(['/c', '/TcMyCcProgram.c'],
['MyCcProgram.c'], 'MyCcProgram.obj')
self._testFull(['/c', '/TpMyCxxProgram.cpp'],
['MyCxxProgram.cpp'], 'MyCxxProgram.obj')
def testLink(self):
self._testFailure(["main.cpp"], CalledForLinkError)
self._testFailure(["/nologo", "main.cpp"], CalledForLinkError)
def testArgumentParameters(self):
# Type 1 (/NAMEparameter) - Arguments with required parameter
self._testFailure(["/c", "/Ob", "main.cpp"], InvalidArgumentError)
self._testFailure(["/c", "/Yl", "main.cpp"], InvalidArgumentError)
self._testFailure(["/c", "/Zm", "main.cpp"], InvalidArgumentError)
self._testSourceFilesOk(["/c", "/Ob999", "main.cpp"])
self._testSourceFilesOk(["/c", "/Yl999", "main.cpp"])
self._testSourceFilesOk(["/c", "/Zm999", "main.cpp"])
# Type 2 (/NAME[parameter]) - Optional argument parameters must not eat up source file
self._testSourceFilesOk(["/c", "/doc", "main.cpp"])
self._testSourceFilesOk(["/c", "/FA", "main.cpp"])
self._testSourceFilesOk(["/c", "/Fr", "main.cpp"])
self._testSourceFilesOk(["/c", "/FR", "main.cpp"])
self._testSourceFilesOk(["/c", "/Gs", "main.cpp"])
self._testSourceFilesOk(["/c", "/MP", "main.cpp"])
self._testSourceFilesOk(["/c", "/Wv", "main.cpp"])
self._testSourceFilesOk(["/c", "/Yc", "main.cpp"])
self._testSourceFilesOk(["/c", "/Yu", "main.cpp"])
self._testSourceFilesOk(["/c", "/Zp", "main.cpp"])
# Type 3 (/NAME[ ]parameter) - Required argument parameters with optional space eat up source file
self._testFailure(["/c", "/FI", "main.cpp"], NoSourceFileError)
self._testFailure(["/c", "/U", "main.cpp"], NoSourceFileError)
self._testFailure(["/c", "/I", "main.cpp"], NoSourceFileError)
self._testSourceFilesOk(["/c", "/FI9999", "main.cpp"])
self._testSourceFilesOk(["/c", "/U9999", "main.cpp"])
self._testSourceFilesOk(["/c", "/I9999", "main.cpp"])
# Type 4 (/NAME parameter) - Forced space
# Some documented, but non implemented
# Documented as type 1 (/NAMEparmeter) but work as type 2 (/NAME[parameter])
self._testSourceFilesOk(["/c", "/Fa", "main.cpp"])
self._testSourceFilesOk(["/c", "/Fi", "main.cpp"])
self._testSourceFilesOk(["/c", "/Fd", "main.cpp"])
self._testSourceFilesOk(["/c", "/Fe", "main.cpp"])
self._testSourceFilesOk(["/c", "/Fm", "main.cpp"])
self._testSourceFilesOk(["/c", "/Fo", "main.cpp"])
self._testSourceFilesOk(["/c", "/Fp", "main.cpp"])
# Documented as type 1 (/NAMEparmeter) but work as type 3 (/NAME[ ]parameter)
self._testFailure(["/c", "/AI", "main.cpp"], NoSourceFileError)
self._testFailure(["/c", "/D", "main.cpp"], NoSourceFileError)
self._testFailure(["/c", "/V", "main.cpp"], NoSourceFileError)
self._testFailure(["/c", "/w1", "main.cpp"], NoSourceFileError)
self._testFailure(["/c", "/w2", "main.cpp"], NoSourceFileError)
self._testFailure(["/c", "/w3", "main.cpp"], NoSourceFileError)
self._testFailure(["/c", "/w4", "main.cpp"], NoSourceFileError)
self._testFailure(["/c", "/wd", "main.cpp"], NoSourceFileError)
self._testFailure(["/c", "/we", "main.cpp"], NoSourceFileError)
self._testFailure(["/c", "/wo", "main.cpp"], NoSourceFileError)
self._testSourceFilesOk(["/c", "/AI999", "main.cpp"])
self._testSourceFilesOk(["/c", "/D999", "main.cpp"])
self._testSourceFilesOk(["/c", "/V999", "main.cpp"])
self._testSourceFilesOk(["/c", "/w1999", "main.cpp"])
self._testSourceFilesOk(["/c", "/w2999", "main.cpp"])
self._testSourceFilesOk(["/c", "/w3999", "main.cpp"])
self._testSourceFilesOk(["/c", "/w4999", "main.cpp"])
self._testSourceFilesOk(["/c", "/wd999", "main.cpp"])
self._testSourceFilesOk(["/c", "/we999", "main.cpp"])
self._testSourceFilesOk(["/c", "/wo999", "main.cpp"])
# Those work a bit differently
self._testSourceFilesOk(["/c", "/Tc", "main.cpp"])
self._testSourceFilesOk(["/c", "/Tp", "main.cpp"])
self._testFailure(["/c", "/Tc", "999", "main.cpp"], MultipleSourceFilesComplexError)
self._testFailure(["/c", "/Tp", "999", "main.cpp"], MultipleSourceFilesComplexError)
self._testFailure(["/c", "/Tc999", "main.cpp"], MultipleSourceFilesComplexError)
self._testFailure(["/c", "/Tp999", "main.cpp"], MultipleSourceFilesComplexError)
# Documented as type 4 (/NAME parameter) but work as type 3 (/NAME[ ]parameter)
self._testFailure(["/c", "/F", "main.cpp"], NoSourceFileError)
self._testFailure(["/c", "/FU", "main.cpp"], NoSourceFileError)
self._testSourceFilesOk(["/c", "/F999", "main.cpp"])
self._testSourceFilesOk(["/c", "/FU999", "main.cpp"])
def testParseArgumentsAndInputFiles(self):
self._testArgInfiles(['/c', 'main.cpp'],
{'c': ['']},
['main.cpp'])
self._testArgInfiles(['/link', 'unit1.obj', 'unit2.obj'],
{'link': ['']},
['unit1.obj', 'unit2.obj'])
self._testArgInfiles(['/Fooutfile.obj', 'main.cpp'],
{'Fo': ['outfile.obj']},
['main.cpp'])
self._testArgInfiles(['/Fo', '/Fooutfile.obj', 'main.cpp'],
{'Fo': ['', 'outfile.obj']},
['main.cpp'])
self._testArgInfiles(['/c', '/I', 'somedir', 'main.cpp'],
{'c': [''], 'I': ['somedir']},
['main.cpp'])
self._testArgInfiles(['/c', '/I.', '/I', 'somedir', 'main.cpp'],
{'c': [''], 'I': ['.', 'somedir']},
['main.cpp'])
# Type 1 (/NAMEparameter) - Arguments with required parameter
# get parameter=99
self._testArgInfiles(["/c", "/Ob99", "main.cpp"], {'c': [''], 'Ob': ['99']}, ['main.cpp'])
self._testArgInfiles(["/c", "/Yl99", "main.cpp"], {'c': [''], 'Yl': ['99']}, ['main.cpp'])
self._testArgInfiles(["/c", "/Zm99", "main.cpp"], {'c': [''], 'Zm': ['99']}, ['main.cpp'])
# # Type 2 (/NAME[parameter]) - Optional argument parameters
# get parameter=99
self._testArgInfiles(["/c", "/doc99", "main.cpp"], {'c': [''], 'doc': ['99']}, ['main.cpp'])
self._testArgInfiles(["/c", "/FA99", "main.cpp"], {'c': [''], 'FA': ['99']}, ['main.cpp'])
self._testArgInfiles(["/c", "/Fr99", "main.cpp"], {'c': [''], 'Fr': ['99']}, ['main.cpp'])
self._testArgInfiles(["/c", "/FR99", "main.cpp"], {'c': [''], 'FR': ['99']}, ['main.cpp'])
self._testArgInfiles(["/c", "/Gs99", "main.cpp"], {'c': [''], 'Gs': ['99']}, ['main.cpp'])
self._testArgInfiles(["/c", "/MP99", "main.cpp"], {'c': [''], 'MP': ['99']}, ['main.cpp'])
self._testArgInfiles(["/c", "/Wv99", "main.cpp"], {'c': [''], 'Wv': ['99']}, ['main.cpp'])
self._testArgInfiles(["/c", "/Yc99", "main.cpp"], {'c': [''], 'Yc': ['99']}, ['main.cpp'])
self._testArgInfiles(["/c", "/Yu99", "main.cpp"], {'c': [''], 'Yu': ['99']}, ['main.cpp'])
self._testArgInfiles(["/c", "/Zp99", "main.cpp"], {'c': [''], 'Zp': ['99']}, ['main.cpp'])
self._testArgInfiles(["/c", "/Fa99", "main.cpp"], {'c': [''], 'Fa': ['99']}, ['main.cpp'])
self._testArgInfiles(["/c", "/Fd99", "main.cpp"], {'c': [''], 'Fd': ['99']}, ['main.cpp'])
self._testArgInfiles(["/c", "/Fe99", "main.cpp"], {'c': [''], 'Fe': ['99']}, ['main.cpp'])
self._testArgInfiles(["/c", "/Fi99", "main.cpp"], {'c': [''], 'Fi': ['99']}, ['main.cpp'])
self._testArgInfiles(["/c", "/Fm99", "main.cpp"], {'c': [''], 'Fm': ['99']}, ['main.cpp'])
self._testArgInfiles(["/c", "/Fo99", "main.cpp"], {'c': [''], 'Fo': ['99']}, ['main.cpp'])
self._testArgInfiles(["/c", "/Fp99", "main.cpp"], {'c': [''], 'Fp': ['99']}, ['main.cpp'])
# get no parameter
self._testArgInfiles(["/c", "/doc", "main.cpp"], {'c': [''], 'doc': ['']}, ['main.cpp'])
self._testArgInfiles(["/c", "/FA", "main.cpp"], {'c': [''], 'FA': ['']}, ['main.cpp'])
self._testArgInfiles(["/c", "/Fr", "main.cpp"], {'c': [''], 'Fr': ['']}, ['main.cpp'])
self._testArgInfiles(["/c", "/FR", "main.cpp"], {'c': [''], 'FR': ['']}, ['main.cpp'])
self._testArgInfiles(["/c", "/Gs", "main.cpp"], {'c': [''], 'Gs': ['']}, ['main.cpp'])
self._testArgInfiles(["/c", "/MP", "main.cpp"], {'c': [''], 'MP': ['']}, ['main.cpp'])
self._testArgInfiles(["/c", "/Wv", "main.cpp"], {'c': [''], 'Wv': ['']}, ['main.cpp'])
self._testArgInfiles(["/c", "/Yc", "main.cpp"], {'c': [''], 'Yc': ['']}, ['main.cpp'])
self._testArgInfiles(["/c", "/Yu", "main.cpp"], {'c': [''], 'Yu': ['']}, ['main.cpp'])
self._testArgInfiles(["/c", "/Zp", "main.cpp"], {'c': [''], 'Zp': ['']}, ['main.cpp'])
self._testArgInfiles(["/c", "/Fa", "main.cpp"], {'c': [''], 'Fa': ['']}, ['main.cpp'])
self._testArgInfiles(["/c", "/Fd", "main.cpp"], {'c': [''], 'Fd': ['']}, ['main.cpp'])
self._testArgInfiles(["/c", "/Fe", "main.cpp"], {'c': [''], 'Fe': ['']}, ['main.cpp'])
self._testArgInfiles(["/c", "/Fi", "main.cpp"], {'c': [''], 'Fi': ['']}, ['main.cpp'])
self._testArgInfiles(["/c", "/Fm", "main.cpp"], {'c': [''], 'Fm': ['']}, ['main.cpp'])
self._testArgInfiles(["/c", "/Fo", "main.cpp"], {'c': [''], 'Fo': ['']}, ['main.cpp'])
self._testArgInfiles(["/c", "/Fp", "main.cpp"], {'c': [''], 'Fp': ['']}, ['main.cpp'])
# Type 3 (/NAME[ ]parameter) - Required argument parameters with optional space
# get space
self._testArgInfiles(["/c", "/FI", "99", "main.cpp"], {'c': [''], 'FI': ['99']}, ['main.cpp'])
self._testArgInfiles(["/c", "/U", "99", "main.cpp"], {'c': [''], 'U': ['99']}, ['main.cpp'])
self._testArgInfiles(["/c", "/I", "99", "main.cpp"], {'c': [''], 'I': ['99']}, ['main.cpp'])
self._testArgInfiles(["/c", "/F", "99", "main.cpp"], {'c': [''], 'F': ['99']}, ['main.cpp'])
self._testArgInfiles(["/c", "/FU", "99", "main.cpp"], {'c': [''], 'FU': ['99']}, ['main.cpp'])
self._testArgInfiles(["/c", "/w1", "99", "main.cpp"], {'c': [''], 'w1': ['99']}, ['main.cpp'])
self._testArgInfiles(["/c", "/w2", "99", "main.cpp"], {'c': [''], 'w2': ['99']}, ['main.cpp'])
self._testArgInfiles(["/c", "/w3", "99", "main.cpp"], {'c': [''], 'w3': ['99']}, ['main.cpp'])
self._testArgInfiles(["/c", "/w4", "99", "main.cpp"], {'c': [''], 'w4': ['99']}, ['main.cpp'])
self._testArgInfiles(["/c", "/wd", "99", "main.cpp"], {'c': [''], 'wd': ['99']}, ['main.cpp'])
self._testArgInfiles(["/c", "/we", "99", "main.cpp"], {'c': [''], 'we': ['99']}, ['main.cpp'])
self._testArgInfiles(["/c", "/wo", "99", "main.cpp"], {'c': [''], 'wo': ['99']}, ['main.cpp'])
self._testArgInfiles(["/c", "/AI", "99", "main.cpp"], {'c': [''], 'AI': ['99']}, ['main.cpp'])
self._testArgInfiles(["/c", "/D", "99", "main.cpp"], {'c': [''], 'D': ['99']}, ['main.cpp'])
self._testArgInfiles(["/c", "/V", "99", "main.cpp"], {'c': [''], 'V': ['99']}, ['main.cpp'])
self._testArgInfiles(["/c", "/Tc", "99", "main.cpp"], {'c': [''], 'Tc': ['99']}, ['main.cpp'])
self._testArgInfiles(["/c", "/Tp", "99", "main.cpp"], {'c': [''], 'Tp': ['99']}, ['main.cpp'])
# don't get space
self._testArgInfiles(["/c", "/FI99", "main.cpp"], {'c': [''], 'FI': ['99']}, ['main.cpp'])
self._testArgInfiles(["/c", "/U99", "main.cpp"], {'c': [''], 'U': ['99']}, ['main.cpp'])
self._testArgInfiles(["/c", "/I99", "main.cpp"], {'c': [''], 'I': ['99']}, ['main.cpp'])
self._testArgInfiles(["/c", "/F99", "main.cpp"], {'c': [''], 'F': ['99']}, ['main.cpp'])
self._testArgInfiles(["/c", "/FU99", "main.cpp"], {'c': [''], 'FU': ['99']}, ['main.cpp'])
self._testArgInfiles(["/c", "/w199", "main.cpp"], {'c': [''], 'w1': ['99']}, ['main.cpp'])
self._testArgInfiles(["/c", "/w299", "main.cpp"], {'c': [''], 'w2': ['99']}, ['main.cpp'])
self._testArgInfiles(["/c", "/w399", "main.cpp"], {'c': [''], 'w3': ['99']}, ['main.cpp'])
self._testArgInfiles(["/c", "/w499", "main.cpp"], {'c': [''], 'w4': ['99']}, ['main.cpp'])
self._testArgInfiles(["/c", "/wd99", "main.cpp"], {'c': [''], 'wd': ['99']}, ['main.cpp'])
self._testArgInfiles(["/c", "/we99", "main.cpp"], {'c': [''], 'we': ['99']}, ['main.cpp'])
self._testArgInfiles(["/c", "/wo99", "main.cpp"], {'c': [''], 'wo': ['99']}, ['main.cpp'])
self._testArgInfiles(["/c", "/AI99", "main.cpp"], {'c': [''], 'AI': ['99']}, ['main.cpp'])
self._testArgInfiles(["/c", "/D99", "main.cpp"], {'c': [''], 'D': ['99']}, ['main.cpp'])
self._testArgInfiles(["/c", "/V99", "main.cpp"], {'c': [''], 'V': ['99']}, ['main.cpp'])
self._testArgInfiles(["/c", "/Tc99", "main.cpp"], {'c': [''], 'Tc': ['99']}, ['main.cpp'])
self._testArgInfiles(["/c", "/Tp99", "main.cpp"], {'c': [''], 'Tp': ['99']}, ['main.cpp'])
# Type 4 (/NAME parameter) - Forced space
# Some documented, but non implemented
class TestMultipleSourceFiles(unittest.TestCase):
CPU_CORES = multiprocessing.cpu_count()
def testCpuCuresPlausibility(self):
# 1 <= CPU_CORES <= 32
self.assertGreaterEqual(self.CPU_CORES, 1)
self.assertLessEqual(self.CPU_CORES, 32)
def testJobCount(self):
# Basic parsing
actual = clcache.jobCount(["/MP1"])
self.assertEqual(actual, 1)
actual = clcache.jobCount(["/MP100"])
self.assertEqual(actual, 100)
# Without optional max process value
actual = clcache.jobCount(["/MP"])
self.assertEqual(actual, self.CPU_CORES)
# Invalid inputs
actual = clcache.jobCount(["/MP100.0"])
self.assertEqual(actual, 1)
actual = clcache.jobCount(["/MP-100"])
self.assertEqual(actual, 1)
actual = clcache.jobCount(["/MPfoo"])
self.assertEqual(actual, 1)
# Multiple values
actual = clcache.jobCount(["/MP1", "/MP44"])
self.assertEqual(actual, 44)
actual = clcache.jobCount(["/MP1", "/MP44", "/MP"])
self.assertEqual(actual, self.CPU_CORES)
# Find /MP in mixed command line
actual = clcache.jobCount(["/c", "/nologo", "/MP44"])
self.assertEqual(actual, 44)
actual = clcache.jobCount(["/c", "/nologo", "/MP44", "mysource.cpp"])
self.assertEqual(actual, 44)
actual = clcache.jobCount(["/MP2", "/c", "/nologo", "/MP44", "mysource.cpp"])
self.assertEqual(actual, 44)
actual = clcache.jobCount(["/MP2", "/c", "/MP44", "/nologo", "/MP", "mysource.cpp"])
self.assertEqual(actual, self.CPU_CORES)
class TestParseIncludes(unittest.TestCase):
def _readSampleFileDefault(self, lang=None):
if lang == "de":
filePath = os.path.join(ASSETS_DIR, 'parse-includes', 'compiler_output_lang_de.txt')
uniqueIncludesCount = 82
else:
filePath = os.path.join(ASSETS_DIR, 'parse-includes', 'compiler_output.txt')
uniqueIncludesCount = 83
with open(filePath, 'r') as infile:
return {
'CompilerOutput': infile.read(),
'UniqueIncludesCount': uniqueIncludesCount
}
def _readSampleFileNoIncludes(self):
with open(os.path.join(ASSETS_DIR, 'parse-includes', 'compiler_output_no_includes.txt'), 'r') as infile:
return {
'CompilerOutput': infile.read(),
'UniqueIncludesCount': 0
}
def testParseIncludesNoStrip(self):
sample = self._readSampleFileDefault()
includesSet, newCompilerOutput = clcache.parseIncludesSet(
sample['CompilerOutput'],
r'C:\Projects\test\smartsqlite\src\version.cpp',
strip=False)
self.assertEqual(len(includesSet), sample['UniqueIncludesCount'])
self.assertTrue(r'c:\projects\test\smartsqlite\include\smartsqlite\version.h' in includesSet)
self.assertTrue(
r'c:\program files (x86)\microsoft visual studio 12.0\vc\include\concurrencysal.h' in includesSet)
self.assertTrue(r'' not in includesSet)
self.assertEqual(newCompilerOutput, sample['CompilerOutput'])
def testParseIncludesStrip(self):
sample = self._readSampleFileDefault()
includesSet, newCompilerOutput = clcache.parseIncludesSet(
sample['CompilerOutput'],
r'C:\Projects\test\smartsqlite\src\version.cpp',
strip=True)
self.assertEqual(len(includesSet), sample['UniqueIncludesCount'])
self.assertTrue(r'c:\projects\test\smartsqlite\include\smartsqlite\version.h' in includesSet)
self.assertTrue(
r'c:\program files (x86)\microsoft visual studio 12.0\vc\include\concurrencysal.h' in includesSet)
self.assertTrue(r'' not in includesSet)
self.assertEqual(newCompilerOutput, "version.cpp\n")
def testParseIncludesNoIncludes(self):
sample = self._readSampleFileNoIncludes()
for stripIncludes in [True, False]:
includesSet, newCompilerOutput = clcache.parseIncludesSet(
sample['CompilerOutput'],
r"C:\Projects\test\myproject\main.cpp",
strip=stripIncludes)
self.assertEqual(len(includesSet), sample['UniqueIncludesCount'])
self.assertEqual(newCompilerOutput, "main.cpp\n")
def testParseIncludesGerman(self):
sample = self._readSampleFileDefault(lang="de")
includesSet, _ = clcache.parseIncludesSet(
sample['CompilerOutput'],
r"C:\Projects\test\smartsqlite\src\version.cpp",
strip=False)
self.assertEqual(len(includesSet), sample['UniqueIncludesCount'])
self.assertTrue(r'c:\projects\test\smartsqlite\include\smartsqlite\version.h' in includesSet)
self.assertTrue(
r'c:\program files (x86)\microsoft visual studio 12.0\vc\include\concurrencysal.h' in includesSet)
self.assertTrue(r'' not in includesSet)
if __name__ == '__main__':
unittest.TestCase.longMessage = True
unittest.main()