Skip to content

文件管理命令测试 tests/commands/test_filemanager.py ​

验证 objection.commands.filemanager 的全套文件操作:cd/pwd/ls/download/upload/rm,及其 iOS/Android 平台私有 helper 方法。覆盖路径校验、相对/绝对路径拼接、./.. 处理、可读性判断、目录列表渲染与下载/上传字节流。

📋 模块概览 ​

项目值
文件路径tests/commands/test_filemanager.py
被测对象objection.commands.filemanager(cd/pwd/ls/download/upload/rm 及 _ios/_android helper)
用例数40
框架pytest + unittest + mock

🎯 测试意图 ​

  • 验证 cd 对参数缺失、.、..、绝对路径、相对路径的处理,以及跨平台(Ios/Android)路径存在性检查代理。
  • 验证 pwd/pwd_print 在已设 cwd 与未设时的回退逻辑(调用 _pwd_ios/_pwd_android 并更新 file_manager_state.cwd)。
  • 验证 ls 在无参时取 pwd,分发到平台 helper,并以表格渲染目录属性(iOS NSFile* 字段、Android isDirectory/lastModified 等)。
  • 验证 download/upload 的参数校验、平台分发,及下载时对可读性、文件类型(folder 需 --folder)的检查与字节写入。
  • 验证 rm 的确认交互(click.confirm)与 Android helper 删除调用。

🧪 用例清单 ​

用例行号验证点
test_cd_argument_validation15无参输出 Usage
test_cd_to_dot_directory_does_nothing21cd . 不变 cwd
test_cd_to_double_dot_moves_up_one_directory28cd .. 上一级并打印新路径
test_cd_to_double_dot_moves_stays_in_current_directory_if_already_root37根目录 cd .. 保持 /
test_cd_to_absoluate_ios_path47iOS 绝对路径设置成功
test_cd_to_absoluate_android_path60Android 绝对路径设置成功
test_cd_to_absoluate_ios_path_that_does_not_exist73不存在路径报 Invalid
test_cd_to_relative_path_ios86iOS 相对路径拼接
test_cd_to_relative_path_android99Android 相对路径拼接
test_cd_to_relative_path_ios_that_does_not_exist112相对路径不存在报错
test_ios_path_exists_helper125_path_exists_ios 调 ios_file_exists
test_rm_dispatcher_validates_arguments130rm 无参 Usage
test_rm_dispatcher_confirms_before_delete140confirm=False 不删除
test_rm_dispatcher_calls_android_rm_helper155confirm=True 调 _rm_android
test_rm_android_helper_file_exists166_rm_android 文件存在时删除
test_android_path_exists_helper178_path_exists_android 调 android_file_exists
test_returns_current_directory_via_helper_when_already_set183cwd 已设时 pwd 直接返回
test_returns_current_directory_via_helper_for_ios189iOS 平台调 _pwd_ios
test_returns_current_directory_via_helper_for_android197Android 平台调 _pwd_android
test_prints_the_current_working_directory204pwd_print 输出当前目录
test_get_ios_pwd_via_helper213_pwd_ios 调 ios_file_cwd 并更新 state
test_get_android_pwd_via_helper220_pwd_android 调 android_file_cwd 并更新 state
test_ls_gets_pwd_from_helper_with_no_argument228ls 无参先取 pwd
test_ls_calls_ios_helper_method236iOS 平台调 _ls_ios
test_ls_calls_android_helper_method244Android 平台调 _ls_android
test_lists_readable_ios_directory_using_helper_method252iOS 目录渲染含 NSFile 字段与大小
test_lists_readable_ios_directory_using_helper_method_no_attributes288属性为空时显示 n/a
test_lists_unreadable_ios_directory_using_helper_method313不可读时只输出 Readable/Writable
test_lists_readable_android_directory_using_helper_method327Android 目录渲染含 Type/Size/时间
test_lists_unreadable_android_directory_using_helper_method358Android 不可读只输出状态
test_download_platform_proxy_validates_arguments371download 无参 Usage
test_download_platform_proxy_calls_ios_method378iOS 平台调 _download_ios
test_download_platform_proxy_calls_android_method386Android 平台调 _download_android
test_downloads_file_with_ios_helper395iOS 下载写盘并打印流程
test_downloads_file_but_fails_on_unreadable_with_ios_helper415iOS 不可读报错
test_downloads_file_but_fails_on_file_type_with_ios_helper424iOS 非文件提示 --folder
test_downloads_file_with_android_helper435Android 下载写盘
test_downloads_file_but_fails_on_unreadable_with_android_helper456Android 不可读报错
test_downloads_file_but_fails_on_file_type_with_android_helper469Android 非文件提示 --folder
test_file_upload_method_proxy_validates_arguments481upload 无参 Usage
test_file_upload_method_proxy_calls_ios_helper_method488iOS 平台调 _upload_ios
test_file_upload_method_proxy_calls_android_helper_method496Android 平台调 _upload_android

⚙️ 测试手法 ​

平台分发用 device_state.platform = Ios/Android 设置当前平台。API 注入统一走 @mock.patch('objection.state.connection.state_connection.get_api'),对 ios_file_*/android_file_* 系列方法设返回值。目录列表用例采用 assertIn 逐 token 断言关键字段而非锁定 tabulate 列宽(见 :281 注释)。下载用例以 @mock.patch('objection.commands.filemanager.open', create=True) mock 内建 open 验证写盘。rm 用 click.confirm mock 控制确认分支。每个用例 tearDown 重置 file_manager_state.cwd = None。

关键代码 tests/commands/test_filemanager.py:252 渲染断言片段:

python
for token in ('NSFileType', 'Perms', 'NSFileProtection', 'Read', 'Write',
              'Owner', 'Group', 'Size', 'Creation', 'Name', ...):
    self.assertIn(token, output)

🔍 源码索引 ​

用例位置
test_cd_argument_validationtests/commands/test_filemanager.py:15
test_cd_to_dot_directory_does_nothingtests/commands/test_filemanager.py:21
test_cd_to_double_dot_moves_up_one_directorytests/commands/test_filemanager.py:28
test_cd_to_double_dot_moves_stays_in_current_directory_if_already_roottests/commands/test_filemanager.py:37
test_cd_to_absoluate_ios_pathtests/commands/test_filemanager.py:47
test_cd_to_absoluate_android_pathtests/commands/test_filemanager.py:60
test_cd_to_absoluate_ios_path_that_does_not_existtests/commands/test_filemanager.py:73
test_cd_to_relative_path_iostests/commands/test_filemanager.py:86
test_cd_to_relative_path_androidtests/commands/test_filemanager.py:99
test_cd_to_relative_path_ios_that_does_not_existtests/commands/test_filemanager.py:112
test_ios_path_exists_helpertests/commands/test_filemanager.py:125
test_rm_dispatcher_validates_argumentstests/commands/test_filemanager.py:130
test_rm_dispatcher_confirms_before_deletetests/commands/test_filemanager.py:140
test_rm_dispatcher_calls_android_rm_helpertests/commands/test_filemanager.py:155
test_rm_android_helper_file_existstests/commands/test_filemanager.py:166
test_android_path_exists_helpertests/commands/test_filemanager.py:178
test_returns_current_directory_via_helper_when_already_settests/commands/test_filemanager.py:183
test_returns_current_directory_via_helper_for_iostests/commands/test_filemanager.py:189
test_returns_current_directory_via_helper_for_androidtests/commands/test_filemanager.py:197
test_prints_the_current_working_directorytests/commands/test_filemanager.py:204
test_get_ios_pwd_via_helpertests/commands/test_filemanager.py:213
test_get_android_pwd_via_helpertests/commands/test_filemanager.py:220
test_ls_gets_pwd_from_helper_with_no_argumenttests/commands/test_filemanager.py:228
test_ls_calls_ios_helper_methodtests/commands/test_filemanager.py:236
test_ls_calls_android_helper_methodtests/commands/test_filemanager.py:244
test_lists_readable_ios_directory_using_helper_methodtests/commands/test_filemanager.py:252
test_lists_readable_ios_directory_using_helper_method_no_attributestests/commands/test_filemanager.py:288
test_lists_unreadable_ios_directory_using_helper_methodtests/commands/test_filemanager.py:313
test_lists_readable_android_directory_using_helper_methodtests/commands/test_filemanager.py:327
test_lists_unreadable_android_directory_using_helper_methodtests/commands/test_filemanager.py:358
test_download_platform_proxy_validates_argumentstests/commands/test_filemanager.py:371
test_download_platform_proxy_calls_ios_methodtests/commands/test_filemanager.py:378
test_download_platform_proxy_calls_android_methodtests/commands/test_filemanager.py:386
test_downloads_file_with_ios_helpertests/commands/test_filemanager.py:395
test_downloads_file_but_fails_on_unreadable_with_ios_helpertests/commands/test_filemanager.py:415
test_downloads_file_but_fails_on_file_type_with_ios_helpertests/commands/test_filemanager.py:424
test_downloads_file_with_android_helpertests/commands/test_filemanager.py:435
test_downloads_file_but_fails_on_unreadable_with_android_helpertests/commands/test_filemanager.py:456
test_downloads_file_but_fails_on_file_type_with_android_helpertests/commands/test_filemanager.py:469
test_file_upload_method_proxy_validates_argumentstests/commands/test_filemanager.py:481
test_file_upload_method_proxy_calls_ios_helper_methodtests/commands/test_filemanager.py:488
test_file_upload_method_proxy_calls_android_helper_methodtests/commands/test_filemanager.py:496

🔗 相关文档 ​

基于 GPL-3.0-or-later 许可发布