Esempio n. 1
0
def test_modname_to_modpath_single():
    with utils.TempDir() as temp:
        dpath = temp.dpath

        # Single module
        single = touch((dpath, '_tmpsingle.py'))
        single_main = touch((dpath, '__main__.py'))

        with utils.PythonPathContext(dpath):
            assert single == _static_modname_to_modpath('_tmpsingle')
            assert single == _static_modname_to_modpath('_tmpsingle',
                                                        hide_init=True,
                                                        hide_main=False)
            assert single == _static_modname_to_modpath('_tmpsingle',
                                                        hide_init=False,
                                                        hide_main=False)
            assert single == _static_modname_to_modpath('_tmpsingle',
                                                        hide_init=False,
                                                        hide_main=True)

            # Weird module named main not in a package
            assert _static_modname_to_modpath('__main__') == single_main
            assert _static_modname_to_modpath('__main__',
                                              hide_init=True,
                                              hide_main=False) == single_main
            assert _static_modname_to_modpath('__main__',
                                              hide_init=False,
                                              hide_main=False) == single_main
            assert _static_modname_to_modpath('__main__',
                                              hide_init=False,
                                              hide_main=True) == single_main
Esempio n. 2
0
def test_global_exec():
    """
    pytest testing/test_runner.py::test_global_exec -s
    """
    from xdoctest import runner

    source = utils.codeblock(
        '''
        def foo():
            """
                Example:
                    >>> print(a)
            """
        ''')

    config = {
        'global_exec': 'a=1',
    }

    with utils.TempDir() as temp:
        dpath = temp.dpath
        modpath = join(dpath, 'test_example_run.py')

        with open(modpath, 'w') as file:
            file.write(source)

        with utils.CaptureStdout() as cap:
            runner.doctest_module(modpath, 'foo', argv=[''], config=config)

    assert '1 passed' in cap.text
Esempio n. 3
0
def test_collect_module_level_singleline():
    """
    pytest testing/test_core.py::test_collect_module_level

    Ignore:
        temp = utils.TempDir()
    """
    temp = utils.TempDir()
    dpath = temp.ensure()
    modpath = join(dpath, 'test_collect_module_level_singleline.py')
    source = utils.codeblock('''">>> pass"''')
    with open(modpath, 'w') as file:
        file.write(source)
    from xdoctest import core
    doctests = list(core.parse_doctestables(modpath, style='freeform'))
    assert len(doctests) == 1
    self = doctests[0]
    assert self.callname == '__doc__'
    self.config['colored'] = False
    assert self.format_src(offset_linenos=True).strip().startswith('1')
    assert self.format_src(offset_linenos=False).strip().startswith('1')

    with utils.PythonPathContext(dpath):
        status = self.run(verbose=0, on_error='return')
    assert status['passed']
    temp.cleanup()
Esempio n. 4
0
def _run_case(source):
    from xdoctest import utils
    COLOR = 'yellow'

    def cprint(msg, color=COLOR):
        print(utils.color_text(str(msg), COLOR))

    cprint('\n\n' '\n <RUN CASE> ' '\n  ========  ' '\n', COLOR)

    cprint('DOCTEST SOURCE:')
    cprint('---------------')
    print(
        utils.indent(
            utils.add_line_numbers(utils.highlight_code(source, 'python'))))

    print('')

    import hashlib
    hasher = hashlib.sha1()
    hasher.update(source.encode('utf8'))
    hashid = hasher.hexdigest()[0:8]

    with utils.TempDir() as temp:
        dpath = temp.dpath
        modpath = join(dpath, 'test_linenos_' + hashid + '.py')

        with open(modpath, 'w') as file:
            file.write(source)

        with utils.CaptureStdout(supress=False) as cap:
            runner.doctest_module(modpath, 'all', argv=[''])

    cprint('\n\n --- </END RUN CASE> --- \n\n', COLOR)
    return cap.text
Esempio n. 5
0
def test_run_zero_arg():
    """
    pytest testing/test_runner.py::test_run_zero_arg -s
    """
    from xdoctest import runner

    source = utils.codeblock(
        '''
        def zero_arg_print():
            print('running zero arg')
        ''')

    with utils.TempDir() as temp:
        dpath = temp.dpath
        modpath = join(dpath, 'test_run_zero_arg.py')

        with open(modpath, 'w') as file:
            file.write(source)

        # disabled tests dont run in "all" mode
        with utils.CaptureStdout() as cap:
            try:
                runner.doctest_module(modpath, 'all', argv=[''], verbose=3)
            except Exception:
                pass
        assert 'running zero arg' not in cap.text

        with utils.CaptureStdout() as cap:
            try:
                runner.doctest_module(modpath, 'zero_arg_print', argv=[''], verbose=3)
            except Exception:
                pass
        # print(cap.text)
        assert 'running zero arg' in cap.text
