1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
""" |
3
|
|
|
Test the CLI Argument Parser |
4
|
|
|
""" |
5
|
|
|
import os |
6
|
|
|
import unittest |
7
|
|
|
from argparse import Namespace, ArgumentError |
8
|
|
|
from mock import patch, call |
9
|
|
|
from awslambdahelper.cli import BundlerArgumentParser |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
class TestArgParserTests(unittest.TestCase): |
13
|
|
|
def setUp(self): |
14
|
|
|
self.original = dict() |
15
|
|
|
self.original['os.path.exists'] = os.path.exists |
16
|
|
|
self.original['os.path.isdir'] = os.path.exists |
17
|
|
|
self.original['os.path.expanduser'] = os.path.expanduser |
18
|
|
|
self.original['os.path.abspath'] = os.path.abspath |
19
|
|
|
|
20
|
|
|
self.path_exists = patch('os.path.exists').start() |
21
|
|
|
|
22
|
|
|
self.path_isdir = patch('os.path.isdir').start() |
23
|
|
|
|
24
|
|
|
self.path_expanduser = patch('os.path.expanduser').start() |
25
|
|
|
|
26
|
|
|
self.path_abspath = patch('os.path.abspath').start() |
27
|
|
|
|
28
|
|
|
def tearDown(self): |
29
|
|
|
os.path.exists = self.original['os.path.exists'] |
30
|
|
|
os.path.isdir = self.original['os.path.isdir'] |
31
|
|
|
os.path.expanduser = self.original['os.path.expanduser'] |
32
|
|
|
os.path.abspath = self.original['os.path.abspath'] |
33
|
|
|
|
34
|
|
|
@patch('awslambdahelper.cli.BundlerArgumentParser._test_missing_directory') |
35
|
|
|
@patch('awslambdahelper.cli.BundlerArgumentParser._test_not_a_directory') |
36
|
|
|
@patch('awslambdahelper.cli.BundlerArgumentParser._test_missing_requirements') |
37
|
|
|
def test_parser_missing_req(self, missing_requirements, not_a_directory, missing_directory): |
38
|
|
|
missing_requirements.return_value = 'missing_requirements' |
39
|
|
|
not_a_directory.return_value = None |
40
|
|
|
missing_directory.return_value = None |
41
|
|
|
|
42
|
|
|
parser = BundlerArgumentParser() |
43
|
|
|
|
44
|
|
|
with self.assertRaises(ArgumentError): |
45
|
|
|
parser._parse_known_args([ |
46
|
|
|
'--directory', 'world', |
47
|
|
|
], Namespace(requirements_name='requirements.txt')) |
48
|
|
|
|
49
|
|
|
@patch('awslambdahelper.cli.BundlerArgumentParser._test_missing_directory') |
50
|
|
|
@patch('awslambdahelper.cli.BundlerArgumentParser._test_not_a_directory') |
51
|
|
|
@patch('awslambdahelper.cli.BundlerArgumentParser._test_missing_requirements') |
52
|
|
|
def test_parser_not_a_dir(self, missing_requirements, not_a_directory, missing_directory): |
53
|
|
|
missing_requirements.return_value = None |
54
|
|
|
not_a_directory.return_value = 'not a directory' |
55
|
|
|
missing_directory.return_value = None |
56
|
|
|
|
57
|
|
|
parser = BundlerArgumentParser() |
58
|
|
|
|
59
|
|
|
with self.assertRaises(ArgumentError): |
60
|
|
|
parser._parse_known_args([ |
61
|
|
|
'--directory', 'world', |
62
|
|
|
], Namespace(requirements_name='requirements.txt')) |
63
|
|
|
|
64
|
|
|
@patch('awslambdahelper.cli.BundlerArgumentParser._test_missing_directory') |
65
|
|
|
@patch('awslambdahelper.cli.BundlerArgumentParser._test_not_a_directory') |
66
|
|
|
@patch('awslambdahelper.cli.BundlerArgumentParser._test_missing_requirements') |
67
|
|
|
@patch('awslambdahelper.cli.BundlerArgumentParser._full_path') |
68
|
|
|
def test_parser_good_response(self, full_path, missing_requirements, not_a_directory, missing_directory): |
69
|
|
|
missing_requirements.return_value = None |
70
|
|
|
not_a_directory.return_value = None |
71
|
|
|
missing_directory.return_value = None |
72
|
|
|
full_path.return_value = 'world' |
73
|
|
|
|
74
|
|
|
parser = BundlerArgumentParser() |
75
|
|
|
|
76
|
|
|
namespace, unparsed_args = parser._parse_known_args([ |
77
|
|
|
'--directory', 'world', |
78
|
|
|
], Namespace(requirements_name='requirements.txt')) |
79
|
|
|
|
80
|
|
|
self.assertEqual(namespace.directory, 'world') |
81
|
|
|
self.assertEqual(namespace.requirements_name, 'requirements.txt') |
82
|
|
|
self.assertEqual(namespace.requirements_path, 'world/requirements.txt') |
83
|
|
|
self.assertEqual(len(unparsed_args), 0) |
84
|
|
|
|
85
|
|
|
def test_missingdir_true(self): |
86
|
|
|
cli_parser = BundlerArgumentParser() |
87
|
|
|
|
88
|
|
|
self.path_exists.return_value = True |
89
|
|
|
missing_directory = cli_parser._test_missing_directory('my_dir') |
90
|
|
|
self.assertEqual(missing_directory, False) |
91
|
|
|
|
92
|
|
|
def test_missingdir_false(self): |
93
|
|
|
cli_parser = BundlerArgumentParser() |
94
|
|
|
|
95
|
|
|
self.path_exists.return_value = False |
96
|
|
|
missing_directory = cli_parser._test_missing_directory('my_dir') |
97
|
|
|
self.assertEqual(missing_directory, "Could not find `--directory=my_dir`.") |
98
|
|
|
|
99
|
|
|
def test_notdirectory_true(self): |
100
|
|
|
cli_parser = BundlerArgumentParser() |
101
|
|
|
|
102
|
|
|
self.path_isdir.return_value = True |
103
|
|
|
missing_directory = cli_parser._test_not_a_directory('my_dir') |
104
|
|
|
self.assertEqual(missing_directory, False) |
105
|
|
|
|
106
|
|
|
def test_notdirectory_false(self): |
107
|
|
|
cli_parser = BundlerArgumentParser() |
108
|
|
|
|
109
|
|
|
self.path_isdir.return_value = False |
110
|
|
|
missing_directory = cli_parser._test_not_a_directory('my_dir') |
111
|
|
|
self.assertEqual(missing_directory, "`--directory=my_dir` is not a directory.") |
112
|
|
|
|
113
|
|
|
def test_missingrequirements_true(self): |
114
|
|
|
cli_parser = BundlerArgumentParser() |
115
|
|
|
|
116
|
|
|
self.path_exists.return_value = True |
117
|
|
|
missing_directory = cli_parser._test_missing_requirements('my_dir') |
118
|
|
|
self.assertEqual(missing_directory, False) |
119
|
|
|
|
120
|
|
|
def test_missingrequirements_false(self): |
121
|
|
|
cli_parser = BundlerArgumentParser() |
122
|
|
|
|
123
|
|
|
self.path_exists.return_value = False |
124
|
|
|
missing_directory = cli_parser._test_missing_requirements('my_dir') |
125
|
|
|
self.assertEqual(missing_directory, "Could not find requirements file at `my_dir`.") |
126
|
|
|
|
127
|
|
|
def test__fullpath_nohomedir(self): |
128
|
|
|
cli_parser = BundlerArgumentParser() |
129
|
|
|
|
130
|
|
|
self.path_exists.return_value = False |
131
|
|
|
self.path_expanduser.return_value = False |
132
|
|
|
self.path_abspath.return_value = True |
133
|
|
|
full_path = cli_parser._full_path('my_dir') |
134
|
|
|
|
135
|
|
|
self.path_abspath.assert_has_calls([call('my_dir')]) |
136
|
|
|
self.path_expanduser.assert_not_called() |
137
|
|
|
|
138
|
|
|
def test__fullpath_hashomedir(self): |
139
|
|
|
cli_parser = BundlerArgumentParser() |
140
|
|
|
|
141
|
|
|
self.path_exists.return_value = False |
142
|
|
|
self.path_expanduser.return_value = "/home/my_dir" |
143
|
|
|
self.path_abspath.return_value = True |
144
|
|
|
full_path = cli_parser._full_path('~/my_dir') |
145
|
|
|
|
146
|
|
|
self.path_abspath.assert_has_calls([call('/home/my_dir')]) |
147
|
|
|
self.path_exists.assert_has_calls([call('~/my_dir')]) |
148
|
|
|
self.path_expanduser.assert_has_calls([call('~/my_dir')]) |
149
|
|
|
|