Skip to content

Commit eefbcf5

Browse files
committed
Allows specifying which features to build, test and lint
1 parent 5b1babf commit eefbcf5

File tree

3 files changed

+83
-7
lines changed

3 files changed

+83
-7
lines changed

kraken-build/src/kraken/std/cargo/tasks/cargo_build_task.py

+40-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,38 @@ class CargoLibraryArtifact(LibraryArtifact):
2121
pass
2222

2323

24+
class CargoCrateFeatures:
25+
"""Specify which crate features to build"""
26+
27+
#: When set to a list of features, only the specified features are built.
28+
features: list[str] = list()
29+
30+
#: When set to False, default features are disabled.
31+
default: bool = True
32+
33+
#: When set to True, all features are enabled.
34+
all: bool = False
35+
36+
def __init__(self, features: list[str] = list(), default: bool = True, all: bool = False):
37+
self.features = features
38+
self.default = default
39+
40+
def flags(self) -> list[str]:
41+
flags = []
42+
43+
if self.all:
44+
flags.append("--all-features")
45+
else:
46+
if not self.default:
47+
flags.append("--no-default-features")
48+
49+
if self.features:
50+
features = ",".join(self.features)
51+
flags.append(f"--features {features}")
52+
53+
return flags
54+
55+
2456
class CargoBuildTask(Task):
2557
"""This task runs `cargo build` using the specified parameters. It will respect the authentication
2658
credentials configured in :attr:`CargoProjectSettings.auth`."""
@@ -29,6 +61,9 @@ class CargoBuildTask(Task):
2961
#: to an empty list instead of parsed from the Cargo manifest.
3062
target: Property[str]
3163

64+
#: Features to enable for this build.
65+
features: Property[CargoCrateFeatures] = Property.default(CargoCrateFeatures)
66+
3267
#: Additional arguments to pass to the Cargo command-line.
3368
additional_args: Property[list[str]] = Property.default_factory(list)
3469

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

61-
def get_cargo_command(self, env: dict[str, str]) -> list[str]:
96+
def get_cargo_subcommand(self, env: dict[str, str], subcommand: str) -> list[str]:
6297
incremental = self.incremental.get()
6398
if incremental is not None:
6499
env["CARGO_INCREMENTAL"] = "1" if incremental else "0"
65-
return ["cargo", "build"] + self.additional_args.get()
100+
return ["cargo", subcommand] + self.additional_args.get() + self.features.get().flags()
101+
102+
def get_cargo_command(self, env: dict[str, str]) -> list[str]:
103+
return self.get_cargo_subcommand(env, "build")
66104

67105
def make_safe(self, args: list[str], env: dict[str, str]) -> None:
68106
pass

kraken-build/src/kraken/std/cargo/tasks/cargo_clippy_task.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@
88
class CargoClippyTask(CargoBuildTask):
99
"""Runs `cargo clippy` for linting or applying suggestions."""
1010

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

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

1617
def get_cargo_command(self, env: dict[str, str]) -> list[str]:
17-
command = ["cargo", "clippy"]
18+
command = super().get_cargo_subcommand(env, "clippy")
1819
if self.fix.get():
1920
command += ["--fix"]
2021
allow = self.allow.get()
@@ -24,4 +25,5 @@ def get_cargo_command(self, env: dict[str, str]) -> list[str]:
2425
command += ["--allow-dirty", "--allow-staged"]
2526
elif allow is not None:
2627
raise ValueError(f"invalid allow: {allow!r}")
28+
2729
return command
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,48 @@
1+
from enum import Enum, auto
2+
3+
from kraken.core import Property
4+
15
from .cargo_build_task import CargoBuildTask
26

37

8+
class CargoTestIgnored(Enum):
9+
"""How to treat ignored tests"""
10+
11+
#: Skip ignored tests
12+
SKIP = auto()
13+
14+
#: Run ignored tests
15+
INCLUDE = auto()
16+
17+
#: Run only ignored tests
18+
ONLY = auto()
19+
20+
421
class CargoTestTask(CargoBuildTask):
522
"""This task runs `cargo test` using the specified parameters. It will respect the authentication
623
credentials configured in :attr:`CargoProjectSettings.auth`."""
724

825
description = "Run `cargo test`."
926

27+
#: When set to a list of filters, run only tests which match any of these filters.
28+
filter: Property[list[str]] = Property.default_factory(list)
29+
30+
#: Specify how to treat ignored tests, by default they are skipped.
31+
ignored: Property[CargoTestIgnored] = Property.default(CargoTestIgnored.SKIP)
32+
1033
def get_cargo_command(self, env: dict[str, str]) -> list[str]:
11-
super().get_cargo_command(env)
12-
return ["cargo", "test"] + self.additional_args.get()
34+
command = super().get_cargo_subcommand(env, "test")
35+
command.append("--")
36+
37+
match self.ignored.get():
38+
case CargoTestIgnored.SKIP:
39+
pass
40+
case CargoTestIgnored.INCLUDE:
41+
command.append("--include-ignored")
42+
case CargoTestIgnored.ONLY:
43+
command.append("--ignored")
44+
45+
for filter in self.filter.get():
46+
command.append(filter)
47+
48+
return command

0 commit comments

Comments
 (0)