Skip to content

Commit

Permalink
Merge pull request rust-lang#104 from orium/make-it-work
Browse files Browse the repository at this point in the history
Fix tests.
  • Loading branch information
ibabushkin authored Jun 8, 2019
2 parents 6a9aaaa + 26073be commit 625524f
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 69 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ name = "rust-semverver"
path = "src/bin/rust_semverver.rs"

[dependencies]
cargo = "0.35"
crates-io = "0.23"
cargo = "0.36"
crates-io = "0.24"
curl = "0.4.21"
env_logger = "0.6"
failure = "0.1"
Expand Down
4 changes: 0 additions & 4 deletions ci/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@

set -ex

# Note: this is required for correctness,
# otherwise executing multiple "full" tests in parallel
# of the same library can alter results.
export RUST_TEST_THREADS=1
export RUST_BACKTRACE=full
#export RUST_TEST_NOCAPTURE=1

Expand Down
6 changes: 5 additions & 1 deletion src/bin/cargo_semver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,10 +352,14 @@ impl<'a> SourceInfo<'a> {
/// Construct a new source info for `crates.io`.
pub fn new(config: &'a cargo::Config) -> Result<SourceInfo<'a>> {
let source_id = SourceId::crates_io(config)?;
let source = source_id.load(config, &HashSet::new())?;
let mut source = source_id.load(config, &HashSet::new())?;

debug!("source id loaded: {:?}", source_id);

// Update the index. Without this we would not be able to fetch recent packages if the
// index is not up-to-date.
source.update()?;

Ok(Self {
id: source_id,
source,
Expand Down
100 changes: 67 additions & 33 deletions tests/examples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ mod features {
use std::{
env,
fs::{read_to_string, File},
io::{BufRead, Write},
io::Write,
path::Path,
process::{Command, Stdio},
str,
};

fn test_example2(name: &str, path: &Path, expected_path: &Path, expected_result: bool) {
Expand Down Expand Up @@ -78,37 +79,57 @@ mod features {
cmd.env("RUST_SEMVER_API_GUIDELINES", "true");
}

let output = cmd.output().expect("could not run rust-semverver");
let expected_output = read_to_string(&expected_path)
.expect(&format!(
"could not read expected output from file {}",
expected_path.display()
))
.lines()
.map(|l| l.trim_end())
.map(|l| l.to_string() + "\n")
.collect::<String>()
.trim_end()
.to_string();

let expected_output =
read_to_string(&expected_path).expect("could not read expected output from file");
let output = cmd.output().expect("could not run rust-semverver");

let new_output = output
.stdout
.lines()
.chain(output.stderr.lines())
.map(|r| r.expect("could not read line from rust-semverver output"))
.map(|line| {
// sanitize paths for reproducibility
(match line.find("-->") {
Some(idx) => {
let (start, end) = line.split_at(idx);
match end.find(name) {
Some(idx) => format!("{}--> {}", start, end.split_at(idx).1),
None => line,
let new_output = {
let stdout: &str = str::from_utf8(&output.stdout)
.expect("could not read line from rust-semverver output")
.trim_end();
let stderr: &str = str::from_utf8(&output.stderr)
.expect("could not read line from rust-semverver output")
.trim_end();

stdout
.lines()
.chain(stderr.lines())
.map(|l| l.trim_end())
.map(|line| {
// sanitize paths for reproducibility
(match line.find("-->") {
Some(idx) => {
let (start, end) = line.split_at(idx);
match end.find(name) {
Some(idx) => format!("{}--> {}", start, end.split_at(idx).1),
None => line.to_string(),
}
}
None => line.to_string(),
})
})
.map(|l| {
if cfg!(target_os = "windows") {
l.replace('\\', "/")
} else {
l
}
None => line,
}) + "\n"
})
.map(|l| {
if cfg!(target_os = "windows") {
l.replace('\\', "/")
} else {
l
}
})
.collect::<String>();
})
.map(|l| l + "\n")
.collect::<String>()
.trim_end()
.to_string()
};

if expected_output != new_output {
eprintln!("rust-semverver failed to produce the expected result");
Expand All @@ -117,11 +138,24 @@ mod features {
let mut new_file = File::create(&new_path).unwrap();
new_file.write_all(new_output.as_bytes()).unwrap();

eprintln!(
"For details, try this command: \n\n diff {} {}\n\n",
expected_path.display(),
new_path.display()
);
match std::env::var_os("CI") {
None => {
eprintln!(
"For details, try this command:\n\n diff {} {}\n\n",
expected_path.display(),
new_path.display()
);
}
Some(_) => {
eprintln!("=== Expected output ===");
eprintln!("{}", expected_output);
eprintln!("=== End of expected output ===");
eprintln!("=== Actual output ===");
eprintln!("{}", new_output);
eprintln!("=== End of actual output ===");
}
};

panic!("unexpected output diff");
}

Expand Down
89 changes: 60 additions & 29 deletions tests/full.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,23 @@ mod full {
use std::{
env,
fs::{read_to_string, File},
io::{BufRead, Write},
io::Write,
path::Path,
process::{Command, Stdio},
str,
};

fn test_full(crate_name: &str, old_version: &str, new_version: &str, expected_result: bool) {
// Full test are not working on windows. See issue #105.
if std::env::var_os("CI").is_some() && cfg!(all(target_os = "windows", target_env = "msvc"))
{
eprintln!(
"Skipping full test of crate {} ({} -> {}) on windows. See issue #105.",
crate_name, old_version, new_version
);
return;
}

// Add target dir to PATH so cargo-semver will call the right rust-semverver
if let Some(path) = env::var_os("PATH") {
let mut paths = env::split_paths(&path).collect::<Vec<_>>();
Expand Down Expand Up @@ -58,33 +69,53 @@ mod full {

let expected_path = Path::new("tests/full_cases").join(&filename);

let expected_output =
read_to_string(&expected_path).expect("could not read expected output from file");

let new_output = output
.stdout
let expected_output = read_to_string(&expected_path)
.expect(&format!(
"could not read expected output from file {}",
expected_path.display()
))
.lines()
.chain(output.stderr.lines())
.map(|r| r.expect("could not read line from cargo-semver output"))
.skip_while(|line|
// skip everything before the first important bit of info
!line.starts_with("version bump") &&
// ...unless debugging is enabled
!log_enabled!(Level::Debug))
.map(|line| {
// sanitize paths for reproducibility
(match line.find("-->") {
Some(idx) => {
let (start, end) = line.split_at(idx);
match end.find(crate_name) {
Some(idx) => format!("{}--> {}", start, end.split_at(idx).1),
None => line,
.map(|l| l.trim_end())
.map(|l| l.to_string() + "\n")
.collect::<String>()
.trim_end()
.to_string();

let new_output = {
let stdout: &str = str::from_utf8(&output.stdout)
.expect("could not read line from rust-semverver output")
.trim_end();
let stderr: &str = str::from_utf8(&output.stderr)
.expect("could not read line from rust-semverver output")
.trim_end();

stdout
.lines()
.chain(stderr.lines())
.map(|l| l.trim_end())
.skip_while(|line|
// skip everything before the first important bit of info
!line.starts_with("version bump") &&
// ...unless debugging is enabled
!log_enabled!(Level::Debug))
.map(|line| {
// sanitize paths for reproducibility
(match line.find("-->") {
Some(idx) => {
let (start, end) = line.split_at(idx);
match end.find(crate_name) {
Some(idx) => format!("{}--> {}", start, end.split_at(idx).1),
None => line.to_string(),
}
}
}
None => line,
}) + "\n"
})
.collect::<String>();
None => line.to_string(),
})
})
.map(|l| l + "\n")
.collect::<String>()
.trim_end()
.to_string()
};

if expected_output != new_output {
eprintln!("cargo-semver failed to produce the expected output");
Expand All @@ -96,17 +127,17 @@ mod full {
match std::env::var_os("CI") {
None => {
eprintln!(
"For details, try this command: \n\n diff {} {}\n\n",
"For details, try this command:\n\n diff {} {}\n\n",
expected_path.display(),
new_path.display()
);
}
Some(_) => {
eprintln!("=== Expected output ===");
eprint!("{}", expected_output);
eprintln!("{}", expected_output);
eprintln!("=== End of expected output ===");
eprintln!("=== Actual output ===");
eprint!("{}", new_output);
eprintln!("{}", new_output);
eprintln!("=== End of actual output ===");
}
};
Expand Down

0 comments on commit 625524f

Please sign in to comment.