Skip to content

cargo package could run tests in the packaged tree #14685

Open
@sourcefrog

Description

@sourcefrog

Problem

Today, cargo package has an option (by default on) to run a build in the packaged tree, to increase confidence that the package will build when downloaded.

Some users would like to check that the tests also pass in the package crate.

Distro packaging of Rust tools

In particular, Debian and probably other distributions that repackage Rust software would like to:

  1. start with a crate tarball, not a git checkout
  2. run the tests to increase confidence that their build is correct

However in cargo-mutants we found that the tests didn't pass in a tree unpacked from a cargo crate, because some testdata files were excluded. (See https://sources.debian.org/patches/rust-cargo-mutants/23.10.0-1/disable-tests-missing-testdata.patch/, and cc @jelmer.)

Archiving packages

Arguably there's also value in crates.io archiving a copy of the tree that's sufficiently complete that someone could resume development from that crate if the VCS repository was lost, and this will be much more feasible if the tests are known to pass. For widely cloned git repos this might seem unlikely, but there might be some crates where the released source is open but the VCS is not.

Proposed Solution

If this is acceptable in principle I could send a PR that adds cargo package --test (and cargo publish --test) that will run the tests in a copy of the packaged tree. The option would be off by default.

Counter-arguments

Package size

One conceivable argument against this is that including all the tests and testdata to make the tests pass will make the package larger, which will increase bandwidth consumption for both crates.io and users. Probably a large majority of clients that download packages do not run or even build their tests, as they only want to build the library or binary.

Perhaps some crates have very large testdata directories that today are excluded, but adding this option might motivate their authors to include them in the package.

I think that might happen at the margin but I don't think it's a conclusive reason not to do it:

  • There's already a cap on package size, and this would not change it.
  • cargo and crates.io as far as I know doesn't make any attempt to strip packages down to only what is needed to build the binary/library: many crates will include docs and other files that aren't strictly needed for the build.
  • Probably many packages do already include most or all of their tests and testdata; this would just help packages that use the option make sure that the shipped files are actually usable to achieve passing tests.
  • I would guess that for many crates the integration tests and testdata are not going to greatly increase the size of the package.

Not strictly necessary

Another conceivable and reasonable argument against adding this feature is that it's not that hard to unpack the tarball and run the tests yourself, so it's not worth adding the complexity of another feature to cargo.

Against this I think there are some reasons to have it built in:

  • Running the build seems like a good strong precedent.
  • Finding the tarball, extracting it, and building it is not quite trivial to script on Windows CI, at least for me.
  • Some package maintainers might want to be confident that all uploaded crate packages will pass their included tests: having it built in to the tool helps achieve this consistently in the same way that cargo checking the build is useful.
  • It saves every package maintainer working out how to script it.
  • Perhaps it helps advance a norm that many packages should include their tests in crate packages.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-feature-requestCategory: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted`Command-packageCommand-publishS-needs-designStatus: Needs someone to work further on the design for the feature or fix. NOT YET accepted.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions