Example #1
0
def test_pretty_print_list_multilinestrings(nocolor):
    a = ["ac\ndf", "qe\nry", 2]
    b = [2, "abc\ndef", "qwe\nrty"]
    path = '/a/b'
    di = diff(a, b, path=path)

    io = StringIO()
    pp.pretty_print_diff(a, di, path, io)
    text = io.getvalue()
    lines = text.splitlines()

    assert lines == [
        '## deleted /a/b/0-1:',
        #'-  ["ac\ndf", "qe\nry"]',
        #'-  b[0]:',
        '-  item[0]:',
        '-    ac',
        '-    df',
        #'-  b[1]:',
        '-  item[1]:',
        '-    qe',
        '-    ry',
        '',
        '## inserted before /a/b/3:',
        #'+  ["abc\ndef", "qwe\nrty"]',
        #'+  b[3+0]:',
        '+  item[0]:',
        '+    abc',
        '+    def',
        #'+  b[3+1]:',
        '+  item[1]:',
        '+    qwe',
        '+    rty',
        '',
    ]
    def diff_nbnode_with_cache(self,
                               pk: int,
                               nb: nbf.NotebookNode,
                               uri: str = "",
                               as_str=False,
                               **kwargs):
        """Return a diff of a notebook to a cached one.

        Note: this will not diff markdown content, since it is not stored in the cache.
        """
        import nbdime
        from nbdime.prettyprint import pretty_print_diff, PrettyPrintConfig

        cached_nb = self.get_cache_bundle(pk).nb
        nb, _ = self.create_hashed_notebook(nb)

        diff = nbdime.diff_notebooks(cached_nb, nb)
        if not as_str:
            return diff
        stream = io.StringIO()
        stream.writelines(
            ["nbdiff\n", f"--- cached pk={pk}\n", f"+++ other: {uri}\n"])
        pretty_print_diff(cached_nb, diff, "nb",
                          PrettyPrintConfig(out=stream, **kwargs))
        return stream.getvalue()
Example #3
0
def test_pretty_print_list_multilinestrings():
    a = ["ac\ndf", "qe\nry", 2]
    b = [2, "abc\ndef", "qwe\nrty"]
    path = '/a/b'
    di = diff(a, b, path=path)

    config = TestConfig(use_color=False)
    pp.pretty_print_diff(a, di, path, config)
    text = config.out.getvalue()
    lines = text.splitlines()

    assert lines == [
        '## deleted /a/b/0-1:',
        #'-  ["ac\ndf", "qe\nry"]',
        #'-  b[0]:',
        '-  item[0]:',
        '-    ac',
        '-    df',
        #'-  b[1]:',
        '-  item[1]:',
        '-    qe',
        '-    ry',
        '',
        '## inserted before /a/b/3:',
        #'+  ["abc\ndef", "qwe\nrty"]',
        #'+  b[3+0]:',
        '+  item[0]:',
        '+    abc',
        '+    def',
        #'+  b[3+1]:',
        '+  item[1]:',
        '+    qwe',
        '+    rty',
        '',
    ]
Example #4
0
def test_pretty_print_string_diff(nocolor):
    a = '\n'.join(['line 1', 'line 2', 'line 3', ''])
    b = '\n'.join(['line 1', 'line 3', 'line 4', ''])
    path = '/a/b'
    di = diff(a, b, path=path)

    with mock.patch('nbdime.prettyprint.which', lambda cmd: None):
        io = StringIO()
        pp.pretty_print_diff(a, di, path, io)
        text = io.getvalue()
        lines = text.splitlines()

    text = '\n'.join(lines)
    assert ('< line 2' in text) or ((pp.REMOVE + 'line 2' + pp.RESET) in text)
    assert ('> line 4' in text) or ((pp.ADD + 'line 4' + pp.RESET) in text)
Example #5
0
def test_pretty_print_string_diff(nocolor):
    a = '\n'.join(['line 1', 'line 2', 'line 3', ''])
    b = '\n'.join(['line 1', 'line 3', 'line 4', ''])
    path = '/a/b'
    di = diff(a, b, path=path)

    with mock.patch('nbdime.prettyprint.which', lambda cmd: None):
        io = StringIO()
        pp.pretty_print_diff(a, di, path, io)
        text = io.getvalue()
        lines = text.splitlines()

    text = '\n'.join(lines)
    assert ('< line 2' in text) or ((pp.REMOVE + 'line 2' + pp.RESET) in text)
    assert ('> line 4' in text) or ((pp.ADD + 'line 4' + pp.RESET) in text)
def test_pretty_print_string_diff():
    a = '\n'.join(['line 1', 'line 2', 'line 3', ''])
    b = '\n'.join(['line 1', 'line 3', 'line 4', ''])
    path = '/a/b'
    di = diff(a, b, path=path)

    with mock.patch('nbdime.prettyprint.which', lambda cmd: None):
        config = TestConfig(use_color=False)
        pp.pretty_print_diff(a, di, path, config)
        text = config.out.getvalue()
        lines = text.splitlines()

    text = '\n'.join(lines)
    assert ('< line 2' in text) or ((config.REMOVE + 'line 2' + config.RESET) in text)
    assert ('> line 4' in text) or ((config.ADD + 'line 4' + config.RESET) in text)
