Skip to content

Commit

Permalink
Test jupytext/nbconvert composition
Browse files Browse the repository at this point in the history
  • Loading branch information
mwouts committed Oct 12, 2019
1 parent 00938f4 commit a017df1
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 0 deletions.
45 changes: 45 additions & 0 deletions tests/notebooks/md/plain_markdown.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
title: A sample document
---

This document is a plain Markdown document that was not created from a notebook.
We use this document to test that inputing a Markdown file into Jupytext, and then converting the
resulting notebook to a Markdown file using nbconvert, is the identity

Another paragraph

# A header

Indented code

def f(x):
return 1


def h(x):
return f(x)+2


A Python code snippet

```python
"""This code cell starts with ` ```python`"""
1 + 1
```

Code snippet without an explicit language
```
echo 'Hello world'
```

Markdown comments
<!-- #comment -->

VS Code region markers

<!-- #region -->
This Markdown cell has two consecutive blank lines


And continues here
<!-- #endregion -->
79 changes: 79 additions & 0 deletions tests/test_jupytext_nbconvert_round_trip.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import pytest
import jupytext
from jupytext.header import header_to_metadata_and_cell
from .utils import list_notebooks, requires_nbconvert


@requires_nbconvert
@pytest.mark.parametrize('md_file', list_notebooks('md', skip='jupytext'))
def test_markdown_jupytext_nbconvert_is_identity(md_file):
"""Test that a Markdown file, converted to a notebook, then
exported back to Markdown with nbconvert, yields the original file"""

with open(md_file) as fp:
md_org = fp.read()

nb = jupytext.reads(md_org, 'md')
import nbconvert
md_nbconvert, _ = nbconvert.export(nbconvert.MarkdownExporter, nb)

# Our expectations
md_expected = md_org.splitlines()
# #region and #endregion comments are removed
md_expected = [line for line in md_expected if line not in ['<!-- #region -->', '<!-- #endregion -->']]
# language is not inserted by nbconvert
md_expected = ['```' if line.startswith('```') else line for line in md_expected]
# nbconvert inserts no empty line after the YAML header (which is in a Raw cell)
md_expected = '\n'.join(md_expected).replace('---\n\n', '---\n') + '\n'

# an extra blank line is inserted before code cells
md_nbconvert = md_nbconvert.replace('\n\n```', '\n```')

jupytext.compare.compare(md_nbconvert, md_expected)


@requires_nbconvert
@pytest.mark.parametrize('nb_file', list_notebooks(skip='(html|magic)'))
def test_jupytext_markdown_similar_to_nbconvert(nb_file):
"""Test that the nbconvert export for a notebook matches Jupytext's one"""

nb = jupytext.read(nb_file)

# Remove cell outputs and metadata
for cell in nb.cells:
cell.outputs = []
cell.execution_count = None
cell.metadata = {}

md_jupytext = jupytext.writes(nb, fmt='md')

import nbconvert
md_nbconvert, _ = nbconvert.export(nbconvert.MarkdownExporter, nb)

# our expectations

# nbconvert file has no YAML header
md_jupytext_lines = md_jupytext.splitlines()
_, _, raw_cell, pos = header_to_metadata_and_cell(md_jupytext_lines, '')
md_jupytext = '\n'.join(md_jupytext_lines[pos:]) + '\n'
if raw_cell is not None:
md_jupytext = raw_cell.source + '\n\n' + md_jupytext

# region comments are not in nbconvert
md_jupytext = md_jupytext.replace('<!-- #region -->\n', '').replace('<!-- #endregion -->\n', '')

# Jupytext uses HTML comments to keep track of raw cells
md_jupytext = (md_jupytext
.replace('\n<!-- #raw -->\n', '')
.replace('<!-- #raw -->\n', '')
.replace('\n<!-- #endraw -->\n', ''))

# nbconvert file may start with an empty line
md_jupytext = md_jupytext.lstrip('\n')
md_nbconvert = md_nbconvert.lstrip('\n')

# Jupytext may not always have two blank lines between cells like Jupyter nbconvert
md_jupytext = md_jupytext.replace('\n\n\n', '\n\n')
md_nbconvert = md_nbconvert.replace('\n\n\n', '\n\n')

jupytext.compare.compare(md_nbconvert, md_jupytext)

0 comments on commit a017df1

Please sign in to comment.