Skip to content

Commit

Permalink
yarn: Expanding globs extraction to nested Arrays
Browse files Browse the repository at this point in the history
It turns out that workspaces could be either Array or
a nested Array in an Object thus we must handle both cases.
Official docs mentioning the former:
  https://classic.yarnpkg.com/lang/en/docs/workspaces/
Official blog containing a hint about the latter:
  https://classic.yarnpkg.com/lang/en/docs/workspaces/

Signed-off-by: Alexey Ovchinnikov <[email protected]>
  • Loading branch information
a-ovchinnikov committed Oct 21, 2024
1 parent fc91989 commit 746bdfe
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
7 changes: 7 additions & 0 deletions cachi2/core/package_managers/yarn_classic/workspaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,14 @@ def _extract_workspaces_globs(
package: dict[str, Any],
) -> list[str]:
"""Extract globs from workspaces entry in package dict."""
# This could be an Array or an Array nested in an Object.
# Official docs mentioning the former:
# https://classic.yarnpkg.com/lang/en/docs/workspaces/
# Official blog containing a hint about the latter:
# https://classic.yarnpkg.com/lang/en/docs/workspaces/
workspaces_globs = package.get("workspaces", [])
if isinstance(workspaces_globs, dict):
workspaces_globs = workspaces_globs.get("packages", [])
return workspaces_globs


Expand Down
22 changes: 19 additions & 3 deletions tests/unit/package_managers/yarn_classic/test_workspaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,26 @@ def test_workspaces_could_be_parsed(
assert result == expected_result


def test_extracting_workspace_globs_works_with_globs_deined_in_list() -> None:
package = {"workspaces": ["foo"]}
@pytest.mark.parametrize(
"package, expected",
[
pytest.param(
{"workspaces": ["foo"]},
["foo"],
id="workspaces_defined_in_an_array",
),
pytest.param(
{"workspaces": {"packages": ["foo"]}},
["foo"],
id="workspaces_defined_in_an_array_within_an_object",
),
],
)
def test_extracting_workspace_globs_works_for_all_types_of_workspaces(
package: dict,
expected: list,
) -> None:

expected = ["foo"]
result = _extract_workspaces_globs(package)

assert expected == result
Expand Down

0 comments on commit 746bdfe

Please sign in to comment.