Oleh Prypin | 69c0222 | 2018-05-23 15:18:12 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | # Copyright (c) 2018 The WebRTC project authors. All Rights Reserved. |
| 4 | # |
| 5 | # Use of this source code is governed by a BSD-style license |
| 6 | # that can be found in the LICENSE file in the root of the source |
| 7 | # tree. An additional intellectual property rights grant can be found |
| 8 | # in the file PATENTS. All contributing project authors may |
| 9 | # be found in the AUTHORS file in the root of the source tree. |
| 10 | |
Mirko Bonadei | dac422f | 2018-06-05 09:12:34 +0200 | [diff] [blame] | 11 | from contextlib import contextmanager |
| 12 | |
| 13 | import os |
| 14 | import tempfile |
Oleh Prypin | 69c0222 | 2018-05-23 15:18:12 +0200 | [diff] [blame] | 15 | import unittest |
| 16 | |
| 17 | script = __import__('gtest-parallel-wrapper') # pylint: disable=invalid-name |
| 18 | |
| 19 | |
Mirko Bonadei | dac422f | 2018-06-05 09:12:34 +0200 | [diff] [blame] | 20 | @contextmanager |
| 21 | def TemporaryDirectory(): |
| 22 | tmp_dir = tempfile.mkdtemp() |
| 23 | yield tmp_dir |
| 24 | os.rmdir(tmp_dir) |
| 25 | |
| 26 | |
Oleh Prypin | 69c0222 | 2018-05-23 15:18:12 +0200 | [diff] [blame] | 27 | class GtestParallelWrapperTest(unittest.TestCase): |
| 28 | @classmethod |
| 29 | def _Expected(cls, gtest_parallel_args): |
| 30 | return ['--shard_index=0', '--shard_count=1'] + gtest_parallel_args |
| 31 | |
| 32 | def testOverwrite(self): |
| 33 | result = script.ParseArgs(['--timeout=123', 'exec', '--timeout', '124']) |
| 34 | expected = self._Expected(['--timeout=124', 'exec']) |
| 35 | self.assertEqual(result.gtest_parallel_args, expected) |
| 36 | |
| 37 | def testMixing(self): |
| 38 | result = script.ParseArgs( |
| 39 | ['--timeout=123', '--param1', 'exec', '--param2', '--timeout', '124']) |
| 40 | expected = self._Expected( |
| 41 | ['--timeout=124', 'exec', '--', '--param1', '--param2']) |
| 42 | self.assertEqual(result.gtest_parallel_args, expected) |
| 43 | |
| 44 | def testMixingPositional(self): |
| 45 | result = script.ParseArgs(['--timeout=123', 'exec', '--foo1', 'bar1', |
| 46 | '--timeout', '124', '--foo2', 'bar2']) |
| 47 | expected = self._Expected(['--timeout=124', 'exec', '--', '--foo1', 'bar1', |
| 48 | '--foo2', 'bar2']) |
| 49 | self.assertEqual(result.gtest_parallel_args, expected) |
| 50 | |
| 51 | def testDoubleDash1(self): |
| 52 | result = script.ParseArgs( |
| 53 | ['--timeout', '123', 'exec', '--', '--timeout', '124']) |
| 54 | expected = self._Expected( |
| 55 | ['--timeout=123', 'exec', '--', '--timeout', '124']) |
| 56 | self.assertEqual(result.gtest_parallel_args, expected) |
| 57 | |
| 58 | def testDoubleDash2(self): |
| 59 | result = script.ParseArgs(['--timeout=123', '--', 'exec', '--timeout=124']) |
| 60 | expected = self._Expected(['--timeout=123', 'exec', '--', '--timeout=124']) |
| 61 | self.assertEqual(result.gtest_parallel_args, expected) |
| 62 | |
| 63 | def testArtifacts(self): |
Mirko Bonadei | dac422f | 2018-06-05 09:12:34 +0200 | [diff] [blame] | 64 | with TemporaryDirectory() as tmp_dir: |
| 65 | output_dir = os.path.join(tmp_dir, 'foo') |
| 66 | result = script.ParseArgs(['exec', '--store-test-artifacts', |
| 67 | '--output_dir', output_dir]) |
| 68 | exp_artifacts_dir = os.path.join(output_dir, 'test_artifacts') |
| 69 | exp = self._Expected(['--output_dir=' + output_dir, 'exec', '--', |
| 70 | '--test_artifacts_dir=' + exp_artifacts_dir]) |
| 71 | self.assertEqual(result.gtest_parallel_args, exp) |
| 72 | self.assertEqual(result.output_dir, output_dir) |
Mirko Bonadei | 40f876d | 2018-08-27 16:32:50 +0200 | [diff] [blame] | 73 | self.assertEqual(result.test_artifacts_dir, exp_artifacts_dir) |
Oleh Prypin | 69c0222 | 2018-05-23 15:18:12 +0200 | [diff] [blame] | 74 | |
| 75 | def testNoDirsSpecified(self): |
| 76 | result = script.ParseArgs(['exec']) |
| 77 | self.assertEqual(result.output_dir, None) |
| 78 | self.assertEqual(result.test_artifacts_dir, None) |
| 79 | |
| 80 | def testOutputDirSpecified(self): |
| 81 | result = script.ParseArgs(['exec', '--output_dir', '/tmp/foo']) |
| 82 | self.assertEqual(result.output_dir, '/tmp/foo') |
| 83 | self.assertEqual(result.test_artifacts_dir, None) |
| 84 | |
| 85 | def testJsonTestResults(self): |
| 86 | result = script.ParseArgs(['--isolated-script-test-output', '/tmp/foo', |
| 87 | 'exec']) |
| 88 | expected = self._Expected(['--dump_json_test_results=/tmp/foo', 'exec']) |
| 89 | self.assertEqual(result.gtest_parallel_args, expected) |
| 90 | |
| 91 | def testShortArg(self): |
| 92 | result = script.ParseArgs(['-d', '/tmp/foo', 'exec']) |
| 93 | expected = self._Expected(['--output_dir=/tmp/foo', 'exec']) |
| 94 | self.assertEqual(result.gtest_parallel_args, expected) |
| 95 | self.assertEqual(result.output_dir, '/tmp/foo') |
| 96 | |
| 97 | def testBoolArg(self): |
| 98 | result = script.ParseArgs(['--gtest_also_run_disabled_tests', 'exec']) |
| 99 | expected = self._Expected(['--gtest_also_run_disabled_tests', 'exec']) |
| 100 | self.assertEqual(result.gtest_parallel_args, expected) |
| 101 | |
| 102 | def testNoArgs(self): |
| 103 | result = script.ParseArgs(['exec']) |
| 104 | expected = self._Expected(['exec']) |
| 105 | self.assertEqual(result.gtest_parallel_args, expected) |
| 106 | |
| 107 | def testDocExample(self): |
Mirko Bonadei | dac422f | 2018-06-05 09:12:34 +0200 | [diff] [blame] | 108 | with TemporaryDirectory() as tmp_dir: |
| 109 | output_dir = os.path.join(tmp_dir, 'foo') |
| 110 | result = script.ParseArgs([ |
| 111 | 'some_test', '--some_flag=some_value', '--another_flag', |
| 112 | '--output_dir=' + output_dir, '--store-test-artifacts', |
| 113 | '--isolated-script-test-output=SOME_DIR', |
| 114 | '--isolated-script-test-perf-output=SOME_OTHER_DIR', |
| 115 | '--foo=bar', '--baz']) |
| 116 | expected_artifacts_dir = os.path.join(output_dir, 'test_artifacts') |
| 117 | expected = self._Expected([ |
| 118 | '--output_dir=' + output_dir, '--dump_json_test_results=SOME_DIR', |
| 119 | 'some_test', '--', |
| 120 | '--test_artifacts_dir=' + expected_artifacts_dir, |
| 121 | '--some_flag=some_value', '--another_flag', |
| 122 | '--isolated-script-test-perf-output=SOME_OTHER_DIR', |
| 123 | '--foo=bar', '--baz']) |
| 124 | self.assertEqual(result.gtest_parallel_args, expected) |
Oleh Prypin | 69c0222 | 2018-05-23 15:18:12 +0200 | [diff] [blame] | 125 | |
| 126 | |
| 127 | if __name__ == '__main__': |
| 128 | unittest.main() |