mirror of
https://github.com/openai/codex.git
synced 2026-11-08 00:36:31 +00:00
Expand home-relative paths on Windows (#15817)
Follow up to: https://github.com/openai/codex/pull/9193, also support this for Windows. --------- Co-authored-by: Michael Bolin <mbolin@openai.com>
This commit is contained in:
1 parent
1ff39b6fa8
commit
6d0525ae70
1 file changed
+23
-28
@@ -23,21 +23,18 @@ pub struct AbsolutePathBuf(PathBuf);
|
|||||||
|
|
||||||
impl AbsolutePathBuf {
|
impl AbsolutePathBuf {
|
||||||
fn maybe_expand_home_directory(path: &Path) -> PathBuf {
|
fn maybe_expand_home_directory(path: &Path) -> PathBuf {
|
||||||
let Some(path_str) = path.to_str() else {
|
if let Some(path_str) = path.to_str()
|
||||||
return path.to_path_buf();
|
|
||||||
};
|
|
||||||
if cfg!(not(target_os = "windows"))
|
|
||||||
&& let Some(home) = home_dir()
|
&& let Some(home) = home_dir()
|
||||||
|
&& let Some(rest) = path_str.strip_prefix('~')
|
||||||
{
|
{
|
||||||
if path_str == "~" {
|
if rest.is_empty() {
|
||||||
return home;
|
return home;
|
||||||
}
|
} else if let Some(rest) = rest.strip_prefix('/') {
|
||||||
if let Some(rest) = path_str.strip_prefix("~/") {
|
return home.join(rest.trim_start_matches('/'));
|
||||||
let rest = rest.trim_start_matches('/');
|
} else if cfg!(windows)
|
||||||
if rest.is_empty() {
|
&& let Some(rest) = rest.strip_prefix('\\')
|
||||||
return home;
|
{
|
||||||
}
|
return home.join(rest.trim_start_matches('\\'));
|
||||||
return home.join(rest);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
path.to_path_buf()
|
path.to_path_buf()
|
||||||
@@ -257,9 +254,8 @@ mod tests {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(target_os = "windows"))]
|
|
||||||
#[test]
|
#[test]
|
||||||
fn home_directory_root_on_non_windows_is_expanded_in_deserialization() {
|
fn home_directory_root_is_expanded_in_deserialization() {
|
||||||
let Some(home) = home_dir() else {
|
let Some(home) = home_dir() else {
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
@@ -271,9 +267,8 @@ mod tests {
|
|||||||
assert_eq!(abs_path_buf.as_path(), home.as_path());
|
assert_eq!(abs_path_buf.as_path(), home.as_path());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(target_os = "windows"))]
|
|
||||||
#[test]
|
#[test]
|
||||||
fn home_directory_subpath_on_non_windows_is_expanded_in_deserialization() {
|
fn home_directory_subpath_is_expanded_in_deserialization() {
|
||||||
let Some(home) = home_dir() else {
|
let Some(home) = home_dir() else {
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
@@ -285,9 +280,8 @@ mod tests {
|
|||||||
assert_eq!(abs_path_buf.as_path(), home.join("code").as_path());
|
assert_eq!(abs_path_buf.as_path(), home.join("code").as_path());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(target_os = "windows"))]
|
|
||||||
#[test]
|
#[test]
|
||||||
fn home_directory_double_slash_on_non_windows_is_expanded_in_deserialization() {
|
fn home_directory_double_slash_is_expanded_in_deserialization() {
|
||||||
let Some(home) = home_dir() else {
|
let Some(home) = home_dir() else {
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
@@ -301,16 +295,17 @@ mod tests {
|
|||||||
|
|
||||||
#[cfg(target_os = "windows")]
|
#[cfg(target_os = "windows")]
|
||||||
#[test]
|
#[test]
|
||||||
fn home_directory_on_windows_is_not_expanded_in_deserialization() {
|
fn home_directory_backslash_subpath_is_expanded_in_deserialization() {
|
||||||
let temp_dir = tempdir().expect("base dir");
|
let Some(home) = home_dir() else {
|
||||||
let base_dir = temp_dir.path();
|
return;
|
||||||
let abs_path_buf = {
|
|
||||||
let _guard = AbsolutePathBufGuard::new(base_dir);
|
|
||||||
serde_json::from_str::<AbsolutePathBuf>("\"~/code\"").expect("failed to deserialize")
|
|
||||||
};
|
};
|
||||||
assert_eq!(
|
let temp_dir = tempdir().expect("base dir");
|
||||||
abs_path_buf.as_path(),
|
let abs_path_buf = {
|
||||||
base_dir.join("~").join("code").as_path()
|
let _guard = AbsolutePathBufGuard::new(temp_dir.path());
|
||||||
);
|
let input =
|
||||||
|
serde_json::to_string(r#"~\code"#).expect("string should serialize as JSON");
|
||||||
|
serde_json::from_str::<AbsolutePathBuf>(&input).expect("is valid abs path")
|
||||||
|
};
|
||||||
|
assert_eq!(abs_path_buf.as_path(), home.join("code").as_path());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user