Trent Apted | 7246835 | 2023-07-11 16:15:57 +1000 | [diff] [blame] | 1 | # 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 Apted | 4d4f1bd | 2023-06-26 12:24:54 +1000 | [diff] [blame] | 7 | from typing import List |
| 8 | |
Trent Apted | 7246835 | 2023-07-11 16:15:57 +1000 | [diff] [blame] | 9 | from chromite.cli import analyzers |
Trent Apted | 4d4f1bd | 2023-06-26 12:24:54 +1000 | [diff] [blame] | 10 | from chromite.lib import commandline |
| 11 | |
| 12 | |
| 13 | def 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 Apted | 7246835 | 2023-07-11 16:15:57 +1000 | [diff] [blame] | 20 | |
| 21 | |
| 22 | def test_get_files_from_commit(run_mock) -> None: |
Trent Apted | 4d4f1bd | 2023-06-26 12:24:54 +1000 | [diff] [blame] | 23 | """Test files from commit are correctly reconstructed as absolute paths.""" |
Trent Apted | 7246835 | 2023-07-11 16:15:57 +1000 | [diff] [blame] | 24 | run_mock.AddCmdResult( |
| 25 | ["git", "rev-parse", "--show-toplevel"], stdout="/path/to/root\n" |
| 26 | ) |
Trent Apted | 4d4f1bd | 2023-06-26 12:24:54 +1000 | [diff] [blame] | 27 | 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 Apted | 7246835 | 2023-07-11 16:15:57 +1000 | [diff] [blame] | 37 | "/path/to/root/file1", |
| 38 | "/path/to/root/sub/file2", |
| 39 | ] |
| 40 | |
| 41 | |
Trent Apted | 4d4f1bd | 2023-06-26 12:24:54 +1000 | [diff] [blame] | 42 | def 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 Apted | 7246835 | 2023-07-11 16:15:57 +1000 | [diff] [blame] | 49 | def test_has_uncommitted_changes(run_mock) -> None: |
Trent Apted | 4d4f1bd | 2023-06-26 12:24:54 +1000 | [diff] [blame] | 50 | """Test handling of porcelain output for uncommitted change.""" |
Trent Apted | 7246835 | 2023-07-11 16:15:57 +1000 | [diff] [blame] | 51 | run_mock.SetDefaultCmdResult(stdout="M file\n") |
| 52 | assert analyzers.HasUncommittedChanges(["/path/to/file"]) is True |
| 53 | |
| 54 | |
| 55 | def test_has_no_uncommitted_changes(run_mock) -> None: |
Trent Apted | 4d4f1bd | 2023-06-26 12:24:54 +1000 | [diff] [blame] | 56 | """Test handling of porcelain output when no uncommitted changes.""" |
Trent Apted | 7246835 | 2023-07-11 16:15:57 +1000 | [diff] [blame] | 57 | run_mock.SetDefaultCmdResult(stdout="") |
| 58 | assert analyzers.HasUncommittedChanges(["/path/to/file"]) is False |