Esempio n. 6
0
def test_runner_config():
    """
    pytest testing/test_runner.py::test_runner_config -s
    """
    from xdoctest import runner

    source = utils.codeblock('''
        def foo():
            """
                Example:
                    >>> print('i wanna see this')
            """
        ''')

    config = {
        'default_runtime_state': {
            'SKIP': True
        },
    }

    with utils.TempDir() as temp:
        dpath = temp.dpath
        modpath = join(dpath, 'test_example_run.py')

        with open(modpath, 'w') as file:
            file.write(source)

        with utils.CaptureStdout() as cap:
            runner.doctest_module(modpath, 'foo', argv=[''], config=config)

    assert 'SKIPPED' in cap.text
Esempio n. 7
0
def test_mod_lineno():
    with utils.TempDir() as temp:
        dpath = temp.dpath
        modpath = join(dpath, 'test_mod_lineno.py')
        source = utils.codeblock('''
            class Fun(object):  #1
                @property
                def test(self):
                    """         # 4
                    >>> a = 1
                    >>> 1 / 0
                    """
            ''')
        with open(modpath, 'w') as file:
            file.write(source)
        doctests = list(core.parse_doctestables(modpath, style='freeform'))
        assert len(doctests) == 1
        self = doctests[0]

        # print(self._parts[0])
        assert self.lineno == 5
        # print(self.format_src())
        self.config['colored'] = False
        assert self.format_src(offset_linenos=False).strip().startswith('1')
        assert self.format_src(offset_linenos=True).strip().startswith('5')

        with utils.PythonPathContext(dpath):
            status = self.run(verbose=10, on_error='return')

        assert not status['passed']
Esempio n. 8
0
def test_show_entire():
    """
    pytest testing/test_core.py::test_show_entire
    """
    temp = utils.TempDir()
    dpath = temp.ensure()
    modpath = join(dpath, 'test_show_entire.py')
    source = utils.codeblock(
        '''
        def foo():
            """
            Prefix

            Example:
                >>> x = 4
                >>> x = 5 + x
                >>> x = 6 + x
                >>> x = 7 + x
                >>> x
                22
                >>> x = 8 + x
                >>> x = 9 + x
                >>> x = 10 + x
                >>> x = 11 + x
                >>> x = 12 + x
                >>> x
                42

            text-line-after
            """
        ''')
    with open(modpath, 'w') as file:
        file.write(source)
    from xdoctest import core

    # calldefs = core.module_calldefs(modpath)
    # docline = calldefs['foo'].doclineno
    # docstr = calldefs['foo'].docstr
    # all_parts = parser.DoctestParser().parse(docstr)
    # assert docline == 2

    doctests = list(core.parse_doctestables(modpath, style='freeform'))
    assert len(doctests) == 1
    self = doctests[0]
    self.config['colored'] = False
    print(self.lineno)
    print(self._parts[0].line_offset)
    print(self.format_src())

    src_offset = self.format_src(offset_linenos=True).strip()
    src_nooffset = self.format_src(offset_linenos=False).strip()

    assert src_offset[:4].startswith('6')
    assert src_nooffset[:4].startswith('1')

    with utils.PythonPathContext(dpath):
        status = self.run(verbose=0, on_error='return')
    assert not status['passed']
    temp.cleanup()
Esempio n. 9
0
def test_runner_syntax_error():
    """
        python testing/test_errors.py test_runner_syntax_error
    """
    source = utils.codeblock(
        '''
        def test_parsetime_syntax_error1():
            """
                Example:
                    >>> from __future__ import print_function
                    >>> print 'Parse-Time Syntax Error'
            """

        def test_parsetime_syntax_error2():
            """
                Example:
                    >>> def bad_syntax() return for
            """

        def test_runtime_error():
            """
                Example:
                    >>> print('Runtime Error {}'.format(5 / 0))
            """

        def test_runtime_name_error():
            """
                Example:
                    >>> print('Name Error {}'.format(foo))
            """

        def test_runtime_warning():
            """
                Example:
                    >>> import warnings
                    >>> warnings.warn('in-code warning')
            """
        ''')

    temp = utils.TempDir(persist=True)
    temp.ensure()
    dpath = temp.dpath
    modpath = join(dpath, 'test_runner_syntax_error.py')
    open(modpath, 'w').write(source)

    with utils.CaptureStdout() as cap:
        runner.doctest_module(modpath, 'all', argv=[''], style='freeform',
                              verbose=0)

    print(utils.indent(cap.text))

    assert '1 run-time warnings' in cap.text
    assert '2 parse-time warnings' in cap.text

    # Assert summary line
    assert '3 warnings' in cap.text
    assert '2 failed' in cap.text
    assert '1 passed' in cap.text
