Skip to content

Commit

Permalink
add unit test for fetch_yarn_source
Browse files Browse the repository at this point in the history
Signed-off-by: Taylor Madore <[email protected]>
  • Loading branch information
taylormadore authored and eskultety committed Nov 29, 2023
1 parent 6e3711c commit 9e07eae
Showing 1 changed file with 112 additions and 1 deletion.
113 changes: 112 additions & 1 deletion tests/unit/package_managers/yarn/test_main.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,48 @@
import itertools
import re
from enum import Enum
from itertools import zip_longest
from pathlib import Path
from typing import List, Optional, Union
from unittest import mock

import pytest
import semver

from cachi2.core.errors import PackageRejected, UnexpectedFormat, YarnCommandError
from cachi2.core.models.output import EnvironmentVariable
from cachi2.core.models.input import Request
from cachi2.core.models.output import BuildConfig, Component, EnvironmentVariable, RequestOutput
from cachi2.core.package_managers.yarn.main import (
_configure_yarn_version,
_fetch_dependencies,
_generate_environment_variables,
_resolve_yarn_project,
_set_yarnrc_configuration,
fetch_yarn_source,
)
from cachi2.core.package_managers.yarn.project import YarnRc
from cachi2.core.rooted_path import RootedPath


@pytest.fixture
def yarn_input_packages(request: pytest.FixtureRequest) -> list[dict[str, str]]:
return request.param


@pytest.fixture
def yarn_request(tmp_path: Path, yarn_input_packages: list[dict[str, str]]) -> Request:
# Create folder in the specified path, otherwise Request validation would fail
for package in yarn_input_packages:
if "path" in package:
(tmp_path / package["path"]).mkdir(exist_ok=True)

return Request(
source_dir=tmp_path,
output_dir=tmp_path / "output",
packages=yarn_input_packages,
)


@pytest.fixture()
def yarn_env_variables() -> list[EnvironmentVariable]:
return [
Expand Down Expand Up @@ -229,3 +252,91 @@ def test_set_yarnrc_configuration(mock_write: mock.Mock) -> None:
def test_generate_environment_variables(yarn_env_variables: list[EnvironmentVariable]) -> None:
result = _generate_environment_variables()
assert result == yarn_env_variables


@pytest.mark.parametrize(
"yarn_input_packages, package_components",
(
pytest.param(
[{"type": "yarn", "path": "."}],
[
[
Component(
name="foo",
purl="pkg:npm/[email protected]",
version="1.0.0",
),
Component(
name="bar",
purl="pkg:npm/[email protected]",
version="2.0.0",
),
],
],
id="single_input_package",
),
pytest.param(
[{"type": "yarn", "path": "."}, {"type": "yarn", "path": "./path"}],
[
[
Component(
name="foo",
purl="pkg:npm/[email protected]",
version="1.0.0",
),
],
[
Component(
name="bar",
purl="pkg:npm/[email protected]",
version="2.0.0",
),
Component(
name="baz",
purl="pkg:npm/[email protected]",
version="3.0.0",
),
],
],
id="multiple_input_packages",
),
),
indirect=["yarn_input_packages"],
)
@mock.patch("cachi2.core.package_managers.yarn.main._resolve_yarn_project")
@mock.patch("cachi2.core.package_managers.yarn.project.Project.from_source_dir")
def test_fetch_yarn_source(
mock_project_from_source_dir: mock.Mock,
mock_resolve_yarn: mock.Mock,
package_components: list[Component],
yarn_request: Request,
yarn_env_variables: list[EnvironmentVariable],
) -> None:
mock_project = [mock.Mock() for _ in yarn_request.packages]
mock_project_from_source_dir.side_effect = mock_project
mock_resolve_yarn.side_effect = package_components

output = fetch_yarn_source(yarn_request)

calls = [
mock.call(
yarn_request.source_dir.join_within_root(package.path),
)
for package in yarn_request.packages
]
mock_project_from_source_dir.assert_has_calls(calls)

calls = [
mock.call(
project,
yarn_request.output_dir,
)
for project in mock_project
]
mock_resolve_yarn.assert_has_calls(calls)

expected_output = RequestOutput(
components=list(itertools.chain.from_iterable(package_components)),
build_config=BuildConfig(environment_variables=yarn_env_variables),
)
assert output == expected_output

0 comments on commit 9e07eae

Please sign in to comment.