Skip to content

refactor(src): group cohesive flat files into modules (assert, console, util) #940

Description

@Chemaclass

Summary

#931 splits large files into modules. This is the other half: src/ still holds 36 flat
files
, many of them small and clearly belonging to the same concept. Ten of them are
assert_*.sh. Group by context so a directory names a concept, following ADR-010's
index.sh convention.

Measured on main (c8b0b53).

Group 1 — src/assert/ (highest confidence, do first)

Ten files, 2313 lines, already aggregated together by src/assertions.sh. The concept is
established; only the directory is missing.

NowBecomes
src/assertions.shsrc/assert/index.sh
src/assert.sh (970)src/assert/core.sh
src/assert_arrays.sh (101)src/assert/arrays.sh
src/assert_assertions.sh (192)src/assert/assertions.sh
src/assert_dates.sh (185)src/assert/dates.sh
src/assert_duration.sh (69)src/assert/duration.sh
src/assert_files.sh (156)src/assert/files.sh
src/assert_folders.sh (118)src/assert/folders.sh
src/assert_json.sh (67)src/assert/json.sh
src/assert_once.sh (156)src/assert/once.sh
src/assert_snapshot.sh (299)src/assert/snapshot.sh

Drop the assert_ prefix inside the directory — it carries the concept, as
learn/lessons/basics.sh does. Function names do not change; only file names.

Two callers hardcode these paths and must move with them:

  • tests/unit/completions_test.sh:30 greps src/assert*.sh to derive the public assertion
    list for the completions parity contract → becomes src/assert/*.sh.
  • tests/unit/build_test.sh special-cases src/assertions.sh as "the one flat-file
    aggregator" outside the src/*/index.sh glob. That special case disappears — a welcome
    simplification, and the reason ADR-010's original "beside" argument was withdrawn.

src/assertions.sh also sources skip_todo.sh and test_doubles.sh, which are not
assertions (skip/todo markers; spies and mocks). Leave both flat in this group, or handle
them in group 4 — do not sweep them into src/assert/ just because the aggregator lists them.

Group 2 — src/console/ (high confidence)

Output rendering, 1220 lines:

  • src/colors.sh (57) → src/console/colors.sh
  • src/console_header.sh (335) → src/console/header.sh
  • src/console_results.sh (828) → src/console/results.sh

console_results.sh is also on #931's list at 828 lines. Prefer grouping first, splitting
second
: move it into src/console/ here, then split it inside the module under #931 if
still warranted.

Group 3 — src/cli/ (high confidence)

The subcommand implementations, ~430 lines. main.sh is the only caller of all four,
and each is literally a bashunit <subcommand> implementation:

  • src/upgrade.sh (54) → src/cli/upgrade.sh
  • src/watch.sh (112) → src/cli/watch.sh
  • src/doc.sh (198) → src/cli/doc.sh
  • src/init.sh (66) → src/cli/init.sh

benchmark.sh looks like it belongs but src/runner/bench.sh also calls it, so it is a
shared implementation rather than purely a subcommand — it stays flat. main.sh is the
dispatcher and needs its own split under #931 first; grouping it now would only relocate a
1427-line file.

Zero hardcoded path references to any of the four.

Won't do: src/util/ and src/system/

Considered and declined, recorded here so it is not re-litigated.

The candidates were str (156), math (104), io (31), check_os (107),
dependencies (42), clock (203) — 2 to 10 functions each. These are the most obviously
named files in the repo; nobody has struggled to find math.sh. Wrapping them costs an
index.sh, a source line and a directory hop, and buys navigation for files that were
never hard to navigate. A two-file util/ is not a module, it is ceremony.

ADR-010's rationale is a directory for something with internal structure. A 31-line file
has none.

The effort is better spent on helpers.sh (909 lines, 30 functions, six unrelated concerns:
test-function naming, discovery, data providers, tags, base64 encoding, misc). That is a
cohesion problem, tracked on #931.

Explicitly stays flat

Not everything benefits from a directory. These are single-concept files or cross-cutting
state, and grouping them would invent a concept that does not exist:

bashunit.sh (public custom-assert facade) · globals.sh (public test API) · env.sh ·
state.sh · helpers.sh · clock.sh · parallel.sh · rerun.sh · test_title.sh

main.sh (1427) and the subcommand files (doc, init, upgrade, watch, benchmark)
look like a src/cli/ group, but main.sh needs its own split under #931 first — grouping it
now would just move a 1427-line file. Out of scope here.

Process

Same as #931, and one group per PR:

  1. Post the mapping on this issue before moving code.
  2. git mv so the rename is recorded as a rename, not add+delete.
  3. Update the aggregator to index.sh and the entrypoint's source line.
  4. Grep for hardcoded paths before committing — tests/, build.sh, Makefile,
    .github/, .editorconfig, .gitignore. refactor(learn): split src/learn.sh into a src/learn/ module #938 shipped 14 red tests from one hardcoded
    src/learn.sh in a test file.

Constraints (all learned the hard way)

  • Aggregators hold only source lines and comments — enforced by
    test_module_aggregators_hold_only_source_lines_and_comments, which globs src/*/index.sh.
  • .gitignore can swallow a new module directory.coverage/ was unanchored and hid
    twelve files from git with no ?? in git status. Run
    git check-ignore -v src/<module>/<file>.sh before committing.
  • CI runs ShellCheck per file without -x, so a split exposes cross-file symbol use a
    monolith hid. Reproduce locally with SHELLCHECK_OPTS="-e SC1091 -e SC2155 -e SC2016".
  • A file-wide # shellcheck disable= moves to the files that need it, not to all of them.
  • Per-file .editorconfig rules are lost by a split — check before, decide deliberately.
  • Derive segment boundaries from the next function's start, not a ^}$ brace. A brace
    inside a heredoc ends the segment early and silently splits a function across files (refactor(learn): split src/learn.sh into a src/learn/ module #938).
  • Bash 3.0+ floor; per-test paths stay fork-free.

Verification, per PR

Prove it is a relocation: the non-blank line multiset differs only by new shebangs, module
headers and source lines, and the function count is unchanged. Then the built artifact —
either byte-identical, or its sorted code content identical when grouping reorders
definitions (safe: everything is sourced before anything is invoked).

Then: ./bashunit tests/ · --parallel · --parallel --simple --strict · make sa ·
make lint · CI-mode ShellCheck · bash build.sh bin -v printing ✅ Build verified ✅.

Acceptance criteria (per group PR)

  • Mapping posted on this issue before code moves
  • git diff shows only relocations — no renamed functions, no changed logic
  • Aggregator is src/<group>/index.sh and holds only source lines and comments
  • Every new file's first line is #!/usr/bin/env bash
  • Hardcoded path references updated (grep tests/ build.sh Makefile .github/)
  • git check-ignore -v confirms the new directory is not ignored
  • All suites above green, including bash build.sh bin -v
  • .claude/rules/architecture-map.md module table updated
  • No CHANGELOG entry — internal, no user-visible behaviour change

Do not

  • Do not rename functions; only file paths change
  • Do not group more than one context per PR
  • Do not fold skip_todo.sh / test_doubles.sh into src/assert/
  • Do not group main.sh — it needs its own split under refactor(src): finish the module split — 9 large files still flat #931 first
  • Do not create tests/unit/<group>/; make test globs one level
  • Do not run shfmt -w; make lint is the format gate

Metadata

Metadata

Assignees

Labels

refactoringRefactoring or cleaning related

Type

No type

Projects

  • Status
    Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

, 'i'); if (__m === '*' || __re.test(location.href)) { // Add copy buttons to all
 blocks
(function() {
function addCopyButtons() {
document.querySelectorAll('pre code').forEach(function(codeBlock) {
if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;
codeBlock.parentElement.setAttribute('data-copy-added', 'true');
var btn = document.createElement('button');
btn.textContent = 'Copy';
btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';
btn.onmouseover = function() { this.style.opacity = '1'; };
btn.onmouseout = function() { this.style.opacity = '0.7'; };
btn.onclick = function() {
navigator.clipboard.writeText(codeBlock.textContent).then(function() {
btn.textContent = 'Copied!';
setTimeout(function() { btn.textContent = 'Copy'; }, 1500);
});
};
codeBlock.parentElement.style.position = 'relative';
codeBlock.parentElement.appendChild(btn);
});
}
addCopyButtons();
// Re-run on dynamic content
var observer = new MutationObserver(addCopyButtons);
observer.observe(document.body, { childList: true, subtree: true });
})();
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
refactor(src): group cohesive flat files into modules (assert, console, util) · Issue #940 · TypedDevs/bashunit · GitHub
Skip to content

refactor(src): group cohesive flat files into modules (assert, console, util) #940

Description

@Chemaclass

Summary

#931 splits large files into modules. This is the other half: src/ still holds 36 flat
files
, many of them small and clearly belonging to the same concept. Ten of them are
assert_*.sh. Group by context so a directory names a concept, following ADR-010's
index.sh convention.

Measured on main (c8b0b53).

Group 1 — src/assert/ (highest confidence, do first)

Ten files, 2313 lines, already aggregated together by src/assertions.sh. The concept is
established; only the directory is missing.

NowBecomes
src/assertions.shsrc/assert/index.sh
src/assert.sh (970)src/assert/core.sh
src/assert_arrays.sh (101)src/assert/arrays.sh
src/assert_assertions.sh (192)src/assert/assertions.sh
src/assert_dates.sh (185)src/assert/dates.sh
src/assert_duration.sh (69)src/assert/duration.sh
src/assert_files.sh (156)src/assert/files.sh
src/assert_folders.sh (118)src/assert/folders.sh
src/assert_json.sh (67)src/assert/json.sh
src/assert_once.sh (156)src/assert/once.sh
src/assert_snapshot.sh (299)src/assert/snapshot.sh

Drop the assert_ prefix inside the directory — it carries the concept, as
learn/lessons/basics.sh does. Function names do not change; only file names.

Two callers hardcode these paths and must move with them:

  • tests/unit/completions_test.sh:30 greps src/assert*.sh to derive the public assertion
    list for the completions parity contract → becomes src/assert/*.sh.
  • tests/unit/build_test.sh special-cases src/assertions.sh as "the one flat-file
    aggregator" outside the src/*/index.sh glob. That special case disappears — a welcome
    simplification, and the reason ADR-010's original "beside" argument was withdrawn.

