Skip to content

Make baseName consistent with C++17 filesystem::path::stem#281

Open
Ozaq wants to merge 1 commit intodevelopfrom
fix/ECKIT-415
Open

Make baseName consistent with C++17 filesystem::path::stem#281
Ozaq wants to merge 1 commit intodevelopfrom
fix/ECKIT-415

Conversation

@Ozaq
Copy link
Member

@Ozaq Ozaq commented Feb 26, 2026

Description

baseName(false) stripped from the first dot instead of the last, producing incorrect results for multi-extension filenames such as archive.tar.gz (yielded "archive" instead of "archive.tar"). This is inconsistent with C++17 std::filesystem::path::stem and with eckit's own extension() method which already uses find_last_of.

Fixes: ECKIT-415

Contributor Declaration

By opening this pull request, I affirm the following:

  • All authors agree to the Contributor License Agreement.
  • The code follows the project's coding standards.
  • I have performed self-review and added comments where needed.
  • I have added or updated tests to verify that my changes are effective and functional.
  • I have run all existing tests and confirmed they pass.

🌦️ >> Documentation << 🌦️
https://sites.ecmwf.int/docs/dev-section/eckit/pull-requests/PR-281

baseName(false) stripped from the first dot instead of the last,
producing incorrect results for multi-extension filenames such as
archive.tar.gz (yielded "archive" instead of "archive.tar"). This
is inconsistent with C++17 std::filesystem::path::stem and with
eckit's own extension() method which already uses find_last_of.

Fixes: ECKIT-415

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes LocalPathName::baseName(false) so it strips only the final extension (matching C++17 std::filesystem::path::stem) and adds regression tests for multi-dot and hidden filenames (ECKIT-415).

Changes:

  • Update LocalPathName::baseName(false) to strip from the last . (not the first).
  • Expand filesystem tests to cover multi-extension filenames and hidden files.
  • Clarify baseName() documentation about stripping only the final extension.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
src/eckit/filesystem/LocalPathName.cc Changes baseName(false) to use the last dot when stripping the extension.
src/eckit/filesystem/LocalPathName.h Updates the baseName() parameter documentation to reflect “final extension only”.
tests/filesystem/test_localpathname.cc Adds new test cases for multi-dot, double-dot, and hidden filenames.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.


EXPECT(LocalPathName("file..txt").baseName(false) == "file.");
EXPECT(LocalPathName("file..txt").baseName(true) == "file..txt");

Copy link

Copilot AI Feb 26, 2026

Choose a reason for hiding this comment

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

Tests cover multi-extension names and hidden files, but there isn’t a regression assertion for the related trailing-dot case (e.g. "fred.." should strip only the final '.' and become "fred."). Adding this helps lock in the intended “strip only the final extension” behavior for edge cases with empty extensions.

Suggested change
// Trailing-dot filenames: strip only the final extension (empty extension case)
EXPECT(LocalPathName("fred..").baseName(false) == "fred.");
EXPECT(LocalPathName("fred..").baseName(true) == "fred..");

Copilot uses AI. Check for mistakes.
Comment on lines 168 to 174
std::string s(path_.c_str() + n + 1);
if (!ext) {
n = -1;
i = 0;
q = s.c_str();
while (*q) {
if (*q == '.') {
n = i;
break;
}
q++;
i++;
}
if (n >= 0) {
s.resize(n);
const auto lastDot = s.find_last_of('.');
if (lastDot != std::string::npos && lastDot != 0) {
s.resize(lastDot);
}
}
Copy link

Copilot AI Feb 26, 2026

Choose a reason for hiding this comment

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

baseName(false) now treats a leading '.' as not being an extension separator (e.g. ".hidden" stays ".hidden"), but extension() still returns the substring from the last '.', which makes .hidden report an extension of ".hidden". This is inconsistent with the new semantics (and with std::filesystem), and can lead to surprising results if callers split/recombine using baseName(false) + extension(). Consider updating extension() to return "" when the only dot is a leading dot (and adding/adjusting tests accordingly), or clearly documenting that extension() treats leading dots differently.

Copilot uses AI. Check for mistakes.

/// Base name of the path
/// @param ext if false the extension is stripped
/// @param ext if false only the final extension is stripped
Copy link

Copilot AI Feb 26, 2026

Choose a reason for hiding this comment

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

The updated baseName(false) behavior also special-cases hidden files (leading dot is not treated as an extension separator). The header comment only mentions stripping the final extension; please document this leading-dot rule as part of the contract so callers understand what happens for names like .hidden and .hidden.tar.gz.

Suggested change
/// @param ext if false only the final extension is stripped
/// @param ext if false only the final extension is stripped. The leading '.' of a
/// hidden file is not treated as an extension separator, e.g. `.hidden` stays
/// `.hidden`, and `.hidden.tar.gz` becomes `.hidden.tar` when ext is false.

Copilot uses AI. Check for mistakes.
@codecov-commenter
Copy link

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 66.31%. Comparing base (e78f873) to head (c316664).
⚠️ Report is 2 commits behind head on develop.

Additional details and impacted files
@@           Coverage Diff            @@
##           develop     #281   +/-   ##
========================================
  Coverage    66.31%   66.31%           
========================================
  Files         1125     1125           
  Lines        57602    57605    +3     
  Branches      4403     4401    -2     
========================================
+ Hits         38199    38201    +2     
- Misses       19403    19404    +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

3 participants