Skip to content

Commit

Permalink
create a temp dir in the test context and use it as workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
glehmann committed May 2, 2024
1 parent c0ce381 commit 9461dc6
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,4 @@ panic = "abort"
[dev-dependencies]
anyhow = "1.0.82"
serde_json = "1.0.116"
temp-dir = "0.1.13"
4 changes: 1 addition & 3 deletions tests/basic.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
mod common;

use std::path::Path;

use crate::common::*;

#[tokio::test]
async fn should_initialize() -> anyhow::Result<()> {
TestContext::new().await?.initialize(Path::new("/tmp")).await?;
TestContext::new().await?.initialize().await?;
Ok(())
}
16 changes: 11 additions & 5 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#![allow(deprecated)]

use std::fmt::Debug;
use std::path::Path;
use std::pin::Pin;
use std::task::{Context, Poll};

use temp_dir::TempDir;
use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
use tokio::sync::mpsc::{self, UnboundedReceiver, UnboundedSender};
use tower_lsp::lsp_types::{Url, WorkspaceFolder};
Expand Down Expand Up @@ -66,6 +66,7 @@ pub struct TestContext {
pub response_rx: UnboundedReceiver<String>,
pub _server: tokio::task::JoinHandle<()>,
pub request_id: i64,
pub workspace: TempDir,
}

impl TestContext {
Expand All @@ -79,7 +80,13 @@ impl TestContext {
let (service, socket) = LspService::build(Backend::new).finish();
let server = tokio::spawn(Server::new(async_in, async_out, socket).serve(service));

Ok(Self { request_tx, response_rx, _server: server, request_id: 0 })
Ok(Self {
request_tx,
response_rx,
_server: server,
request_id: 0,
workspace: TempDir::new().unwrap(),
})
}

pub async fn send(&mut self, request: &jsonrpc::Request) -> anyhow::Result<()> {
Expand Down Expand Up @@ -123,7 +130,6 @@ impl TestContext {

pub async fn initialize(
&mut self,
workspace: &Path,
) -> anyhow::Result<<lsp_types::request::Initialize as Request>::Result> {
// a real set of initialize param from helix. We just have to change the workspace configuration
let initialize = r#"{
Expand Down Expand Up @@ -270,8 +276,8 @@ impl TestContext {
}"#;
let mut initialize: <lsp_types::request::Initialize as Request>::Params =
serde_json::from_str(initialize).unwrap();
let workspace_url = Url::from_file_path(workspace).unwrap();
initialize.root_path = Some(workspace.to_string_lossy().to_string());
let workspace_url = Url::from_file_path(self.workspace.path()).unwrap();
initialize.root_path = Some(self.workspace.path().to_string_lossy().to_string());
initialize.root_uri = Some(workspace_url.clone());
initialize.workspace_folders =
Some(vec![WorkspaceFolder { name: "tmp".to_owned(), uri: workspace_url.clone() }]);
Expand Down

0 comments on commit 9461dc6

Please sign in to comment.