Skip to content

Commit

Permalink
add test with pty
Browse files Browse the repository at this point in the history
  • Loading branch information
ericmarkmartin committed Feb 23, 2025
1 parent 6583e12 commit 549329c
Show file tree
Hide file tree
Showing 5 changed files with 179 additions and 7 deletions.
105 changes: 99 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/uv/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ flate2 = { workspace = true, default-features = false }
ignore = { version = "0.4.23" }
indoc = { workspace = true }
insta = { version = "1.40.0", features = ["filters", "json"] }
portable-pty = "0.9.0"
predicates = { version = "3.1.2" }
regex = { workspace = true }
reqwest = { workspace = true, features = ["blocking"], default-features = false }
Expand Down
10 changes: 10 additions & 0 deletions crates/uv/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,16 @@ async fn run(mut cli: Cli) -> Result<ExitStatus> {

anstream::ColorChoice::write_global(globals.color.into());

(match globals.color {
uv_cli::ColorChoice::Auto => None,
uv_cli::ColorChoice::Always => Some(true),
uv_cli::ColorChoice::Never => Some(false),
})
.inspect(|colors_enabled| {
console::set_colors_enabled(*colors_enabled);
console::set_colors_enabled_stderr(*colors_enabled);
});

miette::set_hook(Box::new(|_| {
Box::new(
miette::MietteHandlerOpts::new()
Expand Down
64 changes: 63 additions & 1 deletion crates/uv/tests/it/pip_install.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::io::Cursor;
use std::io::{Cursor, Read};
use std::process::Command;

use anyhow::Result;
Expand Down Expand Up @@ -9097,3 +9097,65 @@ fn unsupported_git_scheme() {
"###
);
}

#[test]
fn ctrl_c_install_confirmation_prompt() -> Result<()> {
let pty_sys = portable_pty::native_pty_system();

let pair = pty_sys.openpty(portable_pty::PtySize::default())?;

let context = TestContext::new("3.12");

let pyproject_toml = context.temp_dir.child("pyproject.toml");
pyproject_toml.touch()?;

let mut cmd = context.pip_install();
cmd.arg("pyproject.toml");
cmd.args(&["--color", "never"]);

let mut argv = Vec::new();

argv.push(cmd.get_program().into());
argv.extend(cmd.get_args().map(Into::into));

let mut cmdbuild = portable_pty::CommandBuilder::from_argv(argv);

for (key, value) in cmd.get_envs() {
match value {
Some(value) => {
cmdbuild.env(key, value);
}
None => {
cmdbuild.env_remove(key);
}
}
}

match cmd.get_current_dir() {
Some(cwd) => cmdbuild.cwd(cwd),
None => cmdbuild.clear_cwd(),
};

let mut child = pair.slave.spawn_command(cmdbuild)?;
let mut reader = pair.master.try_clone_reader()?;
let mut writer = pair.master.take_writer()?;

const EXPECTED: &'static str = "? `pyproject.toml` looks like a local metadata file but was passed as a package name. Did you mean `-r pyproject.toml`? [y/n] › yes";

let mut buffer = [0u8; EXPECTED.len()];
reader.read_exact(&mut buffer)?;
let output = String::from_utf8_lossy(&buffer).to_string();

insta::assert_snapshot!(output, EXPECTED);

// 0x03 = ETX
writer.write_all(&[0x03])?;
writer.flush()?;

let exit_status = child.wait()?;

let expected_exit_code = if cfg!(windows) { 0xC000_013A_u32 } else { 130 };
assert_eq!(exit_status.exit_code(), expected_exit_code);

Ok(())
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
source: crates/uv/tests/it/pip_install.rs
expression: EXPECTED
snapshot_kind: text
---
? `pyproject.toml` looks like a local metadata file but was passed as a package name. Did you mean `-r pyproject.toml`? [y/n] › yes

0 comments on commit 549329c

Please sign in to comment.