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 serve_repo.py.""" 6 7import argparse 8import unittest 9import unittest.mock as mock 10 11import serve_repo 12 13from common import REPO_ALIAS 14 15_REPO_DIR = 'test_repo_dir' 16_REPO_NAME = 'test_repo_name' 17_TARGET = 'test_target' 18 19 20class ServeRepoTest(unittest.TestCase): 21 """Unittests for serve_repo.py.""" 22 23 def setUp(self) -> None: 24 self._namespace = argparse.Namespace(repo=_REPO_DIR, 25 repo_name=_REPO_NAME, 26 target_id=_TARGET) 27 28 @mock.patch('serve_repo.run_ffx_command') 29 def test_run_serve_cmd_start(self, mock_ffx) -> None: 30 """Test |run_serve_cmd| function for start.""" 31 32 serve_repo.run_serve_cmd('start', self._namespace) 33 self.assertEqual(mock_ffx.call_count, 4) 34 second_call = mock_ffx.call_args_list[1] 35 self.assertEqual(['repository', 'server', 'start'], second_call[0][0]) 36 third_call = mock_ffx.call_args_list[2] 37 self.assertEqual( 38 ['repository', 'add-from-pm', _REPO_DIR, '-r', _REPO_NAME], 39 third_call[0][0]) 40 fourth_call = mock_ffx.call_args_list[3] 41 self.assertEqual([ 42 'target', 'repository', 'register', '-r', _REPO_NAME, '--alias', 43 REPO_ALIAS 44 ], fourth_call[0][0]) 45 self.assertEqual(_TARGET, fourth_call[0][1]) 46 47 @mock.patch('serve_repo.run_ffx_command') 48 def test_run_serve_cmd_stop(self, mock_ffx) -> None: 49 """Test |run_serve_cmd| function for stop.""" 50 51 serve_repo.run_serve_cmd('stop', self._namespace) 52 self.assertEqual(mock_ffx.call_count, 3) 53 first_call = mock_ffx.call_args_list[0] 54 self.assertEqual( 55 ['target', 'repository', 'deregister', '-r', _REPO_NAME], 56 first_call[0][0]) 57 self.assertEqual(_TARGET, first_call[0][1]) 58 second_call = mock_ffx.call_args_list[1] 59 self.assertEqual(['repository', 'remove', _REPO_NAME], 60 second_call[0][0]) 61 third_call = mock_ffx.call_args_list[2] 62 self.assertEqual(['repository', 'server', 'stop'], third_call[0][0]) 63 64 @mock.patch('serve_repo.run_serve_cmd') 65 def test_serve_repository(self, mock_serve) -> None: 66 """Tests |serve_repository| context manager.""" 67 68 with serve_repo.serve_repository(self._namespace): 69 self.assertEqual(mock_serve.call_count, 1) 70 self.assertEqual(mock_serve.call_count, 2) 71 72 def test_main_start_no_serve_repo_flag(self) -> None: 73 """Tests not specifying directory for start raises a ValueError.""" 74 75 with mock.patch('sys.argv', ['serve_repo.py', 'start']): 76 with self.assertRaises(ValueError): 77 serve_repo.main() 78 79 @mock.patch('serve_repo.run_serve_cmd') 80 def test_main_stop(self, mock_serve) -> None: 81 """Tests |main| function.""" 82 83 with mock.patch('sys.argv', ['serve_repo.py', 'stop']): 84 serve_repo.main() 85 self.assertEqual(mock_serve.call_count, 1) 86 87 88if __name__ == '__main__': 89 unittest.main() 90