diff --git a/CHANGELOG.md b/CHANGELOG.md index ad95e5c..c365f51 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## ?.?.? - Unreleased + +* More robust dataset/version/edition URI parsing. + ## 4.2.0 - 2024-06-18 * New commands `okdata -e` and `okdata -v` for printing the current environment diff --git a/okdata/cli/commands/datasets/datasets.py b/okdata/cli/commands/datasets/datasets.py index 5327a24..2718bd1 100644 --- a/okdata/cli/commands/datasets/datasets.py +++ b/okdata/cli/commands/datasets/datasets.py @@ -261,9 +261,9 @@ def copy_file(self): ) def _dataset_components_from_uri( - self, dataset_uri, create_edition=False, auto_resolve=True + self, uri, create_edition=False, auto_resolve=True ): - """Return an ID/version/edition tuple given a dataset URI. + """Return a dataset ID/version/edition tuple given a URI. Four different URI formats are supported: @@ -289,8 +289,15 @@ def _dataset_components_from_uri( Otherwise `None` is returned for missing parts. """ - parts = dataset_uri.split("/") - dataset_id, version, edition = parts + [None] * (3 - len(parts)) + parts = uri.split("/") + + try: + dataset_id, version, edition = parts + [None] * (3 - len(parts)) + except ValueError: + sys.exit( + "URI must be on the format 'dataset_id', " + "'dataset_id/version', or 'dataset_id/version/edition'." + ) # First verify that the dataset exists; `get_dataset` raises an error # if not. diff --git a/tests/origocli/commands/datasets/datasets_test.py b/tests/origocli/commands/datasets/datasets_test.py index fa4ba24..9158e35 100644 --- a/tests/origocli/commands/datasets/datasets_test.py +++ b/tests/origocli/commands/datasets/datasets_test.py @@ -108,6 +108,11 @@ def test_edition(self, mocker, output): cmd.handler() assert output_with_argument(output, [edition]) + def test_invalid_uri(self, mocker, output): + cmd = create_cmd(mocker, "ls", "a/b/c/d/e") + with pytest.raises(SystemExit): + cmd.handler() + class TestDatasetsCp: def test_copy_local_files(self, mocker):