Skip to content

Commit ce533d3

Browse files
authored
Merge pull request #283 from jakub-nt/CFE-4592
CFE-4592: Fix local modules in subdirectories failing validation
2 parents d6838f5 + 3cddc78 commit ce533d3

4 files changed

Lines changed: 44 additions & 8 deletions

File tree

cfbs/module.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ def is_module_added_manually(added_by: str):
88

99

1010
def is_module_local(name: str):
11-
# a module might contain `"local"` in its `"tags"` but this is not required
12-
# the source of truth for whether the module is local is whether it starts with `./`
11+
"""A module might contain `"local"` in its `"tags"` but this is not required.
12+
The source of truth for whether the module is local is whether it starts with `./`.
13+
"""
1314
return name.startswith("./")
1415

1516

cfbs/utils.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import urllib.error
1111
from collections import OrderedDict
1212
from shutil import rmtree
13-
from typing import Union
13+
from typing import Iterable, Union
1414

1515
from cfbs.pretty import pretty
1616

@@ -180,29 +180,29 @@ def item_index(iterable, item, extra_at_end=True):
180180
return -1
181181

182182

183-
def strip_right(string, ending):
183+
def strip_right(string: str, ending) -> str:
184184
# can be replaced with str.removesuffix from Python 3.9 onwards
185185
if not string.endswith(ending):
186186
return string
187187
return string[0 : -len(ending)]
188188

189189

190-
def strip_left(string, beginning):
190+
def strip_left(string: str, beginning) -> str:
191191
# can be replaced with str.removeprefix from Python 3.9 onwards
192192
if not string.startswith(beginning):
193193
return string
194194
return string[len(beginning) :]
195195

196196

197-
def strip_right_any(string, suffixes):
197+
def strip_right_any(string: str, suffixes: Iterable[str]) -> str:
198198
for suffix in suffixes:
199199
if string.endswith(suffix):
200200
return string[0 : -len(suffix)]
201201

202202
return string
203203

204204

205-
def strip_left_any(string, prefixes):
205+
def strip_left_any(string: str, prefixes: Iterable[str]) -> str:
206206
for prefix in prefixes:
207207
if string.startswith(prefix):
208208
return string[len(prefix) :]

cfbs/validate.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,9 @@ def validate_module_name_content(name):
196196
proper_name = strip_left(proper_name, "./")
197197
proper_name = strip_right_any(proper_name, ("/", ".cf", ".json"))
198198

199+
# only validate the local module's name, not the entire path to it
200+
proper_name = proper_name.split("/")[-1]
201+
199202
# allow underscores, only for local modules
200203
proper_name = proper_name.replace("_", "-")
201204

@@ -205,7 +208,7 @@ def validate_module_name_content(name):
205208
"Module name contains illegal characters (only lowercase ASCII alphanumeric characters are legal)",
206209
)
207210

208-
log.debug("Validated name of module %s" % name)
211+
log.debug("Successfully validated name of module %s" % name)
209212

210213

211214
def validate_config_raise_exceptions(config, empty_build_list_ok=False):

tests/test_validate.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import pytest
2+
3+
from cfbs.utils import CFBSValidationError
4+
from cfbs.validate import validate_module_name_content
5+
6+
7+
def test_validate_module_name_content():
8+
validate_module_name_content("regular-name")
9+
with pytest.raises(CFBSValidationError):
10+
validate_module_name_content("Uppercase-name")
11+
with pytest.raises(CFBSValidationError):
12+
validate_module_name_content("underscore_but_not_local")
13+
with pytest.raises(CFBSValidationError):
14+
validate_module_name_content("name with spaces")
15+
with pytest.raises(CFBSValidationError):
16+
validate_module_name_content("-leading-hyphen")
17+
with pytest.raises(CFBSValidationError):
18+
validate_module_name_content(
19+
"module-name-too-longggggggggggggggggggggggggggggggggggggggggggggg"
20+
)
21+
22+
validate_module_name_content("./local_module.cf")
23+
validate_module_name_content("./local_module_directory/")
24+
with pytest.raises(CFBSValidationError):
25+
validate_module_name_content("not_local_module.cf")
26+
with pytest.raises(CFBSValidationError):
27+
validate_module_name_content("./_leading_underscore/")
28+
validate_module_name_content("./good-extension.json")
29+
with pytest.raises(CFBSValidationError):
30+
validate_module_name_content("./bad-extension.zip")
31+
32+
validate_module_name_content("./123 Illeg@l!/legal-name.cf")

0 commit comments

Comments
 (0)