Esempio n. 1
0
def test_decompress_formats():
    assert long_form_multiple_formats('ipynb') == [{'extension': '.ipynb'}]
    assert long_form_multiple_formats('ipynb,md') == [{
        'extension': '.ipynb'
    }, {
        'extension': '.md'
    }]
    assert long_form_multiple_formats('ipynb,py:light') == [{
        'extension':
        '.ipynb'
    }, {
        'extension':
        '.py',
        'format_name':
        'light'
    }]
    assert long_form_multiple_formats(['ipynb', '.py:light']) == [{
        'extension':
        '.ipynb'
    }, {
        'extension':
        '.py',
        'format_name':
        'light'
    }]
    assert long_form_multiple_formats('.pct.py:percent') == [{
        'extension':
        '.py',
        'suffix':
        '.pct',
        'format_name':
        'percent'
    }]
Esempio n. 2
0
def test_simple_pair():
    formats = long_form_multiple_formats('ipynb,py')
    expected_paths = ['notebook.ipynb', 'notebook.py']
    compare(list(zip(expected_paths, formats)),
            paired_paths('notebook.ipynb', 'ipynb', formats))
    compare(list(zip(expected_paths, formats)),
            paired_paths('notebook.py', 'py', formats))
Esempio n. 3
0
def test_many_and_suffix():
    formats = long_form_multiple_formats("ipynb,.pct.py,_lgt.py")
    expected_paths = ["notebook.ipynb", "notebook.pct.py", "notebook_lgt.py"]
    for fmt, path in zip(formats, expected_paths):
        compare(paired_paths(path, fmt, formats), list(zip(expected_paths, formats)))

    with pytest.raises(InconsistentPath):
        paired_paths("wrong_suffix.py", "py", formats)
Esempio n. 4
0
def test_decompress_formats():
    assert long_form_multiple_formats("ipynb") == [{"extension": ".ipynb"}]
    assert long_form_multiple_formats("ipynb,md") == [
        {"extension": ".ipynb"},
        {"extension": ".md"},
    ]
    assert long_form_multiple_formats("ipynb,py:light") == [
        {"extension": ".ipynb"},
        {"extension": ".py", "format_name": "light"},
    ]
    assert long_form_multiple_formats(["ipynb", ".py:light"]) == [
        {"extension": ".ipynb"},
        {"extension": ".py", "format_name": "light"},
    ]
    assert long_form_multiple_formats(".pct.py:percent") == [
        {"extension": ".py", "suffix": ".pct", "format_name": "percent"}
    ]
Esempio n. 5
0
def test_many_and_suffix():
    formats = long_form_multiple_formats('ipynb,.pct.py,_lgt.py')
    expected_paths = ['notebook.ipynb', 'notebook.pct.py', 'notebook_lgt.py']
    for fmt, path in zip(formats, expected_paths):
        compare(zip(expected_paths, formats), paired_paths(path, fmt, formats))

    with pytest.raises(InconsistentPath):
        paired_paths('wrong_suffix.py', 'py', formats)
Esempio n. 6
0
def test_prefix_on_root_174():
    short_formats = "ipynb,python//py:percent"

    formats = long_form_multiple_formats(short_formats)
    assert short_form_multiple_formats(formats) == short_formats

    expected_paths = ["Untitled.ipynb", "python/Untitled.py"]
    for fmt, path in zip(formats, expected_paths):
        compare(paired_paths(path, fmt, formats), list(zip(expected_paths, formats)))
Esempio n. 7
0
def test_simple_pair():
    formats = long_form_multiple_formats("ipynb,py")
    expected_paths = ["notebook.ipynb", "notebook.py"]
    compare(
        paired_paths("notebook.ipynb", "ipynb", formats),
        list(zip(expected_paths, formats)),
    )
    compare(paired_paths("notebook.py", "py", formats),
            list(zip(expected_paths, formats)))
Esempio n. 8
0
def test_prefix_on_root_174():
    short_formats = 'ipynb,python//py:percent'

    formats = long_form_multiple_formats(short_formats)
    assert short_form_multiple_formats(formats) == short_formats

    expected_paths = ['/Untitled.ipynb', '/python/Untitled.py']
    for fmt, path in zip(formats, expected_paths):
        compare(zip(expected_paths, formats), paired_paths(path, fmt, formats))
