Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a default fallback naming scheme for image resources with variants #989

Merged
merged 2 commits into from
Dec 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/989.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Resources that require variants will now use the variant name as part of the filename by default.
16 changes: 10 additions & 6 deletions src/briefcase/commands/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -607,11 +607,13 @@ def install_image(self, role, variant, size, source, target):
full_role = role
else:
try:
source_filename = f"{source[variant]}{target.suffix}"
full_role = f"{variant} {role}"
except (TypeError, KeyError):
source_filename = f"{source[variant]}{target.suffix}"
except TypeError:
source_filename = f"{source}-{variant}{target.suffix}"
except KeyError:
self.logger.info(
f"Unable to find {variant} variant for {role}; using default"
f"Unknown variant {variant!r} for {role}; using default"
)
return
else:
Expand All @@ -631,11 +633,13 @@ def install_image(self, role, variant, size, source, target):
full_role = f"{size}px {role}"
else:
try:
source_filename = f"{source[variant]}-{size}{target.suffix}"
full_role = f"{size}px {variant} {role}"
except (TypeError, KeyError):
source_filename = f"{source[variant]}-{size}{target.suffix}"
except TypeError:
source_filename = f"{source}-{variant}-{size}{target.suffix}"
except KeyError:
self.logger.info(
f"Unable to find {size}px {variant} variant for {role}; using default"
f"Unknown variant {variant!r} for {size}px {role}; using default"
)
return

Expand Down
41 changes: 36 additions & 5 deletions tests/commands/create/test_install_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,8 @@ def test_variant_with_no_requested_size(create_command, tmp_path, capsys):
def test_variant_without_variant_source_and_no_requested_size(
create_command, tmp_path, capsys
):
"""If the template specifies a variant with no size, but app doesn't have
variants, a message is reported."""
"""If the template specifies a variant with no size, but app doesn't define
variants, the default construction is used."""
create_command.tools.shutil = mock.MagicMock(spec_set=shutil)

# Create the source image
Expand All @@ -208,7 +208,7 @@ def test_variant_without_variant_source_and_no_requested_size(
)

# The right message was written to output
expected = "Unable to find round variant for sample image; using default\n"
expected = "Unable to find input/original-round.png for round sample image; using default\n"
assert capsys.readouterr().out == expected

# No file was installed.
Expand Down Expand Up @@ -238,7 +238,7 @@ def test_unknown_variant_with_no_requested_size(create_command, tmp_path, capsys
)

# The right message was written to output
expected = "Unable to find unknown variant for sample image; using default\n"
expected = "Unknown variant 'unknown' for sample image; using default\n"
assert capsys.readouterr().out == expected

# No file was installed.
Expand Down Expand Up @@ -303,7 +303,38 @@ def test_variant_with_size_without_variants(create_command, tmp_path, capsys):
)

# The right message was written to output
expected = "Unable to find 3742px round variant for sample image; using default\n"
expected = "Unable to find input/original-round-3742.png for 3742px round sample image; using default\n"
assert capsys.readouterr().out == expected

# No file was installed.
create_command.tools.shutil.copy.assert_not_called()


def test_unknown_variant_with_size(create_command, tmp_path, capsys):
"""If the app specifies an unknown variant with a size, but no variants are
specified, a message is output."""
create_command.tools.shutil = mock.MagicMock(spec_set=shutil)

# Create the source image
source_file = tmp_path / "project" / "input" / "original-3742.png"
source_file.parent.mkdir(parents=True, exist_ok=True)
with source_file.open("w") as f:
f.write("image")

# Try to install the image
out_path = tmp_path / "output.png"
create_command.install_image(
"sample image",
source={
"round": "input/original",
},
variant="unknown",
size="3742",
target=out_path,
)

# The right message was written to output
expected = "Unknown variant 'unknown' for 3742px sample image; using default\n"
assert capsys.readouterr().out == expected

# No file was installed.
Expand Down