Skip to content

Commit ef9b838

Browse files
authored
Merge branch 'master' into multiline_assert
2 parents b29b9b7 + 402d845 commit ef9b838

File tree

10 files changed

+147
-85
lines changed

10 files changed

+147
-85
lines changed

Cargo.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cargo/core/shell.rs

Lines changed: 78 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,13 @@ pub enum Verbosity {
1515
}
1616

1717
pub struct Shell {
18-
err: StandardStream,
18+
err: ShellOut,
1919
verbosity: Verbosity,
20-
choice: ColorChoice,
20+
}
21+
22+
enum ShellOut {
23+
Write(Box<Write>),
24+
Stream(StandardStream, ColorChoice),
2125
}
2226

2327
#[derive(PartialEq, Clone, Copy)]
@@ -30,9 +34,18 @@ pub enum ColorChoice {
3034
impl Shell {
3135
pub fn new() -> Shell {
3236
Shell {
33-
err: StandardStream::stderr(ColorChoice::CargoAuto.to_termcolor_color_choice()),
37+
err: ShellOut::Stream(
38+
StandardStream::stderr(ColorChoice::CargoAuto.to_termcolor_color_choice()),
39+
ColorChoice::CargoAuto,
40+
),
41+
verbosity: Verbosity::Verbose,
42+
}
43+
}
44+
45+
pub fn from_write(out: Box<Write>) -> Shell {
46+
Shell {
47+
err: ShellOut::Write(out),
3448
verbosity: Verbosity::Verbose,
35-
choice: ColorChoice::CargoAuto,
3649
}
3750
}
3851

@@ -44,24 +57,13 @@ impl Shell {
4457
match self.verbosity {
4558
Verbosity::Quiet => Ok(()),
4659
_ => {
47-
self.err.reset()?;
48-
self.err.set_color(ColorSpec::new()
49-
.set_bold(true)
50-
.set_fg(Some(color)))?;
51-
if justified {
52-
write!(self.err, "{:>12}", status)?;
53-
} else {
54-
write!(self.err, "{}", status)?;
55-
}
56-
self.err.reset()?;
57-
write!(self.err, " {}\n", message)?;
58-
Ok(())
60+
self.err.print(status, message, color, justified)
5961
}
6062
}
6163
}
6264

63-
pub fn err(&mut self) -> &mut StandardStream {
64-
&mut self.err
65+
pub fn err(&mut self) -> &mut Write {
66+
self.err.as_write()
6567
}
6668

6769
pub fn status<T, U>(&mut self, status: T, message: U) -> CargoResult<()>
@@ -117,23 +119,68 @@ impl Shell {
117119
}
118120

119121
pub fn set_color_choice(&mut self, color: Option<&str>) -> CargoResult<()> {
120-
let cfg = match color {
121-
Some("always") => ColorChoice::Always,
122-
Some("never") => ColorChoice::Never,
122+
if let ShellOut::Stream(ref mut err, ref mut cc) = self.err {
123+
let cfg = match color {
124+
Some("always") => ColorChoice::Always,
125+
Some("never") => ColorChoice::Never,
126+
127+
Some("auto") |
128+
None => ColorChoice::CargoAuto,
129+
130+
Some(arg) => bail!("argument for --color must be auto, always, or \
131+
never, but found `{}`", arg),
132+
};
133+
*cc = cfg;
134+
*err = StandardStream::stderr(cfg.to_termcolor_color_choice());
135+
}
136+
Ok(())
137+
}
123138

124-
Some("auto") |
125-
None => ColorChoice::CargoAuto,
139+
pub fn color_choice(&self) -> ColorChoice {
140+
match self.err {
141+
ShellOut::Stream(_, cc) => cc,
142+
ShellOut::Write(_) => ColorChoice::Never,
143+
}
144+
}
145+
}
126146

127-
Some(arg) => bail!("argument for --color must be auto, always, or \
128-
never, but found `{}`", arg),
129-
};
130-
self.choice = cfg;
131-
self.err = StandardStream::stderr(cfg.to_termcolor_color_choice());
132-
return Ok(());
147+
impl ShellOut {
148+
fn print(&mut self,
149+
status: &fmt::Display,
150+
message: &fmt::Display,
151+
color: Color,
152+
justified: bool) -> CargoResult<()> {
153+
match *self {
154+
ShellOut::Stream(ref mut err, _) => {
155+
err.reset()?;
156+
err.set_color(ColorSpec::new()
157+
.set_bold(true)
158+
.set_fg(Some(color)))?;
159+
if justified {
160+
write!(err, "{:>12}", status)?;
161+
} else {
162+
write!(err, "{}", status)?;
163+
}
164+
err.reset()?;
165+
write!(err, " {}\n", message)?;
166+
}
167+
ShellOut::Write(ref mut w) => {
168+
if justified {
169+
write!(w, "{:>12}", status)?;
170+
} else {
171+
write!(w, "{}", status)?;
172+
}
173+
write!(w, " {}\n", message)?;
174+
}
175+
}
176+
Ok(())
133177
}
134178

135-
pub fn color_choice(&self) -> ColorChoice {
136-
self.choice
179+
fn as_write(&mut self) -> &mut Write {
180+
match *self {
181+
ShellOut::Stream(ref mut err, _) => err,
182+
ShellOut::Write(ref mut w) => w,
183+
}
137184
}
138185
}
139186

src/cargo/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ extern crate termcolor;
3333
extern crate toml;
3434
extern crate url;
3535

36-
use std::io::Write;
3736
use std::fmt;
3837
use std::error::Error;
3938

src/cargo/ops/cargo_new.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use std::collections::BTreeMap;
22
use std::env;
33
use std::fs;
4-
use std::io::Write;
54
use std::path::Path;
65

76
use serde::{Deserialize, Deserializer};

src/cargo/ops/cargo_rustc/job_queue.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::collections::HashSet;
22
use std::collections::hash_map::HashMap;
33
use std::fmt;
4-
use std::io::{self, Write};
4+
use std::io;
55
use std::mem;
66
use std::sync::mpsc::{channel, Sender, Receiver};
77

@@ -113,7 +113,7 @@ impl<'a> JobQueue<'a> {
113113
let _p = profile::start("executing the job graph");
114114

115115
// We need to give a handle to the send half of our message queue to the
116-
// jobserver helper thrad. Unfortunately though we need the handle to be
116+
// jobserver helper thread. Unfortunately though we need the handle to be
117117
// `'static` as that's typically what's required when spawning a
118118
// thread!
119119
//

src/cargo/ops/registry.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use std::env;
22
use std::fs::{self, File};
3-
use std::io::Write;
43
use std::iter::repeat;
54
use std::time::Duration;
65

src/cargo/util/toml.rs

Lines changed: 13 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1415,12 +1415,9 @@ fn normalize(package_root: &Path,
14151415

14161416
if let Some(ref lib) = *lib {
14171417
lib_target(&mut ret, lib);
1418-
bin_targets(&mut ret, bins,
1419-
&mut |bin| inferred_bin_path(bin, package_root, true, bins.len()));
1420-
} else if bins.len() > 0 {
1421-
bin_targets(&mut ret, bins,
1422-
&mut |bin| inferred_bin_path(bin, package_root, false, bins.len()));
14231418
}
1419+
bin_targets(&mut ret, bins,
1420+
&mut |bin| inferred_bin_path(bin, lib.is_some(), package_root, bins.len()));
14241421

14251422

14261423
if let Some(custom_build) = custom_build {
@@ -1443,32 +1440,9 @@ fn normalize(package_root: &Path,
14431440
}
14441441

14451442
fn inferred_bin_path(bin: &TomlBinTarget,
1443+
has_lib: bool,
14461444
package_root: &Path,
1447-
lib: bool,
14481445
bin_len: usize) -> PathBuf {
1449-
// we have a lib with multiple bins, so the bins are expected to be located
1450-
// inside src/bin
1451-
if lib && bin_len > 1 {
1452-
return Path::new("src").join("bin").join(&format!("{}.rs", bin.name()))
1453-
.to_path_buf()
1454-
}
1455-
1456-
// we have a lib with one bin, so it's either src/main.rs, src/bin/foo.rs or
1457-
// src/bin/main.rs
1458-
if lib && bin_len == 1 {
1459-
let path = Path::new("src").join(&format!("main.rs"));
1460-
if package_root.join(&path).exists() {
1461-
return path.to_path_buf()
1462-
}
1463-
1464-
let path = Path::new("src").join("bin").join(&format!("{}.rs", bin.name()));
1465-
if package_root.join(&path).exists() {
1466-
return path.to_path_buf()
1467-
}
1468-
1469-
return Path::new("src").join("bin").join(&format!("main.rs")).to_path_buf()
1470-
}
1471-
14721446
// here we have a single bin, so it may be located in src/main.rs, src/foo.rs,
14731447
// srb/bin/foo.rs or src/bin/main.rs
14741448
if bin_len == 1 {
@@ -1477,9 +1451,11 @@ fn inferred_bin_path(bin: &TomlBinTarget,
14771451
return path.to_path_buf()
14781452
}
14791453

1480-
let path = Path::new("src").join(&format!("{}.rs", bin.name()));
1481-
if package_root.join(&path).exists() {
1482-
return path.to_path_buf()
1454+
if !has_lib {
1455+
let path = Path::new("src").join(&format!("{}.rs", bin.name()));
1456+
if package_root.join(&path).exists() {
1457+
return path.to_path_buf()
1458+
}
14831459
}
14841460

14851461
let path = Path::new("src").join("bin").join(&format!("{}.rs", bin.name()));
@@ -1496,9 +1472,11 @@ fn inferred_bin_path(bin: &TomlBinTarget,
14961472
return path.to_path_buf()
14971473
}
14981474

1499-
let path = Path::new("src").join(&format!("{}.rs", bin.name()));
1500-
if package_root.join(&path).exists() {
1501-
return path.to_path_buf()
1475+
if !has_lib {
1476+
let path = Path::new("src").join(&format!("{}.rs", bin.name()));
1477+
if package_root.join(&path).exists() {
1478+
return path.to_path_buf()
1479+
}
15021480
}
15031481

15041482
let path = Path::new("src").join("bin").join(&format!("main.rs"));

tests/bench.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -242,11 +242,10 @@ fn cargo_bench_failing_test() {
242242
[FINISHED] release [optimized] target(s) in [..]
243243
[RUNNING] target[/]release[/]deps[/]foo-[..][EXE]
244244
thread '[..]' panicked at 'assertion failed: \
245-
`(left == right)`
246-
left: `\"hello\"`,
247-
right: `\"nope\"`', src[/]foo.rs:14
248-
[..]
249-
", p.url()))
245+
`(left == right)`[..]", p.url()))
246+
.with_stderr_contains("[..]left: `\"hello\"`[..]")
247+
.with_stderr_contains("[..]right: `\"nope\"`[..]")
248+
.with_stderr_contains("[..]src[/]foo.rs:14")
250249
.with_status(101));
251250
}
252251

tests/build.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3229,3 +3229,45 @@ fn deterministic_cfg_flags() {
32293229
--cfg cfg_a --cfg cfg_b --cfg cfg_c --cfg cfg_d --cfg cfg_e`
32303230
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]"));
32313231
}
3232+
3233+
#[test]
3234+
fn explicit_bins_without_paths() {
3235+
let p = project("foo")
3236+
.file("Cargo.toml", r#"
3237+
[package]
3238+
name = "foo"
3239+
version = "0.1.0"
3240+
authors = []
3241+
3242+
[[bin]]
3243+
name = "foo"
3244+
3245+
[[bin]]
3246+
name = "bar"
3247+
"#)
3248+
.file("src/lib.rs", "")
3249+
.file("src/main.rs", "fn main() {}")
3250+
.file("src/bin/bar.rs", "fn main() {}");
3251+
3252+
assert_that(p.cargo_process("build"), execs().with_status(0));
3253+
}
3254+
3255+
#[test]
3256+
fn no_bin_in_src_with_lib() {
3257+
let p = project("foo")
3258+
.file("Cargo.toml", r#"
3259+
[package]
3260+
name = "foo"
3261+
version = "0.1.0"
3262+
authors = []
3263+
3264+
[[bin]]
3265+
name = "foo"
3266+
"#)
3267+
.file("src/lib.rs", "")
3268+
.file("src/foo.rs", "fn main() {}");
3269+
3270+
assert_that(p.cargo_process("build"),
3271+
execs().with_status(101)
3272+
.with_stderr_contains(r#"[ERROR] couldn't read "[..]main.rs"[..]"#));
3273+
}

tests/cargotest/support/mod.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -436,13 +436,12 @@ impl Execs {
436436
}
437437

438438
if let Some(ref objects) = self.expect_json {
439-
let lines = match str::from_utf8(&actual.stdout) {
440-
Err(..) => return Err("stdout was not utf8 encoded".to_owned()),
441-
Ok(stdout) => stdout.lines().collect::<Vec<_>>(),
442-
};
439+
let stdout = str::from_utf8(&actual.stdout)
440+
.map_err(|_| "stdout was not utf8 encoded".to_owned())?;
441+
let lines = stdout.lines().collect::<Vec<_>>();
443442
if lines.len() != objects.len() {
444-
return Err(format!("expected {} json lines, got {}",
445-
objects.len(), lines.len()));
443+
return Err(format!("expected {} json lines, got {}, stdout:\n{}",
444+
objects.len(), lines.len(), stdout));
446445
}
447446
for (obj, line) in objects.iter().zip(lines) {
448447
self.match_json(obj, line)?;

0 commit comments

Comments
 (0)