Skip to content

fix: update missed CodebaseGraph rename #428

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions tests/unit/codegen/sdk/core/test_directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@


@pytest.fixture
def mock_codebase_context(tmp_path):
def mock_codebase_graph(tmp_path):
mock = MagicMock(spec=CodebaseContext)
mock.transaction_manager = MagicMock()
mock.config = CodebaseConfig()
mock.repo_path = tmp_path
mock.to_absolute = types.MethodType(CodebaseContext.to_absolute, mock)
mock.to_relative = types.MethodType(CodebaseContext.to_relative, mock)
mock.io = MagicMock()
return mock


Expand All @@ -34,17 +35,17 @@ def dir_path(tmp_path):

@pytest.fixture
def sub_dir(subdir_path, tmp_path):
return Directory(path=subdir_path.absolute(), dirpath=subdir_path.relative_to(tmp_path), parent=None)
return Directory(path=subdir_path.absolute(), dirpath=str(subdir_path.relative_to(tmp_path)), parent=None)


@pytest.fixture
def mock_file(dir_path, mock_codebase_graph):
return File(filepath=dir_path / "example.py", G=mock_codebase_graph)
return File(filepath=dir_path / "example.py", ctx=mock_codebase_graph)


@pytest.fixture
def mock_directory(tmp_path, dir_path, sub_dir, mock_file):
directory = Directory(path=dir_path.absolute(), dirpath=dir_path.relative_to(tmp_path), parent=None)
directory = Directory(path=dir_path.absolute(), dirpath=str(dir_path.relative_to(tmp_path)), parent=None)
directory.add_file(mock_file)
directory.add_subdirectory(sub_dir)
return directory
Expand All @@ -53,7 +54,7 @@ def mock_directory(tmp_path, dir_path, sub_dir, mock_file):
def test_directory_init(tmp_path, mock_directory):
"""Test initialization of Directory object."""
assert mock_directory.path == tmp_path / "mock_dir"
assert mock_directory.dirpath == Path("mock_dir")
assert mock_directory.dirpath == "mock_dir"
assert mock_directory.parent is None
assert len(mock_directory.items) == 2
assert mock_directory.items["subdir"] is not None
Expand All @@ -67,7 +68,7 @@ def test_name_property(mock_directory):

def test_add_and_file(mock_directory, mock_codebase_graph):
"""Test adding a file to the directory."""
mock_file = File(filepath=Path("mock_dir/example_2.py"), G=mock_codebase_graph)
mock_file = File(filepath=Path("mock_dir/example_2.py"), ctx=mock_codebase_graph)
mock_directory.add_file(mock_file)
rel_path = os.path.relpath(mock_file.file_path, mock_directory.dirpath)
assert rel_path in mock_directory.items
Expand All @@ -84,7 +85,7 @@ def test_remove_file(mock_directory, mock_file):

def test_remove_file_by_path(mock_directory, mock_file):
"""Test removing a file by path."""
mock_directory.remove_file_by_path(mock_file.file_path)
mock_directory.remove_file_by_path(Path(mock_file.file_path))

rel_path = os.path.relpath(mock_file.file_path, mock_directory.dirpath)
assert rel_path not in mock_directory.items
Expand All @@ -108,7 +109,7 @@ def test_get_file_not_found(mock_directory):
def test_add_subdirectory(mock_directory, dir_path):
"""Test adding a subdirectory."""
new_subdir_path = dir_path / "new_subdir"
subdir = Directory(path=new_subdir_path.absolute(), dirpath=new_subdir_path.relative_to(dir_path), parent=mock_directory)
subdir = Directory(path=new_subdir_path.absolute(), dirpath=str(new_subdir_path.relative_to(dir_path)), parent=mock_directory)
mock_directory.add_subdirectory(subdir)
rel_path = os.path.relpath(subdir.dirpath, mock_directory.dirpath)
assert rel_path in mock_directory.items
Expand Down Expand Up @@ -143,7 +144,7 @@ def test_files_property(mock_directory, sub_dir, mock_codebase_graph):
all_files = mock_directory.files
assert len(all_files) == 1

