1#!/usr/bin/env vpython3 2# Copyright 2022 The Chromium Authors 3# Use of this source code is governed by a BSD-style license that can be 4# found in the LICENSE file. 5"""File for testing publish_package.py.""" 6 7import argparse 8import unittest 9import unittest.mock as mock 10 11from io import StringIO 12 13import publish_package 14 15_PACKAGES = ['test_package'] 16_REPO = 'test_repo' 17 18 19class PublishPackageTest(unittest.TestCase): 20 """Unittests for publish_package.py.""" 21 22 def setUp(self) -> None: 23 self._subprocess_patcher = mock.patch('publish_package.subprocess.run') 24 self._subprocess_mock = self._subprocess_patcher.start() 25 self.addCleanup(self._subprocess_mock.stop) 26 27 def test_new_repo(self) -> None: 28 """Test setting |new_repo| to True in |publish_packages|.""" 29 30 publish_package.publish_packages(_PACKAGES, _REPO, True) 31 self.assertEqual(self._subprocess_mock.call_count, 2) 32 first_call = self._subprocess_mock.call_args_list[0] 33 self.assertEqual(['newrepo', '-repo', _REPO], first_call[0][0][1:]) 34 second_call = self._subprocess_mock.call_args_list[1] 35 self.assertEqual(['publish', '-a', '-r', _REPO, '-f', _PACKAGES[0]], 36 second_call[0][0][1:]) 37 38 def test_no_new_repo(self) -> None: 39 """Test setting |new_repo| to False in |publish_packages|.""" 40 41 publish_package.publish_packages(['test_package'], 'test_repo', False) 42 self.assertEqual(self._subprocess_mock.call_count, 1) 43 44 45 def test_allow_temp_repo(self) -> None: 46 """Test setting |allow_temp_repo| to True in |register_package_args|.""" 47 48 parser = argparse.ArgumentParser() 49 publish_package.register_package_args(parser, True) 50 args = parser.parse_args(['--no-repo-init']) 51 self.assertEqual(args.no_repo_init, True) 52 53 @mock.patch('sys.stderr', new_callable=StringIO) 54 def test_not_allow_temp_repo(self, mock_stderr) -> None: 55 """Test setting |allow_temp_repo| to False in 56 |register_package_args|.""" 57 58 parser = argparse.ArgumentParser() 59 publish_package.register_package_args(parser) 60 with self.assertRaises(SystemExit): 61 parser.parse_args(['--no-repo-init']) 62 self.assertRegex(mock_stderr.getvalue(), 'unrecognized arguments') 63 64 def test_main_no_repo_flag(self) -> None: 65 """Tests that not specifying packages raise a ValueError.""" 66 67 with mock.patch('sys.argv', ['publish_package.py', '--repo', _REPO]): 68 with self.assertRaises(ValueError): 69 publish_package.main() 70 71 def test_main_no_packages_flag(self) -> None: 72 """Tests that not specifying directory raise a ValueError.""" 73 74 with mock.patch('sys.argv', 75 ['publish_package.py', '--packages', _PACKAGES[0]]): 76 with self.assertRaises(ValueError): 77 publish_package.main() 78 79 def test_main_no_out_dir_flag(self) -> None: 80 """Tests |main| with `out_dir` omitted.""" 81 82 with mock.patch('sys.argv', [ 83 'publish_package.py', '--packages', _PACKAGES[0], '--repo', 84 _REPO 85 ]): 86 publish_package.main() 87 self.assertEqual(self._subprocess_mock.call_count, 1) 88 89 @mock.patch('publish_package.read_package_paths') 90 def test_main(self, read_mock) -> None: 91 """Tests |main|.""" 92 93 read_mock.return_value = ['out/test/package/path'] 94 with mock.patch('sys.argv', [ 95 'publish_package.py', '--packages', _PACKAGES[0], '--repo', 96 _REPO, '--out-dir', 'out/test' 97 ]): 98 publish_package.main() 99 self.assertEqual(self._subprocess_mock.call_count, 1) 100 101 102if __name__ == '__main__': 103 unittest.main() 104