src/assertions.sh also sources skip_todo.sh and test_doubles.sh, which are not
assertions (skip/todo markers; spies and mocks). Leave both flat in this group, or handle
them in group 4 — do not sweep them into src/assert/ just because the aggregator lists them.

Group 2 — src/console/ (high confidence)

Output rendering, 1220 lines:

  • src/colors.sh (57) → src/console/colors.sh
  • src/console_header.sh (335) → src/console/header.sh
  • src/console_results.sh (828) → src/console/results.sh

console_results.sh is also on #931's list at 828 lines. Prefer grouping first, splitting
second
: move it into src/console/ here, then split it inside the module under #931 if
still warranted.

Group 3 — src/cli/ (high confidence)

The subcommand implementations, ~430 lines. main.sh is the only caller of all four,
and each is literally a bashunit <subcommand> implementation:

  • src/upgrade.sh (54) → src/cli/upgrade.sh
  • src/watch.sh (112) → src/cli/watch.sh
  • src/doc.sh (198) → src/cli/doc.sh
  • src/init.sh (66) → src/cli/init.sh

benchmark.sh looks like it belongs but src/runner/bench.sh also calls it, so it is a
shared implementation rather than purely a subcommand — it stays flat. main.sh is the
dispatcher and needs its own split under #931 first; grouping it now would only relocate a
1427-line file.

Zero hardcoded path references to any of the four.

Won't do: src/util/ and src/system/

Considered and declined, recorded here so it is not re-litigated.

The candidates were str (156), math (104), io (31), check_os (107),
dependencies (42), clock (203) — 2 to 10 functions each. These are the most obviously
named files in the repo; nobody has struggled to find math.sh. Wrapping them costs an
index.sh, a source line and a directory hop, and buys navigation for files that were
never hard to navigate. A two-file util/ is not a module, it is ceremony.

ADR-010's rationale is a directory for something with internal structure. A 31-line file
has none.

The effort is better spent on helpers.sh (909 lines, 30 functions, six unrelated concerns:
test-function naming, discovery, data providers, tags, base64 encoding, misc). That is a
cohesion problem, tracked on #931.

Explicitly stays flat

Not everything benefits from a directory. These are single-concept files or cross-cutting
state, and grouping them would invent a concept that does not exist:

bashunit.sh (public custom-assert facade) · globals.sh (public test API) · env.sh ·
state.sh · helpers.sh · clock.sh · parallel.sh · rerun.sh · test_title.sh

main.sh (1427) and the subcommand files (doc, init, upgrade, watch, benchmark)
look like a src/cli/ group, but main.sh needs its own split under #931 first — grouping it
now would just move a 1427-line file. Out of scope here.

Process

Same as #931, and one group per PR:

  1. Post the mapping on this issue before moving code.
  2. git mv so the rename is recorded as a rename, not add+delete.
  3. Update the aggregator to index.sh and the entrypoint's source line.
  4. Grep for hardcoded paths before committing — tests/, build.sh, Makefile,
    .github/, .editorconfig, .gitignore. refactor(learn): split src/learn.sh into a src/learn/ module #938 shipped 14 red tests from one hardcoded
    src/learn.sh in a test file.

Constraints (all learned the hard way)

  • Aggregators hold only source lines and comments — enforced by
    test_module_aggregators_hold_only_source_lines_and_comments, which globs src/*/index.sh.
  • .gitignore can swallow a new module directory.coverage/ was unanchored and hid
    twelve files from git with no ?? in git status. Run
    git check-ignore -v src/<module>/<file>.sh before committing.
  • CI runs ShellCheck per file without -x, so a split exposes cross-file symbol use a
    monolith hid. Reproduce locally with SHELLCHECK_OPTS="-e SC1091 -e SC2155 -e SC2016".
  • A file-wide # shellcheck disable= moves to the files that need it, not to all of them.
  • Per-file .editorconfig rules are lost by a split — check before, decide deliberately.
  • Derive segment boundaries from the next function's start, not a ^}$ brace. A brace
    inside a heredoc ends the segment early and silently splits a function across files (refactor(learn): split src/learn.sh into a src/learn/ module #938).
  • Bash 3.0+ floor; per-test paths stay fork-free.

Verification, per PR

Prove it is a relocation: the non-blank line multiset differs only by new shebangs, module
headers and source lines, and the function count is unchanged. Then the built artifact —
either byte-identical, or its sorted code content identical when grouping reorders
definitions (safe: everything is sourced before anything is invoked).

Then: ./bashunit tests/ · --parallel · --parallel --simple --strict · make sa ·
make lint · CI-mode ShellCheck · bash build.sh bin -v printing ✅ Build verified ✅.

Acceptance criteria (per group PR)

  • Mapping posted on this issue before code moves
  • git diff shows only relocations — no renamed functions, no changed logic
  • Aggregator is src/<group>/index.sh and holds only source lines and comments
  • Every new file's first line is #!/usr/bin/env bash
  • Hardcoded path references updated (grep tests/ build.sh Makefile .github/)
  • git check-ignore -v confirms the new directory is not ignored
  • All suites above green, including bash build.sh bin -v
  • .claude/rules/architecture-map.md module table updated
  • No CHANGELOG entry — internal, no user-visible behaviour change

Do not

  • Do not rename functions; only file paths change
  • Do not group more than one context per PR
  • Do not fold skip_todo.sh / test_doubles.sh into src/assert/
  • Do not group main.sh — it needs its own split under refactor(src): finish the module split — 9 large files still flat #931 first
  • Do not create tests/unit/<group>/; make test globs one level
  • Do not run shfmt -w; make lint is the format gate

Metadata

Metadata

Assignees

Labels

refactoringRefactoring or cleaning related

Type

No type

Projects

  • Status
    Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

, 'i'); if (__m === '*' || __re.test(location.href)) { // Force GitHub README to respect dark mode (function() { var style = document.createElement('style'); style.textContent = ' .markdown-body { color-scheme: dark light; } .markdown-body pre { background: #161b22 !important; } .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; } .markdown-body table th, .markdown-body table td { border-color: #30363d !important; } .markdown-body img { background: #0d1117; } .markdown-body blockquote { border-left-color: #8b949e; } .markdown-body hr { border-color: #30363d; } '; document.head.appendChild(style); })(); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' refactor(src): group cohesive flat files into modules (assert, console, util) · Issue #940 · TypedDevs/bashunit · GitHub
Skip to content

refactor(src): group cohesive flat files into modules (assert, console, util) #940

Description

@Chemaclass

Summary

#931 splits large files into modules. This is the other half: src/ still holds 36 flat
files
, many of them small and clearly belonging to the same concept. Ten of them are
assert_*.sh. Group by context so a directory names a concept, following ADR-010's
index.sh convention.

Measured on main (c8b0b53).

Group 1 — src/assert/ (highest confidence, do first)

Ten files, 2313 lines, already aggregated together by src/assertions.sh. The concept is
established; only the directory is missing.

NowBecomes
src/assertions.shsrc/assert/index.sh
src/assert.sh (970)src/assert/core.sh
src/assert_arrays.sh (101)src/assert/arrays.sh
src/assert_assertions.sh (192)src/assert/assertions.sh
src/assert_dates.sh (185)src/assert/dates.sh
src/assert_duration.sh (69)src/assert/duration.sh
src/assert_files.sh (156)src/assert/files.sh
src/assert_folders.sh (118)src/assert/folders.sh
src/assert_json.sh (67)src/assert/json.sh
src/assert_once.sh (156)src/assert/once.sh
src/assert_snapshot.sh (299)src/assert/snapshot.sh

Drop the assert_ prefix inside the directory — it carries the concept, as
learn/lessons/basics.sh does. Function names do not change; only file names.

Two callers hardcode these paths and must move with them:

  • tests/unit/completions_test.sh:30 greps src/assert*.sh to derive the public assertion
    list for the completions parity contract → becomes src/assert/*.sh.
  • tests/unit/build_test.sh special-cases src/assertions.sh as "the one flat-file
    aggregator" outside the src/*/index.sh glob. That special case disappears — a welcome
    simplification, and the reason ADR-010's original "beside" argument was withdrawn.

