Back to Repositories

Testing FileSelector Enhancement Features in gpt-engineer

This test suite validates the enhanced functionality of the FileSelector component in gpt-engineer, focusing on file selection handling and sorting capabilities. The tests ensure proper behavior of file selection skipping and alphabetical sorting of files.

Test Coverage Overview

The test suite covers two main aspects of the FileSelector component:
  • Skip file selection functionality verification
  • File sorting implementation testing
  • Path handling and directory structure validation
  • Configuration file (.gpteng/file_selection.toml) integration

Implementation Analysis

The testing approach utilizes pytest’s temporary directory fixture (tmp_path) for isolated testing environments. Tests implement mock directory structures and file configurations to validate FileSelector behavior. The implementation leverages Path objects for cross-platform compatibility and uses global state tracking for editor interaction verification.

Technical Details

Testing tools and configuration:
  • pytest framework for test execution
  • tmp_path fixture for temporary test directories
  • Mock file system setup with .gpteng configuration
  • Path manipulation using pathlib
  • TOML configuration file handling

Best Practices Demonstrated

The test suite exemplifies several testing best practices:
  • Isolated test environments using temporary directories
  • Clear test case separation and naming
  • Proper assertion messages for debugging
  • Comprehensive setup and teardown handling
  • Mock implementation for external dependencies

gpt-engineer-org/gpt-engineer

tests/core/test_file_selector_enhancements.py

            
import os

from pathlib import Path
from typing import List, Union

from gpt_engineer.applications.cli.file_selector import FileSelector

editorcalled = False


def set_editor_called(
    self, input_path: Union[str, Path], init: bool = True
) -> List[str]:
    global editorcalled
    editorcalled = True
    return []


def set_file_selector_tmpproject(tmp_path):
    project_path = tmp_path / "project/"
    os.mkdir(project_path)
    os.mkdir(project_path / "x")
    os.mkdir(project_path / "a")

    gpteng_path = project_path / ".gpteng"
    os.mkdir(gpteng_path)

    with open(gpteng_path / "file_selection.toml", "w") as file:
        file.write("[files]
")
        file.write(' "x/xxtest.py" = "selected"
')
        file.write(' "a/aatest.py" = "selected"
')

    with open(project_path / "x/xxtest.py", "w") as file:
        file.write('print("Hello")')

    with open(project_path / "a/aatest.py", "w") as file:
        file.write('print("Hello")')

    return project_path


def test_file_selector_enhancement_skip_file_selector(tmp_path):
    project_path = set_file_selector_tmpproject(tmp_path)
    fileSelector = FileSelector(project_path=project_path)
    fileSelector.editor_file_selector = set_editor_called
    fileSelector.ask_for_files(skip_file_selection=True)

    assert editorcalled is False, "FileSelector.skip_file_selector is not working"


def test_file_selector_enhancement_sort(tmp_path):
    project_path = set_file_selector_tmpproject(tmp_path)
    fileSelector = FileSelector(project_path=project_path)

    sortedFiles = fileSelector.get_current_files(project_path)
    assert sortedFiles == [
        "a/aatest.py",
        "x/xxtest.py",
    ], "FileSelector.get_current_files is unsorted!"