Loading...
   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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
# SPDX-License-Identifier: GPL-2.0+
# Copyright (c) 2014 Google, Inc
#
# pylint: disable=too-many-lines

"""Functional tests for buildman

These tests run buildman with mocked commands to verify its behavior
without actually building anything.
"""

import io
import os
from pathlib import Path
import re
import shutil
import sys
import tempfile
import time
import unittest
from unittest import mock

from buildman import board
from buildman import boards
from buildman.boards import Extended
from buildman import bsettings
from buildman import builderthread
from buildman import cfgutil
from buildman import cmdline
from buildman import control
from buildman import toolchain
from u_boot_pylib import command
from u_boot_pylib import gitutil
from u_boot_pylib import terminal
from u_boot_pylib import tools

SETTINGS_DATA = '''
# Buildman settings file
[global]

[toolchain]

[toolchain-alias]

[make-flags]
src=/home/sjg/c/src
chroot=/home/sjg/c/chroot
vboot=VBOOT_DEBUG=1 MAKEFLAGS_VBOOT=DEBUG=1 CFLAGS_EXTRA_VBOOT=-DUNROLL_LOOPS VBOOT_SOURCE=${src}/platform/vboot_reference
chromeos_coreboot=VBOOT=${chroot}/build/link/usr ${vboot}
chromeos_daisy=VBOOT=${chroot}/build/daisy/usr ${vboot}
chromeos_peach=VBOOT=${chroot}/build/peach_pit/usr ${vboot}
'''

BOARDS = [
    ['Active', 'arm', 'armv7', '', 'Tester', 'ARM Board 0', 'board0',  ''],
    ['Active', 'arm', 'armv7', '', 'Tester', 'ARM Board 1', 'board1', ''],
    ['Active', 'powerpc', 'powerpc', '', 'Tester', 'PowerPC board 1', 'board2',
     ''],
    ['Active', 'sandbox', 'sandbox', '', 'Tester', 'Sandbox board', 'board4',
     ''],
]

COMMIT_SHORTLOG = """4aca821 patman: Avoid changing the order of tags
39403bb patman: Use --no-pager' to stop git from forking a pager
db6e6f2 patman: Remove the -a option
f2ccf03 patman: Correct unit tests to run correctly
1d097f9 patman: Fix indentation in terminal.py
d073747 patman: Support the 'reverse' option for 'git log
"""

commit_log = ["""commit 7f6b8315d18f683c5181d0c3694818c1b2a20dcd
Author: Masahiro Yamada <yamada.m@jp.panasonic.com>
Date:   Fri Aug 22 19:12:41 2014 +0900

    buildman: refactor help message

    "buildman [options]" is displayed by default.

    Append the rest of help messages to parser.usage
    instead of replacing it.

    Besides, "-b <branch>" is not mandatory since commit fea5858e.
    Drop it from the usage.

    Signed-off-by: Masahiro Yamada <yamada.m@jp.panasonic.com>
""",
"""commit d0737479be6baf4db5e2cdbee123e96bc5ed0ba8
Author: Simon Glass <sjg@chromium.org>
Date:   Thu Aug 14 16:48:25 2014 -0600

    patman: Support the 'reverse' option for 'git log'

    This option is currently not supported, but needs to be, for buildman to
    operate as expected.

    Series-changes: 7
    - Add new patch to fix the 'reverse' bug

    Series-version: 8

    Change-Id: I79078f792e8b390b8a1272a8023537821d45feda
    Reported-by: York Sun <yorksun@freescale.com>
    Signed-off-by: Simon Glass <sjg@chromium.org>

""",
"""commit 1d097f9ab487c5019152fd47bda126839f3bf9fc
Author: Simon Glass <sjg@chromium.org>
Date:   Sat Aug 9 11:44:32 2014 -0600

    patman: Fix indentation in terminal.py

    This code came from a different project with 2-character indentation. Fix
    it for U-Boot.

    Series-changes: 6
    - Add new patch to fix indentation in teminal.py

    Change-Id: I5a74d2ebbb3cc12a665f5c725064009ac96e8a34
    Signed-off-by: Simon Glass <sjg@chromium.org>

""",
"""commit f2ccf03869d1e152c836515a3ceb83cdfe04a105
Author: Simon Glass <sjg@chromium.org>
Date:   Sat Aug 9 11:08:24 2014 -0600

    patman: Correct unit tests to run correctly

    It seems that doctest behaves differently now, and some of the unit tests
    do not run. Adjust the tests to work correctly.

     ./tools/patman/patman --test
    <unittest.result.TestResult run=10 errors=0 failures=0>

    Series-changes: 6
    - Add new patch to fix patman unit tests

    Change-Id: I3d2ca588f4933e1f9d6b1665a00e4ae58269ff3b

""",
"""commit db6e6f2f9331c5a37647d6668768d4a40b8b0d1c
Author: Simon Glass <sjg@chromium.org>
Date:   Sat Aug 9 12:06:02 2014 -0600

    patman: Remove the -a option

    It seems that this is no longer needed, since checkpatch.pl will catch
    whitespace problems in patches. Also the option is not widely used, so
    it seems safe to just remove it.

    Series-changes: 6
    - Add new patch to remove patman's -a option

    Suggested-by: Masahiro Yamada <yamada.m@jp.panasonic.com>
    Change-Id: I5821a1c75154e532c46513486ca40b808de7e2cc

""",
"""commit 39403bb4f838153028a6f21ca30bf100f3791133
Author: Simon Glass <sjg@chromium.org>
Date:   Thu Aug 14 21:50:52 2014 -0600

    patman: Use --no-pager' to stop git from forking a pager

""",
"""commit 4aca821e27e97925c039e69fd37375b09c6f129c
Author: Simon Glass <sjg@chromium.org>
Date:   Fri Aug 22 15:57:39 2014 -0600

    patman: Avoid changing the order of tags

    patman collects tags that it sees in the commit and places them nicely
    sorted at the end of the patch. However, this is not really necessary and
    in fact is apparently not desirable.

    Series-changes: 9
    - Add new patch to avoid changing the order of tags

    Series-version: 9

    Suggested-by: Masahiro Yamada <yamada.m@jp.panasonic.com>
    Change-Id: Ib1518588c1a189ad5c3198aae76f8654aed8d0db
"""]

TEST_BRANCH = '__testbranch'


