Skip to content

Commit

Permalink
tests: update to use new test functions
Browse files Browse the repository at this point in the history
Use new prepare_*_yaml for tests.
  • Loading branch information
syu-w committed Jul 7, 2023
1 parent 6d389a7 commit d4197d1
Show file tree
Hide file tree
Showing 12 changed files with 2,245 additions and 1,144 deletions.
1 change: 1 addition & 0 deletions charmcraft/metafiles/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

def parse_config_yaml(charm_dir: pathlib.Path) -> Optional[JujuConfig]:
"""Parse project's config.yaml.
:param charm_dir: Directory to read config.yaml from.
:returns: a JujuConfig object.
Expand Down
2 changes: 2 additions & 0 deletions charmcraft/models/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class JujuActions(ModelConfigDefaults):
@pydantic.validator("actions")
def validate_actions(cls, actions, values):
"""Verify actions names and descriptions."""
if not isinstance(actions, dict):
raise ValueError("actions.yaml is not a valid actions configuration")
for action in actions:
if keyword.iskeyword(action):
raise ValueError(
Expand Down
52 changes: 45 additions & 7 deletions tests/commands/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -1247,16 +1247,52 @@ def test_build_package_tree_structure(tmp_path, config):
assert zf.read("linkeddir/file_ext") == b"external file" # from file in the outside linked dir


def test_build_package_name(tmp_path, config):
@pytest.mark.parametrize(
"charmcraft_yaml, metadata_yaml, expected_zipname",
[
[
dedent(
"""\
type: charm
"""
),
dedent(
"""\
name: test-charm-name-from-metadata-yaml
summary: test summary
description: test description
"""
),
"test-charm-name-from-metadata-yaml_xname-xchannel-xarch1.charm",
],
[
dedent(
"""\
name: test-charm-name-from-charmcraft-yaml
type: charm
summary: test summary
description: test description
"""
),
None,
"test-charm-name-from-charmcraft-yaml_xname-xchannel-xarch1.charm",
],
],
)
def test_build_package_name(
tmp_path,
prepare_charmcraft_yaml,
prepare_metadata_yaml,
charmcraft_yaml,
metadata_yaml,
expected_zipname,
):
"""The zip file name comes from the config."""
to_be_zipped_dir = tmp_path / BUILD_DIRNAME
to_be_zipped_dir.mkdir()

# the metadata
metadata_data = {"name": "name-from-metadata-yaml"}
metadata_file = tmp_path / "metadata.yaml"
with metadata_file.open("wt", encoding="ascii") as fh:
yaml.dump(metadata_data, fh)
prepare_charmcraft_yaml(charmcraft_yaml)
prepare_metadata_yaml(metadata_yaml)

# zip it
bases_config = BasesConfiguration(
Expand All @@ -1265,10 +1301,12 @@ def test_build_package_name(tmp_path, config):
"run-on": [Base(name="xname", channel="xchannel", architectures=["xarch1"])],
}
)

config = load(tmp_path)
builder = get_builder(config)
zipname = builder.handle_package(to_be_zipped_dir, bases_config)

assert zipname == "name-from-metadata-yaml_xname-xchannel-xarch1.charm"
assert zipname == expected_zipname


@pytest.mark.parametrize(
Expand Down
230 changes: 184 additions & 46 deletions tests/commands/test_pack.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,45 @@ def test_bundle_shell_after(tmp_path, bundle_yaml, bundle_config, mock_parts, mo


@pytest.mark.skipif(sys.platform == "win32", reason="Windows not [yet] supported")
def test_bundle_parts_not_defined(tmp_path, monkeypatch, bundle_yaml):
@pytest.mark.parametrize(
"charmcraft_yaml, metadata_yaml",
[
[
dedent(
"""\
type: bundle
"""
),
dedent(
"""\
name: test-charm-name-from-metadata-yaml
summary: test summary
description: test description
"""
),
],
[
dedent(
"""\
name: test-charm-name-from-charmcraft-yaml
type: bundle
summary: test-summary
description: test-description
"""
),
None,
],
],
)
def test_bundle_parts_not_defined(
tmp_path,
monkeypatch,
bundle_yaml,
prepare_charmcraft_yaml,
prepare_metadata_yaml,
charmcraft_yaml,
metadata_yaml,
):
"""Parts are not defined.
When the "parts" section does not exist, create an implicit "bundle" part and
Expand All @@ -393,14 +431,8 @@ def test_bundle_parts_not_defined(tmp_path, monkeypatch, bundle_yaml):
bundle_yaml(name="testbundle")
(tmp_path / "README.md").write_text("test readme")

charmcraft_file = tmp_path / "charmcraft.yaml"
charmcraft_file.write_text(
dedent(
"""
type: bundle
"""
)
)
prepare_charmcraft_yaml(charmcraft_yaml)
prepare_metadata_yaml(metadata_yaml)

config = load(tmp_path)

Expand Down Expand Up @@ -432,7 +464,53 @@ def test_bundle_parts_not_defined(tmp_path, monkeypatch, bundle_yaml):