src/assertions.sh also sources skip_todo.sh and test_doubles.sh, which are not
assertions (skip/todo markers; spies and mocks). Leave both flat in this group, or handle
them in group 4 — do not sweep them into src/assert/ just because the aggregator lists them.

Group 2 — src/console/ (high confidence)

Output rendering, 1220 lines:

  • src/colors.sh (57) → src/console/colors.sh
  • src/console_header.sh (335) → src/console/header.sh
  • src/console_results.sh (828) → src/console/results.sh

console_results.sh is also on #931's list at 828 lines. Prefer grouping first, splitting
second
: move it into src/console/ here, then split it inside the module under #931 if
still warranted.

Group 3 — src/cli/ (high confidence)

The subcommand implementations, ~430 lines. main.sh is the only caller of all four,
and each is literally a bashunit <subcommand> implementation:

  • src/upgrade.sh (54) → src/cli/upgrade.sh
  • src/watch.sh (112) → src/cli/watch.sh
  • src/doc.sh (198) → src/cli/doc.sh
  • src/init.sh (66) → src/cli/init.sh

benchmark.sh looks like it belongs but src/runner/bench.sh also calls it, so it is a
shared implementation rather than purely a subcommand — it stays flat. main.sh is the
dispatcher and needs its own split under #931 first; grouping it now would only relocate a
1427-line file.

Zero hardcoded path references to any of the four.

Won't do: src/util/ and src/system/

Considered and declined, recorded here so it is not re-litigated.

The candidates were str (156), math (104), io (31), check_os (107),
dependencies (42), clock (203) — 2 to 10 functions each. These are the most obviously
named files in the repo; nobody has struggled to find math.sh. Wrapping them costs an
index.sh, a source line and a directory hop, and buys navigation for files that were
never hard to navigate. A two-file util/ is not a module, it is ceremony.

ADR-010's rationale is a directory for something with internal structure. A 31-line file
has none.

The effort is better spent on helpers.sh (909 lines, 30 functions, six unrelated concerns:
test-function naming, discovery, data providers, tags, base64 encoding, misc). That is a
cohesion problem, tracked on #931.

Explicitly stays flat

Not everything benefits from a directory. These are single-concept files or cross-cutting
state, and grouping them would invent a concept that does not exist:

bashunit.sh (public custom-assert facade) · globals.sh (public test API) · env.sh ·
state.sh · helpers.sh · clock.sh · parallel.sh · rerun.sh · test_title.sh

main.sh (1427) and the subcommand files (doc, init, upgrade, watch, benchmark)
look like a src/cli/ group, but main.sh needs its own split under #931 first — grouping it
now would just move a 1427-line file. Out of scope here.

Process

Same as #931, and one group per PR:

  1. Post the mapping on this issue before moving code.
  2. git mv so the rename is recorded as a rename, not add+delete.
  3. Update the aggregator to index.sh and the entrypoint's source line.
  4. Grep for hardcoded paths before committing — tests/, build.sh, Makefile,
    .github/, .editorconfig, .gitignore. refactor(learn): split src/learn.sh into a src/learn/ module #938 shipped 14 red tests from one hardcoded
    src/learn.sh in a test file.

Constraints (all learned the hard way)

  • Aggregators hold only source lines and comments — enforced by
    test_module_aggregators_hold_only_source_lines_and_comments, which globs src/*/index.sh.
  • .gitignore can swallow a new module directory.coverage/ was unanchored and hid
    twelve files from git with no ?? in git status. Run
    git check-ignore -v src/<module>/<file>.sh before committing.
  • CI runs ShellCheck per file without -x, so a split exposes cross-file symbol use a
    monolith hid. Reproduce locally with SHELLCHECK_OPTS="-e SC1091 -e SC2155 -e SC2016".
  • A file-wide # shellcheck disable= moves to the files that need it, not to all of them.
  • Per-file .editorconfig rules are lost by a split — check before, decide deliberately.
  • Derive segment boundaries from the next function's start, not a ^}$ brace. A brace
    inside a heredoc ends the segment early and silently splits a function across files (refactor(learn): split src/learn.sh into a src/learn/ module #938).
  • Bash 3.0+ floor; per-test paths stay fork-free.

Verification, per PR

Prove it is a relocation: the non-blank line multiset differs only by new shebangs, module
headers and source lines, and the function count is unchanged. Then the built artifact —
either byte-identical, or its sorted code content identical when grouping reorders
definitions (safe: everything is sourced before anything is invoked).

Then: ./bashunit tests/ · --parallel · --parallel --simple --strict · make sa ·
make lint · CI-mode ShellCheck · bash build.sh bin -v printing ✅ Build verified ✅.

Acceptance criteria (per group PR)

  • Mapping posted on this issue before code moves
  • git diff shows only relocations — no renamed functions, no changed logic
  • Aggregator is src/<group>/index.sh and holds only source lines and comments
  • Every new file's first line is #!/usr/bin/env bash
  • Hardcoded path references updated (grep tests/ build.sh Makefile .github/)
  • git check-ignore -v confirms the new directory is not ignored
  • All suites above green, including bash build.sh bin -v
  • .claude/rules/architecture-map.md module table updated
  • No CHANGELOG entry — internal, no user-visible behaviour change

Do not

  • Do not rename functions; only file paths change
  • Do not group more than one context per PR
  • Do not fold skip_todo.sh / test_doubles.sh into src/assert/
  • Do not group main.sh — it needs its own split under refactor(src): finish the module split — 9 large files still flat #931 first
  • Do not create tests/unit/<group>/; make test globs one level
  • Do not run shfmt -w; make lint is the format gate

Metadata

Metadata

Assignees

Labels

refactoringRefactoring or cleaning related

Type

No type

Projects

  • Status
    Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

, 'i'); if (__m === '*' || __re.test(location.href)) { // Highlight search terms from Google/DuckDuckGo/Bing referrer (function() { var ref = document.referrer; var terms = []; if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) { var url = new URL(ref); var q = url.searchParams.get('q') || url.searchParams.get('p'); if (q) { terms = q.split(/\s+/).filter(function(t) { return t.length > 2; }); } } if (terms.length === 0) return; var style = document.createElement('style'); style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }'; document.head.appendChild(style); function highlight(node) { if (node.nodeType === 3) { // text node var text = node.textContent; var found = false; terms.forEach(function(term) { var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\]\\]/g, '\\') + ')', 'gi'); if (regex.test(text)) { found = true; var frag = document.createDocumentFragment(); var parts = text.split(regex); parts.forEach(function(part, i) { if (i % 2 === 0) { frag.appendChild(document.createTextNode(part)); } else { var span = document.createElement('span'); span.className = 'userscript-highlight'; span.textContent = part; frag.appendChild(span); } }); node.parentNode.replaceChild(frag, node); } }); } else if (node.nodeType === 1 && node.childNodes) { // element var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT']; if (!skipTags.includes(node.tagName)) { Array.from(node.childNodes).forEach(highlight); } } } highlight(document.body); // Re-highlight on dynamic content var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1 || node.nodeType === 3) highlight(node); }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' refactor(src): group cohesive flat files into modules (assert, console, util) · Issue #940 · TypedDevs/bashunit · GitHub
Skip to content

refactor(src): group cohesive flat files into modules (assert, console, util) #940

Description

@Chemaclass

Summary

#931 splits large files into modules. This is the other half: src/ still holds 36 flat
files
, many of them small and clearly belonging to the same concept. Ten of them are
assert_*.sh. Group by context so a directory names a concept, following ADR-010's
index.sh convention.

Measured on main (c8b0b53).

Group 1 — src/assert/ (highest confidence, do first)

Ten files, 2313 lines, already aggregated together by src/assertions.sh. The concept is
established; only the directory is missing.

NowBecomes
src/assertions.shsrc/assert/index.sh
src/assert.sh (970)src/assert/core.sh
src/assert_arrays.sh (101)src/assert/arrays.sh
src/assert_assertions.sh (192)src/assert/assertions.sh
src/assert_dates.sh (185)src/assert/dates.sh
src/assert_duration.sh (69)src/assert/duration.sh
src/assert_files.sh (156)src/assert/files.sh
src/assert_folders.sh (118)src/assert/folders.sh
src/assert_json.sh (67)src/assert/json.sh
src/assert_once.sh (156)src/assert/once.sh
src/assert_snapshot.sh (299)src/assert/snapshot.sh

Drop the assert_ prefix inside the directory — it carries the concept, as
learn/lessons/basics.sh does. Function names do not change; only file names.

Two callers hardcode these paths and must move with them:

  • tests/unit/completions_test.sh:30 greps src/assert*.sh to derive the public assertion
    list for the completions parity contract → becomes src/assert/*.sh.
  • tests/unit/build_test.sh special-cases src/assertions.sh as "the one flat-file
    aggregator" outside the src/*/index.sh glob. That special case disappears — a welcome
    simplification, and the reason ADR-010's original "beside" argument was withdrawn.

