Skip to content

Commit 15f755c

Browse files
committed
chore: Created CargoProjectExt extention trait
1 parent 92d9a94 commit 15f755c

File tree

10 files changed

+73
-51
lines changed

10 files changed

+73
-51
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ cargo-credential-macos-keychain = { version = "0.4.15", path = "credential/cargo
3232
cargo-credential-wincred = { version = "0.4.15", path = "credential/cargo-credential-wincred" }
3333
cargo-platform = { path = "crates/cargo-platform", version = "0.3.0" }
3434
cargo-test-macro = { version = "0.4.4", path = "crates/cargo-test-macro" }
35-
cargo-test-support = { version = "0.7.5", path = "crates/cargo-test-support" }
35+
cargo-test-support = { version = "0.8.0", path = "crates/cargo-test-support" }
3636
cargo-util = { version = "0.2.22", path = "crates/cargo-util" }
3737
cargo-util-schemas = { version = "0.9.0", path = "crates/cargo-util-schemas" }
3838
cargo_metadata = "0.19.1"

crates/cargo-test-support/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "cargo-test-support"
3-
version = "0.7.5"
3+
version = "0.8.0"
44
edition.workspace = true
55
rust-version = "1.87" # MSRV:1
66
license.workspace = true

crates/cargo-test-support/src/lib.rs

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -490,28 +490,6 @@ impl Project {
490490
execs().with_process_builder(p)
491491
}
492492

493-
/// Creates a `ProcessBuilder` to run cargo.
494-
///
495-
/// Arguments can be separated by spaces.
496-
///
497-
/// For `cargo run`, see [`Project::rename_run`].
498-
///
499-
/// # Example:
500-
///
501-
/// ```no_run
502-
/// # let p = cargo_test_support::project().build();
503-
/// p.cargo("build --bin foo").run();
504-
/// ```
505-
pub fn cargo(&self, cmd: &str) -> Execs {
506-
let cargo = cargo_exe();
507-
let mut execs = self.process(&cargo);
508-
if let Some(ref mut p) = execs.process_builder {
509-
p.env("CARGO", cargo);
510-
p.arg_line(cmd);
511-
}
512-
execs
513-
}
514-
515493
/// Safely run a process after `cargo build`.
516494
///
517495
/// Windows has a problem where a process cannot be reliably

crates/cargo-test-support/src/publish.rs

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,6 @@
66
//! # use cargo_test_support::registry::RegistryBuilder;
77
//! # use cargo_test_support::publish::validate_upload;
88
//! # use cargo_test_support::project;
9-
//! // This replaces `registry::init()` and must be called before `Package::new().publish()`
10-
//! let registry = RegistryBuilder::new().http_api().http_index().build();
11-
//!
12-
//! let p = project()
13-
//! .file(
14-
//! "Cargo.toml",
15-
//! r#"
16-
//! [package]
17-
//! name = "foo"
18-
//! version = "0.0.1"
19-
//! edition = "2015"
20-
//! authors = []
21-
//! license = "MIT"
22-
//! description = "foo"
23-
//! "#,
24-
//! )
25-
//! .file("src/main.rs", "fn main() {}")
26-
//! .build();
27-
//!
28-
//! p.cargo("publish --no-verify")
29-
//! .replace_crates_io(registry.index_url())
30-
//! .run();
31-
//!
329
//! validate_upload(
3310
//! r#"
3411
//! {

crates/cargo-test-support/src/registry.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
//! "#)
4040
//! .build();
4141
//!
42-
//! p.cargo("run").with_stdout_data(str!["24"]).run();
42+
//! // p.cargo("run").with_stdout_data(str!["24"]).run();
4343
//! ```
4444
4545
use crate::git::repo;

tests/build-std/main.rs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@
2020
2121
#![allow(clippy::disallowed_methods)]
2222

23-
use cargo_test_support::prelude::*;
2423
use cargo_test_support::{basic_manifest, paths, project, rustc_host, str, Execs};
24+
use cargo_test_support::{prelude::*, Project};
2525
use std::env;
26-
use std::path::Path;
26+
use std::path::{Path, PathBuf};
2727

2828
fn enable_build_std(e: &mut Execs, arg: Option<&str>, isolated: bool) {
2929
if !isolated {
@@ -441,3 +441,34 @@ fn test_panic_abort() {
441441
.arg("-Zbuild-std-features=panic_immediate_abort")
442442
.run();
443443
}
444+
445+
pub trait CargoProjectExt {
446+
/// Creates a `ProcessBuilder` to run cargo.
447+
///
448+
/// Arguments can be separated by spaces.
449+
///
450+
/// For `cargo run`, see [`Project::rename_run`].
451+
///
452+
/// # Example:
453+
///
454+
/// ```no_run
455+
/// # let p = cargo_test_support::project().build();
456+
/// p.cargo("build --bin foo").run();
457+
/// ```
458+
fn cargo(&self, cmd: &str) -> Execs;
459+
}
460+
461+
impl CargoProjectExt for Project {
462+
fn cargo(&self, cmd: &str) -> Execs {
463+
let cargo = cargo_exe();
464+
let mut execs = self.process(&cargo);
465+
execs.env("CARGO", cargo);
466+
execs.arg_line(cmd);
467+
execs
468+
}
469+
}
470+
471+
/// Path to the cargo binary
472+
pub fn cargo_exe() -> PathBuf {
473+
snapbox::cmd::cargo_bin!("cargo").to_path_buf()
474+
}

tests/testsuite/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ mod yank;
196196
use crate::prelude::*;
197197

198198
pub mod prelude {
199+
pub use crate::utils::ext::CargoProjectExt;
199200
pub use cargo_test_support::prelude::*;
200201
pub use cargo_test_support::CargoCommandExt;
201202
}

tests/testsuite/utils/ext.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
use std::path::PathBuf;
2+
3+
use cargo_test_support::{ArgLineCommandExt, Execs, Project};
4+
5+
pub trait CargoProjectExt {
6+
/// Creates a `ProcessBuilder` to run cargo.
7+
///
8+
/// Arguments can be separated by spaces.
9+
///
10+
/// For `cargo run`, see [`Project::rename_run`].
11+
///
12+
/// # Example:
13+
///
14+
/// ```no_run
15+
/// # let p = cargo_test_support::project().build();
16+
/// p.cargo("build --bin foo").run();
17+
/// ```
18+
fn cargo(&self, cmd: &str) -> Execs;
19+
}
20+
21+
impl CargoProjectExt for Project {
22+
fn cargo(&self, cmd: &str) -> Execs {
23+
let cargo = cargo_exe();
24+
let mut execs = self.process(&cargo);
25+
execs.env("CARGO", cargo);
26+
execs.arg_line(cmd);
27+
execs
28+
}
29+
}
30+
31+
/// Path to the cargo binary
32+
pub fn cargo_exe() -> PathBuf {
33+
snapbox::cmd::cargo_bin!("cargo").to_path_buf()
34+
}

tests/testsuite/utils/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
pub mod cross_compile;
2+
pub mod ext;
23
pub mod tools;

0 commit comments

Comments
 (0)