22import os
33import shutil
44import traceback
5- from typing import List , Protocol , Sequence , Tuple , TypeVar
5+ from functools import partial
6+ from typing import IO , Any , Callable , List , Mapping , Protocol , Sequence , Tuple , TypeVar
67
7- import vcr
88from click .testing import CliRunner , Result
9+ from vcr import VCR
910
11+ from foundation .core import compose , flip , partial_1_1 , partial_2_1
1012from icloudpd .base import main
1113
14+ vcr = VCR (decode_compressed_response = True , record_mode = "none" )
1215
13- def print_result_exception (result : Result ) -> None :
16+
17+ def print_result_exception (result : Result ) -> Result :
1418 ex = result .exception
1519 if ex :
1620 # This only works on Python 3
1721 if hasattr (ex , "__traceback__" ):
1822 traceback .print_exception (type (ex ), value = ex , tb = ex .__traceback__ )
1923 else :
2024 print (ex )
25+ return result
2126
2227
2328def path_from_project_root (file_name : str ) -> str :
@@ -59,6 +64,8 @@ def combine_file_lists(
5964
6065
6166_T = TypeVar ("_T" )
67+ _T_co = TypeVar ("_T_co" , covariant = True )
68+ _T_contra = TypeVar ("_T_contra" , contravariant = True )
6269
6370
6471class AssertEquality (Protocol ):
@@ -82,16 +89,39 @@ def assert_files(
8289 )
8390
8491
85- def run_cassette (cassette_path : str , params : Sequence [str ]) -> Result :
92+ DEFAULT_ENV : Mapping [str , str | None ] = {"CLIENT_ID" : "DE309E26-942E-11E8-92F5-14109FE0B321" }
93+
94+
95+ def run_main_env (
96+ env : Mapping [str , str | None ], params : Sequence [str ], input : str | bytes | IO [Any ] | None = None
97+ ) -> Result :
98+ runner = CliRunner (env = env )
99+ result = runner .invoke (main , params , input )
100+ return result
101+
102+
103+ run_main : Callable [[Sequence [str ]], Result ] = compose (
104+ print_result_exception , partial_1_1 (run_main_env , DEFAULT_ENV )
105+ )
106+
107+
108+ def run_with_cassette (cassette_path : str , f : Callable [[_T_contra ], _T_co ], inp : _T_contra ) -> _T_co :
86109 with vcr .use_cassette (cassette_path ):
87- # Pass fixed client ID via environment variable
88- runner = CliRunner (env = {"CLIENT_ID" : "DE309E26-942E-11E8-92F5-14109FE0B321" })
89- result = runner .invoke (
90- main ,
91- params ,
92- )
93- print_result_exception (result )
94- return result
110+ return f (inp )
111+
112+
113+ def run_cassette (
114+ cassette_path : str , params : Sequence [str ], input : str | bytes | IO [Any ] | None = None
115+ ) -> Result :
116+ with vcr .use_cassette (cassette_path ):
117+ return print_result_exception (run_main_env (DEFAULT_ENV , params , input ))
118+
119+
120+ _path_join_flipped = flip (os .path .join )
121+
122+ calc_data_dir = partial_1_1 (_path_join_flipped , "data" )
123+ calc_cookie_dir = partial_1_1 (_path_join_flipped , "cookie" )
124+ calc_vcr_dir = partial_1_1 (_path_join_flipped , "vcr_cassettes" )
95125
96126
97127def run_icloudpd_test (
@@ -102,11 +132,13 @@ def run_icloudpd_test(
102132 files_to_create : Sequence [Tuple [str , str , int ]],
103133 files_to_download : List [Tuple [str , str ]],
104134 params : List [str ],
135+ additional_env : Mapping [str , str | None ] = {},
136+ input : str | bytes | IO [Any ] | None = None ,
105137) -> Tuple [str , Result ]:
106- cookie_dir = os . path . join (base_dir , "cookie" )
107- data_dir = os . path . join (base_dir , "data" )
108- vcr_path = os . path . join (root_path , "vcr_cassettes" )
109- cookie_master_path = os . path . join (root_path , "cookie" )
138+ cookie_dir = calc_cookie_dir (base_dir )
139+ data_dir = calc_data_dir (base_dir )
140+ vcr_path = calc_vcr_dir (root_path )
141+ cookie_master_path = calc_cookie_dir (root_path )
110142
111143 for dir in [base_dir , data_dir ]:
112144 recreate_path (dir )
@@ -115,17 +147,22 @@ def run_icloudpd_test(
115147
116148 create_files (data_dir , files_to_create )
117149
118- result = run_cassette (
119- os .path .join (vcr_path , cassette_filename ),
120- [
121- "-d" ,
122- data_dir ,
123- "--cookie-directory" ,
124- cookie_dir ,
125- ]
126- + params ,
150+ combined_env : Mapping [str , str | None ] = {** additional_env , ** DEFAULT_ENV }
151+
152+ main_runner = compose (print_result_exception , partial (run_main_env , combined_env , input = input ))
153+
154+ with_cassette_main_runner = partial_2_1 (
155+ run_with_cassette , os .path .join (vcr_path , cassette_filename ), main_runner
127156 )
128157
158+ combined_params = [
159+ "-d" ,
160+ data_dir ,
161+ "--cookie-directory" ,
162+ cookie_dir ,
163+ ] + params
164+
165+ result = with_cassette_main_runner (combined_params )
129166 files_to_assert = combine_file_lists (files_to_create , files_to_download )
130167 assert_files (assert_equal , data_dir , files_to_assert )
131168
0 commit comments