2016-06-10 13:43:34 +00:00
|
|
|
#[macro_use]
|
2016-05-26 00:06:25 +00:00
|
|
|
extern crate cargotest;
|
|
|
|
extern crate flate2;
|
|
|
|
extern crate hamcrest;
|
|
|
|
extern crate tar;
|
|
|
|
extern crate url;
|
|
|
|
|
2015-02-27 01:04:25 +00:00
|
|
|
use std::io::prelude::*;
|
|
|
|
use std::fs::{self, File};
|
2016-01-20 17:07:47 +00:00
|
|
|
use std::io::SeekFrom;
|
2015-02-27 01:04:25 +00:00
|
|
|
use std::path::PathBuf;
|
2014-09-09 14:23:09 +00:00
|
|
|
|
2016-05-26 00:06:25 +00:00
|
|
|
use cargotest::support::git::repo;
|
|
|
|
use cargotest::support::paths;
|
|
|
|
use cargotest::support::{project, execs};
|
2015-02-27 01:04:25 +00:00
|
|
|
use flate2::read::GzDecoder;
|
2016-05-26 00:06:25 +00:00
|
|
|
use hamcrest::assert_that;
|
2014-09-09 14:23:09 +00:00
|
|
|
use tar::Archive;
|
|
|
|
use url::Url;
|
|
|
|
|
2015-02-27 01:04:25 +00:00
|
|
|
fn registry_path() -> PathBuf { paths::root().join("registry") }
|
|
|
|
fn registry() -> Url { Url::from_file_path(&*registry_path()).ok().unwrap() }
|
|
|
|
fn upload_path() -> PathBuf { paths::root().join("upload") }
|
|
|
|
fn upload() -> Url { Url::from_file_path(&*upload_path()).ok().unwrap() }
|
2014-09-09 14:23:09 +00:00
|
|
|
|
|
|
|
fn setup() {
|
|
|
|
let config = paths::root().join(".cargo/config");
|
2016-02-03 18:54:07 +00:00
|
|
|
t!(fs::create_dir_all(config.parent().unwrap()));
|
2017-02-18 12:01:10 +00:00
|
|
|
t!(t!(File::create(&config)).write_all(br#"
|
2014-09-09 14:23:09 +00:00
|
|
|
[registry]
|
|
|
|
token = "api-token"
|
2017-02-18 12:01:10 +00:00
|
|
|
"#));
|
2016-02-03 18:54:07 +00:00
|
|
|
t!(fs::create_dir_all(&upload_path().join("api/v1/crates")));
|
2014-09-09 14:23:09 +00:00
|
|
|
|
|
|
|
repo(®istry_path())
|
2015-02-27 01:04:25 +00:00
|
|
|
.file("config.json", &format!(r#"{{
|
2014-09-27 04:14:46 +00:00
|
|
|
"dl": "{0}",
|
|
|
|
"api": "{0}"
|
2014-09-09 14:23:09 +00:00
|
|
|
}}"#, upload()))
|
|
|
|
.build();
|
|
|
|
}
|
|
|
|
|
2016-05-25 20:55:42 +00:00
|
|
|
#[test]
|
|
|
|
fn simple() {
|
|
|
|
setup();
|
|
|
|
|
2014-09-09 14:23:09 +00:00
|
|
|
let p = project("foo")
|
|
|
|
.file("Cargo.toml", r#"
|
|
|
|
[project]
|
|
|
|
name = "foo"
|
|
|
|
version = "0.0.1"
|
|
|
|
authors = []
|
2014-11-18 12:23:53 +00:00
|
|
|
license = "MIT"
|
|
|
|
description = "foo"
|
2014-09-09 14:23:09 +00:00
|
|
|
"#)
|
|
|
|
.file("src/main.rs", "fn main() {}");
|
|
|
|
|
2016-02-03 18:54:07 +00:00
|
|
|
assert_that(p.cargo_process("publish").arg("--no-verify")
|
|
|
|
.arg("--host").arg(registry().to_string()),
|
2016-05-14 21:44:18 +00:00
|
|
|
execs().with_status(0).with_stderr(&format!("\
|
2016-05-11 16:55:43 +00:00
|
|
|
[UPDATING] registry `{reg}`
|
2016-05-20 14:00:15 +00:00
|
|
|
[WARNING] manifest has no documentation, [..]
|
2016-09-01 00:04:29 +00:00
|
|
|
See [..]
|
2016-05-12 15:23:53 +00:00
|
|
|
[PACKAGING] foo v0.0.1 ({dir})
|
|
|
|
[UPLOADING] foo v0.0.1 ({dir})
|
2014-09-09 14:23:09 +00:00
|
|
|
",
|
|
|
|
dir = p.url(),
|
2015-03-26 18:17:44 +00:00
|
|
|
reg = registry())));
|
2014-09-09 14:23:09 +00:00
|
|
|
|
2014-09-27 04:14:46 +00:00
|
|
|
let mut f = File::open(&upload_path().join("api/v1/crates/new")).unwrap();
|
|
|
|
// Skip the metadata payload and the size of the tarball
|
2015-02-27 01:04:25 +00:00
|
|
|
let mut sz = [0; 4];
|
2015-04-02 18:12:21 +00:00
|
|
|
assert_eq!(f.read(&mut sz).unwrap(), 4);
|
2015-02-27 01:04:25 +00:00
|
|
|
let sz = ((sz[0] as u32) << 0) |
|
|
|
|
((sz[1] as u32) << 8) |
|
|
|
|
((sz[2] as u32) << 16) |
|
|
|
|
((sz[3] as u32) << 24);
|
|
|
|
f.seek(SeekFrom::Current(sz as i64 + 4)).unwrap();
|
2014-09-27 04:14:46 +00:00
|
|
|
|
|
|
|
// Verify the tarball
|
|
|
|
let mut rdr = GzDecoder::new(f).unwrap();
|
2015-03-22 23:58:11 +00:00
|
|
|
assert_eq!(rdr.header().filename().unwrap(), "foo-0.0.1.crate".as_bytes());
|
2015-02-27 01:04:25 +00:00
|
|
|
let mut contents = Vec::new();
|
|
|
|
rdr.read_to_end(&mut contents).unwrap();
|
2016-01-20 17:07:47 +00:00
|
|
|
let mut ar = Archive::new(&contents[..]);
|
|
|
|
for file in ar.entries().unwrap() {
|
2014-09-09 14:23:09 +00:00
|
|
|
let file = file.unwrap();
|
2015-08-03 17:04:22 +00:00
|
|
|
let fname = file.header().path_bytes();
|
|
|
|
let fname = &*fname;
|
2014-11-19 06:29:19 +00:00
|
|
|
assert!(fname == b"foo-0.0.1/Cargo.toml" ||
|
Rewrite Cargo.toml when packaging crates
This commit is an implementation of rewriting TOML manifests when we publish
them to the registry. The rationale for doing this is to provide a guarantee
that downloaded tarballs from crates.io can be built with `cargo build`
(literally). This in turn eases a number of other possible consumers of crates
from crates.io
* Vendored sources can now be more easily modified/checked as cargo build should
work and they're standalone crates that suffice for `path` dependencies
* Tools like cargobomb/crater no longer need to edit the manifest and can
instead perform regression testing on the literal tarballs they download
* Other systems such as packaging Rust code may be able to take advantage of
this, but this is a less clear benefit.
Overall I'm hesitatnt about this, unfortunately. This is a silent translation
happening on *publish*, a rare operation, that's difficult to inspect before it
flies up to crates.io. I wrote a script to run this transformation over all
crates.io crates and found a surprisingly large number of discrepancies. The
transformation basically just downloaded all crates at all versions,
regenerated the manifest, and then tested if the two manifests were (in memory)
the same.
Unfortunately historical Cargo had a critical bug which I think made this
exercise not too useful. Cargo used to *not* recreate tarballs if one already
existed, which I believe led to situations such as:
1. `cargo publish`
2. Cargo generates an error about a dependency. This could be that there's a
`version` not present in a `path` dependency, there could be a `git`
dependency, etc.
3. Errors are fixed.
4. `cargo publish`
5. Publish is successful
In step 4 above historical Cargo *would not recreate the tarball*. This means
that the contents of the index (what was published) aren't guaranteed to match
with the tarball's `Cargo.toml`. When building from crates.io this is ok as the
index is the source of truth for dependency information, but it means that *any*
transformation to rewrite Cargo.toml is impossible to verify against all crates
on crates.io (due to historical bugs like these).
I strove to read as many errors as possible regardless, attempting to suss out
bugs in the implementation here. To further guard against surprises I've updated
the verification step of packaging to work "normally" in these sense that it's
not rewriting dependencies itself or changing summaries. I'm hoping that this
serves as a good last-ditch effort that what we're about to publish will indeed
build as expected when uploaded to crates.io
Overall I'm probably 70% confident in this change. I think it's necessary to
make progress, but I think there are going to be very painful bugs that arise
from this feature. I'm open to ideas to help weed out these bugs ahead of time!
I've done what I can but I fear it may not be entirely enough.
Closes #4027
2017-05-11 05:09:44 +00:00
|
|
|
fname == b"foo-0.0.1/Cargo.toml.orig" ||
|
2014-11-19 06:29:19 +00:00
|
|
|
fname == b"foo-0.0.1/src/main.rs",
|
2015-08-03 17:04:22 +00:00
|
|
|
"unexpected filename: {:?}", file.header().path());
|
2014-09-09 14:23:09 +00:00
|
|
|
}
|
2016-05-25 20:55:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn git_deps() {
|
|
|
|
setup();
|
2014-09-09 14:23:09 +00:00
|
|
|
|
|
|
|
let p = project("foo")
|
|
|
|
.file("Cargo.toml", r#"
|
|
|
|
[project]
|
|
|
|
name = "foo"
|
|
|
|
version = "0.0.1"
|
|
|
|
authors = []
|
2014-11-18 12:23:53 +00:00
|
|
|
license = "MIT"
|
|
|
|
description = "foo"
|
2014-09-09 14:23:09 +00:00
|
|
|
|
|
|
|
[dependencies.foo]
|
|
|
|
git = "git://path/to/nowhere"
|
|
|
|
"#)
|
|
|
|
.file("src/main.rs", "fn main() {}");
|
|
|
|
|
2016-02-03 18:54:07 +00:00
|
|
|
assert_that(p.cargo_process("publish").arg("-v").arg("--no-verify")
|
|
|
|
.arg("--host").arg(registry().to_string()),
|
2016-05-12 17:06:36 +00:00
|
|
|
execs().with_status(101).with_stderr("\
|
2016-05-20 14:00:15 +00:00
|
|
|
[UPDATING] registry [..]
|
2017-01-27 17:54:43 +00:00
|
|
|
[ERROR] crates cannot be published to crates.io with dependencies sourced from \
|
|
|
|
a repository\neither publish `foo` as its own crate on crates.io and \
|
|
|
|
specify a crates.io version as a dependency or pull it into this \
|
|
|
|
repository and specify it with a path and version\n\
|
|
|
|
(crate `foo` has repository path `git://path/to/nowhere`)\
|
2016-05-12 17:06:36 +00:00
|
|
|
"));
|
2016-05-25 20:55:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn path_dependency_no_version() {
|
|
|
|
setup();
|
2014-09-27 04:14:46 +00:00
|
|
|
|
|
|
|
let p = project("foo")
|
|
|
|
.file("Cargo.toml", r#"
|
|
|
|
[project]
|
|
|
|
name = "foo"
|
|
|
|
version = "0.0.1"
|
|
|
|
authors = []
|
2014-11-18 12:23:53 +00:00
|
|
|
license = "MIT"
|
|
|
|
description = "foo"
|
2014-09-27 04:14:46 +00:00
|
|
|
|
|
|
|
[dependencies.bar]
|
|
|
|
path = "bar"
|
|
|
|
"#)
|
|
|
|
.file("src/main.rs", "fn main() {}")
|
|
|
|
.file("bar/Cargo.toml", r#"
|
|
|
|
[package]
|
|
|
|
name = "bar"
|
|
|
|
version = "0.0.1"
|
|
|
|
authors = []
|
|
|
|
"#)
|
|
|
|
.file("bar/src/lib.rs", "");
|
2014-09-09 14:23:09 +00:00
|
|
|
|
2016-02-03 18:54:07 +00:00
|
|
|
assert_that(p.cargo_process("publish")
|
|
|
|
.arg("--host").arg(registry().to_string()),
|
2016-05-12 17:06:36 +00:00
|
|
|
execs().with_status(101).with_stderr("\
|
2016-05-20 14:00:15 +00:00
|
|
|
[UPDATING] registry [..]
|
2016-05-11 16:55:43 +00:00
|
|
|
[ERROR] all path dependencies must have a version specified when publishing.
|
2014-09-27 04:14:46 +00:00
|
|
|
dependency `bar` does not specify a version
|
2016-05-12 17:06:36 +00:00
|
|
|
"));
|
2016-05-25 20:55:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn unpublishable_crate() {
|
|
|
|
setup();
|
2016-01-23 21:16:30 +00:00
|
|
|
|
|
|
|
let p = project("foo")
|
|
|
|
.file("Cargo.toml", r#"
|
|
|
|
[project]
|
|
|
|
name = "foo"
|
|
|
|
version = "0.0.1"
|
|
|
|
authors = []
|
|
|
|
license = "MIT"
|
|
|
|
description = "foo"
|
|
|
|
publish = false
|
|
|
|
"#)
|
|
|
|
.file("src/main.rs", "fn main() {}");
|
|
|
|
|
2016-02-03 18:54:07 +00:00
|
|
|
assert_that(p.cargo_process("publish")
|
|
|
|
.arg("--host").arg(registry().to_string()),
|
2016-05-12 17:06:36 +00:00
|
|
|
execs().with_status(101).with_stderr("\
|
2016-05-11 16:55:43 +00:00
|
|
|
[ERROR] some crates cannot be published.
|
2016-01-23 21:16:30 +00:00
|
|
|
`foo` is marked as unpublishable
|
2016-05-12 17:06:36 +00:00
|
|
|
"));
|
2016-05-25 20:55:42 +00:00
|
|
|
}
|
2016-06-10 13:43:34 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn dont_publish_dirty() {
|
|
|
|
setup();
|
2017-02-15 14:16:41 +00:00
|
|
|
let p = project("foo")
|
|
|
|
.file("bar", "");
|
|
|
|
p.build();
|
2016-06-10 13:43:34 +00:00
|
|
|
|
|
|
|
repo(&paths::root().join("foo"))
|
|
|
|
.file("Cargo.toml", r#"
|
|
|
|
[project]
|
|
|
|
name = "foo"
|
|
|
|
version = "0.0.1"
|
|
|
|
authors = []
|
|
|
|
license = "MIT"
|
|
|
|
description = "foo"
|
|
|
|
documentation = "foo"
|
|
|
|
homepage = "foo"
|
|
|
|
repository = "foo"
|
|
|
|
"#)
|
|
|
|
.file("src/main.rs", "fn main() {}")
|
|
|
|
.build();
|
|
|
|
|
2016-02-03 18:54:07 +00:00
|
|
|
assert_that(p.cargo("publish")
|
|
|
|
.arg("--host").arg(registry().to_string()),
|
2016-06-10 13:43:34 +00:00
|
|
|
execs().with_status(101).with_stderr("\
|
|
|
|
[UPDATING] registry `[..]`
|
2017-04-09 02:20:41 +00:00
|
|
|
error: 1 files in the working directory contain changes that were not yet \
|
|
|
|
committed into git:
|
2016-06-10 13:43:34 +00:00
|
|
|
|
|
|
|
bar
|
|
|
|
|
2016-09-03 01:05:26 +00:00
|
|
|
to proceed despite this, pass the `--allow-dirty` flag
|
2016-06-10 13:43:34 +00:00
|
|
|
"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn publish_clean() {
|
|
|
|
setup();
|
|
|
|
|
2017-02-15 14:16:41 +00:00
|
|
|
let p = project("foo");
|
|
|
|
p.build();
|
|
|
|
|
2016-06-10 13:43:34 +00:00
|
|
|
repo(&paths::root().join("foo"))
|
|
|
|
.file("Cargo.toml", r#"
|
|
|
|
[project]
|
|
|
|
name = "foo"
|
|
|
|
version = "0.0.1"
|
|
|
|
authors = []
|
|
|
|
license = "MIT"
|
|
|
|
description = "foo"
|
|
|
|
documentation = "foo"
|
|
|
|
homepage = "foo"
|
|
|
|
repository = "foo"
|
|
|
|
"#)
|
|
|
|
.file("src/main.rs", "fn main() {}")
|
|
|
|
.build();
|
|
|
|
|
2016-02-03 18:54:07 +00:00
|
|
|
assert_that(p.cargo("publish")
|
|
|
|
.arg("--host").arg(registry().to_string()),
|
2016-06-10 13:43:34 +00:00
|
|
|
execs().with_status(0));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn publish_in_sub_repo() {
|
|
|
|
setup();
|
|
|
|
|
2017-02-15 14:16:41 +00:00
|
|
|
let p = project("foo")
|
|
|
|
.file("baz", "");
|
|
|
|
p.build();
|
|
|
|
|
2016-06-10 13:43:34 +00:00
|
|
|
repo(&paths::root().join("foo"))
|
|
|
|
.file("bar/Cargo.toml", r#"
|
|
|
|
[project]
|
|
|
|
name = "foo"
|
|
|
|
version = "0.0.1"
|
|
|
|
authors = []
|
|
|
|
license = "MIT"
|
|
|
|
description = "foo"
|
|
|
|
documentation = "foo"
|
|
|
|
homepage = "foo"
|
|
|
|
repository = "foo"
|
|
|
|
"#)
|
|
|
|
.file("bar/src/main.rs", "fn main() {}")
|
|
|
|
.build();
|
|
|
|
|
2016-02-03 18:54:07 +00:00
|
|
|
assert_that(p.cargo("publish").cwd(p.root().join("bar"))
|
|
|
|
.arg("--host").arg(registry().to_string()),
|
2016-06-10 13:43:34 +00:00
|
|
|
execs().with_status(0));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn publish_when_ignored() {
|
|
|
|
setup();
|
|
|
|
|
2017-02-15 14:16:41 +00:00
|
|
|
let p = project("foo")
|
|
|
|
.file("baz", "");
|
|
|
|
p.build();
|
|
|
|
|
2016-06-10 13:43:34 +00:00
|
|
|
repo(&paths::root().join("foo"))
|
|
|
|
.file("Cargo.toml", r#"
|
|
|
|
[project]
|
|
|
|
name = "foo"
|
|
|
|
version = "0.0.1"
|
|
|
|
authors = []
|
|
|
|
license = "MIT"
|
|
|
|
description = "foo"
|
|
|
|
documentation = "foo"
|
|
|
|
homepage = "foo"
|
|
|
|
repository = "foo"
|
|
|
|
"#)
|
|
|
|
.file("src/main.rs", "fn main() {}")
|
|
|
|
.file(".gitignore", "baz")
|
|
|
|
.build();
|
|
|
|
|
2016-02-03 18:54:07 +00:00
|
|
|
assert_that(p.cargo("publish")
|
|
|
|
.arg("--host").arg(registry().to_string()),
|
2016-06-10 13:43:34 +00:00
|
|
|
execs().with_status(0));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn ignore_when_crate_ignored() {
|
|
|
|
setup();
|
|
|
|
|
2017-02-15 14:16:41 +00:00
|
|
|
let p = project("foo")
|
|
|
|
.file("bar/baz", "");
|
|
|
|
p.build();
|
|
|
|
|
2016-06-10 13:43:34 +00:00
|
|
|
repo(&paths::root().join("foo"))
|
|
|
|
.file(".gitignore", "bar")
|
|
|
|
.nocommit_file("bar/Cargo.toml", r#"
|
|
|
|
[project]
|
|
|
|
name = "foo"
|
|
|
|
version = "0.0.1"
|
|
|
|
authors = []
|
|
|
|
license = "MIT"
|
|
|
|
description = "foo"
|
|
|
|
documentation = "foo"
|
|
|
|
homepage = "foo"
|
|
|
|
repository = "foo"
|
|
|
|
"#)
|
|
|
|
.nocommit_file("bar/src/main.rs", "fn main() {}");
|
2016-02-03 18:54:07 +00:00
|
|
|
assert_that(p.cargo("publish").cwd(p.root().join("bar"))
|
|
|
|
.arg("--host").arg(registry().to_string()),
|
2016-06-10 13:43:34 +00:00
|
|
|
execs().with_status(0));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn new_crate_rejected() {
|
|
|
|
setup();
|
|
|
|
|
2017-02-15 14:16:41 +00:00
|
|
|
let p = project("foo")
|
|
|
|
.file("baz", "");
|
|
|
|
p.build();
|
|
|
|
|
2016-06-10 13:43:34 +00:00
|
|
|
repo(&paths::root().join("foo"))
|
|
|
|
.nocommit_file("Cargo.toml", r#"
|
|
|
|
[project]
|
|
|
|
name = "foo"
|
|
|
|
version = "0.0.1"
|
|
|
|
authors = []
|
|
|
|
license = "MIT"
|
|
|
|
description = "foo"
|
|
|
|
documentation = "foo"
|
|
|
|
homepage = "foo"
|
|
|
|
repository = "foo"
|
|
|
|
"#)
|
|
|
|
.nocommit_file("src/main.rs", "fn main() {}");
|
2016-02-03 18:54:07 +00:00
|
|
|
assert_that(p.cargo("publish")
|
|
|
|
.arg("--host").arg(registry().to_string()),
|
2016-06-10 13:43:34 +00:00
|
|
|
execs().with_status(101));
|
|
|
|
}
|
2016-07-17 23:43:57 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn dry_run() {
|
|
|
|
setup();
|
|
|
|
|
|
|
|
let p = project("foo")
|
|
|
|
.file("Cargo.toml", r#"
|
|
|
|
[project]
|
|
|
|
name = "foo"
|
|
|
|
version = "0.0.1"
|
|
|
|
authors = []
|
|
|
|
license = "MIT"
|
|
|
|
description = "foo"
|
|
|
|
"#)
|
|
|
|
.file("src/main.rs", "fn main() {}");
|
|
|
|
|
2016-02-03 18:54:07 +00:00
|
|
|
assert_that(p.cargo_process("publish").arg("--dry-run")
|
|
|
|
.arg("--host").arg(registry().to_string()),
|
2016-07-17 23:43:57 +00:00
|
|
|
execs().with_status(0).with_stderr(&format!("\
|
2016-07-05 17:28:51 +00:00
|
|
|
[UPDATING] registry `[..]`
|
2016-07-17 23:43:57 +00:00
|
|
|
[WARNING] manifest has no documentation, [..]
|
2016-09-01 00:04:29 +00:00
|
|
|
See [..]
|
2016-07-17 23:43:57 +00:00
|
|
|
[PACKAGING] foo v0.0.1 ({dir})
|
|
|
|
[VERIFYING] foo v0.0.1 ({dir})
|
|
|
|
[COMPILING] foo v0.0.1 [..]
|
2017-01-12 01:03:36 +00:00
|
|
|
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
|
2016-07-17 23:43:57 +00:00
|
|
|
[UPLOADING] foo v0.0.1 ({dir})
|
|
|
|
[WARNING] aborting upload due to dry run
|
|
|
|
",
|
2016-07-05 17:28:51 +00:00
|
|
|
dir = p.url())));
|
2016-07-17 23:43:57 +00:00
|
|
|
|
|
|
|
// Ensure the API request wasn't actually made
|
|
|
|
assert!(!upload_path().join("api/v1/crates/new").exists());
|
|
|
|
}
|