Skip to content

Commit

Permalink
config: add alias check to submodules (bug 1933580) (#156)
Browse files Browse the repository at this point in the history
  • Loading branch information
zzzeid authored Nov 27, 2024
1 parent 6a378ff commit 845ca7e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/mots/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,15 @@ def calculate_hashes(config: dict, export: bytes) -> tuple[dict, dict]:
return original_hashes, hashes


def raise_if_nick_is_invalid(person):
"""Check if provided person dictionary is valid."""
if "bmo_id" not in person and person.get("nick") not in ALLOWED_NICK_ONLY:
raise ValueError(
f"Nick must be one of {', '.join(ALLOWED_NICK_ONLY)} when "
"provided without bmo_id."
)


def clean(file_config: FileConfig, write: bool = True, refresh: bool = True):
"""Clean and re-sort configuration file.
Expand Down Expand Up @@ -250,11 +259,7 @@ def clean(file_config: FileConfig, write: bool = True, refresh: bool = True):
continue
for i, person in enumerate(module[key]):
if "bmo_id" not in person:
if person.get("nick") not in ALLOWED_NICK_ONLY:
raise ValueError(
f"Nick must be one of {', '.join(ALLOWED_NICK_ONLY)} when "
"provided without bmo_id."
)
raise_if_nick_is_invalid(person)
continue
reference_anchor_for_module(
i, person, key, file_config, directory, module
Expand Down Expand Up @@ -289,6 +294,9 @@ def clean(file_config: FileConfig, write: bool = True, refresh: bool = True):
if key not in submodule or not submodule[key]:
continue
for i, person in enumerate(submodule[key]):
if "bmo_id" not in person:
raise_if_nick_is_invalid(person)
continue
reference_anchor_for_module(
i, person, key, file_config, directory, submodule
)
Expand Down
19 changes: 19 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,9 @@ def test_clean_added_user_no_bmo_id_with_refresh(

assert file_config.config["people"] == config["people"]
file_config.config["modules"][0]["owners"].append({"nick": "TLMC"})
file_config.config["modules"][0]["submodules"][0]["owners"].append(
{"nick": "TLMC"}
)
file_config.write()

assert len(file_config.config["people"]) == 4
Expand Down Expand Up @@ -345,6 +348,8 @@ def test_clean_added_user_no_bmo_id_with_refresh(
assert modules[0]["owners"][1] == {"nick": "TLMC"}
assert modules[1]["owners"][0] == people[2]

assert modules[0]["submodules"][0]["owners"][1] == {"nick": "TLMC"}


@mock.patch("mots.config.get_bmo_data")
def test_clean_added_user_no_bmo_id_with_refresh_invalid_nick(
Expand All @@ -362,3 +367,17 @@ def test_clean_added_user_no_bmo_id_with_refresh_invalid_nick(
# Run clean with refresh.
with pytest.raises(ValueError):
clean(file_config, refresh=True)

file_config.config["modules"][0]["owners"].pop()
file_config.write()

clean(file_config, refresh=True)

file_config.config["modules"][0]["submodules"][0]["owners"].append(
{"nick": "ABCD"}
)
file_config.write()

# Run clean with refresh, this time after changing a submodule.
with pytest.raises(ValueError):
clean(file_config, refresh=True)

0 comments on commit 845ca7e

Please sign in to comment.