Skip to content

Add prefix to helper functions to prevent user code conflicts#539

Merged
Chemaclass merged 3 commits intomainfrom
feat/538-add-ns-all-bashunit-funcs
Dec 6, 2025
Merged

Add prefix to helper functions to prevent user code conflicts#539
Chemaclass merged 3 commits intomainfrom
feat/538-add-ns-all-bashunit-funcs

Conversation

@Chemaclass
Copy link
Copy Markdown
Member

@Chemaclass Chemaclass commented Dec 6, 2025

📚 Description

This PR addresses the namespace pollution issue (#538) that makes bashunit unusable for projects that define common function names like log, fail, mock, etc.

The Problem:
Bashunit previously defined generic function names at the global scope. When users have their own functions with the same names (e.g., a log() function in a logging module), conflicts occur silently, causing tests to fail or behave unexpectedly. Since bash has no concept of private methods or namespaces, any function defined by bashunit can collide with user code.

Example conflict scenario:

# User code defines:
function log() {
    echo "[INFO] message" >> /var/log/app.log
}

# bashunit previously also defined:
function log() {
    [[ "$BASHUNIT_DEV_MODE" == true ]] || return 0
    console::print "$@" >&2
}

# When running tests, whichever loads last "wins" - causing silent failures

The Solution:
All helper functions now use the bashunit:: namespace prefix, following the same convention already used internally (e.g., console::print, state::add_assertions_passed). This ensures bashunit functions never conflict with user-defined functions.

🔖 Changes

Function Renames (Breaking Changes)

All helper functions accessible to test files now require the bashunit:: prefix:

Before After
skip bashunit::skip
todo bashunit::todo
fail bashunit::fail
mock bashunit::mock
unmock bashunit::unmock
spy bashunit::spy
temp_file bashunit::temp_file
temp_dir bashunit::temp_dir
data_set bashunit::data_set
current_dir bashunit::current_dir
current_filename bashunit::current_filename
random_str bashunit::random_str
log bashunit::log
set_test_title bashunit::set_test_title

What Remains Unchanged

  • All assert_* functions keep their names (they already have a unique prefix)
  • Lifecycle hooks (set_up, tear_down, set_up_before_script, tear_down_after_script) keep their names (these are user-defined functions, not bashunit-provided)

Files Updated

  • Source files in src/ - renamed function definitions and internal calls
  • Documentation in docs/ - updated all examples to use new names
  • All test files updated to use the new function names
  • CHANGELOG.md updated with migration guide

Migration Guide

Users upgrading to this version need to update their test files:

# Before
function test_example() {
    mock curl echo "mocked response"
    local result
    result="value"
    assert_equals "expected" "$result"
}

function test_skip_example() {
    skip "Not implemented yet"
}

# After
function test_example() {
    bashunit::mock curl echo "mocked response"
    local result
    result="value"
    assert_equals "expected" "$result"
}

function test_skip_example() {
    bashunit::skip "Not implemented yet"
}

Quick migration command:

# Run from your tests directory
sed -i "" \
    -e "s/\\bskip /bashunit::skip /g" \
    -e "s/\\btodo /bashunit::todo /g" \
    -e "s/\\bfail /bashunit::fail /g" \
    -e "s/\\bmock /bashunit::mock /g" \
    -e "s/\\bunmock /bashunit::unmock /g" \
    -e "s/\\bspy /bashunit::spy /g" \
    -e "s/\\btemp_file/bashunit::temp_file/g" \
    -e "s/\\btemp_dir/bashunit::temp_dir/g" \
    -e "s/\\bdata_set/bashunit::data_set/g" \
    -e "s/\\bcurrent_dir/bashunit::current_dir/g" \
    -e "s/\\bcurrent_filename/bashunit::current_filename/g" \
    *_test.sh

✅ To-do list

  • I updated the CHANGELOG.md to reflect the new feature or fix
  • I updated the documentation to reflect the changes

Fixes #538

  BREAKING CHANGE: All helper functions now require bashunit:: prefix.

  Renamed functions:
  - skip → bashunit::skip
  - todo → bashunit::todo
  - fail → bashunit::fail
  - mock → bashunit::mock
  - unmock → bashunit::unmock
  - spy → bashunit::spy
  - temp_file → bashunit::temp_file
  - temp_dir → bashunit::temp_dir
  - data_set → bashunit::data_set
  - current_dir → bashunit::current_dir
  - current_filename → bashunit::current_filename
  - random_str → bashunit::random_str
  - log → bashunit::log
  - set_test_title → bashunit::set_test_title

  All assert_* functions remain unchanged.
@Chemaclass Chemaclass added the enhancement New feature or request label Dec 6, 2025
@Chemaclass Chemaclass self-assigned this Dec 6, 2025
@Chemaclass Chemaclass added the enhancement New feature or request label Dec 6, 2025
@Chemaclass Chemaclass changed the title Add ns all bashunit funcs feat\!: namespace helper functions to prevent user code conflicts Dec 6, 2025
@Chemaclass Chemaclass changed the title feat\!: namespace helper functions to prevent user code conflicts Add prefix to helper functions to prevent user code conflicts Dec 6, 2025
@Chemaclass Chemaclass merged commit 82e8972 into main Dec 6, 2025
15 checks passed
@Chemaclass Chemaclass deleted the feat/538-add-ns-all-bashunit-funcs branch December 6, 2025 15:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve namespace pollution from internal bashunit functions and global variables

3 participants