Skip to content

Commit eec0306

Browse files
authored
Refactor exit codes to use an enum (#3300)
1 parent 4dd02a9 commit eec0306

File tree

9 files changed

+31
-24
lines changed

9 files changed

+31
-24
lines changed

src/ansiblelint/__main__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
render_yaml,
5050
)
5151
from ansiblelint.config import get_version_warning, log_entries, options
52-
from ansiblelint.constants import EXIT_CONTROL_C_RC, GIT_CMD, LOCK_TIMEOUT_RC
52+
from ansiblelint.constants import GIT_CMD, RC
5353
from ansiblelint.file_utils import abspath, cwd, normpath
5454
from ansiblelint.loaders import load_ignore_txt
5555
from ansiblelint.skip_utils import normalize_tag
@@ -136,7 +136,7 @@ def initialize_options(arguments: list[str] | None = None) -> None:
136136
_logger.error(
137137
"Timeout waiting for another instance of ansible-lint to release the lock.",
138138
)
139-
sys.exit(LOCK_TIMEOUT_RC)
139+
sys.exit(RC.LOCK_TIMEOUT)
140140

141141
# Avoid extra output noise from Ansible about using devel versions
142142
if "ANSIBLE_DEVEL_WARNING" not in os.environ: # pragma: no branch
@@ -373,7 +373,7 @@ def _run_cli_entrypoint() -> None:
373373
if exc.errno != errno.EPIPE: # pragma: no cover
374374
raise
375375
except KeyboardInterrupt: # pragma: no cover
376-
sys.exit(EXIT_CONTROL_C_RC)
376+
sys.exit(RC.EXIT_CONTROL_C)
377377
except RuntimeError as exc: # pragma: no cover
378378
raise SystemExit(exc) from exc
379379

src/ansiblelint/_mockings.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import sys
88

99
from ansiblelint.config import options
10-
from ansiblelint.constants import ANSIBLE_MOCKED_MODULE, INVALID_CONFIG_RC
10+
from ansiblelint.constants import ANSIBLE_MOCKED_MODULE, RC
1111

1212
_logger = logging.getLogger(__name__)
1313

@@ -35,7 +35,7 @@ def _make_module_stub(module_name: str) -> None:
3535
)
3636
else:
3737
_logger.error("Config error: %s is not a valid module name.", module_name)
38-
sys.exit(INVALID_CONFIG_RC)
38+
sys.exit(RC.INVALID_CONFIG)
3939

4040

4141
def _write_module_stub(

src/ansiblelint/app.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from ansiblelint.color import console, console_stderr, render_yaml
1717
from ansiblelint.config import PROFILES, get_version_warning
1818
from ansiblelint.config import options as default_options
19-
from ansiblelint.constants import RULE_DOC_URL, SUCCESS_RC, VIOLATIONS_FOUND_RC
19+
from ansiblelint.constants import RC, RULE_DOC_URL
2020
from ansiblelint.errors import MatchError
2121
from ansiblelint.loaders import IGNORE_FILE
2222
from ansiblelint.stats import SummarizedResults, TagStats
@@ -224,7 +224,7 @@ def report_outcome(self, result: LintResult, mark_as_success: bool = False) -> i
224224
is_success=mark_as_success,
225225
)
226226

227-
return SUCCESS_RC if mark_as_success else VIOLATIONS_FOUND_RC
227+
return RC.SUCCESS if mark_as_success else RC.VIOLATIONS_FOUND
228228

