Skip to content

Commit

Permalink
Merge #62
Browse files Browse the repository at this point in the history
62: Change output directory of --html/--open to target/llvm-cov/html r=taiki-e a=taiki-e

Change output directory of `--html` and `--open` options from `target/llvm-cov` to `target/llvm-cov/html`.

Prepare for #60.

Co-authored-by: Taiki Endo <te316e89@gmail.com>
  • Loading branch information
bors[bot] and taiki-e authored Aug 15, 2021
2 parents 602969c + 8378da6 commit acff245
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 19 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ Note: In this file, do not use the hard wrap in the middle of a sentence for com

- [Fix bug in `--exclude` and `--package` options](https://github.com/taiki-e/cargo-llvm-cov/pull/56)

- Change output directory of `--html` and `--open` options from `target/llvm-cov` to `target/llvm-cov/html`.

- `--html` and `--open` options no longer outputs a summary at the same time.

- [Recognize rustflags and rustdocflags set by config file.](https://github.com/taiki-e/cargo-llvm-cov/pull/52)
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ OPTIONS:

--html
Generate coverage reports in "html" format. If --output-dir is not specified, the report
will be generated in `target/llvm-cov` directory.
will be generated in `target/llvm-cov/html` directory.

This internally calls `llvm-cov show -format=html`. See
<https://llvm.org/docs/CommandGuide/llvm-cov.html#llvm-cov-show> for more.
Expand Down Expand Up @@ -205,11 +205,11 @@ By default, only the summary is printed to stdout.
cargo llvm-cov
```

With html report (the report will be generated to `target/llvm-cov` directory):
With html report (the report will be generated to `target/llvm-cov/html` directory):

```sh
cargo llvm-cov --html
open target/llvm-cov/index.html
open target/llvm-cov/html/index.html
```

or
Expand Down
2 changes: 1 addition & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ pub(crate) struct Args {
pub(crate) text: bool,
/// Generate coverage reports in "html" format.
////
/// If --output-dir is not specified, the report will be generated in `target/llvm-cov` directory.
/// If --output-dir is not specified, the report will be generated in `target/llvm-cov/html` directory.
///
/// This internally calls `llvm-cov show -format=html`.
/// See <https://llvm.org/docs/CommandGuide/llvm-cov.html#llvm-cov-show> for more.
Expand Down
5 changes: 4 additions & 1 deletion src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl Context {
let config = cargo::config(&cargo, &metadata.workspace_root)?;
config.merge_to(&mut args, &mut env);

term::set_coloring(args.color);
term::set_coloring(&mut args.color);

if let Some(v) = env::var_os("LLVM_PROFILE_FILE") {
warn!("environment variable LLVM_PROFILE_FILE={:?} will be ignored", v);
Expand All @@ -69,6 +69,9 @@ impl Context {
if args.disable_default_ignore_filename_regex {
warn!("--disable-default-ignore-filename-regex option is unstable");
}
if args.hide_instantiations {
warn!("--hide-instantiations option is unstable");
}
if args.doctests {
warn!("--doctests option is unstable");
}
Expand Down
36 changes: 29 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,12 @@ fn try_main() -> Result<()> {

fn clean_partial(cx: &Context) -> Result<()> {
if let Some(output_dir) = &cx.output_dir {
fs::remove_dir_all(output_dir)?;
if cx.html {
fs::remove_dir_all(output_dir.join("html"))?;
}
if cx.text {
fs::remove_dir_all(output_dir.join("text"))?;
}
}

for path in glob::glob(cx.target_dir.join("*.profraw").as_str())?.filter_map(Result::ok) {
Expand All @@ -88,6 +93,12 @@ fn clean_partial(cx: &Context) -> Result<()> {
fn create_dirs(cx: &Context) -> Result<()> {
if let Some(output_dir) = &cx.output_dir {
fs::create_dir_all(output_dir)?;
if cx.html {
fs::create_dir_all(output_dir.join("html"))?;
}
if cx.text {
fs::create_dir_all(output_dir.join("text"))?;
}
}

if cx.doctests {
Expand Down Expand Up @@ -152,7 +163,7 @@ fn generate_report(cx: &Context) -> Result<()> {
}

if cx.open {
open::that(cx.output_dir.as_ref().unwrap().join("index.html"))
open::that(cx.output_dir.as_ref().unwrap().join("html/index.html"))
.context("couldn't open report")?;
}
Ok(())
Expand Down Expand Up @@ -291,13 +302,16 @@ impl Format {
}
}

fn use_color(self, color: Option<Coloring>) -> Option<&'static str> {
fn use_color(self, cx: &Context) -> Option<&'static str> {
if matches!(self, Self::Json | Self::LCov) {
// `llvm-cov export` doesn't have `-use-color` flag.
// https://llvm.org/docs/CommandGuide/llvm-cov.html#llvm-cov-export
return None;
}
match color {
if self == Self::Text && cx.output_dir.is_some() {
return Some("-use-color=0");
}
match cx.color {
Some(Coloring::Auto) | None => None,
Some(Coloring::Always) => Some("-use-color=1"),
Some(Coloring::Never) => Some("-use-color=0"),
Expand All @@ -308,7 +322,7 @@ impl Format {
let mut cmd = cx.process(&cx.llvm_cov);

cmd.args(self.llvm_cov_args());
cmd.args(self.use_color(cx.color));
cmd.args(self.use_color(cx));
cmd.arg(format!("-instr-profile={}", cx.profdata_file));
cmd.args(object_files.iter().flat_map(|f| [OsStr::new("-object"), f]));

Expand All @@ -328,7 +342,11 @@ impl Format {
"-Xdemangler=demangle",
]);
if let Some(output_dir) = &cx.output_dir {
cmd.arg(&format!("-output-dir={}", output_dir));
if self == Format::Html {
cmd.arg(&format!("-output-dir={}", output_dir.join("html")));
} else {
cmd.arg(&format!("-output-dir={}", output_dir.join("text")));
}
}
}
Format::Json | Format::LCov => {
Expand Down Expand Up @@ -359,7 +377,11 @@ impl Format {
cmd.run()?;
if matches!(self, Self::Html | Self::Text) {
if let Some(output_dir) = &cx.output_dir {
status!("Finished", "report saved to {:#}", output_dir);
if self == Self::Html {
status!("Finished", "report saved to {:#}", output_dir.join("html"));
} else {
status!("Finished", "report saved to {:#}", output_dir.join("text"));
}
}
}
Ok(())
Expand Down
11 changes: 6 additions & 5 deletions src/term.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ const AUTO: u8 = Coloring::Auto as _;
const ALWAYS: u8 = Coloring::Always as _;
const NEVER: u8 = Coloring::Never as _;

pub(crate) fn set_coloring(coloring: Option<Coloring>) {
let mut coloring = coloring.unwrap_or(Coloring::Auto);
if coloring == Coloring::Auto && !atty::is(atty::Stream::Stderr) {
coloring = Coloring::Never;
pub(crate) fn set_coloring(coloring: &mut Option<Coloring>) {
let mut color = coloring.unwrap_or(Coloring::Auto);
if color == Coloring::Auto && !atty::is(atty::Stream::Stderr) {
*coloring = Some(Coloring::Never);
color = Coloring::Never;
}
COLORING.store(coloring as _, Relaxed);
COLORING.store(color as _, Relaxed);
}

fn coloring() -> ColorChoice {
Expand Down
2 changes: 1 addition & 1 deletion tests/long-help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ OPTIONS:

--html
Generate coverage reports in "html" format. If --output-dir is not specified, the report
will be generated in `target/llvm-cov` directory.
will be generated in `target/llvm-cov/html` directory.

This internally calls `llvm-cov show -format=html`. See
<https://llvm.org/docs/CommandGuide/llvm-cov.html#llvm-cov-show> for more.
Expand Down
2 changes: 1 addition & 1 deletion tests/short-help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ OPTIONS:

--html
Generate coverage reports in "html" format. If --output-dir is not specified, the report
will be generated in `target/llvm-cov` directory
will be generated in `target/llvm-cov/html` directory

--open
Generate coverage reports in "html" format and open them in a browser after the
Expand Down

0 comments on commit acff245

Please sign in to comment.