7689f73a87097affbda8856889a719158037ae74
[sdc/sdc-distribution-client.git] /
1 #!/usr/bin/env python
2 #
3 #  Licensed under the Apache License, Version 2.0 (the "License"); you may
4 #  not use this file except in compliance with the License. You may obtain
5 #  a copy of the License at
6 #
7 #       http://www.apache.org/licenses/LICENSE-2.0
8 #
9 #  Unless required by applicable law or agreed to in writing, software
10 #  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 #  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 #  License for the specific language governing permissions and limitations
13 #  under the License.
14
15 import argparse
16 import six
17
18 from cliff.formatters import shell
19 from cliff.tests import test_columns
20
21 import mock
22
23
24 def test_shell_formatter():
25     sf = shell.ShellFormatter()
26     c = ('a', 'b', 'c', 'd')
27     d = ('A', 'B', 'C', '"escape me"')
28     expected = 'a="A"\nb="B"\nd="\\"escape me\\""\n'
29     output = six.StringIO()
30     args = mock.Mock()
31     args.variables = ['a', 'b', 'd']
32     args.prefix = ''
33     sf.emit_one(c, d, output, args)
34     actual = output.getvalue()
35     assert expected == actual
36
37
38 def test_shell_formatter_args():
39     sf = shell.ShellFormatter()
40     c = ('a', 'b', 'c', 'd')
41     d = ('A', 'B', 'C', '"escape me"')
42     expected = 'Xd="\\"escape me\\""\n'
43     output = six.StringIO()
44     # Parse arguments as if passed on the command-line
45     parser = argparse.ArgumentParser(description='Testing...')
46     sf.add_argument_group(parser)
47     parsed_args = parser.parse_args(['--variable', 'd', '--prefix', 'X'])
48     sf.emit_one(c, d, output, parsed_args)
49     actual = output.getvalue()
50     assert expected == actual
51
52
53 def test_shell_formatter_formattable_column():
54     sf = shell.ShellFormatter()
55     c = ('a', 'b', 'c')
56     d = ('A', 'B', test_columns.FauxColumn(['the', 'value']))
57     expected = '\n'.join([
58         'a="A"',
59         'b="B"',
60         'c="[\'the\', \'value\']"\n',
61     ])
62     output = six.StringIO()
63     args = mock.Mock()
64     args.variables = ['a', 'b', 'c']
65     args.prefix = ''
66     sf.emit_one(c, d, output, args)
67     actual = output.getvalue()
68     assert expected == actual
69
70
71 def test_shell_formatter_with_non_string_values():
72     sf = shell.ShellFormatter()
73     c = ('a', 'b', 'c', 'd', 'e')
74     d = (True, False, 100, '"esc"', six.text_type('"esc"'))
75     expected = 'a="True"\nb="False"\nc="100"\nd="\\"esc\\""\ne="\\"esc\\""\n'
76     output = six.StringIO()
77     args = mock.Mock()
78     args.variables = ['a', 'b', 'c', 'd', 'e']
79     args.prefix = ''
80     sf.emit_one(c, d, output, args)
81     actual = output.getvalue()
82     assert expected == actual
83
84
85 def test_shell_formatter_with_non_bash_friendly_values():
86     sf = shell.ShellFormatter()
87     c = ('a', 'foo-bar', 'provider:network_type')
88     d = (True, 'baz', 'vxlan')
89     expected = 'a="True"\nfoo_bar="baz"\nprovider_network_type="vxlan"\n'
90     output = six.StringIO()
91     args = mock.Mock()
92     args.variables = ['a', 'foo-bar', 'provider:network_type']
93     args.prefix = ''
94     sf.emit_one(c, d, output, args)
95     actual = output.getvalue()
96     assert expected == actual