From cbe72be10a646c0b03931bb39aff9285feae47f9 Mon Sep 17 00:00:00 2001 From: phernandez Date: Mon, 24 Mar 2025 18:56:51 -0500 Subject: [PATCH] fix: make set_default_project also activate project for current session to fix #37 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This change makes the 'basic-memory project default ' command both: 1. Set the default project for future invocations (persistent change) 2. Activate the project for the current session (immediate change) Added tests to verify this behavior, which resolves issue #37 where the project name and path weren't changing properly when the default project was changed. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- src/basic_memory/cli/commands/project.py | 14 +++++- tests/cli/test_project_commands.py | 54 +++++++++++++++++++++++- 2 files changed, 64 insertions(+), 4 deletions(-) diff --git a/src/basic_memory/cli/commands/project.py b/src/basic_memory/cli/commands/project.py index 66f11f22..f90522ce 100644 --- a/src/basic_memory/cli/commands/project.py +++ b/src/basic_memory/cli/commands/project.py @@ -92,12 +92,22 @@ def remove_project( def set_default_project( name: str = typer.Argument(..., help="Name of the project to set as default"), ) -> None: - """Set the default project.""" + """Set the default project and activate it for the current session.""" config_manager = ConfigManager() try: + # Set the default project config_manager.set_default_project(name) - console.print(f"[green]Project '{name}' set as default[/green]") + + # Also activate it for the current session by setting the environment variable + os.environ["BASIC_MEMORY_PROJECT"] = name + + # Reload configuration to apply the change + from importlib import reload + from basic_memory import config as config_module + reload(config_module) + + console.print(f"[green]Project '{name}' set as default and activated[/green]") except ValueError as e: # pragma: no cover console.print(f"[red]Error: {e}[/red]") raise typer.Exit(1) diff --git a/tests/cli/test_project_commands.py b/tests/cli/test_project_commands.py index 2dcd8471..9fc86c8a 100644 --- a/tests/cli/test_project_commands.py +++ b/tests/cli/test_project_commands.py @@ -15,6 +15,13 @@ from basic_memory.config import ConfigManager, DATA_DIR_NAME, CONFIG_FILE_NAME @pytest.fixture def temp_home(monkeypatch): """Create a temporary directory for testing.""" + # Save the original environment variable if it exists + original_env = os.environ.get("BASIC_MEMORY_PROJECT") + + # Clear environment variable for clean test + if "BASIC_MEMORY_PROJECT" in os.environ: + del os.environ["BASIC_MEMORY_PROJECT"] + with TemporaryDirectory() as tempdir: temp_home = Path(tempdir) monkeypatch.setattr(Path, "home", lambda: temp_home) @@ -24,6 +31,12 @@ def temp_home(monkeypatch): config_dir.mkdir(parents=True, exist_ok=True) yield temp_home + + # Cleanup: restore original environment variable if it existed + if original_env is not None: + os.environ["BASIC_MEMORY_PROJECT"] = original_env + elif "BASIC_MEMORY_PROJECT" in os.environ: + del os.environ["BASIC_MEMORY_PROJECT"] @pytest.fixture @@ -123,17 +136,33 @@ def test_project_default(cli_runner, temp_home): # Set as default result = cli_runner.invoke(app, ["project", "default", "test"]) assert result.exit_code == 0 - assert "Project 'test' set as default" in result.stdout + assert "Project 'test' set as default and activated" in result.stdout # Verify default was set config_manager = ConfigManager() assert config_manager.default_project == "test" + + # Extra verification: check if the environment variable was set + assert os.environ.get("BASIC_MEMORY_PROJECT") == "test" def test_project_current(cli_runner, temp_home): """Test showing the current project.""" + # Create a bare-bones config.json with main as the default project + config_file = temp_home / DATA_DIR_NAME / CONFIG_FILE_NAME + config_data = { + "projects": { + "main": str(temp_home / "basic-memory"), + }, + "default_project": "main", + } + config_file.write_text(json.dumps(config_data)) + + # Create the main project directory + main_dir = temp_home / "basic-memory" + main_dir.mkdir(parents=True, exist_ok=True) - # Set as default + # Now check the current project result = cli_runner.invoke(app, ["project", "current"]) assert result.exit_code == 0 assert "Current project: main" in result.stdout @@ -156,3 +185,24 @@ def test_project_option(cli_runner, temp_home, monkeypatch): # Verify environment variable was set assert env_vars.get("BASIC_MEMORY_PROJECT") == "test" + + +def test_project_default_activates_project(cli_runner, temp_home, monkeypatch): + """Test that setting the default project also activates it in the current session.""" + # Create a test environment + env = {} + monkeypatch.setattr(os, "environ", env) + + # Create two test projects + config_manager = ConfigManager() + config_manager.add_project("project1", str(temp_home / "project1")) + + # Set project1 as default using the CLI command + result = cli_runner.invoke(app, ["project", "default", "project1"]) + assert result.exit_code == 0 + assert "Project 'project1' set as default and activated" in result.stdout + + # Verify the environment variable was set + # This is the core of our fix - the set_default_project command now also sets + # the BASIC_MEMORY_PROJECT environment variable to activate the project + assert env.get("BASIC_MEMORY_PROJECT") == "project1"