src/assertions.sh also sources skip_todo.sh and test_doubles.sh, which are not
assertions (skip/todo markers; spies and mocks). Leave both flat in this group, or handle
them in group 4 — do not sweep them into src/assert/ just because the aggregator lists them.

Group 2 — src/console/ (high confidence)

Output rendering, 1220 lines:

  • src/colors.sh (57) → src/console/colors.sh
  • src/console_header.sh (335) → src/console/header.sh
  • src/console_results.sh (828) → src/console/results.sh

console_results.sh is also on #931's list at 828 lines. Prefer grouping first, splitting
second
: move it into src/console/ here, then split it inside the module under #931 if
still warranted.

Group 3 — src/cli/ (high confidence)

The subcommand implementations, ~430 lines. main.sh is the only caller of all four,
and each is literally a bashunit <subcommand> implementation:

  • src/upgrade.sh (54) → src/cli/upgrade.sh
  • src/watch.sh (112) → src/cli/watch.sh
  • src/doc.sh (198) → src/cli/doc.sh
  • src/init.sh (66) → src/cli/init.sh

benchmark.sh looks like it belongs but src/runner/bench.sh also calls it, so it is a
shared implementation rather than purely a subcommand — it stays flat. main.sh is the
dispatcher and needs its own split under #931 first; grouping it now would only relocate a
1427-line file.

Zero hardcoded path references to any of the four.

Won't do: src/util/ and src/system/

Considered and declined, recorded here so it is not re-litigated.

The candidates were str (156), math (104), io (31), check_os (107),
dependencies (42), clock (203) — 2 to 10 functions each. These are the most obviously
named files in the repo; nobody has struggled to find math.sh. Wrapping them costs an
index.sh, a source line and a directory hop, and buys navigation for files that were
never hard to navigate. A two-file util/ is not a module, it is ceremony.

ADR-010's rationale is a directory for something with internal structure. A 31-line file
has none.

The effort is better spent on helpers.sh (909 lines, 30 functions, six unrelated concerns:
test-function naming, discovery, data providers, tags, base64 encoding, misc). That is a
cohesion problem, tracked on #931.

Explicitly stays flat

Not everything benefits from a directory. These are single-concept files or cross-cutting
state, and grouping them would invent a concept that does not exist:

bashunit.sh (public custom-assert facade) · globals.sh (public test API) · env.sh ·
state.sh · helpers.sh · clock.sh · parallel.sh · rerun.sh · test_title.sh

main.sh (1427) and the subcommand files (doc, init, upgrade, watch, benchmark)
look like a src/cli/ group, but main.sh needs its own split under #931 first — grouping it
now would just move a 1427-line file. Out of scope here.

Process

Same as #931, and one group per PR:

  1. Post the mapping on this issue before moving code.
  2. git mv so the rename is recorded as a rename, not add+delete.
  3. Update the aggregator to index.sh and the entrypoint's source line.
  4. Grep for hardcoded paths before committing — tests/, build.sh, Makefile,
    .github/, .editorconfig, .gitignore. refactor(learn): split src/learn.sh into a src/learn/ module #938 shipped 14 red tests from one hardcoded
    src/learn.sh in a test file.

Constraints (all learned the hard way)

  • Aggregators hold only source lines and comments — enforced by
    test_module_aggregators_hold_only_source_lines_and_comments, which globs src/*/index.sh.
  • .gitignore can swallow a new module directory.coverage/ was unanchored and hid
    twelve files from git with no ?? in git status. Run
    git check-ignore -v src/<module>/<file>.sh before committing.
  • CI runs ShellCheck per file without -x, so a split exposes cross-file symbol use a
    monolith hid. Reproduce locally with SHELLCHECK_OPTS="-e SC1091 -e SC2155 -e SC2016".
  • A file-wide # shellcheck disable= moves to the files that need it, not to all of them.
  • Per-file .editorconfig rules are lost by a split — check before, decide deliberately.
  • Derive segment boundaries from the next function's start, not a ^}$ brace. A brace
    inside a heredoc ends the segment early and silently splits a function across files (refactor(learn): split src/learn.sh into a src/learn/ module #938).
  • Bash 3.0+ floor; per-test paths stay fork-free.

Verification, per PR

Prove it is a relocation: the non-blank line multiset differs only by new shebangs, module
headers and source lines, and the function count is unchanged. Then the built artifact —
either byte-identical, or its sorted code content identical when grouping reorders
definitions (safe: everything is sourced before anything is invoked).

Then: ./bashunit tests/ · --parallel · --parallel --simple --strict · make sa ·
make lint · CI-mode ShellCheck · bash build.sh bin -v printing ✅ Build verified ✅.

Acceptance criteria (per group PR)

  • Mapping posted on this issue before code moves
  • git diff shows only relocations — no renamed functions, no changed logic
  • Aggregator is src/<group>/index.sh and holds only source lines and comments
  • Every new file's first line is #!/usr/bin/env bash
  • Hardcoded path references updated (grep tests/ build.sh Makefile .github/)
  • git check-ignore -v confirms the new directory is not ignored
  • All suites above green, including bash build.sh bin -v
  • .claude/rules/architecture-map.md module table updated
  • No CHANGELOG entry — internal, no user-visible behaviour change

Do not

  • Do not rename functions; only file paths change
  • Do not group more than one context per PR
  • Do not fold skip_todo.sh / test_doubles.sh into src/assert/
  • Do not group main.sh — it needs its own split under refactor(src): finish the module split — 9 large files still flat #931 first
  • Do not create tests/unit/<group>/; make test globs one level
  • Do not run shfmt -w; make lint is the format gate

Metadata

Metadata

Assignees

Labels

refactoringRefactoring or cleaning related

Type

No type

Projects

  • Status
    Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

, 'i'); if (__m === '*' || __re.test(location.href)) { // Strip utm_, fbclid, gclid, etc. from all links on page (function() { var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid', 'ref', 'ref_src', 'source', 'medium', 'campaign']; function cleanUrl(url) { try { var u = new URL(url, window.location.origin); var changed = false; trackingParams.forEach(function(p) { if (u.searchParams.has(p)) { u.searchParams.delete(p); changed = true; } }); return changed ? u.toString() : url; } catch (e) { return url; } } function cleanLinks() { document.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } cleanLinks(); var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1) { if (node.tagName === 'A') cleanLinks(); node.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + ' refactor(src): group cohesive flat files into modules (assert, console, util) · Issue #940 · TypedDevs/bashunit · GitHub
Skip to content

refactor(src): group cohesive flat files into modules (assert, console, util) #940

Description

@Chemaclass

Summary

#931 splits large files into modules. This is the other half: src/ still holds 36 flat
files
, many of them small and clearly belonging to the same concept. Ten of them are
assert_*.sh. Group by context so a directory names a concept, following ADR-010's
index.sh convention.

Measured on main (c8b0b53).

Group 1 — src/assert/ (highest confidence, do first)

Ten files, 2313 lines, already aggregated together by src/assertions.sh. The concept is
established; only the directory is missing.

NowBecomes
src/assertions.shsrc/assert/index.sh
src/assert.sh (970)src/assert/core.sh
src/assert_arrays.sh (101)src/assert/arrays.sh
src/assert_assertions.sh (192)src/assert/assertions.sh
src/assert_dates.sh (185)src/assert/dates.sh
src/assert_duration.sh (69)src/assert/duration.sh
src/assert_files.sh (156)src/assert/files.sh
src/assert_folders.sh (118)src/assert/folders.sh
src/assert_json.sh (67)src/assert/json.sh
src/assert_once.sh (156)src/assert/once.sh
src/assert_snapshot.sh (299)src/assert/snapshot.sh

Drop the assert_ prefix inside the directory — it carries the concept, as
learn/lessons/basics.sh does. Function names do not change; only file names.

Two callers hardcode these paths and must move with them:

  • tests/unit/completions_test.sh:30 greps src/assert*.sh to derive the public assertion
    list for the completions parity contract → becomes src/assert/*.sh.
  • tests/unit/build_test.sh special-cases src/assertions.sh as "the one flat-file
    aggregator" outside the src/*/index.sh glob. That special case disappears — a welcome
    simplification, and the reason ADR-010's original "beside" argument was withdrawn.

