Skip to content

Commit b3eddc3

Browse files
paths
1 parent d5c5c6e commit b3eddc3

File tree

1 file changed

+54
-135
lines changed

1 file changed

+54
-135
lines changed

crates/djls-workspace/src/paths.rs

Lines changed: 54 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -113,174 +113,93 @@ pub fn path_to_url(path: &Path) -> Option<Url> {
113113

114114
#[cfg(test)]
115115
mod tests {
116-
use super::*;
116+
use std::str::FromStr;
117117

118-
#[test]
119-
fn test_url_to_path_basic() {
120-
let url = Url::parse("file:///home/user/file.txt").unwrap();
121-
let path = url_to_path(&url).unwrap();
122-
assert_eq!(path, PathBuf::from("/home/user/file.txt"));
123-
}
118+
use super::*;
124119

125120
#[test]
126-
fn test_url_to_path_with_spaces() {
127-
let url = Url::parse("file:///home/user/my%20file.txt").unwrap();
128-
let path = url_to_path(&url).unwrap();
129-
assert_eq!(path, PathBuf::from("/home/user/my file.txt"));
121+
fn test_url_to_path_valid_file_url() {
122+
let url = Url::parse("file:///home/user/test.py").unwrap();
123+
assert_eq!(url_to_path(&url), Some(PathBuf::from("/home/user/test.py")));
130124
}
131125

132126
#[test]
133127
fn test_url_to_path_non_file_scheme() {
134-
let url = Url::parse("https://example.com/file.txt").unwrap();
135-
assert!(url_to_path(&url).is_none());
128+
let url = Url::parse("http://example.com/test.py").unwrap();
129+
assert_eq!(url_to_path(&url), None);
136130
}
137131

138-
#[cfg(windows)]
139132
#[test]
140-
fn test_url_to_path_windows() {
141-
let url = Url::parse("file:///C:/Users/user/file.txt").unwrap();
142-
let path = url_to_path(&url).unwrap();
143-
assert_eq!(path, PathBuf::from("C:/Users/user/file.txt"));
133+
fn test_url_to_path_percent_encoded() {
134+
let url = Url::parse("file:///home/user/test%20file.py").unwrap();
135+
assert_eq!(
136+
url_to_path(&url),
137+
Some(PathBuf::from("/home/user/test file.py"))
138+
);
144139
}
145140

146141
#[test]
147-
fn test_path_to_url_absolute() {
148-
let path = if cfg!(windows) {
149-
PathBuf::from("C:/Users/user/file.txt")
150-
} else {
151-
PathBuf::from("/home/user/file.txt")
152-
};
153-
154-
let url = path_to_url(&path).unwrap();
155-
assert_eq!(url.scheme(), "file");
156-
assert!(url.path().contains("file.txt"));
157-
}
158-
159-
#[test]
160-
fn test_round_trip() {
161-
let original_path = if cfg!(windows) {
162-
PathBuf::from("C:/Users/user/test file.txt")
163-
} else {
164-
PathBuf::from("/home/user/test file.txt")
165-
};
166-
167-
let url = path_to_url(&original_path).unwrap();
168-
let converted_path = url_to_path(&url).unwrap();
169-
170-
assert_eq!(original_path, converted_path);
171-
}
172-
173-
#[test]
174-
fn test_url_with_localhost() {
175-
// Some systems use file://localhost/path format
176-
let url = Url::parse("file://localhost/home/user/file.txt").unwrap();
177-
let path = url_to_path(&url);
178-
179-
// Current implementation might not handle this correctly
180-
// since it only checks scheme, not host
181-
if let Some(p) = path {
182-
assert_eq!(p, PathBuf::from("/home/user/file.txt"));
183-
}
142+
#[cfg(windows)]
143+
fn test_url_to_path_windows_drive() {
144+
let url = Url::parse("file:///C:/Users/test.py").unwrap();
145+
assert_eq!(url_to_path(&url), Some(PathBuf::from("C:/Users/test.py")));
184146
}
185147

186148
#[test]
187-
fn test_url_with_empty_host() {
188-
// Standard file:///path format (three slashes, empty host)
189-
let url = Url::parse("file:///home/user/file.txt").unwrap();
190-
let path = url_to_path(&url).unwrap();
191-
assert_eq!(path, PathBuf::from("/home/user/file.txt"));
149+
fn test_parse_lsp_uri_valid() {
150+
let uri = lsp_types::Uri::from_str("file:///test.py").unwrap();
151+
let result = parse_lsp_uri(&uri, LspContext::DidOpen);
152+
assert!(result.is_some());
153+
assert_eq!(result.unwrap().scheme(), "file");
192154
}
193155

194-
#[cfg(windows)]
156+
// lsp_uri_to_path tests
195157
#[test]
196-
fn test_unc_path_to_url() {
197-
// UNC paths like \\server\share\file.txt
198-
let unc_path = PathBuf::from(r"\\server\share\file.txt");
199-
let url = path_to_url(&unc_path);
200-
201-
// Check if UNC paths are handled
202-
if let Some(u) = url {
203-
// UNC paths should convert to file://server/share/file.txt
204-
assert!(u.to_string().contains("server"));
205-
assert!(u.to_string().contains("share"));
206-
}
158+
fn test_lsp_uri_to_path_valid_file() {
159+
let uri = lsp_types::Uri::from_str("file:///home/user/test.py").unwrap();
160+
assert_eq!(
161+
lsp_uri_to_path(&uri),
162+
Some(PathBuf::from("/home/user/test.py"))
163+
);
207164
}
208165

209166
#[test]
210-
fn test_relative_path_with_dotdot() {
211-
// Test relative paths with .. that might not exist
212-
let path = PathBuf::from("../some/nonexistent/path.txt");
213-
let url = path_to_url(&path);
214-
215-
// Should now work even for non-existent files
216-
assert!(url.is_some(), "Should handle non-existent relative paths");
217-
if let Some(u) = url {
218-
assert_eq!(u.scheme(), "file");
219-
assert!(u.path().ends_with("some/nonexistent/path.txt"));
220-
}
167+
fn test_lsp_uri_to_path_non_file() {
168+
let uri = lsp_types::Uri::from_str("http://example.com/test.py").unwrap();
169+
assert_eq!(lsp_uri_to_path(&uri), None);
221170
}
222171

223172
#[test]
224-
fn test_non_existent_absolute_path() {
225-
// Test that absolute paths work even if they don't exist
226-
let path = if cfg!(windows) {
227-
PathBuf::from("C:/NonExistent/Directory/file.txt")
228-
} else {
229-
PathBuf::from("/nonexistent/directory/file.txt")
230-
};
231-
232-
let url = path_to_url(&path);
233-
assert!(url.is_some(), "Should handle non-existent absolute paths");
234-
if let Some(u) = url {
235-
assert_eq!(u.scheme(), "file");
236-
assert!(u.path().contains("file.txt"));
237-
}
173+
fn test_lsp_uri_to_path_invalid_uri() {
174+
let uri = lsp_types::Uri::from_str("not://valid").unwrap();
175+
assert_eq!(lsp_uri_to_path(&uri), None);
238176
}
239177

178+
// path_to_url tests
240179
#[test]
241-
fn test_non_existent_relative_path() {
242-
// Test that relative paths work even if they don't exist
243-
let path = PathBuf::from("nonexistent/file.txt");
244-
let url = path_to_url(&path);
245-
246-
assert!(url.is_some(), "Should handle non-existent relative paths");
247-
if let Some(u) = url {
248-
assert_eq!(u.scheme(), "file");
249-
assert!(u.path().ends_with("nonexistent/file.txt"));
250-
// Should be an absolute URL
251-
assert!(u.path().starts_with('/') || cfg!(windows));
252-
}
180+
fn test_path_to_url_absolute() {
181+
let path = Path::new("/home/user/test.py");
182+
let url = path_to_url(path);
183+
assert!(url.is_some());
184+
assert_eq!(url.clone().unwrap().scheme(), "file");
185+
assert!(url.unwrap().path().contains("test.py"));
253186
}
254187

255188
#[test]
256-
fn test_path_with_special_chars() {
257-
// Test paths with special characters that need encoding
258-
let path = PathBuf::from("/home/user/file with spaces & special!.txt");
259-
let url = path_to_url(&path).unwrap();
260-
261-
// Should be properly percent-encoded
262-
assert!(url.as_str().contains("%20") || url.as_str().contains("with%20spaces"));
263-
264-
// Round-trip should work
265-
let back = url_to_path(&url).unwrap();
266-
assert_eq!(back, path);
189+
fn test_path_to_url_relative() {
190+
let path = Path::new("test.py");
191+
let url = path_to_url(path);
192+
assert!(url.is_some());
193+
assert_eq!(url.clone().unwrap().scheme(), "file");
194+
// Should be resolved to absolute path
195+
assert!(url.unwrap().path().ends_with("/test.py"));
267196
}
268197

269198
#[test]
270-
fn test_url_with_query_or_fragment() {
271-
// URLs with query parameters or fragments should probably be rejected
272-
let url_with_query = Url::parse("file:///path/file.txt?query=param").unwrap();
273-
let url_with_fragment = Url::parse("file:///path/file.txt#section").unwrap();
274-
275-
// These should still work, extracting just the path part
276-
let path1 = url_to_path(&url_with_query);
277-
let path2 = url_to_path(&url_with_fragment);
278-
279-
if let Some(p) = path1 {
280-
assert_eq!(p, PathBuf::from("/path/file.txt"));
281-
}
282-
if let Some(p) = path2 {
283-
assert_eq!(p, PathBuf::from("/path/file.txt"));
284-
}
199+
fn test_path_to_url_nonexistent_absolute() {
200+
let path = Path::new("/definitely/does/not/exist/test.py");
201+
let url = path_to_url(path);
202+
assert!(url.is_some());
203+
assert_eq!(url.unwrap().scheme(), "file");
285204
}
286205
}

0 commit comments

Comments
 (0)