@pytest.mark.skipif(sys.platform == "win32", reason="Windows not [yet] supported")
def test_bundle_parts_with_bundle_part(tmp_path, monkeypatch, bundle_yaml):
@pytest.mark.parametrize(
"charmcraft_yaml, metadata_yaml",
[
[
dedent(
"""\
type: bundle
parts:
bundle:
prime:
- my_extra_file.txt
"""
),
dedent(
"""\
name: test-charm-name-from-metadata-yaml
summary: test summary
description: test description
"""
),
],
[
dedent(
"""\
name: test-charm-name-from-charmcraft-yaml
type: bundle
summary: test-summary
description: test-description
parts:
bundle:
prime:
- my_extra_file.txt
"""
),
None,
],
],
)
def test_bundle_parts_with_bundle_part(
tmp_path,
monkeypatch,
bundle_yaml,
prepare_charmcraft_yaml,
prepare_metadata_yaml,
charmcraft_yaml,
metadata_yaml,
):
"""Parts are declared with a charm part with implicit plugin.
When the "parts" section exists in chamcraft.yaml and a part named "bundle"
Expand All @@ -442,18 +520,8 @@ def test_bundle_parts_with_bundle_part(tmp_path, monkeypatch, bundle_yaml):
bundle_yaml(name="testbundle")
(tmp_path / "README.md").write_text("test readme")

charmcraft_file = tmp_path / "charmcraft.yaml"
charmcraft_file.write_text(
dedent(
"""
type: bundle
parts:
bundle:
prime:
- my_extra_file.txt
"""
)
)
prepare_charmcraft_yaml(charmcraft_yaml)
prepare_metadata_yaml(metadata_yaml)

config = load(tmp_path)

Expand Down Expand Up @@ -486,7 +554,51 @@ def test_bundle_parts_with_bundle_part(tmp_path, monkeypatch, bundle_yaml):


@pytest.mark.skipif(sys.platform == "win32", reason="Windows not [yet] supported")
def test_bundle_parts_without_bundle_part(tmp_path, monkeypatch, bundle_yaml):
@pytest.mark.parametrize(
"charmcraft_yaml, metadata_yaml",
[
[
dedent(
"""\
type: bundle
parts:
foo:
plugin: nil
"""
),
dedent(
"""\
name: test-charm-name-from-metadata-yaml
summary: test summary
description: test description
"""
),
],
[
dedent(
"""\
name: test-charm-name-from-charmcraft-yaml
type: bundle
summary: test-summary
description: test-description
parts:
foo:
plugin: nil
"""
),
None,
],
],
)
def test_bundle_parts_without_bundle_part(
tmp_path,
monkeypatch,
bundle_yaml,
prepare_charmcraft_yaml,
prepare_metadata_yaml,
charmcraft_yaml,
metadata_yaml,
):
"""Parts are declared without a bundle part.
When the "parts" section exists in chamcraft.yaml and a part named "bundle"
Expand All @@ -495,17 +607,8 @@ def test_bundle_parts_without_bundle_part(tmp_path, monkeypatch, bundle_yaml):
bundle_yaml(name="testbundle")
(tmp_path / "README.md").write_text("test readme")

charmcraft_file = tmp_path / "charmcraft.yaml"
charmcraft_file.write_text(
dedent(
"""
type: bundle
parts:
foo:
plugin: nil
"""
)
)
prepare_charmcraft_yaml(charmcraft_yaml)
prepare_metadata_yaml(metadata_yaml)

config = load(tmp_path)

Expand All @@ -532,7 +635,51 @@ def test_bundle_parts_without_bundle_part(tmp_path, monkeypatch, bundle_yaml):


@pytest.mark.skipif(sys.platform == "win32", reason="Windows not [yet] supported")
def test_bundle_parts_with_bundle_part_with_plugin(tmp_path, monkeypatch, bundle_yaml):
@pytest.mark.parametrize(
"charmcraft_yaml, metadata_yaml",
[
[
dedent(
"""\
type: bundle
parts:
bundle:
plugin: nil
"""
),
dedent(
"""\
name: test-charm-name-from-metadata-yaml
summary: test summary
description: test description
"""
),
],
[
dedent(
"""\
name: test-charm-name-from-charmcraft-yaml
type: bundle
summary: test-summary
description: test-description
parts:
bundle:
plugin: nil
"""
),
None,
],
],
)
def test_bundle_parts_with_bundle_part_with_plugin(
tmp_path,
monkeypatch,
bundle_yaml,
prepare_charmcraft_yaml,
prepare_metadata_yaml,
charmcraft_yaml,
metadata_yaml,
):
"""Parts are declared with a bundle part that uses a different plugin.
When the "parts" section exists in chamcraft.yaml and a part named "bundle"
Expand All @@ -542,17 +689,8 @@ def test_bundle_parts_with_bundle_part_with_plugin(tmp_path, monkeypatch, bundle
bundle_yaml(name="testbundle")
(tmp_path / "README.md").write_text("test readme")

charmcraft_file = tmp_path / "charmcraft.yaml"
charmcraft_file.write_text(
dedent(
"""
type: bundle
parts:
bundle:
plugin: nil
"""
)
)
prepare_charmcraft_yaml(charmcraft_yaml)
prepare_metadata_yaml(metadata_yaml)

config = load(tmp_path)

Expand Down
6 changes: 6 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ def set(self, prime=None, **kwargs):
type="charm",
bases=[base],
project=project,
name="test-charm",
summary="test summary",
description="test description",
)


Expand Down Expand Up @@ -107,6 +110,9 @@ def set(self, prime=None, **kwargs):
return TestConfig(
type="bundle",
project=project,
name="test-bundle",
summary="test summary",
description="test description",
)


Expand Down
Loading

0 comments on commit d4197d1

Please sign in to comment.