diff --git a/e2e_tests/test_worktree_status.py b/e2e_tests/test_worktree_status.py index d55fa66..68b86e7 100644 --- a/e2e_tests/test_worktree_status.py +++ b/e2e_tests/test_worktree_status.py @@ -1,10 +1,18 @@ #!/usr/bin/env python3 +import re + from helpers import * +import pytest -def test_worktree_status(): + +@pytest.mark.parametrize("has_config", [True, False]) +def test_worktree_status(has_config): with TempGitRepositoryWorktree() as (base_dir, _commit): + if has_config: + with open(os.path.join(base_dir, "grm.toml"), "w") as f: + f.write("") cmd = grm(["wt", "add", "test"], cwd=base_dir) assert cmd.returncode == 0 @@ -40,3 +48,30 @@ def test_worktree_status_non_git(): assert cmd.returncode != 0 assert len(cmd.stdout) == 0 assert len(cmd.stderr) != 0 + + +def test_worktree_status_warn_with_non_worktree_dir(): + with TempGitRepositoryWorktree() as (base_dir, _commit): + cmd = grm(["wt", "add", "test"], cwd=base_dir) + assert cmd.returncode == 0 + + shell( + f""" + cd {base_dir} + mkdir not_a_worktree + """ + ) + + cmd = grm(["wt", "status"], cwd=base_dir) + + assert cmd.returncode == 0 + assert len(cmd.stdout) != 0 + assert len(cmd.stderr) != 0 + assert ( + re.match( + ".*error.*not_a_worktree.*not a valid worktree directory", + cmd.stderr, + re.IGNORECASE, + ) + is not None + ) diff --git a/src/table.rs b/src/table.rs index 3ea818d..b4a9814 100644 --- a/src/table.rs +++ b/src/table.rs @@ -1,3 +1,5 @@ +use crate::Repo; + use comfy_table::{Cell, Table}; use std::path::Path; @@ -130,25 +132,11 @@ pub fn get_worktree_status_table( )); } } - for entry in std::fs::read_dir(&directory).map_err(|error| error.to_string())? { - let dirname = crate::path_as_string( - entry - .map_err(|error| error.to_string())? - .path() - .strip_prefix(&directory) - // this unwrap is safe, as we can be sure that each subentry of - // &directory also has the prefix &dir - .unwrap(), - ); - if dirname == crate::GIT_MAIN_WORKTREE_DIRECTORY { - continue; - } - if !&worktrees.iter().any(|worktree| worktree.name() == dirname) { - errors.push(format!( - "Found {}, which is not a valid worktree directory!", - &dirname - )); - } + for worktree in Repo::find_unmanaged_worktrees(&repo, &directory).unwrap() { + errors.push(format!( + "Found {}, which is not a valid worktree directory!", + &worktree + )); } Ok((table, errors)) }