Skip to content

Commit

Permalink
Auto merge of #2779 - matklad:propagete-color-config, r=alexcrichton
Browse files Browse the repository at this point in the history
Propagate --color option to rustc

closes #2740

Will try to add a test for this soon (and fix failing tests if any, compiling/running tests locally is slow :( ).

I am not sure what is the right place to add `--color` option to the command line. I use [`build_base_args`]. [`process`] also looks like a good candidate, because it is more general, but if we look at the [`CommandType`] we see that only `rustc` command supports `--color`.

[`build_base_args`]: https://github.com/matklad/cargo/blob/1f7504397ce7c40ff708e2d31da164822e88ed37/src/cargo/ops/cargo_rustc/mod.rs#L449
[`process`]: https://github.com/matklad/cargo/blob/1f7504397ce7c40ff708e2d31da164822e88ed37/src/cargo/ops/cargo_rustc/mod.rs#L608
[`CommandType`]: https://github.com/matklad/cargo/blob/1f7504397ce7c40ff708e2d31da164822e88ed37/src/cargo/ops/cargo_rustc/engine.rs#L102
  • Loading branch information
bors committed Jun 10, 2016
2 parents 22f9cff + 76a48a9 commit 13861b7
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/cargo/core/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ pub enum ColorConfig {
Never
}

impl fmt::Display for ColorConfig {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
ColorConfig::Auto => "auto",
ColorConfig::Always => "always",
ColorConfig::Never => "never",
}.fmt(f)
}
}

#[derive(Clone, Copy)]
pub struct ShellConfig {
pub color_config: ColorConfig,
Expand Down Expand Up @@ -129,6 +139,11 @@ impl MultiShell {
pub fn get_verbose(&self) -> Verbosity {
self.verbosity
}

pub fn color_config(&self) -> ColorConfig {
assert!(self.out.config.color_config == self.err.config.color_config);
self.out.config.color_config
}
}

impl Shell {
Expand Down
6 changes: 6 additions & 0 deletions src/cargo/ops/cargo_rustc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::sync::Arc;

use core::{Package, PackageId, PackageSet, Target, Resolve};
use core::{Profile, Profiles};
use core::shell::ColorConfig;
use util::{self, CargoResult, human};
use util::{Config, internal, ChainError, profile, join_paths};

Expand Down Expand Up @@ -461,6 +462,11 @@ fn build_base_args(cx: &Context,

cmd.arg(&root_path(cx, unit));

let color_config = cx.config.shell().color_config();
if color_config != ColorConfig::Auto {
cmd.arg("--color").arg(&color_config.to_string());
}

cmd.arg("--crate-name").arg(&unit.target.crate_name());

for crate_type in crate_types.iter() {
Expand Down
28 changes: 28 additions & 0 deletions tests/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2212,3 +2212,31 @@ fn panic_abort_compiles_with_panic_abort() {
execs().with_status(0)
.with_stderr_contains("[..] -C panic=abort [..]"));
}

#[test]
fn explicit_color_config_is_propagated_to_rustc() {
let mut p = project("foo");
p = p
.file("Cargo.toml", r#"
[package]
name = "test"
version = "0.0.0"
authors = []
"#)
.file("src/lib.rs", "");

assert_that(p.cargo_process("build").arg("-v").arg("--color").arg("always"),
execs().with_status(0).with_stderr_contains(
"[..]rustc src[..]lib.rs --color always[..]"));

assert_that(p.cargo_process("build").arg("-v").arg("--color").arg("never"),
execs().with_status(0).with_stderr("\
[COMPILING] test v0.0.0 ([..])
[RUNNING] `rustc src[..]lib.rs --color never --crate-name test --crate-type lib -g \
--out-dir [..]target[..]debug \
--emit=dep-info,link \
-L dependency=[..]target[..]debug \
-L dependency=[..]target[..]debug[..]deps`
"));
}

0 comments on commit 13861b7

Please sign in to comment.