blob: 0e40166a25c5ecad58e2d81c1ccf4ba4d567635c [file] [log] [blame]
# Copyright 2022 The ChromiumOS Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Tests the `cros try` command."""
from pathlib import Path
from typing import List
from chromite.cli.cros import cros_try
from chromite.lib import cipd
from chromite.lib import cros_test_lib
from chromite.scripts import cros
MOCK_TRY_DIR = Path("/tmp/try")
MOCK_TRY_BIN = MOCK_TRY_DIR / "try"
class TryCommandTest(cros_test_lib.RunCommandTestCase):
"""Test the TryCommand class."""
def setUp(self):
"""Create patches."""
self._cipd_install_patch = self.PatchObject(
cipd, "InstallPackage", return_value=MOCK_TRY_DIR
)
def runCrosTry(self, try_args: List[str]):
"""Simulate running the `cros try` command with the specified args."""
return cros.main(["try"] + try_args)
def testArgsForwarding(self):
"""Test that calling `cros try` forwards args to the try binary."""
self.runCrosTry(["release", "-staging"])
self.rc.assertCommandCalled(
[str(MOCK_TRY_BIN), "release", "-staging"],
check=False,
stderr=True,
encoding="utf-8",
)
def testExitCode(self):
"""Test that `cros try` returns the try binary's exit code."""
self.rc.AddCmdResult([str(MOCK_TRY_BIN), "invalid-cmd"], returncode=128)
actual_retcode = self.runCrosTry(["invalid-cmd"])
self.assertEqual(actual_retcode, 128)
def testDoubleDashes(self):
"""Unit tests for _ModifyFlagsToDoubleDashes."""
# pylint: disable=protected-access
for in_str, expected_out in (
("-flag", "--flag"),
("\n-flag", "\n--flag"),
("--flag", "--flag"),
("-contains-dash", "--contains-dash"),
("[-flag]", "[--flag]"),
("-g", "-g"),
):
actual_out = cros_try._ModifyFlagsToDoubleDashes(in_str)
self.assertEqual(actual_out, expected_out)
def testTryVersion(self):
"""Test the default/overridden try version."""
self.runCrosTry(["release"])
self._cipd_install_patch.assert_called_with(
cipd.GetCIPDFromCache(),
cros_try.CIPD_TRY_PACKAGE,
"prod",
)
self.runCrosTry(
["--try-version", "my-cool-version", "release", "--staging"]
)
self._cipd_install_patch.assert_called_with(
cipd.GetCIPDFromCache(),
cros_try.CIPD_TRY_PACKAGE,
"my-cool-version",
)
self.rc.assertCommandCalled(
[str(MOCK_TRY_BIN), "release", "--staging"],
check=False,
stderr=True,
encoding="utf-8",
)