Example #7
0
def test_pretty_print_dict_diff():
    a = {'a': 1}
    b = {'a': 2}
    di = diff(a, b, path='x/y')

    config = TestConfig(use_color=False)
    pp.pretty_print_diff(a, di, 'x/y', config)
    text = config.out.getvalue()
    lines = text.splitlines()

    assert lines == [
        '## replaced x/y/a:',
        '-  1',
        '+  2',
        '',
    ]
Example #8
0
def test_pretty_print_dict_diff(nocolor):
    a = {'a': 1}
    b = {'a': 2}
    di = diff(a, b, path='x/y')

    io = StringIO()
    pp.pretty_print_diff(a, di, 'x/y', io)
    text = io.getvalue()
    lines = text.splitlines()

    assert lines == [
        '## replaced x/y/a:',
        '-  1',
        '+  2',
        '',
    ]
Example #9
0
def test_pretty_print_dict_diff(nocolor):
    a = {'a': 1}
    b = {'a': 2}
    di = diff(a, b, path='x/y')

    io = StringIO()
    pp.pretty_print_diff(a, di, 'x/y', io)
    text = io.getvalue()
    lines = text.splitlines()

    assert lines == [
        '## replaced x/y/a:',
        '-  1',
        '+  2',
        '',
    ]
Example #10
0
def test_pretty_print_list_diff():
    a = [1]
    b = [2]
    path = '/a/b'
    di = diff(a, b, path=path)

    config = TestConfig(use_color=False)
    pp.pretty_print_diff(a, di, path, config)
    text = config.out.getvalue()
    lines = text.splitlines()

    assert lines == [
        '## inserted before /a/b/0:',
        '+  [2]',
        '',
        '## deleted /a/b/0:',
        '-  [1]',
        '',
    ]
Example #11
0
def test_pretty_print_list_diff(nocolor):
    a = [1]
    b = [2]
    path = '/a/b'
    di = diff(a, b, path=path)

    io = StringIO()
    pp.pretty_print_diff(a, di, path, io)
    text = io.getvalue()
    lines = text.splitlines()

    assert lines == [
        '## inserted before /a/b/0:',
        '+  [2]',
        '',
        '## deleted /a/b/0:',
        '-  [1]',
        '',
    ]
Example #12
0
def test_pretty_print_list_diff(nocolor):
    a = [1]
    b = [2]
    path = '/a/b'
    di = diff(a, b, path=path)

    io = StringIO()
    pp.pretty_print_diff(a, di, path, io)
    text = io.getvalue()
    lines = text.splitlines()

    assert lines == [
        '## inserted before /a/b/0:',
        '+  [2]',
        '',
        '## deleted /a/b/0:',
        '-  [1]',
        '',
    ]
Example #13
0
def test_pretty_print_string_diff_b64():
    a = b64text(1024)
    b = b64text(800)
    path = '/a/b'
    di = diff(a, b, path=path)

    config = TestConfig(use_color=False)
    pp.pretty_print_diff(a, di, path, config)
    text = config.out.getvalue()
    lines = text.splitlines()

    ha = pp.hash_string(a)
    hb = pp.hash_string(b)

    assert lines == [
        '## modified /a/b:',
        '-  %s...<snip base64, md5=%s...>' % (a[:8], ha[:16]),
        '+  %s...<snip base64, md5=%s...>' % (b[:8], hb[:16]),
        '',
    ]
Example #14
0
def test_pretty_print_string_diff_b64(nocolor):
    a = b64text(1024)
    b = b64text(800)
    path = '/a/b'
    di = diff(a, b, path=path)

    io = StringIO()
    pp.pretty_print_diff(a, di, path, io)
    text = io.getvalue()
    lines = text.splitlines()

    ha = pp.hash_string(a)
    hb = pp.hash_string(b)

    assert lines == [
        '## modified /a/b:',
        '-  %s...<snip base64, md5=%s...>' % (a[:8], ha[:16]),
        '+  %s...<snip base64, md5=%s...>' % (b[:8], hb[:16]),
        '',
    ]
Example #15
0
def test_pretty_print_string_diff_b64(nocolor):
    a = b64text(1024)
    b = b64text( 800)
    path = '/a/b'
    di = diff(a, b, path=path)

    io = StringIO()
    pp.pretty_print_diff(a, di, path, io)
    text = io.getvalue()
    lines = text.splitlines()

    ha = pp.hash_string(a)
    hb = pp.hash_string(b)

    assert lines == [
        '## modified /a/b:',
        '-  %s...<snip base64, md5=%s...>' % (a[:8], ha[:16]),
        '+  %s...<snip base64, md5=%s...>' % (b[:8], hb[:16]),
        '',
    ]
Example #16
0
 def _check_nbs(obtained_filename, expected_filename):
     obtained_nb = nbf.read(str(obtained_filename), nbf.NO_CONVERT)
     expect_nb = nbf.read(str(expected_filename), nbf.NO_CONVERT)
     for cell in expect_nb.cells:
         empty_non_deterministic_outputs(cell)
     for cell in obtained_nb.cells:
         empty_non_deterministic_outputs(cell)
     diff = diff_notebooks(obtained_nb, expect_nb)
     filename_without_path = str(
         expected_filename)[str(expected_filename).rfind("/") + 1:]
     if diff:
         raise AssertionError(
             pretty_print_diff(obtained_nb, diff,
                               str(filename_without_path)))