From 624c8f77ac17cf4e9d2017cf31a353eb9f3e41ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jelmer=20Vernoo=C4=B3?= Date: Sat, 2 Aug 2025 15:10:36 +0100 Subject: [PATCH] Fix worktree CLI tests to properly change to repository directory The worktree CLI commands expect to be run from within a repository directory. Updated the test methods to change to the repo directory before running commands and restore the original directory afterward. diff --git a/tests/test_cli.py b/tests/test_cli.py index 5394104e98..5322433d5c 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -2338,12 +2338,18 @@ def setUp(self): def test_worktree_list(self): """Test worktree list command.""" - io.StringIO() - cmd = cli.cmd_worktree() - result = cmd.run(["list"]) + # Change to repo directory + old_cwd = os.getcwd() + os.chdir(self.repo_path) + try: + io.StringIO() + cmd = cli.cmd_worktree() + result = cmd.run(["list"]) - # Should list the main worktree - self.assertEqual(result, 0) + # Should list the main worktree + self.assertEqual(result, 0) + finally: + os.chdir(old_cwd) def test_worktree_add(self): """Test worktree add command.""" @@ -2367,81 +2373,115 @@ def test_worktree_add_detached(self): """Test worktree add with detached HEAD.""" wt_path = os.path.join(self.test_dir, "detached-wt") - cmd = cli.cmd_worktree() - with patch("sys.stdout", new_callable=io.StringIO): - result = cmd.run(["add", "--detach", wt_path]) + # Change to repo directory + old_cwd = os.getcwd() + os.chdir(self.repo_path) + try: + cmd = cli.cmd_worktree() + with patch("sys.stdout", new_callable=io.StringIO): + result = cmd.run(["add", "--detach", wt_path]) - self.assertEqual(result, 0) - self.assertTrue(os.path.exists(wt_path)) + self.assertEqual(result, 0) + self.assertTrue(os.path.exists(wt_path)) + finally: + os.chdir(old_cwd) def test_worktree_remove(self): """Test worktree remove command.""" # First add a worktree wt_path = os.path.join(self.test_dir, "to-remove") - cmd = cli.cmd_worktree() - cmd.run(["add", wt_path]) - # Then remove it - with patch("sys.stdout", new_callable=io.StringIO) as mock_stdout: - result = cmd.run(["remove", wt_path]) + # Change to repo directory + old_cwd = os.getcwd() + os.chdir(self.repo_path) + try: + cmd = cli.cmd_worktree() + cmd.run(["add", wt_path]) - self.assertEqual(result, 0) - self.assertFalse(os.path.exists(wt_path)) - self.assertIn("Worktree removed:", mock_stdout.getvalue()) + # Then remove it + with patch("sys.stdout", new_callable=io.StringIO) as mock_stdout: + result = cmd.run(["remove", wt_path]) + + self.assertEqual(result, 0) + self.assertFalse(os.path.exists(wt_path)) + self.assertIn("Worktree removed:", mock_stdout.getvalue()) + finally: + os.chdir(old_cwd) def test_worktree_prune(self): """Test worktree prune command.""" # Add a worktree and manually remove it wt_path = os.path.join(self.test_dir, "to-prune") - cmd = cli.cmd_worktree() - cmd.run(["add", wt_path]) - shutil.rmtree(wt_path) - # Prune - with patch("sys.stdout", new_callable=io.StringIO) as mock_stdout: - result = cmd.run(["prune", "-v"]) + # Change to repo directory + old_cwd = os.getcwd() + os.chdir(self.repo_path) + try: + cmd = cli.cmd_worktree() + cmd.run(["add", wt_path]) + shutil.rmtree(wt_path) + + # Prune + with patch("sys.stdout", new_callable=io.StringIO) as mock_stdout: + result = cmd.run(["prune", "-v"]) - self.assertEqual(result, 0) - output = mock_stdout.getvalue() - self.assertIn("to-prune", output) + self.assertEqual(result, 0) + output = mock_stdout.getvalue() + self.assertIn("to-prune", output) + finally: + os.chdir(old_cwd) def test_worktree_lock_unlock(self): """Test worktree lock and unlock commands.""" # Add a worktree wt_path = os.path.join(self.test_dir, "lockable") - cmd = cli.cmd_worktree() - cmd.run(["add", wt_path]) - # Lock it - with patch("sys.stdout", new_callable=io.StringIO) as mock_stdout: - result = cmd.run(["lock", wt_path, "--reason", "Testing"]) + # Change to repo directory + old_cwd = os.getcwd() + os.chdir(self.repo_path) + try: + cmd = cli.cmd_worktree() + cmd.run(["add", wt_path]) - self.assertEqual(result, 0) - self.assertIn("Worktree locked:", mock_stdout.getvalue()) + # Lock it + with patch("sys.stdout", new_callable=io.StringIO) as mock_stdout: + result = cmd.run(["lock", wt_path, "--reason", "Testing"]) - # Unlock it - with patch("sys.stdout", new_callable=io.StringIO) as mock_stdout: - result = cmd.run(["unlock", wt_path]) + self.assertEqual(result, 0) + self.assertIn("Worktree locked:", mock_stdout.getvalue()) - self.assertEqual(result, 0) - self.assertIn("Worktree unlocked:", mock_stdout.getvalue()) + # Unlock it + with patch("sys.stdout", new_callable=io.StringIO) as mock_stdout: + result = cmd.run(["unlock", wt_path]) + + self.assertEqual(result, 0) + self.assertIn("Worktree unlocked:", mock_stdout.getvalue()) + finally: + os.chdir(old_cwd) def test_worktree_move(self): """Test worktree move command.""" # Add a worktree old_path = os.path.join(self.test_dir, "old-location") new_path = os.path.join(self.test_dir, "new-location") - cmd = cli.cmd_worktree() - cmd.run(["add", old_path]) - # Move it - with patch("sys.stdout", new_callable=io.StringIO) as mock_stdout: - result = cmd.run(["move", old_path, new_path]) + # Change to repo directory + old_cwd = os.getcwd() + os.chdir(self.repo_path) + try: + cmd = cli.cmd_worktree() + cmd.run(["add", old_path]) + + # Move it + with patch("sys.stdout", new_callable=io.StringIO) as mock_stdout: + result = cmd.run(["move", old_path, new_path]) - self.assertEqual(result, 0) - self.assertFalse(os.path.exists(old_path)) - self.assertTrue(os.path.exists(new_path)) - self.assertIn("Worktree moved:", mock_stdout.getvalue()) + self.assertEqual(result, 0) + self.assertFalse(os.path.exists(old_path)) + self.assertTrue(os.path.exists(new_path)) + self.assertIn("Worktree moved:", mock_stdout.getvalue()) + finally: + os.chdir(old_cwd) def test_worktree_invalid_command(self): """Test invalid worktree subcommand.""" From 4f52beca896bb66f9a0cd0e4117474af284158e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jelmer=20Vernoo=C4=B3?= Date: Mon, 15 Sep 2025 19:18:20 +0100 Subject: [PATCH] Support TMPDIR as example system directory in symlink test Fixes #1885 --- tests/test_porcelain.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_porcelain.py b/tests/test_porcelain.py index f99b588f58..b3d10423d8 100644 --- a/tests/test_porcelain.py +++ b/tests/test_porcelain.py @@ -1684,7 +1684,7 @@ def test_add_symlink_absolute_to_system(self) -> None: symlink_target = os.environ["TEMP"] else: # On Unix-like systems, use /tmp - symlink_target = "/tmp" + symlink_target = os.environ.get("TMPDIR", "/tmp") os.symlink(symlink_target, symlink_path) # Adding a symlink to a directory outside the repo should raise ValueError