Esempio n. 1
0
def test_pattern_matching(gallery_conf, log_collector):
    """Test if only examples matching pattern are executed"""

    gallery_conf.update(filename_pattern=re.escape(os.sep) + 'plot_0')

    code_output = ('\n Out:\n\n .. code-block:: none\n'
                   '\n'
                   '    Óscar output\n'
                   '    log:Óscar\n'
                   '    $\\langle n_\\uparrow n_\\downarrow \\rangle$\n\n')
    # create three files in tempdir (only one matches the pattern)
    fnames = ['plot_0.py', 'plot_1.py', 'plot_2.py']
    for fname in fnames:
        with codecs.open(os.path.join(gallery_conf['examples_dir'], fname),
                         mode='w',
                         encoding='utf-8') as f:
            f.write('\n'.join(CONTENT))
        # generate rst file
        sg.generate_file_rst(fname, gallery_conf['gallery_dir'],
                             gallery_conf['examples_dir'], gallery_conf)
        # read rst file and check if it contains code output
        rst_fname = os.path.splitext(fname)[0] + '.rst'
        with codecs.open(os.path.join(gallery_conf['gallery_dir'], rst_fname),
                         mode='r',
                         encoding='utf-8') as f:
            rst = f.read()
        if re.search(gallery_conf['filename_pattern'],
                     os.path.join(gallery_conf['gallery_dir'], rst_fname)):
            assert code_output in rst
        else:
            assert code_output not in rst
Esempio n. 2
0
def test_fail_example(gallery_conf, log_collector):
    """Test that failing examples are only executed until failing block."""
    gallery_conf.update(filename_pattern='raise.py')

    failing_code = CONTENT + [
        '#' * 79, 'First_test_fail', '#' * 79, 'second_fail'
    ]

    with codecs.open(os.path.join(gallery_conf['examples_dir'], 'raise.py'),
                     mode='w',
                     encoding='utf-8') as f:
        f.write('\n'.join(failing_code))

    sg.generate_file_rst('raise.py', gallery_conf['gallery_dir'],
                         gallery_conf['examples_dir'], gallery_conf)
    assert len(log_collector.calls['warning']) == 1
    assert 'not defined' in log_collector.calls['warning'][0].args[2]

    # read rst file and check if it contains traceback output

    with codecs.open(os.path.join(gallery_conf['gallery_dir'], 'raise.rst'),
                     mode='r',
                     encoding='utf-8') as f:
        ex_failing_blocks = f.read().count('pytb')
        if ex_failing_blocks == 0:
            raise ValueError('Did not run into errors in bad code')
        elif ex_failing_blocks > 1:
            raise ValueError('Did not stop executing script after error')
Esempio n. 3
0
def test_pattern_matching():
    """Test if only examples matching pattern are executed"""
    examples_dir = tempfile.mkdtemp()
    gallery_dir = tempfile.mkdtemp()

    gallery_conf = {
        'filename_pattern': re.escape(os.sep) + 'plot_0',
        'examples_dirs': examples_dir,
        'gallery_dirs': gallery_dir,
        'plot_gallery': True,
        'mod_example_dir': 'modules/generated',
        'doc_module': (),
        'reference_url': {},
    }

    code_output = '\n Out::\n\n      output\n\n'
    # create three files in tempdir (only one matches the pattern)
    fnames = ['plot_0.py', 'plot_1.py', 'plot_2.py']
    for fname in fnames:
        with open(os.path.join(examples_dir, fname), 'w') as f:
            f.write('\n'.join(CONTENT))
            f.flush()
        # generate rst file
        sg.generate_file_rst(fname, gallery_dir, examples_dir, gallery_conf)
        # read rst file and check if it contains code output
        rst_fname = os.path.splitext(fname)[0] + '.rst'
        with open(os.path.join(gallery_dir, rst_fname), 'r') as f:
            rst = f.read()
            if re.search(gallery_conf['filename_pattern'],
                         os.path.join(gallery_dir, rst_fname)):
                assert_true(code_output in rst)
            else:
                assert_false(code_output in rst)
