|
16 | 16 | # You should have received a copy of the GNU General Public License |
17 | 17 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
18 | 18 |
|
19 | | -import pytest |
| 19 | +import sys |
| 20 | +import subprocess |
20 | 21 | import pathlib |
21 | 22 |
|
| 23 | +import pytest |
| 24 | + |
22 | 25 | from libzim.writer import Article, Blob, Creator |
23 | 26 | from libzim.reader import File |
24 | 27 |
|
@@ -188,3 +191,65 @@ def test_filename_param_types(tmpdir): |
188 | 191 | with Creator(str(path), "welcome") as creator: |
189 | 192 | assert creator.filename == path |
190 | 193 | assert isinstance(creator.filename, pathlib.Path) |
| 194 | + |
| 195 | + |
| 196 | +def test_in_article_exceptions(tmpdir): |
| 197 | + """ make sure we raise RuntimeError from article's virtual methods """ |
| 198 | + |
| 199 | + class BoolErrorArticle(SimpleArticle): |
| 200 | + def is_redirect(self): |
| 201 | + raise RuntimeError("OUPS Redirect") |
| 202 | + |
| 203 | + class StringErrorArticle(SimpleArticle): |
| 204 | + def get_url(self): |
| 205 | + raise IOError |
| 206 | + |
| 207 | + class BlobErrorArticle(SimpleArticle): |
| 208 | + def get_data(self): |
| 209 | + raise IOError |
| 210 | + |
| 211 | + path, main_page = tmpdir / "test.zim", "welcome" |
| 212 | + args = {"title": "Hello", "mime_type": "text/html", "content": "", "url": "welcome"} |
| 213 | + |
| 214 | + with Creator(path, main_page) as zim_creator: |
| 215 | + # make sure we can can exception of all types (except int, not used) |
| 216 | + with pytest.raises(RuntimeError, match="OUPS Redirect"): |
| 217 | + zim_creator.add_article(BoolErrorArticle(**args)) |
| 218 | + with pytest.raises(RuntimeError, match="in get_url"): |
| 219 | + zim_creator.add_article(StringErrorArticle(**args)) |
| 220 | + with pytest.raises(RuntimeError, match="IOError"): |
| 221 | + zim_creator.add_article(BlobErrorArticle(**args)) |
| 222 | + with pytest.raises(RuntimeError, match="NotImplementedError"): |
| 223 | + zim_creator.add_article(Article()) |
| 224 | + |
| 225 | + # make sure we can catch it from outside creator |
| 226 | + with pytest.raises(RuntimeError): |
| 227 | + with Creator(path, main_page) as zim_creator: |
| 228 | + zim_creator.add_article(BlobErrorArticle(**args)) |
| 229 | + |
| 230 | + |
| 231 | +def test_dontcreatezim_onexception(tmpdir): |
| 232 | + """ make sure we can prevent ZIM file creation (workaround missing cancel()) |
| 233 | +
|
| 234 | + A new interpreter is instanciated to get a different memory space. |
| 235 | + This workaround is not safe and may segfault at GC under some circumstances |
| 236 | +
|
| 237 | + Unless we get a proper cancel() on libzim, that's the only way to not create |
| 238 | + a ZIM file on error """ |
| 239 | + path, main_page = tmpdir / "test.zim", "welcome" |
| 240 | + pycode = f""" |
| 241 | +from libzim.writer import Creator |
| 242 | +from libzim.writer import Article |
| 243 | +class BlobErrorArticle(Article): |
| 244 | + def get_data(self): |
| 245 | + raise ValueError |
| 246 | +zim_creator = Creator("{path}", "{main_page}") |
| 247 | +try: |
| 248 | + zim_creator.add_article(BlobErrorArticle(**args)) |
| 249 | +except Exception: |
| 250 | + zim_creator._closed = True |
| 251 | +""" |
| 252 | + |
| 253 | + py = subprocess.run([sys.executable, "-c", pycode]) |
| 254 | + assert py.returncode == 0 |
| 255 | + assert not path.exists() |
0 commit comments