Files
codex/prs/bolinfest/PR-2586.md
2025-09-02 15:17:45 -07:00

252 lines
9.9 KiB
Markdown

# PR #2586: test: simplify tests in config.rs
- URL: https://github.com/openai/codex/pull/2586
- Author: nornagon-openai
- Created: 2025-08-22 16:49:57 UTC
- Updated: 2025-08-22 21:04:30 UTC
- Changes: +37/-51, Files changed: 1, Commits: 4
## Description
this is much easier to read, thanks @bolinfest for the suggestion.
## Full Diff
```diff
diff --git a/codex-rs/core/src/config.rs b/codex-rs/core/src/config.rs
index 5b6a1ed265..f4dfc7f2bb 100644
--- a/codex-rs/core/src/config.rs
+++ b/codex-rs/core/src/config.rs
@@ -274,12 +274,14 @@ pub fn set_project_trusted(codex_home: &Path, project_path: &Path) -> anyhow::Re
// Ensure top-level `projects` exists as a non-inline, explicit table. If it
// exists but was previously represented as a non-table (e.g., inline),
// replace it with an explicit table.
+ let mut created_projects_table = false;
{
let root = doc.as_table_mut();
let needs_table = !root.contains_key("projects")
|| root.get("projects").and_then(|i| i.as_table()).is_none();
if needs_table {
root.insert("projects", toml_edit::table());
+ created_projects_table = true;
}
}
let Some(projects_tbl) = doc["projects"].as_table_mut() else {
@@ -288,6 +290,12 @@ pub fn set_project_trusted(codex_home: &Path, project_path: &Path) -> anyhow::Re
));
};
+ // If we created the `projects` table ourselves, keep it implicit so we
+ // don't render a standalone `[projects]` header.
+ if created_projects_table {
+ projects_tbl.set_implicit(true);
+ }
+
// Ensure the per-project entry is its own explicit table. If it exists but
// is not a table (e.g., an inline table), replace it with an explicit table.
let needs_proj_table = !projects_tbl.contains_key(project_key.as_str())
@@ -1230,37 +1238,22 @@ disable_response_storage = true
// Call the function under test
set_project_trusted(codex_home.path(), project_dir.path())?;
- // Read back the generated config.toml
+ // Read back the generated config.toml and assert exact contents
let config_path = codex_home.path().join(CONFIG_TOML_FILE);
let contents = std::fs::read_to_string(&config_path)?;
- // Verify it does not use inline tables for the project entry
- assert!(
- !contents.contains("{ trust_level"),
- "config.toml should not use inline tables:\n{}",
- contents
- );
-
- // Verify the explicit table for the project exists. toml_edit may choose
- // either basic (double-quoted) or literal (single-quoted) strings for keys
- // containing backslashes (e.g., on Windows). Accept both forms.
- let path_str = project_dir.path().to_string_lossy();
- let project_key_double = format!("[projects.\"{}\"]", path_str);
- let project_key_single = format!("[projects.'{}']", path_str);
- assert!(
- contents.contains(&project_key_double) || contents.contains(&project_key_single),
- "missing explicit project table header: expected to find `{}` or `{}` in:\n{}",
- project_key_double,
- project_key_single,
- contents
- );
-
- // Verify the trust_level entry
- assert!(
- contents.contains("trust_level = \"trusted\""),
- "missing trust_level entry in:\n{}",
- contents
+ let raw_path = project_dir.path().to_string_lossy();
+ let path_str = if raw_path.contains('\\') {
+ format!("'{}'", raw_path)
+ } else {
+ format!("\"{}\"", raw_path)
+ };
+ let expected = format!(
+ r#"[projects.{path_str}]
+trust_level = "trusted"
+"#
);
+ assert_eq!(contents, expected);
Ok(())
}
@@ -1272,11 +1265,17 @@ disable_response_storage = true
// Seed config.toml with an inline project entry under [projects]
let config_path = codex_home.path().join(CONFIG_TOML_FILE);
- let path_str = project_dir.path().to_string_lossy();
- // Use a literal-quoted key so Windows backslashes don't require escaping
+ let raw_path = project_dir.path().to_string_lossy();
+ let path_str = if raw_path.contains('\\') {
+ format!("'{}'", raw_path)
+ } else {
+ format!("\"{}\"", raw_path)
+ };
+ // Use a quoted key so backslashes don't require escaping on Windows
let initial = format!(
- "[projects]\n'{}' = {{ trust_level = \"untrusted\" }}\n",
- path_str
+ r#"[projects]
+{path_str} = {{ trust_level = "untrusted" }}
+"#
);
std::fs::create_dir_all(codex_home.path())?;
std::fs::write(&config_path, initial)?;
@@ -1286,28 +1285,15 @@ disable_response_storage = true
let contents = std::fs::read_to_string(&config_path)?;
- // Should not contain inline table representation anymore (accept both quote styles)
- let inline_double = format!("\"{}\" = {{ trust_level = \"trusted\" }}", path_str);
- let inline_single = format!("'{}' = {{ trust_level = \"trusted\" }}", path_str);
- assert!(
- !contents.contains(&inline_double) && !contents.contains(&inline_single),
- "config.toml should not contain inline project table anymore:\n{}",
- contents
- );
+ // Assert exact output after conversion to explicit table
+ let expected = format!(
+ r#"[projects]
- // And explicit child table header for the project
- let project_key_double = format!("[projects.\"{}\"]", path_str);
- let project_key_single = format!("[projects.'{}']", path_str);
- assert!(
- contents.contains(&project_key_double) || contents.contains(&project_key_single),
- "missing explicit project table header: expected to find `{}` or `{}` in:\n{}",
- project_key_double,
- project_key_single,
- contents
+[projects.{path_str}]
+trust_level = "trusted"
+"#
);
-
- // And the trust level value
- assert!(contents.contains("trust_level = \"trusted\""));
+ assert_eq!(contents, expected);
Ok(())
}
```
## Review Comments
### codex-rs/core/src/config.rs
- Created: 2025-08-22 16:53:23 UTC | Link: https://github.com/openai/codex/pull/2586#discussion_r2294212874
```diff
@@ -1275,7 +1258,9 @@ disable_response_storage = true
let path_str = project_dir.path().to_string_lossy();
// Use a literal-quoted key so Windows backslashes don't require escaping
let initial = format!(
- "[projects]\n'{}' = {{ trust_level = \"untrusted\" }}\n",
+ r#"[projects]
+'{}' = {{ trust_level = "untrusted" }}
+"#,
```
> inline `path_str`?
>
> ```suggestion
> r#"[projects]
> '{path_str}' = {{ trust_level = "untrusted" }}
> "#,
> ```
- Created: 2025-08-22 16:53:53 UTC | Link: https://github.com/openai/codex/pull/2586#discussion_r2294213816
```diff
@@ -1286,28 +1271,16 @@ disable_response_storage = true
let contents = std::fs::read_to_string(&config_path)?;
- // Should not contain inline table representation anymore (accept both quote styles)
- let inline_double = format!("\"{}\" = {{ trust_level = \"trusted\" }}", path_str);
- let inline_single = format!("'{}' = {{ trust_level = \"trusted\" }}", path_str);
- assert!(
- !contents.contains(&inline_double) && !contents.contains(&inline_single),
- "config.toml should not contain inline project table anymore:\n{}",
- contents
- );
+ // Assert exact output after conversion to explicit table
+ let expected = format!(
+ r#"[projects]
- // And explicit child table header for the project
- let project_key_double = format!("[projects.\"{}\"]", path_str);
- let project_key_single = format!("[projects.'{}']", path_str);
- assert!(
- contents.contains(&project_key_double) || contents.contains(&project_key_single),
- "missing explicit project table header: expected to find `{}` or `{}` in:\n{}",
- project_key_double,
- project_key_single,
- contents
+[projects."{}"]
```
> same here?
- Created: 2025-08-22 16:54:34 UTC | Link: https://github.com/openai/codex/pull/2586#discussion_r2294214942
```diff
@@ -1230,37 +1230,20 @@ disable_response_storage = true
// Call the function under test
set_project_trusted(codex_home.path(), project_dir.path())?;
- // Read back the generated config.toml
+ // Read back the generated config.toml and assert exact contents
let config_path = codex_home.path().join(CONFIG_TOML_FILE);
let contents = std::fs::read_to_string(&config_path)?;
- // Verify it does not use inline tables for the project entry
- assert!(
- !contents.contains("{ trust_level"),
- "config.toml should not use inline tables:\n{}",
- contents
- );
-
- // Verify the explicit table for the project exists. toml_edit may choose
- // either basic (double-quoted) or literal (single-quoted) strings for keys
- // containing backslashes (e.g., on Windows). Accept both forms.
let path_str = project_dir.path().to_string_lossy();
- let project_key_double = format!("[projects.\"{}\"]", path_str);
- let project_key_single = format!("[projects.'{}']", path_str);
- assert!(
- contents.contains(&project_key_double) || contents.contains(&project_key_single),
- "missing explicit project table header: expected to find `{}` or `{}` in:\n{}",
- project_key_double,
- project_key_single,
- contents
- );
+ let expected = format!(
+ r#"[projects]
```
> Strictly speaking, is `[projects]` necessary or does it just make this feature easier to implement?