src/assertions.sh also sources skip_todo.sh and test_doubles.sh, which are not
assertions (skip/todo markers; spies and mocks). Leave both flat in this group, or handle
them in group 4 — do not sweep them into src/assert/ just because the aggregator lists them.

Group 2 — src/console/ (high confidence)

Output rendering, 1220 lines:

  • src/colors.sh (57) → src/console/colors.sh
  • src/console_header.sh (335) → src/console/header.sh
  • src/console_results.sh (828) → src/console/results.sh

console_results.sh is also on #931's list at 828 lines. Prefer grouping first, splitting
second
: move it into src/console/ here, then split it inside the module under #931 if
still warranted.

Group 3 — src/cli/ (high confidence)

The subcommand implementations, ~430 lines. main.sh is the only caller of all four,
and each is literally a bashunit <subcommand> implementation:

  • src/upgrade.sh (54) → src/cli/upgrade.sh
  • src/watch.sh (112) → src/cli/watch.sh
  • src/doc.sh (198) → src/cli/doc.sh
  • src/init.sh (66) → src/cli/init.sh

benchmark.sh looks like it belongs but src/runner/bench.sh also calls it, so it is a
shared implementation rather than purely a subcommand — it stays flat. main.sh is the
dispatcher and needs its own split under #931 first; grouping it now would only relocate a
1427-line file.

Zero hardcoded path references to any of the four.

Won't do: src/util/ and src/system/

Considered and declined, recorded here so it is not re-litigated.

The candidates were str (156), math (104), io (31), check_os (107),
dependencies (42), clock (203) — 2 to 10 functions each. These are the most obviously
named files in the repo; nobody has struggled to find math.sh. Wrapping them costs an
index.sh, a source line and a directory hop, and buys navigation for files that were
never hard to navigate. A two-file util/ is not a module, it is ceremony.

ADR-010's rationale is a directory for something with internal structure. A 31-line file
has none.

The effort is better spent on helpers.sh (909 lines, 30 functions, six unrelated concerns:
test-function naming, discovery, data providers, tags, base64 encoding, misc). That is a
cohesion problem, tracked on #931.

Explicitly stays flat

Not everything benefits from a directory. These are single-concept files or cross-cutting
state, and grouping them would invent a concept that does not exist:

bashunit.sh (public custom-assert facade) · globals.sh (public test API) · env.sh ·
state.sh · helpers.sh · clock.sh · parallel.sh · rerun.sh · test_title.sh

main.sh (1427) and the subcommand files (doc, init, upgrade, watch, benchmark)
look like a src/cli/ group, but main.sh needs its own split under #931 first — grouping it
now would just move a 1427-line file. Out of scope here.

Process

Same as #931, and one group per PR:

  1. Post the mapping on this issue before moving code.
  2. git mv so the rename is recorded as a rename, not add+delete.
  3. Update the aggregator to index.sh and the entrypoint's source line.
  4. Grep for hardcoded paths before committing — tests/, build.sh, Makefile,
    .github/, .editorconfig, .gitignore. refactor(learn): split src/learn.sh into a src/learn/ module #938 shipped 14 red tests from one hardcoded
    src/learn.sh in a test file.

Constraints (all learned the hard way)

  • Aggregators hold only source lines and comments — enforced by
    test_module_aggregators_hold_only_source_lines_and_comments, which globs src/*/index.sh.
  • .gitignore can swallow a new module directory.coverage/ was unanchored and hid
    twelve files from git with no ?? in git status. Run
    git check-ignore -v src/<module>/<file>.sh before committing.
  • CI runs ShellCheck per file without -x, so a split exposes cross-file symbol use a
    monolith hid. Reproduce locally with SHELLCHECK_OPTS="-e SC1091 -e SC2155 -e SC2016".
  • A file-wide # shellcheck disable= moves to the files that need it, not to all of them.
  • Per-file .editorconfig rules are lost by a split — check before, decide deliberately.
  • Derive segment boundaries from the next function's start, not a ^}$ brace. A brace
    inside a heredoc ends the segment early and silently splits a function across files (refactor(learn): split src/learn.sh into a src/learn/ module #938).
  • Bash 3.0+ floor; per-test paths stay fork-free.

Verification, per PR

Prove it is a relocation: the non-blank line multiset differs only by new shebangs, module
headers and source lines, and the function count is unchanged. Then the built artifact —
either byte-identical, or its sorted code content identical when grouping reorders
definitions (safe: everything is sourced before anything is invoked).

Then: ./bashunit tests/ · --parallel · --parallel --simple --strict · make sa ·
make lint · CI-mode ShellCheck · bash build.sh bin -v printing ✅ Build verified ✅.

Acceptance criteria (per group PR)

  • Mapping posted on this issue before code moves
  • git diff shows only relocations — no renamed functions, no changed logic
  • Aggregator is src/<group>/index.sh and holds only source lines and comments
  • Every new file's first line is #!/usr/bin/env bash
  • Hardcoded path references updated (grep tests/ build.sh Makefile .github/)
  • git check-ignore -v confirms the new directory is not ignored
  • All suites above green, including bash build.sh bin -v
  • .claude/rules/architecture-map.md module table updated
  • No CHANGELOG entry — internal, no user-visible behaviour change

Do not

  • Do not rename functions; only file paths change
  • Do not group more than one context per PR
  • Do not fold skip_todo.sh / test_doubles.sh into src/assert/
  • Do not group main.sh — it needs its own split under refactor(src): finish the module split — 9 large files still flat #931 first
  • Do not create tests/unit/<group>/; make test globs one level
  • Do not run shfmt -w; make lint is the format gate

Metadata

Metadata

Assignees

Labels

refactoringRefactoring or cleaning related

Type

No type

Projects

  • Status
    Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

, 'i'); if (__m === '*' || __re.test(location.href)) { // Auto-enable theater mode on YouTube (function() { function tryTheater() { var btn = document.querySelector('button[aria-label="Theater mode"], ytd-player #player button[title="Theater mode"]'); if (btn && !btn.classList.contains('activated')) { btn.click(); } } // Try immediately tryTheater(); // Try after navigation (SPA) var lastUrl = location.href; setInterval(function() { if (location.href !== lastUrl) { lastUrl = location.href; setTimeout(tryTheater, 500); } }, 1000); // Also try on player load var observer = new MutationObserver(tryTheater); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' refactor(src): group cohesive flat files into modules (assert, console, util) · Issue #940 · TypedDevs/bashunit · GitHub
Skip to content

refactor(src): group cohesive flat files into modules (assert, console, util) #940

Description

@Chemaclass

Summary

#931 splits large files into modules. This is the other half: src/ still holds 36 flat
files
, many of them small and clearly belonging to the same concept. Ten of them are
assert_*.sh. Group by context so a directory names a concept, following ADR-010's
index.sh convention.

Measured on main (c8b0b53).

Group 1 — src/assert/ (highest confidence, do first)

Ten files, 2313 lines, already aggregated together by src/assertions.sh. The concept is
established; only the directory is missing.

NowBecomes
src/assertions.shsrc/assert/index.sh
src/assert.sh (970)src/assert/core.sh
src/assert_arrays.sh (101)src/assert/arrays.sh
src/assert_assertions.sh (192)src/assert/assertions.sh
src/assert_dates.sh (185)src/assert/dates.sh
src/assert_duration.sh (69)src/assert/duration.sh
src/assert_files.sh (156)src/assert/files.sh
src/assert_folders.sh (118)src/assert/folders.sh
src/assert_json.sh (67)src/assert/json.sh
src/assert_once.sh (156)src/assert/once.sh
src/assert_snapshot.sh (299)src/assert/snapshot.sh

Drop the assert_ prefix inside the directory — it carries the concept, as
learn/lessons/basics.sh does. Function names do not change; only file names.

Two callers hardcode these paths and must move with them:

  • tests/unit/completions_test.sh:30 greps src/assert*.sh to derive the public assertion
    list for the completions parity contract → becomes src/assert/*.sh.
  • tests/unit/build_test.sh special-cases src/assertions.sh as "the one flat-file
    aggregator" outside the src/*/index.sh glob. That special case disappears — a welcome
    simplification, and the reason ADR-010's original "beside" argument was withdrawn.

