Skip to content

Commit 82a48ae

Browse files
committed
Fix tests on windows
1 parent 9a42466 commit 82a48ae

12 files changed

+272
-132
lines changed

Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/red_knot/tests/file_watching.rs

+95-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ impl TestCase {
6969
Some(all_events)
7070
}
7171

72-
#[cfg(unix)]
7372
fn take_watch_changes(&self) -> Vec<watch::ChangeEvent> {
7473
self.try_take_watch_changes(Duration::from_secs(10))
7574
.expect("Expected watch changes but observed none")
@@ -1349,3 +1348,98 @@ fn nested_packages_delete_root() -> anyhow::Result<()> {
13491348

13501349
Ok(())
13511350
}
1351+
1352+
#[test]
1353+
fn added_package() -> anyhow::Result<()> {
1354+
let mut case = setup([
1355+
(
1356+
"pyproject.toml",
1357+
r#"
1358+
[project]
1359+
name = "inner"
1360+
1361+
[tool.knot.workspace]
1362+
members = ["packages/*"]
1363+
"#,
1364+
),
1365+
(
1366+
"packages/a/pyproject.toml",
1367+
r#"
1368+
[project]
1369+
name = "a"
1370+
"#,
1371+
),
1372+
])?;
1373+
1374+
assert_eq!(case.db().workspace().packages(case.db()).len(), 2);
1375+
1376+
std::fs::create_dir(case.workspace_path("packages/b").as_std_path())
1377+
.context("failed to create folder for package 'b'")?;
1378+
1379+
// It seems that the file watcher won't pick up on file changes shortly after the folder
1380+
// was created... I suspect this is because most file watchers don't support recursive
1381+
// file watching. Instead, file-watching libraries manually implement recursive file watching
1382+
// by setting a watcher for each directory. But doing this obviously "lags" behind.
1383+
case.take_watch_changes();
1384+
1385+
std::fs::write(
1386+
case.workspace_path("packages/b/pyproject.toml")
1387+
.as_std_path(),
1388+
r#"
1389+
[project]
1390+
name = "b"
1391+
"#,
1392+
)
1393+
.context("failed to write pyproject.toml for package b")?;
1394+
1395+
let changes = case.stop_watch();
1396+
1397+
case.apply_changes(changes);
1398+
1399+
assert_eq!(case.db().workspace().packages(case.db()).len(), 3);
1400+
1401+
Ok(())
1402+
}
1403+
1404+
#[test]
1405+
fn removed_package() -> anyhow::Result<()> {
1406+
let mut case = setup([
1407+
(
1408+
"pyproject.toml",
1409+
r#"
1410+
[project]
1411+
name = "inner"
1412+
1413+
[tool.knot.workspace]
1414+
members = ["packages/*"]
1415+
"#,
1416+
),
1417+
(
1418+
"packages/a/pyproject.toml",
1419+
r#"
1420+
[project]
1421+
name = "a"
1422+
"#,
1423+
),
1424+
(
1425+
"packages/b/pyproject.toml",
1426+
r#"
1427+
[project]
1428+
name = "b"
1429+
"#,
1430+
),
1431+
])?;
1432+
1433+
assert_eq!(case.db().workspace().packages(case.db()).len(), 3);
1434+
1435+
std::fs::remove_dir_all(case.workspace_path("packages/b").as_std_path())
1436+
.context("failed to remove package 'b'")?;
1437+
1438+
let changes = case.stop_watch();
1439+
1440+
case.apply_changes(changes);
1441+
1442+
assert_eq!(case.db().workspace().packages(case.db()).len(), 2);
1443+
1444+
Ok(())
1445+
}

crates/red_knot_workspace/Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ tracing = { workspace = true }
3535

3636
[dev-dependencies]
3737
ruff_db = { workspace = true, features = ["testing"] }
38-
insta = { workspace = true }
38+
insta = { workspace = true, features = ["filters"] }
39+
regex = { workspace = true }
3940
tempfile = { workspace = true }
4041

4142
[features]

crates/red_knot_workspace/src/db/changes.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::db::{Db, RootDatabase};
22
use crate::watch;
3-
use crate::watch::{CreatedKind, DeletedKind};
3+
use crate::watch::{ChangeEvent, CreatedKind, DeletedKind};
44
use crate::workspace::settings::Configuration;
55
use crate::workspace::{Workspace, WorkspaceMetadata};
66
use red_knot_python_semantic::Program;
@@ -57,7 +57,9 @@ impl RootDatabase {
5757
// Changes to ignore files or settings can change the workspace structure or add/remove files
5858
// from packages.
5959
if let Some(package) = workspace.package(self, path) {
60-
if package.root(self) == workspace.root(self) {
60+
if package.root(self) == workspace.root(self)
61+
|| matches!(change, ChangeEvent::Deleted { .. })
62+
{
6163
workspace_change = true;
6264
}
6365

crates/red_knot_workspace/src/workspace.rs

+6
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,12 @@ impl PackageTree {
566566
}
567567
}
568568

569+
// The package table should never be empty, that's why `is_empty` makes little sense
570+
#[allow(clippy::len_without_is_empty)]
571+
pub fn len(&self) -> usize {
572+
self.0.len()
573+
}
574+
569575
pub fn iter(&self) -> PackageTreeIter {
570576
PackageTreeIter(self.0.values())
571577
}

0 commit comments

Comments
 (0)