Esempio n. 4
0
def _generate_rst(gallery_conf, fname, content):
    """Return the rST text of a given example content.

    This writes a file gallery_conf['examples_dir']/fname with *content*,
    creates the corresponding rst file by running generate_file_rst() and
    returns the generated rST code.

    Parameters
    ----------
    gallery_conf
        A gallery_conf as created by the gallery_conf fixture.
    fname : str
        A filename; e.g. 'test.py'. This is relative to
        gallery_conf['examples_dir']
    content : str
        The content of fname.

    Returns
    -------
    rst : str
        The generated rST code.
    """
    with codecs.open(os.path.join(gallery_conf['examples_dir'], fname),
                     mode='w', encoding='utf-8') as f:
        f.write('\n'.join(content))
    # generate rst file
    sg.generate_file_rst(fname, gallery_conf['gallery_dir'],
                         gallery_conf['examples_dir'], gallery_conf)
    # read rst file and check if it contains code output
    rst_fname = os.path.splitext(fname)[0] + '.rst'
    with codecs.open(os.path.join(gallery_conf['gallery_dir'], rst_fname),
                     mode='r', encoding='utf-8') as f:
        rst = f.read()
    return rst
Esempio n. 5
0
def test_pattern_matching():
    """Test if only examples matching pattern are executed"""

    gallery_conf = build_test_configuration(
        filename_pattern=re.escape(os.sep) + 'plot_0')

    code_output = ('\n Out::\n'
                   '\n'
                   '    Óscar output\n'
                   '    log:Óscar\n'
                   '    $\\langle n_\\uparrow n_\\downarrow \\rangle$\n\n'
                   )
    # create three files in tempdir (only one matches the pattern)
    fnames = ['plot_0.py', 'plot_1.py', 'plot_2.py']
    for fname in fnames:
        with codecs.open(os.path.join(gallery_conf['examples_dir'], fname),
                         mode='w', encoding='utf-8') as f:
            f.write('\n'.join(CONTENT))
        # generate rst file
        sg.generate_file_rst(fname, gallery_conf['gallery_dir'],
                             gallery_conf['examples_dir'], gallery_conf)
        # read rst file and check if it contains code output
        rst_fname = os.path.splitext(fname)[0] + '.rst'
        with codecs.open(os.path.join(gallery_conf['gallery_dir'], rst_fname),
                         mode='r', encoding='utf-8') as f:
            rst = f.read()
        if re.search(gallery_conf['filename_pattern'],
                     os.path.join(gallery_conf['gallery_dir'], rst_fname)):
            assert_true(code_output in rst)
        else:
            assert_false(code_output in rst)
Esempio n. 6
0
def test_fail_example():
    """Test that failing examples are only executed until failing block"""

    gallery_conf = build_test_configuration(filename_pattern='raise.py')

    failing_code = CONTENT + [
        '#' * 79, 'First_test_fail', '#' * 79, 'second_fail'
    ]

    with codecs.open(os.path.join(gallery_conf['examples_dir'], 'raise.py'),
                     mode='w',
                     encoding='utf-8') as f:
        f.write('\n'.join(failing_code))

    with warnings.catch_warnings(record=True) as w:
        sg.generate_file_rst('raise.py', gallery_conf['gallery_dir'],
                             gallery_conf['examples_dir'], gallery_conf)
    assert_equal(len(w), 1)
    assert_true('not defined' in str(w[0].message))

    # read rst file and check if it contains traceback output

    with codecs.open(os.path.join(gallery_conf['gallery_dir'], 'raise.rst'),
                     mode='r',
                     encoding='utf-8') as f:
        ex_failing_blocks = f.read().count('pytb')
        if ex_failing_blocks == 0:
            raise ValueError('Did not run into errors in bad code')
        elif ex_failing_blocks > 1:
            raise ValueError('Did not stop executing script after error')