src/assertions.sh also sources skip_todo.sh and test_doubles.sh, which are not
assertions (skip/todo markers; spies and mocks). Leave both flat in this group, or handle
them in group 4 — do not sweep them into src/assert/ just because the aggregator lists them.

Group 2 — src/console/ (high confidence)

Output rendering, 1220 lines:

  • src/colors.sh (57) → src/console/colors.sh
  • src/console_header.sh (335) → src/console/header.sh
  • src/console_results.sh (828) → src/console/results.sh

console_results.sh is also on #931's list at 828 lines. Prefer grouping first, splitting
second
: move it into src/console/ here, then split it inside the module under #931 if
still warranted.

Group 3 — src/cli/ (high confidence)

The subcommand implementations, ~430 lines. main.sh is the only caller of all four,
and each is literally a bashunit <subcommand> implementation:

  • src/upgrade.sh (54) → src/cli/upgrade.sh
  • src/watch.sh (112) → src/cli/watch.sh
  • src/doc.sh (198) → src/cli/doc.sh
  • src/init.sh (66) → src/cli/init.sh

benchmark.sh looks like it belongs but src/runner/bench.sh also calls it, so it is a
shared implementation rather than purely a subcommand — it stays flat. main.sh is the
dispatcher and needs its own split under #931 first; grouping it now would only relocate a
1427-line file.

Zero hardcoded path references to any of the four.

Won't do: src/util/ and src/system/

Considered and declined, recorded here so it is not re-litigated.

The candidates were str (156), math (104), io (31), check_os (107),
dependencies (42), clock (203) — 2 to 10 functions each. These are the most obviously
named files in the repo; nobody has struggled to find math.sh. Wrapping them costs an
index.sh, a source line and a directory hop, and buys navigation for files that were
never hard to navigate. A two-file util/ is not a module, it is ceremony.

ADR-010's rationale is a directory for something with internal structure. A 31-line file
has none.

The effort is better spent on helpers.sh (909 lines, 30 functions, six unrelated concerns:
test-function naming, discovery, data providers, tags, base64 encoding, misc). That is a
cohesion problem, tracked on #931.

Explicitly stays flat

Not everything benefits from a directory. These are single-concept files or cross-cutting
state, and grouping them would invent a concept that does not exist:

bashunit.sh (public custom-assert facade) · globals.sh (public test API) · env.sh ·
state.sh · helpers.sh · clock.sh · parallel.sh · rerun.sh · test_title.sh

main.sh (1427) and the subcommand files (doc, init, upgrade, watch, benchmark)
look like a src/cli/ group, but main.sh needs its own split under #931 first — grouping it
now would just move a 1427-line file. Out of scope here.

Process

Same as #931, and one group per PR:

  1. Post the mapping on this issue before moving code.
  2. git mv so the rename is recorded as a rename, not add+delete.
  3. Update the aggregator to index.sh and the entrypoint's source line.
  4. Grep for hardcoded paths before committing — tests/, build.sh, Makefile,
    .github/, .editorconfig, .gitignore. refactor(learn): split src/learn.sh into a src/learn/ module #938 shipped 14 red tests from one hardcoded
    src/learn.sh in a test file.

Constraints (all learned the hard way)

  • Aggregators hold only source lines and comments — enforced by
    test_module_aggregators_hold_only_source_lines_and_comments, which globs src/*/index.sh.
  • .gitignore can swallow a new module directory.coverage/ was unanchored and hid
    twelve files from git with no ?? in git status. Run
    git check-ignore -v src/<module>/<file>.sh before committing.
  • CI runs ShellCheck per file without -x, so a split exposes cross-file symbol use a
    monolith hid. Reproduce locally with SHELLCHECK_OPTS="-e SC1091 -e SC2155 -e SC2016".
  • A file-wide # shellcheck disable= moves to the files that need it, not to all of them.
  • Per-file .editorconfig rules are lost by a split — check before, decide deliberately.
  • Derive segment boundaries from the next function's start, not a ^}$ brace. A brace
    inside a heredoc ends the segment early and silently splits a function across files (refactor(learn): split src/learn.sh into a src/learn/ module #938).
  • Bash 3.0+ floor; per-test paths stay fork-free.

Verification, per PR

Prove it is a relocation: the non-blank line multiset differs only by new shebangs, module
headers and source lines, and the function count is unchanged. Then the built artifact —
either byte-identical, or its sorted code content identical when grouping reorders
definitions (safe: everything is sourced before anything is invoked).

Then: ./bashunit tests/ · --parallel · --parallel --simple --strict · make sa ·
make lint · CI-mode ShellCheck · bash build.sh bin -v printing ✅ Build verified ✅.

Acceptance criteria (per group PR)

  • Mapping posted on this issue before code moves
  • git diff shows only relocations — no renamed functions, no changed logic
  • Aggregator is src/<group>/index.sh and holds only source lines and comments
  • Every new file's first line is #!/usr/bin/env bash
  • Hardcoded path references updated (grep tests/ build.sh Makefile .github/)
  • git check-ignore -v confirms the new directory is not ignored
  • All suites above green, including bash build.sh bin -v
  • .claude/rules/architecture-map.md module table updated
  • No CHANGELOG entry — internal, no user-visible behaviour change

Do not

  • Do not rename functions; only file paths change
  • Do not group more than one context per PR
  • Do not fold skip_todo.sh / test_doubles.sh into src/assert/
  • Do not group main.sh — it needs its own split under refactor(src): finish the module split — 9 large files still flat #931 first
  • Do not create tests/unit/<group>/; make test globs one level
  • Do not run shfmt -w; make lint is the format gate

Metadata

Metadata

Assignees

Labels

refactoringRefactoring or cleaning related

Type

No type

Projects

  • Status
    Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

, 'i'); if (__m === '*' || __re.test(location.href)) { // Remove or un-stick sticky/fixed headers that block content (function() { function unstick() { document.querySelectorAll('header, nav, [role="banner"], .header, .navbar, .sticky, .fixed-top, [style*="position: fixed"], [style*="position:sticky"]').forEach(function(el) { if (el.style.position === 'fixed' || el.style.position === 'sticky' || getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') { el.style.position = 'static'; el.style.top = 'auto'; el.style.zIndex = 'auto'; } }); } unstick(); var observer = new MutationObserver(unstick); observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] }); })(); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' refactor(src): group cohesive flat files into modules (assert, console, util) · Issue #940 · TypedDevs/bashunit · GitHub
Skip to content

refactor(src): group cohesive flat files into modules (assert, console, util) #940

Description

@Chemaclass

Summary

#931 splits large files into modules. This is the other half: src/ still holds 36 flat
files
, many of them small and clearly belonging to the same concept. Ten of them are
assert_*.sh. Group by context so a directory names a concept, following ADR-010's
index.sh convention.

Measured on main (c8b0b53).

Group 1 — src/assert/ (highest confidence, do first)

Ten files, 2313 lines, already aggregated together by src/assertions.sh. The concept is
established; only the directory is missing.

NowBecomes
src/assertions.shsrc/assert/index.sh
src/assert.sh (970)src/assert/core.sh
src/assert_arrays.sh (101)src/assert/arrays.sh
src/assert_assertions.sh (192)src/assert/assertions.sh
src/assert_dates.sh (185)src/assert/dates.sh
src/assert_duration.sh (69)src/assert/duration.sh
src/assert_files.sh (156)src/assert/files.sh
src/assert_folders.sh (118)src/assert/folders.sh
src/assert_json.sh (67)src/assert/json.sh
src/assert_once.sh (156)src/assert/once.sh
src/assert_snapshot.sh (299)src/assert/snapshot.sh

Drop the assert_ prefix inside the directory — it carries the concept, as
learn/lessons/basics.sh does. Function names do not change; only file names.

Two callers hardcode these paths and must move with them:

  • tests/unit/completions_test.sh:30 greps src/assert*.sh to derive the public assertion
    list for the completions parity contract → becomes src/assert/*.sh.
  • tests/unit/build_test.sh special-cases src/assertions.sh as "the one flat-file
    aggregator" outside the src/*/index.sh glob. That special case disappears — a welcome
    simplification, and the reason ADR-010's original "beside" argument was withdrawn.

