Skip to content

Commit 31001f9

Browse files
authored
Merge pull request #301 from olehermanse/master
Improved validation error messages for module names
2 parents 55c5cc1 + 1ba8878 commit 31001f9

5 files changed

Lines changed: 30 additions & 70 deletions

File tree

cfbs/cfbs_config.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
3. Add modules and make commits
1414
"""
1515

16-
1716
import os
1817
import copy
1918
import glob

cfbs/git_magic.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import logging as log
2020
from functools import partial
2121

22-
2322
first_commit = True
2423

2524

cfbs/man_generator.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ def generate_man_page():
1818
+ "Modules can be custom promise types, JSON files which enable certain functionality, or reusable CFEngine policy. "
1919
+ "The modules you use can be written by the CFEngine team, others in the community, your colleagues, or yourself."
2020
)
21-
body = (
22-
str(manpage)
23-
+ """
21+
body = str(manpage) + """
2422
.br
2523
Binary packages may be downloaded from https://cfengine.com/download/.
2624
.br
@@ -40,7 +38,6 @@ def generate_man_page():
4038
.SH AUTHOR
4139
Northern.tech AS
4240
"""
43-
)
4441
with open(os.path.dirname(__file__) + "/cfbs.1", "w", encoding="utf-8") as man_file:
4542
man_file.write(body)
4643
return body

cfbs/validate.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,10 @@ def validate_module_name_content(name):
227227
r = "[a-z][a-z0-9]*(-[a-z0-9]+)*"
228228
proper_name = name
229229

230+
if is_module_local(name) or is_module_absolute(name):
231+
# If one of these are true, the other must be false
232+
assert is_module_absolute(name) != is_module_local(name)
233+
230234
if is_module_local(name):
231235
if not name.startswith("./"):
232236
raise CFBSValidationError(name, "Local module names should begin with `./`")
@@ -270,10 +274,18 @@ def validate_module_name_content(name):
270274
"Module name proper is empty",
271275
)
272276

277+
if proper_name[0] not in "abcdefghijklmnopqrstuvwxyz":
278+
raise CFBSValidationError(
279+
name,
280+
"Module name must start with a lowercase ASCII letter ('{}' starts with '{}')".format(
281+
proper_name, proper_name[0]
282+
),
283+
)
284+
273285
if not re.fullmatch(r, proper_name):
274286
raise CFBSValidationError(
275287
name,
276-
"Module name contains illegal characters (only lowercase ASCII alphanumeric characters are legal)",
288+
"Module name contains illegal characters (only lowercase ASCII alphanumeric characters and single dash separators are allowed)",
277289
)
278290

279291
log.debug("Successfully validated name of module %s" % name)

tests/test_pretty.py

