Skip to content

Commit 902bb0c

Browse files
committed
Auto merge of #11111 - Muscraft:http-publish-not-noop, r=ehuss
Http publish not noop Currently the `cargo-test-support` `HttpServer` is noop on publish. This was causing issues with #11062 as there is [not function registry to pull from](#11062 (comment)). [A suggested fix](#11062 (comment)) to this was to have the test `HttpServer` act like a real registry and write to the filesystem. This would allow for tests to be run over the HTTP API and not fail since there was nothing to pull from. This PR implements that suggestion by adding a body field to `Request`, and when hitting the publish endpoint it will try and write the `.crate` and manifest information to the filesystem.
2 parents 9230e48 + 251a2c7 commit 902bb0c

File tree

5 files changed

+254
-85
lines changed

5 files changed

+254
-85
lines changed

crates/cargo-test-support/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ doctest = false
1111
anyhow = "1.0.34"
1212
cargo-test-macro = { path = "../cargo-test-macro" }
1313
cargo-util = { path = "../cargo-util" }
14+
crates-io = { path = "../crates-io" }
1415
snapbox = { version = "0.3.0", features = ["diff", "path"] }
1516
filetime = "0.2"
1617
flate2 = { version = "1.0", default-features = false, features = ["zlib"] }

crates/cargo-test-support/src/publish.rs

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use crate::compare::{assert_match_exact, find_json_mismatch};
2-
use crate::registry::{self, alt_api_path};
2+
use crate::registry::{self, alt_api_path, FeatureMap};
33
use flate2::read::GzDecoder;
44
use std::collections::{HashMap, HashSet};
5+
use std::fs;
56
use std::fs::File;
67
use std::io::{self, prelude::*, SeekFrom};
78
use std::path::{Path, PathBuf};
@@ -155,3 +156,90 @@ pub fn validate_crate_contents(
155156
}
156157
}
157158
}
159+
160+
pub(crate) fn create_index_line(
161+
name: serde_json::Value,
162+
vers: &str,
163+
deps: Vec<serde_json::Value>,
164+
cksum: &str,
165+
features: crate::registry::FeatureMap,
166+
yanked: bool,
167+
links: Option<String>,
168+
v: Option<u32>,
169+
) -> String {
170+
// This emulates what crates.io does to retain backwards compatibility.
171+
let (features, features2) = split_index_features(features.clone());
172+
let mut json = serde_json::json!({
173+
"name": name,
174+
"vers": vers,
175+
"deps": deps,
176+
"cksum": cksum,
177+
"features": features,
178+
"yanked": yanked,
179+
"links": links,
180+
});
181+
if let Some(f2) = &features2 {
182+
json["features2"] = serde_json::json!(f2);
183+
json["v"] = serde_json::json!(2);
184+
}
185+
if let Some(v) = v {
186+
json["v"] = serde_json::json!(v);
187+
}
188+
189+
json.to_string()
190+
}
191+
192+
pub(crate) fn write_to_index(registry_path: &PathBuf, name: &str, line: String, local: bool) {
193+
let file = cargo_util::registry::make_dep_path(name, false);
194+
195+
// Write file/line in the index.
196+
let dst = if local {
197+
registry_path.join("index").join(&file)
198+
} else {
199+
registry_path.join(&file)
200+
};
201+
let prev = fs::read_to_string(&dst).unwrap_or_default();
202+
t!(fs::create_dir_all(dst.parent().unwrap()));
203+
t!(fs::write(&dst, prev + &line[..] + "\n"));
204+
205+
// Add the new file to the index.
206+
if !local {
207+
let repo = t!(git2::Repository::open(&registry_path));
208+
let mut index = t!(repo.index());
209+
t!(index.add_path(Path::new(&file)));
210+
t!(index.write());
211+
let id = t!(index.write_tree());
212+
213+
// Commit this change.
214+
let tree = t!(repo.find_tree(id));
215+
let sig = t!(repo.signature());
216+
let parent = t!(repo.refname_to_id("refs/heads/master"));
217+
let parent = t!(repo.find_commit(parent));
218+
t!(repo.commit(
219+
Some("HEAD"),
220+
&sig,
221+
&sig,
222+
"Another commit",
223+
&tree,
224+
&[&parent]
225+
));
226+
}
227+
}
228+
229+
fn split_index_features(mut features: FeatureMap) -> (FeatureMap, Option<FeatureMap>) {
230+
let mut features2 = FeatureMap::new();
231+
for (feat, values) in features.iter_mut() {
232+
if values
233+
.iter()
234+
.any(|value| value.starts_with("dep:") || value.contains("?/"))
235+
{
236+
let new_values = values.drain(..).collect();
237+
features2.insert(feat.clone(), new_values);
238+
}
239+
}
240+
if features2.is_empty() {
241+
(features, None)
242+
} else {
243+
(features, Some(features2))
244+
}
245+
}

0 commit comments

Comments
 (0)