-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dispatcher): constrained global arguments
Fixes #219
- Loading branch information
Showing
6 changed files
with
140 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Copyright 2024 Canonical Ltd. | ||
# | ||
# This program is free software; you can redistribute it and/or | ||
# modify it under the terms of the GNU Lesser General Public | ||
# License version 3 as published by the Free Software Foundation. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
# Lesser General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Lesser General Public License | ||
# along with this program; if not, write to the Free Software Foundation, | ||
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
"""Utility functions for craft_cli.""" | ||
from typing import Sequence | ||
|
||
|
||
def humanise_list(values: Sequence[str], conjunction: str = "and") -> str: | ||
"""Convert a collection of values to a string that lists the values. | ||
:param values: The values to turn into a list | ||
:param conjunction: The conjunction to use between the last two items | ||
:returns: A string containing the list phrase ready to insert into a sentence. | ||
""" | ||
if not values: | ||
return "" | ||
start = ", ".join(values[:-1]) | ||
return f"{start} {conjunction} {values[-1]}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,6 +33,7 @@ RTD | |
subdirectories | ||
subtree | ||
subfolders | ||
utils | ||
UI | ||
VM | ||
YAML |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# Copyright 2024 Canonical Ltd. | ||
# | ||
# This program is free software; you can redistribute it and/or | ||
# modify it under the terms of the GNU Lesser General Public | ||
# License version 3 as published by the Free Software Foundation. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
# Lesser General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Lesser General Public License | ||
# along with this program; if not, write to the Free Software Foundation, | ||
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
"""Unit tests for utility functions.""" | ||
import re | ||
|
||
import pytest | ||
import pytest_check | ||
from hypothesis import given, strategies | ||
|
||
from craft_cli import utils | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"values", | ||
[ | ||
[], | ||
["one-thing"], | ||
["two", "things"], | ||
], | ||
) | ||
@pytest.mark.parametrize("conjunction", ["and", "or", "but not"]) | ||
def test_humanise_list_success(values, conjunction): | ||
actual = utils.humanise_list(values, conjunction) | ||
|
||
pytest_check.equal(actual.count(","), max((len(values) - 2, 0))) | ||
with pytest_check.check: | ||
assert actual == "" or conjunction in actual | ||
for value in values: | ||
pytest_check.is_in(value, actual) | ||
|
||
|
||
@given( | ||
values=strategies.lists(strategies.text()), | ||
conjunction=strategies.text(), | ||
) | ||
def test_humanise_list_fuzzy(values, conjunction): | ||
actual = utils.humanise_list(values, conjunction) | ||
|
||
pytest_check.greater_equal(actual.count(","), max((len(values) - 2, 0))) | ||
with pytest_check.check: | ||
assert actual == "" or conjunction in actual | ||
for value in values: | ||
pytest_check.is_in(value, actual) |