blob: 2cdb79708ab633513e81494a1063cf1d48b14014 [file] [log] [blame]
kelly668eb212018-03-28 17:20:34 +08001#!/usr/bin/env python
2#
3# Copyright 2018, The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17"""Unittests for atest_utils."""
18
nelsonlie3f90de2018-06-22 14:59:39 +080019import sys
kelly668eb212018-03-28 17:20:34 +080020import unittest
Dan Shi0ddd3e42018-05-30 11:24:30 -070021import mock
kelly668eb212018-03-28 17:20:34 +080022
23import atest_utils
24
nelsonlie3f90de2018-06-22 14:59:39 +080025if sys.version_info[0] == 2:
26 from StringIO import StringIO
27else:
28 from io import StringIO
kelly668eb212018-03-28 17:20:34 +080029
30#pylint: disable=protected-access
31class AtestUtilsUnittests(unittest.TestCase):
32 """Unit tests for atest_utils.py"""
33
34 def test_capture_fail_section_has_fail_section(self):
35 """Test capture_fail_section when has fail section."""
36 test_list = ['AAAAAA', 'FAILED: Error1', '^\n', 'Error2\n',
37 '[ 6% 191/2997] BBBBBB\n', 'CCCCC',
38 '[ 20% 322/2997] DDDDDD\n', 'EEEEE']
39 want_list = ['FAILED: Error1', '^\n', 'Error2\n']
40 self.assertEqual(want_list,
41 atest_utils._capture_fail_section(test_list))
42
43 def test_capture_fail_section_no_fail_section(self):
44 """Test capture_fail_section when no fail section."""
45 test_list = ['[ 6% 191/2997] XXXXX', 'YYYYY: ZZZZZ']
46 want_list = []
47 self.assertEqual(want_list,
48 atest_utils._capture_fail_section(test_list))
49
Dan Shi0ddd3e42018-05-30 11:24:30 -070050 def test_is_test_mapping(self):
51 """Test method is_test_mapping."""
52 tm_option_attributes = [
53 'test_mapping',
54 'include_subdirs'
55 ]
56 for attr_to_test in tm_option_attributes:
57 args = mock.Mock()
58 for attr in tm_option_attributes:
59 setattr(args, attr, attr == attr_to_test)
60 args.tests = []
61 self.assertTrue(
62 atest_utils.is_test_mapping(args),
63 'Failed to validate option %s' % attr_to_test)
64
65 args = mock.Mock()
66 for attr in tm_option_attributes:
67 setattr(args, attr, False)
68 args.tests = [':group_name']
69 self.assertTrue(atest_utils.is_test_mapping(args))
70
71 args = mock.Mock()
72 for attr in tm_option_attributes:
73 setattr(args, attr, False)
74 args.tests = [':test1', 'test2']
75 self.assertFalse(atest_utils.is_test_mapping(args))
76
77 args = mock.Mock()
78 for attr in tm_option_attributes:
79 setattr(args, attr, False)
80 args.tests = ['test2']
81 self.assertFalse(atest_utils.is_test_mapping(args))
82
nelsonlie3f90de2018-06-22 14:59:39 +080083 @mock.patch('curses.tigetnum')
84 def test_has_colors(self, mock_curses_tigetnum):
85 """Test method _has_colors."""
86 # stream is file I/O
87 stream = open('/tmp/test_has_colors.txt', 'wb')
88 self.assertFalse(atest_utils._has_colors(stream))
89 stream.close()
90
91 # stream is not a tty(terminal).
92 stream = mock.Mock()
93 stream.isatty.return_value = False
94 self.assertFalse(atest_utils._has_colors(stream))
95
96 # stream is a tty(terminal) and clolors < 2.
97 stream = mock.Mock()
98 stream.isatty.return_value = True
99 mock_curses_tigetnum.return_value = 1
100 self.assertFalse(atest_utils._has_colors(stream))
101
102 # stream is a tty(terminal) and clolors > 2.
103 stream = mock.Mock()
104 stream.isatty.return_value = True
105 mock_curses_tigetnum.return_value = 256
106 self.assertTrue(atest_utils._has_colors(stream))
107
nelsonli34997d52018-08-17 09:43:28 +0800108
109 @mock.patch('atest_utils._has_colors')
110 def test_colorize(self, mock_has_colors):
111 """Test method colorize."""
112 original_str = "test string"
113 green_no = 2
114
115 # _has_colors() return False.
116 mock_has_colors.return_value = False
117 converted_str = atest_utils.colorize(original_str, green_no,
118 highlight=True)
119 self.assertEqual(original_str, converted_str)
120
121 # Green with highlight.
122 mock_has_colors.return_value = True
123 converted_str = atest_utils.colorize(original_str, green_no,
124 highlight=True)
125 green_highlight_string = '\x1b[1;42m%s\x1b[0m' % original_str
126 self.assertEqual(green_highlight_string, converted_str)
127
128 # Green, no highlight.
129 mock_has_colors.return_value = True
130 converted_str = atest_utils.colorize(original_str, green_no,
131 highlight=False)
132 green_no_highlight_string = '\x1b[1;32m%s\x1b[0m' % original_str
133 self.assertEqual(green_no_highlight_string, converted_str)
134
135
nelsonlie3f90de2018-06-22 14:59:39 +0800136 @mock.patch('atest_utils._has_colors')
137 def test_colorful_print(self, mock_has_colors):
138 """Test method colorful_print."""
139 testing_str = "color_print_test"
140 green_no = 2
141
142 # _has_colors() return False.
143 mock_has_colors.return_value = False
144 capture_output = StringIO()
145 sys.stdout = capture_output
146 atest_utils.colorful_print(testing_str, green_no, highlight=True,
147 auto_wrap=False)
148 sys.stdout = sys.__stdout__
149 uncolored_string = testing_str
150 self.assertEqual(capture_output.getvalue(), uncolored_string)
151
152 # Green with highlight, but no wrap.
153 mock_has_colors.return_value = True
154 capture_output = StringIO()
155 sys.stdout = capture_output
156 atest_utils.colorful_print(testing_str, green_no, highlight=True,
157 auto_wrap=False)
158 sys.stdout = sys.__stdout__
159 green_highlight_no_wrap_string = '\x1b[1;42m%s\x1b[0m' % testing_str
160 self.assertEqual(capture_output.getvalue(),
161 green_highlight_no_wrap_string)
162
163 # Green, no highlight, no wrap.
164 mock_has_colors.return_value = True
165 capture_output = StringIO()
166 sys.stdout = capture_output
167 atest_utils.colorful_print(testing_str, green_no, highlight=False,
168 auto_wrap=False)
169 sys.stdout = sys.__stdout__
170 green_no_high_no_wrap_string = '\x1b[1;32m%s\x1b[0m' % testing_str
171 self.assertEqual(capture_output.getvalue(),
172 green_no_high_no_wrap_string)
173
174 # Green with highlight and wrap.
175 mock_has_colors.return_value = True
176 capture_output = StringIO()
177 sys.stdout = capture_output
178 atest_utils.colorful_print(testing_str, green_no, highlight=True,
179 auto_wrap=True)
180 sys.stdout = sys.__stdout__
181 green_highlight_wrap_string = '\x1b[1;42m%s\x1b[0m\n' % testing_str
182 self.assertEqual(capture_output.getvalue(), green_highlight_wrap_string)
183
184 # Green with wrap, but no highlight.
185 mock_has_colors.return_value = True
186 capture_output = StringIO()
187 sys.stdout = capture_output
188 atest_utils.colorful_print(testing_str, green_no, highlight=False,
189 auto_wrap=True)
190 sys.stdout = sys.__stdout__
191 green_wrap_no_highlight_string = '\x1b[1;32m%s\x1b[0m\n' % testing_str
192 self.assertEqual(capture_output.getvalue(),
193 green_wrap_no_highlight_string)
194
kelly668eb212018-03-28 17:20:34 +0800195
196if __name__ == "__main__":
197 unittest.main()