Skip to content

Commit 40e9c0f

Browse files
committed
Fix tests on Windows
`lsp_types::Url::from_file_path()` returns `None` if not a valid path. Path should start with e.g. `C:\` on Windows.
1 parent d05ea05 commit 40e9c0f

File tree

3 files changed

+23
-22
lines changed

3 files changed

+23
-22
lines changed

crates/ark/src/lsp/definitions.rs

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,13 @@ pub fn goto_definition<'a>(
7878

7979
#[cfg(test)]
8080
mod tests {
81-
use std::path::PathBuf;
82-
8381
use assert_matches::assert_matches;
8482
use tower_lsp::lsp_types;
8583

8684
use super::*;
8785
use crate::lsp::documents::Document;
8886
use crate::lsp::indexer;
87+
use crate::lsp::util::test_path;
8988

9089
#[test]
9190
fn test_goto_definition() {
@@ -96,15 +95,13 @@ foo <- 42
9695
print(foo)
9796
"#;
9897
let doc = Document::new(code, None);
99-
let path = PathBuf::from("/foo/test.R");
98+
let (path, uri) = test_path();
10099

101100
indexer::update(&doc, &path).unwrap();
102101

103102
let params = GotoDefinitionParams {
104103
text_document_position_params: lsp_types::TextDocumentPositionParams {
105-
text_document: lsp_types::TextDocumentIdentifier {
106-
uri: Url::from_file_path(&path).unwrap(),
107-
},
104+
text_document: lsp_types::TextDocumentIdentifier { uri },
108105
position: lsp_types::Position::new(2, 7),
109106
},
110107
work_done_progress_params: Default::default(),
@@ -114,9 +111,6 @@ print(foo)
114111
assert_matches!(
115112
goto_definition(&doc, params).unwrap(),
116113
Some(GotoDefinitionResponse::Link(ref links)) => {
117-
assert!(!links.is_empty());
118-
assert_eq!(links[0].target_uri, Url::from_file_path(&path).unwrap());
119-
120114
assert_eq!(
121115
links[0].target_range,
122116
lsp_types::Range {
@@ -138,14 +132,13 @@ foo <- 1
138132
print(foo)
139133
"#;
140134
let doc = Document::new(code, None);
141-
let path = PathBuf::from("/foo/section_test.R");
135+
let (path, uri) = test_path();
136+
142137
indexer::update(&doc, &path).unwrap();
143138

144139
let params = lsp_types::GotoDefinitionParams {
145140
text_document_position_params: lsp_types::TextDocumentPositionParams {
146-
text_document: lsp_types::TextDocumentIdentifier {
147-
uri: lsp_types::Url::from_file_path(&path).unwrap(),
148-
},
141+
text_document: lsp_types::TextDocumentIdentifier { uri },
149142
position: lsp_types::Position::new(3, 7),
150143
},
151144
work_done_progress_params: Default::default(),
@@ -155,14 +148,6 @@ print(foo)
155148
assert_matches!(
156149
goto_definition(&doc, params).unwrap(),
157150
Some(lsp_types::GotoDefinitionResponse::Link(ref links)) => {
158-
assert!(!links.is_empty());
159-
160-
let link = &links[0];
161-
assert_eq!(
162-
link.target_uri,
163-
lsp_types::Url::from_file_path(&path).unwrap()
164-
);
165-
166151
// The section should is not the target, the variable has priority
167152
assert_eq!(
168153
links[0].target_range,

crates/ark/src/lsp/symbols.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,7 @@ mod tests {
587587
use crate::lsp::config::WorkspaceSymbolsConfig;
588588
use crate::lsp::documents::Document;
589589
use crate::lsp::indexer::ResetIndexerGuard;
590+
use crate::lsp::util::test_path;
590591

591592
fn test_symbol(code: &str) -> Vec<DocumentSymbol> {
592593
let doc = Document::new(code, None);
@@ -914,7 +915,8 @@ a <- function() {
914915

915916
// Index the document
916917
let doc = Document::new(code, None);
917-
indexer::update(&doc, std::path::Path::new("/test.R")).unwrap();
918+
let (path, _) = test_path();
919+
indexer::update(&doc, &path).unwrap();
918920

919921
// Query for all symbols
920922
let params = WorkspaceSymbolParams {

crates/ark/src/lsp/util.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,17 @@ pub unsafe extern "C-unwind" fn ps_object_id(object: SEXP) -> anyhow::Result<SEX
2828
let value = format!("{:p}", object);
2929
return Ok(Rf_mkString(value.as_ptr() as *const c_char));
3030
}
31+
32+
#[cfg(test)]
33+
pub(crate) fn test_path() -> (std::path::PathBuf, url::Url) {
34+
use std::path::PathBuf;
35+
36+
let path = if cfg!(windows) {
37+
PathBuf::from(r"C:\test.R")
38+
} else {
39+
PathBuf::from("/test.R")
40+
};
41+
let uri = url::Url::from_file_path(&path).unwrap();
42+
43+
(path, uri)
44+
}

0 commit comments

Comments
 (0)