Esempio n. 10
0
def test_zero_args():
    """
    python testing/test_runner.py test_zero_args
    """
    from xdoctest import runner

    source = utils.codeblock(
        '''
        # --- HELPERS ---
        def zero_args1(a=1):
            pass


        def zero_args2(*args):
            pass


        def zero_args3(**kwargs):
            pass


        def zero_args4(a=1, b=2, *args, **kwargs):
            pass


        def non_zero_args1(a):
            pass


        def non_zero_args2(a, b):
            pass


        def non_zero_args3(a, b, *args):
            pass


        def non_zero_args4(a, b, **kwargs):
            pass


        def non_zero_args5(a, b=1, **kwargs):
            pass
        ''')

    with utils.TempDir() as temp:
        dpath = temp.dpath
        modpath = join(dpath, 'test_zero_args.py')

        with open(modpath, 'w') as file:
            file.write(source)

        zero_func_names = {
            example.callname
            for example in runner._gather_zero_arg_examples(modpath)
        }
        assert zero_func_names == set(['zero_args1', 'zero_args2',
                                       'zero_args3', 'zero_args4'])
Esempio n. 11
0
def test_list():
    from xdoctest import runner

    source = utils.codeblock(
        '''
        # --- HELPERS ---
        def real_test1(a=1):
            """
                Example:
                    >>> pass
            """
            pass

        def fake_test1(a=1):
            pass

        def real_test2():
            """
                Example:
                    >>> pass
            """
            pass

        def fake_test2():
            pass
        ''')

    with utils.TempDir() as temp:
        dpath = temp.dpath
        modpath = join(dpath, 'test_list.py')

        with open(modpath, 'w') as file:
            file.write(source)

        with utils.CaptureStdout() as cap:
            runner.doctest_module(modpath, 'list', argv=[''])

        assert 'real_test1' in cap.text
        assert 'real_test2' in cap.text
        assert 'fake_test1' not in cap.text
        assert 'fake_test2' not in cap.text

        # test command=None
        with utils.CaptureStdout() as cap:
            runner.doctest_module(modpath, None, argv=[''])

        assert 'real_test1' in cap.text
        assert 'real_test2' in cap.text
        assert 'fake_test1' not in cap.text
        assert 'fake_test2' not in cap.text
Esempio n. 12
0
def test_hack_the_sys_argv():
    """
    Tests hacky solution to issue #76

    pytest testing/test_runner.py::test_global_exec -s

    References:
        https://github.com/Erotemic/xdoctest/issues/76
    """
    from xdoctest import runner

    source = utils.codeblock(
        '''
        def foo():
            """
                Example:
                    >>> # xdoctest: +REQUIRES(--hackedflag)
                    >>> print('This will run if global_exec specified')
            """
        ''')

    import sys
    NEEDS_FIX = '--hackedflag' not in sys.argv

    config = {
        'global_exec': 'import sys; sys.argv.append("--hackedflag")'
    }

    with utils.TempDir() as temp:
        dpath = temp.dpath
        modpath = join(dpath, 'test_example_run.py')

        with open(modpath, 'w') as file:
            file.write(source)

        with utils.CaptureStdout() as cap:
            runner.doctest_module(modpath, 'foo', argv=[''], config=config)

    if NEEDS_FIX:
        # Fix the global state
        sys.argv.remove('--hackedflag')

    # print(cap.text)
    assert '1 passed' in cap.text
Esempio n. 13
0
def _run_case(source, style='auto'):
    """
    Runs all doctests in a source block

    Args:
        source (str): source code of an entire file

    TODO: run case is over-duplicated and should be separated into a test utils directory
    """
    from xdoctest import utils
    from xdoctest import runner
    COLOR = 'yellow'
    def cprint(msg, color=COLOR):
        print(utils.color_text(str(msg), COLOR))
    cprint('\n\n'
           '\n <RUN CASE> '
           '\n  ========  '
           '\n', COLOR)

    cprint('CASE SOURCE:')
    cprint('------------')
    print(utils.indent(
        utils.add_line_numbers(utils.highlight_code(source, 'python'))))

    print('')

    import hashlib
    hasher = hashlib.sha1()
    hasher.update(source.encode('utf8'))
    hashid = hasher.hexdigest()[0:8]

    with utils.TempDir() as temp:
        dpath = temp.dpath
        modpath = join(dpath, 'test_linenos_' + hashid + '.py')

        with open(modpath, 'w') as file:
            file.write(source)

        with utils.CaptureStdout(supress=False) as cap:
            runner.doctest_module(modpath, 'all', argv=[''], style=style)

    cprint('\n\n --- </END RUN CASE> --- \n\n', COLOR)
    return cap.text
