|
| 1 | +import os |
| 2 | +import re |
| 3 | +import sys |
| 4 | +import tempfile |
| 5 | +import unittest |
| 6 | + |
| 7 | +# Ensure the tasks directory is importable when running the test directly |
| 8 | +here = os.path.dirname(__file__) |
| 9 | +if here not in sys.path: |
| 10 | + sys.path.insert(0, here) |
| 11 | + |
| 12 | +from run_benchmarks import update_pre_blocks_under_module |
| 13 | + |
| 14 | + |
| 15 | +class TestRunBenchmarksFiltering(unittest.TestCase): |
| 16 | + def setUp(self): |
| 17 | + # sample JMH table with mixed-class lines |
| 18 | + self.table = ( |
| 19 | + 'Benchmark Mode Cnt Score Error Units\n' |
| 20 | + 'CounterBenchmark.codahaleIncNoLabels thrpt 57881.585 ops/s\n' |
| 21 | + 'HistogramBenchmark.prometheusNative thrpt 2385.134 ops/s\n' |
| 22 | + 'TextFormatUtilBenchmark.prometheusWriteToNull thrpt 885331.328 ops/s\n' |
| 23 | + 'CounterBenchmark.prometheusInc thrpt 54090.469 ops/s\n' |
| 24 | + ) |
| 25 | + |
| 26 | + # create temp dir to act as module path |
| 27 | + self.tmpdir = tempfile.TemporaryDirectory() |
| 28 | + self.module_path = self.tmpdir.name |
| 29 | + |
| 30 | + # Create three files with a javadoc <pre> block that contains mixed results |
| 31 | + self.files = {} |
| 32 | + javadoc_pre = ( |
| 33 | + '/**\n' |
| 34 | + ' * Example javadoc\n' |
| 35 | + ' * <pre>\n' |
| 36 | + ' * Benchmark Mode Cnt Score Error Units\n' |
| 37 | + ' * CounterBenchmark.codahaleIncNoLabels thrpt 57881.585 ops/s\n' |
| 38 | + ' * HistogramBenchmark.prometheusNative thrpt 2385.134 ops/s\n' |
| 39 | + ' * TextFormatUtilBenchmark.prometheusWriteToNull thrpt 885331.328 ops/s\n' |
| 40 | + ' * CounterBenchmark.prometheusInc thrpt 54090.469 ops/s\n' |
| 41 | + ' * </pre>\n' |
| 42 | + ' */\n' |
| 43 | + ) |
| 44 | + |
| 45 | + for cls in ('CounterBenchmark', 'HistogramBenchmark', 'TextFormatUtilBenchmark'): |
| 46 | + fname = os.path.join(self.module_path, f'{cls}.java') |
| 47 | + with open(fname, 'w', encoding='utf-8') as f: |
| 48 | + f.write(javadoc_pre) |
| 49 | + f.write(f'public class {cls} {{}}\n') |
| 50 | + self.files[cls] = fname |
| 51 | + |
| 52 | + def tearDown(self): |
| 53 | + self.tmpdir.cleanup() |
| 54 | + |
| 55 | + def _read_pre_contents(self, path): |
| 56 | + with open(path, 'r', encoding='utf-8') as f: |
| 57 | + content = f.read() |
| 58 | + m = re.search(r'<pre>\n([\s\S]*?)</pre>', content) |
| 59 | + return m.group(1) if m else '' |
| 60 | + |
| 61 | + def test_update_only_inserts_matching_class_lines(self): |
| 62 | + updated = update_pre_blocks_under_module(self.module_path, self.table) |
| 63 | + # All three files should be updated |
| 64 | + self.assertEqual(set(os.path.basename(p) for p in updated), |
| 65 | + {os.path.basename(self.files['CounterBenchmark']), |
| 66 | + os.path.basename(self.files['HistogramBenchmark']), |
| 67 | + os.path.basename(self.files['TextFormatUtilBenchmark'])}) |
| 68 | + |
| 69 | + # Verify CounterBenchmark file contains only CounterBenchmark lines |
| 70 | + cb_pre = self._read_pre_contents(self.files['CounterBenchmark']) |
| 71 | + self.assertIn('CounterBenchmark.codahaleIncNoLabels', cb_pre) |
| 72 | + self.assertIn('CounterBenchmark.prometheusInc', cb_pre) |
| 73 | + self.assertNotIn('HistogramBenchmark.prometheusNative', cb_pre) |
| 74 | + self.assertNotIn('TextFormatUtilBenchmark.prometheusWriteToNull', cb_pre) |
| 75 | + |
| 76 | + # Verify HistogramBenchmark contains only its line |
| 77 | + hb_pre = self._read_pre_contents(self.files['HistogramBenchmark']) |
| 78 | + self.assertIn('HistogramBenchmark.prometheusNative', hb_pre) |
| 79 | + self.assertNotIn('CounterBenchmark.codahaleIncNoLabels', hb_pre) |
| 80 | + self.assertNotIn('TextFormatUtilBenchmark.prometheusWriteToNull', hb_pre) |
| 81 | + |
| 82 | + # Verify TextFormatUtilBenchmark contains only its line |
| 83 | + tf_pre = self._read_pre_contents(self.files['TextFormatUtilBenchmark']) |
| 84 | + self.assertIn('TextFormatUtilBenchmark.prometheusWriteToNull', tf_pre) |
| 85 | + self.assertNotIn('CounterBenchmark.prometheusInc', tf_pre) |
| 86 | + self.assertNotIn('HistogramBenchmark.prometheusNative', tf_pre) |
| 87 | + |
| 88 | + |
| 89 | +if __name__ == '__main__': |
| 90 | + unittest.main() |
0 commit comments