Skip to content

Commit

Permalink
feat(cli): add 'dev-package-managers' flag
Browse files Browse the repository at this point in the history
- adjust existing CLI/input unit tests
- STONEBLD-1750

Add a flag which allows for creating requests using a package manager
which is still under development

Any such package manager(s) will be "hidden" behind the new flag, which
should prevent their use by end-users (at least those using Cachi2 in
pipelines)

This story is part of the "Yarn for Node.js" epic

Signed-off-by: Ben Alkov <[email protected]>
  • Loading branch information
ben-alkov committed Nov 10, 2023
1 parent 655291b commit 14b7dbb
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 7 deletions.
4 changes: 3 additions & 1 deletion cachi2/core/models/input.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ def show_error(error: "ErrorDict") -> str:
# Supported package managers
PackageManagerType = Literal["gomod", "npm", "pip", "yarn"]

Flag = Literal["cgo-disable", "force-gomod-tidy", "gomod-vendor", "gomod-vendor-check"]
Flag = Literal[
"cgo-disable", "dev-package-managers", "force-gomod-tidy", "gomod-vendor", "gomod-vendor-check"
]


class _PackageInputBase(pydantic.BaseModel, extra="forbid"):
Expand Down
11 changes: 9 additions & 2 deletions cachi2/core/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,28 @@
"pip": pip.fetch_pip_source,
}

_dev_package_managers: dict[PackageManagerType, Handler] = {

}

# This is *only* used to provide a list for `cachi2 --version`
supported_package_managers = list(_package_managers)


def resolve_packages(request: Request) -> RequestOutput:
"""Run all requested package managers, return their combined output."""
_supported_package_managers = _package_managers
requested_types = set(pkg.type for pkg in request.packages)
unsupported_types = requested_types - _package_managers.keys()
if "dev-package-managers" in request.flags:
_supported_package_managers = _package_managers | _dev_package_managers
unsupported_types = requested_types - _supported_package_managers.keys()
if unsupported_types:
raise UnsupportedFeature(
f"Package manager(s) not yet supported: {', '.join(sorted(unsupported_types))}",
# unknown package managers shouldn't get past input validation
solution="But the good news is that we're already working on it!",
)
pkg_managers = [_package_managers[type_] for type_ in sorted(requested_types)]
pkg_managers = [_supported_package_managers[type_] for type_ in sorted(requested_types)]
return _merge_outputs(pkg_manager(request) for pkg_manager in pkg_managers)


Expand Down
17 changes: 15 additions & 2 deletions cachi2/interface/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ def fetch_deps(
resolve_path=True,
help="Write output files to this directory.",
),
dev_package_managers: bool = typer.Option(False, "--dev-package-managers", hidden=True),
cgo_disable: bool = typer.Option(
False, "--cgo-disable", help="Set CGO_ENABLED=0 while processing gomod packages."
),
Expand Down Expand Up @@ -216,8 +217,20 @@ def normalize_input() -> dict[str, list[Any]]:
return {"packages": [{"type": raw_input}]}

def combine_option_and_json_flags(json_flags: list[Flag]) -> list[str]:
flag_names = ["cgo-disable", "force-gomod-tidy", "gomod-vendor", "gomod-vendor-check"]
flag_values = [cgo_disable, force_gomod_tidy, gomod_vendor, gomod_vendor_check]
flag_names = [
"cgo-disable",
"dev-package-managers",
"force-gomod-tidy",
"gomod-vendor",
"gomod-vendor-check",
]
flag_values = [
cgo_disable,
dev_package_managers,
force_gomod_tidy,
gomod_vendor,
gomod_vendor_check,
]
flags = [name for name, value in zip(flag_names, flag_values) if value]

if json_flags:
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/models/test_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ def test_invalid_package_paths(self, path: str, expect_error: str, tmp_path: Pat
)

def test_invalid_flags(self) -> None:
expect_error = r"Input should be 'cgo-disable', 'force-gomod-tidy', 'gomod-vendor' or 'gomod-vendor-check'"
expect_error = r"Input should be 'cgo-disable', 'dev-package-managers', 'force-gomod-tidy', 'gomod-vendor' or 'gomod-vendor-check'"
with pytest.raises(pydantic.ValidationError, match=expect_error):
Request(
source_dir="/source",
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ def test_specify_flags(
),
(
['{"packages": [{"type": "gomod"}], "flags": ["no-such-flag"]}'],
"Input should be 'cgo-disable', 'force-gomod-tidy', 'gomod-vendor' or 'gomod-vendor-check'",
"Input should be 'cgo-disable', 'dev-package-managers', 'force-gomod-tidy', 'gomod-vendor' or 'gomod-vendor-check'",
),
],
)
Expand Down

0 comments on commit 14b7dbb

Please sign in to comment.