Пример #1
0
def test_encoding(mocker, log_mock):
    mi_cfg = cli.Config(**BASE_CONFIG.config_values)
    mi_cfg.config_values.update(MI_CONFIG.config_values)
    raw_cfg = cli.Config(**BASE_CONFIG.config_values)
    raw_cfg.config_values.update(RAW_CONFIG.config_values)
    mappings = {
        MIHarvester: mi_cfg,
        RawHarvester: raw_cfg,
        CCHarvester: CC_CONFIG,
    }
    if sys.version_info[0] < 3:
        target = 'data/__init__.py'
    else:
        target = 'data/py3unicode.py'
    fnames = [
        os.path.join(DIRNAME, target),
        # This one will fail if detect_encoding() removes the first lines
        # See #133
        os.path.join(DIRNAME, 'data/no_encoding.py'),
    ]
    for h_class, cfg in mappings.items():
        for f in fnames:
            harvester = h_class([f], cfg)
            assert not any(
                ['error' in kw for msg, args, kw in harvester.to_terminal()])
Пример #2
0
def test_ipynb(log_mock):
    mi_cfg = cli.Config(**BASE_CONFIG_WITH_IPYNB.config_values)
    mi_cfg.config_values.update(MI_CONFIG.config_values)
    raw_cfg = cli.Config(**BASE_CONFIG_WITH_IPYNB.config_values)
    raw_cfg.config_values.update(RAW_CONFIG.config_values)
    cc_cfg = cli.Config(
        order=lambda block: block.name,
        no_assert=False,
        min='A',
        max='F',
        show_complexity=False,
        show_closures=False,
        average=True,
        total_average=False,
    )
    cc_cfg.config_values.update(BASE_CONFIG_WITH_IPYNB.config_values)

    mappings = {
        MIHarvester: mi_cfg,
        RawHarvester: raw_cfg,
        CCHarvester: cc_cfg,
    }
    target = 'data/'
    fnames = [
        os.path.join(DIRNAME, target),
    ]
    for h_class, cfg in mappings.items():
        for f in fnames:
            harvester = h_class([f], cfg)
            out = harvester.as_json()
            assert not any(['error' in out]), out
Пример #3
0
def test_cc(mocker, log_mock):
    harv_mock = mocker.patch('radon.cli.CCHarvester')
    harv_mock.return_value = mocker.sentinel.harvester

    cli.cc(['-'], json=True)

    harv_mock.assert_called_once_with(['-'],
                                      cli.Config(min='A',
                                                 max='F',
                                                 exclude=None,
                                                 ignore=None,
                                                 show_complexity=False,
                                                 average=False,
                                                 order=getattr(
                                                     cc_mod, 'SCORE'),
                                                 no_assert=False,
                                                 total_average=False,
                                                 show_closures=False,
                                                 include_ipynb=False,
                                                 ipynb_cells=False))
    log_mock.assert_called_once_with(mocker.sentinel.harvester,
                                     codeclimate=False,
                                     json=True,
                                     stream=sys.stdout,
                                     xml=False)
Пример #4
0
    def test_raw(self, harv_mock, log_mock):
        harv_mock.return_value = mock.sentinel.harvester

        cli.raw(['-'], summary=True, json=True)

        harv_mock.assert_called_once_with(['-'], cli.Config(exclude=None,
                                                            ignore=None,
                                                            summary=True))
        log_mock.assert_called_once_with(mock.sentinel.harvester, json=True)
Пример #5
0
    def test_mi(self, harv_mock, log_mock):
        harv_mock.return_value = mock.sentinel.harvester

        cli.mi(['-'], show=True, multi=False)

        harv_mock.assert_called_once_with(['-'], cli.Config(
            min='A', max='C', exclude=None, ignore=None, show=True,
            multi=False))
        log_mock.assert_called_once_with(mock.sentinel.harvester, json=False)
Пример #6
0
def test_mi(mocker, log_mock):
    harv_mock = mocker.patch('radon.cli.MIHarvester')
    harv_mock.return_value = mocker.sentinel.harvester

    cli.mi(['-'], show=True, multi=False)

    harv_mock.assert_called_once_with(['-'], cli.Config(
        min='A', max='C', exclude=None, ignore=None, show=True,
        multi=False, sort=False))
    log_mock.assert_called_once_with(mocker.sentinel.harvester, json=False)
Пример #7
0
def test_raw(mocker, log_mock):
    harv_mock = mocker.patch('radon.cli.RawHarvester')
    harv_mock.return_value = mocker.sentinel.harvester

    cli.raw(['-'], summary=True, json=True)

    harv_mock.assert_called_once_with(['-'], cli.Config(exclude=None,
                                                        ignore=None,
                                                        summary=True))
    log_mock.assert_called_once_with(mocker.sentinel.harvester, json=True)
Пример #8
0
    def test_cc(self, harv_mock, log_mock):
        harv_mock.return_value = mock.sentinel.harvester

        cli.cc(['-'], json=True)

        harv_mock.assert_called_once_with(['-'], cli.Config(
            min='A', max='F', exclude=None, ignore=None, show_complexity=False,
            average=False, order=getattr(cc_mod, 'SCORE'), no_assert=False,
            total_average=False, show_closures=False))
        log_mock.assert_called_once_with(mock.sentinel.harvester, codeclimate=False, json=True,
                                         xml=False)
