Skip to content
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

Improve code coverage #10460

Merged
merged 2 commits into from
Mar 7, 2022
Merged
Show file tree
Hide file tree
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
20 changes: 0 additions & 20 deletions src/cargo/core/source/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,11 +264,6 @@ impl<'src> SourceMap<'src> {
}
}

/// Like `HashMap::contains_key`.
pub fn contains(&self, id: SourceId) -> bool {
self.map.contains_key(&id)
}

/// Like `HashMap::get`.
pub fn get(&self, id: SourceId) -> Option<&(dyn Source + 'src)> {
self.map.get(&id).map(|s| s.as_ref())
Expand All @@ -279,32 +274,17 @@ impl<'src> SourceMap<'src> {
self.map.get_mut(&id).map(|s| s.as_mut())
}

/// Like `HashMap::get`, but first calculates the `SourceId` from a `PackageId`.
pub fn get_by_package_id(&self, pkg_id: PackageId) -> Option<&(dyn Source + 'src)> {
self.get(pkg_id.source_id())
}

/// Like `HashMap::insert`, but derives the `SourceId` key from the `Source`.
pub fn insert(&mut self, source: Box<dyn Source + 'src>) {
let id = source.source_id();
self.map.insert(id, source);
}

/// Like `HashMap::is_empty`.
pub fn is_empty(&self) -> bool {
self.map.is_empty()
}

/// Like `HashMap::len`.
pub fn len(&self) -> usize {
self.map.len()
}

/// Like `HashMap::values`.
pub fn sources<'a>(&'a self) -> impl Iterator<Item = &'a Box<dyn Source + 'src>> {
self.map.values()
}

/// Like `HashMap::iter_mut`.
pub fn sources_mut<'a>(
&'a mut self,
Expand Down
32 changes: 32 additions & 0 deletions tests/testsuite/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ use cargo_test_support::{cargo_process, registry::registry_url};
use cargo_test_support::{git, install::cargo_home, t};
use cargo_util::paths::remove_dir_all;
use std::fs::{self, File};
use std::io::{BufRead, BufReader, Write};
use std::path::Path;
use std::process::Stdio;

#[cargo_test]
fn simple() {
Expand Down Expand Up @@ -883,6 +885,36 @@ fn login_with_differently_sized_token() {
assert_eq!(credentials, "[registry]\ntoken = \"lmaolmaolmao\"\n");
}

#[cargo_test]
fn login_with_token_on_stdin() {
registry::init();
let credentials = paths::home().join(".cargo/credentials");
fs::remove_file(&credentials).unwrap();
cargo_process("login lmao -v").run();
let mut cargo = cargo_process("login").build_command();
cargo
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped());
let mut child = cargo.spawn().unwrap();
let out = BufReader::new(child.stdout.as_mut().unwrap())
.lines()
.next()
.unwrap()
.unwrap();
assert!(out.starts_with("please paste the API Token found on "));
assert!(out.ends_with("/me below"));
child
.stdin
.as_ref()
.unwrap()
.write_all(b"some token\n")
.unwrap();
child.wait().unwrap();
let credentials = fs::read_to_string(&credentials).unwrap();
assert_eq!(credentials, "[registry]\ntoken = \"some token\"\n");
}

#[cargo_test]
fn bad_license_file() {
Package::new("foo", "1.0.0").publish();
Expand Down