Skip to content

Commit

Permalink
Submit repo-relative readme path relative to crates.io
Browse files Browse the repository at this point in the history
Added unit tests for publishing with
- cargo.toml in root + readme in root
- cargo.toml in root + readme in subdir
- cargo.toml in subdir + readme next to it
- cargo.toml in subdir + readme in root (../README)

Should fix rust-lang/crates.io#3484 in
combination with rust-lang/crates.io#3861
  • Loading branch information
nipunn1313 committed Aug 28, 2021
1 parent 46fa867 commit bb7a63f
Show file tree
Hide file tree
Showing 2 changed files with 249 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/cargo/ops/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,20 @@ fn transmit(
.with_context(|| format!("failed to read `readme` file for package `{}`", pkg))
})
.transpose()?;

let readme_path_relative_to_repo = readme.as_ref().map(|readme| {
|| -> Option<String> {
let repo = git2::Repository::discover(pkg.root()).ok()?;
let repo_root = repo.workdir()?;
let rel =
paths::strip_prefix_canonical(pkg.root().join(readme), repo_root.to_path_buf())
.ok()?;
let rel = rel.to_str()?;
Some(rel.replace("\\", "/"))
}()
.unwrap_or(readme.clone())
});

if let Some(ref file) = *license_file {
if !pkg.root().join(file).exists() {
bail!("the license file `{}` does not exist", file)
Expand Down Expand Up @@ -281,7 +295,7 @@ fn transmit(
keywords: keywords.clone(),
categories: categories.clone(),
readme: readme_content,
readme_file: readme.clone(),
readme_file: readme_path_relative_to_repo,
repository: repository.clone(),
license: license.clone(),
license_file: license_file.clone(),
Expand Down
234 changes: 234 additions & 0 deletions tests/testsuite/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1800,3 +1800,237 @@ See [..]

validate_upload_bar();
}

#[cargo_test]
fn with_readme() {
registry::init();

let p = git::new("foo", |p| {
p.file(
"Cargo.toml",
r#"
[project]
name = "foo"
version = "0.0.1"
authors = []
license = "MIT"
description = "foo"
readme = "README"
"#,
)
.file("src/main.rs", "fn main() {}")
.file("README", "HELLO")
});

p.cargo("publish --no-verify --token sekrit").run();

publish::validate_upload(
r#"
{
"authors": [],
"badges": {},
"categories": [],
"deps": [],
"description": "foo",
"documentation": null,
"features": {},
"homepage": null,
"keywords": [],
"license": "MIT",
"license_file": null,
"links": null,
"name": "foo",
"readme": "HELLO",
"readme_file": "README",
"repository": null,
"vers": "0.0.1"
}
"#,
"foo-0.0.1.crate",
&[
"Cargo.lock",
"Cargo.toml",
"Cargo.toml.orig",
"src/main.rs",
"README",
".cargo_vcs_info.json",
],
);
}

#[cargo_test]
fn with_non_root_readme() {
registry::init();

let p = git::new("foo", |p| {
p.file(
"Cargo.toml",
r#"
[workspace]
members = ["inner"]
"#,
)
.file(
"inner/Cargo.toml",
r#"
[project]
name = "foo"
version = "0.0.1"
authors = []
license = "MIT"
description = "foo"
readme = "docs/README"
"#,
)
.file("inner/src/main.rs", "fn main() {}")
.file("inner/docs/README", "HELLO")
});

p.cargo("publish --no-verify --token sekrit")
.cwd("inner")
.run();

publish::validate_upload(
r#"
{
"authors": [],
"badges": {},
"categories": [],
"deps": [],
"description": "foo",
"documentation": null,
"features": {},
"homepage": null,
"keywords": [],
"license": "MIT",
"license_file": null,
"links": null,
"name": "foo",
"readme": "HELLO",
"readme_file": "inner/docs/README",
"repository": null,
"vers": "0.0.1"
}
"#,
"foo-0.0.1.crate",
&[
"Cargo.lock",
"Cargo.toml",
"Cargo.toml.orig",
"src/main.rs",
"docs/README",
".cargo_vcs_info.json",
],
);
}

#[cargo_test]
fn in_workspace_with_readme() {
registry::init();

let p = git::new("foo", |p| {
p.file(
"Cargo.toml",
r#"
[workspace]
members = ["foo", "bar"]
"#,
)
.file(
"foo/Cargo.toml",
r#"
[project]
name = "foo"
version = "0.0.1"
authors = []
license = "MIT"
description = "foo"
readme = "README"
"#,
)
.file("foo/src/main.rs", "fn main() {}")
.file("foo/README", "HELLO FOO")
.file(
"bar/Cargo.toml",
r#"
[project]
name = "bar"
version = "0.0.1"
authors = []
license = "MIT"
description = "bar"
workspace = ".."
readme = "../README"
"#,
)
.file("bar/src/main.rs", "fn main() {}")
.file("README", "HELLO WS")
});

p.cargo("publish --no-verify --token sekrit -p foo").run();
publish::validate_upload(
r#"
{
"authors": [],
"badges": {},
"categories": [],
"deps": [],
"description": "foo",
"documentation": null,
"features": {},
"homepage": null,
"keywords": [],
"license": "MIT",
"license_file": null,
"links": null,
"name": "foo",
"readme": "HELLO FOO",
"readme_file": "foo/README",
"repository": null,
"vers": "0.0.1"
}
"#,
"foo-0.0.1.crate",
&[
"Cargo.lock",
"Cargo.toml",
"Cargo.toml.orig",
"src/main.rs",
"README",
".cargo_vcs_info.json",
],
);

p.cargo("publish --no-verify --token sekrit -p bar").run();
publish::validate_upload(
r#"
{
"authors": [],
"badges": {},
"categories": [],
"deps": [],
"description": "bar",
"documentation": null,
"features": {},
"homepage": null,
"keywords": [],
"license": "MIT",
"license_file": null,
"links": null,
"name": "bar",
"readme": "HELLO WS",
"readme_file": "README",
"repository": null,
"vers": "0.0.1"
}
"#,
"bar-0.0.1.crate",
&[
"Cargo.lock",
"Cargo.toml",
"Cargo.toml.orig",
"src/main.rs",
".cargo_vcs_info.json",
],
);
}

0 comments on commit bb7a63f

Please sign in to comment.