Skip to content

Replace panic with user-friendly error for empty test contents - #172

Merged
philocalyst merged 3 commits into
mainfrom
unknown repository
Mar 24, 2026
Merged

Replace panic with user-friendly error for empty test contents#172
philocalyst merged 3 commits into
mainfrom
unknown repository

Conversation

@ghost

@ghost ghost commented Mar 21, 2026

Copy link
Copy Markdown

Fixes #78

When you pass ttyper an empty file (or a language file with no words), it panics with Empty test contents. which isn't great for users. This replaces that with a clean error message on stderr explaining what went wrong.

Also handles the case where you restart with r from the results screen and gen_contents() returns an empty vec. Previously this could trigger an index-out-of-bounds panic in the render code. Now it just stays on the results screen.

Changes

  • src/main.rs: Replace panic! with eprintln! + process::exit(1) for empty contents on startup
  • src/main.rs: Guard restart path against empty contents

Testing

Tested with echo -n "" > /tmp/empty.txt && cargo run -- /tmp/empty.txt (prints error, exits cleanly instead of panicking).

When a zero-length file is provided (or a language file with no words),
ttyper previously panicked with 'Empty test contents.' Now it prints a
helpful error message to stderr and exits cleanly.

Also guards against the same issue when restarting with 'r' from the
results screen, which could panic if gen_contents returned an empty
vec (the restart now just stays on the results screen instead).

Fixes #78
@philocalyst philocalyst self-assigned this Mar 23, 2026

@philocalyst philocalyst left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@philocalyst

Copy link
Copy Markdown
Collaborator

Could you please add a test before I close this? Logic is good. @ctonneslan

- gen_contents_empty_file_returns_empty_vec: verifies empty file
  produces empty vec (which triggers the clean error message)
- gen_contents_nonempty_file_returns_words: verifies normal file
  produces non-empty contents

Added tempfile as a dev dependency for test temp dirs.
@ghost

ghost commented Mar 24, 2026

Copy link
Copy Markdown
Author

Added two tests:

  • gen_contents_empty_file_returns_empty_vec: confirms empty file produces empty vec (triggers the error path)
  • gen_contents_nonempty_file_returns_words: confirms normal file produces words

All 7 tests pass. Added tempfile as a dev dependency for temp dirs.

@philocalyst
philocalyst merged commit e218bad into max-niederman:main Mar 24, 2026
3 checks passed
@philocalyst

Copy link
Copy Markdown
Collaborator

Thank you ! @ctonneslan

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add nice error message for zero-length files.

2 participants