Skip to content

feat(fl/system): FileSystem::sd factory + loadFled sugar (#3311 PR3) - #3327

Merged
zackees merged 3 commits into
masterfrom
feat/file-system-unify-3311
Jun 20, 2026
Merged

zackees merged 3 commits into
masterfrom
feat/file-system-unify-3311

Conversation

@zackees

@zackees zackees commented Jun 20, 2026 •

Copy link
Copy Markdown
Member

Summary

PR3 of #3311. Modest-scope additions to fl::FileSystem:

  • static FileSystem FileSystem::sd(int cs_pin) — one-line factory equivalent to fs; fs.beginSd(cs_pin). Definition lives in file_system_sd.cpp.hpp so the existing SD-library tree-shake property of beginSd is preserved.
  • FileSystem::loadFled(const char* path) — one-line forwarder to fl::Fled::load(*this, path). The Fled subsystem stays independently tree-shakeable.

Together these let bundle-load code read as:

auto fs = fl::FileSystem::sd(CHIP_SELECT_PIN);
auto fled = fs.loadFled(\"sketch.fled\");

Out of scope

The original #3311 plan called for additional FileSystem::littleFs() / wasm() / host() static factories. Each requires a working backend (LittleFS lib on ESP32/RP2040, the WASM VFS, desktop stdio FS) which is a much larger lift than this PR's "sugar" framing. Filed for follow-up; sd(int) + loadFled cover the immediate ergonomic need for the Fled subsystem.

Test plan

  • bash test fled --debug — green (3/3 DLLs in fled subdir, including the new fled_filesystem)
  • bash lint --cpp — green
  • CI matrix

Refs #3311

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features
    • Added a convenience factory to initialize and start the SD card filesystem in one call.
    • Added a helper to load Fled content from a file path, returning a null result on open/malformed failures.
  • Bug Fixes
    • Improved resilience when the filesystem backend is unavailable by safely handling null backend access during read operations.
  • Tests
    • Added coverage for the SD initialization and Fled-loading helpers, including behavior when the file is missing (including version checks).

PR3 of #3311. Modest-scope addition to fl::FileSystem:

- static FileSystem FileSystem::sd(int cs_pin) - one-line factory that
  constructs + beginSd in a single expression. Definition lives in
  file_system_sd.cpp.hpp so the SD-library tree-shake property of
  beginSd is preserved: linkers prune sd(int) along with beginSd when
  no sketch references either.
- FileSystem::loadFled(const char* path) - one-line forwarder to
  Fled::load(*this, path). The Fled subsystem stays independently
  tree-shakeable: file_system.cpp.hpp references it only from inside
  loadFled, which the linker strips when unreferenced.

Out of scope (deferred): the full FileSystem::littleFs / wasm / host
static factory unification from the original #3311 plan. Those each
require a working backend (LittleFS lib on ESP32/RP2040, the WASM VFS,
desktop stdio FS) that is a much larger lift than this PR's "sugar"
framing. Filed for follow-up; sd(int) and loadFled cover the immediate
ergonomic need for the Fled subsystem.

Tests: 2 cases in tests/fl/fled/fled_filesystem.cpp - sd factory
round-trips, and loadFled on a missing path returns a null Fled.

Refs #3311 (this is the third of seven planned PRs)

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@coderabbitai

coderabbitai Bot commented Jun 20, 2026 •

Copy link
Copy Markdown

Review Change Stack

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: c8912477-9d06-459b-a0ef-1abde7e9940c

📥 Commits

Reviewing files that changed from the base of the PR and between f3b97a0 and 63c7cb3.

📒 Files selected for processing (1)
  • src/fl/system/file_system.cpp.hpp
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/fl/system/file_system.cpp.hpp

📝 Walkthrough

Walkthrough

Adds two convenience methods to fl::FileSystem: a static FileSystem::sd(int cs_pin) factory that constructs a FileSystem and calls beginSd, and FileSystem::loadFled(const char* path) that delegates to Fled::load(*this, path). Also hardens FileSystem::openRead with a null-backend check. A new test file validates both methods using the stub filesystem.

Changes

FileSystem Convenience API

Layer / File(s) Summary
Public API declarations and Fled forward declaration
src/fl/system/file_system.h
Adds a forward declaration for fl::Fled, a static FileSystem::sd(int cs_pin) factory declaration, and FileSystem::loadFled(const char* path) to the public FileSystem interface, with comments on failure-mode behavior.
sd factory and loadFled implementation
src/fl/system/sd/file_system_sd.cpp.hpp, src/fl/system/file_system.cpp.hpp
Implements FileSystem::sd(int cs_pin) by default-constructing a FileSystem, calling beginSd(cs_pin), and returning it; implements loadFled by including fl/fled/fled.h and delegating to Fled::load(*this, path).
Defensive null-check in openRead
src/fl/system/file_system.cpp.hpp
Updates FileSystem::openRead to check for a null mFs backend and return a default-constructed (closed) fl::ifstream() rather than unconditionally dereferencing the pointer.
Tests for sd and loadFled
tests/fl/fled/fled_filesystem.cpp
Asserts FileSystem::sd(5) produces a usable object and FileSystem::loadFled("does/not/exist.fled") returns a null Fled with version() equal to fl::u8(0).

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Possibly related PRs

  • FastLED/FastLED#2782: The new FileSystem::sd(int) factory directly calls FileSystem::beginSd(int), which was refactored into a separate SD translation unit in this PR.
  • FastLED/FastLED#3323: The new FileSystem::loadFled() delegates to Fled::load(*this, path), directly depending on the fl::Fled type and load implementation introduced in this PR.

Poem

🐇 Hop, hop, a shortcut born today,
sd() and loadFled() light the way!
No more boilerplate to write,
Just call one method, things work right.
When mFs is null, we guard with care —
A tidy burrow built with flair! 🌟

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 16.67% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main changes: adding a static FileSystem::sd factory and FileSystem::loadFled convenience method.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/file-system-unify-3311

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 coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/fl/system/file_system.cpp.hpp`:
- Line 1: The file is missing the required IWYU pragma directive at the top. Add
the line `// IWYU pragma: private` as the first line in the file, positioned
before the existing include statement `#include "fl/system/file_system.h"`. This
marks the `.cpp.hpp` file as an internal include-only router per coding
guidelines.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: c4800e4a-2f7c-48ac-9dd5-f8ce5bc4609f

📥 Commits

Reviewing files that changed from the base of the PR and between 5f58136 and 009c945.

📒 Files selected for processing (4)
  • src/fl/system/file_system.cpp.hpp
  • src/fl/system/file_system.h
  • src/fl/system/sd/file_system_sd.cpp.hpp
  • tests/fl/fled/fled_filesystem.cpp

Comment thread src/fl/system/file_system.cpp.hpp Resolved
zackees and others added 2 commits June 19, 2026 23:57
…atchet

The CI-side FL_NO_EXCEPT AST ratchet (clang-tool-chain-query backed)
caught the missing annotations on the new APIs added in PR3. The local
lint setup skips the AST ratchet when clang-tool-chain-query is not on
PATH, so the issue surfaced only after CI. Adding FL_NO_EXCEPT to:
- FileSystem::sd(int) declaration + definition
- FileSystem::loadFled(const char*) declaration + definition

Refs #3311 #3327

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
… PR3)

CI ASAN caught a null-deref in FileSystem::openRead when called on a
FileSystem with no backend (default-constructed or one whose begin*()
failed). My new tests for the PR3 sugar exercised the
default-constructed-FS path that nothing had hit before.

Returning a closed ifstream lets downstream callers branch on
is_open() / is_eof() rather than crash. This matches the behavior the
ifstream contract already implies (calling read on a closed stream
returns 0 bytes), so no caller behavior changes.

Refs #3311 #3327

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@zackees
zackees merged commit d06ab85 into master Jun 20, 2026
13 of 14 checks passed
@zackees
zackees deleted the feat/file-system-unify-3311 branch June 20, 2026 07:33
@fastled-project-sync fastled-project-sync Bot moved this from Triage to Done in FastLED Tracker Jun 20, 2026
zackees added a commit that referenced this pull request Jun 20, 2026
Replaces the stub-filesystem approach (which writes real bytes to a real
temp dir on the host disk and gates the whole file on FASTLED_TESTING)
with a true in-memory FsImpl mock. No disk I/O, no temp-dir teardown,
no platform gates - works uniformly anywhere the unit-test binary runs.

Two local helpers in the test TU:
- MemoryFilebuf : public fl::filebuf - seekable in-memory filebuf
  over an owned byte vector; supports read/seek/tell/size/eof.
- MemoryFs : public fl::FsImpl - holds flat_map<string, vector<u8>>
  of path -> bytes; openRead returns a fresh MemoryFilebuf per call
  (independent read cursor across reopens).

Test usage:
    auto mem = fl::make_shared<MemoryFs>();
    mem->add("sketch.fled", buildBundle(env, payload, ...));
    fl::FileSystem fs;
    fs.begin(mem);
    auto fled = fs.loadFled("sketch.fled");

Three cases as before (end-to-end round-trip with version/sectionCount/
json envelope/frame_payload byte checks, missing path, corrupt magic),
plus the FileSystem::sd factory sanity check.

The helpers live in the test TU only - if other fled-style tests want
the same mocking the natural follow-up is to extract them into a shared
test header.

Refs #3311 #3327

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
zackees added a commit that referenced this pull request Jun 20, 2026
…3311) (#3333)

* test(fl/fled): use stub fs to actually exercise FileSystem::loadFled (#3311)

The original tests/fl/fled/fled_filesystem.cpp shipped in #3327 just
verified the new entry points did not crash. They did not actually
exercise the load path because the test FileSystem had no backend
mounted. Rewrite to use the stub filesystem
(platforms/stub/fs_stub.hpp) the way tests/fl/system/file_system.cpp
already does:

- TestDirGuard RAII helper around StubFileSystem::createDirectory /
  forceRemoveDirectory so a failed run cannot poison the next run.
- buildBundle() helper assembles a real v1 .fled byte buffer in memory.
- dropBundle() writes the bundle bytes to the stub fs root via
  StubFileSystem::createTextFile (which opens with ios::binary | trunc
  and writes via ofs.write, so it is binary-clean).
- setTestFileSystemRoot points the stub backend at the temp dir.
- Three cases:
  - End-to-end: FileSystem::sd(5).loadFled("sketch.fled") parses the
    bundle - version, sectionCount, json envelope, frame_payload bytes
    all round-trip correctly.
  - Missing file: loadFled returns a null Fled.
  - Corrupt magic: loadFled returns a null Fled.

Whole file gated on FASTLED_TESTING so the stub headers are only
pulled into the test binary.

Refs #3311 #3327

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>

* test(fl/fled): switch to a true in-memory MemoryFs mock (#3311 #3327)

Replaces the stub-filesystem approach (which writes real bytes to a real
temp dir on the host disk and gates the whole file on FASTLED_TESTING)
with a true in-memory FsImpl mock. No disk I/O, no temp-dir teardown,
no platform gates - works uniformly anywhere the unit-test binary runs.

Two local helpers in the test TU:
- MemoryFilebuf : public fl::filebuf - seekable in-memory filebuf
  over an owned byte vector; supports read/seek/tell/size/eof.
- MemoryFs : public fl::FsImpl - holds flat_map<string, vector<u8>>
  of path -> bytes; openRead returns a fresh MemoryFilebuf per call
  (independent read cursor across reopens).

Test usage:
    auto mem = fl::make_shared<MemoryFs>();
    mem->add("sketch.fled", buildBundle(env, payload, ...));
    fl::FileSystem fs;
    fs.begin(mem);
    auto fled = fs.loadFled("sketch.fled");

Three cases as before (end-to-end round-trip with version/sectionCount/
json envelope/frame_payload byte checks, missing path, corrupt magic),
plus the FileSystem::sd factory sanity check.

The helpers live in the test TU only - if other fled-style tests want
the same mocking the natural follow-up is to extract them into a shared
test header.

Refs #3311 #3327

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>

---------

Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant