Back to Repositories

Testing NPM Script Extraction Functionality in thefuck

This test suite validates the npm script parsing functionality in the ‘thefuck’ command-line tool, specifically focusing on the extraction of available npm scripts from command output. It ensures the get_scripts() function correctly processes npm run-script output and returns valid script names.

Test Coverage Overview

The test coverage focuses on the npm script parsing functionality, verifying the get_scripts() method’s ability to extract script names from npm output.

Key areas tested include:
  • Parsing of multiline npm output containing lifecycle and custom scripts
  • Extraction of available script names from formatted npm command output
  • Filtering of lifecycle scripts to return only custom scripts

Implementation Analysis

The testing approach utilizes pytest’s fixture and mocking capabilities to simulate npm command output. The implementation employs BytesIO for stdout simulation and pytest’s mocker for dependency isolation.

Key patterns include:
  • Use of @pytest.mark.usefixtures for test setup
  • Mocking of Popen to simulate command execution
  • BytesIO implementation for stdout simulation

Technical Details

Testing tools and configuration:
  • pytest as the primary testing framework
  • pytest-mock for mocking functionality
  • BytesIO from io module for stream simulation
  • Custom fixtures for memoization control
  • Popen mocking for command execution simulation

Best Practices Demonstrated

The test implementation showcases several testing best practices in Python unit testing.

Notable practices include:
  • Isolation of external dependencies through mocking
  • Clear test case organization and naming
  • Use of fixtures for test setup management
  • Explicit assertion statements
  • Simulation of system I/O without actual command execution

nvbn/thefuck

tests/specific/test_npm.py

            
from io import BytesIO

import pytest
from thefuck.specific.npm import get_scripts

run_script_stdout = b'''
Lifecycle scripts included in code-view-web:
  test
    jest

available via `npm run-script`:
  build
    cp node_modules/ace-builds/src-min/ -a resources/ace/ && webpack --progress --colors -p --config ./webpack.production.config.js
  develop
    cp node_modules/ace-builds/src/ -a resources/ace/ && webpack-dev-server --progress --colors
  watch-test
    jest --verbose --watch

'''


@pytest.mark.usefixtures('no_memoize')
def test_get_scripts(mocker):
    patch = mocker.patch('thefuck.specific.npm.Popen')
    patch.return_value.stdout = BytesIO(run_script_stdout)
    assert get_scripts() == ['build', 'develop', 'watch-test']