Esempio n. 7
0
def test_fail_example(gallery_conf, log_collector):
    """Test that failing examples are only executed until failing block"""

    gallery_conf.update(filename_pattern='raise.py')

    failing_code = CONTENT + ['#' * 79,
                              'First_test_fail', '#' * 79, 'second_fail']

    with codecs.open(os.path.join(gallery_conf['examples_dir'], 'raise.py'),
                     mode='w', encoding='utf-8') as f:
        f.write('\n'.join(failing_code))

    sg.generate_file_rst('raise.py', gallery_conf['gallery_dir'],
                         gallery_conf['examples_dir'], gallery_conf)
    assert len(log_collector.calls['warning']) == 1
    assert 'not defined' in log_collector.calls['warning'][0].args[2]

    # read rst file and check if it contains traceback output

    with codecs.open(os.path.join(gallery_conf['gallery_dir'], 'raise.rst'),
                     mode='r', encoding='utf-8') as f:
        ex_failing_blocks = f.read().count('pytb')
        if ex_failing_blocks == 0:
            raise ValueError('Did not run into errors in bad code')
        elif ex_failing_blocks > 1:
            raise ValueError('Did not stop executing script after error')
Esempio n. 8
0
def test_fail_example(gallery_conf, failing_code, want, log_collector,
                      req_pil):
    """Test that failing examples are only executed until failing block."""
    gallery_conf.update(image_scrapers=(), reset_modules=())
    gallery_conf.update(filename_pattern='raise.py')

    with codecs.open(os.path.join(gallery_conf['examples_dir'], 'raise.py'),
                     mode='w',
                     encoding='utf-8') as f:
        f.write('\n'.join(failing_code))

    sg.generate_file_rst('raise.py', gallery_conf['gallery_dir'],
                         gallery_conf['examples_dir'], gallery_conf)
    assert len(log_collector.calls['warning']) == 1
    msg = log_collector.calls['warning'][0].args[2]
    assert want in msg
    assert 'gen_gallery' not in msg
    # can only check that gen_rst is removed on non-input ones
    if 'Cannot use input' not in msg:
        assert 'gen_rst' not in msg
    assert '_check_input' not in msg

    # read rst file and check if it contains traceback output

    with codecs.open(os.path.join(gallery_conf['gallery_dir'], 'raise.rst'),
                     mode='r',
                     encoding='utf-8') as f:
        ex_failing_blocks = f.read().count('pytb')
        assert ex_failing_blocks != 0, 'Did not run into errors in bad code'
        assert ex_failing_blocks <= 1, \
            'Did not stop executing script after error'
Esempio n. 9
0
def test_pattern_matching():
    """Test if only examples matching pattern are executed"""
    examples_dir = tempfile.mkdtemp()
    gallery_dir = tempfile.mkdtemp()

    gallery_conf = {
        'filename_pattern': re.escape(os.sep) + 'plot_0',
        'examples_dirs': examples_dir,
        'gallery_dirs': gallery_dir,
        'plot_gallery': True,
        'mod_example_dir': 'modules/generated',
        'doc_module': (),
        'reference_url': {},
    }

    code_output = ('\n Out::\n'
                   '\n'
                   '    Óscar output\n'
                   '    log:Óscar\n'
                   '    $\\langle n_\\uparrow n_\\downarrow \\rangle$\n\n')
    # create three files in tempdir (only one matches the pattern)
    fnames = ['plot_0.py', 'plot_1.py', 'plot_2.py']
    for fname in fnames:
        with codecs.open(os.path.join(examples_dir, fname),
                         mode='w',
                         encoding='utf-8') as f:
            f.write('\n'.join(CONTENT))
        # generate rst file
        sg.generate_file_rst(fname, gallery_dir, examples_dir, gallery_conf)
        # read rst file and check if it contains code output
        rst_fname = os.path.splitext(fname)[0] + '.rst'
        with codecs.open(os.path.join(gallery_dir, rst_fname),
                         mode='r',
                         encoding='utf-8') as f:
            rst = f.read()
        if re.search(gallery_conf['filename_pattern'],
                     os.path.join(gallery_dir, rst_fname)):
            assert_true(code_output in rst)
        else:
            assert_false(code_output in rst)
