Skip to content

Commit

Permalink
Fix None value bug in init_dir function
Browse files Browse the repository at this point in the history
- Handle None values for repo_dir and user_dir in init_dir.
- Simplify directory creation and error handling.
- Update tests to include the cases where it is not in a git repo.
- Rename 'git_ignore' to 'add_gitignore' for clarity.
basicthinker committed Aug 28, 2023
1 parent 8f4753d commit 9ccca08
Showing 4 changed files with 37 additions and 20 deletions.
22 changes: 12 additions & 10 deletions devchat/_cli/utils.py
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@
import sys
from typing import Tuple
import rich_click as click
from devchat.utils import find_root_dir, git_ignore, setup_logger, get_logger
from devchat.utils import find_root_dir, add_gitignore, setup_logger, get_logger

logger = get_logger(__name__)

@@ -36,29 +36,31 @@ def init_dir() -> Tuple[dict, str, str]:
click.echo(f"Error: Failed to find home for .chat: {repo_dir}, {user_dir}", err=True)
sys.exit(1)

if not repo_dir:
repo_dir = user_dir
elif not user_dir:
user_dir = repo_dir

try:
repo_chat_dir = os.path.join(repo_dir, ".chat")
if not os.path.exists(repo_chat_dir):
os.makedirs(repo_chat_dir)
except Exception:
click.echo(f"Error: Failed to create {repo_chat_dir}", err=True)
pass

try:
user_chat_dir = os.path.join(user_dir, ".chat")
if not os.path.exists(user_chat_dir):
os.makedirs(user_chat_dir)
except Exception:
click.echo(f"Error: Failed to create {user_chat_dir}", err=True)
pass

if not os.path.isdir(repo_chat_dir):
repo_chat_dir = user_chat_dir
if not os.path.isdir(user_chat_dir):
user_chat_dir = repo_chat_dir
if not os.path.isdir(repo_chat_dir):
click.echo(f"Error: Failed to create {repo_chat_dir}", err=True)
sys.exit(1)
if not os.path.isdir(user_chat_dir):
click.echo(f"Error: Failed to create {user_chat_dir}", err=True)
if not os.path.isdir(repo_chat_dir) or not os.path.isdir(user_chat_dir):
click.echo(f"Error: Failed to create {repo_chat_dir} and {user_chat_dir}", err=True)
sys.exit(1)

default_config_data = {
@@ -88,8 +90,8 @@ def init_dir() -> Tuple[dict, str, str]:

try:
setup_logger(os.path.join(repo_chat_dir, 'error.log'))
git_ignore(repo_chat_dir, '*')
add_gitignore(repo_chat_dir, '*')
except Exception as exc:
click.echo(f"Error: Failed to setup logger or .gitignore: {exc}", err=True)
logger.error("Failed to setup logger or add .gitignore: %s", exc)

return config_data, repo_chat_dir, user_chat_dir
2 changes: 1 addition & 1 deletion devchat/utils.py
Original file line number Diff line number Diff line change
@@ -79,7 +79,7 @@ def find_root_dir() -> Tuple[Optional[str], Optional[str]]:
return repo_dir, user_dir


def git_ignore(target_dir: str, *ignore_entries: str) -> None:
def add_gitignore(target_dir: str, *ignore_entries: str) -> None:
gitignore_path = os.path.join(target_dir, '.gitignore')

if os.path.exists(gitignore_path):
25 changes: 16 additions & 9 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -6,8 +6,8 @@
from git import Repo


@pytest.fixture(name='git_repo', scope='module')
def fixture_git_repo(request):
@pytest.fixture(scope='function')
def git_repo(request):
# Create a temporary directory
repo_dir = tempfile.mkdtemp()

@@ -27,17 +27,24 @@ def cleanup():
return repo_dir


@pytest.fixture
def mock_home_dir(tmp_path):
@pytest.fixture(scope='function')
def mock_home_dir(tmp_path, request):
home_dir = Path(tmp_path / 'home')
home_dir.mkdir()

original_home = os.environ.get('HOME')
original_cwd = os.getcwd()

os.environ['HOME'] = str(home_dir)
os.chdir(tmp_path)

def cleanup():
if original_home is not None:
os.environ['HOME'] = original_home
else:
del os.environ['HOME']

yield home_dir
os.chdir(original_cwd)

if original_home is not None:
os.environ['HOME'] = original_home
else:
del os.environ['HOME']
request.addfinalizer(cleanup)
return home_dir
8 changes: 8 additions & 0 deletions tests/test_cli_prompt.py
Original file line number Diff line number Diff line change
@@ -181,6 +181,14 @@ def test_prompt_with_function_replay(git_repo, functions_file): # pylint: disab
assert 'get_current_weather' in content


def test_prompt_without_repo(mock_home_dir): # pylint: disable=W0613
content = "What is the capital of France?"
result = runner.invoke(main, ['prompt', content])
assert result.exit_code == 0
assert check_format(result.output)
assert "Paris" in result.output


def test_prompt_response_tokens_exceed_config(mock_home_dir): # pylint: disable=W0613
config_data = {
'model': 'gpt-3.5-turbo',

0 comments on commit 9ccca08

Please sign in to comment.