Selaa lähdekoodia

Fix workspace paths defaults (#1845)

* workspace_mount_path is set to the workspace_base if unset

* unit tests for paths

* workspace_base is absolute path
Engel Nyst 1 vuosi sitten
vanhempi
sitoutus
b3a45ed7fe
2 muutettua tiedostoa jossa 60 lisäystä ja 16 poistoa
  1. 7 2
      opendevin/core/config.py
  2. 53 14
      tests/unit/test_config.py

+ 7 - 2
opendevin/core/config.py

@@ -73,8 +73,8 @@ class AppConfig(metaclass=Singleton):
     runtime: str = 'server'
     file_store: str = 'memory'
     file_store_path: str = '/tmp/file_store'
-    workspace_base: str = os.getcwd()
-    workspace_mount_path: str = os.getcwd()
+    workspace_base: str = os.path.join(os.getcwd(), 'workspace')
+    workspace_mount_path: str | None = None
     workspace_mount_path_in_sandbox: str = '/workspace'
     workspace_mount_rewrite: str | None = None
     cache_dir: str = '/tmp/cache'
@@ -264,6 +264,11 @@ def finalize_config(config: AppConfig):
     More tweaks to the config after it's been loaded.
     """
 
+    # Set workspace_mount_path if not set by the user
+    if config.workspace_mount_path is None:
+        config.workspace_mount_path = os.path.abspath(config.workspace_base)
+    config.workspace_base = os.path.abspath(config.workspace_base)
+
     # In local there is no sandbox, the workspace will have the same pwd as the host
     if config.sandbox_type == 'local':
         config.workspace_mount_path_in_sandbox = config.workspace_mount_path

+ 53 - 14
tests/unit/test_config.py

@@ -28,6 +28,20 @@ def setup_env():
     os.remove('new_style_config.toml')
 
 
+@pytest.fixture
+def temp_toml_file(tmp_path):
+    # Fixture to create a temporary directory and TOML file for testing
+    tmp_toml_file = os.path.join(tmp_path, 'config.toml')
+    yield tmp_toml_file
+
+
+@pytest.fixture
+def default_config(monkeypatch):
+    # Fixture to provide a default AppConfig instance
+    AppConfig.reset()
+    yield AppConfig()
+
+
 def test_compat_env_to_config(monkeypatch, setup_env):
     # Use `monkeypatch` to set environment variables for this specific test
     monkeypatch.setenv('WORKSPACE_BASE', '/repos/opendevin/workspace')
@@ -49,20 +63,6 @@ def test_compat_env_to_config(monkeypatch, setup_env):
     assert config.agent.memory_max_threads == 4
 
 
-@pytest.fixture
-def temp_toml_file(tmp_path):
-    # Fixture to create a temporary directory and TOML file for testing
-    tmp_toml_file = os.path.join(tmp_path, 'config.toml')
-    yield tmp_toml_file
-
-
-@pytest.fixture
-def default_config(monkeypatch):
-    # Fixture to provide a default AppConfig instance
-    AppConfig.reset()
-    yield AppConfig()
-
-
 def test_load_from_old_style_env(monkeypatch, default_config):
     # Test loading configuration from old-style environment variables using monkeypatch
     monkeypatch.setenv('LLM_API_KEY', 'test-api-key')
@@ -171,3 +171,42 @@ def test_finalize_config(default_config):
         default_config.workspace_mount_path_in_sandbox
         == default_config.workspace_mount_path
     )
+
+
+# tests for workspace, mount path, path in sandbox, cache dir
+def test_workspace_mount_path_default(default_config):
+    assert default_config.workspace_mount_path is None
+    finalize_config(default_config)
+    assert default_config.workspace_mount_path == os.path.abspath(
+        default_config.workspace_base
+    )
+
+
+def test_workspace_mount_path_in_sandbox_local(default_config):
+    assert default_config.workspace_mount_path_in_sandbox == '/workspace'
+    default_config.sandbox_type = 'local'
+    finalize_config(default_config)
+    assert (
+        default_config.workspace_mount_path_in_sandbox
+        == default_config.workspace_mount_path
+    )
+
+
+def test_workspace_mount_rewrite(default_config, monkeypatch):
+    default_config.workspace_base = '/home/user/project'
+    default_config.workspace_mount_rewrite = '/home/user:/sandbox'
+    monkeypatch.setattr('os.getcwd', lambda: '/current/working/directory')
+    finalize_config(default_config)
+    assert default_config.workspace_mount_path == '/sandbox/project'
+
+
+def test_embedding_base_url_default(default_config):
+    default_config.llm.base_url = 'https://api.exampleapi.com'
+    finalize_config(default_config)
+    assert default_config.llm.embedding_base_url == 'https://api.exampleapi.com'
+
+
+def test_cache_dir_creation(default_config, tmpdir):
+    default_config.cache_dir = str(tmpdir.join('test_cache'))
+    finalize_config(default_config)
+    assert os.path.exists(default_config.cache_dir)