Skip to content
This repository was archived by the owner on Apr 5, 2024. It is now read-only.

Commit bd29cd6

Browse files
authored
Merge pull request #93 from gnzlbg/fix_exit_code
Fix exit code of rust-semverver in tests
2 parents 3f703b6 + 8bbf05b commit bd29cd6

10 files changed

+139
-81
lines changed

ci/run.sh

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
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.
58
export RUST_TEST_THREADS=1
69
export RUST_BACKTRACE=full
710
#export RUST_TEST_NOCAPTURE=1

src/bin/cargo_semver.rs

+40-14
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ use cargo::core::{Package, PackageId, PackageSet, Source, SourceId, SourceMap, W
1111
use log::debug;
1212
use std::{
1313
env,
14+
fs::File,
1415
io::BufReader,
1516
io::Write,
16-
fs::File,
1717
path::{Path, PathBuf},
1818
process::{Command, Stdio},
1919
};
@@ -23,6 +23,7 @@ pub type Result<T> = cargo::util::CargoResult<T>;
2323
#[derive(Debug, Deserialize)]
2424
struct Invocation {
2525
package_name: String,
26+
target_kind: Vec<String>,
2627
outputs: Vec<PathBuf>,
2728
}
2829

@@ -120,8 +121,10 @@ fn run(config: &cargo::Config, matches: &getopts::Matches, explain: bool) -> Res
120121
(work_info, stable_crate.max_version.clone())
121122
};
122123

123-
let (current_rlib, current_deps_output) = current.rlib_and_dep_output(config, &name, true)?;
124-
let (stable_rlib, stable_deps_output) = stable.rlib_and_dep_output(config, &name, false)?;
124+
let (current_rlib, current_deps_output) =
125+
current.rlib_and_dep_output(config, &name, true, matches)?;
126+
let (stable_rlib, stable_deps_output) =
127+
stable.rlib_and_dep_output(config, &name, false, matches)?;
125128

126129
println!("current_rlib: {:?}", current_rlib);
127130
println!("stable_rlib: {:?}", stable_rlib);
@@ -151,6 +154,10 @@ fn run(config: &cargo::Config, matches: &getopts::Matches, explain: bool) -> Res
151154
child.args(&["--target", &target]);
152155
}
153156

157+
if !matches.opt_present("no-default-features") {
158+
child.args(&["--cfg", "feature=\"default\""]);
159+
}
160+
154161
let child = child
155162
.arg("-")
156163
.stdin(Stdio::piped())
@@ -179,14 +186,20 @@ fn run(config: &cargo::Config, matches: &getopts::Matches, explain: bool) -> Res
179186
extern crate new;"
180187
))?;
181188
} else {
182-
return Err(failure::err_msg("could not pipe to rustc (wtf?)".to_owned()).into());
189+
return Err(failure::err_msg(
190+
"could not pipe to rustc (wtf?)".to_owned(),
191+
));
183192
}
184193

185-
child
194+
let exit_status = child
186195
.wait()
187196
.map_err(|e| failure::err_msg(format!("failed to wait for rustc: {}", e)))?;
188197

189-
Ok(())
198+
if exit_status.success() {
199+
Ok(())
200+
} else {
201+
Err(failure::err_msg("rustc-semverver errored".to_owned()))
202+
}
190203
}
191204