Esempio n. 14
0
def test_no_docstr():
    """
    CommandLine:
        python -m test_core test_no_docstr
    """
    with utils.TempDir() as temp:
        dpath = temp.dpath
        modpath = join(dpath, 'test_no_docstr.py')
        source = utils.codeblock('''
            def get_scales(kpts):
                """ Gets average scale (does not take into account elliptical shape """
                _scales = np.sqrt(get_sqrd_scales(kpts))
                return _scales
            ''')
        with open(modpath, 'w') as file:
            file.write(source)
        from xdoctest import core
        doctests = list(core.parse_doctestables(modpath, style='freeform'))
        assert len(doctests) == 0
Esempio n. 15
0
def test_modname_to_modpath_namespace():
    """
    Ignore:
        import sys
        sys.path.append('/home/joncrall/code/xdoctest/testing')
        from test_static import *
        temp = utils.TempDir()
        temp.__enter__()
        sys.path.append(temp.dpath)

        temp.__exit__(None, None, None)

    %timeit _syspath_modname_to_modpath('xdoctest.static_analysis')
    %timeit _pkgutil_modname_to_modpath('xdoctest.static_analysis')
    """
    with utils.TempDir() as temp:
        dpath = temp.dpath

        # Some "bad" non-module directories
        tmpbad = utils.ensuredir((dpath, '_tmpbad'))

        # Make a submodule of a bad directory, look good.
        sub_bad = utils.ensuredir((tmpbad, 'sub_bad'))
        touch((tmpbad, '_inbad.py'))
        subbad = touch((sub_bad, '__init__.py'))  # NOQA
        b0 = touch((sub_bad, 'b0.py'))  # NOQA

        with utils.PythonPathContext(dpath):
            assert _static_modname_to_modpath('_tmpbad') is None

            # Tricky case, these modules look good outside of _tmpbad WOW, you
            # can actually import this and it works, but pkgloader still
            # returns None so we should too.
            assert _static_modname_to_modpath('_tmpbad.sub_bad') is None
            assert _static_modname_to_modpath('_tmpbad.sub_bad.b0') is None

            # We should be able to statically find all of the good module
            # directories.

            # this should all be static
            import sys
            assert '_tmpsingle' not in sys.modules
            assert '_tmpbad' not in sys.modules
Esempio n. 16
0
def test_all_disabled():
    """
    pytest testing/test_runner.py::test_all_disabled -s -vv
    python testing/test_runner.py test_all_disabled
    """
    from xdoctest import runner

    source = utils.codeblock(
        '''
        def foo():
            """
                Example:
                    >>> # DISABLE_DOCTEST
                    >>> print('all will' + ' not print this')
            """

        def bar():
            """
                Example:
                    >>> print('all will' + ' print this')
            """
        ''')

    with utils.TempDir() as temp:
        dpath = temp.dpath
        modpath = join(dpath, 'test_all_disabled.py')

        with open(modpath, 'w') as file:
            file.write(source)

        # disabled tests dont run in "all" mode
        with utils.CaptureStdout() as cap:
            runner.doctest_module(modpath, 'all', argv=[''])
        assert 'all will print this' in cap.text
        # print('    ' + cap.text.replace('\n', '\n    '))
        assert 'all will not print this' not in cap.text

        # Running an disabled example explicitly should work
        with utils.CaptureStdout() as cap:
            runner.doctest_module(modpath, 'foo', argv=[''])
        # print('    ' + cap.text.replace('\n', '\n    '))
        assert 'all will not print this' in cap.text
Esempio n. 17
0
def test_oneliner():
    """
    python ~/code/xdoctest/testing/test_core.py test_oneliner
    """
    with utils.TempDir() as temp:
        dpath = temp.dpath
        modpath = join(dpath, 'test_oneliner.py')
        source = utils.codeblock('''
            def foo():
                """
                >>> assert False, 'should fail'
                """
            ''')
        with open(modpath, 'w') as file:
            file.write(source)
        doctests = list(core.parse_doctestables(modpath))
        assert len(doctests) == 1
        print('doctests = {!r}'.format(doctests))
        import pytest
        with pytest.raises(AssertionError, match='should fail'):
            doctests[0].run()
Esempio n. 18
0
def test_example_run():
    from xdoctest import runner

    source = utils.codeblock('''
        def foo():
            """
                Example:
                    >>> print('i wanna see this')
            """
        ''')

    with utils.TempDir() as temp:
        dpath = temp.dpath
        modpath = join(dpath, 'test_example_run.py')

        with open(modpath, 'w') as file:
            file.write(source)

        with utils.CaptureStdout() as cap:
            runner.doctest_module(modpath, 'foo', argv=[''])

    assert 'i wanna see this' in cap.text
