Skip to content

Commit

Permalink
Auto merge of #14243 - epage:prelude, r=weihanglo
Browse files Browse the repository at this point in the history
feat(test): Add cargo_test to test-support prelude

### What does this PR try to resolve?

With where we are at with #14039, I was revisiting our test writing documentation.  I was wanting to put more emphasis on rustdoc and found it would be helpful to talk about the concepts if a prelude was used instead of `extern crate`.

### How should we test and review this PR?

Yes, this included some `use` clean ups in tangentially-related commit but its already painful enough
walking through every test file, I didn't want to do it twice.

### Additional information
  • Loading branch information
bors committed Jul 13, 2024
2 parents 916c5a4 + e2e2f31 commit b31577d
Show file tree
Hide file tree
Showing 166 changed files with 298 additions and 112 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,6 @@ features = [

[dev-dependencies]
annotate-snippets = { workspace = true, features = ["testing-colors"] }
cargo-test-macro.workspace = true
cargo-test-support.workspace = true
gix = { workspace = true, features = ["revision"] }
same-file.workspace = true
Expand Down
1 change: 1 addition & 0 deletions crates/cargo-test-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ pub mod registry;
pub mod tools;

pub mod prelude {
pub use crate::cargo_test;
pub use crate::ArgLine;
pub use crate::CargoCommand;
pub use crate::ChannelChanger;
Expand Down
2 changes: 2 additions & 0 deletions src/doc/contrib/src/tests/writing.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ stdout and stderr output against the expected output.

Generally, a functional test will be placed in `tests/testsuite/<command>.rs` and will look roughly like:
```rust,ignore
use cargo_test_support::prelude::*;
#[cargo_test]
fn <description>() {
let p = project()
Expand Down
2 changes: 1 addition & 1 deletion tests/build-std/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#![allow(clippy::disallowed_methods)]

use cargo_test_support::prelude::*;
use cargo_test_support::*;
use cargo_test_support::{basic_manifest, paths, project, rustc_host, str, Execs};
use std::env;
use std::path::Path;

Expand Down
1 change: 1 addition & 0 deletions tests/testsuite/advanced_env.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! -Zadvanced-env tests

use cargo_test_support::prelude::*;
use cargo_test_support::{paths, project, registry::Package};

#[cargo_test]
Expand Down
3 changes: 2 additions & 1 deletion tests/testsuite/alt_registry.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
//! Tests for alternative registries.

use std::fs;

use cargo_test_support::compare::assert_e2e;
use cargo_test_support::prelude::*;
use cargo_test_support::publish::validate_alt_upload;
use cargo_test_support::registry::{self, Package, RegistryBuilder};
use cargo_test_support::str;
use cargo_test_support::{basic_manifest, paths, project};
use std::fs;

#[cargo_test]
fn depend_on_alt_registry() {
Expand Down
7 changes: 4 additions & 3 deletions tests/testsuite/artifact_dir.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
//! Tests for --artifact-dir flag.

use std::env;
use std::fs;
use std::path::Path;

use cargo_test_support::prelude::*;
use cargo_test_support::sleep_ms;
use cargo_test_support::str;
use cargo_test_support::{basic_manifest, project};
use std::env;
use std::fs;
use std::path::Path;

#[cargo_test]
fn binary_with_debug() {
Expand Down
9 changes: 5 additions & 4 deletions tests/testsuite/build.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
//! Tests for the `cargo build` command.

use std::env;
use std::fs;
use std::io::Read;
use std::process::Stdio;

use cargo::{
core::compiler::CompileMode,
core::{Shell, Workspace},
Expand All @@ -17,10 +22,6 @@ use cargo_test_support::{
tools, Execs, ProjectBuilder,
};
use cargo_util::paths::dylib_path_envvar;
use std::env;
use std::fs;
use std::io::Read;
use std::process::Stdio;

#[cargo_test]
fn cargo_compile_simple() {
Expand Down
9 changes: 5 additions & 4 deletions tests/testsuite/build_script.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
//! Tests for build.rs scripts.

use std::env;
use std::fs;
use std::io;
use std::thread;

use cargo_test_support::compare::assert_e2e;
use cargo_test_support::install::cargo_home;
use cargo_test_support::paths::CargoPathExt;
Expand All @@ -12,10 +17,6 @@ use cargo_test_support::{
};
use cargo_test_support::{rustc_host, sleep_ms, slow_cpu_multiplier, symlink_supported};
use cargo_util::paths::{self, remove_dir_all};
use std::env;
use std::fs;
use std::io;
use std::thread;

#[cargo_test]
fn custom_build_script_failed() {
Expand Down
1 change: 1 addition & 0 deletions tests/testsuite/build_script_env.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Tests for build.rs rerun-if-env-changed and rustc-env

use cargo_test_support::basic_manifest;
use cargo_test_support::prelude::*;
use cargo_test_support::project;
use cargo_test_support::sleep_ms;
use cargo_test_support::str;
Expand Down
1 change: 1 addition & 0 deletions tests/testsuite/build_script_extra_link_arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// because MSVC link.exe just gives a warning on unknown flags (how helpful!),
// and other linkers will return an error.

use cargo_test_support::prelude::*;
use cargo_test_support::registry::Package;
use cargo_test_support::str;
use cargo_test_support::{basic_bin_manifest, basic_lib_manifest, basic_manifest, project};
Expand Down
7 changes: 5 additions & 2 deletions tests/testsuite/cache_lock.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
//! Tests for `CacheLock`.

use crate::config::GlobalContextBuilder;
use std::thread::JoinHandle;

use cargo::util::cache_lock::{CacheLockMode, CacheLocker};
use cargo_test_support::paths::{self, CargoPathExt};
use cargo_test_support::prelude::*;
use cargo_test_support::{retry, thread_wait_timeout, threaded_timeout};
use std::thread::JoinHandle;

use crate::config::GlobalContextBuilder;

/// Helper to verify that it is OK to acquire the given lock (it shouldn't block).
fn verify_lock_is_ok(mode: CacheLockMode) {
Expand Down
4 changes: 3 additions & 1 deletion tests/testsuite/cache_messages.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
//! Tests for caching compiler diagnostics.

use super::messages::raw_rustc_output;
use cargo_test_support::prelude::*;
use cargo_test_support::str;
use cargo_test_support::tools;
use cargo_test_support::{basic_manifest, is_coarse_mtime, project, registry::Package, sleep_ms};

use super::messages::raw_rustc_output;

fn as_str(bytes: &[u8]) -> &str {
std::str::from_utf8(bytes).expect("valid utf-8")
}
Expand Down
1 change: 1 addition & 0 deletions tests/testsuite/cargo_alias_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use std::env;

use cargo_test_support::prelude::*;
use cargo_test_support::str;
use cargo_test_support::tools::echo_subcommand;
use cargo_test_support::{basic_bin_manifest, project};
Expand Down
1 change: 1 addition & 0 deletions tests/testsuite/cargo_bench/no_keep_going/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use cargo_test_support::current_dir;
use cargo_test_support::file;
use cargo_test_support::prelude::*;
use cargo_test_support::str;
use cargo_test_support::CargoCommand;
use cargo_test_support::Project;
Expand Down
1 change: 1 addition & 0 deletions tests/testsuite/cargo_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::str;

use cargo_test_support::basic_manifest;
use cargo_test_support::paths::CargoPathExt;
use cargo_test_support::prelude::*;
use cargo_test_support::registry::Package;
use cargo_test_support::str;
use cargo_test_support::tools::echo_subcommand;
Expand Down
1 change: 1 addition & 0 deletions tests/testsuite/cargo_env_config.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Tests for `[env]` config.

use cargo_test_support::basic_manifest;
use cargo_test_support::prelude::*;
use cargo_test_support::str;
use cargo_test_support::{basic_bin_manifest, project};

Expand Down
1 change: 1 addition & 0 deletions tests/testsuite/cargo_features.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Tests for `cargo-features` definitions.

use cargo_test_support::prelude::*;
use cargo_test_support::registry::Package;
use cargo_test_support::str;
use cargo_test_support::{project, registry};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use cargo_test_support::compare::assert_ui;
use cargo_test_support::current_dir;
use cargo_test_support::file;
use cargo_test_support::prelude::*;
use cargo_test_support::str;
use cargo_test_support::CargoCommand;
use cargo_test_support::Project;
Expand Down
3 changes: 2 additions & 1 deletion tests/testsuite/cargo_init/reserved_name/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use std::fs;

use cargo_test_support::file;
use cargo_test_support::paths;
use cargo_test_support::prelude::*;
use cargo_test_support::str;
use std::fs;

#[cargo_test]
fn case() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use cargo_test_support::compare::assert_ui;
use cargo_test_support::current_dir;
use cargo_test_support::file;
use cargo_test_support::prelude::*;
use cargo_test_support::str;
use cargo_test_support::CargoCommand;
use cargo_test_support::Project;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use cargo_test_support::compare::assert_ui;
use cargo_test_support::current_dir;
use cargo_test_support::file;
use cargo_test_support::prelude::*;
use cargo_test_support::str;
use cargo_test_support::CargoCommand;
use cargo_test_support::Project;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use cargo_test_support::compare::assert_ui;
use cargo_test_support::current_dir;
use cargo_test_support::file;
use cargo_test_support::prelude::*;
use cargo_test_support::str;
use cargo_test_support::CargoCommand;
use cargo_test_support::Project;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use cargo_test_support::compare::assert_ui;
use cargo_test_support::current_dir;
use cargo_test_support::file;
use cargo_test_support::prelude::*;
use cargo_test_support::str;
use cargo_test_support::CargoCommand;
use cargo_test_support::Project;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use cargo_test_support::compare::assert_ui;
use cargo_test_support::current_dir;
use cargo_test_support::file;
use cargo_test_support::prelude::*;
use cargo_test_support::str;
use cargo_test_support::CargoCommand;
use cargo_test_support::Project;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use cargo_test_support::compare::assert_ui;
use cargo_test_support::current_dir;
use cargo_test_support::file;
use cargo_test_support::prelude::*;
use cargo_test_support::str;
use cargo_test_support::CargoCommand;
use cargo_test_support::Project;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use cargo_test_support::compare::assert_ui;
use cargo_test_support::current_dir;
use cargo_test_support::file;
use cargo_test_support::prelude::*;
use cargo_test_support::str;
use cargo_test_support::CargoCommand;
use cargo_test_support::Project;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use cargo_test_support::compare::assert_ui;
use cargo_test_support::current_dir;
use cargo_test_support::file;
use cargo_test_support::prelude::*;
use cargo_test_support::str;
use cargo_test_support::CargoCommand;
use cargo_test_support::Project;
Expand Down
1 change: 1 addition & 0 deletions tests/testsuite/cargo_new/empty_name/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use cargo_test_support::compare::assert_ui;
use cargo_test_support::current_dir;
use cargo_test_support::file;
use cargo_test_support::prelude::*;
use cargo_test_support::str;
use cargo_test_support::CargoCommand;
use cargo_test_support::Project;
Expand Down
1 change: 1 addition & 0 deletions tests/testsuite/cargo_new/inherit_workspace_lints/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use cargo_test_support::compare::assert_ui;
use cargo_test_support::current_dir;
use cargo_test_support::file;
use cargo_test_support::prelude::*;
use cargo_test_support::str;
use cargo_test_support::CargoCommand;
use cargo_test_support::Project;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use cargo_test_support::compare::assert_ui;
use cargo_test_support::current_dir;
use cargo_test_support::file;
use cargo_test_support::prelude::*;
use cargo_test_support::str;
use cargo_test_support::CargoCommand;
use cargo_test_support::Project;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use cargo_test_support::compare::assert_ui;
use cargo_test_support::current_dir;
use cargo_test_support::file;
use cargo_test_support::prelude::*;
use cargo_test_support::str;
use cargo_test_support::CargoCommand;
use cargo_test_support::Project;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use cargo_test_support::compare::assert_ui;
use cargo_test_support::current_dir;
use cargo_test_support::file;
use cargo_test_support::prelude::*;
use cargo_test_support::str;
use cargo_test_support::CargoCommand;
use cargo_test_support::Project;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use cargo_test_support::compare::assert_ui;
use cargo_test_support::current_dir;
use cargo_test_support::file;
use cargo_test_support::prelude::*;
use cargo_test_support::str;
use cargo_test_support::CargoCommand;
use cargo_test_support::Project;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use cargo_test_support::compare::assert_ui;
use cargo_test_support::current_dir;
use cargo_test_support::file;
use cargo_test_support::prelude::*;
use cargo_test_support::str;
use cargo_test_support::CargoCommand;
use cargo_test_support::Project;
Expand Down
1 change: 1 addition & 0 deletions tests/testsuite/cargo_remove/avoid_empty_tables/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use cargo_test_support::compare::assert_ui;
use cargo_test_support::current_dir;
use cargo_test_support::file;
use cargo_test_support::prelude::*;
use cargo_test_support::str;
use cargo_test_support::CargoCommand;
use cargo_test_support::Project;
Expand Down
1 change: 1 addition & 0 deletions tests/testsuite/cargo_remove/build/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use cargo_test_support::compare::assert_ui;
use cargo_test_support::current_dir;
use cargo_test_support::file;
use cargo_test_support::prelude::*;
use cargo_test_support::str;
use cargo_test_support::CargoCommand;
use cargo_test_support::Project;
Expand Down
1 change: 1 addition & 0 deletions tests/testsuite/cargo_remove/dev/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use cargo_test_support::compare::assert_ui;
use cargo_test_support::current_dir;
use cargo_test_support::file;
use cargo_test_support::prelude::*;
use cargo_test_support::str;
use cargo_test_support::CargoCommand;
use cargo_test_support::Project;
Expand Down
1 change: 1 addition & 0 deletions tests/testsuite/cargo_remove/dry_run/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use cargo_test_support::compare::assert_ui;
use cargo_test_support::current_dir;
use cargo_test_support::file;
use cargo_test_support::prelude::*;
use cargo_test_support::str;
use cargo_test_support::CargoCommand;
use cargo_test_support::Project;
Expand Down
1 change: 1 addition & 0 deletions tests/testsuite/cargo_remove/gc_keep_used_patch/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use cargo_test_support::compare::assert_ui;
use cargo_test_support::current_dir;
use cargo_test_support::file;
use cargo_test_support::prelude::*;
use cargo_test_support::str;
use cargo_test_support::CargoCommand;
use cargo_test_support::Project;
Expand Down
1 change: 1 addition & 0 deletions tests/testsuite/cargo_remove/gc_patch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use cargo_test_support::compare::assert_ui;
use cargo_test_support::current_dir;
use cargo_test_support::file;
use cargo_test_support::git;
use cargo_test_support::prelude::*;
use cargo_test_support::project;
use cargo_test_support::str;
use cargo_test_support::CargoCommand;
Expand Down
Loading

0 comments on commit b31577d

Please sign in to comment.