Skip to content

Commit

Permalink
Add cargoDocTest (#720)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: Ivan Petkov <[email protected]>
  • Loading branch information
9999years and ipetkov authored Oct 12, 2024
1 parent fd86b78 commit 112a80c
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## Unreleased

### Added

* `cargoDocTest` is now available as an alternative to `cargoTest` which runs
only doc tests.

### Fixed
* Vendoring dependencies avoids creating malformed TOML configurations in
situations where registry name/url definitions cannot be found. When this
Expand Down
7 changes: 7 additions & 0 deletions checks/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,13 @@ in
};
};

runCargoDocTests = myLib.cargoDocTest {
src = ./simple-only-tests;
cargoArtifacts = myLib.buildDepsOnly {
src = ./simple-only-tests;
};
};

simple-nonflake = (import ../default.nix {
inherit pkgs;
}).buildPackage {
Expand Down
35 changes: 33 additions & 2 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,27 @@ environment variables during the build, you can bring them back via
* `cargoDocExtraArgs`
* `cargoExtraArgs`

### `craneLib.cargoDocTest`

`cargoDocTest :: set -> drv`

Create a derivation which will run a `cargo test --doc` invocation in a cargo
workspace. To run all or any tests for a workspace, consider `cargoTest`.

Except where noted below, all derivation attributes are delegated to
* `buildPhaseCargoCommand` will be set to run `cargo test --profile release` in
the workspace.
- `CARGO_PROFILE` can be set on the derivation to alter which cargo profile is
selected; setting it to `""` will omit specifying a profile altogether.
* `pnameSuffix` will be set to `"-doctest"`

#### Optional attributes
* `cargoExtraArgs`: additional flags to be passed in the cargo invocation
- Default value: `"--locked"`
* `cargoTestExtraArgs`: additional flags to be passed in the cargo
invocation
- Default value: `""`

### `craneLib.cargoFmt`

`cargoFmt :: set -> drv`
Expand Down Expand Up @@ -629,7 +650,9 @@ environment variables during the build, you can bring them back via
`cargoNextest :: set -> drv`

Create a derivation which will run a `cargo nextest` invocation in a cargo
workspace.
workspace. Note that [`cargo nextest` doesn't run
doctests](https://github.com/nextest-rs/nextest/issues/16), so you may also
want to build a `cargoDocTest` derivation.

Except where noted below, all derivation attributes are delegated to
`mkCargoDerivation`, and can be used to influence its behavior.
Expand Down Expand Up @@ -748,7 +771,7 @@ Except where noted below, all derivation attributes are delegated to
#### Optional attributes
* `cargoExtraArgs`: additional flags to be passed in the cargo invocation
- Default value: `"--locked"`
* `cargoTestArgs`: additional flags to be passed in the cargo
* `cargoTestExtraArgs`: additional flags to be passed in the cargo
invocation
- Default value: `""`

Expand All @@ -760,6 +783,14 @@ environment variables during the build, you can bring them back via
* `cargoExtraArgs`
* `cargoTestExtraArgs`

#### Remove attributes
The following attributes will be removed before being lowered to
`mkCargoDerivation`. If you absolutely need these attributes present as
environment variables during the build, you can bring them back via
`.overrideAttrs`.
* `cargoExtraArgs`
* `cargoTestExtraArgs`

### `craneLib.cleanCargoSource`

`cleanCargoSource :: path or drv -> drv`
Expand Down
22 changes: 22 additions & 0 deletions lib/cargoDocTest.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{ mkCargoDerivation
}:

{ cargoArtifacts
, cargoExtraArgs ? "--locked"
, cargoTestExtraArgs ? ""
, ...
}@origArgs:
let
args = (builtins.removeAttrs origArgs [
"cargoExtraArgs"
"cargoTestExtraArgs"
]);
in
mkCargoDerivation (args // {
inherit cargoArtifacts;
doCheck = args.doCheck or true;

pnameSuffix = "-doctest";
buildPhaseCargoCommand = "";
checkPhaseCargoCommand = "cargoWithProfile test --doc ${cargoExtraArgs} ${cargoTestExtraArgs}";
})
1 change: 1 addition & 0 deletions lib/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ let
cargoClippy = callPackage ./cargoClippy.nix { };
cargoDeny = callPackage ./cargoDeny.nix { };
cargoDoc = callPackage ./cargoDoc.nix { };
cargoDocTest = callPackage ./cargoDocTest.nix { };
cargoFmt = callPackage ./cargoFmt.nix { };
cargoHelperFunctionsHook = callPackage ./setupHooks/cargoHelperFunctions.nix { };
cargoLlvmCov = callPackage ./cargoLlvmCov.nix { };
Expand Down

0 comments on commit 112a80c

Please sign in to comment.