192205
/// CLI utils
@@ -207,6 +220,11 @@ mod cli {
207220
"api-guidelines",
208221
"report only changes that are breaking according to the API-guidelines",
209222
);
223+
opts.optflag(
224+
"",
225+
"no-default-features",
226+
"Do not activate the `default` feature",
227+
);
210228
opts.optopt(
211229
"s",
212230
"stable-path",
@@ -379,15 +397,24 @@ impl<'a> WorkInfo<'a> {
379397
config: &'a cargo::Config,
380398
name: &str,
381399
current: bool,
400+
matches: &getopts::Matches,
382401
) -> Result<(PathBuf, PathBuf)> {
383402
let mut opts =
384403
cargo::ops::CompileOptions::new(config, cargo::core::compiler::CompileMode::Build)?;
385404
// we need the build plan to find our build artifacts
386405
opts.build_config.build_plan = true;
406+
407+
if let Some(target) = matches.opt_str("target") {
408+
opts.build_config.requested_target = Some(target);
409+
}
410+
opts.no_default_features = matches.opt_present("no-default-features");
411+
387412
// TODO: this is where we could insert feature flag builds (or using the CLI mechanisms)
388413

389-
env::set_var("RUSTFLAGS",
390-
format!("-C metadata={}", if current { "new" } else { "old" }));
414+
env::set_var(
415+
"RUSTFLAGS",
416+
format!("-C metadata={}", if current { "new" } else { "old" }),
417+
);
391418

392419
let mut outdir = env::temp_dir();
393420
outdir.push(&format!("cargo_semver_{}_{}", name, current));
@@ -406,13 +433,14 @@ impl<'a> WorkInfo<'a> {
406433
let compilation = cargo::ops::compile(&self.workspace, &opts)?;
407434
env::remove_var("RUSTFLAGS");
408435

409-
let build_plan: BuildPlan =
410-
serde_json::from_reader(BufReader::new(File::open(&outdir)?))?;
436+
let build_plan: BuildPlan = serde_json::from_reader(BufReader::new(File::open(&outdir)?))?;
411437

412438
// TODO: handle multiple outputs gracefully
413439
for i in &build_plan.invocations {
414-
if i.package_name == name {
415-
return Ok((i.outputs[0].clone(), compilation.deps_output));
440+
if let Some(kind) = i.target_kind.get(0) {
441+
if kind.contains("lib") && i.package_name == name {
442+
return Ok((i.outputs[0].clone(), compilation.deps_output));
443+
}
416444
}
417445
}
418446

@@ -433,15 +461,13 @@ pub fn find_on_crates_io(crate_name: &str) -> Result<crates_io::Crate> {
433461
"failed to retrieve search results from the registry: {}",
434462
e
435463
))
436-
.into()
437464
})
438465
.and_then(|(mut crates, _)| {
439466
crates
440467
.drain(..)
441468
.find(|krate| krate.name == crate_name)
442469
.ok_or_else(|| {
443470
failure::err_msg(format!("failed to find a matching crate `{}`", crate_name))
444-
.into()
445471
})
446472
})
447473
}

src/bin/rust_semverver.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#![feature(rustc_private)]
2-
#![feature(try_from)]
32

43
extern crate getopts;
54
extern crate rustc;