new_file = File(filepath=Path("mock_dir/example_2.py"), G=mock_codebase_graph)
new_file = File(filepath=Path("mock_dir/example_2.py"), ctx=mock_codebase_graph)
sub_dir.add_file(new_file)

all_files = mock_directory.files
Expand All @@ -162,7 +163,7 @@ def test_subdirectories_property(mock_directory, sub_dir):
assert len(all_subdirs) == 1
assert sub_dir in all_subdirs

new_sub_dir = Directory(path=sub_dir.path / "new_subdir", dirpath=sub_dir.dirpath / "new_subdir", parent=sub_dir)
new_sub_dir = Directory(path=sub_dir.path / "new_subdir", dirpath=str(Path(sub_dir.dirpath) / "new_subdir"), parent=sub_dir)
sub_dir.add_subdirectory(new_sub_dir)

all_subdirs = mock_directory.subdirectories
Expand Down
39 changes: 15 additions & 24 deletions tests/unit/codegen/shared/configs/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,30 +34,31 @@ def test_merge_configs_none_values():


def test_merge_configs_empty_string():
base = {"repository": {"organization_name": "org1"}}
override = {"repository": {"organization_name": ""}}
base = {"repository": {"full_name": "org1/test-repo"}}
override = {"repository": {"full_name": ""}}
result = _merge_configs(base, override)
assert result == {"repository": {"organization_name": "org1"}}
assert result == {"repository": {"full_name": "org1/test-repo"}}


# Test _load_from_toml
def test_load_from_toml_existing_file(temp_config_file):
config = _load_from_toml(temp_config_file)
assert isinstance(config, SessionConfig)
assert config.secrets.github_token == "gh_token123"
assert config.repository.organization_name == "test-org"
assert config.repository.repo_name == "test-repo"
assert config.feature_flags.codebase.debug is True
assert config.feature_flags.codebase.typescript.ts_dependency_manager is True
assert config.feature_flags.codebase.import_resolution_overrides == {"@org/pkg": "./local/path"}


@patch("codegen.shared.configs.models.SecretsConfig.model_config", {"env_file": "nonexistent.env"})
@patch.dict("os.environ", {})
@patch("codegen.shared.configs.models.secrets.SecretsConfig.model_config", {"env_file": "nonexistent.env"})
def test_load_from_toml_nonexistent_file():
config = _load_from_toml(Path("nonexistent.toml"))
assert isinstance(config, SessionConfig)
assert config.secrets.github_token is None
assert config.repository.organization_name is None
assert config.feature_flags.codebase.debug is None
assert config.repository.full_name is None
assert config.feature_flags.codebase.debug is False


# Test _load_from_env
Expand All @@ -71,31 +72,21 @@ def test_load_from_env():

# Test load function
@patch.dict("os.environ", {}, clear=True) # Clear all env vars for this test
@patch("codegen.shared.configs.config._load_from_env")
@patch("codegen.shared.configs.config._load_from_toml")
@patch("codegen.shared.configs.models.SecretsConfig.model_config", {"env_file": None, "env_prefix": "CODEGEN_SECRETS__"})
@patch("codegen.shared.configs.session_configs._load_from_env")
@patch("codegen.shared.configs.session_configs._load_from_toml")
@patch("codegen.shared.configs.models.secrets.SecretsConfig.model_config", {"env_file": None, "env_prefix": "CODEGEN_SECRETS__"})
def test_load_with_both_configs(mock_toml, mock_env):
# Setup mock returns
mock_env.return_value = SessionConfig(secrets=SecretsConfig(github_token="env_token"), feature_flags=FeatureFlagsConfig(codebase=CodebaseFeatureFlags(debug=True)))
mock_toml.return_value = SessionConfig(secrets={"openai_api_key": "openai_key"}, repository={"organization_name": "codegen-org"})
mock_env.return_value = SessionConfig(file_path=str(CONFIG_PATH), secrets=SecretsConfig(github_token="env_token"), feature_flags=FeatureFlagsConfig(codebase=CodebaseFeatureFlags(debug=True)))
mock_toml.return_value = SessionConfig(file_path=str(CONFIG_PATH), secrets={"openai_api_key": "openai_key"}, repository={"full_name": "codegen-org/test-repo"})

