-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(models/project): make no parts error explicit (#2073)
- Loading branch information
Showing
12 changed files
with
171 additions
and
50 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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
# Copyright 2023 Canonical Ltd. | ||
# Copyright 2023,2025 Canonical Ltd. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
|
@@ -49,7 +49,7 @@ def fake_extensions(stub_extensions): | |
@pytest.mark.parametrize( | ||
("charmcraft_yaml", "expected"), | ||
[ | ||
( | ||
pytest.param( | ||
dedent( | ||
f""" | ||
name: test-charm-name | ||
|
@@ -60,6 +60,9 @@ def fake_extensions(stub_extensions): | |
base: [email protected] | ||
platforms: | ||
amd64: | ||
parts: | ||
my-part: | ||
plugin: nil | ||
""" | ||
), | ||
dedent( | ||
|
@@ -70,13 +73,52 @@ def fake_extensions(stub_extensions): | |
base: [email protected] | ||
platforms: | ||
amd64: null | ||
parts: {} | ||
parts: | ||
my-part: | ||
plugin: nil | ||
type: charm | ||
terms: | ||
- https://example.com/test | ||
""" | ||
), | ||
) | ||
id="platforms", | ||
), | ||
pytest.param( | ||
dedent( | ||
f""" | ||
name: test-charm-name | ||
type: charm | ||
summary: test-summary | ||
description: test-description | ||
extensions: [{TestExtension.name}] | ||
bases: | ||
- name: ubuntu | ||
channel: "22.04" | ||
""" | ||
), | ||
dedent( | ||
"""\ | ||
name: test-charm-name | ||
summary: test-summary | ||
description: test-description | ||
parts: | ||
charm: | ||
plugin: charm | ||
source: . | ||
type: charm | ||
terms: | ||
- https://example.com/test | ||
bases: | ||
- build-on: | ||
- name: ubuntu | ||
channel: '22.04' | ||
run-on: | ||
- name: ubuntu | ||
channel: '22.04' | ||
""" | ||
), | ||
id="bases", | ||
), | ||
], | ||
) | ||
def test_expand_extensions_simple( | ||
|
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
# Copyright 2024 Canonical Ltd. | ||
# Copyright 2024-2025 Canonical Ltd. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
|
@@ -97,7 +97,7 @@ def test_build_basic_bundle(monkeypatch, capsys, app, new_path, bundle_yaml, fil | |
"build-for": ["amd64"], | ||
} | ||
}, | ||
"parts": {}, | ||
"parts": {"my-part": {"plugin": "nil"}}, | ||
}, | ||
"ubuntu-22.04-amd64", | ||
marks=pytest.mark.skipif( | ||
|
@@ -114,7 +114,7 @@ def test_build_basic_bundle(monkeypatch, capsys, app, new_path, bundle_yaml, fil | |
"description": "A charm for testing", | ||
"base": "[email protected]", | ||
"platforms": {util.get_host_architecture(): None}, | ||
"parts": {}, | ||
"parts": {"my-part": {"plugin": "nil"}}, | ||
}, | ||
util.get_host_architecture(), | ||
marks=pytest.mark.skipif( | ||
|
@@ -132,7 +132,7 @@ def test_build_basic_bundle(monkeypatch, capsys, app, new_path, bundle_yaml, fil | |
"base": "[email protected]", | ||
"build-base": "ubuntu@devel", | ||
"platforms": {util.get_host_architecture(): None}, | ||
"parts": {}, | ||
"parts": {"my-part": {"plugin": "nil"}}, | ||
}, | ||
util.get_host_architecture(), | ||
marks=pytest.mark.skipif( | ||
|
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 |
---|---|---|
|
@@ -10,6 +10,7 @@ platforms: | |
parts: | ||
charm: | ||
plugin: charm | ||
source: . | ||
|
||
actions: | ||
pause: | ||
|
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
12 changes: 12 additions & 0 deletions
12
tests/unit/models/invalid_charms_yaml/platforms-empty-parts.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
type: charm | ||
name: basic-charm | ||
summary: The most basic valid charm | ||
description: | | ||
The most basic possible valid charmcraft.yaml that doesn't need other files and gets returned to its own value. | ||
Note that this means we cannot use short-form bases here because this charm is meant to be rewritable. | ||
base: [email protected] | ||
platforms: | ||
amd64: | ||
build-on: [amd64] | ||
build-for: [amd64] | ||
parts: {} |
11 changes: 11 additions & 0 deletions
11
tests/unit/models/invalid_charms_yaml/platforms-no-parts.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
type: charm | ||
name: basic-charm | ||
summary: The most basic valid charm | ||
description: | | ||
The most basic possible valid charmcraft.yaml that doesn't need other files and gets returned to its own value. | ||
Note that this means we cannot use short-form bases here because this charm is meant to be rewritable. | ||
base: [email protected] | ||
platforms: | ||
amd64: | ||
build-on: [amd64] | ||
build-for: [amd64] |
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
# Copyright 2023 Canonical Ltd. | ||
# Copyright 2023,2025 Canonical Ltd. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
|
@@ -993,6 +993,7 @@ def test_from_yaml_file_exception( | |
"description": "", | ||
"base": "[email protected]", | ||
"platforms": {"amd64": None}, | ||
"parts": {"my-part": {"plugin": "nil"}}, | ||
"charmhub": {"api_url": "http://charmhub.io"}, | ||
}, | ||
), | ||
|
@@ -1127,19 +1128,16 @@ def test_read_charm_from_yaml_file_self_contained_success(tmp_path, filename: st | |
- field 'name' required in top-level configuration | ||
- field 'summary' required in top-level configuration | ||
- field 'description' required in top-level configuration | ||
- field 'bases' required in top-level configuration""" | ||
- field 'platforms' required in top-level configuration | ||
- field 'parts' required in top-level configuration""" | ||
), | ||
), | ||
( | ||
"invalid-type.yaml", | ||
dedent( | ||
"""\ | ||
Bad invalid-type.yaml content: | ||
- field 'name' required in top-level configuration | ||
- field 'summary' required in top-level configuration | ||
- field 'description' required in top-level configuration | ||
- input should be 'charm' (in field 'type') | ||
- field 'bases' required in top-level configuration""" | ||
Bad charmcraft.yaml content: | ||
- field type cannot be 'invalid'""" | ||
), | ||
), | ||
( | ||
|
@@ -1151,13 +1149,31 @@ def test_read_charm_from_yaml_file_self_contained_success(tmp_path, filename: st | |
- base requires 'platforms' definition: {'name': 'ubuntu', 'channel': 'devel'} (in field 'bases[1]')""" | ||
), | ||
), | ||
pytest.param( | ||
"platforms-no-parts.yaml", | ||
dedent( | ||
"""\ | ||
Bad platforms-no-parts.yaml content: | ||
- field 'parts' required in top-level configuration""" | ||
), | ||
id="no-parts-in-platform-charm", | ||
), | ||
pytest.param( | ||
"platforms-empty-parts.yaml", | ||
dedent( | ||
"""\ | ||
Bad platforms-empty-parts.yaml content: | ||
- dictionary should have at least 1 item after validation, not 0 (in field 'parts')""" | ||
), | ||
id="empty-parts-in-platform-charm", | ||
), | ||
], | ||
) | ||
def test_read_charm_from_yaml_file_error(filename, errors): | ||
file_path = pathlib.Path(__file__).parent / "invalid_charms_yaml" / filename | ||
|
||
with pytest.raises(CraftValidationError) as exc: | ||
_ = project.BasesCharm.from_yaml_file(file_path) | ||
_ = project.CharmcraftProject.from_yaml_file(file_path) | ||
|
||
assert exc.value.args[0] == errors | ||
|
||
|
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 |
---|---|---|
|
@@ -10,4 +10,6 @@ platforms: | |
amd64: | ||
build-on: [amd64] | ||
build-for: [amd64] | ||
parts: {} | ||
parts: | ||
my-part: | ||
plugin: nil |
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
Oops, something went wrong.