1# Copyright (C) 2022 The Android Open Source Project 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain 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, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14import datetime 15import os 16import unittest 17 18from util import _next_path_helper 19from util import any_match 20from util import get_top_dir 21from util import hhmmss 22from util import period_to_seconds 23 24 25class UtilTest(unittest.TestCase): 26 def test_next_path_helper(self): 27 examples = [ 28 ('output', 'output-1'), 29 ('output.x', 'output-1.x'), 30 ('output.x.y', 'output-1.x.y'), 31 ('output-1', 'output-2'), 32 ('output-9', 'output-10'), 33 ('output-10', 'output-11'), 34 ] 35 for (pattern, expected) in examples: 36 with self.subTest(msg=pattern, pattern=pattern, expected=expected): 37 self.assertEqual(_next_path_helper(pattern), expected) 38 39 def test_any_match(self): 40 path, matches = any_match('root.bp') 41 self.assertEqual(matches, ['root.bp']) 42 self.assertEqual(path, get_top_dir().joinpath('build/soong')) 43 44 path, matches = any_match('!Android.bp', '!BUILD', 45 'scripts/incremental_build/incremental_build.py') 46 self.assertEqual(matches, 47 ['scripts/incremental_build/incremental_build.py']) 48 self.assertEqual(path, get_top_dir().joinpath('build/bazel')) 49 50 path, matches = any_match('BUILD', 'README.md') 51 self.assertEqual(matches, ['BUILD', 'README.md']) 52 self.assertTrue(path.joinpath('BUILD').exists()) 53 self.assertTrue(path.joinpath('README.md').exists()) 54 55 path, matches = any_match('BUILD', '!README.md') 56 self.assertEqual(matches, ['BUILD']) 57 self.assertTrue(path.joinpath('BUILD').exists()) 58 self.assertFalse(path.joinpath('README.md').exists()) 59 60 path, matches = any_match('!*.bazel', '*') 61 self.assertGreater(len(matches), 0) 62 children = os.listdir(path) 63 self.assertGreater(len(children), 0) 64 for child in children: 65 self.assertFalse(child.endswith('.bazel')) 66 67 path, matches = any_match('*/BUILD', '*/README.md') 68 self.assertGreater(len(matches), 0) 69 for m in matches: 70 self.assertTrue(path.joinpath(m).exists()) 71 72 path, matches = any_match('!**/BUILD', '**/*.cpp') 73 self.assertEqual(len(matches), 1) 74 self.assertTrue(path.joinpath(matches[0]).exists()) 75 self.assertTrue(matches[0].endswith('.cpp')) 76 for _, dirs, files in os.walk(path): 77 self.assertFalse('BUILD' in dirs) 78 self.assertFalse('BUILD' in files) 79 80 def test_hhmmss(self): 81 examples = [ 82 (datetime.timedelta(seconds=(2 * 60 + 5)), '02:05.000'), 83 (datetime.timedelta(seconds=(3600 + 23 * 60 + 45.897898)), 84 '1:23:45.898'), 85 ] 86 for (ts, expected) in examples: 87 self.subTest(ts=ts, expected=expected) 88 self.assertEqual(hhmmss(ts), expected) 89 90 def test_period_to_seconds(self): 91 examples = [ 92 ('02:05.000', 2 * 60 + 5), 93 ('1:23:45.898', 3600 + 23 * 60 + 45.898), 94 ('1.898', 1.898), 95 ('0.3', 0.3), 96 ('0', 0), 97 ('0:00', 0), 98 ('0:00:00', 0), 99 ('', 0) 100 ] 101 for (ts, expected) in examples: 102 self.subTest(ts=ts, expected=expected) 103 self.assertEqual(period_to_seconds(ts), expected) 104 105 106if __name__ == '__main__': 107 unittest.main() 108