cargo/tests/test_cargo_generate_lockfile.rs
Alex Crichton 9879cc55f6 Add a metadata section to the lockfile
In terms of future compatibility, Cargo may wish to add more information to
lockfiles in the future. For example, the minimum required Rust version may one
day be encoded into a lockfile.

One of the major goals of a lockfile is for it to rarely change, and when it
does change the diffs should be minimal. In terms of adding more information to
the lockfile in the future, this presents a problem for older Cargo clients:

1. Cargo 2.0 adds a minimum required version of rust under the metadata.rustc
   key of the lockfile.
2. Cargo 1.0 is then used to update the `foo` dependency in the lockfile. When
   regenerating the lockfile, Cargo 1.0 discards the metadata inserted by Cargo
   2.0.

In order to future-proof ourselves in allowing new metadata in the future, Cargo
is growing support now for transporting an opaque payload of metadata from one
version of a lockfile to a new versions. This should solve the problem presented
above.

This metadata section is designed to be safely ignored by older Cargo versions
(may just have some surprising behavior), while still allowing newer versions of
Cargo to process the data. For example Cargo 2.0 would gracefully fail on an
out-of-date rustc, while Cargo 1.0 may just present an obscure error message.
2014-10-22 12:34:05 -07:00

141 lines
4.1 KiB
Rust

use std::io::File;
use support::{project, execs, cargo_dir, ResultTest};
use hamcrest::assert_that;
fn setup() {}
test!(ignores_carriage_return {
let p = project("foo")
.file("Cargo.toml", r#"
[package]
name = "foo"
authors = []
version = "0.0.1"
"#)
.file("src/main.rs", r#"
mod a; fn main() {}
"#)
.file("src/a.rs", "");
assert_that(p.cargo_process("build"),
execs().with_status(0));
let lockfile = p.root().join("Cargo.lock");
let lock = File::open(&lockfile).read_to_string();
let lock = lock.assert();
let lock = lock.as_slice().replace("\n", "\r\n");
File::create(&lockfile).write_str(lock.as_slice()).assert();
assert_that(p.process(cargo_dir().join("cargo")).arg("build"),
execs().with_status(0));
})
test!(adding_and_removing_packages {
let p = project("foo")
.file("Cargo.toml", r#"
[package]
name = "foo"
authors = []
version = "0.0.1"
"#)
.file("src/main.rs", "fn main() {}")
.file("bar/Cargo.toml", r#"
[package]
name = "bar"
authors = []
version = "0.0.1"
"#)
.file("bar/src/lib.rs", "");
assert_that(p.cargo_process("generate-lockfile"),
execs().with_status(0));
let lockfile = p.root().join("Cargo.lock");
let toml = p.root().join("Cargo.toml");
let lock1 = File::open(&lockfile).read_to_string().assert();
// add a dep
File::create(&toml).write_str(r#"
[package]
name = "foo"
authors = []
version = "0.0.1"
[dependencies.bar]
path = "bar"
"#).assert();
assert_that(p.process(cargo_dir().join("cargo")).arg("generate-lockfile"),
execs().with_status(0));
let lock2 = File::open(&lockfile).read_to_string().assert();
assert!(lock1 != lock2);
// change the dep
File::create(&p.root().join("bar/Cargo.toml")).write_str(r#"
[package]
name = "bar"
authors = []
version = "0.0.2"
"#).assert();
assert_that(p.process(cargo_dir().join("cargo")).arg("generate-lockfile"),
execs().with_status(0));
let lock3 = File::open(&lockfile).read_to_string().assert();
assert!(lock1 != lock3);
assert!(lock2 != lock3);
// remove the dep
File::create(&toml).write_str(r#"
[package]
name = "foo"
authors = []
version = "0.0.1"
"#).assert();
assert_that(p.process(cargo_dir().join("cargo")).arg("generate-lockfile"),
execs().with_status(0));
let lock4 = File::open(&lockfile).read_to_string().assert();
assert_eq!(lock1, lock4);
})
test!(preserve_metadata {
let p = project("foo")
.file("Cargo.toml", r#"
[package]
name = "foo"
authors = []
version = "0.0.1"
"#)
.file("src/main.rs", "fn main() {}")
.file("bar/Cargo.toml", r#"
[package]
name = "bar"
authors = []
version = "0.0.1"
"#)
.file("bar/src/lib.rs", "");
assert_that(p.cargo_process("generate-lockfile"),
execs().with_status(0));
let metadata = r#"
[metadata]
bar = "baz"
foo = "bar"
"#;
let lockfile = p.root().join("Cargo.lock");
{
let lock = File::open(&lockfile).read_to_string().assert();
File::create(&lockfile).write_str((lock + metadata).as_slice()).assert();
}
// Build and make sure the metadata is still there
assert_that(p.process(cargo_dir().join("cargo")).arg("build"),
execs().with_status(0));
let lock = File::open(&lockfile).read_to_string().assert();
assert!(lock.as_slice().contains(metadata.trim()), "{}", lock);
// Update and make sure the metadata is still there
assert_that(p.process(cargo_dir().join("cargo")).arg("update"),
execs().with_status(0));
let lock = File::open(&lockfile).read_to_string().assert();
assert!(lock.as_slice().contains(metadata.trim()), "{}", lock);
})