blob: 9c8e748e82515167d300c852e4ab6975cd1ae350 [file] [log] [blame]
Trent Apted72468352023-07-11 16:15:57 +10001# Copyright 2023 The ChromiumOS Authors
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""Tests for the analyzers module."""
6
Trent Apted4d4f1bd2023-06-26 12:24:54 +10007from typing import List
8
Trent Apted72468352023-07-11 16:15:57 +10009from chromite.cli import analyzers
Trent Apted4d4f1bd2023-06-26 12:24:54 +100010from chromite.lib import commandline
11
12
13def process_args(args: List[str]) -> commandline.ArgumentNamespace:
14 """Feeds an ArgumentParser with the provided `args` to AnalyzerCommand."""
15 parser = commandline.ArgumentParser()
16 analyzers.AnalyzerCommand.AddParser(parser)
17 parser_namespace = parser.parse_args(args)
18 analyzers.AnalyzerCommand.ProcessOptions(parser, parser_namespace)
19 return parser_namespace
Trent Apted72468352023-07-11 16:15:57 +100020
21
22def test_get_files_from_commit(run_mock) -> None:
Trent Apted4d4f1bd2023-06-26 12:24:54 +100023 """Test files from commit are correctly reconstructed as absolute paths."""
Trent Apted72468352023-07-11 16:15:57 +100024 run_mock.AddCmdResult(
25 ["git", "rev-parse", "--show-toplevel"], stdout="/path/to/root\n"
26 )
Trent Apted4d4f1bd2023-06-26 12:24:54 +100027 run_mock.AddCmdResult(
28 ["git", "diff-tree", "--no-commit-id", "--name-only", "-r", "ignored"],
29 stdout="file1\nsub/file2\n",
30 )
31 # There is a third, more verbose call to `git` to check for uncommitted
32 # changes. Use no output to indicate that there are none.
33 run_mock.SetDefaultCmdResult(stdout="")
34 parser_namespace = process_args(["--commit", "ignored"])
35 assert run_mock.call_count == 3
36 assert parser_namespace.files == [
Trent Apted72468352023-07-11 16:15:57 +100037 "/path/to/root/file1",
38 "/path/to/root/sub/file2",
39 ]
40
41
Trent Apted4d4f1bd2023-06-26 12:24:54 +100042def test_commit_is_pre_submit(run_mock) -> None:
43 """Test the special "pre-submit" commit id used by pre-upload.py."""
44 parser_namespace = process_args(["--commit", "pre-submit"])
45 assert not run_mock.called, "Should not call out to git."
46 assert not parser_namespace.files, "Files should remain empty."
47
48
Trent Apted72468352023-07-11 16:15:57 +100049def test_has_uncommitted_changes(run_mock) -> None:
Trent Apted4d4f1bd2023-06-26 12:24:54 +100050 """Test handling of porcelain output for uncommitted change."""
Trent Apted72468352023-07-11 16:15:57 +100051 run_mock.SetDefaultCmdResult(stdout="M file\n")
52 assert analyzers.HasUncommittedChanges(["/path/to/file"]) is True
53
54
55def test_has_no_uncommitted_changes(run_mock) -> None:
Trent Apted4d4f1bd2023-06-26 12:24:54 +100056 """Test handling of porcelain output when no uncommitted changes."""
Trent Apted72468352023-07-11 16:15:57 +100057 run_mock.SetDefaultCmdResult(stdout="")
58 assert analyzers.HasUncommittedChanges(["/path/to/file"]) is False