Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use --color=always in rustdoc command when stderr is tty #360

Merged
merged 4 commits into from
Feb 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions src/rustdoc_cmd.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use crate::GlobalConfig;

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct RustdocCommand {
deps: bool,
silence: bool,
}

impl RustdocCommand {
pub fn new() -> Self {
pub(crate) fn new() -> Self {
Self {
deps: false,
silence: false,
Expand All @@ -23,20 +25,21 @@ impl RustdocCommand {
/// Reasons to have this enabled:
/// - Check for accidental inclusion of dependencies in your API
/// - Detect breaking changes from dependencies in your API
pub fn deps(mut self, yes: bool) -> Self {
pub(crate) fn deps(mut self, yes: bool) -> Self {
self.deps = yes;
self
}

/// Don't write progress to stderr
pub fn silence(mut self, yes: bool) -> Self {
pub(crate) fn silence(mut self, yes: bool) -> Self {
self.silence = yes;
self
}

/// Produce a rustdoc JSON file for the specified configuration.
pub fn dump(
pub(crate) fn dump(
&self,
config: &mut GlobalConfig,
manifest_path: &std::path::Path,
pkg_spec: Option<&str>,
all_features: bool,
Expand Down Expand Up @@ -83,6 +86,9 @@ impl RustdocCommand {
if all_features {
cmd.arg("--all-features");
}
if config.is_stderr_tty() {
cmd.arg("--color=always");
}
let output = cmd.output()?;
if !output.status.success() {
if self.silence {
Expand Down Expand Up @@ -160,6 +166,7 @@ impl Default for RustdocCommand {

#[cfg(test)]
mod tests {
use crate::GlobalConfig;
use std::path::Path;

use super::RustdocCommand;
Expand All @@ -168,6 +175,7 @@ mod tests {
fn rustdoc_for_lib_crate_without_lib_section() {
RustdocCommand::default()
.dump(
&mut GlobalConfig::new(),
Path::new("./test_rustdoc/implicit_lib/Cargo.toml"),
None,
true,
Expand All @@ -179,6 +187,7 @@ mod tests {
fn rustdoc_for_lib_crate_with_lib_section() {
RustdocCommand::default()
.dump(
&mut GlobalConfig::new(),
Path::new("./test_rustdoc/renamed_lib/Cargo.toml"),
None,
true,
Expand All @@ -190,6 +199,7 @@ mod tests {
fn rustdoc_for_bin_crate_without_bin_section() {
RustdocCommand::default()
.dump(
&mut GlobalConfig::new(),
Path::new("./test_rustdoc/implicit_bin/Cargo.toml"),
None,
true,
Expand All @@ -201,6 +211,7 @@ mod tests {
fn rustdoc_for_bin_crate_with_bin_section() {
RustdocCommand::default()
.dump(
&mut GlobalConfig::new(),
Path::new("./test_rustdoc/renamed_bin/Cargo.toml"),
None,
true,
Expand All @@ -212,6 +223,7 @@ mod tests {
fn rustdoc_for_crate_in_workspace_with_workspace_manifest() {
RustdocCommand::default()
.dump(
&mut GlobalConfig::new(),
Path::new("./test_rustdoc/crate_in_workspace/Cargo.toml"),
Some("crate_in_workspace_crate1"),
true,
Expand All @@ -223,6 +235,7 @@ mod tests {
fn rustdoc_for_crate_in_workspace_with_crate_manifest() {
RustdocCommand::default()
.dump(
&mut GlobalConfig::new(),
Path::new("./test_rustdoc/crate_in_workspace/crate1/Cargo.toml"),
None,
true,
Expand Down
1 change: 1 addition & 0 deletions src/rustdoc_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ fn generate_rustdoc(
)?;

let rustdoc_path = rustdoc_cmd.dump(
config,
placeholder_manifest_path.as_path(),
Some(&format!("{name}@{version}")),
false,
Expand Down