fix: Improve path type detection heuristics in Downloader and PathHelpers#1688
Conversation
…pers Addresses #1577 and #1584 by improving the reliability of path type detection: - Add check for trailing directory separator before falling back to Path.HasExtension() heuristic - paths ending with "/" or "\" are now reliably detected as directories - Add comprehensive XML documentation explaining the detection logic, order of precedence, and limitations - Document that directory names with dots (e.g., "my.folder") may still be misidentified as files when using the extension heuristic - Add helper method EndsWithDirectorySeparator() for consistent directory separator checking The extension heuristic remains as a fallback since it handles the common case correctly, but users can now explicitly indicate directory intent by appending a directory separator. Fixes #1577 Fixes #1584 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
SummaryImproves path type detection by checking for trailing directory separators before using the extension heuristic, preventing misidentification of directories with dots (e.g., "my.folder"). Critical Issues1. Code Duplication - DRY Violation The // src/ModularPipelines/Context/Downloader.cs:122-131
private static bool EndsWithDirectorySeparator(string path)
{
if (string.IsNullOrEmpty(path))
{
return false;
}
var lastChar = path[path.Length - 1];
return lastChar == Path.DirectorySeparatorChar || lastChar == Path.AltDirectorySeparatorChar;
}
// src/ModularPipelines/Helpers/PathHelpers.cs:68-77 (identical)
private static bool EndsWithDirectorySeparator(string path) { ... }Recommendation: Extract this method to // In PathHelpers.cs
internal static bool EndsWithDirectorySeparator(string path)
{
if (string.IsNullOrEmpty(path))
{
return false;
}
var lastChar = path[path.Length - 1];
return lastChar == Path.DirectorySeparatorChar || lastChar == Path.AltDirectorySeparatorChar;
}
// In Downloader.cs - remove duplicate and use:
if (PathHelpers.EndsWithDirectorySeparator(options.SavePath))
{
// ...
}2. Missing Test Coverage The PR adds new behavior (handling trailing directory separators) but doesn't add tests to verify it works correctly. Consider adding tests like: [Test]
public async Task Directory_Path_Type_With_Trailing_Separator()
{
var path = Path.Combine(TestContext.WorkingDirectory, "my.folder") + Path.DirectorySeparatorChar;
await Assert.That(path.GetPathType()).IsEqualTo(PathType.Directory);
}
[Test]
public async Task Directory_Path_Type_With_Dots_But_No_Separator()
{
// This should still work as documented - directories without trailing separator
// and with dots will be detected based on extension heuristic
var path = Path.Combine(TestContext.WorkingDirectory, "my.folder");
// Depends on intended behavior
}Similarly, add tests for the SuggestionsThe documentation is excellent - clear explanation of the heuristics and their limitations. The approach is sound, just needs to eliminate the duplication. Verdict |
- Make EndsWithDirectorySeparator internal in PathHelpers - Remove duplicate method from Downloader.cs, now uses PathHelpers - Add comprehensive tests for trailing separator behavior 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Addressed FeedbackThanks for the detailed review! I've addressed all the issues:
All 12 PathHelpersTests now pass. |
SummaryImproves path type detection by checking for trailing directory separators before falling back to the extension heuristic, fixing issues with directory paths ending in '/' or ''. Critical IssuesNone found ✅ Suggestions1. Consider cross-platform edge case in EndsWithDirectorySeparatorLocation: src/ModularPipelines/Helpers/PathHelpers.cs:72 The current implementation checks both This is a minor point and doesn't affect the correctness of the fix. 2. Test coverage for null handlingLocation: test/ModularPipelines.UnitTests/PathHelpersTests.cs:92 The test Current behavior (returning false for null) is reasonable but implicit. Verdict✅ APPROVE - No critical issues Review Notes:
|
Summary
Path.HasExtension()heuristic/or\are now reliably detected as directoriesFixes #1577, #1584
Test plan
🤖 Generated with Claude Code