44
55import pytest
66from click .testing import CliRunner
7- from conftest import TestHandler
87
98import unblob .cli
109from unblob .extractors import Command
1110from unblob .handlers import BUILTIN_HANDLERS
11+ from unblob .models import Handler , HexString
1212from unblob .processing import DEFAULT_DEPTH , DEFAULT_PROCESS_NUM , ExtractionConfig
1313
1414
15+ class TestHandler (Handler ):
16+ NAME = "test_handler"
17+ PATTERNS = [HexString ("21 3C" )]
18+ EXTRACTOR = Command ("testcommand" , "for" , "test" , "handler" )
19+
20+ def calculate_chunk (self , * args , ** kwargs ):
21+ pass
22+
23+
1524class ExistingCommandHandler (TestHandler ):
1625 EXTRACTOR = Command ("sh" , "something" )
1726
@@ -86,7 +95,7 @@ def test_help(params):
8695 result = runner .invoke (unblob .cli .cli , params )
8796 assert result .exit_code == 0
8897 # NOTE: In practice, it writes "Usage: unblob ...", this is done in the `cli.main` with `click.make_context`
89- assert result .output .startswith ("Usage: cli [OPTIONS] FILES... " )
98+ assert result .output .startswith ("Usage: cli [OPTIONS] FILE " )
9099
91100
92101@pytest .mark .parametrize (
@@ -118,19 +127,19 @@ def test_without_file(params: List[str]):
118127 runner = CliRunner ()
119128 result = runner .invoke (unblob .cli .cli , params )
120129 assert result .exit_code == 2
121- assert "Missing argument 'FILES... '" in result .output
130+ assert "Missing argument 'FILE '" in result .output
122131
123132
124133def test_non_existing_file (tmp_path : Path ):
125134 runner = CliRunner ()
126135 path = Path ("non/existing/path/54" )
127136 result = runner .invoke (unblob .cli .cli , ["--extract-dir" , str (tmp_path ), str (path )])
128137 assert result .exit_code == 2
129- assert "Invalid value for 'FILES... '" in result .output
130- assert f"Path '{ str (path )} ' does not exist" in result .output
138+ assert "Invalid value for 'FILE '" in result .output
139+ assert f"File '{ str (path )} ' does not exist" in result .output
131140
132141
133- def test_empty_dir_as_file (tmp_path : Path ):
142+ def test_dir_for_file (tmp_path : Path ):
134143 runner = CliRunner ()
135144 out_path = tmp_path .joinpath ("out" )
136145 out_path .mkdir ()
@@ -139,7 +148,7 @@ def test_empty_dir_as_file(tmp_path: Path):
139148 result = runner .invoke (
140149 unblob .cli .cli , ["--extract-dir" , str (out_path ), str (in_path )]
141150 )
142- assert result .exit_code = = 0
151+ assert result .exit_code ! = 0
143152
144153
145154@pytest .mark .parametrize (
@@ -172,13 +181,14 @@ def test_archive_success(
172181 / "archive"
173182 / "zip"
174183 / "regular"
175- / "__input__/"
184+ / "__input__"
185+ / "apple.zip"
176186 )
177- process_files_mock = mock .MagicMock ()
187+ process_file_mock = mock .MagicMock ()
178188 logger_config_mock = mock .MagicMock ()
179189 new_params = params + ["--extract-dir" , str (tmp_path ), str (in_path )]
180190 with mock .patch .object (
181- unblob .cli , "process_files " , process_files_mock
191+ unblob .cli , "process_file " , process_file_mock
182192 ), mock .patch .object (unblob .cli , "configure_logger" , logger_config_mock ):
183193 result = runner .invoke (unblob .cli .cli , new_params )
184194 assert result .exit_code == 0
@@ -192,7 +202,7 @@ def test_archive_success(
192202 process_num = expected_process_num ,
193203 handlers = BUILTIN_HANDLERS ,
194204 )
195- process_files_mock .assert_called_once_with (config , in_path )
205+ process_file_mock .assert_called_once_with (config , in_path , None )
196206 logger_config_mock .assert_called_once_with (expected_verbosity , tmp_path )
197207
198208
@@ -214,17 +224,18 @@ def test_keep_extracted_chunks(
214224 / "archive"
215225 / "zip"
216226 / "regular"
217- / "__input__/"
227+ / "__input__"
228+ / "apple.zip"
218229 )
219230 params = args + ["--extract-dir" , str (tmp_path ), str (in_path )]
220231
221- process_files_mock = mock .MagicMock ()
222- with mock .patch .object (unblob .cli , "process_files " , process_files_mock ):
232+ process_file_mock = mock .MagicMock ()
233+ with mock .patch .object (unblob .cli , "process_file " , process_file_mock ):
223234 result = runner .invoke (unblob .cli .cli , params )
224235
225236 assert result .exit_code == 0
226- process_files_mock .assert_called_once ()
237+ process_file_mock .assert_called_once ()
227238 assert (
228- process_files_mock .call_args .args [0 ].keep_extracted_chunks
239+ process_file_mock .call_args .args [0 ].keep_extracted_chunks
229240 == keep_extracted_chunks
230241 ), fail_message
0 commit comments