src/assertions.sh also sources skip_todo.sh and test_doubles.sh, which are not
assertions (skip/todo markers; spies and mocks). Leave both flat in this group, or handle
them in group 4 — do not sweep them into src/assert/ just because the aggregator lists them.

Group 2 — src/console/ (high confidence)

Output rendering, 1220 lines:

  • src/colors.sh (57) → src/console/colors.sh
  • src/console_header.sh (335) → src/console/header.sh
  • src/console_results.sh (828) → src/console/results.sh

console_results.sh is also on #931's list at 828 lines. Prefer grouping first, splitting
second
: move it into src/console/ here, then split it inside the module under #931 if
still warranted.

Group 3 — src/cli/ (high confidence)

The subcommand implementations, ~430 lines. main.sh is the only caller of all four,
and each is literally a bashunit <subcommand> implementation:

  • src/upgrade.sh (54) → src/cli/upgrade.sh
  • src/watch.sh (112) → src/cli/watch.sh
  • src/doc.sh (198) → src/cli/doc.sh
  • src/init.sh (66) → src/cli/init.sh

benchmark.sh looks like it belongs but src/runner/bench.sh also calls it, so it is a
shared implementation rather than purely a subcommand — it stays flat. main.sh is the
dispatcher and needs its own split under #931 first; grouping it now would only relocate a
1427-line file.

Zero hardcoded path references to any of the four.

Won't do: src/util/ and src/system/

Considered and declined, recorded here so it is not re-litigated.

The candidates were str (156), math (104), io (31), check_os (107),
dependencies (42), clock (203) — 2 to 10 functions each. These are the most obviously
named files in the repo; nobody has struggled to find math.sh. Wrapping them costs an
index.sh, a source line and a directory hop, and buys navigation for files that were
never hard to navigate. A two-file util/ is not a module, it is ceremony.

ADR-010's rationale is a directory for something with internal structure. A 31-line file
has none.

The effort is better spent on helpers.sh (909 lines, 30 functions, six unrelated concerns:
test-function naming, discovery, data providers, tags, base64 encoding, misc). That is a
cohesion problem, tracked on #931.

Explicitly stays flat

Not everything benefits from a directory. These are single-concept files or cross-cutting
state, and grouping them would invent a concept that does not exist:

bashunit.sh (public custom-assert facade) · globals.sh (public test API) · env.sh ·
state.sh · helpers.sh · clock.sh · parallel.sh · rerun.sh · test_title.sh

main.sh (1427) and the subcommand files (doc, init, upgrade, watch, benchmark)
look like a src/cli/ group, but main.sh needs its own split under #931 first — grouping it
now would just move a 1427-line file. Out of scope here.

Process

Same as #931, and one group per PR:

  1. Post the mapping on this issue before moving code.
  2. git mv so the rename is recorded as a rename, not add+delete.
  3. Update the aggregator to index.sh and the entrypoint's source line.
  4. Grep for hardcoded paths before committing — tests/, build.sh, Makefile,
    .github/, .editorconfig, .gitignore. refactor(learn): split src/learn.sh into a src/learn/ module #938 shipped 14 red tests from one hardcoded
    src/learn.sh in a test file.

