Skip to content

Commit

Permalink
yarn-classic: get workspace packages for workspaces
Browse files Browse the repository at this point in the history
Signed-off-by: Taylor Madore <[email protected]>
  • Loading branch information
taylormadore committed Oct 23, 2024
1 parent 0c6cd40 commit 1d5caee
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 10 deletions.
6 changes: 0 additions & 6 deletions cachi2/core/package_managers/yarn_classic/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from cachi2.core.models.output import Component, EnvironmentVariable, RequestOutput
from cachi2.core.package_managers.yarn.utils import run_yarn_cmd
from cachi2.core.package_managers.yarn_classic.resolver import resolve_packages
from cachi2.core.package_managers.yarn_classic.workspaces import extract_workspace_metadata
from cachi2.core.rooted_path import RootedPath

log = logging.getLogger(__name__)
Expand All @@ -27,11 +26,6 @@ def _ensure_mirror_dir_exists(output_dir: RootedPath) -> None:
package_path = request.source_dir.join_within_root(package.path)
_ensure_mirror_dir_exists(request.output_dir)
_resolve_yarn_project(package_path, request.output_dir)
# Workspaces metadata is not used at the moment, but will
# eventualy be converted into components. Using a noop assertion
# to prevent linters from complaining.
workspaces = extract_workspace_metadata(package_path)
assert workspaces is not None # nosec -- see comment above

return RequestOutput.from_obj_list(
components, _generate_build_environment_variables(), project_files=[]
Expand Down
25 changes: 24 additions & 1 deletion cachi2/core/package_managers/yarn_classic/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
from cachi2.core.errors import PackageRejected, UnexpectedFormat
from cachi2.core.package_managers.npm import NPM_REGISTRY_CNAMES
from cachi2.core.package_managers.yarn.project import PackageJson
from cachi2.core.package_managers.yarn_classic.workspaces import (
Workspace,
extract_workspace_metadata,
)
from cachi2.core.rooted_path import RootedPath

GIT_HOSTS = {"github.com", "gitlab.com", "bitbucket.com", "bitbucket.org"}
Expand Down Expand Up @@ -203,6 +207,25 @@ def _get_main_package(source_dir: RootedPath) -> WorkspacePackage:
)


def _get_workspace_packages(
source_dir: RootedPath, workspaces: list[Workspace]
) -> list[WorkspacePackage]:
"""Return a WorkspacePackage for each Workspace."""
return [
WorkspacePackage(
name=ws.package_contents["name"],
version=ws.package_contents.get("version"),
relpath=ws.path.relative_to(source_dir.path),
)
for ws in workspaces
]


def resolve_packages(source_dir: RootedPath) -> list[YarnClassicPackage]:
"""Return a list of Packages corresponding to all project dependencies."""
return [_get_main_package(source_dir)] + _get_packages_from_lockfile(source_dir)
workspaces = extract_workspace_metadata(source_dir)
return (
[_get_main_package(source_dir)]
+ _get_workspace_packages(source_dir, workspaces)
+ _get_packages_from_lockfile(source_dir)
)
2 changes: 0 additions & 2 deletions tests/unit/package_managers/yarn_classic/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ def test_generate_build_environment_variables(
indirect=["input_request"],
)
@mock.patch("cachi2.core.package_managers.yarn_classic.main._resolve_yarn_project")
@mock.patch("cachi2.core.package_managers.yarn_classic.main.extract_workspace_metadata")
def test_fetch_yarn_source(
mock_extract_metadata: mock.Mock,
mock_resolve_yarn: mock.Mock,
input_request: Request,
yarn_classic_env_variables: list[EnvironmentVariable],
Expand Down
33 changes: 32 additions & 1 deletion tests/unit/package_managers/yarn_classic/test_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@
_classify_pyarn_package,
_get_main_package,
_get_packages_from_lockfile,
_get_workspace_packages,
_is_from_npm_registry,
_is_git_url,
_is_tarball_url,
resolve_packages,
)
from cachi2.core.package_managers.yarn_classic.workspaces import Workspace
from cachi2.core.rooted_path import PathOutsideRoot, RootedPath

VALID_GIT_URLS = [
Expand Down Expand Up @@ -246,23 +248,34 @@ def test__get_packages_from_lockfile(
assert output == [mock_package_1, mock_package_2]


@mock.patch("cachi2.core.package_managers.yarn_classic.resolver._get_workspace_packages")
@mock.patch("cachi2.core.package_managers.yarn_classic.resolver.extract_workspace_metadata")
@mock.patch("cachi2.core.package_managers.yarn_classic.resolver._get_packages_from_lockfile")
@mock.patch("cachi2.core.package_managers.yarn_classic.resolver._get_main_package")
def test_resolve_packages(
mock_get_main_package: mock.Mock,
mock_get_lockfile_packages: mock.Mock,
mock_extract_workspaces: mock.Mock,
mock_get_workspace_packages: mock.Mock,
rooted_tmp_path: RootedPath,
) -> None:
main_package = mock.Mock()
workspace_packages = [mock.Mock()]
lockfile_packages = [mock.Mock(), mock.Mock()]
expected_output = [main_package, *lockfile_packages]
expected_output = [main_package, *workspace_packages, *lockfile_packages]

mock_get_main_package.return_value = main_package
mock_get_lockfile_packages.return_value = lockfile_packages
mock_get_workspace_packages.return_value = workspace_packages

output = resolve_packages(rooted_tmp_path)
mock_extract_workspaces.assert_called_once_with(rooted_tmp_path)
mock_get_main_package.assert_called_once_with(rooted_tmp_path)
mock_get_workspace_packages.assert_called_once_with(
rooted_tmp_path, mock_extract_workspaces.return_value
)
mock_get_lockfile_packages.assert_called_once_with(rooted_tmp_path)

assert output == expected_output


Expand Down Expand Up @@ -297,3 +310,21 @@ def test__get_main_package_no_name(

with pytest.raises(PackageRejected, match=error_msg):
_get_main_package(rooted_tmp_path)


def test__get_workspace_packages(rooted_tmp_path: RootedPath) -> None:
workspace_path = rooted_tmp_path.join_within_root("foo").path
workspace = Workspace(
path=workspace_path,
package_contents={"name": "foo", "version": "1.0.0"},
)
expected = [
WorkspacePackage(
name="foo",
version="1.0.0",
relpath=workspace_path.relative_to(rooted_tmp_path.path),
)
]

output = _get_workspace_packages(rooted_tmp_path, [workspace])
assert output == expected

0 comments on commit 1d5caee

Please sign in to comment.