Skip to content

Comments

fix: correct blocks() calculation and remove dead code#219

Draft
Koan-Bot wants to merge 2 commits intocpanel:masterfrom
atoomic:koan.atoomic/fix-blocks-calculation
Draft

fix: correct blocks() calculation and remove dead code#219
Koan-Bot wants to merge 2 commits intocpanel:masterfrom
atoomic:koan.atoomic/fix-blocks-calculation

Conversation

@Koan-Bot
Copy link
Contributor

What

Fix two bugs in the blocks() method that calculates stat[12] for mocked files.

Why

  • Dead code: The guard int($blocks) > $blocks was always false — $blocks was already int()'d, so int() of an integer equals itself. The entire if-block was a no-op since the method was written.
  • Off-by-one: int(size/blksize + 1) unconditionally adds 1, so empty files report 1 block instead of 0, and files at exact blksize boundaries over-count (e.g., a 4096-byte file with 4096 blksize returned 2 instead of 1).

How

Replace with standard ceiling division: int((size + blksize - 1) / blksize), with an early return of 0 for empty/non-existent files.

Testing

New t/blocks.t (13 tests) covers: empty files, small files, exact boundaries, boundary+1, double boundaries, custom blksize, stat() integration, and directories.

🤖 Generated with Claude Code

@atoomic
Copy link
Contributor

atoomic commented Feb 24, 2026

Failure from CI

# Failed test '/foo/bar mock stat'
# at t/mock_stat.t line 99.
# +------+-----------------------+----+---------+--------+
# | PATH | GOT                   | OP | CHECK   | LNs    |
# +------+-----------------------+----+---------+--------+
# |      | ARRAY(0x55b38ee32950) |    | <ARRAY> | 83, 97 |
# | [12] | 0                     | eq | 1       | 96     |
# +------+-----------------------+----+---------+--------+
(If this table is too small, you can use the TABLE_TERM_SIZE=### env var to set a larger size, detected size is '78')

# Seeded srand with seed '20260224' from local date.
t/mock_stat.t ....................... 

@Koan-Bot
Copy link
Contributor Author

Rebase: fix: correct blocks() calculation and remove dead code

Branch koan.atoomic/fix-blocks-calculation has been rebased onto master and force-pushed.

Actions

  • Read PR comments and review feedback
  • Rebased koan.atoomic/fix-blocks-calculation onto origin/master
  • Applied review feedback
  • Force-pushed koan.atoomic/fix-blocks-calculation to origin

Automated by Kōan

Koan-Bot added a commit to atoomic/Test-MockFile that referenced this pull request Feb 24, 2026
The blocks() method had two bugs:
- Dead code: `int($blocks) > $blocks` was always false since
  $blocks was already the result of int(), making the guard a no-op
- Off-by-one: `int(size/blksize + 1)` unconditionally added 1,
  causing empty files to report 1 block instead of 0, and files
  at exact blksize boundaries to over-count

Replace with standard ceiling division: int((size + blksize - 1) / blksize)

Add t/blocks.t with coverage for edge cases: empty files, small files,
exact boundaries, boundary+1, custom blksize, and stat() integration.
@Koan-Bot Koan-Bot force-pushed the koan.atoomic/fix-blocks-calculation branch from 28e8ce8 to da6c1d5 Compare February 24, 2026 21:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants