|
1 | 1 | import sys |
2 | 2 |
|
| 3 | +import pytest |
| 4 | + |
3 | 5 | from crawler_to_md import cli |
4 | 6 | from crawler_to_md.export_manager import ExportManager |
5 | 7 | from crawler_to_md.scraper import Scraper |
@@ -82,6 +84,42 @@ def fake_init( |
82 | 84 | assert captured.get('proxy') == 'http://proxy:8080' |
83 | 85 |
|
84 | 86 |
|
| 87 | +def test_cli_proxy_short_option(monkeypatch, tmp_path): |
| 88 | + captured = {} |
| 89 | + |
| 90 | + def fake_init( |
| 91 | + self, |
| 92 | + base_url, |
| 93 | + exclude_patterns, |
| 94 | + db_manager, |
| 95 | + rate_limit=0, |
| 96 | + delay=0, |
| 97 | + proxy=None, |
| 98 | + ): |
| 99 | + captured['proxy'] = proxy |
| 100 | + |
| 101 | + monkeypatch.setattr(Scraper, '__init__', fake_init) |
| 102 | + monkeypatch.setattr(Scraper, 'start_scraping', lambda *a, **k: None) |
| 103 | + monkeypatch.setattr(ExportManager, 'export_to_markdown', lambda *a, **k: None) |
| 104 | + monkeypatch.setattr(ExportManager, 'export_to_json', lambda *a, **k: None) |
| 105 | + |
| 106 | + cache_folder = tmp_path / 'cache' |
| 107 | + args = [ |
| 108 | + 'prog', |
| 109 | + '--url', |
| 110 | + 'http://example.com', |
| 111 | + '--output-folder', |
| 112 | + str(tmp_path), |
| 113 | + '--cache-folder', |
| 114 | + str(cache_folder), |
| 115 | + '-p', |
| 116 | + 'http://proxy:8080', |
| 117 | + ] |
| 118 | + monkeypatch.setattr(sys, 'argv', args) |
| 119 | + cli.main() |
| 120 | + assert captured.get('proxy') == 'http://proxy:8080' |
| 121 | + |
| 122 | + |
85 | 123 | def test_cli_socks_proxy(monkeypatch, tmp_path): |
86 | 124 | captured = {} |
87 | 125 |
|
@@ -117,3 +155,25 @@ def fake_init( |
117 | 155 | cli.main() |
118 | 156 | assert captured.get('proxy') == 'socks5://localhost:9050' |
119 | 157 |
|
| 158 | + |
| 159 | +def test_cli_proxy_error(monkeypatch, tmp_path): |
| 160 | + def fake_init(*a, **k): |
| 161 | + raise ValueError('Proxy unreachable') |
| 162 | + |
| 163 | + monkeypatch.setattr(Scraper, '__init__', fake_init) |
| 164 | + cache_folder = tmp_path / 'cache' |
| 165 | + args = [ |
| 166 | + 'prog', |
| 167 | + '--url', |
| 168 | + 'http://example.com', |
| 169 | + '--output-folder', |
| 170 | + str(tmp_path), |
| 171 | + '--cache-folder', |
| 172 | + str(cache_folder), |
| 173 | + '--proxy', |
| 174 | + 'http://proxy:8080', |
| 175 | + ] |
| 176 | + monkeypatch.setattr(sys, 'argv', args) |
| 177 | + with pytest.raises(SystemExit): |
| 178 | + cli.main() |
| 179 | + |
0 commit comments