Skip to content

Commit 625524f

Browse files
authored
Merge pull request rust-lang#104 from orium/make-it-work
Fix tests.
2 parents 6a9aaaa + 26073be commit 625524f

File tree

5 files changed

+134
-69
lines changed

5 files changed

+134
-69
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ name = "rust-semverver"
2323
path = "src/bin/rust_semverver.rs"
2424

2525
[dependencies]
26-
cargo = "0.35"
27-
crates-io = "0.23"
26+
cargo = "0.36"
27+
crates-io = "0.24"
2828
curl = "0.4.21"
2929
env_logger = "0.6"
3030
failure = "0.1"

ci/run.sh

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22

33
set -ex
44

5-
# Note: this is required for correctness,
6-
# otherwise executing multiple "full" tests in parallel
7-
# of the same library can alter results.
8-
export RUST_TEST_THREADS=1
95
export RUST_BACKTRACE=full
106
#export RUST_TEST_NOCAPTURE=1
117

src/bin/cargo_semver.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,10 +352,14 @@ impl<'a> SourceInfo<'a> {
352352
/// Construct a new source info for `crates.io`.
353353
pub fn new(config: &'a cargo::Config) -> Result<SourceInfo<'a>> {
354354
let source_id = SourceId::crates_io(config)?;
355-
let source = source_id.load(config, &HashSet::new())?;
355+
let mut source = source_id.load(config, &HashSet::new())?;
356356

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

359+
// Update the index. Without this we would not be able to fetch recent packages if the
360+
// index is not up-to-date.
361+
source.update()?;
362+
359363
Ok(Self {
360364
id: source_id,
361365
source,

tests/examples.rs

Lines changed: 67 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ mod features {
22
use std::{
33
env,
44
fs::{read_to_string, File},
5-
io::{BufRead, Write},
5+
io::Write,
66
path::Path,
77
process::{Command, Stdio},
8+
str,
89
};
910

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

81-
let output = cmd.output().expect("could not run rust-semverver");
82+
let expected_output = read_to_string(&expected_path)
83+
.expect(&format!(
84+
"could not read expected output from file {}",
85+
expected_path.display()
86+
))
87+
.lines()
88+
.map(|l| l.trim_end())
89+
.map(|l| l.to_string() + "\n")
90+
.collect::<String>()
91+
.trim_end()
92+
.to_string();
8293

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

86-
let new_output = output
87-
.stdout
88-
.lines()
89-
.chain(output.stderr.lines())
90-
.map(|r| r.expect("could not read line from rust-semverver output"))
91-
.map(|line| {
92-
// sanitize paths for reproducibility
93-
(match line.find("-->") {
94-
Some(idx) => {
95-
let (start, end) = line.split_at(idx);
96-
match end.find(name) {
97-
Some(idx) => format!("{}--> {}", start, end.split_at(idx).1),
98-
None => line,
96+
let new_output = {
97+
let stdout: &str = str::from_utf8(&output.stdout)
98+
.expect("could not read line from rust-semverver output")
99+
.trim_end();
100+
let stderr: &str = str::from_utf8(&output.stderr)
101+
.expect("could not read line from rust-semverver output")
102+
.trim_end();
103+
104+
stdout
105+
.lines()
106+
.chain(stderr.lines())
107+
.map(|l| l.trim_end())
108+
.map(|line| {
109+
// sanitize paths for reproducibility
110+
(match line.find("-->") {
111+
Some(idx) => {
112+
let (start, end) = line.split_at(idx);
113+
match end.find(name) {
114+
Some(idx) => format!("{}--> {}", start, end.split_at(idx).1),
115+
None => line.to_string(),
116+
}
99117
}
118+
None => line.to_string(),
119+
})
120+
})
121+
.map(|l| {
122+
if cfg!(target_os = "windows") {
123+
l.replace('\\', "/")
124+
} else {
125+
l
100126
}
101-
None => line,
102-
}) + "\n"
103-
})
104-
.map(|l| {
105-
if cfg!(target_os = "windows") {
106-
l.replace('\\', "/")
107-
} else {
108-
l
109-
}
110-
})
111-
.collect::<String>();
127+
})
128+
.map(|l| l + "\n")
129+
.collect::<String>()
130+
.trim_end()
131+
.to_string()
132+
};
112133

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

120-
eprintln!(
121-
"For details, try this command: \n\n diff {} {}\n\n",
122-
expected_path.display(),
123-
new_path.display()
124-
);
141+
match std::env::var_os("CI") {
142+
None => {
143+
eprintln!(
144+
"For details, try this command:\n\n diff {} {}\n\n",
145+
expected_path.display(),
146+
new_path.display()
147+
);
148+
}
149+
Some(_) => {
150+
eprintln!("=== Expected output ===");
151+
eprintln!("{}", expected_output);
152+
eprintln!("=== End of expected output ===");
153+
eprintln!("=== Actual output ===");
154+
eprintln!("{}", new_output);
155+
eprintln!("=== End of actual output ===");
156+
}
157+
};
158+
125159
panic!("unexpected output diff");
126160
}
127161

tests/full.rs

Lines changed: 60 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,23 @@ mod full {
33
use std::{
44
env,
55
fs::{read_to_string, File},
6-
io::{BufRead, Write},
6+
io::Write,
77
path::Path,
88
process::{Command, Stdio},
9+
str,
910
};
1011

1112
fn test_full(crate_name: &str, old_version: &str, new_version: &str, expected_result: bool) {
13+
// Full test are not working on windows. See issue #105.
14+
if std::env::var_os("CI").is_some() && cfg!(all(target_os = "windows", target_env = "msvc"))
15+
{
16+
eprintln!(
17+
"Skipping full test of crate {} ({} -> {}) on windows. See issue #105.",
18+
crate_name, old_version, new_version
19+
);
20+
return;
21+
}
22+
1223
// Add target dir to PATH so cargo-semver will call the right rust-semverver
1324
if let Some(path) = env::var_os("PATH") {
1425
let mut paths = env::split_paths(&path).collect::<Vec<_>>();
@@ -58,33 +69,53 @@ mod full {
5869

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

61-
let expected_output =
62-
read_to_string(&expected_path).expect("could not read expected output from file");
63-
64-
let new_output = output
65-
.stdout
72+
let expected_output = read_to_string(&expected_path)
73+
.expect(&format!(
74+
"could not read expected output from file {}",
75+
expected_path.display()
76+
))
6677
.lines()
67-
.chain(output.stderr.lines())
68-
.map(|r| r.expect("could not read line from cargo-semver output"))
69-
.skip_while(|line|
70-
// skip everything before the first important bit of info
71-
!line.starts_with("version bump") &&
72-
// ...unless debugging is enabled
73-
!log_enabled!(Level::Debug))
74-
.map(|line| {
75-
// sanitize paths for reproducibility
76-
(match line.find("-->") {
77-
Some(idx) => {
78-
let (start, end) = line.split_at(idx);
79-
match end.find(crate_name) {
80-
Some(idx) => format!("{}--> {}", start, end.split_at(idx).1),
81-
None => line,
78+
.map(|l| l.trim_end())
79+
.map(|l| l.to_string() + "\n")
80+
.collect::<String>()
81+
.trim_end()
82+
.to_string();
83+
84+
let new_output = {
85+
let stdout: &str = str::from_utf8(&output.stdout)
86+
.expect("could not read line from rust-semverver output")
87+
.trim_end();
88+
let stderr: &str = str::from_utf8(&output.stderr)
89+
.expect("could not read line from rust-semverver output")
90+
.trim_end();
91+
92+
stdout
93+
.lines()
94+
.chain(stderr.lines())
95+
.map(|l| l.trim_end())
96+
.skip_while(|line|
97+
// skip everything before the first important bit of info
98+
!line.starts_with("version bump") &&
99+
// ...unless debugging is enabled
100+
!log_enabled!(Level::Debug))
101+
.map(|line| {
102+
// sanitize paths for reproducibility
103+
(match line.find("-->") {
104+
Some(idx) => {
105+
let (start, end) = line.split_at(idx);
106+
match end.find(crate_name) {
107+
Some(idx) => format!("{}--> {}", start, end.split_at(idx).1),
108+
None => line.to_string(),
109+
}
82110
}
83-
}
84-
None => line,
85-
}) + "\n"
86-
})
87-
.collect::<String>();
111+
None => line.to_string(),
112+
})
113+
})
114+
.map(|l| l + "\n")
115+
.collect::<String>()
116+
.trim_end()
117+
.to_string()
118+
};
88119

89120
if expected_output != new_output {
90121
eprintln!("cargo-semver failed to produce the expected output");
@@ -96,17 +127,17 @@ mod full {
96127
match std::env::var_os("CI") {
97128
None => {
98129
eprintln!(
99-
"For details, try this command: \n\n diff {} {}\n\n",
130+
"For details, try this command:\n\n diff {} {}\n\n",
100131
expected_path.display(),
101132
new_path.display()
102133
);
103134
}
104135
Some(_) => {
105136
eprintln!("=== Expected output ===");
106-
eprint!("{}", expected_output);
137+
eprintln!("{}", expected_output);
107138
eprintln!("=== End of expected output ===");
108139
eprintln!("=== Actual output ===");
109-
eprint!("{}", new_output);
140+
eprintln!("{}", new_output);
110141
eprintln!("=== End of actual output ===");
111142
}
112143
};

0 commit comments

Comments
 (0)