Skip to content

Commit

Permalink
Fix python version lookup error (#82)
Browse files Browse the repository at this point in the history

Co-authored-by: Lilli Pearson <[email protected]>
  • Loading branch information
evpearson and ezvp4 authored Oct 21, 2021
1 parent 6982d8a commit 2e31e51
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 53 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed
* Bug preventing Python binaries from being looked up
* Bug requiring that python versions be provided in UPPER_SNAKE_CASE (CP38_CP38)

## [0.2.0] - 2021-10-01

Expand Down
2 changes: 1 addition & 1 deletion little_cheesemonger/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
from little_cheesemonger._errors import LittleCheesemongerError # noqa
from little_cheesemonger._run import run # noqa

__version__ = "0.2.1rc1"
__version__ = "0.2.1rc2"
6 changes: 3 additions & 3 deletions little_cheesemonger/_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ def install_python_dependencies(
) # NOTE: cast to list for mypy, fix this
else:
try:
version_keys = [PythonVersion[version] for version in python_versions]
binaries_paths = [all_binaries[version] for version in version_keys]
except KeyError as e:
validated_versions = [PythonVersion(version) for version in python_versions]
binaries_paths = [all_binaries[version] for version in validated_versions]
except (KeyError, ValueError) as e:
raise LittleCheesemongerError(
f"A Python version from specified versions {python_versions} is not installed on this image: {e}"
)
Expand Down
21 changes: 0 additions & 21 deletions tests/constants.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,10 @@
from enum import Enum
from pathlib import Path

PLATFORM = "PLATFORM"
ARCHITECTURE = "ARCHITECTURE"
PYTHON_VERSION = "FOO38"
PYTHON_BINARIES_PATH = Path("foo/bar")
PLATFORM_RAW = f"{PLATFORM}_{ARCHITECTURE}"


class Architecture(str, Enum):
ARCHITECTURE = ARCHITECTURE


class Platform(str, Enum):
PLATFORM = PLATFORM


class PythonVersion(str, Enum):
FOO38 = PYTHON_VERSION


PYTHON_BINARIES = {
Architecture.ARCHITECTURE: {
Platform.PLATFORM: {PythonVersion.FOO38: PYTHON_BINARIES_PATH}
}
}

DIRECTORY = Path(".")
LOADER_IMPORT_PATH = "foo.bar"
LOADER_ARGS = ("foo",)
Expand Down
68 changes: 40 additions & 28 deletions tests/unit/test_run.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import copy
import logging
import subprocess
from unittest.mock import call

import pytest

from little_cheesemonger._constants import PYTHON_BINARIES, Architecture, Platform
from little_cheesemonger._errors import LittleCheesemongerError
from little_cheesemonger._run import (
execute_steps,
Expand All @@ -14,22 +16,17 @@
set_environment_variables,
)
from tests.constants import (
ARCHITECTURE,
CONFIGURATION,
DIRECTORY,
ENVIRONMENT_VARIABLES,
LOADER_ARGS,
LOADER_IMPORT_PATH,
LOADER_KWARGS,
PLATFORM,
PYTHON_BINARIES,
PYTHON_DEPENDENCIES,
PYTHON_VERSION,
PYTHON_VERSIONS,
STEPS,
SYSTEM_DEPENDENCIES,
)
from tests.constants import PythonVersion as PythonVersionTesting

DEBUG = False

Expand Down Expand Up @@ -70,15 +67,10 @@ def load_configuration(mocker):
def get_python_binaries(mocker):
return mocker.patch(
"little_cheesemonger._run.get_python_binaries",
return_value=PYTHON_BINARIES[ARCHITECTURE][PLATFORM],
return_value=PYTHON_BINARIES[Architecture("x86_64")][Platform("manylinux2014")],
)


@pytest.fixture(autouse=True)
def python_versions_enum(mocker):
return mocker.patch("little_cheesemonger._run.PythonVersion", PythonVersionTesting)


@pytest.fixture
def os_mock(mocker):
return mocker.patch("little_cheesemonger._run.os")
Expand Down Expand Up @@ -263,44 +255,64 @@ def test_install_system_dependencies__run_subprocess_called_with_command(
)


def test_install_python_dependencies__python_versions_set__run_subprocess_called_with_command(
def test_install_python_dependencies__succeeds_when_no_python_versions_provided(
run_subprocess_mock, get_python_binaries
):
install_python_dependencies(PYTHON_DEPENDENCIES, None)

run_subprocess_mock.assert_has_calls(
[
call(
[
str(
PYTHON_BINARIES[Architecture("x86_64")][
Platform("manylinux2014")
]["cp37-cp37m"]
/ "pip"
),
"install",
]
+ PYTHON_DEPENDENCIES
)
]
)


install_python_dependencies(PYTHON_DEPENDENCIES, PYTHON_VERSIONS)
def test_install_python_dependencies__succeeds_when_python_versions_are_correctly_specified(
run_subprocess_mock, get_python_binaries
):
install_python_dependencies(PYTHON_DEPENDENCIES, ["cp37-cp37m"])

run_subprocess_mock.assert_called_once_with(
[
str(PYTHON_BINARIES[ARCHITECTURE][PLATFORM][PYTHON_VERSION] / "pip"),
str(
PYTHON_BINARIES[Architecture("x86_64")][Platform("manylinux2014")][
"cp37-cp37m"
]
/ "pip"
),
"install",
]
+ PYTHON_DEPENDENCIES
)


def test_install_python_dependencies__python_versions_invalid__raise_LittleCheesemongerError(
def test_install_python_dependencies__fails_with_uppercase_python_version__raise_LittleCheesemongerError(
run_subprocess_mock, get_python_binaries
):

with pytest.raises(
LittleCheesemongerError, match=r"A Python version from specified versions .*"
):
install_python_dependencies(PYTHON_DEPENDENCIES, ["invalid"])
install_python_dependencies(PYTHON_DEPENDENCIES, ["CP37_CP37M"])


def test_install_python_dependencies__python_versions_not_set__run_subprocess_called_with_command(
def test_install_python_dependencies__fails_with_underscore_in_python_version__raise_LittleCheesemongerError(
run_subprocess_mock, get_python_binaries
):

install_python_dependencies(PYTHON_DEPENDENCIES, None)

run_subprocess_mock.assert_called_once_with(
[
str(PYTHON_BINARIES[ARCHITECTURE][PLATFORM][PYTHON_VERSION] / "pip"),
"install",
]
+ PYTHON_DEPENDENCIES
)
with pytest.raises(
LittleCheesemongerError, match=r"A Python version from specified versions .*"
):
install_python_dependencies(PYTHON_DEPENDENCIES, ["cp37_cp37m"])


def test_execute_steps__run_subprocess_called_with_commands(run_subprocess_mock):
Expand Down

0 comments on commit 2e31e51

Please sign in to comment.