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

Adding the cargo doc --examples subcommand #9808

Merged
merged 9 commits into from
Sep 1, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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
5 changes: 4 additions & 1 deletion src/bin/cargo/commands/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub fn cli() -> App {
"open",
"Opens the docs in a browser after the operation",
))
.arg(opt("examples", "Document all examples"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't actually do anything; if someone passes the option cargo will ignore it. Consider writing some tests so you can tell whether this works or not, there are lots of examples if you search for cargo_test.

I'm not super familiar with cargo's code, but I expect the right approach would be to loop over all examples and document them (maybe in parallel?). I'd look at how --example x works currently.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your review, which is a huge encouragement to me. It works fine for my local test.

My test file directory is:

hello
├─examples
│    ├───examples1.rs
│    └───examples2.rs
└─src
   └───main.rs

image

I think it's good extensibility in the source code.

..\cargo\src\cargo\util\command_prelude.rs
filter: CompileFilter::from_raw_arguments(
                self._is_present("lib"),
                self._values_of("bin"),
                self._is_present("bins"),
                self._values_of("test"),
                self._is_present("tests"),
                self._values_of("example"),
                self._is_present("examples"),     // examples
                self._values_of("bench"),
                self._is_present("benches"),
                self._is_present("all-targets"),
            ),

.arg_package_spec(
"Package to document",
"Document all packages in the workspace",
Expand All @@ -20,10 +21,12 @@ pub fn cli() -> App {
.arg(opt("no-deps", "Don't build documentation for dependencies"))
.arg(opt("document-private-items", "Document private items"))
.arg_jobs()
.arg_targets_lib_bin(
.arg_targets_lib_bin_example(
"Document only this package's library",
"Document only the specified binary",
"Document all binaries",
"Document only the specified binary",
heisen-li marked this conversation as resolved.
Show resolved Hide resolved
"Document all examples",
)
.arg_release("Build artifacts in release mode, with optimizations")
.arg_profile("Build artifacts with the specified profile")
Expand Down
8 changes: 4 additions & 4 deletions src/cargo/util/command_prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,20 +87,20 @@ pub trait AppExt: Sized {
benches: &'static str,
all: &'static str,
) -> Self {
self.arg_targets_lib_bin(lib, bin, bins)
._arg(optional_multi_opt("example", "NAME", example))
._arg(opt("examples", examples))
self.arg_targets_lib_bin_example(lib, bin, bins, example, examples)
._arg(optional_multi_opt("test", "NAME", test))
._arg(opt("tests", tests))
._arg(optional_multi_opt("bench", "NAME", bench))
._arg(opt("benches", benches))
._arg(opt("all-targets", all))
}

fn arg_targets_lib_bin(self, lib: &'static str, bin: &'static str, bins: &'static str) -> Self {
fn arg_targets_lib_bin_example(self, lib: &'static str, bin: &'static str, bins: &'static str, example: &'static str, examples: &'static str) -> Self {
self._arg(opt("lib", lib))
._arg(optional_multi_opt("bin", "NAME", bin))
._arg(opt("bins", bins))
._arg(optional_multi_opt("example", "NAME", example))
._arg(opt("examples", examples))
}

fn arg_targets_bins_examples(
Expand Down
2 changes: 1 addition & 1 deletion src/doc/man/cargo-doc.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ the manifest settings. Using target selection options will ignore the `doc`
flag and will always document the given target.

{{#options}}
{{> options-targets-lib-bin }}
{{> options-targets-lib-bin-example }}
{{/options}}

{{> section-features }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,13 @@ and supports common Unix glob patterns.
{{#option "`--bins`" }}
{{actionverb}} all binary targets.
{{/option}}

{{#option "`--example` _name_..." }}
{{actionverb}} the specified example. This flag may be specified multiple times
and supports common Unix glob patterns.
{{/option}}

{{#option "`--examples`" }}
{{actionverb}} all examples targets.
{{/option}}

103 changes: 103 additions & 0 deletions tests/testsuite/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,109 @@ fn doc_lib_bin_same_name_documents_bins_when_requested() {
assert!(doc_html.contains("Binary"));
}

#[cargo_test]
fn doc_lib_bin_example_same_name_documents_named_example_when_requested() {
let p = project()
.file(
"src/main.rs",
r#"
//! Binary documentation
extern crate foo;
fn main() {
foo::foo();
}
"#,
)
.file(
"src/lib.rs",
r#"
//! Library documentation
pub fn foo() {}
"#,
)
.file(
"examples/ex1.rs",
r#"
//! Example1 documentation
pub fn x() { f(); }
"#,
)
.build();

p.cargo("doc --example ex1")
.with_stderr(
"\
[CHECKING] foo v0.0.1 ([CWD])
[DOCUMENTING] foo v0.0.1 ([CWD])
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]",
)
.run();

let doc_html = p.read_file("target/doc/ex1/index.html");
assert!(!doc_html.contains("Library"));
assert!(!doc_html.contains("Binary"));
assert!(doc_html.contains("Example1"));
}

#[cargo_test]
fn doc_lib_bin_example_same_name_documents_examples_when_requested() {
let p = project()
.file(
"src/main.rs",
r#"
//! Binary documentation
extern crate foo;
fn main() {
foo::foo();
}
"#,
)
.file(
"src/lib.rs",
r#"
//! Library documentation
pub fn foo() {}
"#,
)
.file(
"examples/ex1.rs",
r#"
//! Example1 documentation
pub fn example1() { f(); }
"#,
)
.file(
"examples/ex2.rs",
r#"
//! Example2 documentation
pub fn example2() { f(); }
"#,
)
.build();

p.cargo("doc --examples")
.with_stderr(
"\
[CHECKING] foo v0.0.1 ([CWD])
[DOCUMENTING] foo v0.0.1 ([CWD])
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]",
)
.run();

let example_doc_html_1 = p.read_file("target/doc/ex1/index.html");
let example_doc_html_2 = p.read_file("target/doc/ex2/index.html");

assert!(!example_doc_html_1.contains("Library"));
assert!(!example_doc_html_1.contains("Binary"));

assert!(!example_doc_html_2.contains("Library"));
assert!(!example_doc_html_2.contains("Binary"));

assert!(example_doc_html_1.contains("Example1"));
assert!(example_doc_html_2.contains("Example2"));
}


#[cargo_test]
fn doc_dash_p() {
let p = project()
Expand Down