Skip to content

Commit a7ec5ca

Browse files
committed
Snippet numbers and accurate line numbers for docs-check command
Signed-off-by: Ole Herman Schumacher Elgesem <ole.elgesem@northern.tech>
1 parent fe92e0e commit a7ec5ca

File tree

2 files changed

+50
-14
lines changed

2 files changed

+50
-14
lines changed

src/cfengine_cli/docs.py

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def fn_extract(origin_path, snippet_path, _language, first_line, last_line):
110110
raise UserError(f"Couldn't open '{origin_path}' or '{snippet_path}'")
111111

112112

113-
def fn_check_syntax(origin_path, snippet_path, language, first_line, _last_line):
113+
def fn_check_syntax(origin_path, snippet_path, language, first_line, _last_line, snippet_number):
114114
snippet_abs_path = os.path.abspath(snippet_path)
115115

116116
if not os.path.exists(snippet_path):
@@ -120,9 +120,9 @@ def fn_check_syntax(origin_path, snippet_path, language, first_line, _last_line)
120120

121121
match language:
122122
case "cf":
123-
r = lint_policy_file(snippet_abs_path)
123+
r = lint_policy_file(snippet_abs_path, origin_path, first_line + 1, snippet_number)
124124
if r != 0:
125-
raise UserError(f"Error when checking '{snippet_abs_path}'")
125+
raise UserError(f"Error when checking '{origin_path}'")
126126
case "json":
127127
try:
128128
with open(snippet_abs_path, "r") as f:
@@ -183,19 +183,34 @@ def fn_autoformat(_origin_path, snippet_path, language, _first_line, _last_line)
183183
except json.decoder.JSONDecodeError:
184184
raise UserError(f"Invalid json in '{snippet_path}'")
185185

186+
def _translate_language(x):
187+
if x == "cf3" or x == "cfengine3":
188+
return "cf"
189+
if x == "yaml":
190+
return "yml"
191+
return x
192+
193+
194+
SUPPORTED_LANGUAGES = ["cf", "cfengine3", "cf3", "json", "yml", "yaml"]
186195

187196
def _process_markdown_code_blocks(
188197
path, languages, extract, syntax_check, output_check, autoformat, replace, cleanup
189198
):
190-
supported_languages = {"cf3": "cf", "json": "json", "yaml": "yml"}
191-
192199
if not os.path.exists(path):
193200
raise UserError("This path doesn't exist")
194201

202+
languages = set(languages)
203+
if "cf3" in languages or "cf" in languages or "cfengine3" in languages:
204+
languages.add("cf3")
205+
languages.add("cfengine3")
206+
languages.add("cf")
207+
if "yaml" in languages or "yml" in languages:
208+
languages.add("yml")
209+
languages.add("yaml")
195210
for language in languages:
196-
if language not in supported_languages:
211+
if language not in SUPPORTED_LANGUAGES:
197212
raise UserError(
198-
f"Unsupported language '{language}'. The supported languages are: {", ".join(supported_languages.keys())}"
213+
f"Unsupported language '{language}'. The supported languages are: {", ".join(SUPPORTED_LANGUAGES)}"
199214
)
200215

201216
parsed_markdowns = get_markdown_files(path, languages)
@@ -211,8 +226,9 @@ def _process_markdown_code_blocks(
211226
cb["first_line"] += offset
212227
cb["last_line"] += offset
213228

214-
language = supported_languages[code_block["language"]]
215-
snippet_path = f"{origin_path}.snippet-{i + 1}.{language}"
229+
language = _translate_language(code_block["language"])
230+
snippet_number = i + 1
231+
snippet_path = f"{origin_path}.snippet-{snippet_number}.{language}"
216232

217233
flags = code_block["flags"]
218234
if "noextract" in flags or "skip" in flags:
@@ -235,6 +251,7 @@ def _process_markdown_code_blocks(
235251
language,
236252
code_block["first_line"],
237253
code_block["last_line"],
254+
snippet_number,
238255
)
239256
except Exception as e:
240257
if cleanup:
@@ -332,7 +349,7 @@ def check_docs() -> int:
332349
cfengine dev docs-check"""
333350
_process_markdown_code_blocks(
334351
path=".",
335-
languages=["json"],
352+
languages=["json", "cf3"],
336353
extract=True,
337354
syntax_check=True,
338355
output_check=False,

src/cfengine_cli/lint.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,14 @@ def _walk(filename, lines, node) -> int:
9797
return errors
9898

9999

100-
def lint_policy_file(filename):
100+
def lint_policy_file(filename, original_filename=None, original_line=None, snippet=None):
101+
assert original_filename is None or type(original_filename) is str
102+
assert original_line is None or type(original_line) is int
103+
assert snippet is None or type(snippet) is int
104+
if original_filename is not None or original_line is not None or snippet is not None:
105+
assert original_filename and os.path.isfile(original_filename)
106+
assert original_line and original_line > 0
107+
assert snippet and snippet > 0
101108
assert os.path.isfile(filename)
102109
assert filename.endswith((".cf", ".cfengine3", ".cf3", ".cf.sub"))
103110
PY_LANGUAGE = Language(tscfengine.language())
@@ -112,12 +119,24 @@ def lint_policy_file(filename):
112119
assert root_node.type == "source_file"
113120
errors = 0
114121
if not root_node.children:
115-
print(f"Error: Empty policy file '{filename}'")
122+
if snippet:
123+
assert original_filename and original_line
124+
print(f"Error: Empty policy snippet {snippet} at '{original_filename}:{original_line}'")
125+
else:
126+
print(f"Error: Empty policy file '{filename}'")
116127
errors += 1
117128
errors += _walk(filename, lines, root_node)
118129
if errors == 0:
119-
print(f"PASS: {filename}")
130+
if snippet:
131+
assert original_filename and original_line
132+
print(f"PASS: Snippet {snippet} at '{original_filename}:{original_line}'")
133+
else:
134+
print(f"PASS: {filename}")
120135
return 0
121136

122-
print(f"FAIL: {filename} ({errors} error{'s' if errors > 0 else ''})")
137+
if snippet:
138+
assert original_filename and original_line
139+
print(f"FAIL: Snippet {snippet} at '{original_filename}:{original_line}'")
140+
else:
141+
print(f"FAIL: {filename} ({errors} error{'s' if errors > 0 else ''})")
123142
return errors

0 commit comments

Comments
 (0)