Nettacker icon indicating copy to clipboard operation
Nettacker copied to clipboard

Fix: Replaced bare open().read() with context managers in lib modules

Open Tech-Psycho95 opened this issue 8 months ago โ€ข 1 comments

Problem

Several files under nettacker/lib/ used bare open().read() without context managers. This pattern leaves file handles open until garbage collection, risking resource leaks - particularly under sustained or repeated use.

Changes Made

Files Changes
nettacker/lib/html_log/log_data.py Added a private _read_file() helper that wraps reads in with open(...) as f:. All 5 module-level reads now use it.
nettacker/lib/graph/d3_tree_v1/engine.py Replaced inline open().read() in start() with a context manager.
nettacker/lib/compare_report/engine.py Replaced inline open().read() in build_report() with a context manager.

Scope: Changes are limited to nettacker/lib/ modules only. No files in nettacker/core/ or nettacker/api/ were modified.


Tests Added

18 new pytest unit tests under tests/lib/:

Test File Tests Coverage
tests/lib/test_html_log.py 5 _read_file() - correct reads, handle closure, empty files, missing files, multiline content
tests/lib/test_d3_tree_v1.py 8 escape_for_html_js() - escaping logic; start() โ€” template substitution, empty events, handle closure, missing template
tests/lib/test_compare_report.py 5 build_report() - data insertion, empty dict, handle closure, missing template, empty template

All 18 tests passed locally:

Checklist

  • [x] I've followed the contributing guidelines

  • [x] I have digitally signed all my commits in this PR

  • [x] I've run make pre-commit and confirm it didn't generate any warnings/changes

  • [x] I've run make test, I confirm all tests passed locally

  • [x] I've linked this PR with an open issue

  • [x] I've tested and verified that my code works as intended and resolves the issue as described

  • [x] I confirm that the code and comments in this PR are not direct unreviewed outputs of AI

  • [x] I confirm that I am the Sole Responsible Author for every line of code, comment, and design decision

Summary

This PR fixes unsafe file handling in lib modules by using context managers and adds 18 unit tests to ensure correctness and prevent regressions.

Closes Issue #1320

Tech-Psycho95 avatar Mar 10 '26 17:03 Tech-Psycho95

No actionable comments were generated in the recent review. ๐ŸŽ‰

โ„น๏ธ Recent review info
โš™๏ธ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 0a9dc1b5-54a1-4993-a5c6-f0e2a6b03144

๐Ÿ“ฅ Commits

Reviewing files that changed from the base of the PR and between c33dcc3fc9651827abddacc272df09d27e1a54e9 and 4d8dcf5d5cdaa587dbb473dc6e01b58dc77ae75f.

๐Ÿ“’ Files selected for processing (1)
  • tests/lib/test_html_log.py

Summary by CodeRabbit

Release Notes

  • Documentation

    • Enhanced module and function documentation for improved clarity
  • Refactor

    • Optimized file I/O operations and centralized asset loading patterns for improved resource efficiency and performance
  • Tests

    • Added comprehensive unit test coverage for report generation, visualization engine, and asset loading modules, including validation of error handling and resource management

Walkthrough

Refactors template and static-file reads to use context managers and a new helper, consolidates template loading into variables, expands a docstring, and adds unit tests for compare_report, d3_tree_v1, and html_log file-reading behavior and error cases.

Changes

Cohort / File(s) Summary
Compare Report I/O
nettacker/lib/compare_report/engine.py
Read the HTML template once using a with-open into template and apply template.replace(...) instead of chained open/read/replace; adds module docstring and improves variable naming and resource handling.
D3 Tree I/O & Escaping
nettacker/lib/graph/d3_tree_v1/engine.py
Read template once (with-context) and reuse for replacements; update escape_for_html_js docstring to list exact character replacements and clarify purpose.
HTML Log helper
nettacker/lib/html_log/log_data.py
Add _read_file(path) helper, introduce module-level docstring, and replace direct file reads for static assets with calls to _read_file, centralizing file loading and error behavior.
Tests โ€” compare report
tests/lib/test_compare_report.py
New tests for build_report() covering normal template substitution, empty input, empty template result, missing-template FileNotFoundError propagation, and file-handle closure via context manager.
Tests โ€” D3 tree
tests/lib/test_d3_tree_v1.py
New tests for escape_for_html_js() and start() covering escaping cases, template replacement logic, empty events, missing-template error, and file-handle closure using mocks.
Tests โ€” html_log helper
tests/lib/test_html_log.py
New tests for _read_file() validating returned contents, file-handle closure, empty-file behavior, FileNotFoundError on missing files, and multiline content preservation.

Estimated code review effort

๐ŸŽฏ 3 (Moderate) | โฑ๏ธ ~25 minutes

๐Ÿšฅ Pre-merge checks | โœ… 3
โœ… Passed checks (3 passed)
Check name Status Explanation
Title check โœ… Passed The PR title clearly and specifically describes the main change: replacing bare open().read() with context managers in lib modules.
Description check โœ… Passed The PR description is directly related to the changeset, providing a detailed problem statement, table of affected files, scope clarification, test coverage summary, and verification checklist.
Docstring Coverage โœ… Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

โœ๏ธ Tip: You can configure your own custom pre-merge checks in the settings.

โœจ Finishing Touches
๐Ÿงช Generate unit tests (beta)
  • [ ] Create PR with unit tests
  • [ ] Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

โค๏ธ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

coderabbitai[bot] avatar Mar 10 '26 17:03 coderabbitai[bot]