1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
import unittest |
3
|
|
|
from mock import patch, call, MagicMock, mock_open |
4
|
|
|
from awslambdahelper.cli import SetupCfgFile |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
class TestCliCfgManipulation(unittest.TestCase): |
8
|
|
|
def test_instantiation(self): |
9
|
|
|
setup = SetupCfgFile('setup', 'temp') |
10
|
|
|
|
11
|
|
|
self.assertEqual(setup.setup_cfg, 'setup') |
12
|
|
|
self.assertEqual(setup.temp_setup_cfg, 'temp') |
13
|
|
|
|
14
|
|
|
@patch('os.path.exists') |
15
|
|
|
def test_loadexists(self, path_exists): |
16
|
|
|
setup = SetupCfgFile('setup', 'temp') |
17
|
|
|
|
18
|
|
|
setup.read = MagicMock() |
19
|
|
|
path_exists.return_value = True |
20
|
|
|
setup.load() |
21
|
|
|
|
22
|
|
|
setup.read.assert_called_once_with('setup') |
23
|
|
|
|
24
|
|
|
@patch('os.path.exists') |
25
|
|
|
def test_loadnotexist(self, path_exists): |
26
|
|
|
setup = SetupCfgFile('setup', 'temp') |
27
|
|
|
|
28
|
|
|
setup.read = MagicMock() |
29
|
|
|
path_exists.return_value = False |
30
|
|
|
setup.load() |
31
|
|
|
|
32
|
|
|
setup.read.assert_not_called() |
33
|
|
|
|
34
|
|
|
@patch('ConfigParser.ConfigParser.write') |
35
|
|
|
def test_writeexistinginstall(self, super_write): |
36
|
|
|
""" |
37
|
|
|
Test the case where we already have 'install' entry in our setup.cfg |
38
|
|
|
""" |
39
|
|
|
setup = SetupCfgFile('setup', 'temp') |
40
|
|
|
setup.sections = MagicMock() |
41
|
|
|
setup.set = MagicMock() |
42
|
|
|
setup.add_section = MagicMock() |
43
|
|
|
|
44
|
|
|
setup.sections.return_value = ['install'] |
45
|
|
|
|
46
|
|
|
m = mock_open() |
47
|
|
|
with patch('__builtin__.open', m): |
48
|
|
|
setup.write() |
49
|
|
|
|
50
|
|
|
setup.add_section.assert_not_called() |
51
|
|
|
setup.set.assert_called_once_with('install', 'prefix', '') |
52
|
|
|
m.assert_called_once_with('temp', 'w') |
53
|
|
|
|
54
|
|
|
@patch('ConfigParser.ConfigParser.write') |
55
|
|
|
def test_writemissinginstall(self, super_write): |
56
|
|
|
""" |
57
|
|
|
Test the case where we already have 'install' entry in our setup.cfg |
58
|
|
|
""" |
59
|
|
|
setup = SetupCfgFile('setup', 'temp') |
60
|
|
|
setup.sections = MagicMock() |
61
|
|
|
setup.set = MagicMock() |
62
|
|
|
setup.add_section = MagicMock() |
63
|
|
|
|
64
|
|
|
setup.sections.return_value = [] |
65
|
|
|
|
66
|
|
|
m = mock_open() |
67
|
|
|
with patch('__builtin__.open', m): |
68
|
|
|
setup.write() |
69
|
|
|
|
70
|
|
|
setup.add_section.assert_called_once_with('install') |
71
|
|
|
setup.set.assert_called_once_with('install', 'prefix', '') |
72
|
|
|
m.assert_called_once_with('temp', 'w') |
73
|
|
|
|