Esempio n. 19
0
def test_mod_globals():
    with utils.TempDir() as temp:
        dpath = temp.dpath
        modpath = join(dpath, 'test_mod_globals.py')
        source = utils.codeblock('''
            X = 10
            def test(self):
                """
                >>> X
                10
                """
            ''')
        with open(modpath, 'w') as file:
            file.write(source)
        from xdoctest import core
        doctests = list(core.parse_doctestables(modpath, style='freeform'))
        assert len(doctests) == 1
        self = doctests[0]

        with utils.PythonPathContext(dpath):
            status = self.run(verbose=0, on_error='return')
        assert status['passed']
        assert self.logged_evals[0] == 10
Esempio n. 20
0
def test_package_submodules():
    """
    CommandLine:
        pytest testing/test_static.py::test_package_submodules -s
        pass

    Ignore:
        import sys
        sys.path.append('/home/joncrall/code/xdoctest/testing')
        from test_static import *
        temp = utils.TempDir()
        temp.__enter__()
        sys.path.append(temp.dpath)

        temp.__exit__(None, None, None)
    """
    with utils.TempDir() as temp:
        dpath = temp.dpath

        # Create a dummy package heirachy
        root = utils.ensuredir((dpath, '_tmproot'))
        sub1 = utils.ensuredir((root, 'sub1'))
        sub2 = utils.ensuredir((sub1, 'sub2'))

        root_init = touch((root, '__init__.py'))
        sub1_init = touch((sub1, '__init__.py'))
        sub2_init = touch((sub2, '__init__.py'))

        mod0 = touch((root, 'mod0.py'))
        mod1 = touch((sub1, 'mod1.py'))
        mod2 = touch((sub2, 'mod2.py'))

        root_main = touch((root, '__main__.py'))
        sub2_main = touch((sub2, '__main__.py'))

        bad1 = utils.ensuredir((root, 'bad1'))
        bad2 = utils.ensuredir((sub1, 'bad2'))
        b0 = touch((bad1, 'b0.py'))
        b1 = touch((bad2, 'b1.py'))

        with utils.PythonPathContext(dpath):
            subpaths = sorted(static.package_modpaths(root, with_pkg=True))

            # should only return files not directories
            assert root_init in subpaths
            assert sub1_init in subpaths
            assert sub2_init in subpaths
            assert root not in subpaths
            assert sub1 not in subpaths
            assert sub2 not in subpaths

            assert root_main in subpaths
            assert sub2_main in subpaths

            assert mod0 in subpaths
            assert mod1 in subpaths
            assert mod2 in subpaths

            assert bad1 not in subpaths
            assert b0 not in subpaths
            assert b1 not in subpaths

            assert '_tmproot' not in sys.modules
            assert '_tmproot.mod0' not in sys.modules
            assert '_tmproot.sub1' not in sys.modules
            assert '_tmproot.sub1.mod1' not in sys.modules
            assert '_tmproot.sub1.sub2' not in sys.modules
            assert '_tmproot.sub1.mod2.mod2' not in sys.modules
