Skip to content

Commit

Permalink
Add a test that stderr from rustc is shown when we fail to determine …
Browse files Browse the repository at this point in the history
…the installed toolchain
  • Loading branch information
jyn514 committed Oct 3, 2022
1 parent f9724c6 commit fed7d14
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 20 deletions.
50 changes: 34 additions & 16 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ fs_extra = "1.2.0"
human-size = "0.4.2"
predicates = "2.1.1"
tempfile = "3.3.0"
which = { version = "4.3.0", default-features = false }
40 changes: 36 additions & 4 deletions tests/flags.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::{
borrow::BorrowMut,
fmt::Debug,
path::{Path, PathBuf}, borrow::BorrowMut,
fs::Permissions,
path::{Path, PathBuf},
};

use anyhow::{Context, Result};
Expand All @@ -11,6 +12,7 @@ use fs_extra::dir::get_size;
use predicates::prelude::PredicateBooleanExt;
use predicates::str::contains;
use tempfile::{tempdir, TempDir};
use which::which;

struct AnyhowWithContext(anyhow::Error);
impl Debug for AnyhowWithContext {
Expand All @@ -23,6 +25,7 @@ impl<T: Into<anyhow::Error>> From<T> for AnyhowWithContext {
Self(err.into())
}
}
type TestResult = Result<(), AnyhowWithContext>;

fn test_dir() -> PathBuf {
Path::new(env!("CARGO_MANIFEST_DIR")).join("tests")
Expand Down Expand Up @@ -125,7 +128,7 @@ fn count_cleaned_dry_run(target: &TempDir, args: &[&str], old_size: u64) -> Resu
}

#[test]
fn all_flags() -> Result<(), AnyhowWithContext> {
fn all_flags() -> TestResult {
let all_combos = [
["--time", "0"].as_slice(),
&["--maxsize", "0"],
Expand All @@ -147,7 +150,7 @@ fn all_flags() -> Result<(), AnyhowWithContext> {
}

#[test]
fn stamp_file() -> Result<(), AnyhowWithContext> {
fn stamp_file() -> TestResult {
let (size, target) = build("sample-project")?;

// Create a stamp file for --file.
Expand Down Expand Up @@ -175,7 +178,7 @@ fn stamp_file() -> Result<(), AnyhowWithContext> {
}

#[test]
fn hidden() -> Result<(), AnyhowWithContext> {
fn hidden() -> TestResult {
// This path is so strange because we use CARGO_TARGET_DIR to set the target to a temporary directory.
// So we can't let cargo-sweep discover any other projects, or it will think they share the same directory as this hidden project.
let (size, target) = build("fresh-prefix/.hidden/hidden-project")?;
Expand All @@ -198,3 +201,32 @@ fn hidden() -> Result<(), AnyhowWithContext> {

Ok(())
}

#[test]
#[cfg(unix)]
/// Setup a PATH that has a rustc that always gives an error. Make sure we see the error output.
fn error_output() -> TestResult {
use std::{io::ErrorKind, os::unix::prelude::PermissionsExt};

let cargo = which("cargo")?;
match std::os::unix::fs::symlink(&cargo, test_dir().join("cargo")) {
Err(e) if e.kind() == ErrorKind::AlreadyExists => {}
Err(e) => return Err(e.into()),
Ok(_) => {}
}

// lmao
const FAKE_RUSTC: &str = r#"#!/bin/sh
echo "oh no an error" >&2
exit 1
"#;
let rustc = test_dir().join("rustc");
std::fs::write(&rustc, FAKE_RUSTC)?;
std::fs::set_permissions(&rustc, Permissions::from_mode(0o700))?;

build("sample-project")?;
let assert = run(sweep(&["--installed"]).env("PATH", test_dir()));
assert.stdout(contains("oh no an error"));

Ok(())
}

0 comments on commit fed7d14

Please sign in to comment.