Merge branch 'main' into assists

This commit is contained in:
Max Brunsfeld 2022-02-08 12:41:57 -08:00
commit e0fe8b5a7c
10 changed files with 126 additions and 66 deletions

1
Cargo.lock generated
View file

@ -5850,6 +5850,7 @@ dependencies = [
"tide-compress", "tide-compress",
"time 0.2.25", "time 0.2.25",
"toml", "toml",
"util",
"zed", "zed",
] ]

View file

@ -1,6 +1,7 @@
#[cfg(test)] #[cfg(test)]
#[ctor::ctor] #[ctor::ctor]
fn init_logger() { fn init_logger() {
// std::env::set_var("RUST_LOG", "info"); if std::env::var("RUST_LOG").is_ok() {
env_logger::init(); env_logger::init();
}
} }

View file

@ -18,9 +18,9 @@ use crate::{
#[cfg(test)] #[cfg(test)]
#[ctor::ctor] #[ctor::ctor]
fn init_logger() { fn init_logger() {
env_logger::builder() if std::env::var("RUST_LOG").is_ok() {
.filter_level(log::LevelFilter::Info) env_logger::init();
.init(); }
} }
pub fn run_test( pub fn run_test(

View file

@ -17,8 +17,9 @@ use util::test::Network;
#[cfg(test)] #[cfg(test)]
#[ctor::ctor] #[ctor::ctor]
fn init_logger() { fn init_logger() {
// std::env::set_var("RUST_LOG", "info"); if std::env::var("RUST_LOG").is_ok() {
env_logger::init(); env_logger::init();
}
} }
#[test] #[test]

View file

@ -562,7 +562,7 @@ impl FakeLanguageServer {
request.params, request.params,
); );
} else { } else {
println!( log::info!(
"skipping message in fake language server {:?}", "skipping message in fake language server {:?}",
std::str::from_utf8(&self.buffer) std::str::from_utf8(&self.buffer)
); );

View file

@ -61,6 +61,7 @@ gpui = { path = "../gpui" }
zed = { path = "../zed", features = ["test-support"] } zed = { path = "../zed", features = ["test-support"] }
ctor = "0.1" ctor = "0.1"
env_logger = "0.8" env_logger = "0.8"
util = { path = "../util" }
lazy_static = "1.4" lazy_static = "1.4"
serde_json = { version = "1.0.64", features = ["preserve_order"] } serde_json = { version = "1.0.64", features = ["preserve_order"] }

View file

@ -526,68 +526,120 @@ pub struct ChannelMessage {
#[cfg(test)] #[cfg(test)]
pub mod tests { pub mod tests {
use super::*; use super::*;
use lazy_static::lazy_static;
use parking_lot::Mutex;
use rand::prelude::*; use rand::prelude::*;
use sqlx::{ use sqlx::{
migrate::{MigrateDatabase, Migrator}, migrate::{MigrateDatabase, Migrator},
Postgres, Postgres,
}; };
use std::path::Path; use std::{
mem,
path::Path,
sync::atomic::{AtomicUsize, Ordering::SeqCst},
};
use util::ResultExt as _;
pub struct TestDb { pub struct TestDb {
pub db: Db, pub db: Option<Db>,
pub name: String, pub name: String,
pub url: String, pub url: String,
} }
lazy_static! {
static ref DB_POOL: Mutex<Vec<TestDb>> = Default::default();
static ref DB_COUNT: AtomicUsize = Default::default();
}
impl TestDb { impl TestDb {
pub fn new() -> Self { pub fn new() -> Self {
// Enable tests to run in parallel by serializing the creation of each test database. DB_COUNT.fetch_add(1, SeqCst);
lazy_static::lazy_static! { let mut pool = DB_POOL.lock();
static ref DB_CREATION: std::sync::Mutex<()> = std::sync::Mutex::new(()); if let Some(db) = pool.pop() {
} db.truncate();
db
let mut rng = StdRng::from_entropy(); } else {
let name = format!("zed-test-{}", rng.gen::<u128>()); let mut rng = StdRng::from_entropy();
let url = format!("postgres://postgres@localhost/{}", name); let name = format!("zed-test-{}", rng.gen::<u128>());
let migrations_path = Path::new(concat!(env!("CARGO_MANIFEST_DIR"), "/migrations")); let url = format!("postgres://postgres@localhost/{}", name);
let db = block_on(async { let migrations_path = Path::new(concat!(env!("CARGO_MANIFEST_DIR"), "/migrations"));
{ let db = block_on(async {
let _lock = DB_CREATION.lock();
Postgres::create_database(&url) Postgres::create_database(&url)
.await .await
.expect("failed to create test db"); .expect("failed to create test db");
} let mut db = Db::new(&url, 5).await.unwrap();
let mut db = Db::new(&url, 5).await.unwrap(); db.test_mode = true;
db.test_mode = true; let migrator = Migrator::new(migrations_path).await.unwrap();
let migrator = Migrator::new(migrations_path).await.unwrap(); migrator.run(&db.pool).await.unwrap();
migrator.run(&db.pool).await.unwrap(); db
db });
});
Self { db, name, url } Self {
db: Some(db),
name,
url,
}
}
} }
pub fn db(&self) -> &Db { pub fn db(&self) -> &Db {
&self.db self.db.as_ref().unwrap()
}
fn truncate(&self) {
block_on(async {
let query = "
SELECT tablename FROM pg_tables
WHERE schemaname = 'public';
";
let table_names = sqlx::query_scalar::<_, String>(query)
.fetch_all(&self.db().pool)
.await
.unwrap();
sqlx::query(&format!(
"TRUNCATE TABLE {} RESTART IDENTITY",
table_names.join(", ")
))
.execute(&self.db().pool)
.await
.unwrap();
})
}
async fn teardown(mut self) -> Result<()> {
let db = self.db.take().unwrap();
let query = "
SELECT pg_terminate_backend(pg_stat_activity.pid)
FROM pg_stat_activity
WHERE pg_stat_activity.datname = '{}' AND pid <> pg_backend_pid();
";
sqlx::query(query)
.bind(&self.name)
.execute(&db.pool)
.await?;
db.pool.close().await;
Postgres::drop_database(&self.url).await?;
Ok(())
} }
} }
impl Drop for TestDb { impl Drop for TestDb {
fn drop(&mut self) { fn drop(&mut self) {
block_on(async { if let Some(db) = self.db.take() {
let query = " DB_POOL.lock().push(TestDb {
SELECT pg_terminate_backend(pg_stat_activity.pid) db: Some(db),
FROM pg_stat_activity name: mem::take(&mut self.name),
WHERE pg_stat_activity.datname = '{}' AND pid <> pg_backend_pid(); url: mem::take(&mut self.url),
"; });
sqlx::query(query) if DB_COUNT.fetch_sub(1, SeqCst) == 1 {
.bind(&self.name) block_on(async move {
.execute(&self.db.pool) let mut pool = DB_POOL.lock();
.await for db in pool.drain(..) {
.unwrap(); db.teardown().await.log_err();
self.db.pool.close().await; }
Postgres::drop_database(&self.url).await.unwrap(); });
}); }
}
} }
} }

View file

@ -1244,11 +1244,12 @@ mod tests {
#[cfg(test)] #[cfg(test)]
#[ctor::ctor] #[ctor::ctor]
fn init_logger() { fn init_logger() {
// std::env::set_var("RUST_LOG", "info"); if std::env::var("RUST_LOG").is_ok() {
env_logger::init(); env_logger::init();
}
} }
#[gpui::test] #[gpui::test(iterations = 10)]
async fn test_share_project(mut cx_a: TestAppContext, mut cx_b: TestAppContext) { async fn test_share_project(mut cx_a: TestAppContext, mut cx_b: TestAppContext) {
let (window_b, _) = cx_b.add_window(|_| EmptyView); let (window_b, _) = cx_b.add_window(|_| EmptyView);
let lang_registry = Arc::new(LanguageRegistry::new()); let lang_registry = Arc::new(LanguageRegistry::new());
@ -1387,7 +1388,7 @@ mod tests {
.await; .await;
} }
#[gpui::test] #[gpui::test(iterations = 10)]
async fn test_unshare_project(mut cx_a: TestAppContext, mut cx_b: TestAppContext) { async fn test_unshare_project(mut cx_a: TestAppContext, mut cx_b: TestAppContext) {
let lang_registry = Arc::new(LanguageRegistry::new()); let lang_registry = Arc::new(LanguageRegistry::new());
let fs = Arc::new(FakeFs::new(cx_a.background())); let fs = Arc::new(FakeFs::new(cx_a.background()));
@ -1484,7 +1485,7 @@ mod tests {
.unwrap(); .unwrap();
} }
#[gpui::test] #[gpui::test(iterations = 10)]
async fn test_propagate_saves_and_fs_changes( async fn test_propagate_saves_and_fs_changes(
mut cx_a: TestAppContext, mut cx_a: TestAppContext,
mut cx_b: TestAppContext, mut cx_b: TestAppContext,
@ -1674,7 +1675,7 @@ mod tests {
.await; .await;
} }
#[gpui::test] #[gpui::test(iterations = 10)]
async fn test_buffer_conflict_after_save(mut cx_a: TestAppContext, mut cx_b: TestAppContext) { async fn test_buffer_conflict_after_save(mut cx_a: TestAppContext, mut cx_b: TestAppContext) {
cx_a.foreground().forbid_parking(); cx_a.foreground().forbid_parking();
let lang_registry = Arc::new(LanguageRegistry::new()); let lang_registry = Arc::new(LanguageRegistry::new());
@ -1767,7 +1768,7 @@ mod tests {
}); });
} }
#[gpui::test] #[gpui::test(iterations = 10)]
async fn test_buffer_reloading(mut cx_a: TestAppContext, mut cx_b: TestAppContext) { async fn test_buffer_reloading(mut cx_a: TestAppContext, mut cx_b: TestAppContext) {
cx_a.foreground().forbid_parking(); cx_a.foreground().forbid_parking();
let lang_registry = Arc::new(LanguageRegistry::new()); let lang_registry = Arc::new(LanguageRegistry::new());
@ -1929,7 +1930,7 @@ mod tests {
buffer_b.condition(&cx_b, |buf, _| buf.text() == text).await; buffer_b.condition(&cx_b, |buf, _| buf.text() == text).await;
} }
#[gpui::test] #[gpui::test(iterations = 10)]
async fn test_leaving_worktree_while_opening_buffer( async fn test_leaving_worktree_while_opening_buffer(
mut cx_a: TestAppContext, mut cx_a: TestAppContext,
mut cx_b: TestAppContext, mut cx_b: TestAppContext,
@ -2007,7 +2008,7 @@ mod tests {
.await; .await;
} }
#[gpui::test] #[gpui::test(iterations = 10)]
async fn test_peer_disconnection(mut cx_a: TestAppContext, mut cx_b: TestAppContext) { async fn test_peer_disconnection(mut cx_a: TestAppContext, mut cx_b: TestAppContext) {
cx_a.foreground().forbid_parking(); cx_a.foreground().forbid_parking();
let lang_registry = Arc::new(LanguageRegistry::new()); let lang_registry = Arc::new(LanguageRegistry::new());
@ -2078,7 +2079,7 @@ mod tests {
.await; .await;
} }
#[gpui::test] #[gpui::test(iterations = 10)]
async fn test_collaborating_with_diagnostics( async fn test_collaborating_with_diagnostics(
mut cx_a: TestAppContext, mut cx_a: TestAppContext,
mut cx_b: TestAppContext, mut cx_b: TestAppContext,
@ -2302,7 +2303,7 @@ mod tests {
}); });
} }
#[gpui::test] #[gpui::test(iterations = 10)]
async fn test_collaborating_with_completion( async fn test_collaborating_with_completion(
mut cx_a: TestAppContext, mut cx_a: TestAppContext,
mut cx_b: TestAppContext, mut cx_b: TestAppContext,
@ -2527,7 +2528,7 @@ mod tests {
); );
} }
#[gpui::test] #[gpui::test(iterations = 10)]
async fn test_formatting_buffer(mut cx_a: TestAppContext, mut cx_b: TestAppContext) { async fn test_formatting_buffer(mut cx_a: TestAppContext, mut cx_b: TestAppContext) {
cx_a.foreground().forbid_parking(); cx_a.foreground().forbid_parking();
let mut lang_registry = Arc::new(LanguageRegistry::new()); let mut lang_registry = Arc::new(LanguageRegistry::new());
@ -2631,7 +2632,7 @@ mod tests {
); );
} }
#[gpui::test] #[gpui::test(iterations = 10)]
async fn test_definition(mut cx_a: TestAppContext, mut cx_b: TestAppContext) { async fn test_definition(mut cx_a: TestAppContext, mut cx_b: TestAppContext) {
cx_a.foreground().forbid_parking(); cx_a.foreground().forbid_parking();
let mut lang_registry = Arc::new(LanguageRegistry::new()); let mut lang_registry = Arc::new(LanguageRegistry::new());
@ -2788,7 +2789,7 @@ mod tests {
.await; .await;
} }
#[gpui::test] #[gpui::test(iterations = 10)]
async fn test_open_buffer_while_getting_definition_pointing_to_it( async fn test_open_buffer_while_getting_definition_pointing_to_it(
mut cx_a: TestAppContext, mut cx_a: TestAppContext,
mut cx_b: TestAppContext, mut cx_b: TestAppContext,
@ -2902,7 +2903,7 @@ mod tests {
assert_eq!(definitions[0].target_buffer, buffer_b2); assert_eq!(definitions[0].target_buffer, buffer_b2);
} }
#[gpui::test] #[gpui::test(iterations = 10)]
async fn test_basic_chat(mut cx_a: TestAppContext, mut cx_b: TestAppContext) { async fn test_basic_chat(mut cx_a: TestAppContext, mut cx_b: TestAppContext) {
cx_a.foreground().forbid_parking(); cx_a.foreground().forbid_parking();
@ -3042,7 +3043,7 @@ mod tests {
.await; .await;
} }
#[gpui::test] #[gpui::test(iterations = 10)]
async fn test_chat_message_validation(mut cx_a: TestAppContext) { async fn test_chat_message_validation(mut cx_a: TestAppContext) {
cx_a.foreground().forbid_parking(); cx_a.foreground().forbid_parking();
@ -3102,7 +3103,7 @@ mod tests {
); );
} }
#[gpui::test] #[gpui::test(iterations = 10)]
async fn test_chat_reconnection(mut cx_a: TestAppContext, mut cx_b: TestAppContext) { async fn test_chat_reconnection(mut cx_a: TestAppContext, mut cx_b: TestAppContext) {
cx_a.foreground().forbid_parking(); cx_a.foreground().forbid_parking();
@ -3314,7 +3315,7 @@ mod tests {
.await; .await;
} }
#[gpui::test] #[gpui::test(iterations = 10)]
async fn test_contacts( async fn test_contacts(
mut cx_a: TestAppContext, mut cx_a: TestAppContext,
mut cx_b: TestAppContext, mut cx_b: TestAppContext,

View file

@ -12,8 +12,9 @@ use util::test::Network;
#[cfg(test)] #[cfg(test)]
#[ctor::ctor] #[ctor::ctor]
fn init_logger() { fn init_logger() {
// std::env::set_var("RUST_LOG", "info"); if std::env::var("RUST_LOG").is_ok() {
env_logger::init(); env_logger::init();
}
} }
#[test] #[test]

View file

@ -12,7 +12,9 @@ use workspace::Settings;
#[cfg(test)] #[cfg(test)]
#[ctor::ctor] #[ctor::ctor]
fn init_logger() { fn init_logger() {
env_logger::init(); if std::env::var("RUST_LOG").is_ok() {
env_logger::init();
}
} }
pub fn test_app_state(cx: &mut MutableAppContext) -> Arc<AppState> { pub fn test_app_state(cx: &mut MutableAppContext) -> Arc<AppState> {