-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_client.py
182 lines (147 loc) · 5.88 KB
/
test_client.py
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# -*- coding: utf-8 -*-
""" GPG Remote client module testing
copyright: 2015, Vlad "SATtva" Miller, http://vladmiller.info
license: GNU GPL, see COPYING for details.
"""
import unittest, tempfile
import gpgremote_client as client
class TestUpdateConfig(unittest.TestCase):
"""Config parser tests."""
def test_parse(self):
"""Parse proper config file."""
with tempfile.NamedTemporaryFile(prefix='gpgremote_test_',
mode='wt', delete=False) as file:
file.write("""
# Comment.
foo = bar
value = 1
flag
""")
parsed = {}
expected = {'foo': 'bar', 'value': 1, 'flag': True}
client.update_config(file.name, parsed)
self.assertEqual(parsed, expected)
def test_silent(self):
"""Supress errors in silent mode."""
parsed = {'foo': 'bar'}
expected = {'foo': 'bar'}
client.update_config('/foobar', parsed)
self.assertEqual(parsed, expected)
def test_nonsilent(self):
"""Raise error in non-silent mode."""
self.assertRaises(ValueError, client.update_config,
'/foobar', {}, silent=False)
class TestParseOptions(unittest.TestCase):
"""Command line arguments parsing tests."""
@staticmethod
def parse(argv):
"""Call shortening wrapper."""
return client.parse_options(argv)
def test_empty(self):
"""No arguments."""
argv = []
parsed = self.parse(argv)
self.assertEqual(parsed, [])
def test_opts1(self):
"""Options without parameters, case 1."""
argv = ['-o']
parsed = self.parse(argv)
self.assertEqual(parsed, [('-o', None)])
def test_opts2(self):
"""Options without parameters, case 2."""
argv = ['-o', '--option']
parsed = self.parse(argv)
self.assertEqual(parsed, [('-o', None), ('--option', None)])
def test_opts_params1(self):
"""Options with parameters, case 1."""
argv = ['-o', 'foo']
parsed = self.parse(argv)
self.assertEqual(parsed, [('-o', 'foo')])
def test_opts_params2(self):
"""Options with parameters, case 2."""
argv = ['-o', 'foo', '--output', 'bar']
parsed = self.parse(argv)
self.assertEqual(parsed, [('-o', 'foo'), ('--output', 'bar')])
def test_args1(self):
"""Trailing arguments only, case 1."""
argv = ['foo']
parsed = self.parse(argv)
self.assertEqual(parsed, [(None, 'foo')])
def test_args2(self):
"""Trailing arguments only, case 2."""
argv = ['foo', 'bar']
parsed = self.parse(argv)
self.assertEqual(parsed, [(None, 'foo'), (None, 'bar')])
def test_opts_args(self):
"""Options and trailing arguments."""
argv = ['-o', 'foo', 'bar']
parsed = self.parse(argv)
self.assertEqual(parsed, [('-o', 'foo'), (None, 'bar')])
def test_args_explicit(self):
"""Explicitly defined trailing arguments."""
argv = ['--', '-o', 'foo']
parsed = self.parse(argv)
self.assertEqual(parsed, [(None, '-o'), (None, 'foo')])
def test_opt_args_explicit(self):
"""Option and explicitly defined trailing arguments."""
argv = ['-o', '--', 'foo', 'bar']
parsed = self.parse(argv)
self.assertEqual(parsed, [('-o', None),
(None, 'foo'), (None, 'bar')])
def test_opts_expand(self):
"""Expand multiple shortened options."""
argv = ['-op']
parsed = self.parse(argv)
self.assertEqual(parsed, [('-o', None), ('-p', None)])
def test_opts_param_expand(self):
"""Expand multiple shortened options with parameter."""
argv = ['-op', 'foo']
parsed = self.parse(argv)
self.assertEqual(parsed, [('-o', None), ('-p', 'foo')])
def test_posix1(self):
"""POSIX compliance, case 1."""
argv = ['foo', '-o']
parsed = self.parse(argv)
self.assertEqual(parsed, [('-o', None)])
def test_posix2(self):
"""POSIX compliance, case 2."""
argv = ['--option', 'foo', 'bar', '-o']
parsed = self.parse(argv)
self.assertEqual(parsed, [('--option', 'foo'), ('-o', None)])
def test_stdout(self):
"""STDOUT placeholder."""
argv = ['-o', '-', '-']
parsed = self.parse(argv)
self.assertEqual(parsed, [('-o', '-'), (None, '-')])
def test_complex(self):
"""Complex case."""
argv = ['non-posix', '--option', 'foo', 'non-posix',
'-ovp', 'two words', '-a', 'param', '--',
'--opt', 'arg1', 'arg2']
parsed = self.parse(argv)
self.assertEqual(parsed, [('--option', 'foo'),
('-o', None), ('-v', None),
('-p', 'two words'), ('-a', 'param'),
(None, '--opt'), (None, 'arg1'),
(None, 'arg2')])
class TestGetFilenames(unittest.TestCase):
"""File names of existing files in command line arguments tests."""
def test_missing(self):
"""No existing files in args list."""
args = [(None, '/foo')] # Hopefully there is no such file.
files = client.get_filenames(args)
self.assertEqual(files, [])
def test_ignore_output(self):
"""Ignore existing files given in --output option."""
file = tempfile.NamedTemporaryFile(prefix='gpgremote_test_')
args = [('--output', file.name)]
files = client.get_filenames(args)
self.assertEqual(files, [])
def test_existing(self):
"""Output existing filename."""
file = tempfile.NamedTemporaryFile(prefix='gpgremote_test_')
args = [('--opt1', file.name), ('--opt2', '/foo')]
files = client.get_filenames(args)
self.assertEqual(files, [file.name])
if __name__ == '__main__':
unittest.main()