summaryrefslogtreecommitdiff
path: root/unit_tests/test_cover_plugin.py
blob: 599941306ede26e520324e4225d49498153494aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import os
from optparse import OptionParser
from nose.config import Config
from nose.plugins.cover import Coverage
from nose.tools import eq_


class TestCoveragePlugin(object):

    def test_cover_options_packages(self):
        _test_options_helper('--cover-package', 'coverPackages',
                             ['pkg1', 'pkg2', 'pkg3'], [],
                             'pkg1,pkg2,pkg3', 'NOSE_COVER_PACKAGE')

    def test_cover_options_noprint(self):
        _test_options_helper('--cover-no-print', 'coverPrint',
                             False, True,
                             env_key='NOSE_COVER_NO_PRINT')

    def test_cover_options_erase(self):
        _test_options_helper('--cover-erase', 'coverErase',
                             True, False,
                             env_key='NOSE_COVER_ERASE')

    def test_cover_options_tests(self):
        _test_options_helper('--cover-tests', 'coverTests',
                             True, False,
                             env_key='NOSE_COVER_TESTS')

    def test_cover_options_config_file(self):
        def get_sys_info(cov_inst):
            # Older coverage uses sysinfo, while newer coverage uses sys_info.
            if hasattr(cov_inst, 'sysinfo'):
                return cov_inst.sysinfo()
            else:
                return cov_inst.sys_info()

        def get_config_files(cov_info):
            cov_info = dict(cov_info)
            if 'config_files' in cov_info:
                return cov_info['config_files']
            return None

        f = open('not_default_config_file', 'wb')
        f.close()
        try:
            c1, c2 = _test_options_helper(
                '--cover-config-file', 'coverConfigFile',
                'not_default_config_file', True,
                arg_value='not_default_config_file',
                env_key='NOSE_COVER_CONFIG_FILE')

            cov_info = get_sys_info(c1.coverInstance)
            config_files = get_config_files(cov_info)
            if config_files is not None:
                assert '.coveragerc' in config_files
            else:
                assert False, "coverage did not load default config file"

            cov_info = get_sys_info(c2.coverInstance)
            config_files = get_config_files(cov_info)
            if config_files is not None:
                assert 'not_default_config_file' in config_files
            else:
                assert False, "coverage did not load expected config file"
        finally:
            os.unlink('not_default_config_file')


def _test_options_helper(arg_option, cover_option,
                         expected_set, expected_not_set,
                         arg_value=None, env_key=None):
    prefix_args = ['test_can_be_disabled', '--with-coverage']

    # Assert that the default works as expected
    parser = OptionParser()
    c_arg_not_set = Coverage()
    c_arg_not_set.addOptions(parser)
    options, _ = parser.parse_args(prefix_args)
    c_arg_not_set.configure(options, Config())
    eq_(expected_not_set, getattr(c_arg_not_set, cover_option))

    # Assert that the argument parser picks up the expected value
    parser = OptionParser()
    c_arg_set = Coverage()
    c_arg_set.addOptions(parser)

    args = arg_option
    if arg_value is not None:
        args += '=' + arg_value

    options, _ = parser.parse_args(prefix_args + [args])
    c_arg_set.configure(options, Config())
    eq_(expected_set, getattr(c_arg_set, cover_option))

    # If the option supports environment variables, check that too
    if env_key is not None:
        args = 'true'
        if arg_value is not None:
            args = arg_value

        env = {env_key: args}
        c = Coverage()
        parser = OptionParser()
        c.addOptions(parser, env)
        options, _ = parser.parse_args(prefix_args)
        c.configure(options, Config())
        eq_(expected_set, getattr(c, cover_option))

    return c_arg_not_set, c_arg_set