Пример #9
0
def test_raw_ipynb(log_mock):
    raw_cfg = cli.Config(**BASE_CONFIG_WITH_IPYNB.config_values)

    target = os.path.join(DIRNAME, 'data/example.ipynb')
    harvester = RawHarvester([DIRNAME], raw_cfg)
    out = json.loads(harvester.as_json())
    assert harvester.config.include_ipynb == True
    assert target in out
    assert out[target]['loc'] == 63
    assert out[target]['lloc'] == 37
    assert out[target]['sloc'] == 37
    assert out[target]['comments'] == 3
    assert out[target]['multi'] == 10
    assert out[target]['blank'] == 14
    assert out[target]['single_comments'] == 2
Пример #10
0
def test_raw(mocker, log_mock):
    harv_mock = mocker.patch('radon.cli.RawHarvester')
    harv_mock.return_value = mocker.sentinel.harvester

    cli.raw(['-'], summary=True, json=True)

    harv_mock.assert_called_once_with(['-'],
                                      cli.Config(exclude=None,
                                                 ignore=None,
                                                 summary=True,
                                                 include_ipynb=False,
                                                 ipynb_cells=False))
    log_mock.assert_called_once_with(mocker.sentinel.harvester,
                                     stream=sys.stdout,
                                     json=True)
Пример #11
0
def test_raw_ipynb_cells(log_mock):
    raw_cfg = cli.Config(**BASE_CONFIG_WITH_IPYNB_AND_CELLS.config_values)

    target = os.path.join(DIRNAME, 'data/example.ipynb')
    harvester = RawHarvester([DIRNAME], raw_cfg)
    out = json.loads(harvester.as_json())
    cell_target = target + ":[3]"
    assert target in out
    assert cell_target in out
    assert out[cell_target]['loc'] == 52
    assert out[cell_target]['lloc'] == 27
    assert out[cell_target]['sloc'] == 27
    assert out[cell_target]['comments'] == 3
    assert out[cell_target]['multi'] == 10
    assert out[cell_target]['blank'] == 13
    assert out[cell_target]['single_comments'] == 2
Пример #12
0
import os
import json

import pytest
import radon.cli as cli
from radon.cli.harvest import MIHarvester, RawHarvester, CCHarvester, SUPPORTS_IPYNB, Harvester
from radon.cli.tools import _is_python_file

from radon.tests.test_cli_harvest import RAW_CONFIG, MI_CONFIG

BASE_CONFIG_WITH_IPYNB = cli.Config(
    exclude="*.py",
    ignore=None,
    include_ipynb=True,
    ipynb_cells=False,
)

BASE_CONFIG_WITH_IPYNB_AND_CELLS = cli.Config(
    exclude="*.py",
    ignore=None,
    include_ipynb=True,
    ipynb_cells=True,
)

DIRNAME = os.path.dirname(__file__)


@pytest.mark.skipif(not SUPPORTS_IPYNB, reason="nbformat not installed")
def test_harvestor_yields_ipynb(log_mock):
    '''Test that Harvester will try ipynb files when configured'''
    target = os.path.join(DIRNAME, 'data/example.ipynb')
Пример #13
0
 def test_eq(self):
     self.assertEqual(cli.Config(), cli.Config())
     self.assertEqual(cli.Config(a=2), cli.Config(a=2))
     self.assertNotEqual(cli.Config(a=2), cli.Config(b=2))
Пример #14
0
 def test_exceptions(self):
     c = cli.Config(a=2)
     self.assertEqual(c.__dict__, {'config_values': {'a': 2}})
     self.assertRaises(AttributeError, lambda: c.notexistent)
Пример #15
0
def test_config_exceptions():
    c = cli.Config(a=2)
    assert c.__dict__, {'config_values': {'a': 2}}
    with pytest.raises(AttributeError):
        c.notexistent
Пример #16
0
def test_config_base_behavior():
    c = cli.Config(a=2, b=3)
    assert c.config_values == {'a': 2, 'b': 3}
    assert c.a == 2
    assert c.b == 3
Пример #17
0
 def test_base_behavior(self):
     c = cli.Config(a=2, b=3)
     self.assertEqual(c.config_values, {'a': 2, 'b': 3})
     self.assertEqual(c.a, 2)
     self.assertEqual(c.b, 3)
Пример #18
0
def test_config_str():
    assert str(cli.Config()) == '{}'
    assert str(cli.Config(a=2)) == '{\'a\': 2}'
Пример #19
0
 def test_str(self):
     self.assertEqual(str(cli.Config()), '{}')
     self.assertEqual(str(cli.Config(a=2)), '{\'a\': 2}')
Пример #20
0
def test_config_eq():
    assert cli.Config() == cli.Config()
    assert cli.Config(a=2) == cli.Config(a=2)
    assert cli.Config(a=2) != cli.Config(b=2)
Пример #21
0
 def test_for(self):
     self.assertEqual(cli.Config.from_function(func),
                      cli.Config(b=2, c=[], d=None))
     self.assertEqual(cli.Config.from_function(func2), cli.Config())
     self.assertEqual(cli.Config.from_function(func3), cli.Config(b=3))
Пример #22
0
def test_config_for():
    assert cli.Config.from_function(func) == cli.Config(b=2, c=[], d=None)
    assert cli.Config.from_function(func2) == cli.Config()
    assert cli.Config.from_function(func3) == cli.Config(b=3)