Back to Repositories

Testing Heroku Multiple Apps Command Handling in thefuck

This test suite validates the functionality of handling multiple Heroku apps in git remotes for the thefuck command-line tool. It ensures proper error handling and command suggestions when working with multiple Heroku applications in a single repository.

Test Coverage Overview

The test suite covers the core functionality of Heroku multiple apps detection and command suggestion generation.

Key areas tested include:
  • Matching logic for multiple apps scenarios
  • Command suggestion generation for different Heroku commands
  • Handling of non-matching cases
  • Validation of app-specific command formatting

Implementation Analysis

The testing approach utilizes pytest’s parametrize feature to efficiently test multiple input scenarios with minimal code duplication.

Testing patterns include:
  • Separate test cases for matching and non-matching scenarios
  • Parametrized test inputs for different command variations
  • Mock command output validation
  • Structured command object handling

Technical Details

Testing infrastructure includes:
  • pytest as the primary testing framework
  • Custom Command type for input handling
  • Mock output strings for different scenarios
  • Parametrized test decorators for multiple test cases
  • Integration with thefuck’s rule system

Best Practices Demonstrated

The test suite exemplifies several testing best practices in Python.

Notable practices include:
  • Clear separation of test cases
  • Comprehensive mock data representation
  • Efficient test parameterization
  • Explicit assertion statements
  • Well-structured test organization

nvbn/thefuck

tests/rules/test_heroku_multiple_apps.py

            
# -*- coding: utf-8 -*-

import pytest
from thefuck.types import Command
from thefuck.rules.heroku_multiple_apps import match, get_new_command


suggest_output = '''
 ▸    Multiple apps in git remotes
 ▸    Usage: --remote heroku-dev
 ▸    or: --app myapp-dev
 ▸    Your local git repository has more than 1 app referenced in git remotes.
 ▸    Because of this, we can't determine which app you want to run this command against.
 ▸    Specify the app you want with --app or --remote.
 ▸    Heroku remotes in repo:
 ▸    myapp (heroku)
 ▸    myapp-dev (heroku-dev)
 ▸
 ▸    https://devcenter.heroku.com/articles/multiple-environments
'''

not_match_output = '''
=== HEROKU_POSTGRESQL_TEAL_URL, DATABASE_URL
Plan:                  Hobby-basic
Status:                Available
Connections:           20/20
PG Version:            9.6.4
Created:               2017-01-01 00:00 UTC
Data Size:             99.9 MB
Tables:                99
Rows:                  12345/10000000 (In compliance)
Fork/Follow:           Unsupported
Rollback:              Unsupported
Continuous Protection: Off
Add-on:                postgresql-round-12345
'''


@pytest.mark.parametrize('cmd', ['pg'])
def test_match(cmd):
    assert match(
        Command('heroku {}'.format(cmd), suggest_output))


@pytest.mark.parametrize('script, output', [
    ('heroku pg', not_match_output)])
def test_not_match(script, output):
    assert not match(Command(script, output))


@pytest.mark.parametrize('cmd, result', [
    ('pg', ['heroku pg --app myapp', 'heroku pg --app myapp-dev'])])
def test_get_new_command(cmd, result):
    command = Command('heroku {}'.format(cmd), suggest_output)
    assert get_new_command(command) == result