| 1 |
#!/usr/bin/env python3 |
| 2 |
# Copyright: 2026, Maximiliano Curia <[email protected]> |
| 3 |
# |
| 4 |
# License: ISC |
| 5 |
# Permission to use, copy, modify, and/or distribute this software for any |
| 6 |
# purpose with or without fee is hereby granted, provided that the above |
| 7 |
# copyright notice and this permission notice appear in all copies. |
| 8 |
# . |
| 9 |
# THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH |
| 10 |
# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY |
| 11 |
# AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, |
| 12 |
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM |
| 13 |
# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE |
| 14 |
# OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 15 |
# PERFORMANCE OF THIS SOFTWARE. |
| 16 |
|
| 17 |
"""Unit tests for decopy._resolve_run_options. |
| 18 |
|
| 19 |
The resolver settles the run's scope (Files-Excluded + git-changed set) into a |
| 20 |
NEW, immutable Options and a deleted-paths tuple, without mutating its input. |
| 21 |
These tests mock the git call, so they need no repository. |
| 22 |
""" |
| 23 |
|
| 24 |
from __future__ import annotations |
| 25 |
|
| 26 |
import unittest |
| 27 |
from pathlib import Path |
| 28 |
from unittest.mock import patch |
| 29 |
|
| 30 |
from decopy import decopy |
| 31 |
from decopy.cmdoptions import process_options |
| 32 |
from decopy.fstree import vcs |
| 33 |
|
| 34 |
_ROOT = str(Path(__file__).parent / "testdata" / "one") |
| 35 |
|
| 36 |
|
| 37 |
class ResolveRunOptionsTest(unittest.TestCase): |
| 38 |
def _options(self, *extra): |
| 39 |
return process_options(["--no-progress", "--root", _ROOT, *extra]) |
| 40 |
|
| 41 |
def test_non_changed_sets_files_excluded_and_leaves_input(self): |
| 42 |
options = self._options() |
| 43 |
with patch.object(decopy, "read_files_excluded", return_value=("foo/*",)): |
| 44 |
resolved, deleted = decopy._resolve_run_options(options) |
| 45 |
self.assertIsNot(resolved, options) |
| 46 |
self.assertEqual(resolved.files_excluded, ("foo/*",)) |
| 47 |
self.assertEqual(resolved.mode, "full") |
| 48 |
self.assertEqual(deleted, ()) |
| 49 |
# The input is untouched. |
| 50 |
self.assertEqual(options.files_excluded, ()) |
| 51 |
|
| 52 |
def test_changed_fallback_degrades_to_full_without_mutating(self): |
| 53 |
options = self._options("--changed") |
| 54 |
fallback = vcs.Fallback(reason="no-git", message="not a git repo") |
| 55 |
with ( |
| 56 |
patch.object(decopy, "read_files_excluded", return_value=()), |
| 57 |
patch.object(decopy.vcs, "changed_paths", return_value=fallback), |
| 58 |
): |
| 59 |
resolved, deleted = decopy._resolve_run_options(options) |
| 60 |
self.assertEqual(resolved.mode, "full") |
| 61 |
self.assertEqual(deleted, ()) |
| 62 |
# The input mode is not rewritten. |
| 63 |
self.assertEqual(options.mode, "changed") |
| 64 |
|
| 65 |
def test_changed_unions_files_and_reports_deleted_without_mutating(self): |
| 66 |
options = self._options("--changed") |
| 67 |
original_files = list(options.files) |
| 68 |
scope = vcs.ChangedPaths( |
| 69 |
added_or_modified=("a.py", "b.py"), |
| 70 |
deleted=("old.py",), |
| 71 |
renamed=(("was.py", "now.py"),), |
| 72 |
) |
| 73 |
with ( |
| 74 |
patch.object(decopy, "read_files_excluded", return_value=()), |
| 75 |
patch.object(decopy.vcs, "changed_paths", return_value=scope), |
| 76 |
): |
| 77 |
resolved, deleted = decopy._resolve_run_options(options) |
| 78 |
self.assertEqual( |
| 79 |
set(resolved.files), {*original_files, "a.py", "b.py", "now.py"} |
| 80 |
) |
| 81 |
# deleted = removals plus the old side of renames, sorted. |
| 82 |
self.assertEqual(deleted, ("old.py", "was.py")) |
| 83 |
self.assertEqual(resolved.mode, "changed") |
| 84 |
# The input file list is untouched. |
| 85 |
self.assertEqual(options.files, original_files) |
| 86 |
|
| 87 |
|
| 88 |
if __name__ == "__main__": |
| 89 |
unittest.main() |