Esempio n. 21
0
def test_modname_to_modpath_package():
    """
    CommandLine:
        pytest testing/test_static.py::test_modname_to_modpath_package

    Ignore:
        import sys
        sys.path.append('/home/joncrall/code/xdoctest/testing')
        from test_static import *
        temp = utils.TempDir()
        temp.__enter__()
        sys.path.append(temp.dpath)

        temp.__exit__(None, None, None)
    """
    with utils.TempDir() as temp:
        dpath = temp.dpath

        # Create a dummy package heirachy
        root = utils.ensuredir((dpath, '_tmproot'))
        sub1 = utils.ensuredir((root, 'sub1'))
        sub2 = utils.ensuredir((sub1, 'sub2'))

        root_init = touch((root, '__init__.py'))
        sub1_init = touch((sub1, '__init__.py'))
        sub2_init = touch((sub2, '__init__.py'))

        mod0 = touch((root, 'mod0.py'))
        mod1 = touch((sub1, 'mod1.py'))
        mod2 = touch((sub2, 'mod2.py'))

        root_main = touch((root, '__main__.py'))
        sub2_main = touch((sub2, '__main__.py'))

        bad1 = utils.ensuredir((root, 'bad1'))
        bad2 = utils.ensuredir((sub1, 'bad2'))
        touch((bad1, 'b0.py'))
        touch((bad2, 'b0.py'))

        with utils.PythonPathContext(dpath):
            # Bad module directories should return None
            assert _static_modname_to_modpath('_tmproot.bad1') is None
            assert _static_modname_to_modpath('_tmproot.sub1.bad1') is None
            assert _static_modname_to_modpath('_tmproot.bad1.b0') is None
            assert _static_modname_to_modpath('_tmproot.sub1.bad1.b1') is None
            assert _static_modname_to_modpath('_tmproot.bad1') is None

            # package modules are accessable by the full path
            assert root == _static_modname_to_modpath('_tmproot')
            assert sub1 == _static_modname_to_modpath('_tmproot.sub1')
            assert sub2 == _static_modname_to_modpath('_tmproot.sub1.sub2')
            assert mod0 == _static_modname_to_modpath('_tmproot.mod0')
            assert mod1 == _static_modname_to_modpath('_tmproot.sub1.mod1')
            assert mod2 == _static_modname_to_modpath(
                '_tmproot.sub1.sub2.mod2')

            # specifying a suffix will not work
            assert _static_modname_to_modpath('sub1') is None
            assert _static_modname_to_modpath('sub1.sub2') is None
            assert _static_modname_to_modpath('mod0') is None
            assert _static_modname_to_modpath('sub1.mod1') is None
            assert _static_modname_to_modpath('sub1.sub2.mod2') is None

            # Specify init if available
            assert root_init == _static_modname_to_modpath('_tmproot',
                                                           hide_init=False)

            if 1:
                # Test init
                assert _static_modname_to_modpath('_tmproot',
                                                  hide_init=False) == root_init
                assert _static_modname_to_modpath('_tmproot.__init__',
                                                  hide_init=False) == root_init
                assert _static_modname_to_modpath('_tmproot.__main__',
                                                  hide_init=False,
                                                  hide_main=True) == root

                # Test main
                assert _static_modname_to_modpath('_tmproot',
                                                  hide_main=False) == root
                assert _static_modname_to_modpath('_tmproot.__init__',
                                                  hide_main=False) == root
                assert _static_modname_to_modpath('_tmproot.__main__',
                                                  hide_main=False) == root_main

                # Test init and main both false
                assert _static_modname_to_modpath('_tmproot.__init__') == root
                assert _static_modname_to_modpath('_tmproot.__main__',
                                                  hide_main=True) == root

                # Test init and main both true
                assert _static_modname_to_modpath('_tmproot',
                                                  hide_init=False,
                                                  hide_main=False) == root_init
                assert _static_modname_to_modpath('_tmproot.__init__',
                                                  hide_init=False,
                                                  hide_main=False) == root_init
                assert _static_modname_to_modpath('_tmproot.__main__',
                                                  hide_init=False,
                                                  hide_main=False) == root_main

            if 2:
                # Test in a nested directory
                # Test init
                assert _static_modname_to_modpath('_tmproot.sub1.sub2',
                                                  hide_init=False) == sub2_init
                assert _static_modname_to_modpath(
                    '_tmproot.sub1.sub2.__init__',
                    hide_init=False) == sub2_init
                assert _static_modname_to_modpath(
                    '_tmproot.sub1.sub2.__main__',
                    hide_init=False,
                    hide_main=True) == sub2

                # Test main
                assert _static_modname_to_modpath('_tmproot.sub1.sub2',
                                                  hide_main=False) == sub2
                assert _static_modname_to_modpath(
                    '_tmproot.sub1.sub2.__main__',
                    hide_main=False) == sub2_main
                assert _static_modname_to_modpath(
                    '_tmproot.sub1.sub2.__init__', hide_main=False) == sub2

                # Test init and main both false
                assert _static_modname_to_modpath(
                    '_tmproot.sub1.sub2.__init__', hide_main=True) == sub2
                assert _static_modname_to_modpath(
                    '_tmproot.sub1.sub2.__main__', hide_main=True) == sub2

                # Test init and main both true
                assert _static_modname_to_modpath('_tmproot.sub1.sub2',
                                                  hide_init=False,
                                                  hide_main=False) == sub2_init
                assert _static_modname_to_modpath(
                    '_tmproot.sub1.sub2.__init__',
                    hide_init=False,
                    hide_main=False) == sub2_init
                assert _static_modname_to_modpath(
                    '_tmproot.sub1.sub2.__main__',
                    hide_init=False,
                    hide_main=False) == sub2_main

            if 3:
                # Test in a nested directory with __init__ but no __main__
                # Test init
                assert _static_modname_to_modpath('_tmproot.sub1',
                                                  hide_init=False) == sub1_init
                assert _static_modname_to_modpath('_tmproot.sub1.__init__',
                                                  hide_init=False) == sub1_init
                assert _static_modname_to_modpath('_tmproot.sub1.__main__',
                                                  hide_init=False) is None

                # Test main
                assert _static_modname_to_modpath('_tmproot.sub1',
                                                  hide_main=False) == sub1
                assert _static_modname_to_modpath('_tmproot.sub1.__main__',
                                                  hide_main=False) is None
                assert _static_modname_to_modpath('_tmproot.sub1.__init__',
                                                  hide_main=False) == sub1

                # Test init and main both false
                assert _static_modname_to_modpath(
                    '_tmproot.sub1.__init__') == sub1
                assert _static_modname_to_modpath(
                    '_tmproot.sub1.__main__') is None

                # Test init and main both true
                assert _static_modname_to_modpath('_tmproot.sub1',
                                                  hide_init=False,
                                                  hide_main=False) == sub1_init
                assert _static_modname_to_modpath('_tmproot.sub1.__init__',
                                                  hide_init=False,
                                                  hide_main=False) == sub1_init
                assert _static_modname_to_modpath('_tmproot.sub1.__main__',
                                                  hide_init=False,
                                                  hide_main=False) is None

            assert '_tmproot' not in sys.modules
            assert '_tmproot.mod0' not in sys.modules
            assert '_tmproot.sub1' not in sys.modules
            assert '_tmproot.sub1.mod1' not in sys.modules
            assert '_tmproot.sub1.sub2' not in sys.modules
            assert '_tmproot.sub1.mod2.mod2' not in sys.modules
