Files
computer-playbook/tests/unit/cli/deploy/development/test_profile.py
T
kevinveenbirkenbachandClaude Opus 4.7 26ef3ff504 chore(ruff): enable strict ruleset (E/F/I/B/UP, py311) — 2819 → 0
Adds ``[tool.ruff.lint]`` with ``select = ["E", "F", "I", "B", "UP"]``
and ``target-version = "py311"``. ``E501`` is ignored because
``ruff format`` already reflows what it can; the remaining long lines
are unsplittable string literals / URLs.

Auto-fixed (2753 issues, behavior-preserving):
- UP006/UP035: ``List[X]`` → ``list[X]``, ``Dict[X]`` → ``dict[X]``,
  ``from typing import …`` redirected where superseded by builtins.
- UP045: ``Optional[X]`` → ``X | None``.
- UP010/UP009/UP015: dropped Python-2-era pragmas (``from __future__
  import absolute_import, division, print_function``,
  ``# -*- coding: utf-8 -*-``), redundant ``open(..., "r")`` mode.
- UP004: dropped implicit ``class X(object):``.
- UP022/UP008/UP032: stdlib idiom modernization (``capture_output``,
  super-call simplification, etc.).
- B905: ``zip(...)`` → ``zip(..., strict=False)`` (preserves prior
  behavior; switching to ``strict=True`` is a separate semantic
  choice not made here).
- I001: import-block ordering.

Hand-fixed (66 issues):
- B904 × 54: appended ``from <bound>`` (when the enclosing ``except``
  binds a name) or ``from None`` (when it doesn't) so chained
  exceptions stay distinguishable from errors-during-exception-
  handling. Applied via a small AST-aware script that walked back to
  the enclosing ``except`` clause and matched the binding.
- B017 × 4: narrowed broad ``assertRaises(Exception)`` to the actual
  ``AnsibleError`` / ``AnsibleFilterError`` raised by the code under
  test.
- B023 × 2: bound captured loop variables via default args
  (``def _record(name, *, _path_real=path_real, ...)``) so each
  closure gets its own snapshot.
- B007 × 2: unused loop control variables renamed to ``_`` /
  rewritten to ``.values()``.
- UP007 × 2: ``Union[X, Y]`` → ``X | Y`` (plus dropping the now-unused
  ``from typing import Union``).
- B018 × 1: Odoo addon manifests are bare dict expressions at module
  level by design — suppressed with an inline ``# noqa: B018`` and
  rationale comment.
- UP031 × 1: printf-style format → f-string.

The 65 ``# noqa: …`` markers introduced by the auto-fixer all carry
real ruff codes (``B018``, ``E402``, ``F401``, etc.), so
``test_noqa_only_ruff_codes`` stays green.

Verification
============
- ``ruff check .`` → All checks passed
- ``ruff format --check .`` → 894 files already formatted
- ``tests.lint.repository.{test_project_root_import,
  test_no_direct_yaml_calls, test_noqa_only_ruff_codes}`` and
  ``tests.unit.utils.annotations.test_suppress`` → 20 tests OK
- 1621 unit tests → all pass
- Integration suite: 44 failures (pre-existing baseline, none from
  this change), 1 skipped.

Going forward, the new rules block regressions on E (style), F
(pyflakes), I (import order), B (likely-bug patterns), and UP
(modernization to py311) for any new code.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-07 16:57:20 +02:00

92 lines
3.3 KiB
Python

"""Unit tests for Profile."""
from __future__ import annotations
import os
import unittest
from unittest.mock import patch
from cli.deploy.development.profile import Profile
# Blank explicit signals; clear=True would unset PATH.
_BLANK_CI_ENV = {
"GITHUB_ACTIONS": "",
"RUNNING_ON_GITHUB": "",
"CI": "",
}
class TestProfileIsCI(unittest.TestCase):
@patch.dict(os.environ, _BLANK_CI_ENV, clear=False)
def test_is_ci_false_when_no_signals_set(self) -> None:
self.assertFalse(Profile().is_ci())
@patch.dict(os.environ, {**_BLANK_CI_ENV, "GITHUB_ACTIONS": "true"}, clear=False)
def test_is_ci_true_when_github_actions_signal_set(self) -> None:
self.assertTrue(Profile().is_ci())
@patch.dict(os.environ, {**_BLANK_CI_ENV, "RUNNING_ON_GITHUB": "true"}, clear=False)
def test_is_ci_true_when_running_on_github_signal_set(self) -> None:
self.assertTrue(Profile().is_ci())
@patch.dict(os.environ, {**_BLANK_CI_ENV, "CI": "true"}, clear=False)
def test_is_ci_true_when_generic_ci_signal_set(self) -> None:
self.assertTrue(Profile().is_ci())
@patch.dict(
os.environ,
{**_BLANK_CI_ENV, "GITHUB_ACTIONS": "1"},
clear=False,
)
def test_is_ci_false_when_signal_value_is_one(self) -> None:
self.assertFalse(Profile().is_ci())
@patch.dict(os.environ, {**_BLANK_CI_ENV, "GITHUB_ACTIONS": "false"}, clear=False)
def test_is_ci_false_when_signal_value_is_explicit_false(self) -> None:
self.assertFalse(Profile().is_ci())
@patch.dict(os.environ, {**_BLANK_CI_ENV, "GITHUB_ACTIONS": "yes"}, clear=False)
def test_is_ci_false_when_signal_value_is_yes(self) -> None:
self.assertFalse(Profile().is_ci())
class TestProfileRegistryCacheActive(unittest.TestCase):
@patch.dict(os.environ, _BLANK_CI_ENV, clear=False)
def test_active_locally_when_no_ci_signal_set(self) -> None:
self.assertTrue(Profile().registry_cache_active())
@patch.dict(os.environ, {**_BLANK_CI_ENV, "GITHUB_ACTIONS": "true"}, clear=False)
def test_inactive_under_github_actions(self) -> None:
self.assertFalse(Profile().registry_cache_active())
@patch.dict(os.environ, {**_BLANK_CI_ENV, "CI": "true"}, clear=False)
def test_inactive_under_generic_ci_signal(self) -> None:
self.assertFalse(Profile().registry_cache_active())
@patch.dict(os.environ, {**_BLANK_CI_ENV, "RUNNING_ON_GITHUB": "true"}, clear=False)
def test_is_strict_inverse_of_is_ci(self) -> None:
p = Profile()
self.assertEqual(p.registry_cache_active(), not p.is_ci())
class TestProfileArgs(unittest.TestCase):
@patch.dict(os.environ, {**_BLANK_CI_ENV, "CI": "true"}, clear=False)
def test_args_ci_only_on_runner(self) -> None:
self.assertEqual(Profile().args(), ["--profile", "ci"])
@patch.dict(os.environ, _BLANK_CI_ENV, clear=False)
def test_args_returns_only_ci_profile_locally(self) -> None:
self.assertEqual(Profile().args(), ["--profile", "ci"])
@patch.dict(os.environ, _BLANK_CI_ENV, clear=False)
def test_args_returns_a_fresh_list_each_call(self) -> None:
# Callers may mutate the returned list.
p = Profile()
first = p.args()
first.append("--mutated")
self.assertNotIn("--mutated", p.args())
if __name__ == "__main__": # pragma: no cover
unittest.main()