Esempio n. 10
0
def generate_dir_rst(src_dir, target_dir, gallery_conf, seen_backrefs):
    """Generate the gallery reStructuredText for an example directory"""

    fhindex = GALLERY_HEADER

    if not os.path.exists(target_dir):
        os.makedirs(target_dir)
    tagged_examples = example_groups(src_dir)
    tagged_examples = order_examples(tagged_examples)

    computation_times = []
    build_target_dir = os.path.relpath(target_dir, gallery_conf['src_dir'])

    seen = set()
    if not os.path.exists(outdir):
        os.makedirs(outdir)

    for tag, examples in tagged_examples.items():
        sorted_listdir = sorted(
            examples, key=gallery_conf['within_subsection_order'](src_dir))

        entries_text = []
        iterator = sphinx_compatibility.status_iterator(
            sorted_listdir,
            'Generating gallery for %s ' % tag,
            length=len(sorted_listdir))
        for fname in iterator:
            write_example(os.path.join(src_dir, fname), outdir)
            intro, time_elapsed = generate_file_rst(fname, target_dir, outdir,
                                                    gallery_conf)

            if fname not in seen:
                seen.add(fname)
                computation_times.append((time_elapsed, fname))

            this_entry = _thumbnail_div(build_target_dir, fname,
                                        intro) + textwrap.dedent("""

                .. toctree::
                   :hidden:

                   /%s

                   """) % os.path.join(build_target_dir, fname[:-3]).replace(
                                            os.sep, '/')  # noqa: E501

            entries_text.append(this_entry)

            if gallery_conf['backreferences_dir']:
                write_backreferences(seen_backrefs, gallery_conf, target_dir,
                                     fname, intro)

        fhindex += textwrap.dedent("""

        {tag}
        {tag_underline}

        .. container:: gallery_images

        """.format(tag=tag, tag_underline='-' * len(tag)))

        for entry_text in entries_text:
            fhindex += '\n    '.join(entry_text.split('\n'))

        # clear at the end of the section
        fhindex += """.. raw:: html\n
        <div style='clear:both'></div>\n\n"""

    return fhindex, computation_times
Esempio n. 11
0
def generate_dir_rst(src_dir, target_dir, gallery_conf, seen_backrefs):
    """Generate the gallery reStructuredText for an example directory"""

    fhindex = GALLERY_HEADER

    if not os.path.exists(target_dir):
        os.makedirs(target_dir)
    tagged_examples = example_groups(src_dir)
    tagged_examples = order_examples(tagged_examples)

    computation_times = []
    build_target_dir = os.path.relpath(target_dir, gallery_conf['src_dir'])

    seen = set()
    tmp_dir = tempfile.mkdtemp()

    for tag, examples in tagged_examples.items():
        sorted_listdir = sorted(
            examples,
            key=gallery_conf['within_subsection_order'](src_dir))

        entries_text = []
        iterator = sphinx_compatibility.status_iterator(
            sorted_listdir,
            'Generating gallery for %s ' % tag,
            length=len(sorted_listdir))
        for fname in iterator:
            write_example(os.path.join(src_dir, fname), tmp_dir)
            intro, time_elapsed = generate_file_rst(
                fname, target_dir, tmp_dir, gallery_conf)

            if fname not in seen:
                seen.add(fname)
                computation_times.append((time_elapsed, fname))

            this_entry = _thumbnail_div(build_target_dir, fname, intro) + textwrap.dedent("""

                .. toctree::
                   :hidden:

                   /%s

                   """) % os.path.join(build_target_dir, fname[:-3]).replace(os.sep, '/')  # noqa: E501

            entries_text.append(this_entry)

            if gallery_conf['backreferences_dir']:
                write_backreferences(seen_backrefs, gallery_conf,
                                     target_dir, fname, intro)

        fhindex += textwrap.dedent("""

        {tag}
        {tag_underline}

        .. container:: gallery_images

        """.format(tag=tag, tag_underline='-' * len(tag)))

        for entry_text in entries_text:
            fhindex += '\n    '.join(entry_text.split('\n'))

        # clear at the end of the section
        fhindex += """.. raw:: html\n
        <div style='clear:both'></div>\n\n"""

    # Tidy up the temp directory
    shutil.rmtree(tmp_dir)

    return fhindex, computation_times