Esempio n. 22
0
def test_runner_failures():
    """
    python testing/test_runner.py  test_runner_failures
    pytest testing/test_runner.py::test_runner_failures -s
    pytest testing/test_runner.py::test_all_disabled -s
    """
    from xdoctest import runner

    source = utils.codeblock('''
        def test1():
            """
                Example:
                    >>> pass
            """

        def test2():
            """
                Example:
                    >>> assert False, 'test 2.1'

                Example:
                    >>> assert False, 'test 2.2'
            """

        def test3():
            """
                Example:
                    >>> pass

                Example:
                    >>> pass
            """

        def test4():
            """
                Example:
                    >>> assert False, 'test 3'
            """
        ''')

    temp = utils.TempDir()
    temp.ensure()
    # with utils.TempDir() as temp:
    dpath = temp.dpath
    modpath = join(dpath, 'test_runner_failures.py')

    with open(modpath, 'w') as file:
        file.write(source)

    # disabled tests dont run in "all" mode
    with utils.CaptureStdout(supress=True) as cap:
        try:
            runner.doctest_module(modpath, 'all', argv=[''], verbose=1)
        except Exception:
            pass

    print('\nNOTE: the following output is part of a test')
    print(utils.indent(cap.text, '... '))
    print('NOTE: above output is part of a test')

    # assert '.FFF' in cap.text
    assert '3 / 6 passed' in cap.text
    assert '3 failed 3 passed' in cap.text