Esempio n. 9
0
def test_path_in_tree_limited_to_config_dir(tmpdir):
    root_nb_dir = tmpdir.mkdir("notebooks")
    nb_dir = root_nb_dir.mkdir("notebooks")
    src_dir = root_nb_dir.mkdir("scripts")
    other_dir = root_nb_dir.mkdir("other")

    formats = "notebooks///ipynb,scripts///py"
    fmt = long_form_multiple_formats(formats)[0]

    # Notebook in nested 'notebook' dir is paired
    notebook_in_nb_dir = nb_dir.join("subfolder").join("nb.ipynb")

    assert set(
        path
        for (path,
             _) in paired_paths(str(notebook_in_nb_dir), fmt, formats)) == {
                 str(notebook_in_nb_dir),
                 str(src_dir.join("subfolder").join("nb.py"))
             }

    # Notebook in base 'notebook' dir is paired if no config file is found
    notebook_in_other_dir = other_dir.mkdir("subfolder").join("nb.ipynb")
    assert set(
        path
        for (path,
             _) in paired_paths(str(notebook_in_other_dir), fmt, formats)) == {
                 str(notebook_in_other_dir),
                 str(
                     tmpdir.join("scripts").join("other").join(
                         "subfolder").join("nb.py")),
             }

    # When a config file exists
    root_nb_dir.join("jupytext.toml").write("\n")

    # Notebook in nested 'notebook' dir is still paired
    assert set(
        path
        for (path,
             _) in paired_paths(str(notebook_in_nb_dir), fmt, formats)) == {
                 str(notebook_in_nb_dir),
                 str(src_dir.join("subfolder").join("nb.py"))
             }

    # But the notebook in base 'notebook' dir is not paired any more
    alert = (
        "Notebook directory '/other/subfolder' does not match prefix root 'notebooks'"
        if os.path.sep == "/"
        # we escape twice the backslash because pytest.raises matches it as a regular expression
        else
        "Notebook directory '\\\\other\\\\subfolder' does not match prefix root 'notebooks'"
    )
    with pytest.raises(InconsistentPath, match=alert):
        paired_paths(str(notebook_in_other_dir), fmt, formats)
Esempio n. 10
0
def test_prefix_and_suffix():
    short_formats = (
        "notebook_folder/notebook_prefix_/_notebook_suffix.ipynb,"
        "script_folder//_in_percent_format.py:percent,"
        "script_folder//_in_light_format.py"
    )

    formats = long_form_multiple_formats(short_formats)
    assert short_form_multiple_formats(formats) == short_formats

    expected_paths = [
        "parent/notebook_folder/notebook_prefix_NOTEBOOK_NAME_notebook_suffix.ipynb",
        "parent/script_folder/NOTEBOOK_NAME_in_percent_format.py",
        "parent/script_folder/NOTEBOOK_NAME_in_light_format.py",
    ]
    for fmt, path in zip(formats, expected_paths):
        compare(paired_paths(path, fmt, formats), list(zip(expected_paths, formats)))

    # without the parent folder
    expected_paths = [path[7:] for path in expected_paths]
    for fmt, path in zip(formats, expected_paths):
        compare(paired_paths(path, fmt, formats), list(zip(expected_paths, formats)))

    # Not the expected parent folder
    with pytest.raises(InconsistentPath):
        paired_paths(
            "script_folder_incorrect/NOTEBOOK_NAME_in_percent_format.py",
            formats[1],
            formats,
        )

    # Not the expected suffix
    with pytest.raises(InconsistentPath):
        paired_paths(
            "parent/script_folder/NOTEBOOK_NAME_in_LIGHT_format.py", formats[2], formats
        )

    # Not the expected extension
    with pytest.raises(InconsistentPath):
        paired_paths(
            "notebook_folder/notebook_prefix_NOTEBOOK_NAME_notebook_suffix.py",
            formats[0],
            formats,
        )
Esempio n. 11
0
def test_set_auto_ext():
    with pytest.raises(ValueError):
        long_form_multiple_formats('ipynb,auto:percent', {})
Esempio n. 12
0
def test_duplicated_paths():
    formats = long_form_multiple_formats("ipynb,py:percent,py:light")
    with pytest.raises(InconsistentPath):
        paired_paths("notebook.ipynb", "ipynb", formats)
Esempio n. 13
0
def test_pandoc_format_is_preserved():
    formats_org = 'ipynb,md,.pandoc.md:pandoc,py:light'
    long = long_form_multiple_formats(formats_org)
    formats_new = short_form_multiple_formats(long)

    compare(formats_org, formats_new)