Skip to content

Latest commit

 

History

History
134 lines (99 loc) · 3.95 KB

File metadata and controls

134 lines (99 loc) · 3.95 KB

Shell Testing Documentation

This document describes the comprehensive test suite for the Python shell implementation.

Test Structure

The test suite is organized into several files:

  • tests/test_shell.py - Unit tests for individual shell components
  • tests/test_integration.py - Integration tests for complete shell functionality
  • run_tests.py - Simple test runner script

Test Categories

Unit Tests (test_shell.py)

  1. Command Parsing Tests - Test command line parsing logic

    • Simple commands (no arguments)
    • Commands with arguments
    • Commands with quoted arguments
    • Commands with redirection
    • Edge cases and error conditions
  2. Built-in Command Tests - Test each built-in command individually

    • echo - Text output
    • pwd - Current directory display
    • type - Command type identification
    • cd - Directory navigation
    • exit - Shell termination
  3. External Command Tests - Test external program execution

    • Successful command execution
    • Failed command execution
    • Non-existent commands
  4. PATH Management Tests - Test PATH environment handling

    • Building executable command dictionary
    • Handling empty PATH
    • Handling non-existent PATH directories
  5. Edge Case Tests - Test unusual inputs and error conditions

    • Empty commands
    • Whitespace-only commands
    • Complex quoting
    • Multiple redirect symbols

Integration Tests (test_integration.py)

  1. Redirection Tests - Test output redirection functionality

    • Basic redirection with >
    • Redirection with 1>
    • Commands without redirection
  2. Full Integration Tests - Test complete shell workflows

    • End-to-end command processing
    • Environment setup and teardown
    • Real directory navigation
    • External command execution
  3. Command Chaining Tests - Test multiple command sequences

    • Multiple echo commands
    • Directory navigation sequences
    • Mixed command types

Running Tests

Using pipenv (recommended)

# Run all tests
pipenv run python -m pytest

# Run with verbose output
pipenv run python -m pytest -v

# Run specific test file
pipenv run python -m pytest tests/test_shell.py

# Run specific test class
pipenv run python -m pytest tests/test_shell.py::TestEchoCommand

# Run specific test
pipenv run python -m pytest tests/test_shell.py::TestEchoCommand::test_echo_single_word

Using the test runner script

# Simple test execution
python run_tests.py

# Help information
python run_tests.py --help

Test Coverage

The test suite covers:

  • ✅ All built-in commands (echo, pwd, type, cd, exit)
  • ✅ External command execution
  • ✅ Command parsing with various arguments and quoting
  • ✅ Output redirection parsing (preparation for implementation)
  • ✅ PATH environment variable handling
  • ✅ Error handling and edge cases
  • ✅ Directory navigation and environment setup
  • ✅ Integration scenarios with multiple commands

Test Environment

Tests use isolated environments:

  • Temporary directories for file system operations
  • Mocked PATH variables to avoid system interference
  • Mocked HOME directories for consistent testing
  • Subprocess mocking for external command testing

Adding New Tests

To add new tests:

  1. For unit tests: Add to tests/test_shell.py in the appropriate test class
  2. For integration tests: Add to tests/test_integration.py
  3. Follow naming conventions: Test methods should start with test_
  4. Use descriptive names: Test names should clearly describe what they test
  5. Include docstrings: Explain what each test validates
  6. Clean up: Use setup/teardown methods to maintain test isolation

Test Philosophy

  • Comprehensive: Cover all functionality and edge cases
  • Isolated: Each test should be independent
  • Fast: Tests should run quickly for rapid feedback
  • Readable: Test code should be clear and well-documented
  • Maintainable: Tests should be easy to update as code evolves