Visual Studio Code, rebuilt in Rust
Rust
test(windows): run the pty tests through cmd.exe and PowerShell
rokartur1978ca5b7b91
diff --git a/native/crates/app/src/terminal/tests.rs b/native/crates/app/src/terminal/tests.rsindex 794ec41e8e1..5ad3cb4e965 100644--- a/native/crates/app/src/terminal/tests.rs+++ b/native/crates/app/src/terminal/tests.rs@@ -12,6 +12,16 @@ fn dark_colors() -> colors::TerminalColors {colors::TerminalColors::new(&crate::theme::load_theme(&[themes], "Dark Modern").unwrap())}+/// `sh -c`, on Windows `cmd.exe /c` with the process's environment, which cmd.exe needs (`SystemRoot`).+fn shell(sh_script: &str, cmd_script: &str) -> ShellLaunchConfig {+ let (executable, args, env) = if cfg!(windows) {+ ("cmd.exe", vec!["/c".into(), cmd_script.into()], std::env::vars().collect())+ } else {+ ("/bin/sh", vec!["-c".into(), sh_script.into()], HashMap::from([("PATH".into(), "/usr/bin:/bin".into())]))+ };+ ShellLaunchConfig { executable: executable.into(), args, cwd: std::env::temp_dir(), env, ..Default::default() }+}+fn screen_line(instance: &TerminalInstance, line: i32) -> String {let columns = instance.term.columns();(0..columns).map(|column| instance.term.grid()[Point::new(Line(line), Column(column))].c).collect::<String>().trim_end().to_owned()@@ -19,12 +29,17 @@ fn screen_line(instance: &TerminalInstance, line: i32) -> String {#[test]fn a_shell_runs_in_a_pty_and_its_output_reaches_the_screen() {- let launch = ShellLaunchConfig {- executable: "/bin/sh".into(),- args: vec!["-c".into(), "printf 'cols=%s\\n' \"$(tput cols)\"; printf '\\033[31mred\\033[0m'; exit 3".into()],- cwd: std::env::temp_dir(),- env: HashMap::from([("TERM".into(), "xterm-256color".into()), ("PATH".into(), "/usr/bin:/bin".into())]),- ..Default::default()+ let launch = if cfg!(windows) {+ let script = "Write-Output ('cols=' + $Host.UI.RawUI.WindowSize.Width); Write-Host -NoNewline ([char]27 + '[31mred' + [char]27 + '[0m'); exit 3";+ ShellLaunchConfig { executable: "powershell.exe".into(), args: vec!["-NoProfile".into(), "-Command".into(), script.into()], cwd: std::env::temp_dir(), env: std::env::vars().collect(), ..Default::default() }+ } else {+ ShellLaunchConfig {+ executable: "/bin/sh".into(),+ args: vec!["-c".into(), "printf 'cols=%s\\n' \"$(tput cols)\"; printf '\\033[31mred\\033[0m'; exit 3".into()],+ cwd: std::env::temp_dir(),+ env: HashMap::from([("TERM".into(), "xterm-256color".into()), ("PATH".into(), "/usr/bin:/bin".into())]),+ ..Default::default()+ }};let colors = dark_colors();let (mut instance, receiver) = TerminalInstance::new(1, launch, 40, 5, Config { scrolling_history: 1000, ..Config::default() });@@ -47,8 +62,8 @@ fn a_shell_runs_in_a_pty_and_its_output_reaches_the_screen() {/// childProcessMonitor.ts `hasChildProcesses`: a background job of the shell counts, a shell alone does not.#[test]fn a_background_job_is_a_child_process_of_the_shell() {- let has_children_once_started = |script: &str| {- let launch = ShellLaunchConfig { executable: "/bin/sh".into(), args: vec!["-c".into(), script.into()], cwd: std::env::temp_dir(), env: HashMap::from([("PATH".into(), "/usr/bin:/bin".into())]), ..Default::default() };+ let has_children_once_started = |sh_script: &str, cmd_script: &str| {+ let launch = shell(sh_script, cmd_script);let (instance, receiver) = TerminalInstance::new(1, launch, 40, 5, Config::default());let mut receiver = receiver.expect("the shell spawned");let deadline = Instant::now() + Duration::from_secs(10);@@ -61,8 +76,8 @@ fn a_background_job_is_a_child_process_of_the_shell() {}instance.has_child_processes()};- assert!(has_children_once_started("sleep 5 & echo started; read line"));- assert!(!has_children_once_started("echo started; read line"));+ assert!(has_children_once_started("sleep 5 & echo started; read line", "start /b ping -n 6 127.0.0.1 >nul & echo started & set /p line="));+ assert!(!has_children_once_started("echo started; read line", "echo started & set /p line="));}fn run_to_exit(instance: &mut TerminalInstance, mut receiver: futures::channel::mpsc::UnboundedReceiver<PtyEvent>, colors: &colors::TerminalColors) {@@ -78,24 +93,17 @@ fn run_to_exit(instance: &mut TerminalInstance, mut receiver: futures::channel::#[test]fn a_reused_terminal_runs_the_next_process_below_the_last_one_and_keeps_its_name() {- let launch = |script: &str| ShellLaunchConfig {- name: Some(script.into()),- executable: "/bin/sh".into(),- args: vec!["-c".into(), script.into()],- cwd: std::env::temp_dir(),- env: HashMap::from([("PATH".into(), "/usr/bin:/bin".into())]),- ..Default::default()- };+ let launch = |sh_script: &str, cmd_script: &str| ShellLaunchConfig { name: Some("first".into()), ..shell(sh_script, cmd_script) };let colors = dark_colors();- let (mut instance, receiver) = TerminalInstance::new(1, launch("printf one"), 40, 5, Config::default());+ let (mut instance, receiver) = TerminalInstance::new(1, launch("printf one", "<nul set /p =one"), 40, 5, Config::default());run_to_exit(&mut instance, receiver.expect("the shell spawned"), &colors);instance.waiting_on_exit = true;- let receiver = instance.reuse(launch("printf two; exit 2"), "next\r\n", &colors);+ let receiver = instance.reuse(ShellLaunchConfig { name: Some("second".into()), ..shell("printf two; exit 2", "<nul set /p =two & exit /b 2") }, "next\r\n", &colors);assert!(!instance.waiting_on_exit && instance.exit_code.is_none());run_to_exit(&mut instance, receiver.expect("the shell spawned again"), &colors);assert_eq!(instance.exit_code, Some(Some(2)));assert_eq!([0, 1, 2].map(|line| screen_line(&instance, line)), ["one", "next", "two"]);- assert_eq!(instance.title(), "printf one");+ assert_eq!(instance.title(), "first");}#[test]