# pylint: disable=too-many-instance-attributes disable=too-many-public-methods
class TestFunctional(unittest.TestCase):
    """Functional test for buildman.

    This aims to test from just below the invocation of buildman (parsing
    of arguments) to 'make' and 'git' invocation. It is not a true
    emd-to-end test, as it mocks git, make and the tool chain. But this
    makes it easier to detect when the builder is doing the wrong thing,
    since in many cases this test code will fail. For example, only a
    very limited subset of 'git' arguments is supported - anything
    unexpected will fail.
    """
    def setUp(self):
        self._base_dir = tempfile.mkdtemp()
        self._output_dir = tempfile.mkdtemp()
        self._git_dir = os.path.join(self._base_dir, 'src')
        self._buildman_pathname = sys.argv[0]
        self._buildman_dir = os.path.dirname(os.path.realpath(sys.argv[0]))
        command.TEST_RESULT = self._handle_command
        bsettings.setup(None)
        bsettings.add_file(SETTINGS_DATA)
        self.setup_toolchains()
        self._toolchains.add('arm-gcc', test=False)
        self._toolchains.add('powerpc-gcc', test=False)
        self._boards = boards.Boards()
        for brd in BOARDS:
            self._boards.add_board(board.Board(*brd))

        # Directories where the source been cloned
        self._clone_dirs = []
        self._commits = len(COMMIT_SHORTLOG.splitlines()) + 1
        self._total_builds = self._commits * len(BOARDS)

        # Number of calls to make
        self._make_calls = 0

        # Map of [board, commit] to error messages
        self._error = {}

        self._test_branch = TEST_BRANCH

        # Set to True to report missing blobs
        self._missing = False

        # Builder instance from control module (set by _run_control)
        self._builder = None

        # Counters for mock command handlers
        self._nm_calls = 0
        self._size_calls = 0

        # Captured make arguments for testing
        self._captured_make_args = []

        self._buildman_dir = os.path.dirname(os.path.realpath(sys.argv[0]))
        self._test_dir = os.path.join(self._buildman_dir, 'test')

        # Set up some fake source files
        shutil.copytree(self._test_dir, self._git_dir)

        # Avoid sending any output and clear all terminal output
        terminal.set_print_test_mode()
        terminal.get_print_test_lines()

    def tearDown(self):
        shutil.rmtree(self._base_dir)
        shutil.rmtree(self._output_dir)

    def setup_toolchains(self):
        """Set up toolchains for testing"""
        self._toolchains = toolchain.Toolchains()
        self._toolchains.add('gcc', test=False)

    def _run_buildman(self, *args):
        all_args = [self._buildman_pathname] + list(args)
        return command.run_one(*all_args, capture=True, capture_stderr=True)

    def _run_control(self, *args, brds=False, clean_dir=False,
                    test_thread_exceptions=False, get_builder=True):
        """Run buildman

        Args:
            args: List of arguments to pass
            brds: Boards object, or False to pass self._boards, or None to pass
                None
            clean_dir: Used for tests only, indicates that the existing
                output_dir should be removed before starting the build
            test_thread_exceptions: Uses for tests only, True to make the
                threads raise an exception instead of reporting their result.
                This simulates a failure in the code somewhere
            get_builder (bool): Set self._builder to the resulting builder

        Returns:
            result code from buildman
        """
        sys.argv = [sys.argv[0]] + list(args)
        args = cmdline.parse_args()
        if brds is False:
            brds = self._boards
        result = control.do_buildman(
            args, toolchains=self._toolchains, make_func=self._handle_make,
            brds=brds, clean_dir=clean_dir,
            test_thread_exceptions=test_thread_exceptions)
        if get_builder:
            self._builder = control.TEST_BUILDER
        return result

    def test_full_help(self):
        """Test that -H shows the full README.rst file"""
        command.TEST_RESULT = None
        result = self._run_buildman('-H')
        help_file = os.path.join(self._buildman_dir, 'README.rst')
        # Remove possible extraneous strings
        extra = '::::::::::::::\n' + help_file + '\n::::::::::::::\n'
        gothelp = result.stdout.replace(extra, '')
        self.assertEqual(len(gothelp.encode('utf-8')),
                         os.path.getsize(help_file))
        self.assertEqual(0, len(result.stderr))
        self.assertEqual(0, result.return_code)

    def test_help(self):
        """Test that -h shows help text"""
        command.TEST_RESULT = None
        result = self._run_buildman('-h')
        self.assertTrue(len(result.stdout) > 1000)
        self.assertEqual(0, len(result.stderr))
        self.assertEqual(0, result.return_code)

    def test_git_setup(self):
        """Test gitutils.Setup(), from outside the module itself"""
        command.TEST_RESULT = command.CommandResult(return_code=1)
        gitutil.setup()
        self.assertEqual(gitutil.USE_NO_DECORATE, False)

        command.TEST_RESULT = command.CommandResult(return_code=0)
        gitutil.setup()
        self.assertEqual(gitutil.USE_NO_DECORATE, True)

    def _handle_command_git_log(self, args):
        if args[-1] == '--':
            args = args[:-1]
        if '-n0' in args:
            return command.CommandResult(return_code=0)
        if args[-1] == f'upstream/master..{self._test_branch}':
            return command.CommandResult(return_code=0, stdout=COMMIT_SHORTLOG)
        if args[:3] == ['--no-color', '--no-decorate', '--reverse']:
            if args[-1] == self._test_branch:
                count = int(args[3][2:])
                return command.CommandResult(return_code=0,
                                            stdout=''.join(commit_log[:count]))

        # Not handled, so abort
        print('git log', args)
        sys.exit(1)

    def _handle_command_git_config(self, args):
        config = args[0]
        if config == 'sendemail.aliasesfile':
            return command.CommandResult(return_code=0)
        if config.startswith('branch.badbranch'):
            return command.CommandResult(return_code=1)
        if config == f'branch.{self._test_branch}.remote':
            return command.CommandResult(return_code=0, stdout='upstream\n')
        if config == f'branch.{self._test_branch}.merge':
            return command.CommandResult(return_code=0,
                                         stdout='refs/heads/master\n')

        # Not handled, so abort
        print('git config', args)
        sys.exit(1)

    def _handle_command_git(self, in_args):
        """Handle execution of a git command

        This uses a hacked-up parser.

        Args:
            in_args: Arguments after 'git' from the command line
        """
        git_args = []           # Top-level arguments to git itself
        sub_cmd = None          # Git sub-command selected
        args = []               # Arguments to the git sub-command
        for arg in in_args:
            if sub_cmd:
                args.append(arg)
            elif arg[0] == '-':
                git_args.append(arg)
            else:
                if git_args and git_args[-1] in ['--git-dir', '--work-tree']:
                    git_args.append(arg)
                else:
                    sub_cmd = arg
        if sub_cmd == 'config':
            return self._handle_command_git_config(args)
        if sub_cmd == 'log':
            return self._handle_command_git_log(args)
        if sub_cmd == 'clone':
            return command.CommandResult(return_code=0)
        if sub_cmd == 'checkout':
            return command.CommandResult(return_code=0)
        if sub_cmd == 'worktree':
            return command.CommandResult(return_code=0)

        # Not handled, so abort
        print('git', git_args, sub_cmd, args)
        sys.exit(1)

    def _handle_command_nm(self, _args):
        # Return nm --size-sort output with function sizes that vary between
        # calls to simulate changes between commits
        self._nm_calls += 1
        base = self._nm_calls * 0x10
        stdout = f'''{0x100 + base:08x} T main
{0x80 + base:08x} T board_init
{0x50 + base:08x} t local_func
{0x30:08x} d data_var
'''
        return command.CommandResult(return_code=0, stdout=stdout)

    def _handle_command_objdump(self, _args):
        # Return objdump -h output with .rodata section
        stdout = '''
u-boot:     file format elf32-littlearm

Sections:
Idx Name          Size      VMA       LMA       File off  Algn
  0 .text         00010000  00000000  00000000  00001000  2**2
  1 .rodata       00001000  00010000  00010000  00011000  2**2
  2 .data         00000100  00011000  00011000  00012000  2**2
'''
        return command.CommandResult(return_code=0, stdout=stdout)

    def _handle_command_objcopy(self, _args):
        return command.CommandResult(return_code=0)

    def _handle_command_size(self, args):
        # Return size output - vary the size based on call count to simulate
        # changes between commits
        self._size_calls += 1
        text = 10000 + self._size_calls * 100
        data = 1000
        bss = 500
        total = text + data + bss
        fname = args[-1] if args else 'u-boot'
        stdout = f'''   text    data     bss     dec     hex filename
  {text}    {data}     {bss}   {total}    {total:x} {fname}
'''
        return command.CommandResult(return_code=0, stdout=stdout)

    def _handle_command_cpp(self, args):
        # args ['-nostdinc', '-P', '-I', '/tmp/tmp7f17xk_o/src', '-undef',
        # '-x', 'assembler-with-cpp', fname]
        fname = args[7]
        buf = io.StringIO()
        for line in tools.read_file(fname, False).splitlines():
            if line.startswith('#include'):
                # Example: #include <configs/renesas_rcar2.config>
                m_incfname = re.match('#include <(.*)>', line)
                data = tools.read_file(m_incfname.group(1), False)
                for line in data.splitlines():
                    print(line, file=buf)
            else:
                print(line, file=buf)
        return command.CommandResult(stdout=buf.getvalue(), return_code=0)

    def _run_merge_config(self, cmd_list, _kwargs):
        """Run the real merge_config.sh script

        Runs merge_config.sh from the real U-Boot source tree with real
        Kconfig files for actual dependency resolution.

        Args:
            cmd_list (list): Original command and arguments

        Returns:
            CommandResult: Result of running the script
        """
        # Run from the real U-Boot source tree (not the test's fake git dir)
        src_root = os.path.dirname(os.path.dirname(self._buildman_dir))
        merge_script = os.path.join(src_root, 'scripts', 'kconfig',
                                    'merge_config.sh')

        # Build command with real script path, keeping original arguments
        new_cmd = [merge_script] + list(cmd_list[1:])

        # Use a clean host environment with CROSS_COMPILE cleared so Kconfig
        # uses the host gcc
        env = dict(os.environ)
        env['CROSS_COMPILE'] = ''

        # Temporarily disable TEST_RESULT to run the real command
        old_test_result = command.TEST_RESULT
        command.TEST_RESULT = None
        try:
            result = command.run_one(*new_cmd, cwd=src_root, env=env,
                                     capture=True, capture_stderr=True)
        finally:
            command.TEST_RESULT = old_test_result
        return result

    def _handle_make_savedefconfig(self, args, _kwargs):
        """Handle make savedefconfig command

        This runs the real make savedefconfig to create a minimal defconfig
        from the current .config file.

        Args:
            args (list): Arguments to make (after 'make')

        Returns:
            CommandResult: Result of running the command
        """
        # Run from the U-Boot source tree
        src_root = os.path.dirname(os.path.dirname(self._buildman_dir))

        # Build the full command
        new_cmd = ['make'] + list(args)

        # Use a clean host environment with CROSS_COMPILE cleared
        env = dict(os.environ)
        env['CROSS_COMPILE'] = ''

        # Temporarily disable TEST_RESULT to run the real command
        old_test_result = command.TEST_RESULT
        command.TEST_RESULT = None
        try:
            result = command.run_one(*new_cmd, cwd=src_root, env=env,
                                     capture=True, capture_stderr=True)
        finally:
            command.TEST_RESULT = old_test_result
        return result

    def _handle_command(self, **kwargs): # pylint: disable=too-many-branches
        """Handle a command execution.

        The command is in kwargs['pipe-list'], as a list of pipes, each a
        list of commands. The command should be emulated as required for
        testing purposes.

        Returns:
            A CommandResult object
        """
        pipe_list = kwargs['pipe_list']
        wc = False
        if len(pipe_list) != 1:
            if pipe_list[1] == ['wc', '-l']:
                wc = True
            else:
                print('invalid pipe', kwargs)
                sys.exit(1)
        cmd = pipe_list[0][0]
        args = pipe_list[0][1:]
        if cmd == 'git':
            result = self._handle_command_git(args)
        elif cmd == './scripts/show-gnu-make':
            result = command.CommandResult(return_code=0, stdout='make')
        elif cmd.endswith('nm'):
            result = self._handle_command_nm(args)
        elif cmd.endswith('objdump'):
            result = self._handle_command_objdump(args)
        elif cmd.endswith('objcopy'):
            result = self._handle_command_objcopy(args)
        elif cmd.endswith('size'):
            result = self._handle_command_size(args)
        elif cmd.endswith('cpp'):
            result = self._handle_command_cpp(args)
        elif cmd == 'gcc' and args[0] == '-E':
            result = self._handle_command_cpp(args[1:])
        elif cmd.endswith('merge_config.sh'):
            # Run the real merge_config.sh using command.run_one()
            result = self._run_merge_config(pipe_list[0], kwargs)
        elif cmd == 'make' and 'savedefconfig' in args:
            # Handle make savedefconfig - create minimal defconfig from .config
            result = self._handle_make_savedefconfig(args, kwargs)
        else:
            # Not handled, so abort
            print('unknown command', kwargs)
            sys.exit(1)

        if wc:
            result.stdout = len(result.stdout.splitlines())
        return result

    def _handle_make(self, commit, brd, stage, cwd, *args, **_kwargs): # pylint: disable=too-many-branches
        """Handle execution of 'make'

        Args:
            commit: Commit object that is being built
            brd: Board object that is being built
            stage: Stage that we are at (mrproper, config, build)
            cwd: Directory where make should be run
            args: Arguments to pass to make
            kwargs: Arguments to pass to command.run_one()
        """
        self._make_calls += 1
        # Capture args for tests that need to inspect them
        if hasattr(self, '_captured_make_args') and stage == 'build':
            self._captured_make_args.append(args)
        out_dir = ''
        for arg in args:
            if arg.startswith('O='):
                out_dir = arg[2:]
        if stage == 'mrproper':
            return command.CommandResult(return_code=0)
        if stage == 'config':
            fname = os.path.join(cwd or '', out_dir, '.config')
            # Vary config based on commit to simulate config changes
            seq = commit.sequence if hasattr(commit, 'sequence') else 0
            config = f'CONFIG_SOMETHING={seq + 1}\n'
            if seq > 0:
                config += 'CONFIG_NEW_OPTION=y\n'
            tools.write_file(fname, config.encode('utf-8'))
            # Also create u-boot.cfg which buildman reads for -K flag
            cfg_fname = os.path.join(cwd or '', out_dir, 'u-boot.cfg')
            cfg_content = f'#define CONFIG_VALUE {seq + 100}\n'
            if seq > 0:
                cfg_content += '#define CONFIG_EXTRA 1\n'
            tools.write_file(cfg_fname, cfg_content.encode('utf-8'))
            return command.CommandResult(return_code=0,
                    combined='Test configuration complete')
        if stage == 'oldconfig':
            return command.CommandResult(return_code=0)
        if stage == 'build':
            stderr = ''
            fname = os.path.join(cwd or '', out_dir, 'u-boot')
            tools.write_file(fname, b'U-Boot')

            # Handle missing blobs
            if self._missing:
                if 'BINMAN_ALLOW_MISSING=1' in args:
                    stderr = ("+Image 'main-section' is missing external "
                              'blobs and is non-functional: intel-descriptor '
                              'intel-ifwi intel-fsp-m intel-fsp-s intel-vbt\n'
                              "Image 'main-section' has faked external blobs "
                              'and is non-functional: descriptor.bin fsp_m.bin '
                              'fsp_s.bin vbt.bin\n\nSome images are invalid')
                else:
                    stderr = ("binman: Filename 'fsp.bin' not found in "
                              'input path')
            elif not isinstance(commit, str):
                stderr = self._error.get((brd.target, commit.sequence))
            else:
                # For current source builds, commit is 'current'
                stderr = self._error.get((brd.target, commit))

            if stderr:
                return command.CommandResult(return_code=2, stderr=stderr)
            return command.CommandResult(return_code=0)

        # Not handled, so abort
        print('_handle_make failure: make', stage)
        sys.exit(1)

    def print_lines(self, lines):
        """Print output lines for debugging tests

        Args:
            lines (list of str): Lines to print
        """
        print(len(lines))
        for line in lines:
            print(line)
        #self.print_lines(terminal.get_print_test_lines())

    def test_no_boards(self):
        """Test that buildman aborts when there are no boards"""
        self._boards = boards.Boards()
        with self.assertRaises(SystemExit):
            self._run_control()

    def test_current_source(self):
        """Very simple test to invoke buildman on the current source"""
        self.setup_toolchains()
        self._run_control('-o', self._output_dir)
        lines = terminal.get_print_test_lines()
        self.assertIn(f'Building current source for {len(BOARDS)} boards',
                      lines[0].text)

    def test_bad_branch(self):
        """Test that we can detect an invalid branch"""
        with self.assertRaises(ValueError):
            self._run_control('-b', 'badbranch')

    def test_bad_toolchain(self):
        """Test that missing toolchains are detected"""
        self.setup_toolchains()
        ret_code = self._run_control('-b', TEST_BRANCH, '-o', self._output_dir)
        lines = terminal.get_print_test_lines()

        # Buildman always builds the upstream commit as well
        self.assertIn(
            f'Building {self._commits} commits for {len(BOARDS)} boards',
            lines[0].text)
        self.assertEqual(self._builder.count, self._total_builds)

        # Only sandbox should succeed, the others don't have toolchains
        self.assertEqual(self._builder.fail,
                         self._total_builds - self._commits)
        self.assertEqual(ret_code, 100)

        for commit in range(self._commits):
            for brd in self._boards.get_list():
                if brd.arch != 'sandbox':
                    errfile = self._builder.get_err_file(commit, brd.target)
                    data = tools.read_file(errfile, binary=False)
                    self.assertEqual(
                        data.splitlines(),
                        [f'Tool chain error for {brd.arch}: '
                         f"No tool chain found for arch '{brd.arch}'"])

    def test_toolchain_errors(self):
        """Test that toolchain errors are reported in the summary

        When toolchains are missing, boards cannot be built. The summary
        should report which boards were not built.
        """
        self.setup_toolchains()
        # Build with missing toolchains - only sandbox will succeed
        self._run_control('-b', TEST_BRANCH, '-o', self._output_dir)

        # Now show summary - should report boards not built
        terminal.get_print_test_lines()  # Clear
        self._run_control('-b', TEST_BRANCH, '-o', self._output_dir, '-s',
                         clean_dir=False)
        lines = terminal.get_print_test_lines()
        text = '\n'.join(line.text for line in lines)

        # Check that boards with missing toolchains are reported as not built
        self.assertIn('Boards not built', text)
        self.assertIn('board0', text)
        self.assertIn('board1', text)
        self.assertIn('board2', text)

    def test_branch(self):
        """Test building a branch with all toolchains present"""
        self._run_control('-b', TEST_BRANCH, '-o', self._output_dir)
        self.assertEqual(self._builder.count, self._total_builds)
        self.assertEqual(self._builder.fail, 0)

    def test_current_source_ide(self):
        """Test building current source with IDE mode enabled

        This tests that:
        - Build errors are written to stderr
        - Progress output does not go to stdout in IDE mode
        - Summary mode (-s) shows output again
        """
        # Set up a build error for sandbox board (board4) which has toolchain
        # For current source builds, commit is 'current' string
        error_msg = 'test_error_msg.c:123: error: test failure\n'
        self._error['board4', 'current'] = error_msg

        # Capture stderr during the build
        captured_stderr = io.StringIO()
        old_stderr = sys.stderr
        try:
            sys.stderr = captured_stderr
            terminal.get_print_test_lines()  # Clear any previous output
            self._run_control('-o', self._output_dir, '-I')
        finally:
            sys.stderr = old_stderr

        # Verify there is a build failure
        self.assertEqual(self._builder.fail, 1)

        # Check stderr has exactly the expected error
        self.assertEqual(captured_stderr.getvalue(),
                         'test_error_msg.c:123: error: test failure\n')

        # In IDE mode, there should be no stdout output at all
        self.assertEqual(terminal.get_print_test_lines(), [])

        # Now run with -s to show summary - output should appear again
        terminal.get_print_test_lines()  # Clear
        self._run_control('-o', self._output_dir, '-s', clean_dir=False)
        lines = terminal.get_print_test_lines()
        self.assertEqual(len(lines), 2)
        self.assertIn('Summary of', lines[0].text)
        self.assertIn('board4', lines[1].text)

    def test_branch_ide(self):
        """Test building a branch with IDE mode and summary

        This tests _print_ide_output() which outputs errors to stderr during
        the summary phase for branch builds.
        """
        # Set up error for commit 1 on sandbox board
        error_msg = 'branch_error.c:456: error: branch failure\n'
        self._error['board4', 1] = error_msg

        # Build branch normally first (writes results to disk)
        terminal.get_print_test_lines()
        self._run_control('-b', TEST_BRANCH, '-o', self._output_dir)
        self.assertEqual(self._builder.fail, 1)

        # Run summary with IDE mode - errors go to stderr via _print_ide_output
        captured_stderr = io.StringIO()
        old_stderr = sys.stderr
        try:
            sys.stderr = captured_stderr
            terminal.get_print_test_lines()
            self._run_control('-b', TEST_BRANCH, '-o', self._output_dir, '-sI',
                             clean_dir=False)
        finally:
            sys.stderr = old_stderr

        # Check stderr has the error from _print_ide_output
        self.assertEqual(captured_stderr.getvalue(),
                         'branch_error.c:456: error: branch failure\n')

    def test_branch_summary(self): # pylint: disable=too-many-statements
        """Test building a branch and then showing a summary"""
        self._run_control('-b', TEST_BRANCH, '-o', self._output_dir)
        self.assertEqual(self._builder.count, self._total_builds)
        self.assertEqual(self._builder.fail, 0)

        # Now run with -s to show summary
        self._make_calls = 0
        self._run_control('-b', TEST_BRANCH, '-s', '-o', self._output_dir,
                         clean_dir=False)
        # Summary should not trigger any builds
        self.assertEqual(self._make_calls, 0)
        lines = terminal.get_print_test_lines()
        self.assertIn('(no errors to report)', lines[-1].text)

        # Now run with -S to show sizes as well
        self._make_calls = 0
        self._run_control('-b', TEST_BRANCH, '-sS', '-o', self._output_dir,
                         clean_dir=False)
        self.assertEqual(self._make_calls, 0)
        lines = terminal.get_print_test_lines()
        # Check that size information is displayed (arch names with size deltas)
        text = '\n'.join(line.text for line in lines)
        self.assertIn('arm', text)
        self.assertIn('sandbox', text)
        self.assertIn('(no errors to report)', lines[-1].text)

        # Now run with -B to show bloat (function size changes)
        self._make_calls = 0
        self._run_control('-b', TEST_BRANCH, '-sSB', '-o', self._output_dir,
                         clean_dir=False)
        self.assertEqual(self._make_calls, 0)
        lines = terminal.get_print_test_lines()
        text = '\n'.join(line.text for line in lines)
        # Check function names appear in the bloat output
        self.assertIn('main', text)
        self.assertIn('board_init', text)
        self.assertIn('(no errors to report)', lines[-1].text)

        # Now run with -K to show config changes
        # First, create config files in the output directory to simulate
        # varying configs between commits. Use the builder to get correct paths.
        for commit_num in range(self._commits):
            for brd in BOARDS:
                target = brd[6]  # target name is 7th element
                board_dir = self._builder.get_build_dir(commit_num, target)
                cfg_fname = os.path.join(board_dir, 'u-boot.cfg')
                cfg_content = f'#define CONFIG_VALUE {commit_num + 100}\n'
                if commit_num == 0:
                    # Add a config that will be removed in later commits
                    cfg_content += '#define CONFIG_OLD_OPTION 1\n'
                if commit_num > 0:
                    cfg_content += '#define CONFIG_NEW_OPTION 1\n'
                tools.write_file(cfg_fname, cfg_content.encode('utf-8'))

        self._make_calls = 0
        self._run_control('-b', TEST_BRANCH, '-sK', '-o', self._output_dir,
                         clean_dir=False)
        self.assertEqual(self._make_calls, 0)
        lines = terminal.get_print_test_lines()
        text = '\n'.join(line.text for line in lines)
        # Check config options appear in the output - both additions and
        # value changes should be detected
        self.assertIn('CONFIG_NEW_OPTION', text)  # Addition
        self.assertIn('CONFIG_VALUE', text)  # Value change
        self.assertIn('(no errors to report)', lines[-1].text)

        # Now run with -U to show environment changes
        # Create uboot.env files with varying content between commits
        for commit_num in range(self._commits):
            for brd in BOARDS:
                target = brd[6]  # target name is 7th element
                board_dir = self._builder.get_build_dir(commit_num, target)
                env_fname = os.path.join(board_dir, 'uboot.env')
                # Environment uses null-terminated strings
                env_content = f'bootdelay={commit_num + 1}\x00'
                if commit_num == 0:
                    # Add a variable that will be removed in later commits
                    env_content += 'oldvar=removed\x00'
                if commit_num > 0:
                    env_content += 'newvar=value\x00'
                tools.write_file(env_fname, env_content.encode('utf-8'))

        self._make_calls = 0
        self._run_control('-b', TEST_BRANCH, '-sU', '-o', self._output_dir,
                         clean_dir=False)
        self.assertEqual(self._make_calls, 0)
        lines = terminal.get_print_test_lines()
        text = '\n'.join(line.text for line in lines)
        # Check environment variables appear in the output
        self.assertIn('bootdelay', text)
        self.assertIn('(no errors to report)', lines[-1].text)

    def test_warnings_as_errors(self):
        """Test the -E flag adds -Werror to make arguments"""
        self._captured_make_args = []
        self._run_control('-o', self._output_dir, '-E')

        # Check that at least one build had -Werror flags
        found_werror = False
        for args in self._captured_make_args:
            args_str = ' '.join(args)
            if 'KCFLAGS=-Werror' in args_str:
                found_werror = True
                self.assertIn('HOSTCFLAGS=-Werror', args_str)
                break
        self.assertTrue(found_werror, 'KCFLAGS=-Werror not found in make args')

    def test_count(self):
        """Test building a specific number of commitst"""
        self._run_control('-b', TEST_BRANCH, '-c2', '-o', self._output_dir)
        self.assertEqual(self._builder.count, 2 * len(BOARDS))
        self.assertEqual(self._builder.fail, 0)
        # Each board has a config, and then one make per commit
        self.assertEqual(self._make_calls, len(BOARDS) * (1 + 2))

    def test_incremental(self):
        """Test building a branch twice - the second time should do nothing"""
        self._run_control('-b', TEST_BRANCH, '-o', self._output_dir)

        # Each board has a mrproper, config, and then one make per commit
        self.assertEqual(self._make_calls, len(BOARDS) * (self._commits + 1))
        self._make_calls = 0
        self._run_control('-b', TEST_BRANCH, '-o', self._output_dir,
                          clean_dir=False)
        self.assertEqual(self._make_calls, 0)
        self.assertEqual(self._builder.count, self._total_builds)
        self.assertEqual(self._builder.fail, 0)

    def test_force_build(self):
        """The -f flag should force a rebuild"""
        self._run_control('-b', TEST_BRANCH, '-o', self._output_dir)
        self._make_calls = 0
        self._run_control('-b', TEST_BRANCH, '-f', '-o', self._output_dir,
                          clean_dir=False)
        # Each board has a config and one make per commit
        self.assertEqual(self._make_calls, len(BOARDS) * (self._commits + 1))

    def test_force_reconfigure(self):
        """The -f flag should force a rebuild"""
        self._run_control('-b', TEST_BRANCH, '-C', '-o', self._output_dir)
        # Each commit has a config and make
        self.assertEqual(self._make_calls, len(BOARDS) * self._commits * 2)

    def test_mrproper(self):
        """The -f flag should force a rebuild"""
        self._run_control('-b', TEST_BRANCH, '-m', '-o', self._output_dir)
        # Each board has a mkproper, config and then one make per commit
        self.assertEqual(self._make_calls, len(BOARDS) * (self._commits + 2))

    def test_errors(self):
        """Test handling of build errors"""
        self._error['board2', 1] = 'fred\n'
        self._run_control('-b', TEST_BRANCH, '-o', self._output_dir)
        self.assertEqual(self._builder.count, self._total_builds)
        self.assertEqual(self._builder.fail, 1)

        # Remove the error. This should have no effect since the commit will
        # not be rebuilt
        del self._error['board2', 1]
        self._make_calls = 0
        self._run_control('-b', TEST_BRANCH, '-o', self._output_dir,
                          clean_dir=False)
        self.assertEqual(self._builder.count, self._total_builds)
        self.assertEqual(self._make_calls, 0)
        self.assertEqual(self._builder.fail, 1)

        # Now use the -F flag to force rebuild of the bad commit
        self._run_control('-b', TEST_BRANCH, '-o', self._output_dir, '-F',
                          clean_dir=False)
        self.assertEqual(self._builder.count, self._total_builds)
        self.assertEqual(self._builder.fail, 0)
        self.assertEqual(self._make_calls, 2)

    def test_branch_with_slash(self):
        """Test building a branch with a '/' in the name"""
        self._test_branch = '/__dev/__testbranch'
        self._run_control('-b', self._test_branch, '-o', self._output_dir,
                         clean_dir=False)
        self.assertEqual(self._builder.count, self._total_builds)
        self.assertEqual(self._builder.fail, 0)

    def test_environment(self):
        """Test that the done and environment files are written to out-env"""
        self._run_control('-o', self._output_dir)
        board0_dir = os.path.join(self._output_dir, 'current', 'board0')
        self.assertTrue(os.path.exists(os.path.join(board0_dir, 'done')))
        self.assertTrue(os.path.exists(os.path.join(board0_dir, 'out-env')))

    def test_environment_unicode(self):
        """Test there are no unicode errors when the env has non-ASCII chars"""
        try:
            varname = b'buildman_test_var'
            os.environb[varname] = b'strange\x80chars'
            self.assertEqual(0, self._run_control('-o', self._output_dir))
            board0_dir = os.path.join(self._output_dir, 'current', 'board0')
            self.assertTrue(os.path.exists(os.path.join(board0_dir, 'done')))
            self.assertTrue(os.path.exists(os.path.join(board0_dir, 'out-env')))
        finally:
            del os.environb[varname]

    def test_work_in_output(self):
        """Test the -w option which should write directly to the output dir"""
        board_list = boards.Boards()
        board_list.add_board(board.Board(*BOARDS[0]))
        self._run_control('-o', self._output_dir, '-w', clean_dir=False,
                         brds=board_list)
        self.assertTrue(
            os.path.exists(os.path.join(self._output_dir, 'u-boot')))
        self.assertTrue(
            os.path.exists(os.path.join(self._output_dir, 'done')))
        self.assertTrue(
            os.path.exists(os.path.join(self._output_dir, 'out-env')))

    def test_work_in_output_fail(self):
        """Test the -w option failures"""
        with self.assertRaises(SystemExit) as e:
            self._run_control('-o', self._output_dir, '-w', clean_dir=False)
        self.assertIn("single board", str(e.exception))
        self.assertFalse(
            os.path.exists(os.path.join(self._output_dir, 'u-boot')))

        board_list = boards.Boards()
        board_list.add_board(board.Board(*BOARDS[0]))
        with self.assertRaises(SystemExit) as e:
            self._run_control('-b', self._test_branch, '-o', self._output_dir,
                             '-w', clean_dir=False, brds=board_list)
        self.assertIn("single commit", str(e.exception))

        board_list = boards.Boards()
        board_list.add_board(board.Board(*BOARDS[0]))
        with self.assertRaises(SystemExit) as e:
            self._run_control('-w', clean_dir=False)
        self.assertIn("specify -o", str(e.exception))

    def test_thread_exceptions(self):
        """Test that exceptions in threads are reported"""
        with terminal.capture() as (stdout, _stderr):
            self.assertEqual(102, self._run_control('-o', self._output_dir,
                                                   test_thread_exceptions=True))
        self.assertIn(
            'Thread exception (use -T0 to run without threads): test exception',
            stdout.getvalue())

    def test_blobs(self):
        """Test handling of missing blobs"""
        self._missing = True

        board0_dir = os.path.join(self._output_dir, 'current', 'board0')
        errfile = os.path.join(board0_dir, 'err')

        # We expect failure when there are missing blobs
        result = self._run_control('board0', '-o', self._output_dir)
        self.assertEqual(100, result)
        self.assertTrue(os.path.exists(os.path.join(board0_dir, 'done')))
        self.assertTrue(os.path.exists(errfile))
        self.assertIn(b"Filename 'fsp.bin' not found in input path",
                      tools.read_file(errfile))

    def test_blobs_allow_missing(self):
        """Allow missing blobs - still failure but a different exit code"""
        self._missing = True
        result = self._run_control('board0', '-o', self._output_dir, '-M',
                                  clean_dir=True)
        self.assertEqual(101, result)
        board0_dir = os.path.join(self._output_dir, 'current', 'board0')
        errfile = os.path.join(board0_dir, 'err')
        self.assertTrue(os.path.exists(errfile))
        self.assertIn(b'Some images are invalid', tools.read_file(errfile))

    def test_blobs_warning(self):
        """Allow missing blobs and ignore warnings"""
        self._missing = True
        result = self._run_control('board0', '-o', self._output_dir, '-MW')
        self.assertEqual(0, result)
        board0_dir = os.path.join(self._output_dir, 'current', 'board0')
        errfile = os.path.join(board0_dir, 'err')
        self.assertIn(b'Some images are invalid', tools.read_file(errfile))

    def test_blob_settings(self):
        """Test with no settings"""
        self.assertEqual(False,
                         control.get_allow_missing(False, False, 1, False))
        self.assertEqual(True,
                         control.get_allow_missing(True, False, 1, False))
        self.assertEqual(False,
                         control.get_allow_missing(True, True, 1, False))

    def test_blob_settings_always(self):
        """Test the 'always' policy"""
        bsettings.set_item('global', 'allow-missing', 'always')
        self.assertEqual(True,
                         control.get_allow_missing(False, False, 1, False))
        self.assertEqual(False,
                         control.get_allow_missing(False, True, 1, False))

    def test_blob_settings_branch(self):
        """Test the 'branch' policy"""
        bsettings.set_item('global', 'allow-missing', 'branch')
        self.assertEqual(False,
                         control.get_allow_missing(False, False, 1, False))
        self.assertEqual(True,
                         control.get_allow_missing(False, False, 1, True))
        self.assertEqual(False,
                         control.get_allow_missing(False, True, 1, True))

    def test_blob_settings_multiple(self):
        """Test the 'multiple' policy"""
        bsettings.set_item('global', 'allow-missing', 'multiple')
        self.assertEqual(False,
                         control.get_allow_missing(False, False, 1, False))
        self.assertEqual(True,
                         control.get_allow_missing(False, False, 2, False))
        self.assertEqual(False,
                         control.get_allow_missing(False, True, 2, False))

    def test_blob_settings_branch_multiple(self):
        """Test the 'branch multiple' policy"""
        bsettings.set_item('global', 'allow-missing', 'branch multiple')
        self.assertEqual(False,
                         control.get_allow_missing(False, False, 1, False))
        self.assertEqual(True,
                         control.get_allow_missing(False, False, 1, True))
        self.assertEqual(True,
                         control.get_allow_missing(False, False, 2, False))
        self.assertEqual(True,
                         control.get_allow_missing(False, False, 2, True))
        self.assertEqual(False,
                         control.get_allow_missing(False, True, 2, True))

    def check_command(self, brd, *extra_args):
        """Run a command with the extra arguments and return the commands used

        Args:
            brd (str): Board name to build
            extra_args (list of str): List of extra arguments

        Returns:
            tuple:
                list of str: Lines returned in the out-cmd file
                bytes: Contents of .config file
        """
        self._run_control('-o', self._output_dir, brd, *extra_args)
        board_dir = os.path.join(self._output_dir, 'current', brd)
        self.assertTrue(os.path.exists(os.path.join(board_dir, 'done')))
        cmd_fname = os.path.join(board_dir, 'out-cmd')
        self.assertTrue(os.path.exists(cmd_fname))
        data = tools.read_file(cmd_fname)

        config_fname = os.path.join(board_dir, '.config')
        self.assertTrue(os.path.exists(config_fname))
        cfg_data = tools.read_file(config_fname)

        return data.splitlines(), cfg_data

    def test_cmd_file(self):
        """Test that the -cmd-out file is produced"""
        lines = self.check_command('board0')[0]
        self.assertEqual(2, len(lines))
        self.assertRegex(lines[0], b'make O=/.*board0_defconfig')
        self.assertRegex(lines[0], b'make O=/.*-s.*')

    def test_no_lto(self):
        """Test that the --no-lto flag works"""
        lines = self.check_command('board0', '-L')[0]
        self.assertIn(b'NO_LTO=1', lines[0])

    def test_fragments(self):
        """Test passing of configuration fragments to the make command"""
        # Single fragment passed as argument
        extra_args = ['board0', '--fragments', 'f1.config']
        lines, _cfg_data = self.check_command(*extra_args)
        self.assertRegex(lines[0].decode('utf-8'),
                         r'make O=/.*board0_defconfig\s+f1\.config',
                         'Test single fragment')
        # Multiple fragments passed as comma-separated list
        extra_args = ['board0', '--fragments', 'f1.config,f2.config']
        lines, _cfg_data = self.check_command(*extra_args)
        self.assertRegex(
            lines[0].decode('utf-8'),
            r'make O=/.*board0_defconfig\s+f1\.config\s+f2\.config',
            'Test multiple fragments')

    def test_reproducible(self):
        """Test that the -r flag works"""
        # Use single board to avoid parallel merge_config.sh race conditions
        lines, cfg_data = self.check_command('board0', '-r')
        self.assertIn(b'SOURCE_DATE_EPOCH=0', lines[0])

        # We should see CONFIG_LOCALVERSION_AUTO unset (uses real Kconfig)
        self.assertIn(b'# CONFIG_LOCALVERSION_AUTO is not set', cfg_data)

        with terminal.capture() as (stdout, _stderr):
            lines, cfg_data = self.check_command('board0', '-r', '-a',
                                                 'LOCALVERSION')
        self.assertIn(b'SOURCE_DATE_EPOCH=0', lines[0])

        # When user explicitly sets LOCALVERSION, the warning appears
        self.assertIn('Not dropping LOCALVERSION_AUTO', stdout.getvalue())

        # LOCALVERSION should be present in .config (it's a string config)
        self.assertIn(b'CONFIG_LOCALVERSION=', cfg_data)

    def test_scan_defconfigs(self):
        """Test scanning the defconfigs to obtain all the boards"""
        src = self._git_dir

        # Scan the test directory which contains a Kconfig and some *_defconfig
        # files
        params, warnings = self._boards.scan_defconfigs(src, src)

        # We should get two boards
        self.assertEqual(2, len(params))
        self.assertFalse(warnings)
        first = 0 if params[0]['target'] == 'board0' else 1
        board0 = params[first]
        board2 = params[1 - first]

        self.assertEqual('arm', board0['arch'])
        self.assertEqual('armv7', board0['cpu'])
        self.assertEqual('-', board0['soc'])
        self.assertEqual('Tester', board0['vendor'])
        self.assertEqual('ARM Board 0', board0['board'])
        self.assertEqual('config0', board0['config'])
        self.assertEqual('board0', board0['target'])

        self.assertEqual('powerpc', board2['arch'])
        self.assertEqual('ppc', board2['cpu'])
        self.assertEqual('mpc85xx', board2['soc'])
        self.assertEqual('Tester', board2['vendor'])
        self.assertEqual('PowerPC board 1', board2['board'])
        self.assertEqual('config2', board2['config'])
        self.assertEqual('board2', board2['target'])

    def test_output_is_new(self):
        """Test detecting new changes to Kconfig"""
        base = self._base_dir
        src = self._git_dir
        config_dir = os.path.join(src, 'configs')
        delay = 0.02

        # Create a boards.cfg file
        boards_cfg = os.path.join(base, 'boards.cfg')
        content = b'''#
# List of boards
#   Automatically generated by buildman/boards.py: don't edit
#
# Status, Arch, CPU, SoC, Vendor, Board, Target, Config, Maintainers

Active  aarch64     armv8 - armltd corstone1000 board0
Active  aarch64     armv8 - armltd total_compute board2
'''
        # Check missing file
        self.assertFalse(boards.output_is_new(boards_cfg, config_dir, src))

        # Check that the board.cfg file is newer
        time.sleep(delay)
        tools.write_file(boards_cfg, content)
        self.assertTrue(boards.output_is_new(boards_cfg, config_dir, src))

        # Touch the Kconfig files after a show delay to avoid a race
        time.sleep(delay)
        Path(os.path.join(src, 'Kconfig')).touch()
        self.assertFalse(boards.output_is_new(boards_cfg, config_dir, src))
        Path(boards_cfg).touch()
        self.assertTrue(boards.output_is_new(boards_cfg, config_dir, src))

        # Touch a different Kconfig file
        time.sleep(delay)
        Path(os.path.join(src, 'Kconfig.something')).touch()
        self.assertFalse(boards.output_is_new(boards_cfg, config_dir, src))
        Path(boards_cfg).touch()
        self.assertTrue(boards.output_is_new(boards_cfg, config_dir, src))

        # Touch a MAINTAINERS file
        time.sleep(delay)
        Path(os.path.join(src, 'MAINTAINERS')).touch()
        self.assertFalse(boards.output_is_new(boards_cfg, config_dir, src))

        Path(boards_cfg).touch()
        self.assertTrue(boards.output_is_new(boards_cfg, config_dir, src))

        # Touch a defconfig file
        time.sleep(delay)
        Path(os.path.join(config_dir, 'board0_defconfig')).touch()
        self.assertFalse(boards.output_is_new(boards_cfg, config_dir, src))
        Path(boards_cfg).touch()
        self.assertTrue(boards.output_is_new(boards_cfg, config_dir, src))

        # Remove a board and check that the board.cfg file is now older
        Path(os.path.join(config_dir, 'board0_defconfig')).unlink()
        self.assertFalse(boards.output_is_new(boards_cfg, config_dir, src))

    def test_maintainers(self): # pylint: disable=too-many-statements
        """Test detecting boards without a MAINTAINERS entry"""
        src = self._git_dir
        main = os.path.join(src, 'boards', 'board0', 'MAINTAINERS')
        other = os.path.join(src, 'boards', 'board2', 'MAINTAINERS')
        kc_file = os.path.join(src, 'Kconfig')
        config_dir = os.path.join(src, 'configs')
        params_list, warnings = self._boards.build_board_list(config_dir, src)

        # There should be two boards no warnings
        self.assertEqual(2, len(params_list))
        self.assertFalse(warnings)

        # Set an invalid status line in the file
        orig_data = tools.read_file(main, binary=False)
        lines = ['S:      Other\n' if line.startswith('S:') else line
                  for line in orig_data.splitlines(keepends=True)]
        tools.write_file(main, ''.join(lines), binary=False)
        params_list, warnings = self._boards.build_board_list(config_dir, src)
        self.assertEqual(2, len(params_list))
        params = params_list[0]
        if params['target'] == 'board2':
            params = params_list[1]
        self.assertEqual('-', params['status'])
        self.assertEqual(["WARNING: Other: unknown status for 'board0'"],
                          warnings)

        # Remove the status line (S:) from a file
        lines = [line for line in orig_data.splitlines(keepends=True)
                 if not line.startswith('S:')]
        tools.write_file(main, ''.join(lines), binary=False)
        params_list, warnings = self._boards.build_board_list(config_dir, src)
        self.assertEqual(2, len(params_list))
        self.assertEqual(["WARNING: -: unknown status for 'board0'"], warnings)

        # Remove the configs/ line (F:) from a file - this is the last line
        data = ''.join(orig_data.splitlines(keepends=True)[:-1])
        tools.write_file(main, data, binary=False)
        params_list, warnings = self._boards.build_board_list(config_dir, src)
        self.assertEqual(2, len(params_list))
        self.assertEqual(["WARNING: no maintainers for 'board0'"], warnings)

        # Mark a board as orphaned - this should give a warning
        lines = ['S: Orphaned' if line.startswith('S') else line
                 for line in orig_data.splitlines(keepends=True)]
        tools.write_file(main, ''.join(lines), binary=False)
        params_list, warnings = self._boards.build_board_list(config_dir, src)
        self.assertEqual(2, len(params_list))
        self.assertEqual(["WARNING: no maintainers for 'board0'"], warnings)

        # Change the maintainer to '-' - this should give a warning
        lines = ['M: -' if line.startswith('M') else line
                 for line in orig_data.splitlines(keepends=True)]
        tools.write_file(main, ''.join(lines), binary=False)
        params_list, warnings = self._boards.build_board_list(config_dir, src)
        self.assertEqual(2, len(params_list))
        self.assertEqual(["WARNING: -: unknown status for 'board0'"], warnings)

        # Remove the maintainer line (M:) from a file
        lines = [line for line in orig_data.splitlines(keepends=True)
                 if not line.startswith('M:')]
        tools.write_file(main, ''.join(lines), binary=False)
        params_list, warnings = self._boards.build_board_list(config_dir, src)
        self.assertEqual(2, len(params_list))
        self.assertEqual(["WARNING: no maintainers for 'board0'"], warnings)

        # Move the contents of the second file into this one, removing the
        # second file, to check multiple records in a single file.
        both_data = orig_data + tools.read_file(other, binary=False)
        tools.write_file(main, both_data, binary=False)
        os.remove(other)
        params_list, warnings = self._boards.build_board_list(config_dir, src)
        self.assertEqual(2, len(params_list))
        self.assertFalse(warnings)

        # Add another record, this should be ignored with a warning
        extra = '\n\nAnother\nM: Fred\nF: configs/board9_defconfig\nS: other\n'
        tools.write_file(main, both_data + extra, binary=False)
        params_list, warnings = self._boards.build_board_list(config_dir, src)
        self.assertEqual(2, len(params_list))
        self.assertFalse(warnings)

        # Add another TARGET to the Kconfig
        tools.write_file(main, both_data, binary=False)
        orig_kc_data = tools.read_file(kc_file)
        extra = b'''
if TARGET_BOARD2
config TARGET_OTHER
\tbool "other"
\tdefault y
endif
'''
        tools.write_file(kc_file, orig_kc_data + extra)
        params_list, warnings = self._boards.build_board_list(config_dir, src,
                                                              warn_targets=True)
        self.assertEqual(2, len(params_list))
        self.assertEqual(
            ['WARNING: board2_defconfig: Duplicate TARGET_xxx: '
             'board2 and other'], warnings)

        # Remove the TARGET_BOARD0 Kconfig option
        lines = [b'' if line == b'config TARGET_BOARD2\n' else line
                  for line in orig_kc_data.splitlines(keepends=True)]
        tools.write_file(kc_file, b''.join(lines))
        params_list, warnings = self._boards.build_board_list(config_dir, src,
                                                              warn_targets=True)
        self.assertEqual(2, len(params_list))
        self.assertEqual(
            ['WARNING: board2_defconfig: No TARGET_BOARD2 enabled'],
             warnings)
        tools.write_file(kc_file, orig_kc_data)

        # Replace the last F: line of board 2 with an N: line
        data = ''.join(both_data.splitlines(keepends=True)[:-1])
        tools.write_file(main, data + 'N: oa.*2\n', binary=False)
        params_list, warnings = self._boards.build_board_list(config_dir, src)
        self.assertEqual(2, len(params_list))
        self.assertFalse(warnings)

    def test_regen_boards(self):
        """Test that we can regenerate the boards.cfg file"""
        outfile = os.path.join(self._output_dir, 'test-boards.cfg')
        if os.path.exists(outfile):
            os.remove(outfile)
        with terminal.capture() as (_stdout, _stderr):
            self._run_control('-R', outfile, brds=None, get_builder=False)
        self.assertTrue(os.path.exists(outfile))

    def test_print_prefix(self):
        """Test that we can print the toolchain prefix"""
        with terminal.capture() as (stdout, stderr):
            self._run_control('-A', 'board0')
        self.assertEqual('arm-\n', stdout.getvalue())
        self.assertEqual('', stderr.getvalue())

    def test_exclude_one(self):
        """Test excluding a single board from an arch"""
        self._run_control('arm', '-x', 'board1', '-o', self._output_dir)
        self.assertEqual(['board0'],
                         [b.target for b in self._boards.get_selected()])

    def test_exclude_arch(self):
        """Test excluding an arch"""
        self._run_control('-x', 'arm', '-o', self._output_dir)
        self.assertEqual(['board2', 'board4'],
                         [b.target for b in self._boards.get_selected()])

    def test_exclude_comma(self):
        """Test excluding a comma-separated list of things"""
        self._run_control('-x', 'arm,powerpc', '-o', self._output_dir)
        self.assertEqual(['board4'],
                         [b.target for b in self._boards.get_selected()])

    def test_exclude_list(self):
        """Test excluding a list of things"""
        self._run_control('-x', 'board2', '-x', 'board4', '-o',
                          self._output_dir)
        self.assertEqual(['board0', 'board1'],
                         [b.target for b in self._boards.get_selected()])

    def test_single_boards(self):
        """Test building single boards"""
        self._run_control('--boards', 'board1', '-o', self._output_dir)
        self.assertEqual(1, self._builder.count)

        self._run_control('--boards', 'board1', '--boards', 'board2',
                         '-o', self._output_dir)
        self.assertEqual(2, self._builder.count)

        self._run_control('--boards', 'board1,board2', '--boards', 'board4',
                         '-o', self._output_dir)
        self.assertEqual(3, self._builder.count)

    def test_print_arch(self):
        """Test that we can print the board architecture"""
        with terminal.capture() as (stdout, stderr):
            self._run_control('--print-arch', 'board0')
        self.assertEqual('arm\n', stdout.getvalue())
        self.assertEqual('', stderr.getvalue())

    def test_kconfig_scanner(self):
        """Test using the kconfig scanner to determine important values

        Note that there is already a test_scan_defconfigs() which checks the
        higher-level scan_defconfigs() function. This test checks just the
        scanner itself
        """
        src = self._git_dir
        scanner = boards.KconfigScanner(src)

        # First do a simple sanity check
        norm = os.path.join(src, 'board0_defconfig')
        tools.write_file(norm, 'CONFIG_TARGET_BOARD0=y', False)
        res = scanner.scan(norm, True)
        self.assertEqual(({
            'arch': 'arm',
            'cpu': 'armv7',
            'soc': '-',
            'vendor': 'Tester',
            'board': 'ARM Board 0',
            'config': 'config0',
            'target': 'board0'}, []), res)

        # Check that the SoC cannot be changed and the filename does not affect
        # the resulting board
        tools.write_file(norm, '''CONFIG_TARGET_BOARD2=y
CONFIG_SOC="fred"
''', False)
        res = scanner.scan(norm, True)
        self.assertEqual(({
            'arch': 'powerpc',
            'cpu': 'ppc',
            'soc': 'mpc85xx',
            'vendor': 'Tester',
            'board': 'PowerPC board 1',
            'config': 'config2',
            'target': 'board0'}, []), res)

        # Check handling of missing information
        tools.write_file(norm, '', False)
        res = scanner.scan(norm, True)
        self.assertEqual(({
            'arch': '-',
            'cpu': '-',
            'soc': '-',
            'vendor': '-',
            'board': '-',
            'config': '-',
            'target': 'board0'},
            ['WARNING: board0_defconfig: No TARGET_BOARD0 enabled']), res)

        # check handling of #include files; see _handle_command_cpp()
        inc = os.path.join(src, 'common')
        tools.write_file(inc, b'CONFIG_TARGET_BOARD0=y\n')
        tools.write_file(norm, f'#include <{inc}>', False)
        res = scanner.scan(norm, True)
        self.assertEqual(({
            'arch': 'arm',
            'cpu': 'armv7',
            'soc': '-',
            'vendor': 'Tester',
            'board': 'ARM Board 0',
            'config': 'config0',
            'target': 'board0'}, []), res)

    def test_target(self):
        """Test that the --target flag works"""
        lines = self.check_command('board0', '--target', 'u-boot.dtb')[0]

        # It should not affect the defconfig line
        self.assertNotIn(b'u-boot.dtb', lines[0])

        # It should appear at the end of the build line
        self.assertEqual(b'u-boot.dtb', lines[1].split()[-1])

    def test_extended(self):
        """Test parsing of extended (.buildman) files"""
        data = '''
name: acpi_boards
desc: Build RISC-V QEMU builds with ACPI
fragment: acpi
targets:
  qemu_riscv*

name: am62x_dfu
desc: Build Android variant of 'k3' boards, with DFU
fragment: am62x_r5_usbdfu
fragment: am62x_a53_android
targets:
  CONFIG_SYS_SOC="k3"
'''
        fname = os.path.join(self._base_dir, 'try.buildman')
        tools.write_file(fname, data.encode('utf-8'))
        result = boards.ExtendedParser.parse_file(fname)
        self.maxDiff = None  # pylint: disable=C0103
        self.assertEqual([
            Extended(name='acpi_boards',
                     desc='Build RISC-V QEMU builds with ACPI',
                     fragments=['acpi'],
                     targets=[
                         ['regex', 'qemu_riscv*']]),
            Extended(name='am62x_dfu',
                     desc="Build Android variant of 'k3' boards, with DFU",
                     fragments=['am62x_r5_usbdfu', 'am62x_a53_android'],
                     targets=[
                         ['CONFIG_SYS_SOC', '"k3"']]
                     )], result)

    def test_extended_bad_indent(self):
        """Test unexpected indentation"""
        with self.assertRaises(ValueError) as exc:
            boards.ExtendedParser.parse_data('mary', ' name: fred')
        self.assertEqual('mary:1: Unexpected indent',
                         str(exc.exception))

    def test_extended_invalid_config(self):
        """Test a bad CONFIG_xxx= line"""
        with self.assertRaises(ValueError) as exc:
            boards.ExtendedParser.parse_data('anna', '''
name: joan
targets:
  CONFIG_SOMETHING=val="
''')
        self.assertEqual('anna:4: Invalid CONFIG syntax',
                         str(exc.exception))

    def test_extended_invalid_target(self):
        """Test a bad target regex"""
        with self.assertRaises(ValueError) as exc:
            boards.ExtendedParser.parse_data('john', '''
name: fred
targets:
  qemu* *riscv
''')
        self.assertEqual('john:4: Invalid target regex',
                         str(exc.exception))

    def test_extended_invalid_tag(self):
        """Test a bad tag"""
        with self.assertRaises(ValueError) as exc:
            boards.ExtendedParser.parse_data('hannah', '''
name: frank :was here
''')
        self.assertEqual('hannah:2: Invalid tag',
                         str(exc.exception))

    def test_extended_unknown_tag(self):
        """Test a unknown tag"""
        with self.assertRaises(ValueError) as exc:
            boards.ExtendedParser.parse_data('susan', '''
name: allan
something: me
''')
        self.assertEqual("susan:3: Unknown tag 'something'",
                         str(exc.exception))

    def test_extended_bad_name(self):
        """Test a name with an invalid char"""
        with self.assertRaises(ValueError) as exc:
            boards.ExtendedParser.parse_data('bert', 'name: katie was here')
        self.assertEqual('bert:1: Invalid name',
                         str(exc.exception))

    def test_kconfig_change(self):
        """Test that Kconfig change detection triggers reconfig"""
        # Track calls to kconfig_changed_since
        call_count = [0]
        config_exists = [False]

        def mock_kconfig_changed(fname, _srcdir='.', _target=None,
                                 _commit_upto=None):
            """Mock for kconfig_changed_since that checks if .config exists

            Args:
                fname (str): Path to the .config file

            Returns:
                bool: True if .config exists, False otherwise
            """
            call_count[0] += 1
            # Return True only if .config exists (i.e. not the first build)
            # The first build has no .config to compare against
            exists = os.path.exists(fname)
            if exists:
                config_exists[0] = True
            return exists

        # Run buildman with kconfig checking enabled. Use -P to give each
        # board its own output directory, preventing .config from leaking
        # between boards when a thread processes multiple boards sequentially.
        with mock.patch.object(builderthread, 'kconfig_changed_since',
                               mock_kconfig_changed):
            self._run_control('-b', TEST_BRANCH, '-c2', '-o', self._output_dir,
                             '-P')

        # Verify kconfig_changed_since was called
        self.assertGreater(call_count[0], 0)

        # Each board should have one reconfig (for the second commit)
        # First commit has no .config, so no reconfig
        # Second commit has .config from first commit, so reconfig triggered
        self.assertEqual(len(BOARDS), self._builder.kconfig_reconfig)

    def test_kconfig_change_disabled(self):
        """Test that -Z flag disables Kconfig change detection"""
        call_count = [0]

        def mock_kconfig_changed(_fname, _srcdir='.', _target=None,
                                 _commit_upto=None):
            """Mock for kconfig_changed_since that always returns True

            Returns:
                bool: Always True
            """
            call_count[0] += 1
            return True  # Would trigger reconfig if checking was enabled

        # Run buildman with kconfig checking disabled (-Z)
        with mock.patch.object(builderthread, 'kconfig_changed_since',
                               mock_kconfig_changed):
            self._run_control('-b', TEST_BRANCH, '-c2', '-o', self._output_dir,
                             '-Z')

        # Verify kconfig_changed_since was NOT called (feature disabled)
        self.assertEqual(0, call_count[0])

        # No reconfigs should be triggered
        self.assertEqual(0, self._builder.kconfig_reconfig)

    def test_adjust_cfg_no_imply(self):
        """Test that direct .config modification does not resolve imply

        Modifying .config directly with cfgutil.adjust_cfg_file() does not
        run Kconfig, so 'imply' dependencies are not resolved. This test
        demonstrates the limitation that merge_config.sh fixes.
        """
        # Create a temporary .config file
        cfg_file = os.path.join(self._output_dir, '.config')
        tools.write_file(cfg_file, b'# Empty config\n')

        # Use cfgutil to directly modify .config (old approach)
        adjust_cfg = {'BUILDMAN_TEST_A': 'BUILDMAN_TEST_A'}
        cfgutil.adjust_cfg_file(cfg_file, adjust_cfg)

        # Read the result
        cfg_data = tools.read_file(cfg_file)

        # BUILDMAN_TEST_A should be enabled
        self.assertIn(b'CONFIG_BUILDMAN_TEST_A=y', cfg_data)

        # But BUILDMAN_TEST_B should NOT be enabled - imply is not resolved
        # because we didn't run Kconfig
        self.assertNotIn(b'CONFIG_BUILDMAN_TEST_B=y', cfg_data,
                         'Direct .config modification should not resolve imply')

    def test_adjust_cfg_imply(self):
        """Test that merge_config.sh resolves Kconfig 'imply' dependencies

        The --adjust-cfg option uses merge_config.sh to apply config changes,
        which runs 'make alldefconfig' to resolve all Kconfig dependencies
        including 'imply'. CONFIG_BUILDMAN_TEST_A implies
        CONFIG_BUILDMAN_TEST_B, so enabling BUILDMAN_TEST_A should also enable
        BUILDMAN_TEST_B.
        """
        # Use single board to avoid parallel merge_config.sh race conditions
        # Enable UNIT_TEST since BUILDMAN_TEST_A depends on it
        _lines, cfg_data = self.check_command(
            'board0', '-a', 'UNIT_TEST,BUILDMAN_TEST_A')

        # Verify BUILDMAN_TEST_A was enabled
        self.assertIn(b'CONFIG_BUILDMAN_TEST_A=y', cfg_data)

        # merge_config.sh resolves imply dependencies, so enabling
        # BUILDMAN_TEST_A should also enable BUILDMAN_TEST_B
        self.assertIn(b'CONFIG_BUILDMAN_TEST_B=y', cfg_data,
                      '--adjust-cfg should resolve imply dependencies')