xref: /aosp_15_r20/external/cronet/build/fuchsia/test/publish_package_unittests.py (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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._ffx_patcher = mock.patch('publish_package.run_ffx_command')
24        self._ffx_mock = self._ffx_patcher.start()
25        self.addCleanup(self._ffx_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._ffx_mock.call_count, 2)
32        first_call, second_call = self._ffx_mock.call_args_list
33        self.assertEqual(['repository', 'create', _REPO],
34                         first_call.kwargs['cmd'])
35        self.assertEqual([
36            'repository', 'publish', '--package-archive', _PACKAGES[0], _REPO
37        ], second_call.kwargs['cmd'])
38
39    def test_no_new_repo(self) -> None:
40        """Test setting |new_repo| to False in |publish_packages|."""
41
42        publish_package.publish_packages(['test_package'], 'test_repo', False)
43        self.assertEqual(self._ffx_mock.call_count, 1)
44
45
46    def test_allow_temp_repo(self) -> None:
47        """Test setting |allow_temp_repo| to True in |register_package_args|."""
48
49        parser = argparse.ArgumentParser()
50        publish_package.register_package_args(parser, True)
51        args = parser.parse_args(['--no-repo-init'])
52        self.assertEqual(args.no_repo_init, True)
53
54    @mock.patch('sys.stderr', new_callable=StringIO)
55    def test_not_allow_temp_repo(self, mock_stderr) -> None:
56        """Test setting |allow_temp_repo| to False in
57        |register_package_args|."""
58
59        parser = argparse.ArgumentParser()
60        publish_package.register_package_args(parser)
61        with self.assertRaises(SystemExit):
62            parser.parse_args(['--no-repo-init'])
63        self.assertRegex(mock_stderr.getvalue(), 'unrecognized arguments')
64
65    def test_main_no_repo_flag(self) -> None:
66        """Tests that not specifying packages raise a ValueError."""
67
68        with mock.patch('sys.argv', ['publish_package.py', '--repo', _REPO]):
69            with self.assertRaises(ValueError):
70                publish_package.main()
71
72    def test_main_no_packages_flag(self) -> None:
73        """Tests that not specifying directory raise a ValueError."""
74
75        with mock.patch('sys.argv',
76                        ['publish_package.py', '--packages', _PACKAGES[0]]):
77            with self.assertRaises(ValueError):
78                publish_package.main()
79
80    def test_main_no_out_dir_flag(self) -> None:
81        """Tests |main| with `out_dir` omitted."""
82
83        with mock.patch('sys.argv', [
84                'publish_package.py', '--packages', _PACKAGES[0], '--repo',
85                _REPO
86        ]):
87            publish_package.main()
88            self.assertEqual(self._ffx_mock.call_count, 1)
89
90    @mock.patch('publish_package.read_package_paths')
91    def test_main(self, read_mock) -> None:
92        """Tests |main|."""
93
94        read_mock.return_value = ['out/test/package/path']
95        with mock.patch('sys.argv', [
96                'publish_package.py', '--packages', _PACKAGES[0], '--repo',
97                _REPO, '--out-dir', 'out/test'
98        ]):
99            publish_package.main()
100            self.assertEqual(self._ffx_mock.call_count, 1)
101
102    @mock.patch('publish_package.read_package_paths')
103    @mock.patch('publish_package.make_clean_directory')
104    def test_purge_repo(self, read_mock, make_clean_directory_mock) -> None:
105        """Tests purge_repo flag."""
106
107        read_mock.return_value = ['out/test/package/path']
108        with mock.patch('sys.argv', [
109                'publish_package.py', '--packages', _PACKAGES[0], '--repo',
110                _REPO, '--out-dir', 'out/test', '--purge-repo'
111        ]):
112            publish_package.main()
113            self.assertEqual(self._ffx_mock.call_count, 2)
114            self.assertEqual(make_clean_directory_mock.call_count, 1)
115
116
117if __name__ == '__main__':
118    unittest.main()
119