229229
def report_summary( # pylint: disable=too-many-branches,too-many-locals
230230
self,

src/ansiblelint/cli.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from ansiblelint.constants import (
1515
CUSTOM_RULESDIR_ENVVAR,
1616
DEFAULT_RULESDIR,
17-
INVALID_CONFIG_RC,
17+
RC,
1818
)
1919
from ansiblelint.file_utils import (
2020
Lintable,
@@ -68,7 +68,7 @@ def load_config(config_file: str) -> dict[Any, Any]:
6868
config_path = os.path.abspath(config_file)
6969
if not os.path.exists(config_path):
7070
_logger.error("Config file not found '%s'", config_path)
71-
sys.exit(INVALID_CONFIG_RC)
71+
sys.exit(RC.INVALID_CONFIG)
7272
config_path = config_path or get_config_path()
7373
if not config_path or not os.path.exists(config_path):
7474
# a missing default config file should not trigger an error
@@ -82,7 +82,7 @@ def load_config(config_file: str) -> dict[Any, Any]:
8282

8383
for error in validate_file_schema(config_lintable):
8484
_logger.error("Invalid configuration file %s. %s", config_path, error)
85-
sys.exit(INVALID_CONFIG_RC)
85+
sys.exit(RC.INVALID_CONFIG)
8686

8787
config = clean_json(config_lintable.data)
8888
if not isinstance(config, dict):

src/ansiblelint/constants.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,18 @@
77
CUSTOM_RULESDIR_ENVVAR = "ANSIBLE_LINT_CUSTOM_RULESDIR"
88
RULE_DOC_URL = "https://ansible-lint.readthedocs.io/rules/"
99

10-
SUCCESS_RC = 0
11-
VIOLATIONS_FOUND_RC = 2
12-
INVALID_CONFIG_RC = 3
13-
LOCK_TIMEOUT_RC = 4
14-
EXIT_CONTROL_C_RC = 130
10+
11+
# Not using an IntEnum because only starting with py3.11 it will evaluate it
12+
# as int.
13+
class RC: # pylint: disable=too-few-public-methods
14+
"""All exist codes used by ansible-lint."""
15+
16+
SUCCESS = 0
17+
VIOLATIONS_FOUND = 2
18+
INVALID_CONFIG = 3
19+
LOCK_TIMEOUT = 4
20+
EXIT_CONTROL_C = 130
21+
1522

1623
# Minimal version of Ansible we support for runtime
1724
ANSIBLE_MIN_VERSION = "2.12"

src/ansiblelint/rules/only_builtins.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def matchtask(
4545
# pylint: disable=ungrouped-imports
4646
import pytest
4747

48-
from ansiblelint.constants import SUCCESS_RC, VIOLATIONS_FOUND_RC
48+
from ansiblelint.constants import RC
4949
from ansiblelint.testing import RunFromText, run_ansible_lint
5050

5151
SUCCESS_PLAY = """
@@ -68,7 +68,7 @@ def test_only_builtins_fail() -> None:
6868
"only-builtins",
6969
"examples/playbooks/rule-only-builtins.yml",
7070
)
71-
assert result.returncode == VIOLATIONS_FOUND_RC
71+
assert result.returncode == RC.VIOLATIONS_FOUND
7272
assert "Failed" in result.stderr
7373
assert "warning(s)" in result.stderr
7474
assert "only-builtins: Use only builtin actions" in result.stdout
@@ -85,7 +85,7 @@ def test_only_builtins_allow() -> None:
8585
"examples/playbooks/rule-only-builtins.yml",
8686
)
8787
assert "only-builtins" not in result.stdout
88-
assert result.returncode == SUCCESS_RC
88+
assert result.returncode == RC.SUCCESS
8989

9090
@pytest.mark.parametrize(
9191
"rule_runner",

src/ansiblelint/rules/var_naming.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from ansible.parsing.yaml.objects import AnsibleUnicode
1010

1111
from ansiblelint.config import options
12-
from ansiblelint.constants import LINE_NUMBER_KEY, SUCCESS_RC
12+
from ansiblelint.constants import LINE_NUMBER_KEY, RC
1313
from ansiblelint.file_utils import Lintable
1414
from ansiblelint.rules import AnsibleLintRule, RulesCollection
1515
from ansiblelint.runner import Runner
@@ -235,7 +235,7 @@ def test_var_naming_with_pattern() -> None:
235235
f"--config-file={conf_path}",
236236
role_path,
237237
)
238-
assert result.returncode == SUCCESS_RC
238+
assert result.returncode == RC.SUCCESS
239239
assert "var-naming" not in result.stdout
240240

241241
def test_is_invalid_variable_name() -> None:

test/test_mockings.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
import pytest
33

44
from ansiblelint._mockings import _make_module_stub
5-
from ansiblelint.constants import INVALID_CONFIG_RC
5+
from ansiblelint.constants import RC
66

77

88
def test_make_module_stub() -> None:
99
"""Test make module stub."""
1010
with pytest.raises(SystemExit) as exc:
1111
_make_module_stub("")
1212
assert exc.type == SystemExit
13-
assert exc.value.code == INVALID_CONFIG_RC
13+
assert exc.value.code == RC.INVALID_CONFIG

test/test_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
from ansiblelint import cli, constants, utils
3939
from ansiblelint.__main__ import initialize_logger
4040
from ansiblelint.cli import get_rules_dirs
41-
from ansiblelint.constants import VIOLATIONS_FOUND_RC
41+
from ansiblelint.constants import RC
4242
from ansiblelint.file_utils import Lintable
4343
from ansiblelint.rules import RulesCollection
4444
from ansiblelint.runner import Runner
@@ -292,7 +292,7 @@ def test_cli_auto_detect(capfd: CaptureFixture[str]) -> None:
292292
result = subprocess.run(cmd, check=False).returncode
293293

294294
# We de expect to fail on our own repo due to test examples we have
295-
assert result == VIOLATIONS_FOUND_RC
295+
assert result == RC.VIOLATIONS_FOUND
296296

297297
out, err = capfd.readouterr()
298298

0 commit comments

Comments
 (0)