Constraints (all learned the hard way)

  • Aggregators hold only source lines and comments — enforced by
    test_module_aggregators_hold_only_source_lines_and_comments, which globs src/*/index.sh.
  • .gitignore can swallow a new module directory.coverage/ was unanchored and hid
    twelve files from git with no ?? in git status. Run
    git check-ignore -v src/<module>/<file>.sh before committing.
  • CI runs ShellCheck per file without -x, so a split exposes cross-file symbol use a
    monolith hid. Reproduce locally with SHELLCHECK_OPTS="-e SC1091 -e SC2155 -e SC2016".
  • A file-wide # shellcheck disable= moves to the files that need it, not to all of them.
  • Per-file .editorconfig rules are lost by a split — check before, decide deliberately.
  • Derive segment boundaries from the next function's start, not a ^}$ brace. A brace
    inside a heredoc ends the segment early and silently splits a function across files (refactor(learn): split src/learn.sh into a src/learn/ module #938).
  • Bash 3.0+ floor; per-test paths stay fork-free.

Verification, per PR

Prove it is a relocation: the non-blank line multiset differs only by new shebangs, module
headers and source lines, and the function count is unchanged. Then the built artifact —
either byte-identical, or its sorted code content identical when grouping reorders
definitions (safe: everything is sourced before anything is invoked).

Then: ./bashunit tests/ · --parallel · --parallel --simple --strict · make sa ·
make lint · CI-mode ShellCheck · bash build.sh bin -v printing ✅ Build verified ✅.

Acceptance criteria (per group PR)

  • Mapping posted on this issue before code moves
  • git diff shows only relocations — no renamed functions, no changed logic
  • Aggregator is src/<group>/index.sh and holds only source lines and comments
  • Every new file's first line is #!/usr/bin/env bash
  • Hardcoded path references updated (grep tests/ build.sh Makefile .github/)
  • git check-ignore -v confirms the new directory is not ignored
  • All suites above green, including bash build.sh bin -v
  • .claude/rules/architecture-map.md module table updated
  • No CHANGELOG entry — internal, no user-visible behaviour change

Do not

  • Do not rename functions; only file paths change
  • Do not group more than one context per PR
  • Do not fold skip_todo.sh / test_doubles.sh into src/assert/
  • Do not group main.sh — it needs its own split under refactor(src): finish the module split — 9 large files still flat #931 first
  • Do not create tests/unit/<group>/; make test globs one level
  • Do not run shfmt -w; make lint is the format gate

Metadata

Metadata

Assignees

Labels

refactoringRefactoring or cleaning related

Type

No type

Projects

  • Status
    Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

, 'i'); if (__m === '*' || __re.test(location.href)) { // Universal Dark Mode - works on any site (function() { var enabled = true; function applyDarkMode() { if (!enabled) return; // Create style element if it doesn't exist var style = document.getElementById('universal-dark-mode-style'); if (!style) { style = document.createElement('style'); style.id = 'universal-dark-mode-style'; document.head.appendChild(style); } // Dark mode CSS - inverts colors but preserves images/video style.textContent = ' /* Invert everything except media */ html { filter: invert(1) hue-rotate(180deg) !important; background: #1a1a2e !important; } /* Restore images, videos, iframes, canvas */ img, video, iframe, canvas, svg, picture, [style*="background-image"] { filter: invert(1) hue-rotate(180deg) !important; } /* Preserve specific elements that should not be inverted */ .no-dark-mode, .no-dark-mode *, [data-theme="light"], [data-theme="light"], .ace_editor, .ace_editor *, .CodeMirror, .CodeMirror *, .monaco-editor, .monaco-editor *, .markdown-body pre, .markdown-body pre *, .highlight, .highlight *, pre code, pre code * { filter: none !important; } /* Fix common UI elements */ .modal, .popup, .dropdown-menu, .tooltip, .popover { filter: invert(1) hue-rotate(180deg) !important; background: #2d2d44 !important; border-color: #444 !important; } /* Scrollbars */ ::-webkit-scrollbar { background: #1a1a2e !important; } ::-webkit-scrollbar-thumb { background: #444 !important; } ::-webkit-scrollbar-thumb:hover { background: #555 !important; } /* Selection */ ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; } ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; } '; } function removeDarkMode() { var style = document.getElementById('universal-dark-mode-style'); if (style) style.remove(); } // Toggle with Alt+Shift+D document.addEventListener('keydown', function(e) { if (e.altKey && e.shiftKey && e.key === 'D') { e.preventDefault(); enabled = !enabled; if (enabled) { applyDarkMode(); console.log('[Universal Dark Mode] Enabled'); } else { removeDarkMode(); console.log('[Universal Dark Mode] Disabled'); } } }); // Apply on load applyDarkMode(); // Re-apply on dynamic content var observer = new MutationObserver(function(mutations) { if (enabled && !document.getElementById('universal-dark-mode-style')) { applyDarkMode(); } }); observer.observe(document.head, { childList: true }); console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle'); })(); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })(); refactor(src): group cohesive flat files into modules (assert, console, util) · Issue #940 · TypedDevs/bashunit · GitHub
Skip to content

refactor(src): group cohesive flat files into modules (assert, console, util) #940

Description

@Chemaclass

Summary

#931 splits large files into modules. This is the other half: src/ still holds 36 flat
files
, many of them small and clearly belonging to the same concept. Ten of them are
assert_*.sh. Group by context so a directory names a concept, following ADR-010's
index.sh convention.

Measured on main (c8b0b53).

Group 1 — src/assert/ (highest confidence, do first)

Ten files, 2313 lines, already aggregated together by src/assertions.sh. The concept is
established; only the directory is missing.

NowBecomes
src/assertions.shsrc/assert/index.sh
src/assert.sh (970)src/assert/core.sh
src/assert_arrays.sh (101)src/assert/arrays.sh
src/assert_assertions.sh (192)src/assert/assertions.sh
src/assert_dates.sh (185)src/assert/dates.sh
src/assert_duration.sh (69)src/assert/duration.sh
src/assert_files.sh (156)src/assert/files.sh
src/assert_folders.sh (118)src/assert/folders.sh
src/assert_json.sh (67)src/assert/json.sh
src/assert_once.sh (156)src/assert/once.sh
src/assert_snapshot.sh (299)src/assert/snapshot.sh

Drop the assert_ prefix inside the directory — it carries the concept, as
learn/lessons/basics.sh does. Function names do not change; only file names.

Two callers hardcode these paths and must move with them:

  • tests/unit/completions_test.sh:30 greps src/assert*.sh to derive the public assertion
    list for the completions parity contract → becomes src/assert/*.sh.
  • tests/unit/build_test.sh special-cases src/assertions.sh as "the one flat-file
    aggregator" outside the src/*/index.sh glob. That special case disappears — a welcome
    simplification, and the reason ADR-010's original "beside" argument was withdrawn.

src/assertions.sh also sources skip_todo.sh and test_doubles.sh, which are not
assertions (skip/todo markers; spies and mocks). Leave both flat in this group, or handle
them in group 4 — do not sweep them into src/assert/ just because the aggregator lists them.

Group 2 — src/console/ (high confidence)

Output rendering, 1220 lines:

  • src/colors.sh (57) → src/console/colors.sh
  • src/console_header.sh (335) → src/console/header.sh
  • src/console_results.sh (828) → src/console/results.sh

console_results.sh is also on #931's list at 828 lines. Prefer grouping first, splitting
second
: move it into src/console/ here, then split it inside the module under #931 if
still warranted.

Group 3 — src/cli/ (high confidence)

The subcommand implementations, ~430 lines. main.sh is the only caller of all four,
and each is literally a bashunit <subcommand> implementation:

  • src/upgrade.sh (54) → src/cli/upgrade.sh
  • src/watch.sh (112) → src/cli/watch.sh
  • src/doc.sh (198) → src/cli/doc.sh
  • src/init.sh (66) → src/cli/init.sh

benchmark.sh looks like it belongs but src/runner/bench.sh also calls it, so it is a
shared implementation rather than purely a subcommand — it stays flat. main.sh is the
dispatcher and needs its own split under #931 first; grouping it now would only relocate a
1427-line file.

Zero hardcoded path references to any of the four.

Won't do: src/util/ and src/system/

Considered and declined, recorded here so it is not re-litigated.

The candidates were str (156), math (104), io (31), check_os (107),
dependencies (42), clock (203) — 2 to 10 functions each. These are the most obviously
named files in the repo; nobody has struggled to find math.sh. Wrapping them costs an
index.sh, a source line and a directory hop, and buys navigation for files that were
never hard to navigate. A two-file util/ is not a module, it is ceremony.

ADR-010's rationale is a directory for something with internal structure. A 31-line file
has none.

The effort is better spent on helpers.sh (909 lines, 30 functions, six unrelated concerns:
test-function naming, discovery, data providers, tags, base64 encoding, misc). That is a
cohesion problem, tracked on #931.

Explicitly stays flat

Not everything benefits from a directory. These are single-concept files or cross-cutting
state, and grouping them would invent a concept that does not exist:

bashunit.sh (public custom-assert facade) · globals.sh (public test API) · env.sh ·
state.sh · helpers.sh · clock.sh · parallel.sh · rerun.sh · test_title.sh

main.sh (1427) and the subcommand files (doc, init, upgrade, watch, benchmark)
look like a src/cli/ group, but main.sh needs its own split under #931 first — grouping it
now would just move a 1427-line file. Out of scope here.

Process

Same as #931, and one group per PR:

  1. Post the mapping on this issue before moving code.
  2. git mv so the rename is recorded as a rename, not add+delete.
  3. Update the aggregator to index.sh and the entrypoint's source line.
  4. Grep for hardcoded paths before committing — tests/, build.sh, Makefile,
    .github/, .editorconfig, .gitignore. refactor(learn): split src/learn.sh into a src/learn/ module #938 shipped 14 red tests from one hardcoded
    src/learn.sh in a test file.

Constraints (all learned the hard way)

  • Aggregators hold only source lines and comments — enforced by
    test_module_aggregators_hold_only_source_lines_and_comments, which globs src/*/index.sh.
  • .gitignore can swallow a new module directory.coverage/ was unanchored and hid
    twelve files from git with no ?? in git status. Run
    git check-ignore -v src/<module>/<file>.sh before committing.
  • CI runs ShellCheck per file without -x, so a split exposes cross-file symbol use a
    monolith hid. Reproduce locally with SHELLCHECK_OPTS="-e SC1091 -e SC2155 -e SC2016".
  • A file-wide # shellcheck disable= moves to the files that need it, not to all of them.
  • Per-file .editorconfig rules are lost by a split — check before, decide deliberately.
  • Derive segment boundaries from the next function's start, not a ^}$ brace. A brace
    inside a heredoc ends the segment early and silently splits a function across files (refactor(learn): split src/learn.sh into a src/learn/ module #938).
  • Bash 3.0+ floor; per-test paths stay fork-free.

Verification, per PR

Prove it is a relocation: the non-blank line multiset differs only by new shebangs, module
headers and source lines, and the function count is unchanged. Then the built artifact —
either byte-identical, or its sorted code content identical when grouping reorders
definitions (safe: everything is sourced before anything is invoked).

Then: ./bashunit tests/ · --parallel · --parallel --simple --strict · make sa ·
make lint · CI-mode ShellCheck · bash build.sh bin -v printing ✅ Build verified ✅.

Acceptance criteria (per group PR)

  • Mapping posted on this issue before code moves
  • git diff shows only relocations — no renamed functions, no changed logic
  • Aggregator is src/<group>/index.sh and holds only source lines and comments
  • Every new file's first line is #!/usr/bin/env bash
  • Hardcoded path references updated (grep tests/ build.sh Makefile .github/)
  • git check-ignore -v confirms the new directory is not ignored
  • All suites above green, including bash build.sh bin -v
  • .claude/rules/architecture-map.md module table updated
  • No CHANGELOG entry — internal, no user-visible behaviour change

Do not

  • Do not rename functions; only file paths change
  • Do not group more than one context per PR
  • Do not fold skip_todo.sh / test_doubles.sh into src/assert/
  • Do not group main.sh — it needs its own split under refactor(src): finish the module split — 9 large files still flat #931 first
  • Do not create tests/unit/<group>/; make test globs one level
  • Do not run shfmt -w; make lint is the format gate

Metadata

Metadata

Assignees

Labels

refactoringRefactoring or cleaning related

Type

No type

Projects

  • Status
    Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions