Skip to content

feat: implement EnsureDirectoryExists method to verify and create directories as needed#84

Merged
arika0093 merged 3 commits into
mainfrom
feat/create-ensure-dir
May 24, 2026
Merged

feat: implement EnsureDirectoryExists method to verify and create directories as needed#84
arika0093 merged 3 commits into
mainfrom
feat/create-ensure-dir

Conversation

@arika0093
Copy link
Copy Markdown
Owner

@arika0093 arika0093 commented May 24, 2026

Summary by CodeRabbit

  • New Features

    • Improved save-location behavior: target directories are auto-created when missing, verified for write access, and operations fail fast with a clear error if a directory cannot be created or written to.
    • Directory verification now applies consistently across storage providers (including archive-backed and in-memory variants).
  • Tests

    • Added comprehensive tests covering directory creation and write-access validation for multiple providers and save scenarios.

Review Change Stack

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 24, 2026

Warning

Review limit reached

@arika0093, we couldn't start this review because you've used your available PR reviews for now.

Your plan currently allows 1 review/hour. Refill in 50 minutes and 50 seconds.

Your organization has run out of usage credits. Purchase more in the billing tab.

⌛ How to resolve this issue?

After more review capacity refills, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than trial, open-source, and free plans. In all cases, review capacity refills continuously over time.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 17579eca-99c4-4cf2-8705-b709613f1b13

📥 Commits

Reviewing files that changed from the base of the PR and between 79805e4 and 61d72db.

📒 Files selected for processing (2)
  • src/Configuration.Writable.Core/Configure/SaveLocationManager.cs
  • tests/Configuration.Writable.Tests/CommonFileWriterTests.cs
📝 Walkthrough

Walkthrough

Adds EnsureDirectoryExists to IFileProvider and implements it in CommonFileProvider and ZipFileProvider. SaveLocationManager now treats directories without explicit paths as writable when appropriate and calls EnsureDirectoryExists to create/verify the target directory before finalizing the save path. Tests added for providers and builder flows.

Changes

Directory Existence and Writeability

Layer / File(s) Summary
Interface contract and file provider implementations
src/Configuration.Writable.Core/FileProvider/IFileProvider.cs, src/Configuration.Writable.Core/FileProvider/CommonFileProvider.cs, src/Configuration.Writable.Core/FileProvider/ZipFileProvider.cs, tests/Configuration.Writable.Tests/Utility/InMemoryFileProvider.cs, tests/Configuration.Writable.Tests.PublicApi/Approvals/PublicApiCheck.Check.Configuration.Writable.Core.approved.txt
IFileProvider adds EnsureDirectoryExists(string path). CommonFileProvider and ZipFileProvider implement it by creating the target directory when missing and validating writability by creating a temporary file. InMemoryFileProvider test utility returns success. Public API approval updated.
SaveLocationManager directory creation integration
src/Configuration.Writable.Core/Configure/SaveLocationManager.cs
Selection logic now treats paths with no explicit directory component as writable when the current directory is writable. After choosing a target path, Build(...) calls IFileProvider.EnsureDirectoryExists(targetPath.Path) and throws InvalidOperationException if the directory cannot be created or written to. Inline comment for rule #3 updated to "writable (or created if it doesn’t exist)".
CommonFileProvider directory tests
tests/Configuration.Writable.Tests/CommonFileWriterTests.cs
Unit tests for EnsureDirectoryExists(...): creating missing directories, returning true for existing directories, returning false for invalid paths, creating nested directories; tests also cover CanWriteToDirectory(...) before and after directory creation.
ZipFileProvider and builder tests
tests/Configuration.Writable.Tests/ZipFileProviderTests.cs, tests/Configuration.Writable.Tests/WritableOptionsConfigBuilder.cs
ZipFileProvider tests validate directory creation and existing-directory behavior. WritableOptionsConfigBuilderTests verify UseCustomDirectory and UseStandardSaveDirectory cause the expected directories to be created and ConfigFilePath to resolve to the expected file under those directories.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~22 minutes

Possibly related PRs

  • arika0093/Configuration.Writable#25: Both PRs modify SaveLocationManager.Build(...) to change save-destination selection logic using writable and directory-existence checks combined with explicit path priority rules.

Poem

🐰 I nibble at paths with a curious cheer,
A missing folder I happily rear,
I test with a temp file to see if it's kind,
Now configs find homes and peace of mind,
Hooray — directories created, safe, and clear!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 40.91% 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 directly and accurately describes the main feature being implemented across the changeset: adding an EnsureDirectoryExists method to verify and create directories.
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 unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/create-ensure-dir

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.

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 0778cd8fec

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +82 to +83
CanWriteDir = fileProvider.CanWriteToDirectory(p.Path)
|| !fileProvider.DirectoryExists(Path.GetDirectoryName(p.Path) ?? ""),
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Skip non-creatable directories when ranking save paths

Treating every non-existent directory as writable (CanWriteDir = ... || !DirectoryExists(...)) can select an unusable path ahead of a valid fallback at the same priority (the default case), because selection now prefers registration order among those true values and then immediately throws if EnsureDirectoryExists fails. In practice, if the first configured path points to a directory that does not exist and cannot be created (permissions/invalid parent) while a later path is writable, Build fails instead of choosing the later writable location, which is a regression from the previous ranking behavior.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 3