src/changes.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -875,7 +875,7 @@ impl<'tcx> ChangeSet<'tcx> {
875875
pub fn trait_item_breaking(&self, old: DefId) -> bool {
876876
self.changes
877877
.get(&old)
878-
.map_or(false, |change| change.trait_item_breaking())
878+
.map_or(false, Change::trait_item_breaking)
879879
}
880880

881881
/// Format the contents of a change set for user output.

tests/cases/regions/stdout

+17-1
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,21 @@ error: breaking changes in `def`
3131
|
3232
= warning: type error: expected reference, found bool (breaking)
3333

34-
error: aborting due to 4 previous errors
34+
error: breaking changes in `efg`
35+
--> $REPO_PATH/tests/cases/regions/new.rs:17:1
36+
|
37+
17 | pub fn efg(_: &str) { }
38+
| ^^^^^^^^^^^^^^^^^^^^^^^
39+
|
40+
= warning: type error: expected bound lifetime parameterBrAnon(0), found concrete lifetime (breaking)
41+
42+
error: breaking changes in `fgh`
43+
--> $REPO_PATH/tests/cases/regions/new.rs:19:1
44+
|
45+
19 | pub fn fgh(_: &'static str) { }
46+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
47+
|
48+
= warning: type error: expected bound lifetime parameterBrAnon(0), found concrete lifetime (breaking)
49+
50+
error: aborting due to 6 previous errors
3551

tests/examples.rs

+52-50
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ mod features {
55
process::{Command, Stdio},
66
};
77

8-
fn test_example(path: &Path, out_file: &Path) {
9-
let mut success = true;
10-
8+
fn test_example(path: &Path, out_file: &Path, expected_result: bool) {
119
let old_rlib = path.join("libold.rlib").to_str().unwrap().to_owned();
1210
let new_rlib = path.join("libnew.rlib").to_str().unwrap().to_owned();
1311

@@ -29,8 +27,8 @@ mod features {
2927
cmd.args(target_args);
3028
}
3129

32-
success &= cmd.status().expect("could not run rustc").success();
33-
assert!(success, "couldn't compile old");
30+
let rustc_old_result = cmd.status().expect("could not run rustc on old").success();
31+
assert!(rustc_old_result, "couldn't compile old");
3432

3533
let mut cmd = Command::new("rustc");
3634
cmd.args(&["--crate-type=lib", "-o", &new_rlib])
@@ -42,9 +40,8 @@ mod features {
4240
cmd.args(target_args);
4341
}
4442

45-
success &= cmd.status().expect("could not run rustc").success();
46-
47-
assert!(success, "couldn't compile new");
43+
let rustc_new_result = cmd.status().expect("could not run rustc on new").success();
44+
assert!(rustc_new_result, "couldn't compile new");
4845

4946
let mut cmd = Command::new(
5047
Path::new(".")
@@ -81,12 +78,14 @@ mod features {
8178
cmd.env("RUST_SEMVER_API_GUIDELINES", "true");
8279
}
8380

84-
success &= cmd
81+
let rustsemverver_result = cmd
8582
.status()
8683
.expect("could not run rust-semverver")
8784
.success();
88-
89-
assert!(success, "rust-semverver");
85+
assert_eq!(
86+
rustsemverver_result, expected_result,
87+
"rust-semverver returned an unexpected exit status"
88+
);
9089

9190
{
9291
// replace root path with with $REPO_PATH
@@ -122,7 +121,7 @@ mod features {
122121
}
123122
}
124123

125-
success &= Command::new("git")
124+
let git_result = Command::new("git")
126125
.args(&[
127126
"diff",
128127
"--ignore-space-at-eol",
@@ -134,7 +133,7 @@ mod features {
134133
.expect("could not run git diff")
135134
.success();
136135

137-
assert!(success, "git");
136+
assert!(git_result, "git reports unexpected diff");
138137

139138
Command::new("rm")
140139
.args(&[&old_rlib, &new_rlib])
@@ -143,54 +142,57 @@ mod features {
143142
}
144143

145144
macro_rules! test {
146-
($name:ident) => {
145+
($name:ident => $result:literal) => {
147146
#[test]
148147
fn $name() {
149148
let path = Path::new("tests").join("cases").join(stringify!($name));
150-
test_example(&path, &path.join("stdout"));
149+
test_example(&path, &path.join("stdout"), $result);
151150

152151
if path.join("stdout_api_guidelines").exists() {
153152
eprintln!("api-guidelines");
154-
test_example(&path, &path.join("stdout_api_guidelines"));
153+
test_example(&path, &path.join("stdout_api_guidelines"), $result);
155154
}
156155
}
157156
};
158-
($($name:ident),*) => {
159-
$(test!($name);)*
160-
}
157+
($($name:ident => $result:literal),*) => {
158+
$(test!($name => $result);)*
159+
};
160+
($($name:ident => $result:literal,)*) => {
161+
$(test!($name => $result);)*
162+
};
161163
}
162164

163165
test! {
164-
addition,
165-
addition_path,
166-
addition_use,
167-
bounds,
168-
circular,
169-
consts,
170-
enums,
171-
func,
172-
func_local_items,
173-
infer,
174-
infer_regress,
175-
inherent_impls,
176-
issue_34,
177-
issue_50,
178-
kind_change,
179-
macros,
180-
max_priv,
181-
mix,
182-
pathologic_paths,
183-
pub_use,
184-
regions,
185-
removal,
186-
removal_path,
187-
removal_use,
188-
sealed_traits,
189-
structs,
190-
swap,
191-
traits,
192-
trait_impls,
193-
trait_objects,
194-
ty_alias
166+
addition => true,
167+
addition_path => true,
168+
addition_use => false,
169+
bounds => false,
170+
circular => true,
171+
consts => false,
172+
enums => false,
173+
func => false,
174+
func_local_items => true,
175+
infer => true,
176+
infer_regress => false,
177+
inherent_impls => false,
178+
issue_34 => true,
179+
issue_50 => true,
180+
kind_change => false,
181+
macros => false,
182+
max_priv => true,
183+
mix => false,
184+
pathologic_paths => true,
185+
pub_use => true,
186+
regions => false,
187+
removal => false,
188+
removal_path => false,
189+
removal_use => false,
190+
sealed_traits => true,
191+
structs => false,
192+
swap => true,
193+
traits => false,
194+
trait_impls => false,
195+
trait_objects => true,
196+
ty_alias => false,
195197
}
196198
}

0 commit comments

Comments
 (0)