• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2024 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 os
8import pathlib
9from unittest import mock
10
11from crossbench import path as pth
12from tests import test_helper
13from tests.crossbench.mock_helper import WinMockPlatform
14from tests.crossbench.plt.helper import BaseMockPlatformTestCase
15
16
17class WinMockPlatformTestCase(BaseMockPlatformTestCase):
18  __test__ = True
19
20  def mock_platform_setup(self):
21    self.mock_platform = WinMockPlatform()
22    self.platform = self.mock_platform
23
24  def path(self, path: pth.AnyPathLike) -> pathlib.PureWindowsPath:
25    return pathlib.PureWindowsPath(path)
26
27  def test_is_win(self):
28    self.assertTrue(self.platform.is_win)
29
30  def test_path_conversion(self):
31    self.assertIsInstance(
32        self.platform.path("foo/bar"), pathlib.PureWindowsPath)
33    self.assertIsInstance(
34        self.platform.path(pathlib.PurePath("foo/bar")),
35        pathlib.PureWindowsPath)
36    self.assertIsInstance(
37        self.platform.path(pathlib.PureWindowsPath("foo/bar")),
38        pathlib.PureWindowsPath)
39    self.assertIsInstance(
40        self.platform.path(pathlib.PurePosixPath("foo/bar")),
41        pathlib.PureWindowsPath)
42
43  def test_which(self):
44    bin_path = self.path("foo/bar/default/crossbench_mock_binary.exe")
45    self.assertIsNone(self.platform.which(bin_path))
46    with mock.patch("shutil.which", return_value=bin_path) as cm:
47      self.assertEqual(self.platform.which(bin_path), bin_path)
48    cm.assert_called_once_with(os.fspath(bin_path))
49
50  def test_which_invalid(self):
51    with self.assertRaises(ValueError) as cm:
52      self.platform.which("")
53    self.assertIn("empty", str(cm.exception))
54
55  def test_search_binary_invalid(self):
56    with self.assertRaises(ValueError) as cm:
57      self.platform.search_binary("")
58    self.assertIn("empty", str(cm.exception))
59    with self.assertRaises(ValueError) as cm:
60      self.platform.search_binary("foo/bar")
61    self.assertIn(".exe", str(cm.exception))
62
63  def test_search_binary_broken_which(self):
64    bin_path = self.path("foo/bar/default/crossbench_mock_binary.exe")
65    self.assertIsNone(self.platform.search_app(bin_path))
66    with mock.patch("shutil.which", return_value=bin_path) as cm:
67      with self.assertRaises(AssertionError) as search_cm:
68        self.assertEqual(self.platform.search_app(bin_path), bin_path)
69      self.assertIn("exist", str(search_cm.exception))
70    cm.assert_called_once_with(os.fspath(bin_path))
71
72  def test_search_binary(self):
73    bin_path = self.path("foo/bar/default/crossbench_mock_binary.exe")
74    self.assertFalse(self.platform.exists(bin_path))
75    self.assertIsNone(self.platform.search_app(bin_path))
76    self.fs.create_file(self.platform.local_path(bin_path), st_size=100)
77    self.assertTrue(self.platform.exists(bin_path))
78    with mock.patch("shutil.which", return_value=bin_path) as cm:
79      self.assertEqual(self.platform.search_app(bin_path), bin_path)
80    cm.assert_called_once_with(os.fspath(bin_path))
81
82
83if __name__ == "__main__":
84  test_helper.run_pytest(__file__)
85