🤖 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/Configuration.Writable.Core/Configure/SaveLocationManager.cs`:
- Around line 80-84: The current CanWriteDir logic in SaveLocationManager marks
non-existent paths as writable which can cause Build(...) to try creating an
uncreatable path and fail instead of trying lower-priority locations; change the
condition so CanWriteDir is true only when the directory already exists and
fileProvider.CanWriteToDirectory(p.Path) returns true (i.e., require
fileProvider.DirectoryExists(dir) && fileProvider.CanWriteToDirectory(p.Path)),
so non-existent directories are not pre-marked writable and the selection can
fall back to other configured locations.

In `@src/Configuration.Writable.Core/FileProvider/CommonFileProvider.cs`:
- Around line 278-282: The current logic in CommonFileProvider where directory
is obtained via Path.GetDirectoryName(path) incorrectly treats null/empty as
invalid (causing valid relative filenames like "file.json" to fail); update the
method (the block using Path.GetDirectoryName(path) and the subsequent
write-check) to default directory to the current directory when GetDirectoryName
returns null or empty (e.g., set directory = "." or
Directory.GetCurrentDirectory()) instead of returning false so the existing
write-permission/check logic proceeds for files in the current directory.

In `@src/Configuration.Writable.Core/FileProvider/ZipFileProvider.cs`:
- Around line 108-112: The code incorrectly treats relative targets as
unwritable when Path.GetDirectoryName(zipPath) returns null/empty; instead
normalize the path and treat the current directory as the fallback: compute var
full = Path.GetFullPath(zipPath) and then var directory =
Path.GetDirectoryName(full); if directory is null or empty set directory =
Directory.GetCurrentDirectory(); then continue the existing writability checks
using that directory (references: zipPath, directory, use Path.GetFullPath and
Directory.GetCurrentDirectory in ZipFileProvider.cs).
🪄 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: defaults

Review profile: CHILL

Plan: Pro

Run ID: 85ccf6f1-154a-418a-bd84-e1a39b5b4ebc

📥 Commits

Reviewing files that changed from the base of the PR and between a807f50 and 0778cd8.

📒 Files selected for processing (9)
  • src/Configuration.Writable.Core/Configure/SaveLocationManager.cs
  • src/Configuration.Writable.Core/FileProvider/CommonFileProvider.cs
  • src/Configuration.Writable.Core/FileProvider/IFileProvider.cs
  • src/Configuration.Writable.Core/FileProvider/ZipFileProvider.cs
  • tests/Configuration.Writable.Tests.PublicApi/Approvals/PublicApiCheck.Check.Configuration.Writable.Core.approved.txt
  • tests/Configuration.Writable.Tests/CommonFileWriterTests.cs
  • tests/Configuration.Writable.Tests/Utility/InMemoryFileProvider.cs
  • tests/Configuration.Writable.Tests/WritableOptionsConfigBuilder.cs
  • tests/Configuration.Writable.Tests/ZipFileProviderTests.cs

Comment thread src/Configuration.Writable.Core/Configure/SaveLocationManager.cs Outdated
Comment thread src/Configuration.Writable.Core/FileProvider/CommonFileProvider.cs Outdated
Comment thread src/Configuration.Writable.Core/FileProvider/ZipFileProvider.cs Outdated
- SaveLocationManager: Only consider existing directories as writable in
  CanWriteDir evaluation, so Build() falls back to other locations if
  EnsureDirectoryExists fails for the selected path

- CommonFileProvider / ZipFileProvider: Default to current directory when
  GetDirectoryName returns null/empty (e.g. relative filenames like
  'file.json'), instead of returning false

- Test: EnsureDirectoryExists_ShouldReturnFalseForInvalidPath now uses
  a UNC path guaranteed to be inaccessible rather than a relative path

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 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/Configuration.Writable.Core/Configure/SaveLocationManager.cs`:
- Around line 80-86: The current check sets CanWriteDir using
fileProvider.CanWriteToDirectory(".") which can false-negative; instead, when
p.Path has no directory part (filename-only), call
fileProvider.CanWriteToDirectory with a file-target probe (e.g., the filename
portion of p.Path or a generated probe filename) so the provider receives a
file-like path and can infer writability correctly; update the CanWriteDir
expression in SaveLocationManager (the block that computes CanWriteDir for
p.Path) to use Path.GetFileName(p.Path) or a probe filename rather than "." when
Path.GetDirectoryName(p.Path) is empty.

In `@tests/Configuration.Writable.Tests/CommonFileWriterTests.cs`:
- Around line 315-317: Replace the non-deterministic UNC path used in the test
with a platform-agnostic, immediately-invalid path so Directory.CreateDirectory
fails deterministically; specifically, change the invalidPath variable in
CommonFileWriterTests (the value passed into writer.EnsureDirectoryExists) to a
string containing an invalid path character (e.g., a NUL character or use
Path.GetInvalidPathChars to build an invalid name) and keep the
Assert/expectation the same so the test fails fast on all CI platforms.
🪄 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: defaults

Review profile: CHILL

Plan: Pro

Run ID: 7641e1d6-dc70-4cb6-a225-416aba229e57

📥 Commits

Reviewing files that changed from the base of the PR and between 0778cd8 and 79805e4.

📒 Files selected for processing (4)
  • src/Configuration.Writable.Core/Configure/SaveLocationManager.cs
  • src/Configuration.Writable.Core/FileProvider/CommonFileProvider.cs
  • src/Configuration.Writable.Core/FileProvider/ZipFileProvider.cs
  • tests/Configuration.Writable.Tests/CommonFileWriterTests.cs

Comment thread src/Configuration.Writable.Core/Configure/SaveLocationManager.cs Outdated
Comment thread tests/Configuration.Writable.Tests/CommonFileWriterTests.cs Outdated
@sonarqubecloud
Copy link
Copy Markdown

@arika0093 arika0093 merged commit a0377c3 into main May 24, 2026
5 checks passed
@arika0093 arika0093 deleted the feat/create-ensure-dir branch May 24, 2026 12:28
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.

1 participant