Skip to content

Commit

Permalink
test: Fix simple_completions test
Browse files Browse the repository at this point in the history
  • Loading branch information
k3yss committed Dec 23, 2024
1 parent 96c015c commit b5565ac
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 32 deletions.
34 changes: 34 additions & 0 deletions devenv/src/devenv.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::{cli, cnix, config, lsp, tasks, utils};
use crate::log::{DevenvFormat, DevenvLayer};
use clap::crate_version;
use cli_table::Table;
use cli_table::{print_stderr, WithTitle};
Expand All @@ -11,13 +12,16 @@ use serde::Deserialize;
use sha2::Digest;
use std::collections::HashMap;
use std::io::Write;
use std::io::{self, BufWriter};
use std::os::unix::{fs::PermissionsExt, process::CommandExt};
use std::{
fs,
path::{Path, PathBuf},
};
use tower_lsp::{LspService, Server};
use tracing::{debug, error, info, info_span, warn, Instrument};
use tracing_subscriber::filter::LevelFilter;
use tracing_subscriber::{prelude::*, EnvFilter};

// templates
const FLAKE_TMPL: &str = include_str!("flake.tmpl.nix");
Expand Down Expand Up @@ -398,6 +402,36 @@ impl Devenv {

pub async fn lsp(&mut self) -> Result<()> {
self.assemble(false)?;
// Setup file logging
let file = std::fs::File::create("/tmp/devenv-lsp.log")
.expect("Couldn't create devenv-lsp.log file");
let file = BufWriter::new(file);
let (non_blocking, _guard) = tracing_appender::non_blocking(file);

// Create a dedicated subscriber for LSP
let subscriber = tracing_subscriber::registry()
.with(
EnvFilter::builder()
.with_default_directive(LevelFilter::DEBUG.into())
.from_env_lossy(),
)
.with(
tracing_subscriber::fmt::layer()
.with_writer(io::stderr)
.with_ansi(false)
.event_format(DevenvFormat::default()),
)
.with(
tracing_subscriber::fmt::layer()
.with_writer(non_blocking)
.with_ansi(false),
)
.with(DevenvLayer::new());

// Set as global default for LSP session
let _guard = tracing::subscriber::set_global_default(subscriber)
.expect("Failed to set tracing subscriber");

let options = self.nix.build(&["optionsJSON"]).await?;
debug!("{:?}", options);
let options_path = options[0]
Expand Down
1 change: 0 additions & 1 deletion devenv/src/lsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ impl Backend {
}

fn get_completion_items(&self, uri: &str, position: Position) -> Vec<CompletionItem> {
println!("Test document uri {:?}", uri);
let (content, tree) = self
.document_map
.get(uri)
Expand Down
19 changes: 5 additions & 14 deletions devenv/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ use devenv::{
config, log, Devenv,
};
use miette::Result;
use std::io::BufWriter;
use tracing::level_filters::LevelFilter;
use tracing::{info, warn};

#[tokio::main]
Expand Down Expand Up @@ -35,19 +33,12 @@ async fn main() -> Result<()> {
log::Level::default()
};

log::init_tracing(level, cli.global_options.log_format);
// log::init_tracing(level, cli.global_options.log_format);

let file =
std::fs::File::create("/tmp/devenv-lsp.log").expect("Couldn't create devenv-lsp.log file");

let file = BufWriter::new(file);
let (non_blocking, _guard) = tracing_appender::non_blocking(file);
let subscriber = tracing_subscriber::fmt()
.with_max_level(LevelFilter::DEBUG)
.with_ansi(false)
.with_writer(non_blocking)
.finish();
let _ = tracing::subscriber::set_global_default(subscriber);
// Only initialize tracing if we're not running LSP
if !matches!(command, Commands::Lsp { .. }) {
log::init_tracing(level, cli.global_options.log_format);
}

let mut config = config::Config::load()?;
for input in cli.global_options.override_input.chunks_exact(2) {
Expand Down
32 changes: 19 additions & 13 deletions devenv/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use core::panic;
use devenv::lsp::Backend;
use devenv::utils;
use fs_extra::dir::CopyOptions;
use std::fmt::Debug;
use std::fs;
Expand Down Expand Up @@ -40,15 +41,20 @@ impl TestContext {
let (resp_server, response_rx) = duplex(1024);
let response_rx = BufReader::new(response_rx);
// create a demo completion json file
let completion_json = serde_json::json!({ "languages": {
"python": { "description": "Python language" },
"nodejs": { "description": "Node.js runtime" }
},
"services": {
"nginx": { "description": "Web server" },
"redis": { "description": "Cache server" }
}
});

let options_path = concat!(env!("CARGO_MANIFEST_DIR"), "/tests/common/options.json");

let options_contents = fs::read(options_path).expect("Failed to read options.json");
let options_json: serde_json::Value =
serde_json::from_slice(&options_contents).expect("Failed to parse options.json");
let mut flatten_json = utils::flatten(options_json);
let filter_keys = vec![
String::from("declarations"),
String::from("loc"),
String::from("readOnly"),
];
let filter_keys_refs: Vec<&str> = filter_keys.iter().map(|s| s.as_str()).collect();
let completion_json = utils::filter_json(&mut flatten_json, filter_keys_refs);

let (service, socket) =
LspService::build(|client| Backend::new(client, completion_json.clone())).finish();
Expand All @@ -57,7 +63,7 @@ impl TestContext {
// create a temporary workspace an init it with our test inputs
let workspace = TempDir::new().unwrap();
for item in fs::read_dir(Path::new("tests").join("workspace").join(base)).unwrap() {
eprintln!("copying {item:?}");
// eprintln!("copying {item:?}");
fs_extra::copy_items(
&[item.unwrap().path()],
workspace.path(),
Expand Down Expand Up @@ -98,7 +104,7 @@ impl TestContext {
let mut content = vec![0; length];
self.response_rx.read_exact(&mut content).await.unwrap();
let content = String::from_utf8(content).unwrap();
eprintln!("received: {content}");
// eprintln!("received: {content}");
std::io::stderr().flush().unwrap();
// skip log messages
if content.contains("window/logMessage") {
Expand Down Expand Up @@ -129,7 +135,7 @@ impl TestContext {
let mut content = vec![0; length];
self.response_rx.read_exact(&mut content).await.unwrap();
let content = String::from_utf8(content).unwrap();
eprintln!("received: {content}");
// eprintln!("received: {content}");
std::io::stderr().flush().unwrap();
// skip log messages
if content.contains("window/logMessage") {
Expand All @@ -143,7 +149,7 @@ impl TestContext {

pub async fn send(&mut self, request: &jsonrpc::Request) {
let content = serde_json::to_string(request).unwrap();
eprintln!("\nsending: {content}");
// eprintln!("\nsending: {content}");
std::io::stderr().flush().unwrap();
self.request_tx
.write_all(encode_message(None, &content).as_bytes())
Expand Down
1 change: 1 addition & 0 deletions devenv/tests/common/options.json

Large diffs are not rendered by default.

7 changes: 3 additions & 4 deletions devenv/tests/completions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,12 @@ async fn test_simple_completions() {
if let Some(tower_lsp::lsp_types::CompletionResponse::List(list)) = completion_response {
assert!(!list.items.is_empty(), "Should have completion items");
let item_labels: Vec<String> = list.items.into_iter().map(|item| item.label).collect();

println!("labels are {:?}", item_labels);
assert!(
item_labels.contains(&"python".to_string()),
"Should suggest python"
);
assert!(
item_labels.contains(&"nodejs".to_string()),
"Should suggest nodejs"
);
} else {
panic!("Expected CompletionResponse::List");
}
Expand Down Expand Up @@ -103,6 +101,7 @@ async fn test_simple_nested_completions() {
if let Some(tower_lsp::lsp_types::CompletionResponse::List(list)) = completion_response {
assert!(!list.items.is_empty(), "Should have completion items");
let item_labels: Vec<String> = list.items.into_iter().map(|item| item.label).collect();
// println!("Completion list is {:?}", item_labels);
assert!(
item_labels.contains(&"python".to_string()),
"Should suggest python"
Expand Down

0 comments on commit b5565ac

Please sign in to comment.