config = load_session_config(CONFIG_PATH)

assert isinstance(config, SessionConfig)
assert config.secrets.github_token == "env_token"
assert config.secrets.openai_api_key == "openai_key"
assert config.repository.organization_name == "codegen-org"
assert config.feature_flags.codebase.debug is True


@patch("codegen.shared.configs.config._load_from_env")
@patch("codegen.shared.configs.config._load_from_toml")
def test_load_with_custom_path(mock_toml, mock_env):
custom_path = Path("custom/config.toml")
load_session_config(config_path=custom_path)

mock_toml.assert_called_once_with(custom_path)
mock_env.assert_called_once()
assert config.repository.full_name == "codegen-org/test-repo"
assert config.feature_flags.codebase.debug is False


# Error cases
Expand Down
19 changes: 10 additions & 9 deletions tests/unit/codegen/shared/configs/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import pytest
import toml

from codegen.shared.configs.constants import CONFIG_PATH
from codegen.shared.configs.models.feature_flags import CodebaseFeatureFlags, FeatureFlagsConfig
from codegen.shared.configs.models.repository import RepositoryConfig
from codegen.shared.configs.models.session import SessionConfig
Expand All @@ -12,7 +13,7 @@
@pytest.fixture
def sample_config(tmpdir):
codebase_flags = CodebaseFeatureFlags(debug=True, verify_graph=False)
return SessionConfig(file_path=f"{tmpdir}/test_config.toml", repository=RepositoryConfig(full_name="test-org", repo_name="test-repo"), feature_flags=FeatureFlagsConfig(codebase=codebase_flags))
return SessionConfig(file_path=str(CONFIG_PATH), repository=RepositoryConfig(full_name="test-org/test-repo", repo_name="test-repo"), feature_flags=FeatureFlagsConfig(codebase=codebase_flags))


def test_config_initialization(tmpdir):
Expand All @@ -22,9 +23,9 @@ def test_config_initialization(tmpdir):
assert config.secrets is not None


def test_config_with_values(tmpdir):
config = SessionConfig(file_path=f"{tmpdir}/test_config.toml", repository={"full_name": "test-org", "repo_name": "test-repo"})
assert config.repository.full_name == "test-org"
def test_config_with_values():
config = SessionConfig(file_path=str(CONFIG_PATH), repository={"full_name": "test-org/test-repo", "repo_name": "test-repo"})
assert config.repository.full_name == "test-org/test-repo"
assert config.repository.repo_name == "test-repo"


Expand All @@ -39,12 +40,12 @@ def test_save_config(mock_mkdir, mock_file, sample_config):
# Verify the content being written
written_data = mock_file().write.call_args[0][0]
parsed_data = toml.loads(written_data)
assert parsed_data["repository"]["full_name"] == "test-org"
assert parsed_data["repository"]["full_name"] == "test-org/test-repo"


def test_get_config_value(sample_config):
# Test getting a simple value
assert json.loads(sample_config.get("repository.full_name")) == "test-org"
assert json.loads(sample_config.get("repository.full_name")) == "test-org/test-repo"

# Test getting a nested value
assert json.loads(sample_config.get("feature_flags.codebase.debug")) is True
Expand All @@ -57,8 +58,8 @@ def test_set_config_value(sample_config):
# Instead of mocking save, we'll mock the open function used within save
with patch("builtins.open", new_callable=mock_open) as mock_file:
# Test setting a simple string value
sample_config.set("repository.full_name", "new-org")
assert sample_config.repository.full_name == "new-org"
sample_config.set("repository.full_name", "new-org/test-repo")
assert sample_config.repository.full_name == "new-org/test-repo"

# Test setting a boolean value
sample_config.set("feature_flags.codebase.debug", "false")
Expand All @@ -83,7 +84,7 @@ def test_config_str_representation(sample_config):
assert isinstance(config_str, str)
# Verify it's valid JSON
parsed = json.loads(config_str)
assert parsed["repository"]["full_name"] == "test-org"
assert parsed["repository"]["full_name"] == "test-org/test-repo"


def test_set_config_new_override_key(sample_config):
Expand Down
Loading