From 2124be5caee6803d5bfe6f7cdc8e3367cb375807 Mon Sep 17 00:00:00 2001 From: George Burgess IV Date: Thu, 21 Apr 2022 10:27:37 -0700 Subject: toolchain_utils: s/Cr OS/CrOS/g Result of running `sed -ri 's/Chrom(ium|e) OS/Chrom\1OS/g' $(find -type f)`. BUG=None TEST=None Change-Id: I59be92537aa19bc989f52b585e307e76dbde401b Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/toolchain-utils/+/3600147 Reviewed-by: Manoj Gupta Commit-Queue: George Burgess Tested-by: George Burgess --- heatmaps/heat_map_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'heatmaps/heat_map_test.py') diff --git a/heatmaps/heat_map_test.py b/heatmaps/heat_map_test.py index ad62cd91..aabb3cac 100755 --- a/heatmaps/heat_map_test.py +++ b/heatmaps/heat_map_test.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- # -# Copyright 2019 The Chromium OS Authors. All rights reserved. +# Copyright 2019 The ChromiumOS Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. -- cgit v1.2.3 From 74bd380a27f4f0e8e90ff2dc1cef0b502d74961b Mon Sep 17 00:00:00 2001 From: George Burgess IV Date: Fri, 2 Sep 2022 16:59:27 -0700 Subject: Autoformat all Python code This autoformats all Python code with our new Python formatter, `black`. BUG=b:244644217 TEST=None Change-Id: I15ee49233d98fb6295c0c53c129bbf8e78e0d9ff Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/toolchain-utils/+/3877337 Tested-by: George Burgess Reviewed-by: Jordan Abrahams-Whitehead Commit-Queue: George Burgess --- heatmaps/heat_map_test.py | 278 +++++++++++++++++++++++++--------------------- 1 file changed, 150 insertions(+), 128 deletions(-) (limited to 'heatmaps/heat_map_test.py') diff --git a/heatmaps/heat_map_test.py b/heatmaps/heat_map_test.py index aabb3cac..0d3ca4e2 100755 --- a/heatmaps/heat_map_test.py +++ b/heatmaps/heat_map_test.py @@ -9,150 +9,172 @@ from __future__ import print_function -import unittest.mock as mock -import unittest - import os +import unittest +import unittest.mock as mock from cros_utils import command_executer - from heatmaps import heat_map from heatmaps import heatmap_generator -def make_heatmap(chromeos_root='/path/to/fake/chromeos_root/', - perf_data='/any_path/perf.data'): - return heat_map.HeatMapProducer(chromeos_root, perf_data, None, None, '') +def make_heatmap( + chromeos_root="/path/to/fake/chromeos_root/", + perf_data="/any_path/perf.data", +): + return heat_map.HeatMapProducer(chromeos_root, perf_data, None, None, "") def fake_mkdtemp(prefix): - """Mock tempfile.mkdtemp() by just create a pathname.""" - return prefix + 'random_dir' + """Mock tempfile.mkdtemp() by just create a pathname.""" + return prefix + "random_dir" def fake_parser_error(_, msg): - """Redirect parser.error() to exception.""" - raise Exception(msg) + """Redirect parser.error() to exception.""" + raise Exception(msg) def fake_generate_perf_report_exception(_): - raise Exception + raise Exception class HeatmapTest(unittest.TestCase): - """All of our tests for heat_map.""" - - # pylint: disable=protected-access - @mock.patch('shutil.copy2') - @mock.patch('tempfile.mkdtemp') - def test_EnsureFileInChrootAlreadyInside(self, mock_mkdtemp, mock_copy): - perf_data_inchroot = ( - '/path/to/fake/chromeos_root/chroot/inchroot_path/perf.data') - heatmap = make_heatmap(perf_data=perf_data_inchroot) - heatmap._EnsureFileInChroot() - self.assertFalse(heatmap.temp_dir_created) - self.assertEqual(heatmap.temp_dir, - '/path/to/fake/chromeos_root/chroot/inchroot_path/') - self.assertEqual(heatmap.temp_perf_inchroot, '/inchroot_path/') - mock_mkdtemp.assert_not_called() - mock_copy.assert_not_called() - - @mock.patch('shutil.copy2') - @mock.patch('tempfile.mkdtemp', fake_mkdtemp) - def test_EnsureFileInChrootOutsideNeedCopy(self, mock_copy): - heatmap = make_heatmap() - heatmap._EnsureFileInChroot() - self.assertTrue(heatmap.temp_dir_created) - self.assertEqual(mock_copy.call_count, 1) - self.assertEqual(heatmap.temp_dir, - '/path/to/fake/chromeos_root/src/random_dir') - self.assertEqual(heatmap.temp_perf_inchroot, '~/trunk/src/random_dir') - - @mock.patch.object(command_executer.CommandExecuter, 'ChrootRunCommand') - def test_GeneratePerfReport(self, mock_ChrootRunCommand): - heatmap = make_heatmap() - heatmap.temp_dir = '/fake/chroot/inchroot_path/' - heatmap.temp_perf_inchroot = '/inchroot_path/' - mock_ChrootRunCommand.return_value = 0 - heatmap._GeneratePerfReport() - cmd = ('cd %s && perf report -D -i perf.data > perf_report.txt' % - heatmap.temp_perf_inchroot) - mock_ChrootRunCommand.assert_called_with(heatmap.chromeos_root, cmd) - self.assertEqual(mock_ChrootRunCommand.call_count, 1) - self.assertEqual(heatmap.perf_report, - '/fake/chroot/inchroot_path/perf_report.txt') - - @mock.patch.object(heatmap_generator, 'HeatmapGenerator') - def test_GetHeatMap(self, mock_heatmap_generator): - heatmap = make_heatmap() - heatmap._GetHeatMap(10) - self.assertTrue(mock_heatmap_generator.called) - - @mock.patch.object(heat_map.HeatMapProducer, '_EnsureFileInChroot') - @mock.patch.object(heat_map.HeatMapProducer, '_GeneratePerfReport') - @mock.patch.object(heat_map.HeatMapProducer, '_GetHeatMap') - @mock.patch.object(heat_map.HeatMapProducer, '_RemoveFiles') - def test_Run(self, mock_remove_files, mock_get_heatmap, - mock_generate_perf_report, mock_ensure_file_in_chroot): - heatmap = make_heatmap() - heatmap.Run(10) - mock_ensure_file_in_chroot.assert_called_once_with() - mock_generate_perf_report.assert_called_once_with() - mock_get_heatmap.assert_called_once_with(10) - mock_remove_files.assert_called_once_with() - - @mock.patch.object(heat_map.HeatMapProducer, '_EnsureFileInChroot') - @mock.patch.object( - heat_map.HeatMapProducer, - '_GeneratePerfReport', - new=fake_generate_perf_report_exception) - @mock.patch.object(heat_map.HeatMapProducer, '_GetHeatMap') - @mock.patch.object(heat_map.HeatMapProducer, '_RemoveFiles') - @mock.patch('builtins.print') - def test_Run_with_exception(self, mock_print, mock_remove_files, - mock_get_heatmap, mock_ensure_file_in_chroot): - heatmap = make_heatmap() - with self.assertRaises(Exception): - heatmap.Run(10) - mock_ensure_file_in_chroot.assert_called_once_with() - mock_get_heatmap.assert_not_called() - mock_remove_files.assert_called_once_with() - mock_print.assert_not_called() - - @mock.patch('argparse.ArgumentParser.error', fake_parser_error) - @mock.patch.object(os.path, 'isfile') - @mock.patch.object(heat_map, 'IsARepoRoot') - def test_main_arg_format(self, mock_IsARepoRoot, mock_isfile): - """Test wrong arg format are detected.""" - args = ['--chromeos_root=/fake/chroot/', '--perf_data=/path/to/perf.data'] - - # Test --chromeos_root format - mock_IsARepoRoot.return_value = False - with self.assertRaises(Exception) as msg: - heat_map.main(args) - self.assertIn('does not contain .repo dir.', str(msg.exception)) - - # Test --perf_data format - mock_IsARepoRoot.return_value = True - mock_isfile.return_value = False - with self.assertRaises(Exception) as msg: - heat_map.main(args) - self.assertIn('Cannot find perf_data', str(msg.exception)) - - # Test --hugepage format - mock_isfile.return_value = True - args.append('--hugepage=0') - with self.assertRaises(Exception) as msg: - heat_map.main(args) - self.assertIn('Wrong format of hugepage range', str(msg.exception)) - - # Test --hugepage parse - args[-1] = '--hugepage=0,4096' - heat_map.HeatMapProducer = mock.MagicMock() - heat_map.main(args) - heat_map.HeatMapProducer.assert_called_with( - '/fake/chroot/', '/path/to/perf.data', [0, 4096], None, '') - - -if __name__ == '__main__': - unittest.main() + """All of our tests for heat_map.""" + + # pylint: disable=protected-access + @mock.patch("shutil.copy2") + @mock.patch("tempfile.mkdtemp") + def test_EnsureFileInChrootAlreadyInside(self, mock_mkdtemp, mock_copy): + perf_data_inchroot = ( + "/path/to/fake/chromeos_root/chroot/inchroot_path/perf.data" + ) + heatmap = make_heatmap(perf_data=perf_data_inchroot) + heatmap._EnsureFileInChroot() + self.assertFalse(heatmap.temp_dir_created) + self.assertEqual( + heatmap.temp_dir, + "/path/to/fake/chromeos_root/chroot/inchroot_path/", + ) + self.assertEqual(heatmap.temp_perf_inchroot, "/inchroot_path/") + mock_mkdtemp.assert_not_called() + mock_copy.assert_not_called() + + @mock.patch("shutil.copy2") + @mock.patch("tempfile.mkdtemp", fake_mkdtemp) + def test_EnsureFileInChrootOutsideNeedCopy(self, mock_copy): + heatmap = make_heatmap() + heatmap._EnsureFileInChroot() + self.assertTrue(heatmap.temp_dir_created) + self.assertEqual(mock_copy.call_count, 1) + self.assertEqual( + heatmap.temp_dir, "/path/to/fake/chromeos_root/src/random_dir" + ) + self.assertEqual(heatmap.temp_perf_inchroot, "~/trunk/src/random_dir") + + @mock.patch.object(command_executer.CommandExecuter, "ChrootRunCommand") + def test_GeneratePerfReport(self, mock_ChrootRunCommand): + heatmap = make_heatmap() + heatmap.temp_dir = "/fake/chroot/inchroot_path/" + heatmap.temp_perf_inchroot = "/inchroot_path/" + mock_ChrootRunCommand.return_value = 0 + heatmap._GeneratePerfReport() + cmd = ( + "cd %s && perf report -D -i perf.data > perf_report.txt" + % heatmap.temp_perf_inchroot + ) + mock_ChrootRunCommand.assert_called_with(heatmap.chromeos_root, cmd) + self.assertEqual(mock_ChrootRunCommand.call_count, 1) + self.assertEqual( + heatmap.perf_report, "/fake/chroot/inchroot_path/perf_report.txt" + ) + + @mock.patch.object(heatmap_generator, "HeatmapGenerator") + def test_GetHeatMap(self, mock_heatmap_generator): + heatmap = make_heatmap() + heatmap._GetHeatMap(10) + self.assertTrue(mock_heatmap_generator.called) + + @mock.patch.object(heat_map.HeatMapProducer, "_EnsureFileInChroot") + @mock.patch.object(heat_map.HeatMapProducer, "_GeneratePerfReport") + @mock.patch.object(heat_map.HeatMapProducer, "_GetHeatMap") + @mock.patch.object(heat_map.HeatMapProducer, "_RemoveFiles") + def test_Run( + self, + mock_remove_files, + mock_get_heatmap, + mock_generate_perf_report, + mock_ensure_file_in_chroot, + ): + heatmap = make_heatmap() + heatmap.Run(10) + mock_ensure_file_in_chroot.assert_called_once_with() + mock_generate_perf_report.assert_called_once_with() + mock_get_heatmap.assert_called_once_with(10) + mock_remove_files.assert_called_once_with() + + @mock.patch.object(heat_map.HeatMapProducer, "_EnsureFileInChroot") + @mock.patch.object( + heat_map.HeatMapProducer, + "_GeneratePerfReport", + new=fake_generate_perf_report_exception, + ) + @mock.patch.object(heat_map.HeatMapProducer, "_GetHeatMap") + @mock.patch.object(heat_map.HeatMapProducer, "_RemoveFiles") + @mock.patch("builtins.print") + def test_Run_with_exception( + self, + mock_print, + mock_remove_files, + mock_get_heatmap, + mock_ensure_file_in_chroot, + ): + heatmap = make_heatmap() + with self.assertRaises(Exception): + heatmap.Run(10) + mock_ensure_file_in_chroot.assert_called_once_with() + mock_get_heatmap.assert_not_called() + mock_remove_files.assert_called_once_with() + mock_print.assert_not_called() + + @mock.patch("argparse.ArgumentParser.error", fake_parser_error) + @mock.patch.object(os.path, "isfile") + @mock.patch.object(heat_map, "IsARepoRoot") + def test_main_arg_format(self, mock_IsARepoRoot, mock_isfile): + """Test wrong arg format are detected.""" + args = [ + "--chromeos_root=/fake/chroot/", + "--perf_data=/path/to/perf.data", + ] + + # Test --chromeos_root format + mock_IsARepoRoot.return_value = False + with self.assertRaises(Exception) as msg: + heat_map.main(args) + self.assertIn("does not contain .repo dir.", str(msg.exception)) + + # Test --perf_data format + mock_IsARepoRoot.return_value = True + mock_isfile.return_value = False + with self.assertRaises(Exception) as msg: + heat_map.main(args) + self.assertIn("Cannot find perf_data", str(msg.exception)) + + # Test --hugepage format + mock_isfile.return_value = True + args.append("--hugepage=0") + with self.assertRaises(Exception) as msg: + heat_map.main(args) + self.assertIn("Wrong format of hugepage range", str(msg.exception)) + + # Test --hugepage parse + args[-1] = "--hugepage=0,4096" + heat_map.HeatMapProducer = mock.MagicMock() + heat_map.main(args) + heat_map.HeatMapProducer.assert_called_with( + "/fake/chroot/", "/path/to/perf.data", [0, 4096], None, "" + ) + + +if __name__ == "__main__": + unittest.main() -- cgit v1.2.3 From c0041a9550814e402f661a560855ff99863cffb2 Mon Sep 17 00:00:00 2001 From: George Burgess IV Date: Tue, 6 Sep 2022 12:12:02 -0700 Subject: remove `from __future__ import ...` directives These are only useful when we're running code in a Python 2.7 interpreter. Since we no longer support python2, drop these. BUG=b:244644217 TEST=run_tests_for.py shows no new failures Change-Id: Ief9a12b87a560ab38ca71668636874bcb434a0b3 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/toolchain-utils/+/3877339 Reviewed-by: Ryan Beltran Commit-Queue: George Burgess Reviewed-by: Jordan Abrahams-Whitehead Tested-by: George Burgess --- heatmaps/heat_map_test.py | 1 - 1 file changed, 1 deletion(-) (limited to 'heatmaps/heat_map_test.py') diff --git a/heatmaps/heat_map_test.py b/heatmaps/heat_map_test.py index 0d3ca4e2..2b86363b 100755 --- a/heatmaps/heat_map_test.py +++ b/heatmaps/heat_map_test.py @@ -7,7 +7,6 @@ """Tests for heat_map.py.""" -from __future__ import print_function import os import unittest -- cgit v1.2.3 From fdcd39d5de4bd61cee94cf1e26416838d23092b8 Mon Sep 17 00:00:00 2001 From: Mike Frysinger Date: Tue, 13 Sep 2022 14:19:58 -0400 Subject: Update license boilerplate text in source code files Normally we don't do this, but enough changes have accumulated that we're doing a tree-wide one-off update of the name & style. BUG=chromium:1098010 TEST=`repo upload` works Change-Id: Icb42e5012a87920c2cd13b666fb3e55e7e4fb3b8 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/toolchain-utils/+/3891080 Auto-Submit: Mike Frysinger Tested-by: Mike Frysinger Commit-Queue: George Burgess Reviewed-by: George Burgess --- heatmaps/heat_map_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'heatmaps/heat_map_test.py') diff --git a/heatmaps/heat_map_test.py b/heatmaps/heat_map_test.py index 2b86363b..96300bb4 100755 --- a/heatmaps/heat_map_test.py +++ b/heatmaps/heat_map_test.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- # -# Copyright 2019 The ChromiumOS Authors. All rights reserved. +# Copyright 2019 The ChromiumOS Authors # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. -- cgit v1.2.3