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
7 # http://www.apache.org/licenses/LICENSE-2.0
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
18 from cliff.formatters import shell
19 from cliff.tests import test_columns
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()
31 args.variables = ['a', 'b', 'd']
33 sf.emit_one(c, d, output, args)
34 actual = output.getvalue()
35 assert expected == actual
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
53 def test_shell_formatter_formattable_column():
54 sf = shell.ShellFormatter()
56 d = ('A', 'B', test_columns.FauxColumn(['the', 'value']))
57 expected = '\n'.join([
60 'c="[\'the\', \'value\']"\n',
62 output = six.StringIO()
64 args.variables = ['a', 'b', 'c']
66 sf.emit_one(c, d, output, args)
67 actual = output.getvalue()
68 assert expected == actual
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()
78 args.variables = ['a', 'b', 'c', 'd', 'e']
80 sf.emit_one(c, d, output, args)
81 actual = output.getvalue()
82 assert expected == actual
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()
92 args.variables = ['a', 'foo-bar', 'provider:network_type']
94 sf.emit_one(c, d, output, args)
95 actual = output.getvalue()
96 assert expected == actual