1# Copyright 2021 The Pigweed Authors 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); you may not 4# use this file except in compliance with the License. You may obtain a copy of 5# the License at 6# 7# https://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12# License for the specific language governing permissions and limitations under 13# the License. 14"""Components Tests.""" 15 16import pathlib 17import unittest 18import xml.etree.ElementTree 19from itertools import chain 20 21from pw_build_mcuxpresso.components import Project 22 23# pylint: disable=missing-function-docstring 24# pylint: disable=line-too-long 25 26 27class ParseDefinesTest(unittest.TestCase): 28 """parse_defines tests.""" 29 30 def test_parse_defines(self): 31 test_manifest_xml = ''' 32 <manifest> 33 <components> 34 <component id="test"> 35 <defines> 36 <define name="TEST_WITH_VALUE" value="1"/> 37 <define name="TEST_WITHOUT_VALUE"/> 38 </defines> 39 </component> 40 </components> 41 </manifest> 42 ''' 43 manifest = ( 44 xml.etree.ElementTree.fromstring(test_manifest_xml), 45 pathlib.Path.cwd() / "manifest.xml", 46 ) 47 project = Project([manifest], pathlib.Path.cwd(), ["*"], None) 48 defines = project.components["test"].defines 49 50 self.assertCountEqual( 51 defines, ['TEST_WITH_VALUE=1', 'TEST_WITHOUT_VALUE'] 52 ) 53 54 def test_no_defines(self): 55 test_manifest_xml = ''' 56 <manifest> 57 <components> 58 <component id="test"> 59 </component> 60 </components> 61 </manifest> 62 ''' 63 manifest = ( 64 xml.etree.ElementTree.fromstring(test_manifest_xml), 65 pathlib.Path.cwd() / "manifest.xml", 66 ) 67 project = Project([manifest], pathlib.Path.cwd(), ["*"], None) 68 defines = project.components["test"].defines 69 70 self.assertCountEqual(defines, []) 71 72 def test_device_cores(self): 73 test_manifest_xml = ''' 74 <manifest> 75 <components> 76 <component id="test"> 77 <defines> 78 <define name="TEST_WITH_CORE" device_cores="CORE0"/> 79 <define name="TEST_WITHOUT_CORE" device_cores="CORE1"/> 80 <define name="TEST_WITHOUT_CORES"/> 81 </defines> 82 </component> 83 </components> 84 </manifest> 85 ''' 86 manifest = ( 87 xml.etree.ElementTree.fromstring(test_manifest_xml), 88 pathlib.Path.cwd() / "manifest.xml", 89 ) 90 project = Project([manifest], pathlib.Path.cwd(), ["*"], None, "CORE0") 91 defines = project.components["test"].defines 92 93 self.assertCountEqual(defines, ['TEST_WITH_CORE', 'TEST_WITHOUT_CORES']) 94 95 96class ParseIncludePathsTest(unittest.TestCase): 97 """parse_include_paths tests.""" 98 99 def test_parse_include_paths(self): 100 test_manifest_xml = ''' 101 <manifest> 102 <components> 103 <component id="test"> 104 <include_paths> 105 <include_path relative_path="example" type="c_include"/> 106 <include_path relative_path="asm" type="asm_include"/> 107 </include_paths> 108 </component> 109 </components> 110 </manifest> 111 ''' 112 manifest = ( 113 xml.etree.ElementTree.fromstring(test_manifest_xml), 114 pathlib.Path.cwd() / "manifest.xml", 115 ) 116 project = Project([manifest], pathlib.Path.cwd(), ["*"], None) 117 include_paths = project.components["test"].include_dirs 118 119 self.assertCountEqual( 120 include_paths, [pathlib.Path("example"), pathlib.Path("asm")] 121 ) 122 123 def test_with_base_path(self): 124 test_manifest_xml = ''' 125 <manifest> 126 <components> 127 <component id="test" package_base_path="src"> 128 <include_paths> 129 <include_path relative_path="example" type="c_include"/> 130 <include_path relative_path="asm" type="asm_include"/> 131 </include_paths> 132 </component> 133 </components> 134 </manifest> 135 ''' 136 manifest = ( 137 xml.etree.ElementTree.fromstring(test_manifest_xml), 138 pathlib.Path.cwd() / "manifest.xml", 139 ) 140 project = Project([manifest], pathlib.Path.cwd(), ["*"], None) 141 include_paths = project.components["test"].include_dirs 142 143 self.assertCountEqual( 144 include_paths, 145 [pathlib.Path('src/example'), pathlib.Path('src/asm')], 146 ) 147 148 def test_unknown_type(self): 149 test_manifest_xml = ''' 150 <manifest> 151 <components> 152 <component id="test" package_base_path="src"> 153 <include_paths> 154 <include_path relative_path="rust" type="rust_include"/> 155 </include_paths> 156 </component> 157 </components> 158 </manifest> 159 ''' 160 manifest = ( 161 xml.etree.ElementTree.fromstring(test_manifest_xml), 162 pathlib.Path.cwd() / "manifest.xml", 163 ) 164 project = Project([manifest], pathlib.Path.cwd(), ["*"], None) 165 include_paths = project.components["test"].include_dirs 166 167 self.assertCountEqual(include_paths, []) 168 169 def test_no_include_paths(self): 170 test_manifest_xml = ''' 171 <manifest> 172 <components> 173 <component id="test"> 174 </component> 175 </components> 176 </manifest> 177 ''' 178 manifest = ( 179 xml.etree.ElementTree.fromstring(test_manifest_xml), 180 pathlib.Path.cwd() / "manifest.xml", 181 ) 182 project = Project([manifest], pathlib.Path.cwd(), ["*"], None) 183 include_paths = project.components["test"].include_dirs 184 185 self.assertCountEqual(include_paths, []) 186 187 def test_device_cores(self): 188 test_manifest_xml = ''' 189 <manifest> 190 <components> 191 <component id="test"> 192 <include_paths> 193 <include_path relative_path="with_core" type="c_include" 194 device_cores="CORE0"/> 195 <include_path relative_path="without_core" type="c_include" 196 device_cores="CORE1"/> 197 <include_path relative_path="without_cores" type="c_include"/> 198 </include_paths> 199 </component> 200 </components> 201 </manifest> 202 ''' 203 manifest = ( 204 xml.etree.ElementTree.fromstring(test_manifest_xml), 205 pathlib.Path.cwd() / "manifest.xml", 206 ) 207 project = Project([manifest], pathlib.Path.cwd(), ["*"], None, "CORE0") 208 include_paths = project.components["test"].include_dirs 209 210 self.assertCountEqual( 211 include_paths, 212 [pathlib.Path('with_core'), pathlib.Path('without_cores')], 213 ) 214 215 216class ParseHeadersTest(unittest.TestCase): 217 """parse_headers tests.""" 218 219 def test_parse_headers(self): 220 test_manifest_xml = ''' 221 <manifest> 222 <components> 223 <component id="test"> 224 <source relative_path="include" type="c_include"> 225 <files mask="test.h"/> 226 <files mask="test_types.h"/> 227 </source> 228 </component> 229 </components> 230 </manifest> 231 ''' 232 manifest = ( 233 xml.etree.ElementTree.fromstring(test_manifest_xml), 234 pathlib.Path.cwd() / "manifest.xml", 235 ) 236 project = Project([manifest], pathlib.Path.cwd(), ["*"], None) 237 headers = project.components["test"].headers 238 239 self.assertCountEqual( 240 headers, 241 [ 242 pathlib.Path('include/test.h'), 243 pathlib.Path('include/test_types.h'), 244 ], 245 ) 246 247 def test_with_base_path(self): 248 test_manifest_xml = ''' 249 <manifest> 250 <components> 251 <component id="test" package_base_path="src"> 252 <source relative_path="include" type="c_include"> 253 <files mask="test.h"/> 254 <files mask="test_types.h"/> 255 </source> 256 </component> 257 </components> 258 </manifest> 259 ''' 260 manifest = ( 261 xml.etree.ElementTree.fromstring(test_manifest_xml), 262 pathlib.Path.cwd() / "manifest.xml", 263 ) 264 project = Project([manifest], pathlib.Path.cwd(), ["*"], None) 265 headers = project.components["test"].headers 266 267 self.assertCountEqual( 268 headers, 269 [ 270 pathlib.Path('src/include/test.h'), 271 pathlib.Path('src/include/test_types.h'), 272 ], 273 ) 274 275 def test_multiple_sets(self): 276 test_manifest_xml = ''' 277 <manifest> 278 <components> 279 <component id="test"> 280 <source relative_path="include" type="c_include"> 281 <files mask="test.h"/> 282 </source> 283 <source relative_path="internal" type="c_include"> 284 <files mask="test_types.h"/> 285 </source> 286 </component> 287 </components> 288 </manifest> 289 ''' 290 manifest = ( 291 xml.etree.ElementTree.fromstring(test_manifest_xml), 292 pathlib.Path.cwd() / "manifest.xml", 293 ) 294 project = Project([manifest], pathlib.Path.cwd(), ["*"], None) 295 headers = project.components["test"].headers 296 297 self.assertCountEqual( 298 headers, 299 [ 300 pathlib.Path('include/test.h'), 301 pathlib.Path('internal/test_types.h'), 302 ], 303 ) 304 305 def test_no_headers(self): 306 test_manifest_xml = ''' 307 <manifest> 308 <components> 309 <component id="test"> 310 </component> 311 </components> 312 </manifest> 313 ''' 314 manifest = ( 315 xml.etree.ElementTree.fromstring(test_manifest_xml), 316 pathlib.Path.cwd() / "manifest.xml", 317 ) 318 project = Project([manifest], pathlib.Path.cwd(), ["*"], None) 319 headers = project.components["test"].headers 320 321 self.assertCountEqual(headers, []) 322 323 def test_device_cores(self): 324 test_manifest_xml = ''' 325 <manifest> 326 <components> 327 <component id="test"> 328 <source relative_path="with_core" type="c_include" 329 device_cores="CORE0"> 330 <files mask="test.h"/> 331 </source> 332 <source relative_path="without_core" type="c_include" 333 device_cores="CORE1"> 334 <files mask="test.h"/> 335 </source> 336 <source relative_path="without_cores" type="c_include"> 337 <files mask="test.h"/> 338 </source> 339 </component> 340 </components> 341 </manifest> 342 ''' 343 manifest = ( 344 xml.etree.ElementTree.fromstring(test_manifest_xml), 345 pathlib.Path.cwd() / "manifest.xml", 346 ) 347 project = Project([manifest], pathlib.Path.cwd(), ["*"], None, "CORE0") 348 headers = project.components["test"].headers 349 350 self.assertCountEqual( 351 headers, 352 [ 353 pathlib.Path('with_core/test.h'), 354 pathlib.Path('without_cores/test.h'), 355 ], 356 ) 357 358 359class ParseSourcesTest(unittest.TestCase): 360 """parse_sources tests.""" 361 362 def test_parse_sources(self): 363 test_manifest_xml = ''' 364 <manifest> 365 <components> 366 <component id="test"> 367 <source relative_path="src" type="src"> 368 <files mask="main.cc"/> 369 <files mask="test.cc"/> 370 </source> 371 </component> 372 </components> 373 </manifest> 374 ''' 375 manifest = ( 376 xml.etree.ElementTree.fromstring(test_manifest_xml), 377 pathlib.Path.cwd() / "manifest.xml", 378 ) 379 project = Project([manifest], pathlib.Path.cwd(), ["*"], None) 380 sources = project.components["test"].sources 381 382 self.assertCountEqual( 383 sources, [pathlib.Path('src/main.cc'), pathlib.Path('src/test.cc')] 384 ) 385 386 def test_with_base_path(self): 387 test_manifest_xml = ''' 388 <manifest> 389 <components> 390 <component id="test" package_base_path="src"> 391 <source relative_path="app" type="src"> 392 <files mask="main.cc"/> 393 <files mask="test.cc"/> 394 </source> 395 </component> 396 </components> 397 </manifest> 398 ''' 399 manifest = ( 400 xml.etree.ElementTree.fromstring(test_manifest_xml), 401 pathlib.Path.cwd() / "manifest.xml", 402 ) 403 project = Project([manifest], pathlib.Path.cwd(), ["*"], None) 404 sources = project.components["test"].sources 405 406 self.assertCountEqual( 407 sources, 408 [pathlib.Path('src/app/main.cc'), pathlib.Path('src/app/test.cc')], 409 ) 410 411 def test_multiple_sets(self): 412 test_manifest_xml = ''' 413 <manifest> 414 <components> 415 <component id="test"> 416 <source relative_path="shared" type="src"> 417 <files mask="test.cc"/> 418 </source> 419 <source relative_path="lib" type="src_c"> 420 <files mask="test.c"/> 421 </source> 422 <source relative_path="app" type="src_cpp"> 423 <files mask="main.cc"/> 424 </source> 425 <source relative_path="startup" type="asm_include"> 426 <files mask="boot.s"/> 427 </source> 428 </component> 429 </components> 430 </manifest> 431 ''' 432 manifest = ( 433 xml.etree.ElementTree.fromstring(test_manifest_xml), 434 pathlib.Path.cwd() / "manifest.xml", 435 ) 436 project = Project([manifest], pathlib.Path.cwd(), ["*"], None) 437 sources = project.components["test"].sources 438 439 self.assertCountEqual( 440 sources, 441 [ 442 pathlib.Path('shared/test.cc'), 443 pathlib.Path('lib/test.c'), 444 pathlib.Path('app/main.cc'), 445 pathlib.Path('startup/boot.s'), 446 ], 447 ) 448 449 def test_unknown_type(self): 450 test_manifest_xml = ''' 451 <manifest> 452 <components> 453 <component id="test"> 454 <source relative_path="src" type="rust"> 455 <files mask="test.rs"/> 456 </source> 457 </component> 458 </components> 459 </manifest> 460 ''' 461 manifest = ( 462 xml.etree.ElementTree.fromstring(test_manifest_xml), 463 pathlib.Path.cwd() / "manifest.xml", 464 ) 465 project = Project([manifest], pathlib.Path.cwd(), ["*"], None) 466 sources = project.components["test"].sources 467 468 self.assertCountEqual(sources, []) 469 470 def test_no_sources(self): 471 test_manifest_xml = ''' 472 <manifest> 473 <components> 474 <component id="test"> 475 </component> 476 </components> 477 </manifest> 478 ''' 479 manifest = ( 480 xml.etree.ElementTree.fromstring(test_manifest_xml), 481 pathlib.Path.cwd() / "manifest.xml", 482 ) 483 project = Project([manifest], pathlib.Path.cwd(), ["*"], None) 484 sources = project.components["test"].sources 485 486 self.assertCountEqual(sources, []) 487 488 def test_device_cores(self): 489 test_manifest_xml = ''' 490 <manifest> 491 <components> 492 <component id="test"> 493 <source relative_path="with_core" type="src" device_cores="CORE0"> 494 <files mask="main.cc"/> 495 </source> 496 <source relative_path="without_core" type="src" 497 device_cores="CORE1"> 498 <files mask="main.cc"/> 499 </source> 500 <source relative_path="without_cores" type="src"> 501 <files mask="main.cc"/> 502 </source> 503 </component> 504 </components> 505 </manifest> 506 ''' 507 manifest = ( 508 xml.etree.ElementTree.fromstring(test_manifest_xml), 509 pathlib.Path.cwd() / "manifest.xml", 510 ) 511 project = Project([manifest], pathlib.Path.cwd(), ["*"], None, "CORE0") 512 sources = project.components["test"].sources 513 514 self.assertCountEqual( 515 sources, 516 [ 517 pathlib.Path('with_core/main.cc'), 518 pathlib.Path('without_cores/main.cc'), 519 ], 520 ) 521 522 523class ParseLibsTest(unittest.TestCase): 524 """parse_libs tests.""" 525 526 def test_parse_libs(self): 527 test_manifest_xml = ''' 528 <manifest> 529 <components> 530 <component id="test"> 531 <source toolchain="armgcc" relative_path="gcc" type="lib"> 532 <files mask="libtest.a"/> 533 <files mask="libtest_arm.a"/> 534 </source> 535 </component> 536 </components> 537 </manifest> 538 ''' 539 manifest = ( 540 xml.etree.ElementTree.fromstring(test_manifest_xml), 541 pathlib.Path.cwd() / "manifest.xml", 542 ) 543 project = Project([manifest], pathlib.Path.cwd(), ["*"], None) 544 libs = project.components["test"].libs 545 546 self.assertCountEqual( 547 libs, 548 [pathlib.Path('gcc/libtest.a'), pathlib.Path('gcc/libtest_arm.a')], 549 ) 550 551 def test_with_base_path(self): 552 test_manifest_xml = ''' 553 <manifest> 554 <components> 555 <component id="test" package_base_path="src"> 556 <source toolchain="armgcc" relative_path="gcc" type="lib"> 557 <files mask="libtest.a"/> 558 <files mask="libtest_arm.a"/> 559 </source> 560 </component> 561 </components> 562 </manifest> 563 ''' 564 manifest = ( 565 xml.etree.ElementTree.fromstring(test_manifest_xml), 566 pathlib.Path.cwd() / "manifest.xml", 567 ) 568 project = Project([manifest], pathlib.Path.cwd(), ["*"], None) 569 libs = project.components["test"].libs 570 571 self.assertCountEqual( 572 libs, 573 [ 574 pathlib.Path('src/gcc/libtest.a'), 575 pathlib.Path('src/gcc/libtest_arm.a'), 576 ], 577 ) 578 579 def test_multiple_sets(self): 580 test_manifest_xml = ''' 581 <manifest> 582 <components> 583 <component id="test"> 584 <source toolchain="armgcc" relative_path="gcc" type="lib"> 585 <files mask="libtest.a"/> 586 </source> 587 <source toolchain="armgcc" relative_path="arm" type="lib"> 588 <files mask="libtest_arm.a"/> 589 </source> 590 </component> 591 </components> 592 </manifest> 593 ''' 594 manifest = ( 595 xml.etree.ElementTree.fromstring(test_manifest_xml), 596 pathlib.Path.cwd() / "manifest.xml", 597 ) 598 project = Project([manifest], pathlib.Path.cwd(), ["*"], None) 599 libs = project.components["test"].libs 600 601 self.assertCountEqual( 602 libs, 603 [pathlib.Path('gcc/libtest.a'), pathlib.Path('arm/libtest_arm.a')], 604 ) 605 606 def test_multiple_toolchains(self): 607 test_manifest_xml = ''' 608 <manifest> 609 <components> 610 <component id="test"> 611 <source toolchain="armgcc" relative_path="gcc" type="lib"> 612 <files mask="libtest.a"/> 613 </source> 614 <source toolchain="mcuxpresso" relative_path="arm" type="lib"> 615 <files mask="libtest_mcux.a"/> 616 </source> 617 </component> 618 </components> 619 </manifest> 620 ''' 621 manifest = ( 622 xml.etree.ElementTree.fromstring(test_manifest_xml), 623 pathlib.Path.cwd() / "manifest.xml", 624 ) 625 project = Project([manifest], pathlib.Path.cwd(), ["*"], None) 626 libs = project.components["test"].libs 627 628 self.assertCountEqual( 629 libs, 630 [pathlib.Path('gcc/libtest.a')], 631 ) 632 633 def test_no_libs(self): 634 test_manifest_xml = ''' 635 <manifest> 636 <components> 637 <component id="test"> 638 </component> 639 </components> 640 </manifest> 641 ''' 642 manifest = ( 643 xml.etree.ElementTree.fromstring(test_manifest_xml), 644 pathlib.Path.cwd() / "manifest.xml", 645 ) 646 project = Project([manifest], pathlib.Path.cwd(), ["*"], None) 647 libs = project.components["test"].libs 648 649 self.assertCountEqual(libs, []) 650 651 def test_device_cores(self): 652 test_manifest_xml = ''' 653 <manifest> 654 <components> 655 <component id="test"> 656 <source toolchain="armgcc" relative_path="with_core" type="lib" device_cores="CORE0"> 657 <files mask="libtest.a"/> 658 </source> 659 <source toolchain="armgcc" relative_path="without_core" type="lib" 660 device_cores="CORE1"> 661 <files mask="libtest.a"/> 662 </source> 663 <source toolchain="armgcc" relative_path="without_cores" type="lib"> 664 <files mask="libtest.a"/> 665 </source> 666 </component> 667 </components> 668 </manifest> 669 ''' 670 manifest = ( 671 xml.etree.ElementTree.fromstring(test_manifest_xml), 672 pathlib.Path.cwd() / "manifest.xml", 673 ) 674 project = Project([manifest], pathlib.Path.cwd(), ["*"], None, "CORE0") 675 libs = project.components["test"].libs 676 677 self.assertCountEqual( 678 libs, 679 [ 680 pathlib.Path('with_core/libtest.a'), 681 pathlib.Path('without_cores/libtest.a'), 682 ], 683 ) 684 685 686class ParseDependenciesTest(unittest.TestCase): 687 """parse_dependencies tests.""" 688 689 def test_component_dependency(self): 690 test_manifest_xml = ''' 691 <manifest> 692 <components> 693 <component id="test"> 694 <dependencies> 695 <component_dependency value="foo"/> 696 </dependencies> 697 </component> 698 <component id="foo"> 699 </component> 700 </components> 701 </manifest> 702 ''' 703 manifest = ( 704 xml.etree.ElementTree.fromstring(test_manifest_xml), 705 pathlib.Path.cwd() / "manifest.xml", 706 ) 707 project = Project([manifest], pathlib.Path.cwd(), ["*"], None) 708 dependencies = project.components["test"].dependencies 709 710 self.assertCountEqual(dependencies, {'foo'}) 711 712 def test_all(self): 713 test_manifest_xml = ''' 714 <manifest> 715 <components> 716 <component id="test"> 717 <dependencies> 718 <all> 719 <component_dependency value="foo"/> 720 <component_dependency value="bar"/> 721 <component_dependency value="baz"/> 722 </all> 723 </dependencies> 724 </component> 725 <component id="foo"> 726 </component> 727 <component id="bar"> 728 </component> 729 <component id="baz"> 730 </component> 731 </components> 732 </manifest> 733 ''' 734 manifest = ( 735 xml.etree.ElementTree.fromstring(test_manifest_xml), 736 pathlib.Path.cwd() / "manifest.xml", 737 ) 738 project = Project([manifest], pathlib.Path.cwd(), ["*"], None) 739 dependencies = project.components["test"].dependencies 740 741 self.assertCountEqual(dependencies, {'foo', 'bar', 'baz'}) 742 743 def test_any_of(self): 744 test_manifest_xml = ''' 745 <manifest> 746 <components> 747 <component id="test"> 748 <dependencies> 749 <any_of> 750 <component_dependency value="foo"/> 751 <component_dependency value="bar"/> 752 <component_dependency value="baz"/> 753 </any_of> 754 </dependencies> 755 </component> 756 <component id="foo"> 757 </component> 758 <component id="bar"> 759 </component> 760 <component id="baz"> 761 </component> 762 </components> 763 </manifest> 764 ''' 765 manifest = ( 766 xml.etree.ElementTree.fromstring(test_manifest_xml), 767 pathlib.Path.cwd() / "manifest.xml", 768 ) 769 project = Project([manifest], pathlib.Path.cwd(), ["test", "foo"], None) 770 dependencies = project.components["test"].dependencies 771 772 self.assertCountEqual(dependencies, {'foo'}) 773 774 def test_any_of_inside_all(self): 775 test_manifest_xml = ''' 776 <manifest> 777 <components> 778 <component id="test"> 779 <dependencies> 780 <all> 781 <component_dependency value="foo"/> 782 <component_dependency value="bar"/> 783 <component_dependency value="baz"/> 784 <any_of> 785 <all> 786 <component_dependency value="frodo"/> 787 <component_dependency value="bilbo"/> 788 </all> 789 <component_dependency value="gandalf"/> 790 </any_of> 791 </all> 792 </dependencies> 793 </component> 794 <component id="foo"> 795 </component> 796 <component id="bar"> 797 </component> 798 <component id="baz"> 799 </component> 800 <component id="frodo"> 801 </component> 802 <component id="bilbo"> 803 </component> 804 <component id="gandalf"> 805 </component> 806 </components> 807 </manifest> 808 ''' 809 manifest = ( 810 xml.etree.ElementTree.fromstring(test_manifest_xml), 811 pathlib.Path.cwd() / "manifest.xml", 812 ) 813 project = Project( 814 [manifest], pathlib.Path.cwd(), ["test", "frodo", "bilbo"], None 815 ) 816 dependencies = project.components["test"].dependencies 817 818 self.assertCountEqual( 819 dependencies, {'foo', 'bar', 'baz', 'frodo', 'bilbo'} 820 ) 821 822 def test_no_dependencies(self): 823 test_manifest_xml = ''' 824 <manifest> 825 <components> 826 <component id="test"> 827 </component> 828 </components> 829 </manifest> 830 ''' 831 manifest = ( 832 xml.etree.ElementTree.fromstring(test_manifest_xml), 833 pathlib.Path.cwd() / "manifest.xml", 834 ) 835 project = Project([manifest], pathlib.Path.cwd(), ["*"], ["gandalf"]) 836 dependencies = project.components["test"].dependencies 837 838 self.assertCountEqual(dependencies, []) 839 840 841class ProjectTest(unittest.TestCase): 842 """Project tests.""" 843 844 def test_create_project(self): 845 """test creating a project.""" 846 test_manifest_xml = ''' 847 <manifest> 848 <components> 849 <component id="test"> 850 <dependencies> 851 <component_dependency value="foo"/> 852 <component_dependency value="bar"/> 853 <any_of> 854 <component_dependency value="baz"/> 855 </any_of> 856 </dependencies> 857 </component> 858 <component id="foo" package_base_path="foo"> 859 <defines> 860 <define name="FOO"/> 861 </defines> 862 <source relative_path="include" type="c_include"> 863 <files mask="foo.h"/> 864 </source> 865 <source relative_path="src" type="src"> 866 <files mask="foo.cc"/> 867 </source> 868 <include_paths> 869 <include_path relative_path="include" type="c_include"/> 870 </include_paths> 871 <!-- core0 should be included in the project --> 872 <source relative_path="include" type="c_include" 873 device_cores="CORE0"> 874 <files mask="core0.h"/> 875 </source> 876 <source relative_path="src" type="src" device_cores="CORE0"> 877 <files mask="core0.cc"/> 878 </source> 879 <!-- core1 should not be included in the project --> 880 <source relative_path="include" type="c_include" 881 device_cores="CORE1"> 882 <files mask="core1.h"/> 883 </source> 884 <source relative_path="src" type="src" device_cores="CORE1"> 885 <files mask="core0.cc"/> 886 </source> 887 <!-- common should be --> 888 <source relative_path="include" type="c_include" 889 device_cores="CORE0 CORE1"> 890 <files mask="common.h"/> 891 </source> 892 <source relative_path="src" type="src" device_cores="CORE0 CORE1"> 893 <files mask="common.cc"/> 894 </source> 895 </component> 896 <component id="bar" package_base_path="bar"> 897 <defines> 898 <define name="BAR"/> 899 </defines> 900 <source relative_path="include" type="c_include"> 901 <files mask="bar.h"/> 902 </source> 903 <source relative_path="src" type="src"> 904 <files mask="bar.cc"/> 905 </source> 906 <include_paths> 907 <include_path relative_path="include" type="c_include"/> 908 </include_paths> 909 </component> 910 <!-- baz should not be included in the output --> 911 <component id="baz" package_base_path="baz"> 912 <defines> 913 <define name="BAZ"/> 914 </defines> 915 <source relative_path="include" type="c_include"> 916 <files mask="baz.h"/> 917 </source> 918 <source relative_path="src" type="src"> 919 <files mask="baz.cc"/> 920 </source> 921 <include_paths> 922 <include_path relative_path="include" type="c_include"/> 923 </include_paths> 924 </component> 925 <component id="frodo" package_base_path="frodo"> 926 <dependencies> 927 <component_dependency value="bilbo"/> 928 </dependencies> 929 <defines> 930 <define name="FRODO"/> 931 </defines> 932 <source relative_path="include" type="c_include"> 933 <files mask="frodo.h"/> 934 </source> 935 <source relative_path="src" type="src"> 936 <files mask="frodo.cc"/> 937 </source> 938 <source toolchain="armgcc" relative_path="./" type="lib"> 939 <files mask="libonering.a"/> 940 </source> 941 <include_paths> 942 <include_path relative_path="include" type="c_include"/> 943 </include_paths> 944 </component> 945 <!-- bilbo should be excluded from the project --> 946 <component id="bilbo" package_base_path="bilbo"> 947 <defines> 948 <define name="BILBO"/> 949 </defines> 950 <source relative_path="include" type="c_include"> 951 <files mask="bilbo.h"/> 952 </source> 953 <source relative_path="src" type="src"> 954 <files mask="bilbo.cc"/> 955 </source> 956 <include_paths> 957 <include_path relative_path="include" type="c_include"/> 958 </include_paths> 959 </component> 960 </components> 961 </manifest> 962 ''' 963 manifest = ( 964 xml.etree.ElementTree.fromstring(test_manifest_xml), 965 pathlib.Path.cwd() / "manifest.xml", 966 ) 967 project = Project( 968 [manifest], 969 pathlib.Path.cwd(), 970 ["test", "frodo"], 971 ["baz"], 972 "CORE0", 973 ) 974 components = project.components.values() 975 976 component_ids = (component.id for component in components) 977 self.assertCountEqual( 978 component_ids, ['test', 'frodo', 'bilbo', 'foo', 'bar'] 979 ) 980 981 defines = chain.from_iterable( 982 component.defines for component in components 983 ) 984 self.assertCountEqual(defines, ['FRODO', 'BILBO', 'FOO', 'BAR']) 985 986 include_dirs = chain.from_iterable( 987 component.include_dirs for component in components 988 ) 989 self.assertCountEqual( 990 include_dirs, 991 [ 992 pathlib.Path('frodo/include'), 993 pathlib.Path('bilbo/include'), 994 pathlib.Path('foo/include'), 995 pathlib.Path('bar/include'), 996 ], 997 ) 998 999 headers = chain.from_iterable( 1000 component.headers for component in components 1001 ) 1002 self.assertCountEqual( 1003 headers, 1004 [ 1005 pathlib.Path('frodo/include/frodo.h'), 1006 pathlib.Path('bilbo/include/bilbo.h'), 1007 pathlib.Path('foo/include/foo.h'), 1008 pathlib.Path('foo/include/core0.h'), 1009 pathlib.Path('foo/include/common.h'), 1010 pathlib.Path('bar/include/bar.h'), 1011 ], 1012 ) 1013 1014 sources = chain.from_iterable( 1015 component.sources for component in components 1016 ) 1017 self.assertCountEqual( 1018 sources, 1019 [ 1020 pathlib.Path('frodo/src/frodo.cc'), 1021 pathlib.Path('bilbo/src/bilbo.cc'), 1022 pathlib.Path('foo/src/foo.cc'), 1023 pathlib.Path('foo/src/core0.cc'), 1024 pathlib.Path('foo/src/common.cc'), 1025 pathlib.Path('bar/src/bar.cc'), 1026 ], 1027 ) 1028 1029 libs = chain.from_iterable(component.libs for component in components) 1030 self.assertCountEqual(libs, [pathlib.Path('frodo/libonering.a')]) 1031 1032 1033if __name__ == '__main__': 1034 unittest.main() 1035