1 # Copyright (c) 2015-2016, NVIDIA CORPORATION. All rights reserved.
3 # SPDX-License-Identifier: GPL-2.0
5 # Test operation of the "if" shell command.
11 pytestmark = pytest.mark.buildconfigspec('hush_parser')
13 # The list of "if test" conditions to test.
15 # Base if functionality.
22 ('test aaa = aaa', True),
23 ('test aaa = bbb', False),
25 ('test aaa != bbb', True),
26 ('test aaa != aaa', False),
28 ('test aaa < bbb', True),
29 ('test bbb < aaa', False),
31 ('test bbb > aaa', True),
32 ('test aaa > bbb', False),
34 ('test 123 -eq 123', True),
35 ('test 123 -eq 456', False),
37 ('test 123 -ne 456', True),
38 ('test 123 -ne 123', False),
40 ('test 123 -lt 456', True),
41 ('test 123 -lt 123', False),
42 ('test 456 -lt 123', False),
44 ('test 123 -le 456', True),
45 ('test 123 -le 123', True),
46 ('test 456 -le 123', False),
48 ('test 456 -gt 123', True),
49 ('test 123 -gt 123', False),
50 ('test 123 -gt 456', False),
52 ('test 456 -ge 123', True),
53 ('test 123 -ge 123', True),
54 ('test 123 -ge 456', False),
57 ('test -z "aaa"', False),
59 ('test -n "aaa"', True),
60 ('test -n ""', False),
62 # Inversion of simple tests.
64 ('test ! aaa = aaa', False),
65 ('test ! aaa = bbb', True),
66 ('test ! ! aaa = aaa', True),
67 ('test ! ! aaa = bbb', False),
71 ('test aaa != aaa -o bbb != bbb', False),
72 ('test aaa != aaa -o bbb = bbb', True),
73 ('test aaa = aaa -o bbb != bbb', True),
74 ('test aaa = aaa -o bbb = bbb', True),
76 ('test aaa != aaa -a bbb != bbb', False),
77 ('test aaa != aaa -a bbb = bbb', False),
78 ('test aaa = aaa -a bbb != bbb', False),
79 ('test aaa = aaa -a bbb = bbb', True),
81 # Inversion within binary operators.
83 ('test ! aaa != aaa -o ! bbb != bbb', True),
84 ('test ! aaa != aaa -o ! bbb = bbb', True),
85 ('test ! aaa = aaa -o ! bbb != bbb', True),
86 ('test ! aaa = aaa -o ! bbb = bbb', False),
88 ('test ! ! aaa != aaa -o ! ! bbb != bbb', False),
89 ('test ! ! aaa != aaa -o ! ! bbb = bbb', True),
90 ('test ! ! aaa = aaa -o ! ! bbb != bbb', True),
91 ('test ! ! aaa = aaa -o ! ! bbb = bbb', True),
95 ('test -z "$ut_var_nonexistent"', True),
96 ('test -z "$ut_var_exists"', False),
99 def exec_hush_if(u_boot_console, expr, result):
100 """Execute a shell "if" command, and validate its result."""
102 config = u_boot_console.config.buildconfig
103 maxargs = int(config.get('config_sys_maxargs', '0'))
104 args = len(expr.split(' ')) - 1
106 u_boot_console.log.warning('CONFIG_SYS_MAXARGS too low; need ' +
110 cmd = 'if ' + expr + '; then echo true; else echo false; fi'
111 response = u_boot_console.run_command(cmd)
112 assert response.strip() == str(result).lower()
114 def test_hush_if_test_setup(u_boot_console):
115 """Set up environment variables used during the "if" tests."""
117 u_boot_console.run_command('setenv ut_var_nonexistent')
118 u_boot_console.run_command('setenv ut_var_exists 1')
120 @pytest.mark.parametrize('expr,result', subtests)
121 def test_hush_if_test(u_boot_console, expr, result):
122 """Test a single "if test" condition."""
124 exec_hush_if(u_boot_console, expr, result)
126 def test_hush_if_test_teardown(u_boot_console):
127 """Clean up environment variables used during the "if" tests."""
129 u_boot_console.run_command('setenv ut_var_exists')
131 # We might test this on real filesystems via UMS, DFU, 'save', etc.
132 # Of those, only UMS currently allows file removal though.
133 @pytest.mark.boardspec('sandbox')
134 def test_hush_if_test_host_file_exists(u_boot_console):
135 """Test the "if test -e" shell command."""
137 test_file = u_boot_console.config.result_dir + \
138 '/creating_this_file_breaks_u_boot_tests'
144 assert not os.path.exists(test_file)
146 expr = 'test -e hostfs - ' + test_file
147 exec_hush_if(u_boot_console, expr, False)
150 with file(test_file, 'wb'):
152 assert os.path.exists(test_file)
154 expr = 'test -e hostfs - ' + test_file
155 exec_hush_if(u_boot_console, expr, True)
159 expr = 'test -e hostfs - ' + test_file
160 exec_hush_if(u_boot_console, expr, False)