Skip to content

Commit

Permalink
Allows specifying which features to build, test and lint
Browse files Browse the repository at this point in the history
  • Loading branch information
xfbs committed Dec 20, 2023
1 parent 5b1babf commit eefbcf5
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 7 deletions.
42 changes: 40 additions & 2 deletions kraken-build/src/kraken/std/cargo/tasks/cargo_build_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,38 @@ class CargoLibraryArtifact(LibraryArtifact):
pass


class CargoCrateFeatures:
"""Specify which crate features to build"""

#: When set to a list of features, only the specified features are built.
features: list[str] = list()

#: When set to False, default features are disabled.
default: bool = True

#: When set to True, all features are enabled.
all: bool = False

def __init__(self, features: list[str] = list(), default: bool = True, all: bool = False):
self.features = features
self.default = default

def flags(self) -> list[str]:
flags = []

if self.all:
flags.append("--all-features")
else:
if not self.default:
flags.append("--no-default-features")

if self.features:
features = ",".join(self.features)
flags.append(f"--features {features}")

return flags


class CargoBuildTask(Task):
"""This task runs `cargo build` using the specified parameters. It will respect the authentication
credentials configured in :attr:`CargoProjectSettings.auth`."""
Expand All @@ -29,6 +61,9 @@ class CargoBuildTask(Task):
#: to an empty list instead of parsed from the Cargo manifest.
target: Property[str]

#: Features to enable for this build.
features: Property[CargoCrateFeatures] = Property.default(CargoCrateFeatures)

#: Additional arguments to pass to the Cargo command-line.
additional_args: Property[list[str]] = Property.default_factory(list)

Expand Down Expand Up @@ -58,11 +93,14 @@ def get_description(self) -> str | None:
def get_cargo_command_additional_flags(self) -> list[str]:
return shlex.split(os.environ.get("KRAKEN_CARGO_BUILD_FLAGS", ""))

def get_cargo_command(self, env: dict[str, str]) -> list[str]:
def get_cargo_subcommand(self, env: dict[str, str], subcommand: str) -> list[str]:
incremental = self.incremental.get()
if incremental is not None:
env["CARGO_INCREMENTAL"] = "1" if incremental else "0"
return ["cargo", "build"] + self.additional_args.get()
return ["cargo", subcommand] + self.additional_args.get() + self.features.get().flags()

def get_cargo_command(self, env: dict[str, str]) -> list[str]:
return self.get_cargo_subcommand(env, "build")

def make_safe(self, args: list[str], env: dict[str, str]) -> None:
pass
Expand Down
8 changes: 5 additions & 3 deletions kraken-build/src/kraken/std/cargo/tasks/cargo_clippy_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@
class CargoClippyTask(CargoBuildTask):
"""Runs `cargo clippy` for linting or applying suggestions."""

#: When set to True, tells clippy to fix the issues.
fix: Property[bool] = Property.default(False)
allow: Property[str | None] = Property.default("staged")

# CargoBuildTask
#: When running Clippy in Fix mode, allow a dirty or staged Git work tree.
allow: Property[str | None] = Property.default("staged")

def get_cargo_command(self, env: dict[str, str]) -> list[str]:
command = ["cargo", "clippy"]
command = super().get_cargo_subcommand(env, "clippy")
if self.fix.get():
command += ["--fix"]
allow = self.allow.get()
Expand All @@ -24,4 +25,5 @@ def get_cargo_command(self, env: dict[str, str]) -> list[str]:
command += ["--allow-dirty", "--allow-staged"]
elif allow is not None:
raise ValueError(f"invalid allow: {allow!r}")

return command
40 changes: 38 additions & 2 deletions kraken-build/src/kraken/std/cargo/tasks/cargo_test_task.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,48 @@
from enum import Enum, auto

from kraken.core import Property

from .cargo_build_task import CargoBuildTask


class CargoTestIgnored(Enum):
"""How to treat ignored tests"""

#: Skip ignored tests
SKIP = auto()

#: Run ignored tests
INCLUDE = auto()

#: Run only ignored tests
ONLY = auto()


class CargoTestTask(CargoBuildTask):
"""This task runs `cargo test` using the specified parameters. It will respect the authentication
credentials configured in :attr:`CargoProjectSettings.auth`."""

description = "Run `cargo test`."

#: When set to a list of filters, run only tests which match any of these filters.
filter: Property[list[str]] = Property.default_factory(list)

#: Specify how to treat ignored tests, by default they are skipped.
ignored: Property[CargoTestIgnored] = Property.default(CargoTestIgnored.SKIP)

def get_cargo_command(self, env: dict[str, str]) -> list[str]:
super().get_cargo_command(env)
return ["cargo", "test"] + self.additional_args.get()
command = super().get_cargo_subcommand(env, "test")
command.append("--")

match self.ignored.get():
case CargoTestIgnored.SKIP:
pass
case CargoTestIgnored.INCLUDE:
command.append("--include-ignored")
case CargoTestIgnored.ONLY:
command.append("--ignored")

for filter in self.filter.get():
command.append(filter)

return command

0 comments on commit eefbcf5

Please sign in to comment.