Skip to content

Commit dd26ce3

Browse files
committed
Add a new CARGO_PKG_AUTHORS environment variable
This will allow crates to use the CARGO_PKG_AUTHORS env variable to get a colon seperated list of the authors declared in the manifest. Closes #2441
1 parent 31214eb commit dd26ce3

File tree

4 files changed

+41
-0
lines changed

4 files changed

+41
-0
lines changed

src/cargo/core/package.rs

+1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ impl Package {
8080
pub fn summary(&self) -> &Summary { self.manifest.summary() }
8181
pub fn targets(&self) -> &[Target] { self.manifest().targets() }
8282
pub fn version(&self) -> &Version { self.package_id().version() }
83+
pub fn authors(&self) -> &Vec<String> { &self.manifest.metadata().authors }
8384
pub fn publish(&self) -> bool { self.manifest.publish() }
8485

8586
pub fn has_custom_build(&self) -> bool {

src/cargo/ops/cargo_rustc/compilation.rs

+1
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ impl<'cfg> Compilation<'cfg> {
120120
.env("CARGO_PKG_NAME", &pkg.name())
121121
.env("CARGO_PKG_DESCRIPTION", metadata.description.as_ref().unwrap_or(&String::new()))
122122
.env("CARGO_PKG_HOMEPAGE", metadata.homepage.as_ref().unwrap_or(&String::new()))
123+
.env("CARGO_PKG_AUTHORS", &pkg.authors().join(":"))
123124
.cwd(pkg.root());
124125
Ok(cmd)
125126
}

src/doc/environment-variables.md

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ let version = env!("CARGO_PKG_VERSION");
4545
* `CARGO_PKG_VERSION_MINOR` - The minor version of your package.
4646
* `CARGO_PKG_VERSION_PATCH` - The patch version of your package.
4747
* `CARGO_PKG_VERSION_PRE` - The pre-release version of your package.
48+
* `CARGO_PKG_AUTHORS` - Colon seperated list of authors from the manifest of your package.
4849

4950
# Environment variables Cargo sets for build scripts
5051

tests/test_cargo_compile.rs

+38
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,44 @@ test!(crate_env_vars {
784784
execs().with_status(0));
785785
});
786786

787+
test!(crate_authors_env_vars {
788+
let p = project("foo")
789+
.file("Cargo.toml", r#"
790+
[project]
791+
name = "foo"
792+
version = "0.5.1-alpha.1"
793+
authors = ["wycats@example.com", "neikos@example.com"]
794+
"#)
795+
.file("src/main.rs", r#"
796+
extern crate foo;
797+
798+
static AUTHORS: &'static str = env!("CARGO_PKG_AUTHORS");
799+
800+
fn main() {
801+
let s = "wycats@example.com:neikos@example.com";
802+
assert_eq!(AUTHORS, foo::authors());
803+
println!("{}", AUTHORS);
804+
assert_eq!(s, AUTHORS);
805+
}
806+
"#)
807+
.file("src/lib.rs", r#"
808+
pub fn authors() -> String {
809+
format!("{}", env!("CARGO_PKG_AUTHORS"))
810+
}
811+
"#);
812+
813+
println!("build");
814+
assert_that(p.cargo_process("build").arg("-v"), execs().with_status(0));
815+
816+
println!("bin");
817+
assert_that(process(&p.bin("foo")),
818+
execs().with_stdout(&format!("wycats@example.com:neikos@example.com")));
819+
820+
println!("test");
821+
assert_that(p.cargo("test").arg("-v"),
822+
execs().with_status(0));
823+
});
824+
787825
// this is testing that src/<pkg-name>.rs still works (for now)
788826
test!(many_crate_types_old_style_lib_location {
789827
let mut p = project("foo");

0 commit comments

Comments
 (0)