Skip to content

Commit

Permalink
Don't add channel if the env is not provided explicitly.
Browse files Browse the repository at this point in the history
In test compile and run test should use the same
channel configuration.
Skip a test on msrv because it needs a libray that use an higher msrv.
  • Loading branch information
la10736 committed Dec 8, 2024
1 parent 9a78bfb commit 11869b5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 27 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,7 @@ jobs:
- uses: actions/checkout@v4
- uses: taiki-e/install-action@cargo-hack
- run: cargo hack check --rust-version --workspace --ignore-private
- run: cargo hack test --rust-version --workspace --exclude rstest_reuse --ignore-private
- run: |
cargo hack test --rust-version --workspace \
--exclude rstest_reuse --ignore-private \
-- --skip rstest::ignore_args_not_fixtures
55 changes: 29 additions & 26 deletions rstest_test/src/prj.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,17 @@ pub enum Channel {
Custom(String),
}

pub static CHANNEL_DEFAULT: Channel = Channel::Stable;
impl ToString for Channel {
fn to_string(&self) -> String {
match self {
Channel::Stable => "+stable".into(),
Channel::Beta => "+beta".into(),
Channel::Nightly => "+nightly".into(),
Channel::Custom(name) => format!("+{name}"),
}
}
}

pub static ENV_CHANNEL: &str = "RSTEST_TEST_CHANNEL";

impl From<String> for Channel {
Expand All @@ -33,19 +43,14 @@ impl From<String> for Channel {
}
}

impl Default for Channel {
fn default() -> Self {
std::env::var(ENV_CHANNEL)
.ok()
.map(Channel::from)
.unwrap_or_else(|| CHANNEL_DEFAULT.clone())
}
fn read_channel_env() -> Option<Channel> {
std::env::var(ENV_CHANNEL).ok().map(Channel::from)
}

pub struct Project {
pub name: OsString,
root: PathBuf,
channel: Channel,
channel: Option<Channel>,
nocapture: bool,
ws: Arc<std::sync::RwLock<()>>,
default_timeout: Option<u64>,
Expand All @@ -58,7 +63,7 @@ impl Project {
Self {
root: root.as_ref().to_owned(),
name: "project".into(),
channel: Default::default(),
channel: read_channel_env(),
nocapture: false,
ws: Arc::new(std::sync::RwLock::new(())),
default_timeout: Default::default(),
Expand Down Expand Up @@ -103,15 +108,13 @@ impl Project {
if !self.has_test_global_attribute(self.code_path()) {
self.add_test_global_attribute(self.code_path())
}
let mut cmd = Command::new("cargo");
let mut cmd = self.cargo_cmd();

if let Some(timeout) = self.default_timeout {
cmd.env("RSTEST_TIMEOUT", timeout.to_string());
}

cmd.current_dir(self.path())
.arg(self.cargo_channel_arg())
.arg("test");
cmd.arg("test");

if self.nocapture {
cmd.args(["--", "--nocapture"]);
Expand All @@ -122,11 +125,7 @@ impl Project {

pub fn compile(&self) -> Result<std::process::Output, std::io::Error> {
let _guard = self.ws.read().expect("Cannot lock workspace resource");
Command::new("cargo")
.current_dir(self.path())
.arg("build")
.arg("--tests")
.output()
self.cargo_cmd().arg("build").arg("--tests").output()
}

fn create(self) -> Self {
Expand Down Expand Up @@ -207,6 +206,15 @@ impl Project {
.to_owned()
}

fn cargo_cmd(&self) -> Command {
let mut cmd = Command::new("cargo");
if let Some(channel) = &self.cargo_channel_arg() {
cmd.arg(channel);
}
cmd.current_dir(self.path());
cmd
}

fn workspace_add(&self, prj: &str) {
let mut doc = self.read_cargo_toml();

Expand Down Expand Up @@ -249,13 +257,8 @@ impl Project {
.expect("cannot write Cargo.toml");
}

fn cargo_channel_arg(&self) -> String {
match &self.channel {
Channel::Stable => "+stable".into(),
Channel::Beta => "+beta".into(),
Channel::Nightly => "+nightly".into(),
Channel::Custom(name) => format!("+{name}"),
}
fn cargo_channel_arg(&self) -> Option<String> {
self.channel.as_ref().map(ToString::to_string)
}

// in seconds
Expand Down

0 comments on commit 11869b5

Please sign in to comment.