예제 #1
0
def test_make_zim_file_working(build_data, png_image):
    build_data["build_dir"].mkdir()

    # add an image
    shutil.copyfile(png_image, build_data["build_dir"] / png_image.name)
    # add an HTML file
    with open(build_data["build_dir"] / "welcome", "w") as fh:
        fh.write("<html><title>Coucou</title></html>")
    # add a CSS file
    with open(build_data["build_dir"] / "style.css", "w") as fh:
        fh.write("body { background-color: red; }")
    # add a JS file
    with open(build_data["build_dir"] / "app.js", "w") as fh:
        fh.write("console.log(window);")

    make_zim_file(**build_data)
    assert build_data["fpath"].exists()
    reader = Archive(build_data["fpath"])
    # welcome (actual) and two redirs
    assert reader.entry_count == 7  # includes redirect

    assert reader.get_item("style.css").mimetype == "text/css"
    assert reader.get_item("app.js").mimetype == "application/javascript"
    assert reader.get_suggestions_count("bienvenue") == 1
    assert reader.get_suggestions_count("coucou") == 0
    assert "Accueil" in list(reader.get_suggestions("bienvenue"))
예제 #2
0
def test_make_zim_file_working(build_data, png_image):
    build_data["build_dir"].mkdir()

    # add an image
    shutil.copyfile(png_image, build_data["build_dir"] / png_image.name)
    # add an HTML file
    with open(build_data["build_dir"] / "welcome", "w") as fh:
        fh.write("<html><title>Coucou</title></html>")
    # add a CSS file
    with open(build_data["build_dir"] / "style.css", "w") as fh:
        fh.write("body { background-color: red; }")
    # add a JS file
    with open(build_data["build_dir"] / "app.js", "w") as fh:
        fh.write("console.log(window);")

    make_zim_file(**build_data)
    assert build_data["fpath"].exists()
    with libzim.reader.File(build_data["fpath"]) as reader:
        # A/welcome (actual) and two redirs
        assert reader.get_namespace_count("A") == 3
        # I/commons.png (actual) and two redirs
        assert reader.get_namespace_count("I") == 3
        # 1 x CSS, 1x JS and -/favicon redirect
        assert reader.get_namespace_count("-") == 3

        assert reader.get_article("-/style.css").mimetype == "text/css"
        assert reader.get_article(
            "-/app.js").mimetype == "application/javascript"
        assert reader.get_suggestions_results_count("bienvenue") == 2
        assert reader.get_suggestions_results_count("coucou") == 2
        assert "A/Accueil" in list(reader.suggest("bienvenue"))
예제 #3
0
def test_make_zim_file_exceptions_while_building(tmp_path, png_image,
                                                 build_data):
    build_data["build_dir"].mkdir()
    shutil.copyfile(png_image, build_data["build_dir"] / png_image.name)
    build_data["redirects_file"] = tmp_path / "toto.tsv"
    with pytest.raises(FileNotFoundError):
        make_zim_file(**build_data, workaround_nocancel=False)
    # disabled workaround, we shall have a ZIM file
    assert build_data["fpath"].exists()
예제 #4
0
def test_redirects_file(tmp_path, png_image, build_data):
    build_data["build_dir"].mkdir()
    shutil.copyfile(png_image, build_data["build_dir"] / png_image.name)
    build_data["redirects_file"] = tmp_path / "toto.tsv"
    with open(build_data["redirects_file"], "w") as fh:
        # write a redirect with a namespace (old ns scheme)
        fh.write("A\tAccueil\t\tcommons.png\n")

    # call make_zim_file with redirects_file
    make_zim_file(
        build_dir=build_data["build_dir"],
        fpath=build_data["fpath"],
        name="test-zim",
        main_page="welcome",
        favicon=png_image.name,
        title="Test ZIM",
        description="A test ZIM",
        redirects_file=build_data["redirects_file"],
    )
예제 #5
0
def test_make_zim_file_fail_nofavicon(build_data):
    # ensure we fail on missing favicon
    build_data["build_dir"].mkdir()
    with pytest.raises(IOError):
        make_zim_file(**build_data)
    assert not build_data["fpath"].exists()
예제 #6
0
def test_make_zim_file_fail_nobuildir(build_data):
    # ensure we fail on missing build dir
    with pytest.raises(IOError):
        make_zim_file(**build_data)
    assert not build_data["fpath"].exists()