Skip to content

Add test for publish with [patch] + cleanup. #6544

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 12, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions tests/testsuite/alt_registry.rs
Original file line number Diff line number Diff line change
@@ -467,6 +467,8 @@ fn block_publish_due_to_no_token() {
// Setup the registry by publishing a package
Package::new("bar", "0.0.1").alternative(true).publish();

fs::remove_file(paths::home().join(".cargo/credentials")).unwrap();

// Now perform the actual publish
p.cargo("publish --registry alternative -Zunstable-options")
.masquerade_as_nightly_cargo()
6 changes: 3 additions & 3 deletions tests/testsuite/cargo_features.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::support::{project, publish};
use crate::support::{project, registry};

#[test]
fn feature_required() {
@@ -295,7 +295,7 @@ fn z_flags_rejected() {

#[test]
fn publish_allowed() {
publish::setup();
registry::init();

let p = project()
.file(
@@ -312,7 +312,7 @@ fn publish_allowed() {
.file("src/lib.rs", "")
.build();
p.cargo("publish --index")
.arg(publish::registry().to_string())
.arg(registry::registry_url().to_string())
.masquerade_as_nightly_cargo()
.run();
}
8 changes: 4 additions & 4 deletions tests/testsuite/cross_publish.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::fs::File;

use crate::support::{cross_compile, project, publish};
use crate::support::{cross_compile, project, publish, registry};

#[test]
fn simple_cross_package() {
@@ -64,7 +64,7 @@ fn publish_with_target() {
return;
}

publish::setup();
registry::init();

let p = project()
.file(
@@ -96,7 +96,7 @@ fn publish_with_target() {
let target = cross_compile::alternate();

p.cargo("publish --index")
.arg(publish::registry().to_string())
.arg(registry::registry_url().to_string())
.arg("--target")
.arg(&target)
.with_stderr(&format!(
@@ -107,7 +107,7 @@ fn publish_with_target() {
Finished dev [unoptimized + debuginfo] target(s) in [..]
Uploading foo v0.0.0 ([CWD])
",
registry = publish::registry_path().to_str().unwrap()
registry = registry::registry_path().to_str().unwrap()
))
.run();
}
10 changes: 5 additions & 5 deletions tests/testsuite/login.rs
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@ use std::io::prelude::*;

use crate::support::cargo_process;
use crate::support::install::cargo_home;
use crate::support::registry::{self, registry};
use crate::support::registry::{self, registry_url};
use cargo::core::Shell;
use cargo::util::config::Config;
use toml;
@@ -62,7 +62,7 @@ fn login_with_old_credentials() {
registry::init();

cargo_process("login --host")
.arg(registry().to_string())
.arg(registry_url().to_string())
.arg(TOKEN)
.run();

@@ -76,7 +76,7 @@ fn login_with_new_credentials() {
setup_new_credentials();

cargo_process("login --host")
.arg(registry().to_string())
.arg(registry_url().to_string())
.arg(TOKEN)
.run();

@@ -94,7 +94,7 @@ fn login_with_old_and_new_credentials() {
fn login_without_credentials() {
registry::init();
cargo_process("login --host")
.arg(registry().to_string())
.arg(registry_url().to_string())
.arg(TOKEN)
.run();

@@ -108,7 +108,7 @@ fn new_credentials_is_used_instead_old() {
setup_new_credentials();

cargo_process("login --host")
.arg(registry().to_string())
.arg(registry_url().to_string())
.arg(TOKEN)
.run();

2 changes: 1 addition & 1 deletion tests/testsuite/package.rs
Original file line number Diff line number Diff line change
@@ -839,7 +839,7 @@ registry-index = "{}"
[dependencies.ghi]
version = "1.0"
"#,
registry::alt_registry()
registry::alt_registry_url()
);

validate_crate_contents(
164 changes: 109 additions & 55 deletions tests/testsuite/publish.rs

Large diffs are not rendered by default.

25 changes: 9 additions & 16 deletions tests/testsuite/registry.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,12 @@
use std::fs::{self, File};
use std::io::prelude::*;
use std::path::PathBuf;

use crate::support::cargo_process;
use crate::support::git;
use crate::support::paths::{self, CargoPathExt};
use crate::support::registry::{self, Dependency, Package};
use crate::support::registry::{self, registry_path, registry_url, Dependency, Package};
use crate::support::{basic_manifest, project};
use cargo::util::paths::remove_dir_all;
use url::Url;

fn registry_path() -> PathBuf {
paths::root().join("registry")
}
fn registry() -> Url {
Url::from_file_path(&*registry_path()).ok().unwrap()
}

#[test]
fn simple() {
@@ -47,7 +38,7 @@ fn simple() {
[COMPILING] foo v0.0.1 ([CWD])
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]s
",
reg = registry::registry_path().to_str().unwrap()
reg = registry_path().to_str().unwrap()
))
.run();

@@ -98,7 +89,7 @@ fn deps() {
[COMPILING] foo v0.0.1 ([CWD])
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]s
",
reg = registry::registry_path().to_str().unwrap()
reg = registry_path().to_str().unwrap()
))
.run();
}
@@ -336,7 +327,7 @@ required by package `foo v0.0.1 ([..])`
[COMPILING] foo v0.0.1 ([CWD])
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]s
",
reg = registry::registry_path().to_str().unwrap()
reg = registry_path().to_str().unwrap()
))
.run();
}
@@ -581,7 +572,7 @@ fn yanks_in_lockfiles_are_ok() {

p.cargo("build").run();

registry::registry_path().join("3").rm_rf();
registry_path().join("3").rm_rf();

Package::new("bar", "0.0.1").yanked(true).publish();

@@ -806,10 +797,12 @@ fn login_with_no_cargo_dir() {
fn login_with_differently_sized_token() {
// Verify that the configuration file gets properly truncated.
registry::init();
let credentials = paths::home().join(".cargo/credentials");
fs::remove_file(&credentials).unwrap();
cargo_process("login lmaolmaolmao -v").run();
cargo_process("login lmao -v").run();
cargo_process("login lmaolmaolmao -v").run();
let credentials = fs::read_to_string(paths::home().join(".cargo/credentials")).unwrap();
let credentials = fs::read_to_string(&credentials).unwrap();
assert_eq!(credentials, "[registry]\ntoken = \"lmaolmaolmao\"\n");
}

@@ -832,7 +825,7 @@ fn bad_license_file() {
.file("src/main.rs", "fn main() {}")
.build();
p.cargo("publish -v --index")
.arg(registry().to_string())
.arg(registry_url().to_string())
.with_status(101)
.with_stderr_contains("[ERROR] the license file `foo` does not exist")
.run();
2 changes: 1 addition & 1 deletion tests/testsuite/search.rs
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@ use std::path::Path;
use crate::support::cargo_process;
use crate::support::git::repo;
use crate::support::paths;
use crate::support::registry::{api_path, registry as registry_url, registry_path};
use crate::support::registry::{api_path, registry_path, registry_url};
use url::Url;

fn api() -> Url {
65 changes: 4 additions & 61 deletions tests/testsuite/support/publish.rs
Original file line number Diff line number Diff line change
@@ -1,75 +1,18 @@
use std::collections::{HashMap, HashSet};
use std::fs::{self, File};
use std::fs::File;
use std::io::{prelude::*, SeekFrom};
use std::path::{Path, PathBuf};

use crate::support::git::{repo, Repository};
use crate::support::registry::alt_api_path;
use crate::support::{find_json_mismatch, paths};
use crate::support::registry::{self, alt_api_path};
use crate::support::find_json_mismatch;

use byteorder::{LittleEndian, ReadBytesExt};
use flate2::read::GzDecoder;
use tar::Archive;
use url::Url;

pub fn setup() -> Repository {
let config = paths::root().join(".cargo/config");
t!(fs::create_dir_all(config.parent().unwrap()));
t!(t!(File::create(&config)).write_all(
format!(
r#"
[registry]
token = "api-token"
[registries.alternative]
index = "{registry}"
"#,
registry = registry().to_string()
)
.as_bytes()
));

let credentials = paths::root().join("home/.cargo/credentials");
t!(fs::create_dir_all(credentials.parent().unwrap()));
t!(t!(File::create(&credentials)).write_all(
br#"
[registries.alternative]
token = "api-token"
"#
));

t!(fs::create_dir_all(&upload_path().join("api/v1/crates")));

repo(&registry_path())
.file(
"config.json",
&format!(
r#"{{
"dl": "{0}",
"api": "{0}"
}}"#,
upload()
),
)
.build()
}

pub fn registry_path() -> PathBuf {
paths::root().join("registry")
}
pub fn registry() -> Url {
Url::from_file_path(&*registry_path()).ok().unwrap()
}
pub fn upload_path() -> PathBuf {
paths::root().join("upload")
}
fn upload() -> Url {
Url::from_file_path(&*upload_path()).ok().unwrap()
}

/// Check the result of a crate publish.
pub fn validate_upload(expected_json: &str, expected_crate_name: &str, expected_files: &[&str]) {
let new_path = upload_path().join("api/v1/crates/new");
let new_path = registry::api_path().join("api/v1/crates/new");
_validate_upload(
&new_path,
expected_json,
58 changes: 41 additions & 17 deletions tests/testsuite/support/registry.rs
Original file line number Diff line number Diff line change
@@ -15,39 +15,54 @@ use url::Url;
use crate::support::git::repo;
use crate::support::paths;

/// Path to the local index pretending to be crates.io. This is a git repo
/// initialized with a `config.json` file pointing to `dl_path` for downloads
/// and `api_path` for uploads.
pub fn registry_path() -> PathBuf {
paths::root().join("registry")
}
pub fn registry() -> Url {
Url::from_file_path(&*registry_path()).ok().unwrap()
pub fn registry_url() -> Url {
Url::from_file_path(registry_path()).ok().unwrap()
}
/// Path for local web API uploads. Cargo will place the contents of a web API
/// request here, for example `api/v1/crates/new` is the result of publishing
/// a crate.
pub fn api_path() -> PathBuf {
paths::root().join("api")
}
pub fn api_url() -> Url {
Url::from_file_path(api_path()).ok().unwrap()
}
/// Path where crates can be downloaded using the web API endpoint. Crates
/// should be organized as `{name}/{version}/download` to match the web API
/// endpoint. This is rarely used and must be manually set up.
pub fn dl_path() -> PathBuf {
paths::root().join("dl")
}
pub fn dl_url() -> Url {
Url::from_file_path(&*dl_path()).ok().unwrap()
Url::from_file_path(dl_path()).ok().unwrap()
}
/// Alternative-registry version of `registry_path`.
pub fn alt_registry_path() -> PathBuf {
paths::root().join("alternative-registry")
}
pub fn alt_registry() -> Url {
Url::from_file_path(&*alt_registry_path()).ok().unwrap()
pub fn alt_registry_url() -> Url {
Url::from_file_path(alt_registry_path()).ok().unwrap()
}
/// Alternative-registry version of `dl_path`.
pub fn alt_dl_path() -> PathBuf {
paths::root().join("alt_dl")
}
pub fn alt_dl_url() -> String {
let base = Url::from_file_path(&*alt_dl_path()).ok().unwrap();
let base = Url::from_file_path(alt_dl_path()).ok().unwrap();
format!("{}/{{crate}}/{{version}}/{{crate}}-{{version}}.crate", base)
}
/// Alternative-registry version of `api_path`.
pub fn alt_api_path() -> PathBuf {
paths::root().join("alt_api")
}
pub fn alt_api_url() -> Url {
Url::from_file_path(&*alt_api_path()).ok().unwrap()
Url::from_file_path(alt_api_path()).ok().unwrap()
}

/// A builder for creating a new package in a registry.
@@ -145,9 +160,6 @@ pub fn init() {
t!(t!(File::create(&config)).write_all(
format!(
r#"
[registry]
token = "api-token"
[source.crates-io]
registry = 'https://wut'
replace-with = 'dummy-registry'
@@ -158,25 +170,37 @@ pub fn init() {
[registries.alternative]
index = '{alt}'
"#,
reg = registry(),
alt = alt_registry()
reg = registry_url(),
alt = alt_registry_url()
)
.as_bytes()
));
let credentials = paths::home().join(".cargo/credentials");
t!(t!(File::create(&credentials)).write_all(
br#"
[registry]
token = "api-token"
[registries.alternative]
token = "api-token"
"#
));


// Init a new registry
let _ = repo(&registry_path())
.file(
"config.json",
&format!(
r#"
{{"dl":"{0}","api":"{0}"}}
{{"dl":"{}","api":"{}"}}
"#,
dl_url()
dl_url(),
api_url()
),
)
.build();
fs::create_dir_all(dl_path().join("api/v1/crates")).unwrap();
fs::create_dir_all(api_path().join("api/v1/crates")).unwrap();

// Init an alt registry
repo(&alt_registry_path())
@@ -338,7 +362,7 @@ impl Package {
let registry_url =
match (self.alternative, dep.registry.as_ref().map(|s| s.as_ref())) {
(false, None) => None,
(false, Some("alternative")) => Some(alt_registry().to_string()),
(false, Some("alternative")) => Some(alt_registry_url().to_string()),
(true, None) => Some(CRATES_IO_INDEX.to_string()),
(true, Some("alternative")) => None,
_ => panic!("registry_dep currently only supports `alternative`"),
@@ -455,7 +479,7 @@ impl Package {
));
if let Some(registry) = &dep.registry {
assert_eq!(registry, "alternative");
manifest.push_str(&format!("registry-index = \"{}\"", alt_registry()));
manifest.push_str(&format!("registry-index = \"{}\"", alt_registry_url()));
}
}