OLD | NEW |
(Empty) | |
| 1 # Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. |
| 4 |
| 5 import os |
| 6 import unittest |
| 7 |
| 8 from infra.tools.metric_tool import metric_tool |
| 9 from infra_libs import temporary_directory |
| 10 |
| 11 |
| 12 DATA_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'data') |
| 13 |
| 14 |
| 15 class MainMetricToolTest(unittest.TestCase): |
| 16 def test_smoke_main(self): |
| 17 metric_tool.main(DATA_DIR) |
| 18 |
| 19 |
| 20 class DescriptionExtractionTest(unittest.TestCase): |
| 21 def test_extract_normal_case(self): |
| 22 descriptions = metric_tool.extract_metrics_descriptions( |
| 23 os.path.join(DATA_DIR, 'normal_case.py')) |
| 24 |
| 25 self.assertEqual(len(descriptions), len(metric_tool.METRICS_NAMES)) |
| 26 # Cheap way of testing we're retrieving the correct strings. |
| 27 for filename, _, name, desc in descriptions: |
| 28 self.assertTrue(filename.endswith('normal_case.py')) |
| 29 self.assertTrue(name.endswith(desc)) |
| 30 |
| 31 def test_extract_normal_case_2(self): |
| 32 descriptions = metric_tool.extract_metrics_descriptions( |
| 33 os.path.join(DATA_DIR, 'normal_case_2.py')) |
| 34 |
| 35 self.assertEqual(len(descriptions), len(metric_tool.METRICS_NAMES)) |
| 36 # Cheap way of testing we're retrieving the correct strings. |
| 37 for filename, _, name, desc in descriptions: |
| 38 self.assertTrue(filename.endswith('normal_case_2.py')) |
| 39 self.assertTrue(name.endswith(desc)) |
| 40 |
| 41 def test_missing_descriptions(self): |
| 42 descriptions = metric_tool.extract_metrics_descriptions( |
| 43 os.path.join(DATA_DIR, 'missing_descriptions.py')) |
| 44 |
| 45 self.assertEqual(len(descriptions), 9) |
| 46 |
| 47 self.assertEqual(sum([desc[3] is None for desc in descriptions]), 5) |
| 48 self.assertEqual(sum([desc[3] is not None for desc in descriptions]), 4) |
| 49 |
| 50 # Cheap way of testing we're retrieving the correct strings. |
| 51 for filename, _, name, desc in descriptions: |
| 52 self.assertTrue(filename.endswith('missing_descriptions.py')) |
| 53 self.assertTrue(desc is None or name.endswith(desc)) |
| 54 |
| 55 def test_metric_name_not_a_string(self): |
| 56 descriptions = metric_tool.extract_metrics_descriptions( |
| 57 os.path.join(DATA_DIR, 'metric_name_not_a_string.py')) |
| 58 self.assertEqual(len(descriptions), 1) |
| 59 self.assertEqual(descriptions[0][2], 'DYNAMIC') |
| 60 self.assertEqual(descriptions[0][3], 'metric1') |
| 61 |
| 62 def test_missing_file(self): |
| 63 descriptions = metric_tool.extract_metrics_descriptions( |
| 64 os.path.join(DATA_DIR, 'should_not_exist.py')) |
| 65 self.assertEqual(descriptions, []) |
| 66 |
| 67 def test_invalid_syntax(self): |
| 68 with temporary_directory(prefix='metric_tool-') as tempdir: |
| 69 tempfile = os.path.join(tempdir, 'invalid_syntax.py') |
| 70 with open(tempfile, 'w') as f: |
| 71 f.write('=1\n') |
| 72 |
| 73 descriptions = metric_tool.extract_metrics_descriptions(tempfile) |
| 74 |
| 75 self.assertEqual(descriptions, []) |
| 76 |
| 77 def test_other_tests(self): |
| 78 descriptions = metric_tool.extract_metrics_descriptions( |
| 79 os.path.join(DATA_DIR, 'other_tests.py')) |
| 80 self.assertEqual(len(descriptions), 1) |
| 81 description = descriptions[0] |
| 82 self.assertEqual(description[2], '/my/metric') |
| 83 self.assertEqual(description[3], None) |
OLD | NEW |