Esempio n. 23
0
def test_modpath_to_modname():
    """
    CommandLine:
        pytest testing/test_static.py::test_modpath_to_modname -s
        python testing/test_static.py test_modpath_to_modname
    """
    with utils.TempDir() as temp:
        dpath = temp.dpath

        # Create a dummy package heirachy
        root = utils.ensuredir((dpath, '_tmproot'))
        sub1 = utils.ensuredir((root, 'sub1'))
        sub2 = utils.ensuredir((sub1, 'sub2'))

        root_init = touch((root, '__init__.py'))
        sub1_init = touch((sub1, '__init__.py'))
        sub2_init = touch((sub2, '__init__.py'))

        mod0 = touch((root, 'mod0.py'))
        mod1 = touch((sub1, 'mod1.py'))
        mod2 = touch((sub2, 'mod2.py'))

        root_main = touch((root, '__main__.py'))
        sub2_main = touch((sub2, '__main__.py'))

        bad1 = utils.ensuredir((root, 'bad1'))
        bad2 = utils.ensuredir((sub1, 'bad2'))
        b0 = touch((bad1, 'b0.py'))
        b1 = touch((bad2, 'b1.py'))

        with utils.PythonPathContext(dpath):

            assert static.modpath_to_modname(root) == '_tmproot'
            assert static.modpath_to_modname(sub1) == '_tmproot.sub1'
            assert static.modpath_to_modname(sub2) == '_tmproot.sub1.sub2'

            assert static.modpath_to_modname(mod0) == '_tmproot.mod0'
            assert static.modpath_to_modname(mod1) == '_tmproot.sub1.mod1'
            assert static.modpath_to_modname(mod2) == '_tmproot.sub1.sub2.mod2'

            assert static.modpath_to_modname(root_init) == '_tmproot'
            assert static.modpath_to_modname(sub1_init) == '_tmproot.sub1'
            assert static.modpath_to_modname(sub2_init) == '_tmproot.sub1.sub2'

            assert static.modpath_to_modname(
                root_init, hide_init=False) == '_tmproot.__init__'
            assert static.modpath_to_modname(
                sub1_init, hide_init=False) == '_tmproot.sub1.__init__'
            assert static.modpath_to_modname(
                sub2_init, hide_init=False) == '_tmproot.sub1.sub2.__init__'

            assert static.modpath_to_modname(
                root, hide_main=True, hide_init=False) == '_tmproot.__init__'
            assert static.modpath_to_modname(
                sub1, hide_main=True,
                hide_init=False) == '_tmproot.sub1.__init__'
            assert static.modpath_to_modname(
                sub2, hide_main=True,
                hide_init=False) == '_tmproot.sub1.sub2.__init__'

            assert static.modpath_to_modname(
                root, hide_main=False, hide_init=False) == '_tmproot.__init__'
            assert static.modpath_to_modname(
                sub1, hide_main=False,
                hide_init=False) == '_tmproot.sub1.__init__'
            assert static.modpath_to_modname(
                sub2, hide_main=False,
                hide_init=False) == '_tmproot.sub1.sub2.__init__'

            assert static.modpath_to_modname(root,
                                             hide_main=False,
                                             hide_init=True) == '_tmproot'
            assert static.modpath_to_modname(sub1,
                                             hide_main=False,
                                             hide_init=True) == '_tmproot.sub1'
            assert static.modpath_to_modname(
                sub2, hide_main=False, hide_init=True) == '_tmproot.sub1.sub2'

            assert static.modpath_to_modname(
                root_main, hide_main=False,
                hide_init=True) == '_tmproot.__main__'
            assert static.modpath_to_modname(
                sub2_main, hide_main=False,
                hide_init=True) == '_tmproot.sub1.sub2.__main__'

            assert static.modpath_to_modname(
                root_main, hide_main=False,
                hide_init=True) == '_tmproot.__main__'
            assert static.modpath_to_modname(
                sub2_main, hide_main=False,
                hide_init=True) == '_tmproot.sub1.sub2.__main__'

            assert static.modpath_to_modname(root_main,
                                             hide_main=True,
                                             hide_init=True) == '_tmproot'
            assert static.modpath_to_modname(
                sub2_main, hide_main=True,
                hide_init=True) == '_tmproot.sub1.sub2'

            assert static.modpath_to_modname(root_main,
                                             hide_main=True,
                                             hide_init=False) == '_tmproot'
            assert static.modpath_to_modname(
                sub2_main, hide_main=True,
                hide_init=False) == '_tmproot.sub1.sub2'

            # Non-existant / invalid modules should always be None
            for a, b in it.product([True, False], [True, False]):
                with pytest.raises(ValueError):
                    static.modpath_to_modname(join(sub1, '__main__.py'),
                                              hide_main=a,
                                              hide_init=b)
                assert static.modpath_to_modname(b0, hide_main=a,
                                                 hide_init=b) == 'b0'
                assert static.modpath_to_modname(b1, hide_main=a,
                                                 hide_init=b) == 'b1'
                with pytest.raises(ValueError):
                    static.modpath_to_modname(bad1, hide_main=a, hide_init=b)
                with pytest.raises(ValueError):
                    static.modpath_to_modname(bad2, hide_main=a, hide_init=b)

            assert '_tmproot' not in sys.modules
            assert '_tmproot.mod0' not in sys.modules
            assert '_tmproot.sub1' not in sys.modules
            assert '_tmproot.sub1.mod1' not in sys.modules
            assert '_tmproot.sub1.sub2' not in sys.modules
            assert '_tmproot.sub1.mod2.mod2' not in sys.modules
Esempio n. 24
0
def test_runner_syntax_error():
    """
        python testing/test_errors.py test_runner_syntax_error

        xdoctest -m testing/test_errors.py test_runner_syntax_error
    """
    source = utils.codeblock(r'''
        def demo_parsetime_syntax_error1():
            """
                Example:
                    >>> from __future__ import print_function
                    >>> print 'Parse-Time Syntax Error'
            """

        def demo_parsetime_syntax_error2():
            """
                Example:
                    >>> def bad_syntax() return for
            """

        def demo_runtime_error():
            """
                Example:
                    >>> print('Runtime Error {}'.format(5 / 0))
            """

        def demo_runtime_name_error():
            """
                Example:
                    >>> print('Name Error {}'.format(foo))
            """

        def demo_runtime_warning():
            """
                Example:
                    >>> import warnings
                    >>> warnings.warn('in-code warning')
            """
        ''')

    temp = utils.TempDir(persist=True)
    temp.ensure()
    dpath = temp.dpath
    modpath = join(dpath, 'demo_runner_syntax_error.py')
    with open(modpath, 'w') as file:
        file.write(source)

    with utils.CaptureStdout() as cap:
        runner.doctest_module(modpath,
                              'all',
                              argv=[''],
                              style='freeform',
                              verbose=1)

    print('CAPTURED [[[[[[[[')
    print(utils.indent(cap.text))
    print(']]]]]]]] # CAPTURED')

    if six.PY2:
        captext = utils.ensure_unicode(cap.text)
    else:
        captext = cap.text

    if True or not six.PY2:  # Why does this have issues on the dashboards?
        assert '1 run-time warnings' in captext
        assert '2 parse-time warnings' in captext

        # Assert summary line
        assert '3 warnings' in captext
        assert '2 failed' in captext
        assert '1 passed' in captext