| |
@@ -3,7 +3,13 @@
|
| |
from unittest import mock
|
| |
import os
|
| |
|
| |
- from pungi.phases.image_build import ImageBuildPhase, CreateImageBuildThread
|
| |
+ import pytest
|
| |
+
|
| |
+ from pungi.phases.image_build import (
|
| |
+ ImageBuildPhase,
|
| |
+ CreateImageBuildThread,
|
| |
+ find_type_and_format,
|
| |
+ )
|
| |
from tests.helpers import DummyCompose, PungiTestCase, boom
|
| |
|
| |
|
| |
@@ -1098,3 +1104,85 @@
|
| |
with self.assertRaises(RuntimeError):
|
| |
with mock.patch("time.sleep"):
|
| |
t.process((compose, cmd, None), 1)
|
| |
+
|
| |
+
|
| |
+ @pytest.mark.parametrize(
|
| |
+ "path,expected_type,expected_format",
|
| |
+ [
|
| |
+ ("test-image.qcow2", "qcow2", "qcow2"),
|
| |
+ ("test-image.tar.xz", "docker", "tar.xz"),
|
| |
+ ("test-image.vhd", "vpc", "vhd"),
|
| |
+ ("test-image.unknown", None, None),
|
| |
+ # Test edge cases like empty paths, paths with dots, etc.
|
| |
+ ("", None, None),
|
| |
+ (".", None, None),
|
| |
+ ("test-image.", None, None),
|
| |
+ ("test.image.with.dots.qcow2", "qcow2", "qcow2"),
|
| |
+ (".test-image.qcow2.", None, None),
|
| |
+ ("qcow2", "qcow2", "qcow2"),
|
| |
+ ("test-image.qcow2.", None, None),
|
| |
+ (".qcow2", "qcow2", "qcow2"),
|
| |
+ ],
|
| |
+ )
|
| |
+ def test_find_type_and_format_with_global_extensions(
|
| |
+ path, expected_type, expected_format
|
| |
+ ):
|
| |
+ """Test finding type and format using global EXTENSIONS mapping."""
|
| |
+ result = find_type_and_format(path)
|
| |
+ assert result == (expected_type, expected_format)
|
| |
+
|
| |
+
|
| |
+ @pytest.mark.parametrize(
|
| |
+ "path,specific_extensions,expected_type,expected_format",
|
| |
+ [
|
| |
+ (
|
| |
+ "test-image.vhdfixed.xz",
|
| |
+ [("vhd-compressed", ["vhdfixed.xz"], "vhd.xz")],
|
| |
+ "vhd-compressed",
|
| |
+ "vhd.xz",
|
| |
+ ),
|
| |
+ (
|
| |
+ "test-image.multi1",
|
| |
+ [("multi-format", ["multi1", "multi2"], "multi.format")],
|
| |
+ "multi-format",
|
| |
+ "multi.format",
|
| |
+ ),
|
| |
+ (
|
| |
+ "test-image.multi2",
|
| |
+ [("multi-format", ["multi1", "multi2"], "multi.format")],
|
| |
+ "multi-format",
|
| |
+ "multi.format",
|
| |
+ ),
|
| |
+ # Test fallback to global EXTENSIONS when specific extensions don't
|
| |
+ # match.
|
| |
+ (
|
| |
+ "test-image.qcow2",
|
| |
+ [("custom-only", ["custom.only"], "custom.only")],
|
| |
+ "qcow2",
|
| |
+ "qcow2",
|
| |
+ ),
|
| |
+ ("test-image.qcow2", [], "qcow2", "qcow2"),
|
| |
+ ("test-image.qcow2", None, "qcow2", "qcow2"),
|
| |
+ # Test that specific extensions take priority over global ones.
|
| |
+ (
|
| |
+ "test-image.tar.xz",
|
| |
+ [("custom-docker", ["tar.xz"], "custom-tar.xz")],
|
| |
+ "custom-docker",
|
| |
+ "custom-tar.xz",
|
| |
+ ),
|
| |
+ # Test behavior when no extensions match.
|
| |
+ (
|
| |
+ "test-image.unknown",
|
| |
+ [("custom-only", ["custom.only"], "custom.only")],
|
| |
+ None,
|
| |
+ None,
|
| |
+ ),
|
| |
+ ("test-image.nonexistent", [], None, None),
|
| |
+ ],
|
| |
+ )
|
| |
+ def test_find_type_and_format_with_specific_extensions(
|
| |
+ path, specific_extensions, expected_type, expected_format
|
| |
+ ):
|
| |
+ """Test finding type and format with specific extensions parameter."""
|
| |
+ result = find_type_and_format(path, specific_extensions)
|
| |
+ assert result == (expected_type, expected_format)
|
| |
The logic was duplicated in two locations, this patch makes it a common function.
There are a few things I'm not excited about here:
utilsmodule as that would cause circular imports. And a new utils module doesn't seem better than this.unittest.subTest, but that doesn't play great with pytest (unless we install pytest-subtests).CC @supakeen