-
Notifications
You must be signed in to change notification settings - Fork 0
/
pre-upload_unittest.py
executable file
·1147 lines (923 loc) · 43.4 KB
/
pre-upload_unittest.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
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
#!/usr/bin/python2
# -*- coding: utf-8 -*-
# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Unittests for pre-upload.py."""
from __future__ import print_function
import os
import sys
import errors
# pylint: disable=W0212
# We access private members of the pre_upload module all over the place.
# Make sure we can find the chromite paths.
sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(__file__)),
'..', '..'))
from chromite.cbuildbot import constants
from chromite.lib import cros_test_lib
from chromite.lib import git
from chromite.lib import osutils
# Needs to be after chromite imports so we use the bundled copy.
import mock
pre_upload = __import__('pre-upload')
def ProjectNamed(project_name):
"""Wrapper to create a Project with just the name"""
return pre_upload.Project(project_name, None, None)
class TryUTF8DecodeTest(cros_test_lib.TestCase):
"""Verify we sanely handle unicode content."""
def runTest(self):
self.assertEquals(u'', pre_upload._try_utf8_decode(''))
self.assertEquals(u'abc', pre_upload._try_utf8_decode('abc'))
self.assertEquals(
u'ä½ å¥½å¸èæ©',
pre_upload._try_utf8_decode('ä½ å¥½å¸èæ©'))
# Invalid UTF-8
self.assertEquals('\x80', pre_upload._try_utf8_decode('\x80'))
class CheckNoLongLinesTest(cros_test_lib.MockTestCase):
"""Tests for _check_no_long_lines."""
def setUp(self):
self.PatchObject(pre_upload, '_get_affected_files', return_value=['x.py'])
self.PatchObject(pre_upload, '_filter_files', return_value=['x.py'])
self.diff_mock = self.PatchObject(pre_upload, '_get_file_diff')
def runTest(self):
self.diff_mock.return_value = [
(1, u"x" * 80), # OK
(2, "\x80" * 80), # OK
(3, u"x" * 81), # Too long
(4, "\x80" * 81), # Too long
(5, u"See http://" + (u"x" * 80)), # OK (URL)
(6, u"See https://" + (u"x" * 80)), # OK (URL)
(7, u"# define " + (u"x" * 80)), # OK (compiler directive)
(8, u"#define" + (u"x" * 74)), # Too long
]
failure = pre_upload._check_no_long_lines(ProjectNamed('PROJECT'), 'COMMIT')
self.assertTrue(failure)
self.assertEquals('Found lines longer than 80 characters (first 5 shown):',
failure.msg)
self.assertEquals(['x.py, line %d, 81 chars' % line
for line in [3, 4, 8]],
failure.items)
class CheckProjectPrefix(cros_test_lib.MockTempDirTestCase):
"""Tests for _check_project_prefix."""
def setUp(self):
self.orig_cwd = os.getcwd()
os.chdir(self.tempdir)
self.file_mock = self.PatchObject(pre_upload, '_get_affected_files')
self.desc_mock = self.PatchObject(pre_upload, '_get_commit_desc')
def tearDown(self):
os.chdir(self.orig_cwd)
def _WriteAliasFile(self, filename, project):
"""Writes a project name to a file, creating directories if needed."""
os.makedirs(os.path.dirname(filename))
osutils.WriteFile(filename, project)
def testInvalidPrefix(self):
"""Report an error when the prefix doesn't match the base directory."""
self.file_mock.return_value = ['foo/foo.cc', 'foo/subdir/baz.cc']
self.desc_mock.return_value = 'bar: Some commit'
failure = pre_upload._check_project_prefix(ProjectNamed('PROJECT'),
'COMMIT')
self.assertTrue(failure)
self.assertEquals(('The commit title for changes affecting only foo' +
' should start with "foo: "'), failure.msg)
def testValidPrefix(self):
"""Use a prefix that matches the base directory."""
self.file_mock.return_value = ['foo/foo.cc', 'foo/subdir/baz.cc']
self.desc_mock.return_value = 'foo: Change some files.'
self.assertFalse(
pre_upload._check_project_prefix(ProjectNamed('PROJECT'), 'COMMIT'))
def testAliasFile(self):
"""Use .project_alias to override the project name."""
self._WriteAliasFile('foo/.project_alias', 'project')
self.file_mock.return_value = ['foo/foo.cc', 'foo/subdir/bar.cc']
self.desc_mock.return_value = 'project: Use an alias.'
self.assertFalse(
pre_upload._check_project_prefix(ProjectNamed('PROJECT'), 'COMMIT'))
def testAliasFileWithSubdirs(self):
"""Check that .project_alias is used when only modifying subdirectories."""
self._WriteAliasFile('foo/.project_alias', 'project')
self.file_mock.return_value = [
'foo/subdir/foo.cc',
'foo/subdir/bar.cc'
'foo/subdir/blah/baz.cc'
]
self.desc_mock.return_value = 'project: Alias with subdirs.'
self.assertFalse(
pre_upload._check_project_prefix(ProjectNamed('PROJECT'), 'COMMIT'))
class CheckKernelConfig(cros_test_lib.MockTestCase):
"""Tests for _kernel_configcheck."""
def setUp(self):
self.file_mock = self.PatchObject(pre_upload, '_get_affected_files')
def testMixedChanges(self):
"""Mixing of changes should fail."""
self.file_mock.return_value = [
'/kernel/files/chromeos/config/base.config',
'/kernel/files/arch/arm/mach-exynos/mach-exynos5-dt.c'
]
failure = pre_upload._kernel_configcheck('PROJECT', 'COMMIT')
self.assertTrue(failure)
def testCodeOnly(self):
"""Code-only changes should pass."""
self.file_mock.return_value = [
'/kernel/files/Makefile',
'/kernel/files/arch/arm/mach-exynos/mach-exynos5-dt.c'
]
failure = pre_upload._kernel_configcheck('PROJECT', 'COMMIT')
self.assertFalse(failure)
def testConfigOnlyChanges(self):
"""Config-only changes should pass."""
self.file_mock.return_value = [
'/kernel/files/chromeos/config/base.config',
]
failure = pre_upload._kernel_configcheck('PROJECT', 'COMMIT')
self.assertFalse(failure)
class CheckPortageMakeUseVar(cros_test_lib.MockTestCase):
"""Tests for _check_portage_make_use_var."""
def setUp(self):
self.file_mock = self.PatchObject(pre_upload, '_get_affected_files')
self.content_mock = self.PatchObject(pre_upload, '_get_file_content')
def testMakeConfOmitsOriginalUseValue(self):
"""Fail for make.conf that discards the previous value of $USE."""
self.file_mock.return_value = ['make.conf']
self.content_mock.return_value = 'USE="foo"\nUSE="${USE} bar"'
failure = pre_upload._check_portage_make_use_var('PROJECT', 'COMMIT')
self.assertTrue(failure, failure)
def testMakeConfCorrectUsage(self):
"""Succeed for make.conf that preserves the previous value of $USE."""
self.file_mock.return_value = ['make.conf']
self.content_mock.return_value = 'USE="${USE} foo"\nUSE="${USE}" bar'
failure = pre_upload._check_portage_make_use_var('PROJECT', 'COMMIT')
self.assertFalse(failure, failure)
def testMakeDefaultsReferencesOriginalUseValue(self):
"""Fail for make.defaults that refers to a not-yet-set $USE value."""
self.file_mock.return_value = ['make.defaults']
self.content_mock.return_value = 'USE="${USE} foo"'
failure = pre_upload._check_portage_make_use_var('PROJECT', 'COMMIT')
self.assertTrue(failure, failure)
# Also check for "$USE" without curly brackets.
self.content_mock.return_value = 'USE="$USE foo"'
failure = pre_upload._check_portage_make_use_var('PROJECT', 'COMMIT')
self.assertTrue(failure, failure)
def testMakeDefaultsOverwritesUseValue(self):
"""Fail for make.defaults that discards its own $USE value."""
self.file_mock.return_value = ['make.defaults']
self.content_mock.return_value = 'USE="foo"\nUSE="bar"'
failure = pre_upload._check_portage_make_use_var('PROJECT', 'COMMIT')
self.assertTrue(failure, failure)
def testMakeDefaultsCorrectUsage(self):
"""Succeed for make.defaults that sets and preserves $USE."""
self.file_mock.return_value = ['make.defaults']
self.content_mock.return_value = 'USE="foo"\nUSE="${USE}" bar'
failure = pre_upload._check_portage_make_use_var('PROJECT', 'COMMIT')
self.assertFalse(failure, failure)
class CheckEbuildEapi(cros_test_lib.MockTestCase):
"""Tests for _check_ebuild_eapi."""
PORTAGE_STABLE = ProjectNamed('chromiumos/overlays/portage-stable')
def setUp(self):
self.file_mock = self.PatchObject(pre_upload, '_get_affected_files')
self.content_mock = self.PatchObject(pre_upload, '_get_file_content')
self.diff_mock = self.PatchObject(pre_upload, '_get_file_diff',
side_effect=Exception())
def testSkipUpstreamOverlays(self):
"""Skip ebuilds found in upstream overlays."""
self.file_mock.side_effect = Exception()
ret = pre_upload._check_ebuild_eapi(self.PORTAGE_STABLE, 'HEAD')
self.assertEqual(ret, None)
# Make sure our condition above triggers.
self.assertRaises(Exception, pre_upload._check_ebuild_eapi, 'o', 'HEAD')
def testSkipNonEbuilds(self):
"""Skip non-ebuild files."""
self.content_mock.side_effect = Exception()
self.file_mock.return_value = ['some-file', 'ebuild/dir', 'an.ebuild~']
ret = pre_upload._check_ebuild_eapi(ProjectNamed('overlay'), 'HEAD')
self.assertEqual(ret, None)
# Make sure our condition above triggers.
self.file_mock.return_value.append('a/real.ebuild')
self.assertRaises(Exception, pre_upload._check_ebuild_eapi,
ProjectNamed('o'), 'HEAD')
def testSkipSymlink(self):
"""Skip files that are just symlinks."""
self.file_mock.return_value = ['a-r1.ebuild']
self.content_mock.return_value = 'a.ebuild'
ret = pre_upload._check_ebuild_eapi(ProjectNamed('overlay'), 'HEAD')
self.assertEqual(ret, None)
def testRejectEapiImplicit0Content(self):
"""Reject ebuilds that do not declare EAPI (so it's 0)."""
self.file_mock.return_value = ['a.ebuild']
self.content_mock.return_value = """# Header
IUSE="foo"
src_compile() { }
"""
ret = pre_upload._check_ebuild_eapi(ProjectNamed('overlay'), 'HEAD')
self.assertTrue(isinstance(ret, errors.HookFailure))
def testRejectExplicitEapi1Content(self):
"""Reject ebuilds that do declare old EAPI explicitly."""
self.file_mock.return_value = ['a.ebuild']
template = """# Header
EAPI=%s
IUSE="foo"
src_compile() { }
"""
# Make sure we only check the first EAPI= setting.
self.content_mock.return_value = template % '1\nEAPI=4'
ret = pre_upload._check_ebuild_eapi(ProjectNamed('overlay'), 'HEAD')
self.assertTrue(isinstance(ret, errors.HookFailure))
# Verify we handle double quotes too.
self.content_mock.return_value = template % '"1"'
ret = pre_upload._check_ebuild_eapi(ProjectNamed('overlay'), 'HEAD')
self.assertTrue(isinstance(ret, errors.HookFailure))
# Verify we handle single quotes too.
self.content_mock.return_value = template % "'1'"
ret = pre_upload._check_ebuild_eapi(ProjectNamed('overlay'), 'HEAD')
self.assertTrue(isinstance(ret, errors.HookFailure))
def testAcceptExplicitEapi4Content(self):
"""Accept ebuilds that do declare new EAPI explicitly."""
self.file_mock.return_value = ['a.ebuild']
template = """# Header
EAPI=%s
IUSE="foo"
src_compile() { }
"""
# Make sure we only check the first EAPI= setting.
self.content_mock.return_value = template % '4\nEAPI=1'
ret = pre_upload._check_ebuild_eapi(ProjectNamed('overlay'), 'HEAD')
self.assertEqual(ret, None)
# Verify we handle double quotes too.
self.content_mock.return_value = template % '"5"'
ret = pre_upload._check_ebuild_eapi(ProjectNamed('overlay'), 'HEAD')
self.assertEqual(ret, None)
# Verify we handle single quotes too.
self.content_mock.return_value = template % "'5-hdepend'"
ret = pre_upload._check_ebuild_eapi(ProjectNamed('overlay'), 'HEAD')
self.assertEqual(ret, None)
class CheckEbuildKeywords(cros_test_lib.MockTestCase):
"""Tests for _check_ebuild_keywords."""
def setUp(self):
self.file_mock = self.PatchObject(pre_upload, '_get_affected_files')
self.content_mock = self.PatchObject(pre_upload, '_get_file_content')
def testNoEbuilds(self):
"""If no ebuilds are found, do not scan."""
self.file_mock.return_value = ['a.file', 'ebuild-is-not.foo']
ret = pre_upload._check_ebuild_keywords(ProjectNamed('overlay'), 'HEAD')
self.assertEqual(ret, None)
self.assertEqual(self.content_mock.call_count, 0)
def testSomeEbuilds(self):
"""If ebuilds are found, only scan them."""
self.file_mock.return_value = ['a.file', 'blah', 'foo.ebuild', 'cow']
self.content_mock.return_value = ''
ret = pre_upload._check_ebuild_keywords(ProjectNamed('overlay'), 'HEAD')
self.assertEqual(ret, None)
self.assertEqual(self.content_mock.call_count, 1)
def _CheckContent(self, content, fails):
"""Test helper for inputs/outputs.
Args:
content: The ebuild content to test.
fails: Whether |content| should trigger a hook failure.
"""
self.file_mock.return_value = ['a.ebuild']
self.content_mock.return_value = content
ret = pre_upload._check_ebuild_keywords(ProjectNamed('overlay'), 'HEAD')
if fails:
self.assertTrue(isinstance(ret, errors.HookFailure))
else:
self.assertEqual(ret, None)
self.assertEqual(self.content_mock.call_count, 1)
def testEmpty(self):
"""Check KEYWORDS= is accepted."""
self._CheckContent('# HEADER\nKEYWORDS=\nblah\n', False)
def testEmptyQuotes(self):
"""Check KEYWORDS="" is accepted."""
self._CheckContent('# HEADER\nKEYWORDS=" "\nblah\n', False)
def testStableGlob(self):
"""Check KEYWORDS=* is accepted."""
self._CheckContent('# HEADER\nKEYWORDS="\t*\t"\nblah\n', False)
def testUnstableGlob(self):
"""Check KEYWORDS=~* is accepted."""
self._CheckContent('# HEADER\nKEYWORDS="~* "\nblah\n', False)
def testRestrictedGlob(self):
"""Check KEYWORDS=-* is accepted."""
self._CheckContent('# HEADER\nKEYWORDS="\t-* arm"\nblah\n', False)
def testMissingGlobs(self):
"""Reject KEYWORDS missing any globs."""
self._CheckContent('# HEADER\nKEYWORDS="~arm x86"\nblah\n', True)
class CheckEbuildVirtualPv(cros_test_lib.MockTestCase):
"""Tests for _check_ebuild_virtual_pv."""
PORTAGE_STABLE = ProjectNamed('chromiumos/overlays/portage-stable')
CHROMIUMOS_OVERLAY = ProjectNamed('chromiumos/overlays/chromiumos')
BOARD_OVERLAY = ProjectNamed('chromiumos/overlays/board-overlays')
PRIVATE_OVERLAY = ProjectNamed('chromeos/overlays/overlay-link-private')
PRIVATE_VARIANT_OVERLAY = ProjectNamed('chromeos/overlays/'
'overlay-variant-daisy-spring-private')
def setUp(self):
self.file_mock = self.PatchObject(pre_upload, '_get_affected_files')
def testNoVirtuals(self):
"""Skip non virtual packages."""
self.file_mock.return_value = ['some/package/package-3.ebuild']
ret = pre_upload._check_ebuild_virtual_pv(ProjectNamed('overlay'), 'H')
self.assertEqual(ret, None)
def testCommonVirtuals(self):
"""Non-board overlays should use PV=1."""
template = 'virtual/foo/foo-%s.ebuild'
self.file_mock.return_value = [template % '1']
ret = pre_upload._check_ebuild_virtual_pv(self.CHROMIUMOS_OVERLAY, 'H')
self.assertEqual(ret, None)
self.file_mock.return_value = [template % '2']
ret = pre_upload._check_ebuild_virtual_pv(self.CHROMIUMOS_OVERLAY, 'H')
self.assertTrue(isinstance(ret, errors.HookFailure))
def testPublicBoardVirtuals(self):
"""Public board overlays should use PV=2."""
template = 'overlay-lumpy/virtual/foo/foo-%s.ebuild'
self.file_mock.return_value = [template % '2']
ret = pre_upload._check_ebuild_virtual_pv(self.BOARD_OVERLAY, 'H')
self.assertEqual(ret, None)
self.file_mock.return_value = [template % '2.5']
ret = pre_upload._check_ebuild_virtual_pv(self.BOARD_OVERLAY, 'H')
self.assertTrue(isinstance(ret, errors.HookFailure))
def testPublicBoardVariantVirtuals(self):
"""Public board variant overlays should use PV=2.5."""
template = 'overlay-variant-lumpy-foo/virtual/foo/foo-%s.ebuild'
self.file_mock.return_value = [template % '2.5']
ret = pre_upload._check_ebuild_virtual_pv(self.BOARD_OVERLAY, 'H')
self.assertEqual(ret, None)
self.file_mock.return_value = [template % '3']
ret = pre_upload._check_ebuild_virtual_pv(self.BOARD_OVERLAY, 'H')
self.assertTrue(isinstance(ret, errors.HookFailure))
def testPrivateBoardVirtuals(self):
"""Private board overlays should use PV=3."""
template = 'virtual/foo/foo-%s.ebuild'
self.file_mock.return_value = [template % '3']
ret = pre_upload._check_ebuild_virtual_pv(self.PRIVATE_OVERLAY, 'H')
self.assertEqual(ret, None)
self.file_mock.return_value = [template % '3.5']
ret = pre_upload._check_ebuild_virtual_pv(self.PRIVATE_OVERLAY, 'H')
self.assertTrue(isinstance(ret, errors.HookFailure))
def testPrivateBoardVariantVirtuals(self):
"""Private board variant overlays should use PV=3.5."""
template = 'virtual/foo/foo-%s.ebuild'
self.file_mock.return_value = [template % '3.5']
ret = pre_upload._check_ebuild_virtual_pv(self.PRIVATE_VARIANT_OVERLAY, 'H')
self.assertEqual(ret, None)
self.file_mock.return_value = [template % '4']
ret = pre_upload._check_ebuild_virtual_pv(self.PRIVATE_VARIANT_OVERLAY, 'H')
self.assertTrue(isinstance(ret, errors.HookFailure))
class CheckCrosLicenseCopyrightHeader(cros_test_lib.MockTestCase):
"""Tests for _check_cros_license."""
def setUp(self):
self.file_mock = self.PatchObject(pre_upload, '_get_affected_files')
self.content_mock = self.PatchObject(pre_upload, '_get_file_content')
def testOldHeaders(self):
"""Accept old header styles."""
HEADERS = (
('#!/bin/sh\n'
'# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.\n'
'# Use of this source code is governed by a BSD-style license that'
' can be\n'
'# found in the LICENSE file.\n'),
('// Copyright 2010-13 The Chromium OS Authors. All rights reserved.\n'
'// Use of this source code is governed by a BSD-style license that'
' can be\n'
'// found in the LICENSE file.\n'),
)
self.file_mock.return_value = ['file']
for header in HEADERS:
self.content_mock.return_value = header
self.assertEqual(None, pre_upload._check_cros_license('proj', 'sha1'))
def testRejectC(self):
"""Reject the (c) in newer headers."""
HEADERS = (
('// Copyright (c) 2015 The Chromium OS Authors. All rights reserved.\n'
'// Use of this source code is governed by a BSD-style license that'
' can be\n'
'// found in the LICENSE file.\n'),
('// Copyright (c) 2020 The Chromium OS Authors. All rights reserved.\n'
'// Use of this source code is governed by a BSD-style license that'
' can be\n'
'// found in the LICENSE file.\n'),
)
self.file_mock.return_value = ['file']
for header in HEADERS:
self.content_mock.return_value = header
self.assertNotEqual(None, pre_upload._check_cros_license('proj', 'sha1'))
class CheckAOSPLicenseCopyrightHeader(cros_test_lib.MockTestCase):
"""Tests for _check_aosp_license."""
def setUp(self):
self.file_mock = self.PatchObject(pre_upload, '_get_affected_files')
self.content_mock = self.PatchObject(pre_upload, '_get_file_content')
def testHeaders(self):
"""Accept old header styles."""
HEADERS = (
"""//
// Copyright (C) 2011 The Android Open Source Project
//
// 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.
//
""",
"""#
# Copyright (c) 2015 The Android Open Source Project
#
# 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.
#
""",
)
self.file_mock.return_value = ['file']
for header in HEADERS:
self.content_mock.return_value = header
self.assertEqual(None, pre_upload._check_aosp_license('proj', 'sha1'))
def testRejectNoLinesAround(self):
"""Reject headers missing the empty lines before/after the license."""
HEADERS = (
"""# Copyright (c) 2015 The Android Open Source Project
#
# 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.
""",
)
self.file_mock.return_value = ['file']
for header in HEADERS:
self.content_mock.return_value = header
self.assertNotEqual(None, pre_upload._check_aosp_license('proj', 'sha1'))
class CheckLayoutConfTestCase(cros_test_lib.MockTestCase):
"""Tests for _check_layout_conf."""
def setUp(self):
self.file_mock = self.PatchObject(pre_upload, '_get_affected_files')
self.content_mock = self.PatchObject(pre_upload, '_get_file_content')
def assertAccepted(self, files, project='project', commit='fake sha1'):
"""Assert _check_layout_conf accepts |files|."""
self.file_mock.return_value = files
ret = pre_upload._check_layout_conf(project, commit)
self.assertEqual(ret, None, msg='rejected with:\n%s' % ret)
def assertRejected(self, files, project='project', commit='fake sha1'):
"""Assert _check_layout_conf rejects |files|."""
self.file_mock.return_value = files
ret = pre_upload._check_layout_conf(project, commit)
self.assertTrue(isinstance(ret, errors.HookFailure))
def GetLayoutConf(self, filters=()):
"""Return a valid layout.conf with |filters| lines removed."""
all_lines = [
'masters = portage-stable chromiumos',
'profile-formats = portage-2 profile-default-eapi',
'profile_eapi_when_unspecified = 5-progress',
'repo-name = link',
'thin-manifests = true',
'use-manifests = strict',
]
lines = []
for line in all_lines:
for filt in filters:
if line.startswith(filt):
break
else:
lines.append(line)
return '\n'.join(lines)
def testNoFilesToCheck(self):
"""Don't blow up when there are no layout.conf files."""
self.assertAccepted([])
def testRejectRepoNameFile(self):
"""If profiles/repo_name is set, kick it out."""
self.assertRejected(['profiles/repo_name'])
def testAcceptValidLayoutConf(self):
"""Accept a fully valid layout.conf."""
self.content_mock.return_value = self.GetLayoutConf()
self.assertAccepted(['metadata/layout.conf'])
def testAcceptUnknownKeys(self):
"""Accept keys we don't explicitly know about."""
self.content_mock.return_value = self.GetLayoutConf() + '\nzzz-top = ok'
self.assertAccepted(['metadata/layout.conf'])
def testRejectUnsorted(self):
"""Reject an unsorted layout.conf."""
self.content_mock.return_value = 'zzz-top = bad\n' + self.GetLayoutConf()
self.assertRejected(['metadata/layout.conf'])
def testRejectMissingThinManifests(self):
"""Reject a layout.conf missing thin-manifests."""
self.content_mock.return_value = self.GetLayoutConf(
filters=['thin-manifests'])
self.assertRejected(['metadata/layout.conf'])
def testRejectMissingUseManifests(self):
"""Reject a layout.conf missing use-manifests."""
self.content_mock.return_value = self.GetLayoutConf(
filters=['use-manifests'])
self.assertRejected(['metadata/layout.conf'])
def testRejectMissingEapiFallback(self):
"""Reject a layout.conf missing profile_eapi_when_unspecified."""
self.content_mock.return_value = self.GetLayoutConf(
filters=['profile_eapi_when_unspecified'])
self.assertRejected(['metadata/layout.conf'])
def testRejectMissingRepoName(self):
"""Reject a layout.conf missing repo-name."""
self.content_mock.return_value = self.GetLayoutConf(filters=['repo-name'])
self.assertRejected(['metadata/layout.conf'])
class CommitMessageTestCase(cros_test_lib.MockTestCase):
"""Test case for funcs that check commit messages."""
def setUp(self):
self.msg_mock = self.PatchObject(pre_upload, '_get_commit_desc')
@staticmethod
def CheckMessage(_project, _commit):
raise AssertionError('Test class must declare CheckMessage')
# This dummy return is to silence pylint warning W1111 so we don't have to
# enable it for all the call sites below.
return 1 # pylint: disable=W0101
def assertMessageAccepted(self, msg, project=ProjectNamed('project'),
commit='1234'):
"""Assert _check_change_has_bug_field accepts |msg|."""
self.msg_mock.return_value = msg
ret = self.CheckMessage(project, commit)
self.assertEqual(ret, None)
def assertMessageRejected(self, msg, project=ProjectNamed('project'),
commit='1234'):
"""Assert _check_change_has_bug_field rejects |msg|."""
self.msg_mock.return_value = msg
ret = self.CheckMessage(project, commit)
self.assertTrue(isinstance(ret, errors.HookFailure))
class CheckCommitMessageBug(CommitMessageTestCase):
"""Tests for _check_change_has_bug_field."""
AOSP_PROJECT = pre_upload.Project(name='overlay', dir='', remote='aosp')
CROS_PROJECT = pre_upload.Project(name='overlay', dir='', remote='cros')
@staticmethod
def CheckMessage(project, commit):
return pre_upload._check_change_has_bug_field(project, commit)
def testNormal(self):
"""Accept a commit message w/a valid BUG."""
self.assertMessageAccepted('\nBUG=chromium:1234\n', self.CROS_PROJECT)
self.assertMessageAccepted('\nBUG=chrome-os-partner:1234\n',
self.CROS_PROJECT)
self.assertMessageAccepted('\nBUG=b:1234\n', self.CROS_PROJECT)
self.assertMessageAccepted('\nBug: 1234\n', self.AOSP_PROJECT)
self.assertMessageAccepted('\nBug:1234\n', self.AOSP_PROJECT)
def testNone(self):
"""Accept BUG=None."""
self.assertMessageAccepted('\nBUG=None\n', self.CROS_PROJECT)
self.assertMessageAccepted('\nBUG=none\n', self.CROS_PROJECT)
self.assertMessageAccepted('\nBug: None\n', self.AOSP_PROJECT)
self.assertMessageAccepted('\nBug:none\n', self.AOSP_PROJECT)
for project in (self.AOSP_PROJECT, self.CROS_PROJECT):
self.assertMessageRejected('\nBUG=NONE\n', project)
def testBlank(self):
"""Reject blank values."""
for project in (self.AOSP_PROJECT, self.CROS_PROJECT):
self.assertMessageRejected('\nBUG=\n', project)
self.assertMessageRejected('\nBUG= \n', project)
self.assertMessageRejected('\nBug:\n', project)
self.assertMessageRejected('\nBug: \n', project)
def testNotFirstLine(self):
"""Reject the first line."""
for project in (self.AOSP_PROJECT, self.CROS_PROJECT):
self.assertMessageRejected('BUG=None\n\n\n', project)
def testNotInline(self):
"""Reject not at the start of line."""
for project in (self.AOSP_PROJECT, self.CROS_PROJECT):
self.assertMessageRejected('\n BUG=None\n', project)
self.assertMessageRejected('\n\tBUG=None\n', project)
def testOldTrackers(self):
"""Reject commit messages using old trackers."""
self.assertMessageRejected('\nBUG=chromium-os:1234\n', self.CROS_PROJECT)
def testNoTrackers(self):
"""Reject commit messages w/invalid trackers."""
self.assertMessageRejected('\nBUG=booga:1234\n', self.CROS_PROJECT)
self.assertMessageRejected('\nBUG=br:1234\n', self.CROS_PROJECT)
def testMissing(self):
"""Reject commit messages w/no BUG line."""
self.assertMessageRejected('foo\n')
def testCase(self):
"""Reject bug lines that are not BUG."""
self.assertMessageRejected('\nbug=none\n')
class CheckCommitMessageCqDepend(CommitMessageTestCase):
"""Tests for _check_change_has_valid_cq_depend."""
@staticmethod
def CheckMessage(project, commit):
return pre_upload._check_change_has_valid_cq_depend(project, commit)
def testNormal(self):
"""Accept valid CQ-DEPENDs line."""
self.assertMessageAccepted('\nCQ-DEPEND=CL:1234\n')
def testInvalid(self):
"""Reject invalid CQ-DEPENDs line."""
self.assertMessageRejected('\nCQ-DEPEND=CL=1234\n')
self.assertMessageRejected('\nCQ-DEPEND=None\n')
class CheckCommitMessageTest(CommitMessageTestCase):
"""Tests for _check_change_has_test_field."""
@staticmethod
def CheckMessage(project, commit):
return pre_upload._check_change_has_test_field(project, commit)
def testNormal(self):
"""Accept a commit message w/a valid TEST."""
self.assertMessageAccepted('\nTEST=i did it\n')
def testNone(self):
"""Accept TEST=None."""
self.assertMessageAccepted('\nTEST=None\n')
self.assertMessageAccepted('\nTEST=none\n')
def testBlank(self):
"""Reject blank values."""
self.assertMessageRejected('\nTEST=\n')
self.assertMessageRejected('\nTEST= \n')
def testNotFirstLine(self):
"""Reject the first line."""
self.assertMessageRejected('TEST=None\n\n\n')
def testNotInline(self):
"""Reject not at the start of line."""
self.assertMessageRejected('\n TEST=None\n')
self.assertMessageRejected('\n\tTEST=None\n')
def testMissing(self):
"""Reject commit messages w/no TEST line."""
self.assertMessageRejected('foo\n')
def testCase(self):
"""Reject bug lines that are not TEST."""
self.assertMessageRejected('\ntest=none\n')
class CheckCommitMessageChangeId(CommitMessageTestCase):
"""Tests for _check_change_has_proper_changeid."""
@staticmethod
def CheckMessage(project, commit):
return pre_upload._check_change_has_proper_changeid(project, commit)
def testNormal(self):
"""Accept a commit message w/a valid Change-Id."""
self.assertMessageAccepted('foo\n\nChange-Id: I1234\n')
def testBlank(self):
"""Reject blank values."""
self.assertMessageRejected('\nChange-Id:\n')
self.assertMessageRejected('\nChange-Id: \n')
def testNotFirstLine(self):
"""Reject the first line."""
self.assertMessageRejected('TEST=None\n\n\n')
def testNotInline(self):
"""Reject not at the start of line."""
self.assertMessageRejected('\n Change-Id: I1234\n')
self.assertMessageRejected('\n\tChange-Id: I1234\n')
def testMissing(self):
"""Reject commit messages missing the line."""
self.assertMessageRejected('foo\n')
def testCase(self):
"""Reject bug lines that are not Change-Id."""
self.assertMessageRejected('\nchange-id: I1234\n')
self.assertMessageRejected('\nChange-id: I1234\n')
self.assertMessageRejected('\nChange-ID: I1234\n')
def testEnd(self):
"""Reject Change-Id's that are not last."""
self.assertMessageRejected('\nChange-Id: I1234\nbar\n')
def testSobTag(self):
"""Permit s-o-b tags to follow the Change-Id."""
self.assertMessageAccepted('foo\n\nChange-Id: I1234\nSigned-off-by: Hi\n')
class CheckCommitMessageStyle(CommitMessageTestCase):
"""Tests for _check_commit_message_style."""
@staticmethod
def CheckMessage(project, commit):
return pre_upload._check_commit_message_style(project, commit)
def testNormal(self):
"""Accept valid commit messages."""
self.assertMessageAccepted('one sentence.\n')
self.assertMessageAccepted('some.module: do it!\n')
self.assertMessageAccepted('one line\n\nmore stuff here.')
def testNoBlankSecondLine(self):
"""Reject messages that have stuff on the second line."""
self.assertMessageRejected('one sentence.\nbad fish!\n')
def testFirstLineMultipleSentences(self):
"""Reject messages that have more than one sentence in the summary."""
self.assertMessageRejected('one sentence. two sentence!\n')
def testFirstLineTooLone(self):
"""Reject first lines that are too long."""
self.assertMessageRejected('o' * 200)
def DiffEntry(src_file=None, dst_file=None, src_mode=None, dst_mode='100644',
status='M'):
"""Helper to create a stub RawDiffEntry object"""
if src_mode is None:
if status == 'A':
src_mode = '000000'
elif status == 'M':
src_mode = dst_mode
elif status == 'D':
src_mode = dst_mode
dst_mode = '000000'
src_sha = dst_sha = 'abc'
if status == 'D':
dst_sha = '000000'
elif status == 'A':
src_sha = '000000'
return git.RawDiffEntry(src_mode=src_mode, dst_mode=dst_mode, src_sha=src_sha,
dst_sha=dst_sha, status=status, score=None,
src_file=src_file, dst_file=dst_file)
class HelpersTest(cros_test_lib.MockTempDirTestCase):
"""Various tests for utility functions."""
def setUp(self):
self.orig_cwd = os.getcwd()
os.chdir(self.tempdir)
self.PatchObject(git, 'RawDiff', return_value=[
# A modified normal file.
DiffEntry(src_file='buildbot/constants.py', status='M'),
# A new symlink file.
DiffEntry(dst_file='scripts/cros_env_whitelist', dst_mode='120000',
status='A'),
# A deleted file.
DiffEntry(src_file='scripts/sync_sonic.py', status='D'),
])
def tearDown(self):
os.chdir(self.orig_cwd)
def _WritePresubmitIgnoreFile(self, subdir, data):
"""Writes to a .presubmitignore file in the passed-in subdirectory."""
directory = os.path.join(self.tempdir, subdir)
if not os.path.exists(directory):
os.makedirs(directory)
osutils.WriteFile(os.path.join(directory, pre_upload._IGNORE_FILE), data)
def testGetAffectedFilesNoDeletesNoRelative(self):
"""Verify _get_affected_files() works w/no delete & not relative."""
path = os.getcwd()
files = pre_upload._get_affected_files('HEAD', include_deletes=False,
relative=False)
exp_files = [os.path.join(path, 'buildbot/constants.py')]
self.assertEquals(files, exp_files)
def testGetAffectedFilesDeletesNoRelative(self):
"""Verify _get_affected_files() works w/delete & not relative."""
path = os.getcwd()
files = pre_upload._get_affected_files('HEAD', include_deletes=True,
relative=False)
exp_files = [os.path.join(path, 'buildbot/constants.py'),
os.path.join(path, 'scripts/sync_sonic.py')]
self.assertEquals(files, exp_files)
def testGetAffectedFilesNoDeletesRelative(self):
"""Verify _get_affected_files() works w/no delete & relative."""
files = pre_upload._get_affected_files('HEAD', include_deletes=False,
relative=True)
exp_files = ['buildbot/constants.py']
self.assertEquals(files, exp_files)
def testGetAffectedFilesDeletesRelative(self):
"""Verify _get_affected_files() works w/delete & relative."""
files = pre_upload._get_affected_files('HEAD', include_deletes=True,
relative=True)
exp_files = ['buildbot/constants.py', 'scripts/sync_sonic.py']
self.assertEquals(files, exp_files)
def testGetAffectedFilesDetails(self):
"""Verify _get_affected_files() works w/full_details."""
files = pre_upload._get_affected_files('HEAD', full_details=True,
relative=True)
self.assertEquals(files[0].src_file, 'buildbot/constants.py')
def testGetAffectedFilesPresubmitIgnoreDirectory(self):
"""Verify .presubmitignore can be used to exclude a directory."""
self._WritePresubmitIgnoreFile('.', 'buildbot/')
self.assertEquals(pre_upload._get_affected_files('HEAD', relative=True), [])
def testGetAffectedFilesPresubmitIgnoreDirectoryWildcard(self):
"""Verify .presubmitignore can be used with a directory wildcard."""
self._WritePresubmitIgnoreFile('.', '*/constants.py')
self.assertEquals(pre_upload._get_affected_files('HEAD', relative=True), [])
def testGetAffectedFilesPresubmitIgnoreWithinDirectory(self):
"""Verify .presubmitignore can be placed in a subdirectory."""
self._WritePresubmitIgnoreFile('buildbot', '*.py')
self.assertEquals(pre_upload._get_affected_files('HEAD', relative=True), [])
def testGetAffectedFilesPresubmitIgnoreDoesntMatch(self):
"""Verify .presubmitignore has no effect when it doesn't match a file."""
self._WritePresubmitIgnoreFile('buildbot', '*.txt')
self.assertEquals(pre_upload._get_affected_files('HEAD', relative=True),
['buildbot/constants.py'])
def testGetAffectedFilesPresubmitIgnoreAddedFile(self):
"""Verify .presubmitignore matches added files."""
self._WritePresubmitIgnoreFile('.', 'buildbot/\nscripts/')
self.assertEquals(pre_upload._get_affected_files('HEAD', relative=True,
include_symlinks=True),
[])
def testGetAffectedFilesPresubmitIgnoreSkipIgnoreFile(self):
"""Verify .presubmitignore files are automatically skipped."""