From 851e20b2945965578d10026a7a39c43f6ab07573 Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Wed, 17 Oct 2018 21:29:51 +0100 Subject: [PATCH 1/5] Preserve lockfile top comment --- src/cargo/ops/lockfile.rs | 9 +++++++++ tests/testsuite/update.rs | 27 ++++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/cargo/ops/lockfile.rs b/src/cargo/ops/lockfile.rs index 91eb0b4d81e..4deb7d11665 100644 --- a/src/cargo/ops/lockfile.rs +++ b/src/cargo/ops/lockfile.rs @@ -43,6 +43,15 @@ pub fn write_pkg_lockfile(ws: &Workspace, resolve: &Resolve) -> CargoResult<()> let mut out = String::new(); + if let Ok(ref orig) = orig { + if let Some(first_line) = orig.lines().into_iter().next() { + if first_line.starts_with("#") { + out.push_str(first_line); + out.push_str("\n"); + } + } + } + let deps = toml["package"].as_array().unwrap(); for dep in deps.iter() { let dep = dep.as_table().unwrap(); diff --git a/tests/testsuite/update.rs b/tests/testsuite/update.rs index 8cdae253e0f..73fe80c9f67 100644 --- a/tests/testsuite/update.rs +++ b/tests/testsuite/update.rs @@ -1,4 +1,4 @@ -use std::fs::File; +use std::fs::{ File, self }; use std::io::prelude::*; use support::registry::Package; @@ -374,3 +374,28 @@ fn update_precise() { ", ).run(); } + +#[test] +fn preserve_top_comment() { + let p = project().file("src/lib.rs", "").build(); + + p.cargo("update").run(); + + let lockfile_path = p.root().join("Cargo.lock"); + + let mut lockfile = String::new(); + let mut f = File::open(&lockfile_path).unwrap(); + lockfile.push_str("# @generated\n"); + f.read_to_string(&mut lockfile).unwrap(); + println!("saving Cargo.lock contents:\n{}", lockfile); + + p.change_file("Cargo.lock", &lockfile); + + p.cargo("update").run(); + + let lockfile2 = fs::read_to_string(&lockfile_path).unwrap(); + println!("loaded Cargo.lock contents:\n{}", lockfile2); + + let first_line = lockfile2.lines().into_iter().next().unwrap(); + assert!(first_line == "# @generated"); +} From 1408eeccdb5e228aceac55e894fe11d27faa26e9 Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Wed, 17 Oct 2018 21:49:35 +0100 Subject: [PATCH 2/5] Learn to use Project::read_file & String::insert --- tests/testsuite/update.rs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/tests/testsuite/update.rs b/tests/testsuite/update.rs index 73fe80c9f67..dac199c43cc 100644 --- a/tests/testsuite/update.rs +++ b/tests/testsuite/update.rs @@ -1,4 +1,4 @@ -use std::fs::{ File, self }; +use std::fs::File; use std::io::prelude::*; use support::registry::Package; @@ -381,19 +381,15 @@ fn preserve_top_comment() { p.cargo("update").run(); - let lockfile_path = p.root().join("Cargo.lock"); - - let mut lockfile = String::new(); - let mut f = File::open(&lockfile_path).unwrap(); - lockfile.push_str("# @generated\n"); - f.read_to_string(&mut lockfile).unwrap(); + let mut lockfile = p.read_file("Cargo.lock"); + lockfile.insert_str(0, "# @generated\n"); println!("saving Cargo.lock contents:\n{}", lockfile); p.change_file("Cargo.lock", &lockfile); p.cargo("update").run(); - let lockfile2 = fs::read_to_string(&lockfile_path).unwrap(); + let lockfile2 = p.read_file("Cargo.lock"); println!("loaded Cargo.lock contents:\n{}", lockfile2); let first_line = lockfile2.lines().into_iter().next().unwrap(); From 0cdb780345b642417bf01c42e984e3d3f5779457 Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Thu, 18 Oct 2018 06:43:59 +0100 Subject: [PATCH 3/5] Assert the lock files are byte-for-byte equal --- tests/testsuite/update.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/testsuite/update.rs b/tests/testsuite/update.rs index dac199c43cc..0b120daa903 100644 --- a/tests/testsuite/update.rs +++ b/tests/testsuite/update.rs @@ -392,6 +392,5 @@ fn preserve_top_comment() { let lockfile2 = p.read_file("Cargo.lock"); println!("loaded Cargo.lock contents:\n{}", lockfile2); - let first_line = lockfile2.lines().into_iter().next().unwrap(); - assert!(first_line == "# @generated"); + assert!(lockfile == lockfile2); } From ce40ab87c8102b457118bb5c0f868593c4fb8c85 Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Thu, 18 Oct 2018 06:44:12 +0100 Subject: [PATCH 4/5] Preserve multiline top comments --- src/cargo/ops/lockfile.rs | 10 ++++------ tests/testsuite/update.rs | 1 + 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/cargo/ops/lockfile.rs b/src/cargo/ops/lockfile.rs index 4deb7d11665..10387626481 100644 --- a/src/cargo/ops/lockfile.rs +++ b/src/cargo/ops/lockfile.rs @@ -43,12 +43,10 @@ pub fn write_pkg_lockfile(ws: &Workspace, resolve: &Resolve) -> CargoResult<()> let mut out = String::new(); - if let Ok(ref orig) = orig { - if let Some(first_line) = orig.lines().into_iter().next() { - if first_line.starts_with("#") { - out.push_str(first_line); - out.push_str("\n"); - } + if let Ok(orig) = &orig { + for line in orig.lines().take_while(|line| line.starts_with("#")) { + out.push_str(line); + out.push_str("\n"); } } diff --git a/tests/testsuite/update.rs b/tests/testsuite/update.rs index 0b120daa903..be527b288db 100644 --- a/tests/testsuite/update.rs +++ b/tests/testsuite/update.rs @@ -383,6 +383,7 @@ fn preserve_top_comment() { let mut lockfile = p.read_file("Cargo.lock"); lockfile.insert_str(0, "# @generated\n"); + lockfile.insert_str(0, "# some other comment\n"); println!("saving Cargo.lock contents:\n{}", lockfile); p.change_file("Cargo.lock", &lockfile); From 32fbfe3025454ece1acc9b11993667c1aafc1365 Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Thu, 18 Oct 2018 14:30:40 +0100 Subject: [PATCH 5/5] Explain why top comments are preserved in Cargo.lock --- src/cargo/ops/lockfile.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/cargo/ops/lockfile.rs b/src/cargo/ops/lockfile.rs index 10387626481..5547a75059c 100644 --- a/src/cargo/ops/lockfile.rs +++ b/src/cargo/ops/lockfile.rs @@ -43,6 +43,9 @@ pub fn write_pkg_lockfile(ws: &Workspace, resolve: &Resolve) -> CargoResult<()> let mut out = String::new(); + // Preserve the top comments in the lockfile + // This is in preparation for marking it as generated + // https://github.com/rust-lang/cargo/issues/6180 if let Ok(orig) = &orig { for line in orig.lines().take_while(|line| line.starts_with("#")) { out.push_str(line);