-
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
There were previously edge cases where a platform charm could be declared but the lack of a `parts` key wouldn't be caught. This now enforces at least one part.
- Loading branch information
Showing
11 changed files
with
167 additions
and
46 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
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
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. | ||
|
@@ -46,6 +46,16 @@ | |
id="empty-charm", | ||
), | ||
pytest.param(BASIC_CHARM.copy(), BASIC_CHARM, id="basic-charm"), | ||
pytest.param( | ||
{"type": "charm", "platforms": {"amd64": None}}, | ||
{"type": "charm", "platforms": {"amd64": None}}, | ||
id="platforms-charm-no-base", | ||
), | ||
pytest.param( | ||
{"type": "charm", "base": "[email protected]"}, | ||
{"type": "charm", "base": "[email protected]"}, | ||
id="platforms-charm-no-platforms", | ||
), | ||
], | ||
) | ||
def test_add_default_parts_correct(yaml_data, expected): | ||
|