Skip to content

Commit fae02e9

Browse files
committed
Submit repo-relative readme path relative to crates.io
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
1 parent 8da3cf7 commit fae02e9

File tree

2 files changed

+249
-1
lines changed

2 files changed

+249
-1
lines changed

src/cargo/ops/registry.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,20 @@ fn transmit(
242242
.with_context(|| format!("failed to read `readme` file for package `{}`", pkg))
243243
})
244244
.transpose()?;
245+
246+
let readme_path_relative_to_repo = readme.as_ref().map(|readme| {
247+
|| -> Option<String> {
248+
let repo = git2::Repository::discover(pkg.root()).ok()?;
249+
let repo_root = repo.workdir()?;
250+
let rel =
251+
paths::strip_prefix_canonical(pkg.root().join(readme), repo_root.to_path_buf())
252+
.ok()?;
253+
let rel = rel.to_str()?;
254+
Some(rel.replace("\\", "/"))
255+
}()
256+
.unwrap_or(readme.clone())
257+
});
258+
245259
if let Some(ref file) = *license_file {
246260
if !pkg.root().join(file).exists() {
247261
bail!("the license file `{}` does not exist", file)
@@ -281,7 +295,7 @@ fn transmit(
281295
keywords: keywords.clone(),
282296
categories: categories.clone(),
283297
readme: readme_content,
284-
readme_file: readme.clone(),
298+
readme_file: readme_path_relative_to_repo,
285299
repository: repository.clone(),
286300
license: license.clone(),
287301
license_file: license_file.clone(),

tests/testsuite/publish.rs

Lines changed: 234 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1800,3 +1800,237 @@ See [..]
18001800

18011801
validate_upload_bar();
18021802
}
1803+
1804+
#[cargo_test]
1805+
fn with_readme() {
1806+
registry::init();
1807+
1808+
let p = git::new("foo", |p| {
1809+
p.file(
1810+
"Cargo.toml",
1811+
r#"
1812+
[project]
1813+
name = "foo"
1814+
version = "0.0.1"
1815+
authors = []
1816+
license = "MIT"
1817+
description = "foo"
1818+
readme = "README"
1819+
"#,
1820+
)
1821+
.file("src/main.rs", "fn main() {}")
1822+
.file("README", "HELLO")
1823+
});
1824+
1825+
p.cargo("publish --no-verify --token sekrit").run();
1826+
1827+
publish::validate_upload(
1828+
r#"
1829+
{
1830+
"authors": [],
1831+
"badges": {},
1832+
"categories": [],
1833+
"deps": [],
1834+
"description": "foo",
1835+
"documentation": null,
1836+
"features": {},
1837+
"homepage": null,
1838+
"keywords": [],
1839+
"license": "MIT",
1840+
"license_file": null,
1841+
"links": null,
1842+
"name": "foo",
1843+
"readme": "HELLO",
1844+
"readme_file": "README",
1845+
"repository": null,
1846+
"vers": "0.0.1"
1847+
}
1848+
"#,
1849+
"foo-0.0.1.crate",
1850+
&[
1851+
"Cargo.lock",
1852+
"Cargo.toml",
1853+
"Cargo.toml.orig",
1854+
"src/main.rs",
1855+
"README",
1856+
".cargo_vcs_info.json",
1857+
],
1858+
);
1859+
}
1860+
1861+
#[cargo_test]
1862+
fn with_non_root_readme() {
1863+
registry::init();
1864+
1865+
let p = git::new("foo", |p| {
1866+
p.file(
1867+
"Cargo.toml",
1868+
r#"
1869+
[workspace]
1870+
members = ["inner"]
1871+
"#,
1872+
)
1873+
.file(
1874+
"inner/Cargo.toml",
1875+
r#"
1876+
[project]
1877+
name = "foo"
1878+
version = "0.0.1"
1879+
authors = []
1880+
license = "MIT"
1881+
description = "foo"
1882+
readme = "docs/README"
1883+
"#,
1884+
)
1885+
.file("inner/src/main.rs", "fn main() {}")
1886+
.file("inner/docs/README", "HELLO")
1887+
});
1888+
1889+
p.cargo("publish --no-verify --token sekrit")
1890+
.cwd("inner")
1891+
.run();
1892+
1893+
publish::validate_upload(
1894+
r#"
1895+
{
1896+
"authors": [],
1897+
"badges": {},
1898+
"categories": [],
1899+
"deps": [],
1900+
"description": "foo",
1901+
"documentation": null,
1902+
"features": {},
1903+
"homepage": null,
1904+
"keywords": [],
1905+
"license": "MIT",
1906+
"license_file": null,
1907+
"links": null,
1908+
"name": "foo",
1909+
"readme": "HELLO",
1910+
"readme_file": "inner/docs/README",
1911+
"repository": null,
1912+
"vers": "0.0.1"
1913+
}
1914+
"#,
1915+
"foo-0.0.1.crate",
1916+
&[
1917+
"Cargo.lock",
1918+
"Cargo.toml",
1919+
"Cargo.toml.orig",
1920+
"src/main.rs",
1921+
"docs/README",
1922+
".cargo_vcs_info.json",
1923+
],
1924+
);
1925+
}
1926+
1927+
#[cargo_test]
1928+
fn in_workspace_with_readme() {
1929+
registry::init();
1930+
1931+
let p = git::new("foo", |p| {
1932+
p.file(
1933+
"Cargo.toml",
1934+
r#"
1935+
[workspace]
1936+
members = ["foo", "bar"]
1937+
"#,
1938+
)
1939+
.file(
1940+
"foo/Cargo.toml",
1941+
r#"
1942+
[project]
1943+
name = "foo"
1944+
version = "0.0.1"
1945+
authors = []
1946+
license = "MIT"
1947+
description = "foo"
1948+
readme = "README"
1949+
"#,
1950+
)
1951+
.file("foo/src/main.rs", "fn main() {}")
1952+
.file("foo/README", "HELLO FOO")
1953+
.file(
1954+
"bar/Cargo.toml",
1955+
r#"
1956+
[project]
1957+
name = "bar"
1958+
version = "0.0.1"
1959+
authors = []
1960+
license = "MIT"
1961+
description = "bar"
1962+
workspace = ".."
1963+
readme = "../README"
1964+
"#,
1965+
)
1966+
.file("bar/src/main.rs", "fn main() {}")
1967+
.file("README", "HELLO WS")
1968+
});
1969+
1970+
p.cargo("publish --no-verify --token sekrit -p foo").run();
1971+
publish::validate_upload(
1972+
r#"
1973+
{
1974+
"authors": [],
1975+
"badges": {},
1976+
"categories": [],
1977+
"deps": [],
1978+
"description": "foo",
1979+
"documentation": null,
1980+
"features": {},
1981+
"homepage": null,
1982+
"keywords": [],
1983+
"license": "MIT",
1984+
"license_file": null,
1985+
"links": null,
1986+
"name": "foo",
1987+
"readme": "HELLO FOO",
1988+
"readme_file": "foo/README",
1989+
"repository": null,
1990+
"vers": "0.0.1"
1991+
}
1992+
"#,
1993+
"foo-0.0.1.crate",
1994+
&[
1995+
"Cargo.lock",
1996+
"Cargo.toml",
1997+
"Cargo.toml.orig",
1998+
"src/main.rs",
1999+
"README",
2000+
".cargo_vcs_info.json",
2001+
],
2002+
);
2003+
2004+
p.cargo("publish --no-verify --token sekrit -p bar").run();
2005+
publish::validate_upload(
2006+
r#"
2007+
{
2008+
"authors": [],
2009+
"badges": {},
2010+
"categories": [],
2011+
"deps": [],
2012+
"description": "bar",
2013+
"documentation": null,
2014+
"features": {},
2015+
"homepage": null,
2016+
"keywords": [],
2017+
"license": "MIT",
2018+
"license_file": null,
2019+
"links": null,
2020+
"name": "bar",
2021+
"readme": "HELLO WS",
2022+
"readme_file": "README",
2023+
"repository": null,
2024+
"vers": "0.0.1"
2025+
}
2026+
"#,
2027+
"bar-0.0.1.crate",
2028+
&[
2029+
"Cargo.lock",
2030+
"Cargo.toml",
2031+
"Cargo.toml.orig",
2032+
"src/main.rs",
2033+
".cargo_vcs_info.json",
2034+
],
2035+
);
2036+
}

0 commit comments

Comments
 (0)