-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathtest_arguments.py
232 lines (200 loc) · 7.97 KB
/
test_arguments.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# tests/arguments.py --------------------------------------------*- python -*-
#
# This source file is part of the Swift.org open source project
#
# Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
# Licensed under Apache License v2.0 with Runtime Library Exception
#
# See https://swift.org/LICENSE.txt for license information
# See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
#
# ----------------------------------------------------------------------------
import argparse
import os
import sys
import unittest
try:
# py2
from StringIO import StringIO
except ImportError:
# py3
from io import StringIO
from swift_build_support.arguments import (
action as argaction,
type as argtype,
)
class ArgumentsTypeTestCase(unittest.TestCase):
def test_bool(self):
self.assertTrue(argtype.bool("1"))
self.assertTrue(argtype.bool("true"))
self.assertTrue(argtype.bool("True"))
self.assertFalse(argtype.bool("0"))
self.assertFalse(argtype.bool("false"))
self.assertFalse(argtype.bool("False"))
self.assertRaises(argparse.ArgumentTypeError, argtype.bool, 'foobar')
self.assertRaises(argparse.ArgumentTypeError, argtype.bool, 'TRUE')
self.assertRaises(argparse.ArgumentTypeError, argtype.bool, 'FALSE')
def test_shell_split(self):
self.assertEqual(
argtype.shell_split("-BAR=\"foo bar\" -BAZ='foo,bar',-QUX $baz"),
['-BAR=foo bar', '-BAZ=foo,bar', '-QUX', '$baz'])
def test_clang_compiler_version(self):
self.assertEqual(
argtype.clang_compiler_version('1.23.456').components,
("1", "23", "456", None))
self.assertEqual(
argtype.clang_compiler_version('1.2.3').components,
("1", "2", "3", None))
self.assertEqual(
argtype.clang_compiler_version('1.2.3.4').components,
("1", "2", "3", "4"))
self.assertEqual(
argtype.clang_compiler_version('12.34.56').components,
("12", "34", "56", None))
self.assertEqual(
argtype.clang_compiler_version('12.34.56.78').components,
("12", "34", "56", "78"))
self.assertRaises(
argparse.ArgumentTypeError,
argtype.clang_compiler_version,
"ver1.2.3")
self.assertRaises(
argparse.ArgumentTypeError,
argtype.clang_compiler_version,
"1.beta2.3")
self.assertRaises(
argparse.ArgumentTypeError,
argtype.clang_compiler_version,
"1.2.preview3")
self.assertRaises(
argparse.ArgumentTypeError,
argtype.clang_compiler_version,
"1.2.3-rc4")
self.assertRaises(
argparse.ArgumentTypeError,
argtype.clang_compiler_version,
"1..2")
def test_executable(self):
python = sys.executable
self.assertTrue(os.path.isabs(argtype.executable(python)))
# On this test directory, specifying "../../build-script-impl" returns
# absolute path of build-script-impl
impl = os.path.join("..", "..", "build-script-impl")
cwd = os.getcwd()
os.chdir(os.path.dirname(__file__))
self.assertTrue(os.path.isabs(argtype.executable(impl)))
os.chdir(cwd)
self.assertRaises(
argparse.ArgumentTypeError,
argtype.executable, __file__) # this file is not executable
self.assertRaises(
argparse.ArgumentTypeError,
argtype.executable, os.path.dirname(__file__))
self.assertRaises(
argparse.ArgumentTypeError,
argtype.executable, "/bin/example-command-not-exist")
self.assertRaises(
argparse.ArgumentTypeError,
argtype.executable, "../example-command-not-exist")
class ArgumentsActionTestCase(unittest.TestCase):
def test_unavailable(self):
orig_stderr = sys.stderr
parser = argparse.ArgumentParser()
parser.add_argument("--foo")
parser.add_argument(
"--do-not-use",
"--never-ever",
action=argaction.unavailable)
args, unknown_args = parser.parse_known_args(
['--foo', 'bar', '--baz', 'qux'])
self.assertEqual(args.foo, 'bar')
self.assertEqual(unknown_args, ['--baz', 'qux'])
self.assertFalse(hasattr(args, 'sentinel'))
stderr = StringIO()
sys.stderr = stderr
self.assertRaises(
SystemExit,
parser.parse_known_args,
['--foo', 'bar', '--do-not-use', 'baz'])
self.assertIn('--do-not-use', stderr.getvalue())
stderr = StringIO()
sys.stderr = stderr
self.assertRaises(
SystemExit,
parser.parse_known_args,
['--foo', 'bar', '--never-ever=baz'])
self.assertIn('--never-ever', stderr.getvalue())
sys.stderr = orig_stderr
def test_concat(self):
# Has default
parser = argparse.ArgumentParser()
parser.add_argument(
"--str-opt",
action=argaction.concat,
default="def")
parser.add_argument(
"--list-opt",
action=argaction.concat,
type=argtype.shell_split,
default=["def"])
self.assertEqual(
parser.parse_args(['--str-opt', '12', '--str-opt=42']),
argparse.Namespace(str_opt="def1242", list_opt=["def"]))
self.assertEqual(
parser.parse_args(['--list-opt', 'foo 12', '--list-opt=bar 42']),
argparse.Namespace(
str_opt="def", list_opt=["def", "foo", "12", "bar", "42"]))
# Default less
parser = argparse.ArgumentParser()
parser.add_argument(
"--str-opt",
action=argaction.concat)
parser.add_argument(
"--list-opt",
action=argaction.concat,
type=argtype.shell_split)
self.assertEqual(
parser.parse_args(['--str-opt', '12', '--str-opt=42']),
argparse.Namespace(str_opt="1242", list_opt=None))
self.assertEqual(
parser.parse_args(['--list-opt', 'foo 12', '--list-opt=bar 42']),
argparse.Namespace(
str_opt=None, list_opt=["foo", "12", "bar", "42"]))
def test_optional_bool(self):
parser = argparse.ArgumentParser()
parser.add_argument(
"--test-default-default",
action=argaction.optional_bool)
parser.add_argument(
"--test-default-true",
action=argaction.optional_bool,
default=True)
parser.add_argument(
"--test-default-false",
action=argaction.optional_bool,
default=False)
args, unknown_args = parser.parse_known_args([])
self.assertEqual(args.test_default_default, False)
self.assertEqual(args.test_default_true, True)
self.assertEqual(args.test_default_false, False)
args, unknown_args = parser.parse_known_args(
['--test-default-default', '0',
'--test-default-true', '0',
'--test-default-false', '0'])
self.assertEqual(args.test_default_default, False)
self.assertEqual(args.test_default_true, False)
self.assertEqual(args.test_default_false, False)
args, unknown_args = parser.parse_known_args(
['--test-default-default', '1',
'--test-default-true', '1',
'--test-default-false', '1'])
self.assertEqual(args.test_default_default, True)
self.assertEqual(args.test_default_true, True)
self.assertEqual(args.test_default_false, True)
args, unknown_args = parser.parse_known_args(
['--test-default-default',
'--test-default-true',
'--test-default-false'])
self.assertEqual(args.test_default_default, True)
self.assertEqual(args.test_default_true, True)
self.assertEqual(args.test_default_false, True)