1# Copyright 2023 The Chromium Authors 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4 5from __future__ import annotations 6 7import abc 8import pathlib 9 10from crossbench import plt 11from crossbench.plt.posix import PosixPlatform 12from tests.crossbench.base import CrossbenchFakeFsTestCase 13from tests.crossbench.mock_helper import MockPlatform 14 15 16class BaseMockPlatformTestCase(CrossbenchFakeFsTestCase, metaclass=abc.ABCMeta): 17 __test__ = False 18 platform: plt.Platform 19 mock_platform: MockPlatform 20 21 def setUp(self) -> None: 22 super().setUp() 23 self.mock_platform_setup() 24 25 def mock_platform_setup(self): 26 self.mock_platform = MockPlatform() # pytype: disable=not-instantiable 27 self.platform = self.mock_platform 28 29 def tearDown(self): 30 expected_sh_cmds = self.mock_platform.expected_sh_cmds 31 if expected_sh_cmds is not None: 32 self.assertListEqual(expected_sh_cmds, [], 33 "Got additional unused shell cmds.") 34 super().tearDown() 35 36 def expect_sh(self, *args, result=""): 37 self.mock_platform.expect_sh(*args, result=result) 38 39 def test_is_android(self): 40 self.assertFalse(self.platform.is_android) 41 42 def test_is_macos(self): 43 self.assertFalse(self.platform.is_macos) 44 45 def test_is_linux(self): 46 self.assertFalse(self.platform.is_linux) 47 48 def test_is_win(self): 49 self.assertFalse(self.platform.is_win) 50 51 def test_is_posix(self): 52 self.assertFalse(self.platform.is_posix) 53 54 def test_is_remote_ssh(self): 55 self.assertFalse(self.platform.is_remote_ssh) 56 57 def test_is_chromeos(self): 58 self.assertFalse(self.platform.is_chromeos) 59 60 61class BasePosixMockPlatformTestCase(BaseMockPlatformTestCase): 62 platform: PosixPlatform 63 64 def tearDown(self) -> None: 65 assert isinstance(self.platform, PosixPlatform) 66 super().tearDown() 67 68 def test_is_posix(self): 69 self.assertTrue(self.platform.is_posix) 70 71 def test_path_conversion(self): 72 self.assertIsInstance(self.platform.path("foo/bar"), pathlib.PurePosixPath) 73 self.assertIsInstance( 74 self.platform.path(pathlib.PurePath("foo/bar")), pathlib.PurePosixPath) 75 self.assertIsInstance( 76 self.platform.path(pathlib.PureWindowsPath("foo/bar")), 77 pathlib.PurePosixPath) 78 self.assertIsInstance( 79 self.platform.path(pathlib.PurePosixPath("foo/bar")), 80 pathlib.PurePosixPath) 81