Skip to content

Commit

Permalink
Make ensure_readiness async
Browse files Browse the repository at this point in the history
  • Loading branch information
renan-r-santos committed Dec 21, 2024
1 parent 4fe982d commit 2541a6d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 30 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ strict = true
[tool.pytest.ini_options]
addopts = ["--strict-config", "--strict-markers"]
asyncio_default_fixture_loop_scope = "function"
asyncio_mode = "auto"
filterwarnings = [
"ignore:Jupyter is migrating its paths to use standard platformdirs",
]
Expand Down
2 changes: 1 addition & 1 deletion src/pixi_kernel/pixi.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Project(BaseModel):
manifest_path: str


def ensure_readiness(
async def ensure_readiness(
*,
cwd: Path,
env: dict[str, str],
Expand Down
2 changes: 1 addition & 1 deletion src/pixi_kernel/provisioner.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ async def pre_launch(self, **kwargs: Any) -> dict[str, Any]:
logger.info(f"The current working directory is {cwd}")

env: dict[str, str] = kwargs.get("env", os.environ)
pixi_environment = ensure_readiness(
pixi_environment = await ensure_readiness(
cwd=cwd.resolve(),
env=env,
required_package=required_package,
Expand Down
56 changes: 28 additions & 28 deletions tests/unit/test_pixi.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,67 +20,67 @@


@pytest.mark.usefixtures("_patch_path")
def test_pixi_not_installed():
async def test_pixi_not_installed():
message = re.escape(PIXI_NOT_FOUND.format(kernel_name="Pixi"))
with pytest.raises(RuntimeError, match=message):
ensure_readiness(
await ensure_readiness(
cwd=Path.cwd(), env=os.environ.copy(), required_package="pixi", kernel_name="Pixi"
)


@pytest.mark.usefixtures("_patch_pixi_version_exit_code")
def test_pixi_version_bad_exit_code():
async def test_pixi_version_bad_exit_code():
message = re.escape(PIXI_VERSION_ERROR.format(kernel_name="Pixi"))
with pytest.raises(RuntimeError, match=message):
ensure_readiness(
await ensure_readiness(
cwd=Path.cwd(), env=os.environ.copy(), required_package="pixi", kernel_name="Pixi"
)


@pytest.mark.usefixtures("_patch_pixi_version_stdout")
def test_pixi_version_bad_stdout():
async def test_pixi_version_bad_stdout():
message = re.escape(PIXI_VERSION_ERROR.format(kernel_name="Pixi"))
with pytest.raises(RuntimeError, match=message):
ensure_readiness(
await ensure_readiness(
cwd=Path.cwd(), env=os.environ.copy(), required_package="pixi", kernel_name="Pixi"
)


@pytest.mark.usefixtures("_patch_pixi_version")
def test_outdated_pixi():
async def test_outdated_pixi():
message = re.escape(
PIXI_OUTDATED.format(
kernel_name="Pixi",
minimum_version=MINIMUM_PIXI_VERSION,
)
)
with pytest.raises(RuntimeError, match=message):
ensure_readiness(
await ensure_readiness(
cwd=Path.cwd(), env=os.environ.copy(), required_package="pixi", kernel_name="Pixi"
)


@pytest.mark.usefixtures("_patch_pixi_info_exit_code")
def test_pixi_info_bad_exit_code():
async def test_pixi_info_bad_exit_code():
message = re.escape("Failed to run 'pixi info': error")
with pytest.raises(RuntimeError, match=message):
ensure_readiness(
await ensure_readiness(
cwd=Path.cwd(), env=os.environ.copy(), required_package="pixi", kernel_name="Pixi"
)


@pytest.mark.usefixtures("_patch_pixi_info_stdout")
def test_pixi_info_bad_stdout():
async def test_pixi_info_bad_stdout():
message = re.escape(
("Failed to parse 'pixi info' output: not JSON\n1 validation error for PixiInfo")
)
with pytest.raises(RuntimeError, match=message):
ensure_readiness(
await ensure_readiness(
cwd=Path.cwd(), env=os.environ.copy(), required_package="pixi", kernel_name="Pixi"
)


def test_empty_project():
async def test_empty_project():
cwd = Path("/")
assert not (cwd / "pixi.toml").exists(), "You should not have a pixi.toml file in /"
assert not (cwd / "pyproject.toml").exists(), "You should not have a pyproject.toml file in /"
Expand All @@ -89,30 +89,30 @@ def test_empty_project():
"could not find pixi.toml or pyproject.toml which is configured to use pixi"
)
with pytest.raises(RuntimeError, match=message):
ensure_readiness(
await ensure_readiness(
cwd=cwd, env=os.environ.copy(), required_package="pixi", kernel_name="Pixi"
)


def test_bad_pixi_toml():
async def test_bad_pixi_toml():
cwd = data_dir / "bad_pixi_toml"
message = re.escape("failed to parse project manifest") + "|" + re.escape("unknown field")
with pytest.raises(RuntimeError, match=message):
ensure_readiness(
await ensure_readiness(
cwd=cwd, env=os.environ.copy(), required_package="pixi", kernel_name="Pixi"
)


@pytest.mark.usefixtures("_patch_pixi_info_no_default_env")
def test_missing_default_environment():
async def test_missing_default_environment():
message = re.escape("Default Pixi environment not found.")
with pytest.raises(RuntimeError, match=message):
ensure_readiness(
await ensure_readiness(
cwd=Path.cwd(), env=os.environ.copy(), required_package="pixi", kernel_name="Pixi"
)


def test_missing_ipykernel():
async def test_missing_ipykernel():
cwd = data_dir / "missing_ipykernel"
required_package = "ipykernel"
kernel_name = "Python (Pixi)"
Expand All @@ -125,35 +125,35 @@ def test_missing_ipykernel():
)
)
with pytest.raises(RuntimeError, match=message):
ensure_readiness(
await ensure_readiness(
cwd=cwd,
env=os.environ.copy(),
required_package=required_package,
kernel_name=kernel_name,
)


def test_non_existing_dependency():
async def test_non_existing_dependency():
cwd = data_dir / "non_existing_dependency"
required_package = "ipykernel"
kernel_name = "Python (Pixi)"

message = re.escape("Failed to run 'pixi install':")
with pytest.raises(RuntimeError, match=message):
ensure_readiness(
await ensure_readiness(
cwd=cwd,
env=os.environ.copy(),
required_package=required_package,
kernel_name=kernel_name,
)


def test_pixi_project():
async def test_pixi_project():
cwd = data_dir / "pixi_project"
required_package = "ipykernel"
kernel_name = "Python (Pixi)"

environment = ensure_readiness(
environment = await ensure_readiness(
cwd=cwd,
env=os.environ.copy(),
required_package=required_package,
Expand All @@ -162,12 +162,12 @@ def test_pixi_project():
assert Path(environment.prefix).parts[-2:] == ("envs", "default")


def test_pyproject_project():
async def test_pyproject_project():
cwd = data_dir / "pyproject_project"
required_package = "ipykernel"
kernel_name = "Python (Pixi)"

environment = ensure_readiness(
environment = await ensure_readiness(
cwd=cwd,
env=os.environ.copy(),
required_package=required_package,
Expand Down Expand Up @@ -202,12 +202,12 @@ def env_for_pixi_in_pixi():

# https://github.com/renan-r-santos/pixi-kernel/issues/35
@pytest.mark.skipif(sys.platform == "win32", reason="No need to write Windows-specific code here")
def test_pixi_in_pixi(env_for_pixi_in_pixi: dict[str, str]):
async def test_pixi_in_pixi(env_for_pixi_in_pixi: dict[str, str]):
cwd = data_dir / "pixi_in_pixi" / "good_project"
required_package = "ipykernel"
kernel_name = "Python (Pixi)"

environment = ensure_readiness(
environment = await ensure_readiness(
cwd=cwd,
env=env_for_pixi_in_pixi,
required_package=required_package,
Expand Down

0 comments on commit 2541a6d

Please sign in to comment.