@@ -638,8 +638,7 @@ def test_install_platforms_from_path(capsys, mocker, path_exists, platform_list)
638
638
mocker .patch ("compilesketches.CompileSketches.get_platform_installation_path" ,
639
639
autospec = True ,
640
640
return_value = platform_installation_path )
641
- mocker .patch .object (pathlib .Path , "mkdir" , autospec = True )
642
- mocker .patch .object (pathlib .Path , "symlink_to" , autospec = True )
641
+ mocker .patch ("compilesketches.install_from_path" , autospec = True )
643
642
644
643
if not path_exists :
645
644
with pytest .raises (expected_exception = SystemExit , match = "1" ):
@@ -655,24 +654,23 @@ def test_install_platforms_from_path(capsys, mocker, path_exists, platform_list)
655
654
compile_sketches .install_platforms_from_path (platform_list = platform_list )
656
655
657
656
get_platform_installation_path_calls = []
658
- mkdir_calls = []
659
- symlink_to_calls = []
657
+ install_from_path_calls = []
660
658
for platform in platform_list :
661
659
get_platform_installation_path_calls .append (unittest .mock .call (compile_sketches , platform = platform ))
662
- mkdir_calls .append (unittest .mock .call (platform_installation_path .parent , parents = True , exist_ok = True ))
663
- symlink_to_calls .append (
660
+ install_from_path_calls .append (
664
661
unittest .mock .call (
665
- platform_installation_path ,
666
- target = compilesketches .absolute_path (
662
+ source_path = compilesketches .absolute_path (
667
663
platform [compilesketches .CompileSketches .dependency_source_path_key ]
668
664
),
669
- target_is_directory = True
665
+ destination_parent_path = platform_installation_path .parent ,
666
+ destination_name = platform_installation_path .name
670
667
)
671
668
)
672
669
673
670
# noinspection PyUnresolvedReferences
674
- pathlib .Path .mkdir .assert_has_calls (calls = mkdir_calls )
675
- pathlib .Path .symlink_to .assert_has_calls (calls = symlink_to_calls )
671
+ compile_sketches .get_platform_installation_path .assert_has_calls (calls = get_platform_installation_path_calls )
672
+ # noinspection PyUnresolvedReferences
673
+ compilesketches .install_from_path .assert_has_calls (calls = install_from_path_calls )
676
674
677
675
678
676
@pytest .mark .parametrize (
@@ -937,22 +935,19 @@ def test_install_libraries_from_library_manager(mocker):
937
935
["FooRepoName" ]),
938
936
(True ,
939
937
[{compilesketches .CompileSketches .dependency_source_path_key : os .environ ["GITHUB_WORKSPACE" ] + "/Bar" }],
940
- ["Bar" ])]
938
+ [None ])]
941
939
)
942
940
def test_install_libraries_from_path (capsys , monkeypatch , mocker , path_exists , library_list ,
943
941
expected_destination_name_list ):
944
942
libraries_path = pathlib .Path ("/foo/LibrariesPath" )
945
- symlink_source_path = pathlib .Path ("/foo/SymlinkSourcePath" )
946
943
947
944
monkeypatch .setenv ("GITHUB_REPOSITORY" , "foo/FooRepoName" )
948
945
949
946
compile_sketches = get_compilesketches_object ()
950
947
compile_sketches .libraries_path = libraries_path
951
948
952
949
mocker .patch .object (pathlib .Path , "exists" , autospec = True , return_value = path_exists )
953
- mocker .patch .object (pathlib .Path , "mkdir" , autospec = True )
954
- mocker .patch .object (pathlib .Path , "joinpath" , autospec = True , return_value = symlink_source_path )
955
- mocker .patch .object (pathlib .Path , "symlink_to" , autospec = True )
950
+ mocker .patch ("compilesketches.install_from_path" , autospec = True )
956
951
957
952
if not path_exists :
958
953
with pytest .raises (expected_exception = SystemExit , match = "1" ):
@@ -966,23 +961,20 @@ def test_install_libraries_from_path(capsys, monkeypatch, mocker, path_exists, l
966
961
else :
967
962
compile_sketches .install_libraries_from_path (library_list = library_list )
968
963
969
- mkdir_calls = []
970
- joinpath_calls = []
971
- symlink_to_calls = []
964
+ install_from_path_calls = []
972
965
for library , expected_destination_name in zip (library_list , expected_destination_name_list ):
973
- mkdir_calls .append (unittest .mock .call (libraries_path , parents = True , exist_ok = True ))
974
- joinpath_calls .append (unittest .mock .call (libraries_path , expected_destination_name ))
975
- symlink_to_calls .append (
976
- unittest .mock .call (symlink_source_path ,
977
- target = compilesketches .absolute_path (
978
- library [compilesketches .CompileSketches .dependency_source_path_key ]),
979
- target_is_directory = True ))
966
+ install_from_path_calls .append (
967
+ unittest .mock .call (
968
+ source_path = compilesketches .absolute_path (
969
+ library [compilesketches .CompileSketches .dependency_source_path_key ]
970
+ ),
971
+ destination_parent_path = libraries_path ,
972
+ destination_name = expected_destination_name
973
+ )
974
+ )
980
975
981
976
# noinspection PyUnresolvedReferences
982
- pathlib .Path .mkdir .assert_has_calls (calls = mkdir_calls )
983
- # noinspection PyUnresolvedReferences
984
- pathlib .Path .joinpath .assert_has_calls (calls = joinpath_calls )
985
- pathlib .Path .symlink_to .assert_has_calls (calls = symlink_to_calls )
977
+ compilesketches .install_from_path .assert_has_calls (calls = install_from_path_calls )
986
978
987
979
988
980
def test_install_libraries_from_repository (mocker ):
@@ -1121,6 +1113,87 @@ def test_get_list_from_multiformat_input(input_value, expected_list, expected_wa
1121
1113
assert input_list .was_yaml_list == expected_was_yaml_list
1122
1114
1123
1115
1116
+ # noinspection PyUnresolvedReferences
1117
+ @pytest .mark .parametrize (
1118
+ "source_path, destination_parent_path, destination_name, expected_destination_path" ,
1119
+ [(pathlib .Path ("foo/source-path" ),
1120
+ pathlib .Path ("bar/destination-parent-path" ),
1121
+ None ,
1122
+ pathlib .Path ("bar/destination-parent-path/source-path" )),
1123
+ (pathlib .Path ("foo/source-path" ),
1124
+ pathlib .Path ("bar/destination-parent-path" ),
1125
+ "destination-name" ,
1126
+ pathlib .Path ("bar/destination-parent-path/destination-name" ))])
1127
+ @pytest .mark .parametrize ("is_dir" , [True , False ])
1128
+ def test_install_from_path (mocker ,
1129
+ source_path ,
1130
+ destination_parent_path ,
1131
+ destination_name ,
1132
+ expected_destination_path ,
1133
+ is_dir ):
1134
+ mocker .patch .object (pathlib .Path , "mkdir" , autospec = True )
1135
+ mocker .patch .object (pathlib .Path , "is_dir" , autospec = True , return_value = is_dir )
1136
+ mocker .patch ("shutil.copytree" , autospec = True )
1137
+ mocker .patch ("shutil.copy" , autospec = True )
1138
+
1139
+ compilesketches .install_from_path (source_path = source_path , destination_parent_path = destination_parent_path ,
1140
+ destination_name = destination_name )
1141
+ if is_dir :
1142
+ shutil .copytree .assert_called_once_with (src = source_path , dst = expected_destination_path )
1143
+ else :
1144
+ shutil .copy .assert_called_once_with (src = source_path , dst = expected_destination_path )
1145
+
1146
+
1147
+ def test_install_from_path_functional (tmp_path ):
1148
+ source_path = tmp_path .joinpath ("foo-source" )
1149
+ test_file_path = source_path .joinpath ("foo-test-file" )
1150
+ destination_parent_path = tmp_path .joinpath ("foo-destination-parent" )
1151
+
1152
+ def prep_test_folders ():
1153
+ shutil .rmtree (path = source_path , ignore_errors = True )
1154
+ source_path .mkdir (parents = True )
1155
+ test_file_path .write_text ("foo test file contents" )
1156
+ shutil .rmtree (path = destination_parent_path , ignore_errors = True )
1157
+ # destination_parent_path is created by install_from_path()
1158
+
1159
+ # Test install of folder
1160
+ # Test naming according to source
1161
+ # Test existing destination_parent_path
1162
+ prep_test_folders ()
1163
+ destination_parent_path .mkdir (parents = True )
1164
+ compilesketches .install_from_path (source_path = source_path , destination_parent_path = destination_parent_path ,
1165
+ destination_name = None )
1166
+ assert directories_are_same (left_directory = source_path ,
1167
+ right_directory = destination_parent_path .joinpath (source_path .name ))
1168
+
1169
+ # Test custom folder name
1170
+ prep_test_folders ()
1171
+ destination_name = "foo-destination-name"
1172
+ compilesketches .install_from_path (source_path = source_path ,
1173
+ destination_parent_path = destination_parent_path ,
1174
+ destination_name = destination_name )
1175
+ assert directories_are_same (left_directory = source_path ,
1176
+ right_directory = destination_parent_path .joinpath (destination_name ))
1177
+
1178
+ # Test install of file
1179
+ # Test naming according to source
1180
+ prep_test_folders ()
1181
+ compilesketches .install_from_path (source_path = test_file_path ,
1182
+ destination_parent_path = destination_parent_path ,
1183
+ destination_name = None )
1184
+ assert filecmp .cmp (f1 = test_file_path ,
1185
+ f2 = destination_parent_path .joinpath (test_file_path .name ))
1186
+
1187
+ # Test custom folder name
1188
+ prep_test_folders ()
1189
+ destination_name = "foo-destination-name"
1190
+ compilesketches .install_from_path (source_path = test_file_path ,
1191
+ destination_parent_path = destination_parent_path ,
1192
+ destination_name = destination_name )
1193
+ assert filecmp .cmp (f1 = test_file_path ,
1194
+ f2 = destination_parent_path .joinpath (destination_name ))
1195
+
1196
+
1124
1197
def test_path_is_sketch ():
1125
1198
# Sketch file
1126
1199
assert compilesketches .path_is_sketch (path = test_data_path .joinpath ("HasSketches" , "Sketch1" , "Sketch1.ino" )) is True
@@ -2156,10 +2229,10 @@ def test_get_archive_root_path(archive_extract_path, expected_archive_root_path)
2156
2229
[("https://example.com/foo/FooRepositoryName.git" , "." , None , "FooRepositoryName" ),
2157
2230
("https://example.com/foo/FooRepositoryName.git/" , "./examples" , "FooDestinationName" ,
2158
2231
"FooDestinationName" ),
2159
- ("git://example.com/foo/FooRepositoryName" , "examples" , None , "examples" )])
2232
+ ("git://example.com/foo/FooRepositoryName" , "examples" , None , None )])
2160
2233
def test_install_from_repository (mocker , url , source_path , destination_name , expected_destination_name ):
2161
2234
git_ref = unittest .mock .sentinel .git_ref
2162
- destination_parent_path = pathlib . PurePath ( "/foo/DestinationParentPath" )
2235
+ destination_parent_path = unittest . mock . sentinel . destination_parent_path
2163
2236
clone_path = pathlib .PurePath ("/foo/ClonePath" )
2164
2237
2165
2238
# Stub
@@ -2176,7 +2249,7 @@ def __exit__(self, *exc):
2176
2249
mocker .patch ("tempfile.TemporaryDirectory" , autospec = True ,
2177
2250
return_value = TemporaryDirectory (temporary_directory = clone_path ))
2178
2251
mocker .patch ("compilesketches.CompileSketches.clone_repository" , autospec = True )
2179
- mocker .patch ("shutil.move " , autospec = True )
2252
+ mocker .patch ("compilesketches.install_from_path " , autospec = True )
2180
2253
2181
2254
compile_sketches = get_compilesketches_object ()
2182
2255
@@ -2186,21 +2259,17 @@ def __exit__(self, *exc):
2186
2259
destination_parent_path = destination_parent_path ,
2187
2260
destination_name = destination_name )
2188
2261
2189
- if source_path .rstrip ("/" ) == "." :
2190
- compile_sketches .clone_repository .assert_called_once_with (compile_sketches ,
2191
- url = url ,
2192
- git_ref = git_ref ,
2193
- destination_path = destination_parent_path .joinpath (
2194
- expected_destination_name ))
2195
- else :
2196
- tempfile .TemporaryDirectory .assert_called_once ()
2197
- compile_sketches .clone_repository .assert_called_once_with (compile_sketches ,
2198
- url = url ,
2199
- git_ref = git_ref ,
2200
- destination_path = clone_path )
2201
- # noinspection PyUnresolvedReferences
2202
- shutil .move .assert_called_once_with (src = str (clone_path .joinpath (source_path )),
2203
- dst = str (destination_parent_path .joinpath (expected_destination_name )))
2262
+ tempfile .TemporaryDirectory .assert_called_once ()
2263
+ compile_sketches .clone_repository .assert_called_once_with (compile_sketches ,
2264
+ url = url ,
2265
+ git_ref = git_ref ,
2266
+ destination_path = clone_path )
2267
+ # noinspection PyUnresolvedReferences
2268
+ compilesketches .install_from_path .assert_called_once_with (
2269
+ source_path = clone_path .joinpath (source_path ),
2270
+ destination_parent_path = destination_parent_path ,
2271
+ destination_name = expected_destination_name
2272
+ )
2204
2273
2205
2274
2206
2275
@pytest .mark .parametrize ("git_ref" , ["v1.0.2" , "latest" , None ])
0 commit comments