Пример #1
0
    def test_open(self):
        with tools._open('-') as fobj:
            self.assertTrue(fobj is sys.stdin)

        m = mock.mock_open()
        with mock.patch('radon.cli.tools.open', m, create=True):
            tools._open('randomfile.py').__enter__()
        m.assert_called_with('randomfile.py')
Пример #2
0
    def test_open(self):
        with tools._open('-') as fobj:
            self.assertTrue(fobj is sys.stdin)

        m = mock.mock_open()
        with mock.patch('radon.cli.tools.open', m, create=True):
            tools._open('randomfile.py').__enter__()
        m.assert_called_with('randomfile.py')
Пример #3
0
    def test_open(self):
        with tools._open('-') as fobj:
            self.assertTrue(fobj is sys.stdin)

        m = mock.mock_open()
        with mock.patch('radon.cli.tools.open', m, create=True):
            tools._open('randomfile.py').__enter__()

        if platform.python_implementation() == 'PyPy':
            m.assert_called_with('randomfile.py')
        else:
            except_encoding = os.getenv('RADONFILESENCODING',
                                        locale.getpreferredencoding(False))
            m.assert_called_with('randomfile.py', encoding=except_encoding)
Пример #4
0
    def run(self):
        '''Start the analysis. For every file, this method calls the
        :meth:`gobble` method. Results are yielded as tuple:
        ``(filename, analysis_results)``.
        '''
        for name in self._iter_filenames():
            with _open(name) as fobj:
                try:
                    if name.endswith('.ipynb'):
                        if SUPPORTS_IPYNB and self.config.include_ipynb:
                            nb = nbformat.read(fobj,
                                               as_version=nbformat.NO_CONVERT)
                            cells = [
                                cell.source for cell in nb.cells
                                if cell.cell_type == 'code'
                            ]
                            # Whole document
                            doc = "\n".join(cells)
                            yield (name,
                                   self.gobble(StringIO(strip_ipython(doc))))

                            if self.config.ipynb_cells:
                                # Individual cells
                                cellid = 0
                                for source in cells:
                                    yield ("{0}:[{1}]".format(name, cellid),
                                           self.gobble(
                                               StringIO(
                                                   strip_ipython(source))))
                                    cellid += 1
                    else:
                        yield (name, self.gobble(fobj))
                except Exception as e:
                    yield (name, {'error': str(e)})
Пример #5
0
 def run(self):
     '''Start the analysis. For every file, this method calls the
     :meth:`gobble` method. Results are yielded as tuple:
     ``(filename, analysis_results)``.
     '''
     for name in self._iter_filenames():
         with _open(name) as fobj:
             try:
                 yield (name, self.gobble(fobj))
             except Exception as e:
                 yield (name, {'error': str(e)})
Пример #6
0
 def run(self):
     '''Start the analysis. For every file, this method calls the
     :meth:`gobble` method. Results are yielded as tuple:
     ``(filename, analysis_results)``.
     '''
     for name in self._iter_filenames():
         with _open(name) as fobj:
             try:
                 yield (name, self.gobble(fobj))
             except Exception as e:
                 yield (name, {'error': str(e)})
Пример #7
0
def test_open(mocker):
    with tools._open('-') as fobj:
        assert fobj is sys.stdin

    try:
        with tools._open(__file__) as fobj:
            assert True
    except TypeError:  # issue 101
        assert False, 'tools._open raised TypeError'

    m = mocker.mock_open()

    if platform.python_implementation() == 'PyPy':
        mocker.patch('radon.cli.tools.open', m, create=True)
        tools._open('randomfile.py').__enter__()
        m.assert_called_with('randomfile.py')
    else:
        mocker.patch('radon.cli.tools._open_function', m, create=True)
        tools._open('randomfile.py').__enter__()
        if sys.version_info[:2] >= (3, 0):
            default_encoding = 'utf-8'
        else:
            default_encoding = locale.getpreferredencoding(False)
        except_encoding = os.getenv('RADONFILESENCODING', default_encoding)
        m.assert_called_with('randomfile.py', encoding=except_encoding)
Пример #8
0
def test_open(mocker):
    with tools._open('-') as fobj:
        assert fobj is sys.stdin

    try:
        with tools._open(__file__) as fobj:
            assert True
    except TypeError:  # issue 101
        assert False, 'tools._open raised TypeError'

    m = mocker.mock_open()

    if platform.python_implementation() == 'PyPy':
        mocker.patch('radon.cli.tools.open', m, create=True)
        tools._open('randomfile.py').__enter__()
        m.assert_called_with('randomfile.py')
    else:
        mocker.patch('radon.cli.tools._open_function', m, create=True)
        tools._open('randomfile.py').__enter__()
        except_encoding = os.getenv('RADONFILESENCODING',
                                    locale.getpreferredencoding(False))
        m.assert_called_with('randomfile.py', encoding=except_encoding)