-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathtest_compilesketches.py
3197 lines (2758 loc) · 131 KB
/
test_compilesketches.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import filecmp
import json
import os
import pathlib
import shutil
import subprocess
import tarfile
import tempfile
import unittest.mock
import git
import github
import pytest
import compilesketches
os.environ["GITHUB_WORKSPACE"] = "/foo/github-workspace"
test_data_path = pathlib.PurePath(os.path.dirname(os.path.realpath(__file__)), "testdata")
def get_compilesketches_object(
cli_version="0.12.3",
fqbn_arg="foo fqbn_arg",
platforms="- name: FooVendor:BarArchitecture",
libraries="foo libraries",
sketch_paths="foo sketch_paths",
cli_compile_flags="--foo",
verbose="false",
github_token="",
github_api=unittest.mock.sentinel.github_api,
deltas_base_ref="foodeltasbaseref",
enable_deltas_report="false",
enable_warnings_report="false",
sketches_report_path="foo report_folder_name",
):
with unittest.mock.patch(
"compilesketches.CompileSketches.get_deltas_base_ref", autospec=True, return_value=deltas_base_ref
):
compilesketches_object = compilesketches.CompileSketches(
cli_version=cli_version,
fqbn_arg=fqbn_arg,
platforms=platforms,
libraries=libraries,
sketch_paths=sketch_paths,
cli_compile_flags=cli_compile_flags,
verbose=verbose,
github_token=github_token,
enable_deltas_report=enable_deltas_report,
enable_warnings_report=enable_warnings_report,
sketches_report_path=sketches_report_path,
)
compilesketches_object.github_api = github_api
return compilesketches_object
def directories_are_same(left_directory, right_directory):
"""Check recursively whether two directories contain the same files.
Based on https://stackoverflow.com/a/24860799
"""
directory_comparison = filecmp.dircmp(a=left_directory, b=right_directory)
if (
directory_comparison.left_only
or directory_comparison.right_only
or directory_comparison.diff_files
or directory_comparison.funny_files
):
return False
for subdirectory in directory_comparison.common_dirs:
if not directories_are_same(left_directory.joinpath(subdirectory), right_directory.joinpath(subdirectory)):
return False
return True
def test_directories_are_same():
assert directories_are_same(left_directory=test_data_path, right_directory=test_data_path) is True
assert (
directories_are_same(
left_directory=test_data_path.joinpath("HasSketches"), right_directory=test_data_path.joinpath("NoSketches")
)
is False
)
assert (
directories_are_same(
left_directory=test_data_path.joinpath("HasSketches", "NoSketches"),
right_directory=test_data_path.joinpath("NoSketches"),
)
is False
)
@pytest.fixture
def setup_action_inputs(monkeypatch):
class ActionInputs:
cli_version = "1.0.0"
fqbn_arg = "foo:bar:baz"
platforms = "- name: FooVendor:BarArchitecture"
libraries = "foo libraries"
sketch_paths = "foo/Sketch bar/OtherSketch"
cli_compile_flags = "--foo"
verbose = "true"
github_token = "FooGitHubToken"
enable_size_deltas_report = "FooEnableSizeDeltasReport"
enable_deltas_report = "FooEnableDeltasReport"
enable_warnings_report = "FooEnableWarningsReport"
sketches_report_path = "FooSketchesReportPath"
size_deltas_report_folder_name = "FooSizeDeltasReportFolderName"
monkeypatch.setenv("INPUT_CLI-VERSION", ActionInputs.cli_version)
monkeypatch.setenv("INPUT_FQBN", ActionInputs.fqbn_arg)
monkeypatch.setenv("INPUT_PLATFORMS", ActionInputs.platforms)
monkeypatch.setenv("INPUT_LIBRARIES", ActionInputs.libraries)
monkeypatch.setenv("INPUT_SKETCH-PATHS", ActionInputs.sketch_paths)
monkeypatch.setenv("INPUT_CLI-COMPILE-FLAGS", ActionInputs.cli_compile_flags)
monkeypatch.setenv("INPUT_VERBOSE", ActionInputs.verbose)
monkeypatch.setenv("INPUT_GITHUB-TOKEN", ActionInputs.github_token)
monkeypatch.setenv("INPUT_ENABLE-DELTAS-REPORT", ActionInputs.enable_deltas_report)
monkeypatch.setenv("INPUT_ENABLE-WARNINGS-REPORT", ActionInputs.enable_warnings_report)
monkeypatch.setenv("INPUT_SKETCHES-REPORT-PATH", ActionInputs.sketches_report_path)
return ActionInputs()
@pytest.fixture
def stub_compilesketches_object(mocker):
class CompileSketches:
def compile_sketches(self):
pass # pragma: no cover
mocker.patch("compilesketches.CompileSketches", autospec=True, return_value=CompileSketches())
mocker.patch.object(CompileSketches, "compile_sketches")
@pytest.mark.parametrize("use_size_report_sketch", [True, False])
def test_main_size_report_sketch_deprecation_warning(
capsys, monkeypatch, setup_action_inputs, stub_compilesketches_object, use_size_report_sketch
):
if use_size_report_sketch:
monkeypatch.setenv("INPUT_SIZE-REPORT-SKETCH", "foo")
compilesketches.main()
expected_output = ""
if use_size_report_sketch:
expected_output = "::warning::The size-report-sketch input is no longer used"
assert capsys.readouterr().out.strip() == expected_output
@pytest.mark.parametrize("use_enable_size_trends_report", [True, False])
def test_main_enable_size_trends_report_deprecation_warning(
capsys, monkeypatch, setup_action_inputs, stub_compilesketches_object, use_enable_size_trends_report
):
if use_enable_size_trends_report:
monkeypatch.setenv("INPUT_ENABLE-SIZE-TRENDS-REPORT", "true")
compilesketches.main()
expected_output = ""
if use_enable_size_trends_report:
expected_output = (
expected_output + "::warning::The size trends report feature has been moved to a dedicated action. See the "
"documentation at "
"https://github.com/arduino/actions/tree/report-size-trends-action/libraries/report-size-trends"
)
assert capsys.readouterr().out.strip() == expected_output
@pytest.mark.parametrize("use_size_deltas_report_folder_name", [True, False])
def test_main_size_deltas_report_folder_name_deprecation(
capsys, monkeypatch, setup_action_inputs, stub_compilesketches_object, use_size_deltas_report_folder_name
):
size_deltas_report_folder_name = "foo-size-deltas-report-folder-name"
if use_size_deltas_report_folder_name:
monkeypatch.setenv("INPUT_SIZE-DELTAS-REPORT-FOLDER-NAME", size_deltas_report_folder_name)
compilesketches.main()
expected_output = ""
if use_size_deltas_report_folder_name:
expected_output = (
expected_output
+ "::warning::The size-deltas-report-folder-name input is deprecated. Use the equivalent input: "
"sketches-report-path instead."
)
assert capsys.readouterr().out.strip() == expected_output
if use_size_deltas_report_folder_name:
expected_sketches_report_path = size_deltas_report_folder_name
else:
expected_sketches_report_path = setup_action_inputs.sketches_report_path
assert os.environ["INPUT_SKETCHES-REPORT-PATH"] == expected_sketches_report_path
@pytest.mark.parametrize("use_enable_size_deltas_report", [True, False])
def test_main_enable_size_deltas_report_deprecation(
capsys, monkeypatch, setup_action_inputs, stub_compilesketches_object, use_enable_size_deltas_report
):
enable_size_deltas_report = "foo-enable-size-deltas-report"
if use_enable_size_deltas_report:
monkeypatch.setenv("INPUT_ENABLE-SIZE-DELTAS-REPORT", enable_size_deltas_report)
compilesketches.main()
expected_output = ""
if use_enable_size_deltas_report:
expected_output = (
expected_output + "::warning::The enable-size-deltas-report input is deprecated. Use the equivalent input: "
"enable-deltas-report instead."
)
assert capsys.readouterr().out.strip() == expected_output
if use_enable_size_deltas_report:
expected_enable_deltas_report = enable_size_deltas_report
else:
expected_enable_deltas_report = setup_action_inputs.enable_deltas_report
assert os.environ["INPUT_ENABLE-DELTAS-REPORT"] == expected_enable_deltas_report
def test_main(mocker, setup_action_inputs):
class CompileSketches:
def compile_sketches(self):
pass # pragma: no cover
mocker.patch("compilesketches.CompileSketches", autospec=True, return_value=CompileSketches())
mocker.patch.object(CompileSketches, "compile_sketches")
compilesketches.main()
compilesketches.CompileSketches.assert_called_once_with(
cli_version=setup_action_inputs.cli_version,
fqbn_arg=setup_action_inputs.fqbn_arg,
platforms=setup_action_inputs.platforms,
libraries=setup_action_inputs.libraries,
sketch_paths=setup_action_inputs.sketch_paths,
cli_compile_flags=setup_action_inputs.cli_compile_flags,
verbose=setup_action_inputs.verbose,
github_token=setup_action_inputs.github_token,
enable_deltas_report=setup_action_inputs.enable_deltas_report,
enable_warnings_report=setup_action_inputs.enable_warnings_report,
sketches_report_path=setup_action_inputs.sketches_report_path,
)
CompileSketches.compile_sketches.assert_called_once()
def test_compilesketches():
expected_fqbn = "foo:bar:baz"
expected_additional_url = "https://example.com/package_foo_index.json"
cli_version = unittest.mock.sentinel.cli_version
platforms = unittest.mock.sentinel.platforms
libraries = unittest.mock.sentinel.libraries
sketch_paths = "examples/FooSketchPath examples/BarSketchPath"
expected_sketch_paths_list = [
compilesketches.absolute_path(path="examples/FooSketchPath"),
compilesketches.absolute_path(path="examples/BarSketchPath"),
]
cli_compile_flags = "- --foo\n- --bar"
expected_cli_compile_flags = ["--foo", "--bar"]
verbose = "false"
github_token = "fooGitHubToken"
expected_deltas_base_ref = unittest.mock.sentinel.deltas_base_ref
enable_deltas_report = "true"
enable_warnings_report = "true"
sketches_report_path = "FooSketchesReportFolder"
with unittest.mock.patch(
"compilesketches.CompileSketches.get_deltas_base_ref", autospec=True, return_value=expected_deltas_base_ref
):
compile_sketches = compilesketches.CompileSketches(
cli_version=cli_version,
fqbn_arg="'\"" + expected_fqbn + '" "' + expected_additional_url + "\"'",
platforms=platforms,
libraries=libraries,
sketch_paths=sketch_paths,
cli_compile_flags=cli_compile_flags,
verbose=verbose,
github_token=github_token,
enable_deltas_report=enable_deltas_report,
enable_warnings_report=enable_warnings_report,
sketches_report_path=sketches_report_path,
)
assert compile_sketches.cli_version == cli_version
assert compile_sketches.fqbn == expected_fqbn
assert compile_sketches.additional_url == expected_additional_url
assert compile_sketches.platforms == platforms
assert compile_sketches.libraries == libraries
assert compile_sketches.sketch_paths == expected_sketch_paths_list
assert compile_sketches.cli_compile_flags == expected_cli_compile_flags
assert compile_sketches.verbose is False
assert compile_sketches.deltas_base_ref == expected_deltas_base_ref
assert compile_sketches.enable_deltas_report is True
assert compile_sketches.enable_warnings_report is True
assert compile_sketches.sketches_report_path == pathlib.PurePath(sketches_report_path)
assert get_compilesketches_object(cli_compile_flags="").cli_compile_flags is None
assert get_compilesketches_object(cli_compile_flags="- --foo").cli_compile_flags == ["--foo"]
assert get_compilesketches_object(cli_compile_flags='- --foo\n- "bar baz"').cli_compile_flags == [
"--foo",
"bar baz",
]
# Test invalid enable_deltas_report value
with pytest.raises(expected_exception=SystemExit, match="1"):
get_compilesketches_object(enable_deltas_report="fooInvalidEnableSizeDeltasBoolean")
# Test invalid enable_deltas_report value
with pytest.raises(expected_exception=SystemExit, match="1"):
get_compilesketches_object(enable_warnings_report="fooInvalidEnableWarningsReportBoolean")
# Test deltas_base_ref when size deltas report is disabled
compile_sketches = get_compilesketches_object(enable_deltas_report="false")
assert compile_sketches.deltas_base_ref is None
@pytest.mark.parametrize(
"event_name, expected_ref",
[
("pull_request", unittest.mock.sentinel.pull_request_base_ref),
("push", unittest.mock.sentinel.parent_commit_ref),
],
)
def test_get_deltas_base_ref(monkeypatch, mocker, event_name, expected_ref):
monkeypatch.setenv("GITHUB_EVENT_NAME", event_name)
mocker.patch(
"compilesketches.CompileSketches.get_pull_request_base_ref",
autospec=True,
return_value=unittest.mock.sentinel.pull_request_base_ref,
)
mocker.patch(
"compilesketches.get_parent_commit_ref", autospec=True, return_value=unittest.mock.sentinel.parent_commit_ref
)
compile_sketches = get_compilesketches_object()
assert compile_sketches.get_deltas_base_ref() == expected_ref
def test_get_pull_request_base_ref(monkeypatch, mocker):
class Github:
"""Stub"""
ref = unittest.mock.sentinel.pull_request_base_ref
def __init__(self):
self.base = self
def get_repo(self):
pass # pragma: no cover
def get_pull(self, number):
pass # pragma: no cover
github_api_object = Github()
monkeypatch.setenv("GITHUB_EVENT_PATH", str(test_data_path.joinpath("githubevent.json")))
monkeypatch.setenv("GITHUB_REPOSITORY", "fooRepository/fooOwner")
mocker.patch.object(Github, "get_repo", return_value=Github())
mocker.patch.object(Github, "get_pull", return_value=Github())
compile_sketches = get_compilesketches_object(github_api=github_api_object)
assert compile_sketches.get_pull_request_base_ref() == unittest.mock.sentinel.pull_request_base_ref
github_api_object.get_repo.assert_called_once_with(full_name_or_id=os.environ["GITHUB_REPOSITORY"])
github_api_object.get_pull.assert_called_once_with(number=42) # PR number is hardcoded into test file
mocker.patch.object(
Github, "get_repo", side_effect=github.UnknownObjectException(status=42, data="foo", headers=None)
)
with pytest.raises(expected_exception=SystemExit, match="1"):
compile_sketches.get_pull_request_base_ref()
def test_get_parent_commit_ref(mocker):
parent_commit_ref = unittest.mock.sentinel.parent_commit_ref
class Repo:
"""Stub"""
hexsha = parent_commit_ref
def __init__(self):
self.head = self
self.object = self
self.parents = [self]
mocker.patch("git.Repo", autospec=True, return_value=Repo())
assert compilesketches.get_parent_commit_ref() == parent_commit_ref
git.Repo.assert_called_once_with(path=os.environ["GITHUB_WORKSPACE"])
@pytest.mark.parametrize("enable_warnings_report, expected_clean_build_cache", [("true", True), ("false", False)])
@pytest.mark.parametrize(
"compilation_success_list, expected_success",
[
([True, True, True], True),
([False, True, True], False),
([True, False, True], False),
([True, True, False], False),
],
)
def test_compile_sketches(
mocker, enable_warnings_report, expected_clean_build_cache, compilation_success_list, expected_success
):
sketch_list = [unittest.mock.sentinel.sketch1, unittest.mock.sentinel.sketch2, unittest.mock.sentinel.sketch3]
compilation_result_list = []
for success in compilation_success_list:
compilation_result_list.append(type("CompilationResult", (), {"success": success}))
sketch_report = unittest.mock.sentinel.sketch_report
sketches_report = unittest.mock.sentinel.sketch_report_from_sketches_report
compile_sketches = get_compilesketches_object(enable_warnings_report=enable_warnings_report)
mocker.patch("compilesketches.CompileSketches.install_arduino_cli", autospec=True)
mocker.patch("compilesketches.CompileSketches.install_platforms", autospec=True)
mocker.patch("compilesketches.CompileSketches.install_libraries", autospec=True)
mocker.patch("compilesketches.CompileSketches.find_sketches", autospec=True, return_value=sketch_list)
mocker.patch("compilesketches.CompileSketches.compile_sketch", autospec=True, side_effect=compilation_result_list)
mocker.patch("compilesketches.CompileSketches.get_sketch_report", autospec=True, return_value=sketch_report)
mocker.patch("compilesketches.CompileSketches.get_sketches_report", autospec=True, return_value=sketches_report)
mocker.patch("compilesketches.CompileSketches.create_sketches_report_file", autospec=True)
if expected_success:
compile_sketches.compile_sketches()
else:
with pytest.raises(expected_exception=SystemExit, match="1"):
compile_sketches.compile_sketches()
compile_sketches.install_arduino_cli.assert_called_once()
compile_sketches.install_platforms.assert_called_once()
compile_sketches.install_libraries.assert_called_once()
compile_sketches.find_sketches.assert_called_once()
compile_sketch_calls = []
get_sketch_report_calls = []
sketch_report_list = []
for sketch, compilation_result in zip(sketch_list, compilation_result_list):
compile_sketch_calls.append(
unittest.mock.call(compile_sketches, sketch_path=sketch, clean_build_cache=expected_clean_build_cache)
)
get_sketch_report_calls.append(unittest.mock.call(compile_sketches, compilation_result=compilation_result))
sketch_report_list.append(sketch_report)
compile_sketches.compile_sketch.assert_has_calls(calls=compile_sketch_calls)
compile_sketches.get_sketch_report.assert_has_calls(calls=get_sketch_report_calls)
compile_sketches.get_sketches_report.assert_called_once_with(
compile_sketches, sketch_report_list=sketch_report_list
)
compile_sketches.create_sketches_report_file.assert_called_once_with(
compile_sketches, sketches_report=sketches_report
)
def test_install_arduino_cli(mocker):
cli_version = "1.2.3"
arduino_cli_installation_path = unittest.mock.sentinel.arduino_cli_installation_path
arduino_cli_data_directory_path = pathlib.PurePath("/foo/arduino_cli_data_directory_path")
arduino_cli_user_directory_path = pathlib.PurePath("/foo/arduino_cli_user_directory_path")
compile_sketches = get_compilesketches_object(cli_version=cli_version)
compile_sketches.arduino_cli_installation_path = arduino_cli_installation_path
compile_sketches.arduino_cli_user_directory_path = arduino_cli_user_directory_path
compile_sketches.arduino_cli_data_directory_path = arduino_cli_data_directory_path
mocker.patch("compilesketches.CompileSketches.install_from_download", autospec=True)
compile_sketches.install_arduino_cli()
compile_sketches.install_from_download.assert_called_once_with(
compile_sketches,
url="https://downloads.arduino.cc/arduino-cli/arduino-cli_" + cli_version + "_Linux_64bit.tar.gz",
source_path="arduino-cli",
destination_parent_path=arduino_cli_installation_path,
force=False,
)
assert os.environ["ARDUINO_DIRECTORIES_USER"] == str(arduino_cli_user_directory_path)
assert os.environ["ARDUINO_DIRECTORIES_DATA"] == str(arduino_cli_data_directory_path)
del os.environ["ARDUINO_DIRECTORIES_USER"]
del os.environ["ARDUINO_DIRECTORIES_DATA"]
@pytest.mark.parametrize("platforms", ["", "foo"])
def test_install_platforms(mocker, platforms):
fqbn_platform_dependency = unittest.mock.sentinel.fqbn_platform_dependency
dependency_list_manager = [unittest.mock.sentinel.manager]
dependency_list_path = [unittest.mock.sentinel.path]
dependency_list_repository = [unittest.mock.sentinel.repository]
dependency_list_download = [unittest.mock.sentinel.download]
dependency_list = compilesketches.CompileSketches.Dependencies()
dependency_list.manager = dependency_list_manager
dependency_list.path = dependency_list_path
dependency_list.repository = dependency_list_repository
dependency_list.download = dependency_list_download
compile_sketches = get_compilesketches_object(platforms=platforms)
mocker.patch(
"compilesketches.CompileSketches.get_fqbn_platform_dependency",
autospec=True,
return_value=fqbn_platform_dependency,
)
mocker.patch("compilesketches.CompileSketches.sort_dependency_list", autospec=True, return_value=dependency_list)
mocker.patch("compilesketches.CompileSketches.install_platforms_from_board_manager", autospec=True)
mocker.patch("compilesketches.CompileSketches.install_platforms_from_path", autospec=True)
mocker.patch("compilesketches.CompileSketches.install_platforms_from_repository", autospec=True)
mocker.patch("compilesketches.CompileSketches.install_platforms_from_download", autospec=True)
compile_sketches.install_platforms()
if platforms == "":
compile_sketches.install_platforms_from_board_manager.assert_called_once_with(
compile_sketches, platform_list=[fqbn_platform_dependency]
)
compile_sketches.install_platforms_from_path.assert_not_called()
compile_sketches.install_platforms_from_repository.assert_not_called()
compile_sketches.install_platforms_from_download.assert_not_called()
else:
compile_sketches.install_platforms_from_board_manager.assert_called_once_with(
compile_sketches, platform_list=dependency_list_manager
)
compile_sketches.install_platforms_from_path.assert_called_once_with(
compile_sketches, platform_list=dependency_list_path
)
compile_sketches.install_platforms_from_repository.assert_called_once_with(
compile_sketches, platform_list=dependency_list_repository
)
compile_sketches.install_platforms_from_download.assert_called_once_with(
compile_sketches, platform_list=dependency_list_download
)
@pytest.mark.parametrize(
"fqbn_arg, expected_platform, expected_additional_url",
[
("arduino:avr:uno", "arduino:avr", None),
# FQBN with space, additional Board Manager URL
(
'\'"foo bar:baz:asdf" "https://example.com/platform_foo_index.json"\'',
"foo bar:baz",
"https://example.com/platform_foo_index.json",
),
# Custom board option
("arduino:avr:nano:cpu=atmega328old", "arduino:avr", None),
],
)
def test_get_fqbn_platform_dependency(fqbn_arg, expected_platform, expected_additional_url):
compile_sketches = get_compilesketches_object(fqbn_arg=fqbn_arg)
fqbn_platform_dependency = compile_sketches.get_fqbn_platform_dependency()
assert fqbn_platform_dependency[compilesketches.CompileSketches.dependency_name_key] == expected_platform
if expected_additional_url is not None:
assert fqbn_platform_dependency[compilesketches.CompileSketches.dependency_source_url_key] == (
expected_additional_url
)
@pytest.mark.parametrize(
"dependency_list, expected_dependency_type_list",
[
([None], []),
(
[{compilesketches.CompileSketches.dependency_source_url_key: "https://example.com/foo/bar.git"}],
["repository"],
),
(
[{compilesketches.CompileSketches.dependency_source_url_key: "https://example.com/foo/bar.git/"}],
["repository"],
),
([{compilesketches.CompileSketches.dependency_source_url_key: "git://example.com/foo/bar"}], ["repository"]),
([{compilesketches.CompileSketches.dependency_source_url_key: "https://example.com/foo/bar"}], ["download"]),
([{compilesketches.CompileSketches.dependency_source_path_key: "foo/bar"}], ["path"]),
([{compilesketches.CompileSketches.dependency_name_key: "FooBar"}], ["manager"]),
(
[
{
compilesketches.CompileSketches.dependency_name_key: "FooBar",
compilesketches.CompileSketches.dependency_source_url_key: "https://example.com/package_foo_index.json",
}
],
["manager"],
),
(
[
{compilesketches.CompileSketches.dependency_source_url_key: "git://example.com/foo/bar"},
{compilesketches.CompileSketches.dependency_source_url_key: "https://example.com/foo/bar"},
{compilesketches.CompileSketches.dependency_source_path_key: "foo/bar"},
{compilesketches.CompileSketches.dependency_name_key: "FooBar"},
],
["repository", "download", "path", "manager"],
),
([{compilesketches.CompileSketches.dependency_source_url_key: "git://example.com/foo/bar"}], ["repository"]),
],
)
def test_sort_dependency_list(dependency_list, expected_dependency_type_list):
compile_sketches = get_compilesketches_object()
for dependency, expected_dependency_type in zip(dependency_list, expected_dependency_type_list):
assert dependency in getattr(
compile_sketches.sort_dependency_list(dependency_list=[dependency]), expected_dependency_type
)
@pytest.mark.parametrize(
"platform_list, expected_core_update_index_command_list, expected_core_install_command_list",
[
(
[
{compilesketches.CompileSketches.dependency_name_key: "Foo"},
{compilesketches.CompileSketches.dependency_name_key: "Bar"},
],
[["core", "update-index"], ["core", "update-index"]],
[["core", "install", "Foo"], ["core", "install", "Bar"]],
),
(
# Additional Board Manager URL
[
{
compilesketches.CompileSketches.dependency_name_key: "Foo",
compilesketches.CompileSketches.dependency_source_url_key: "https://example.com/package_foo_index.json",
},
{
compilesketches.CompileSketches.dependency_name_key: "Bar",
compilesketches.CompileSketches.dependency_source_url_key: "https://example.com/package_bar_index.json",
},
],
[
["core", "update-index", "--additional-urls", "https://example.com/package_foo_index.json"],
["core", "update-index", "--additional-urls", "https://example.com/package_bar_index.json"],
],
[
["core", "install", "--additional-urls", "https://example.com/package_foo_index.json", "Foo"],
["core", "install", "--additional-urls", "https://example.com/package_bar_index.json", "Bar"],
],
),
],
)
def test_install_platforms_from_board_manager(
mocker, platform_list, expected_core_update_index_command_list, expected_core_install_command_list
):
run_command_output_level = unittest.mock.sentinel.run_command_output_level
compile_sketches = get_compilesketches_object()
mocker.patch(
"compilesketches.CompileSketches.get_run_command_output_level",
autospec=True,
return_value=run_command_output_level,
)
mocker.patch("compilesketches.CompileSketches.run_arduino_cli_command", autospec=True)
compile_sketches.install_platforms_from_board_manager(platform_list=platform_list)
run_arduino_cli_command_calls = []
for expected_core_update_index_command, expected_core_install_command in zip(
expected_core_update_index_command_list, expected_core_install_command_list
):
run_arduino_cli_command_calls.extend(
[
unittest.mock.call(
compile_sketches, command=expected_core_update_index_command, enable_output=run_command_output_level
),
unittest.mock.call(
compile_sketches, command=expected_core_install_command, enable_output=run_command_output_level
),
]
)
compile_sketches.run_arduino_cli_command.assert_has_calls(calls=run_arduino_cli_command_calls)
@pytest.mark.parametrize(
"verbose, expected_output_level",
[
("true", compilesketches.CompileSketches.RunCommandOutput.ALWAYS),
("false", compilesketches.CompileSketches.RunCommandOutput.ON_FAILURE),
],
)
def test_get_run_command_output_level(verbose, expected_output_level):
compile_sketches = get_compilesketches_object(verbose=verbose)
assert compile_sketches.get_run_command_output_level() == expected_output_level
@pytest.mark.parametrize("verbose", [True, False])
def test_run_arduino_cli_command(mocker, verbose):
run_command_return = unittest.mock.sentinel.run_command_return
command = ["foo", "command"]
enable_output = unittest.mock.sentinel.enable_output
exit_on_failure = unittest.mock.sentinel.exit_on_failure
arduino_cli_installation_path = pathlib.PurePath("fooCLIinstallationPath")
compile_sketches = get_compilesketches_object()
compile_sketches.verbose = verbose
compile_sketches.arduino_cli_installation_path = arduino_cli_installation_path
mocker.patch("compilesketches.CompileSketches.run_command", autospec=True, return_value=run_command_return)
assert (
compile_sketches.run_arduino_cli_command(
command=command, enable_output=enable_output, exit_on_failure=exit_on_failure
)
== run_command_return
)
expected_run_command_command = [arduino_cli_installation_path.joinpath("arduino-cli")]
expected_run_command_command.extend(command)
if verbose:
expected_run_command_command.extend(["--log-level", "warn", "--verbose"])
compile_sketches.run_command.assert_called_once_with(
compile_sketches,
command=expected_run_command_command,
enable_output=enable_output,
exit_on_failure=exit_on_failure,
)
@pytest.mark.parametrize(
"enable_output",
[
compilesketches.CompileSketches.RunCommandOutput.NONE,
compilesketches.CompileSketches.RunCommandOutput.ON_FAILURE,
compilesketches.CompileSketches.RunCommandOutput.ALWAYS,
],
)
@pytest.mark.parametrize(
"exit_on_failure, returncode, expected_success",
[(False, 0, True), (False, 1, True), (True, 0, True), (True, 1, False)],
)
def test_run_command(capsys, mocker, enable_output, exit_on_failure, returncode, expected_success):
command = unittest.mock.sentinel.command
# Stub
class CommandData:
stdout = "foo stdout"
args = ["foo", "args"]
CommandData.returncode = returncode
command_data = CommandData()
compile_sketches = get_compilesketches_object()
mocker.patch("subprocess.run", autospec=True, return_value=command_data)
if expected_success:
run_command_output = compile_sketches.run_command(
command=command, enable_output=enable_output, exit_on_failure=exit_on_failure
)
assert run_command_output == command_data
else:
with pytest.raises(expected_exception=SystemExit, match=str(returncode)):
compile_sketches.run_command(command=command, enable_output=enable_output, exit_on_failure=exit_on_failure)
expected_output = (
"::group::Running command: "
+ " ".join(command_data.args)
+ " \n "
+ str(CommandData.stdout)
+ " \n "
+ "::endgroup::"
)
if returncode != 0 and (
enable_output == compilesketches.CompileSketches.RunCommandOutput.ON_FAILURE
or enable_output == compilesketches.CompileSketches.RunCommandOutput.ALWAYS
):
expected_output = expected_output + "\n::error::Command failed"
elif enable_output == compilesketches.CompileSketches.RunCommandOutput.ALWAYS:
expected_output = expected_output
else:
expected_output = ""
assert capsys.readouterr().out.strip() == expected_output
# noinspection PyUnresolvedReferences
subprocess.run.assert_called_once_with(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
@pytest.mark.parametrize(
"dependency, expected_name",
[
(
{
compilesketches.CompileSketches.dependency_name_key: "Foo",
compilesketches.CompileSketches.dependency_version_key: "1.2.3",
},
),
(
{
compilesketches.CompileSketches.dependency_name_key: "Foo",
compilesketches.CompileSketches.dependency_version_key: "latest",
},
"Foo",
),
({compilesketches.CompileSketches.dependency_name_key: "[email protected]"}, "[email protected]"),
({compilesketches.CompileSketches.dependency_name_key: "Foo"}, "Foo"),
],
)
def test_get_manager_dependency_name(dependency, expected_name):
compile_sketches = get_compilesketches_object()
assert compile_sketches.get_manager_dependency_name(dependency=dependency) == expected_name
@pytest.mark.parametrize(
"path_exists, platform_list",
[
(False, [{compilesketches.CompileSketches.dependency_source_path_key: pathlib.Path("Foo")}]),
(True, [{compilesketches.CompileSketches.dependency_source_path_key: pathlib.Path("Foo")}]),
],
)
def test_install_platforms_from_path(capsys, mocker, path_exists, platform_list):
class PlatformInstallationPath:
def __init__(self):
self.path = pathlib.PurePath()
self.is_overwrite = False
platform_installation_path = PlatformInstallationPath()
platform_installation_path.path = pathlib.Path("/foo/PlatformInstallationPathParent/PlatformInstallationPathName")
compile_sketches = get_compilesketches_object()
mocker.patch.object(pathlib.Path, "exists", autospec=True, return_value=path_exists)
mocker.patch(
"compilesketches.CompileSketches.get_platform_installation_path",
autospec=True,
return_value=platform_installation_path,
)
mocker.patch("compilesketches.CompileSketches.install_from_path", autospec=True)
if not path_exists:
with pytest.raises(expected_exception=SystemExit, match="1"):
compile_sketches.install_platforms_from_path(platform_list=platform_list)
assert capsys.readouterr().out.strip() == (
"::error::Platform source path: "
+ str(platform_list[0][compilesketches.CompileSketches.dependency_source_path_key])
+ " doesn't exist"
)
else:
compile_sketches.install_platforms_from_path(platform_list=platform_list)
get_platform_installation_path_calls = []
install_from_path_calls = []
for platform in platform_list:
get_platform_installation_path_calls.append(unittest.mock.call(compile_sketches, platform=platform))
install_from_path_calls.append(
unittest.mock.call(
compile_sketches,
source_path=compilesketches.absolute_path(
platform[compilesketches.CompileSketches.dependency_source_path_key]
),
destination_parent_path=platform_installation_path.path.parent,
destination_name=platform_installation_path.path.name,
force=platform_installation_path.is_overwrite,
)
)
# noinspection PyUnresolvedReferences
compile_sketches.get_platform_installation_path.assert_has_calls(calls=get_platform_installation_path_calls)
# noinspection PyUnresolvedReferences
compile_sketches.install_from_path.assert_has_calls(calls=install_from_path_calls)
@pytest.mark.parametrize(
"platform," "command_data_stdout," "expected_installation_path",
# No match to previously installed platforms
[
(
{compilesketches.CompileSketches.dependency_name_key: "foo:bar"},
'[{"ID": "asdf:zxcv"}]',
pathlib.PurePath("/foo/UserPlatformsPath/foo/bar"),
),
# Match with previously installed platform
(
{compilesketches.CompileSketches.dependency_name_key: "foo:bar"},
'[{"ID": "foo:bar", "Installed": "1.2.3"}]',
pathlib.PurePath("/foo/BoardManagerPlatformsPath/foo/hardware/bar/1.2.3"),
),
],
)
def test_get_platform_installation_path(mocker, platform, command_data_stdout, expected_installation_path):
class CommandData:
def __init__(self, stdout):
self.stdout = stdout
command_data = CommandData(stdout=command_data_stdout)
mocker.patch("compilesketches.CompileSketches.run_arduino_cli_command", autospec=True, return_value=command_data)
compile_sketches = get_compilesketches_object()
compile_sketches.user_platforms_path = pathlib.PurePath("/foo/UserPlatformsPath")
compile_sketches.board_manager_platforms_path = pathlib.PurePath("/foo/BoardManagerPlatformsPath")
platform_installation_path = compile_sketches.get_platform_installation_path(platform=platform)
assert platform_installation_path.path == expected_installation_path
run_arduino_cli_command_calls = [
unittest.mock.call(compile_sketches, command=["core", "update-index"]),
unittest.mock.call(compile_sketches, command=["core", "list", "--format", "json"]),
]
compilesketches.CompileSketches.run_arduino_cli_command.assert_has_calls(calls=run_arduino_cli_command_calls)
def test_install_platforms_from_repository(mocker):
platform_list = [
{
compilesketches.CompileSketches.dependency_source_url_key: unittest.mock.sentinel.source_url,
compilesketches.CompileSketches.dependency_source_path_key: unittest.mock.sentinel.source_path,
compilesketches.CompileSketches.dependency_destination_name_key: unittest.mock.sentinel.destination_name,
},
{compilesketches.CompileSketches.dependency_source_url_key: unittest.mock.sentinel.source_url2},
]
git_ref = unittest.mock.sentinel.git_ref
class PlatformInstallationPath:
def __init__(self):
self.path = pathlib.PurePath()
self.is_overwrite = False
platform_installation_path = PlatformInstallationPath()
platform_installation_path.path = pathlib.Path("/foo/PlatformInstallationPathParent/PlatformInstallationPathName")
expected_source_path_list = [unittest.mock.sentinel.source_path, "."]
expected_destination_name_list = [unittest.mock.sentinel.destination_name, None]
compile_sketches = get_compilesketches_object()
mocker.patch("compilesketches.CompileSketches.get_repository_dependency_ref", autospec=True, return_value=git_ref)
mocker.patch(
"compilesketches.CompileSketches.get_platform_installation_path",
autospec=True,
return_value=platform_installation_path,
)
mocker.patch("compilesketches.CompileSketches.install_from_repository", autospec=True, return_value=git_ref)
compile_sketches.install_platforms_from_repository(platform_list=platform_list)
get_repository_dependency_ref_calls = []
get_platform_installation_path_calls = []
install_from_repository_calls = []
for platform, expected_source_path, expected_destination_name in zip(
platform_list, expected_source_path_list, expected_destination_name_list
):
get_repository_dependency_ref_calls.append(unittest.mock.call(compile_sketches, dependency=platform))
get_platform_installation_path_calls.append(unittest.mock.call(compile_sketches, platform=platform))
install_from_repository_calls.append(
unittest.mock.call(
compile_sketches,
url=platform[compilesketches.CompileSketches.dependency_source_url_key],
git_ref=git_ref,
source_path=expected_source_path,
destination_parent_path=platform_installation_path.path.parent,
destination_name=platform_installation_path.path.name,
force=platform_installation_path.is_overwrite,
)
)
compile_sketches.get_repository_dependency_ref.assert_has_calls(calls=get_repository_dependency_ref_calls)
compile_sketches.install_from_repository.assert_has_calls(calls=install_from_repository_calls)
@pytest.mark.parametrize(
"dependency, expected_ref",
[({compilesketches.CompileSketches.dependency_version_key: "1.2.3"}, "1.2.3"), ({}, None)],
)
def test_get_repository_dependency_ref(dependency, expected_ref):
compile_sketches = get_compilesketches_object()
assert compile_sketches.get_repository_dependency_ref(dependency=dependency) == expected_ref
def test_install_platforms_from_download(mocker):
platform_list = [
{
compilesketches.CompileSketches.dependency_source_url_key: unittest.mock.sentinel.source_url1,
compilesketches.CompileSketches.dependency_source_path_key: unittest.mock.sentinel.source_path,
compilesketches.CompileSketches.dependency_destination_name_key: unittest.mock.sentinel.destination_name,
},
{compilesketches.CompileSketches.dependency_source_url_key: unittest.mock.sentinel.source_url2},
]