Coverage for tests / unit / tools / hadolint / test_options.py: 100%
90 statements
« prev ^ index » next coverage.py v7.13.0, created at 2026-04-03 18:53 +0000
« prev ^ index » next coverage.py v7.13.0, created at 2026-04-03 18:53 +0000
1"""Unit tests for hadolint plugin options and command building."""
3from __future__ import annotations
5import pytest
6from assertpy import assert_that
8from lintro.tools.definitions.hadolint import (
9 HADOLINT_DEFAULT_FAILURE_THRESHOLD,
10 HADOLINT_DEFAULT_FORMAT,
11 HADOLINT_DEFAULT_NO_COLOR,
12 HADOLINT_DEFAULT_TIMEOUT,
13 HadolintPlugin,
14)
17@pytest.mark.parametrize(
18 ("option_name", "expected_value"),
19 [
20 ("timeout", HADOLINT_DEFAULT_TIMEOUT),
21 ("format", HADOLINT_DEFAULT_FORMAT),
22 ("failure_threshold", HADOLINT_DEFAULT_FAILURE_THRESHOLD),
23 ("no_color", HADOLINT_DEFAULT_NO_COLOR),
24 ],
25 ids=[
26 "timeout_equals_default",
27 "format_equals_default",
28 "failure_threshold_equals_default",
29 "no_color_equals_default",
30 ],
31)
32def test_default_options_values(
33 hadolint_plugin: HadolintPlugin,
34 option_name: str,
35 expected_value: object,
36) -> None:
37 """Default options have correct values.
39 Args:
40 hadolint_plugin: The HadolintPlugin instance to test.
41 option_name: The name of the option to check.
42 expected_value: The expected value for the option.
43 """
44 assert_that(hadolint_plugin.definition.default_options).contains_key(option_name)
45 assert_that(hadolint_plugin.definition.default_options[option_name]).is_equal_to(
46 expected_value,
47 )
50# =============================================================================
51# Tests for HadolintPlugin.set_options method
52# =============================================================================
55def test_set_options_format(hadolint_plugin: HadolintPlugin) -> None:
56 """Set format option.
58 Args:
59 hadolint_plugin: The HadolintPlugin instance to test.
60 """
61 hadolint_plugin.set_options(format="json")
62 assert_that(hadolint_plugin.options.get("format")).is_equal_to("json")
65def test_set_options_failure_threshold(hadolint_plugin: HadolintPlugin) -> None:
66 """Set failure_threshold option.
68 Args:
69 hadolint_plugin: The HadolintPlugin instance to test.
70 """
71 hadolint_plugin.set_options(failure_threshold="warning")
72 assert_that(hadolint_plugin.options.get("failure_threshold")).is_equal_to("warning")
75def test_set_options_ignore(hadolint_plugin: HadolintPlugin) -> None:
76 """Set ignore option.
78 Args:
79 hadolint_plugin: The HadolintPlugin instance to test.
80 """
81 rules = ["DL3006", "SC2086"]
82 hadolint_plugin.set_options(ignore=rules)
83 assert_that(hadolint_plugin.options.get("ignore")).is_equal_to(rules)
86def test_set_options_trusted_registries(hadolint_plugin: HadolintPlugin) -> None:
87 """Set trusted_registries option.
89 Args:
90 hadolint_plugin: The HadolintPlugin instance to test.
91 """
92 registries = ["docker.io", "gcr.io"]
93 hadolint_plugin.set_options(trusted_registries=registries)
94 assert_that(hadolint_plugin.options.get("trusted_registries")).is_equal_to(
95 registries,
96 )
99def test_set_options_require_labels(hadolint_plugin: HadolintPlugin) -> None:
100 """Set require_labels option.
102 Args:
103 hadolint_plugin: The HadolintPlugin instance to test.
104 """
105 labels = ["maintainer:text", "version:semver"]
106 hadolint_plugin.set_options(require_labels=labels)
107 assert_that(hadolint_plugin.options.get("require_labels")).is_equal_to(labels)
110def test_set_options_strict_labels(hadolint_plugin: HadolintPlugin) -> None:
111 """Set strict_labels option.
113 Args:
114 hadolint_plugin: The HadolintPlugin instance to test.
115 """
116 hadolint_plugin.set_options(strict_labels=True)
117 assert_that(hadolint_plugin.options.get("strict_labels")).is_true()
120def test_set_options_no_fail(hadolint_plugin: HadolintPlugin) -> None:
121 """Set no_fail option.
123 Args:
124 hadolint_plugin: The HadolintPlugin instance to test.
125 """
126 hadolint_plugin.set_options(no_fail=True)
127 assert_that(hadolint_plugin.options.get("no_fail")).is_true()
130def test_set_options_no_color(hadolint_plugin: HadolintPlugin) -> None:
131 """Set no_color option.
133 Args:
134 hadolint_plugin: The HadolintPlugin instance to test.
135 """
136 hadolint_plugin.set_options(no_color=False)
137 assert_that(hadolint_plugin.options.get("no_color")).is_false()
140def test_set_options_no_options(hadolint_plugin: HadolintPlugin) -> None:
141 """Handle no options set.
143 Args:
144 hadolint_plugin: The HadolintPlugin instance to test.
145 """
146 hadolint_plugin.set_options()
147 # Should not raise
150def test_set_options_invalid_ignore_type(hadolint_plugin: HadolintPlugin) -> None:
151 """Raise ValueError for invalid ignore type.
153 Args:
154 hadolint_plugin: The HadolintPlugin instance to test.
155 """
156 with pytest.raises(ValueError, match="ignore must be a list"):
157 hadolint_plugin.set_options(ignore="DL3006") # type: ignore[arg-type]
160def test_set_options_invalid_strict_labels_type(
161 hadolint_plugin: HadolintPlugin,
162) -> None:
163 """Raise ValueError for invalid strict_labels type.
165 Args:
166 hadolint_plugin: The HadolintPlugin instance to test.
167 """
168 with pytest.raises(ValueError, match="strict_labels must be a boolean"):
169 hadolint_plugin.set_options(strict_labels="yes") # type: ignore[arg-type]
172# =============================================================================
173# Tests for HadolintPlugin._build_command method
174# =============================================================================
177def test_build_command_default(hadolint_plugin: HadolintPlugin) -> None:
178 """Build command with default options.
180 Args:
181 hadolint_plugin: The HadolintPlugin instance to test.
182 """
183 cmd = hadolint_plugin._build_command()
185 assert_that(cmd).contains("hadolint")
186 assert_that(cmd).contains("--format", "tty")
187 assert_that(cmd).contains("--failure-threshold", "info")
188 assert_that(cmd).contains("--no-color")
191def test_build_command_with_format(hadolint_plugin: HadolintPlugin) -> None:
192 """Build command with custom format.
194 Args:
195 hadolint_plugin: The HadolintPlugin instance to test.
196 """
197 hadolint_plugin.set_options(format="json")
198 cmd = hadolint_plugin._build_command()
200 assert_that("--format" in cmd).is_true()
201 idx = cmd.index("--format")
202 assert_that(cmd[idx + 1]).is_equal_to("json")
205def test_build_command_with_failure_threshold(hadolint_plugin: HadolintPlugin) -> None:
206 """Build command with custom failure threshold.
208 Args:
209 hadolint_plugin: The HadolintPlugin instance to test.
210 """
211 hadolint_plugin.set_options(failure_threshold="error")
212 cmd = hadolint_plugin._build_command()
214 assert_that("--failure-threshold" in cmd).is_true()
215 idx = cmd.index("--failure-threshold")
216 assert_that(cmd[idx + 1]).is_equal_to("error")
219def test_build_command_with_ignore(hadolint_plugin: HadolintPlugin) -> None:
220 """Build command with ignore rules.
222 Args:
223 hadolint_plugin: The HadolintPlugin instance to test.
224 """
225 hadolint_plugin.set_options(ignore=["DL3006", "SC2086"])
226 cmd = hadolint_plugin._build_command()
228 assert_that(cmd.count("--ignore")).is_equal_to(2)
229 assert_that(cmd).contains("DL3006")
230 assert_that(cmd).contains("SC2086")
233def test_build_command_with_trusted_registries(
234 hadolint_plugin: HadolintPlugin,
235) -> None:
236 """Build command with trusted registries.
238 Args:
239 hadolint_plugin: The HadolintPlugin instance to test.
240 """
241 hadolint_plugin.set_options(trusted_registries=["docker.io", "gcr.io"])
242 cmd = hadolint_plugin._build_command()
244 assert_that(cmd.count("--trusted-registry")).is_equal_to(2)
245 assert_that(cmd).contains("docker.io")
246 assert_that(cmd).contains("gcr.io")
249def test_build_command_with_require_labels(hadolint_plugin: HadolintPlugin) -> None:
250 """Build command with required labels.
252 Args:
253 hadolint_plugin: The HadolintPlugin instance to test.
254 """
255 hadolint_plugin.set_options(require_labels=["maintainer:text"])
256 cmd = hadolint_plugin._build_command()
258 assert_that(cmd).contains("--require-label")
259 assert_that(cmd).contains("maintainer:text")
262def test_build_command_with_strict_labels(hadolint_plugin: HadolintPlugin) -> None:
263 """Build command with strict labels.
265 Args:
266 hadolint_plugin: The HadolintPlugin instance to test.
267 """
268 hadolint_plugin.set_options(strict_labels=True)
269 cmd = hadolint_plugin._build_command()
271 assert_that(cmd).contains("--strict-labels")
274def test_build_command_with_no_fail(hadolint_plugin: HadolintPlugin) -> None:
275 """Build command with no-fail option.
277 Args:
278 hadolint_plugin: The HadolintPlugin instance to test.
279 """
280 hadolint_plugin.set_options(no_fail=True)
281 cmd = hadolint_plugin._build_command()
283 assert_that(cmd).contains("--no-fail")
286def test_build_command_without_no_color(hadolint_plugin: HadolintPlugin) -> None:
287 """Build command without no-color when disabled.
289 Args:
290 hadolint_plugin: The HadolintPlugin instance to test.
291 """
292 hadolint_plugin.set_options(no_color=False)
293 cmd = hadolint_plugin._build_command()
295 assert_that("--no-color" in cmd).is_false()