Completed
Push — master ( 1eecb7...7e6e06 )
by Kale
187:13 queued 122:08
created

test_conda_comment_version_parsin()   A

Complexity

Conditions 3

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
dl 0
loc 9
rs 9.95
c 1
b 0
f 0
1
from os.path import dirname
2
import unittest
3
4
from .decorators import skip_if_no_mock
5
from .helpers import mock
6
from .test_create import make_temp_prefix
7
8
from conda import history
9
from conda.resolve import MatchSpec
10
11
12
class HistoryTestCase(unittest.TestCase):
13
    def test_works_as_context_manager(self):
14
        h = history.History("/path/to/prefix")
15
        self.assertTrue(getattr(h, '__enter__'))
16
        self.assertTrue(getattr(h, '__exit__'))
17
18
    @skip_if_no_mock
19
    def test_calls_update_on_exit(self):
20
        h = history.History("/path/to/prefix")
21
        with mock.patch.object(h, 'init_log_file') as init_log_file:
22
            init_log_file.return_value = None
23
            with mock.patch.object(h, 'update') as update:
24
                with h:
25
                    self.assertEqual(0, update.call_count)
26
                    pass
27
            self.assertEqual(1, update.call_count)
28
29
    @skip_if_no_mock
30
    def test_returns_history_object_as_context_object(self):
31
        h = history.History("/path/to/prefix")
32
        with mock.patch.object(h, 'init_log_file') as init_log_file:
33
            init_log_file.return_value = None
34
            with mock.patch.object(h, 'update'):
35
                with h as h2:
36
                    self.assertEqual(h, h2)
37
38
    @skip_if_no_mock
39
    def test_empty_history_check_on_empty_env(self):
40
        with mock.patch.object(history.History, 'file_is_empty') as mock_file_is_empty:
41
            with history.History(make_temp_prefix()) as h:
42
                self.assertEqual(mock_file_is_empty.call_count, 0)
43
            self.assertEqual(mock_file_is_empty.call_count, 0)
44
            assert h.file_is_empty()
45
        self.assertEqual(mock_file_is_empty.call_count, 1)
46
        assert not h.file_is_empty()
47
48
    @skip_if_no_mock
49
    def test_parse_on_empty_env(self):
50
        with mock.patch.object(history.History, 'parse') as mock_parse:
51
            with history.History(make_temp_prefix()) as h:
52
                self.assertEqual(mock_parse.call_count, 0)
53
                self.assertEqual(len(h.parse()), 0)
54
        self.assertEqual(len(h.parse()), 1)
55
56
57
class UserRequestsTestCase(unittest.TestCase):
58
59
    h = history.History(dirname(__file__))
60
    user_requests = h.get_user_requests()
61
62
    def test_len(self):
63
        self.assertEqual(len(self.user_requests), 6)
64
65
    def test_0(self):
66
        self.assertEqual(self.user_requests[0],
67
                         {'cmd': ['conda', 'update', 'conda'],
68
                          'date': '2016-02-16 13:31:33',
69
                          'unlink_dists': (),
70
                          'link_dists': (),
71
                          })
72
73
    def test_last(self):
74
        self.assertEqual(self.user_requests[-1],
75
                         {'action': 'install',
76
                          'cmd': ['conda', 'install', 'pyflakes'],
77
                          'date': '2016-02-18 22:53:20',
78
                          'specs': ['pyflakes', 'conda', 'python 2.7*'],
79
                          'update_specs': ['pyflakes', 'conda', 'python 2.7*'],
80
                          'unlink_dists': (),
81
                          'link_dists': ['+pyflakes-1.0.0-py27_0'],
82
                          })
83
84
    def test_conda_comment_version_parsin(self):
85
        test_cases = [
86
            "# conda version: 4.5.1",
87
            "# conda version: 4.5.1rc1",
88
            "# conda version: 4.5.1dev0",
89
        ]
90
        for line in test_cases:
91
            item = history.History._parse_comment_line(line)
92
            assert not item
93
94
    def test_action_command_comment_parsing(self):
95
        test_cases = [
96
            # New format (>=4.5)
97
            "# update specs: [\"param[version='>=1.5.1,<2.0']\"]",
98
            # Old format (<4.5)
99
            '# install specs: param >=1.5.1,<2.0',
100
            '# install specs: python>=3.5.1,jupyter >=1.0.0,<2.0,matplotlib >=1.5.1,<2.0,numpy >=1.11.0,<2.0,pandas >=0.19.2,<1.0,psycopg2 >=2.6.1,<3.0,pyyaml >=3.12,<4.0,scipy >=0.17.0,<1.0',
101
        ]
102
        for line in test_cases:
103
            item = history.History._parse_comment_line(line)
104
            specs = item.get('specs')
105
            for spec in specs:
106
                try:
107
                    MatchSpec(spec)
108
                except Exception as e:
109
                    print('Specs item:', item)
110
                    print('Specs:', specs)
111
                    print('Invalid Spec:', spec)
112
                    raise Exception(e)
113