-
Notifications
You must be signed in to change notification settings - Fork 7.9k
fix: integration test for #9011 #9166
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| // Aggregates all former standalone integration tests as modules. | ||
| mod no_panic_on_startup; | ||
| mod status_indicator; | ||
| mod vt100_history; | ||
| mod vt100_live_commit; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| use std::collections::HashMap; | ||
| use std::path::Path; | ||
| use std::time::Duration; | ||
| use tokio::select; | ||
| use tokio::time::timeout; | ||
|
|
||
| /// Regression test for https://github.com/openai/codex/issues/8803. | ||
| #[tokio::test] | ||
| async fn malformed_rules_should_not_panic() -> anyhow::Result<()> { | ||
| // run_codex_cli() does not work on Windows due to PTY limitations. | ||
| if cfg!(windows) { | ||
| return Ok(()); | ||
| } | ||
|
|
||
| let tmp = tempfile::tempdir()?; | ||
| let codex_home = tmp.path(); | ||
| std::fs::write( | ||
| codex_home.join("rules"), | ||
| "rules should be a directory not a file", | ||
| )?; | ||
|
|
||
| // TODO(mbolin): Figure out why using a temp dir as the cwd causes this test | ||
| // to hang. | ||
| let cwd = std::env::current_dir()?; | ||
| let config_contents = format!( | ||
| r#" | ||
| # Pick a local provider so the CLI doesn't prompt for OpenAI auth in this test. | ||
| model_provider = "ollama" | ||
|
|
||
| [projects] | ||
| "{cwd}" = {{ trust_level = "trusted" }} | ||
| "#, | ||
| cwd = cwd.display() | ||
| ); | ||
| std::fs::write(codex_home.join("config.toml"), config_contents)?; | ||
|
|
||
| let CodexCliOutput { exit_code, output } = run_codex_cli(codex_home, cwd).await?; | ||
| assert_eq!(1, exit_code, "Codex CLI should exit nonzero."); | ||
| assert!( | ||
| output.contains("ERROR: Failed to initialize codex:"), | ||
| "expected startup error in output, got: {output}" | ||
| ); | ||
| assert!( | ||
| output.contains("failed to read execpolicy files"), | ||
| "expected execpolicy read error in output, got: {output}" | ||
| ); | ||
| Ok(()) | ||
| } | ||
|
|
||
| struct CodexCliOutput { | ||
| exit_code: i32, | ||
| output: String, | ||
| } | ||
|
|
||
| async fn run_codex_cli( | ||
| codex_home: impl AsRef<Path>, | ||
| cwd: impl AsRef<Path>, | ||
| ) -> anyhow::Result<CodexCliOutput> { | ||
| let codex_cli = codex_utils_cargo_bin::cargo_bin("codex")?; | ||
| let mut env = HashMap::new(); | ||
| env.insert( | ||
| "CODEX_HOME".to_string(), | ||
| codex_home.as_ref().display().to_string(), | ||
| ); | ||
|
|
||
| let args = vec!["-c".to_string(), "analytics_enabled=false".to_string()]; | ||
| let spawned = codex_utils_pty::spawn_pty_process( | ||
| codex_cli.to_string_lossy().as_ref(), | ||
| &args, | ||
| cwd.as_ref(), | ||
| &env, | ||
| &None, | ||
| ) | ||
| .await?; | ||
| let mut output = Vec::new(); | ||
| let mut output_rx = spawned.output_rx; | ||
| let mut exit_rx = spawned.exit_rx; | ||
| let writer_tx = spawned.session.writer_sender(); | ||
| let exit_code_result = timeout(Duration::from_secs(10), async { | ||
| // Read PTY output until the process exits while replying to cursor | ||
| // position queries so the TUI can initialize without a real terminal. | ||
| loop { | ||
| select! { | ||
| result = output_rx.recv() => match result { | ||
| Ok(chunk) => { | ||
| // The TUI asks for the cursor position via ESC[6n. | ||
| // Respond with a valid position to unblock startup. | ||
| if chunk.windows(4).any(|window| window == b"\x1b[6n") { | ||
| let _ = writer_tx.send(b"\x1b[1;1R".to_vec()).await; | ||
| } | ||
|
Comment on lines
+88
to
+90
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is this? |
||
| output.extend_from_slice(&chunk); | ||
| } | ||
| Err(tokio::sync::broadcast::error::RecvError::Closed) => break exit_rx.await, | ||
| Err(tokio::sync::broadcast::error::RecvError::Lagged(_)) => {} | ||
| }, | ||
| result = &mut exit_rx => break result, | ||
| } | ||
| } | ||
| }) | ||
| .await; | ||
| let exit_code = match exit_code_result { | ||
| Ok(Ok(code)) => code, | ||
| Ok(Err(err)) => return Err(err.into()), | ||
| Err(_) => { | ||
| spawned.session.terminate(); | ||
| anyhow::bail!("timed out waiting for codex CLI to exit"); | ||
| } | ||
| }; | ||
| // Drain any output that raced with the exit notification. | ||
| while let Ok(chunk) = output_rx.try_recv() { | ||
| output.extend_from_slice(&chunk); | ||
| } | ||
|
|
||
| let output = String::from_utf8_lossy(&output); | ||
| Ok(CodexCliOutput { | ||
| exit_code, | ||
| output: output.to_string(), | ||
| }) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| // Aggregates all former standalone integration tests as modules. | ||
| mod no_panic_on_startup; | ||
| mod status_indicator; | ||
| mod vt100_history; | ||
| mod vt100_live_commit; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| use std::collections::HashMap; | ||
| use std::path::Path; | ||
| use std::time::Duration; | ||
| use tokio::select; | ||
| use tokio::time::timeout; | ||
|
|
||
| /// Regression test for https://github.com/openai/codex/issues/8803. | ||
| #[tokio::test] | ||
| async fn malformed_rules_should_not_panic() -> anyhow::Result<()> { | ||
| // run_codex_cli() does not work on Windows due to PTY limitations. | ||
| if cfg!(windows) { | ||
| return Ok(()); | ||
| } | ||
|
|
||
| let tmp = tempfile::tempdir()?; | ||
| let codex_home = tmp.path(); | ||
| std::fs::write( | ||
| codex_home.join("rules"), | ||
| "rules should be a directory not a file", | ||
| )?; | ||
|
|
||
| // TODO(mbolin): Figure out why using a temp dir as the cwd causes this test | ||
| // to hang. | ||
| let cwd = std::env::current_dir()?; | ||
| let config_contents = format!( | ||
| r#" | ||
| # Pick a local provider so the CLI doesn't prompt for OpenAI auth in this test. | ||
| model_provider = "ollama" | ||
|
|
||
| [projects] | ||
| "{cwd}" = {{ trust_level = "trusted" }} | ||
| "#, | ||
| cwd = cwd.display() | ||
| ); | ||
| std::fs::write(codex_home.join("config.toml"), config_contents)?; | ||
|
|
||
| let CodexCliOutput { exit_code, output } = run_codex_cli(codex_home, cwd).await?; | ||
| assert_eq!(1, exit_code, "Codex CLI should exit nonzero."); | ||
| assert!( | ||
| output.contains("ERROR: Failed to initialize codex:"), | ||
| "expected startup error in output, got: {output}" | ||
| ); | ||
| assert!( | ||
| output.contains("failed to read execpolicy files"), | ||
| "expected execpolicy read error in output, got: {output}" | ||
| ); | ||
| Ok(()) | ||
| } | ||
|
|
||
| struct CodexCliOutput { | ||
| exit_code: i32, | ||
| output: String, | ||
| } | ||
|
|
||
| async fn run_codex_cli( | ||
| codex_home: impl AsRef<Path>, | ||
| cwd: impl AsRef<Path>, | ||
| ) -> anyhow::Result<CodexCliOutput> { | ||
| let codex_cli = codex_utils_cargo_bin::cargo_bin("codex")?; | ||
| let mut env = HashMap::new(); | ||
| env.insert( | ||
| "CODEX_HOME".to_string(), | ||
| codex_home.as_ref().display().to_string(), | ||
| ); | ||
|
|
||
| let args = vec!["-c".to_string(), "analytics_enabled=false".to_string()]; | ||
| let spawned = codex_utils_pty::spawn_pty_process( | ||
| codex_cli.to_string_lossy().as_ref(), | ||
| &args, | ||
| cwd.as_ref(), | ||
| &env, | ||
| &None, | ||
| ) | ||
| .await?; | ||
| let mut output = Vec::new(); | ||
| let mut output_rx = spawned.output_rx; | ||
| let mut exit_rx = spawned.exit_rx; | ||
| let writer_tx = spawned.session.writer_sender(); | ||
| let exit_code_result = timeout(Duration::from_secs(10), async { | ||
| // Read PTY output until the process exits while replying to cursor | ||
| // position queries so the TUI can initialize without a real terminal. | ||
| loop { | ||
| select! { | ||
| result = output_rx.recv() => match result { | ||
| Ok(chunk) => { | ||
| // The TUI asks for the cursor position via ESC[6n. | ||
| // Respond with a valid position to unblock startup. | ||
| if chunk.windows(4).any(|window| window == b"\x1b[6n") { | ||
| let _ = writer_tx.send(b"\x1b[1;1R".to_vec()).await; | ||
| } | ||
| output.extend_from_slice(&chunk); | ||
| } | ||
| Err(tokio::sync::broadcast::error::RecvError::Closed) => break exit_rx.await, | ||
| Err(tokio::sync::broadcast::error::RecvError::Lagged(_)) => {} | ||
| }, | ||
| result = &mut exit_rx => break result, | ||
| } | ||
| } | ||
| }) | ||
| .await; | ||
| let exit_code = match exit_code_result { | ||
| Ok(Ok(code)) => code, | ||
| Ok(Err(err)) => return Err(err.into()), | ||
| Err(_) => { | ||
| spawned.session.terminate(); | ||
| anyhow::bail!("timed out waiting for codex CLI to exit"); | ||
| } | ||
| }; | ||
| // Drain any output that raced with the exit notification. | ||
| while let Ok(chunk) = output_rx.try_recv() { | ||
| output.extend_from_slice(&chunk); | ||
| } | ||
|
|
||
| let output = String::from_utf8_lossy(&output); | ||
| Ok(CodexCliOutput { | ||
| exit_code, | ||
| output: output.to_string(), | ||
| }) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be good to have a small amount of inline / doc comments on this one to help give details about how it works.
Right now all I could really say is that there's a bunch of code that seems to work and codex understood it when it wrote it. I'd have to read every line to understand this code and then infer constraints / edge cases that are encoded here.