Lines changed: 16 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -220,15 +220,10 @@ def test_pretty_file():
220220
def test_pretty_check_string():
221221
assert pretty_check_string(' "Hello" ') is False
222222
assert pretty_check_string('"Hello"') is True
223-
assert (
224-
pretty_check_string(
225-
"""{
223+
assert pretty_check_string("""{
226224
"name": "lars",
227225
"age": 27
228-
}"""
229-
)
230-
is True
231-
)
226+
}""") is True
232227
assert pretty_check_string('{ "name": "lars", "age": 27 }') is False
233228

234229

@@ -242,29 +237,20 @@ def test_pretty_sorting_simple_top_level():
242237
),
243238
}
244239
assert pretty_string("""{}""", lex_sorting) == """{}"""
245-
assert (
246-
pretty_string("""{"a":1}""", lex_sorting)
247-
== """{
240+
assert pretty_string("""{"a":1}""", lex_sorting) == """{
248241
"a": 1
249242
}"""
250-
)
251243

252-
assert (
253-
pretty_string("""{"b":2,"a":1}""", lex_sorting)
254-
== """{
244+
assert pretty_string("""{"b":2,"a":1}""", lex_sorting) == """{
255245
"a": 1,
256246
"b": 2
257247
}"""
258-
)
259248

260-
assert (
261-
pretty_string("""{"b":2,"a":1,"c":3}""", lex_sorting)
262-
== """{
249+
assert pretty_string("""{"b":2,"a":1,"c":3}""", lex_sorting) == """{
263250
"a": 1,
264251
"b": 2,
265252
"c": 3
266253
}"""
267-
)
268254

269255
length_sorting = {
270256
None: (
@@ -274,29 +260,20 @@ def test_pretty_sorting_simple_top_level():
274260
}
275261

276262
assert pretty_string("""{}""", length_sorting) == """{}"""
277-
assert (
278-
pretty_string("""{"aa":1}""", length_sorting)
279-
== """{
263+
assert pretty_string("""{"aa":1}""", length_sorting) == """{
280264
"aa": 1
281265
}"""
282-
)
283266

284-
assert (
285-
pretty_string("""{"bbb":2,"aa":1}""", length_sorting)
286-
== """{
267+
assert pretty_string("""{"bbb":2,"aa":1}""", length_sorting) == """{
287268
"aa": 1,
288269
"bbb": 2
289270
}"""
290-
)
291271

292-
assert (
293-
pretty_string("""{"bbb":2,"aa":1,"c":3}""", length_sorting)
294-
== """{
272+
assert pretty_string("""{"bbb":2,"aa":1,"c":3}""", length_sorting) == """{
295273
"c": 3,
296274
"aa": 1,
297275
"bbb": 2
298276
}"""
299-
)
300277

301278
integer_sorting = {
302279
None: (
@@ -306,39 +283,27 @@ def test_pretty_sorting_simple_top_level():
306283
}
307284

308285
assert pretty_string("""{}""", integer_sorting) == """{}"""
309-
assert (
310-
pretty_string("""{"a":1}""", integer_sorting)
311-
== """{
286+
assert pretty_string("""{"a":1}""", integer_sorting) == """{
312287
"a": 1
313288
}"""
314-
)
315289

316-
assert (
317-
pretty_string("""{"b":2,"a":1}""", integer_sorting)
318-
== """{
290+
assert pretty_string("""{"b":2,"a":1}""", integer_sorting) == """{
319291
"a": 1,
320292
"b": 2
321293
}"""
322-
)
323294

324-
assert (
325-
pretty_string("""{"b":2,"a":1,"c":3}""", integer_sorting)
326-
== """{
295+
assert pretty_string("""{"b":2,"a":1,"c":3}""", integer_sorting) == """{
327296
"a": 1,
328297
"b": 2,
329298
"c": 3
330299
}"""
331-
)
332300

333-
assert (
334-
pretty_string("""{"b":2,"a":1,"c":3,"z":-1}""", integer_sorting)
335-
== """{
301+
assert pretty_string("""{"b":2,"a":1,"c":3,"z":-1}""", integer_sorting) == """{
336302
"z": -1,
337303
"a": 1,
338304
"b": 2,
339305
"c": 3
340306
}"""
341-
)
342307

343308
specific_sorting = {
344309
None: (
@@ -349,38 +314,26 @@ def test_pretty_sorting_simple_top_level():
349314

350315
assert pretty_string("""{}""", specific_sorting) == """{}"""
351316

352-
assert (
353-
pretty_string("""{"a":1}""", specific_sorting)
354-
== """{
317+
assert pretty_string("""{"a":1}""", specific_sorting) == """{
355318
"a": 1
356319
}"""
357-
)
358320

359-
assert (
360-
pretty_string("""{"b":2,"a":1}""", specific_sorting)
361-
== """{
321+
assert pretty_string("""{"b":2,"a":1}""", specific_sorting) == """{
362322
"b": 2,
363323
"a": 1
364324
}"""
365-
)
366325

367-
assert (
368-
pretty_string("""{"b":2,"a":1,"c":3}""", specific_sorting)
369-
== """{
326+
assert pretty_string("""{"b":2,"a":1,"c":3}""", specific_sorting) == """{
370327
"b": 2,
371328
"a": 1,
372329
"c": 3
373330
}"""
374-
)
375-
assert (
376-
pretty_string("""{"b":2,"a":1,"c":3,"z":-1}""", specific_sorting)
377-
== """{
331+
assert pretty_string("""{"b":2,"a":1,"c":3,"z":-1}""", specific_sorting) == """{
378332
"z": -1,
379333
"b": 2,
380334
"a": 1,
381335
"c": 3
382336
}"""
383-
)
384337

385338

386339
def test_pretty_sorting_array():

0 commit comments

Comments
 (0)