Skip to content

Commit

Permalink
fix: Ensure nameless FSEntry is not and cannot be constructed
Browse files Browse the repository at this point in the history
We currently do not expect to have nameless FSEntry save for the root,
so add an assertiong that would catch such a case. If a use case arise,
this can always be removed.
  • Loading branch information
Geod24 committed Jan 6, 2025
1 parent 362452f commit f5d9aa9
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions source/dub/internal/io/mockfs.d
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,12 @@ public final class MockFS : Filesystem {
import std.algorithm.iteration : reduce;

const abs = path.absolute();
auto segments = path.bySegment;
// `library-nonet` (using vibe.d) has an empty front for absolute path,
// while our built-in module (in vibecompat) does not.
if (abs && segments.front.name.length == 0) segments.popFront();
reduce!((FSEntry dir, segment) => dir.mkdir(segment.name))(
(abs ? this.root : this.cwd), path.bySegment);
(abs ? this.root : this.cwd), segments);
}

/// Ditto
Expand Down Expand Up @@ -276,11 +280,15 @@ public final class MockFS : Filesystem {
import std.algorithm.iteration : reduce;

const abs = path.absolute();
auto segments = path.bySegment;
// `library-nonet` (using vibe.d) has an empty front for absolute path,
// while our built-in module (in vibecompat) does not.
if (abs && segments.front.name.length == 0) segments.popFront();
// Casting away constness because no good way to do this with `inout`,
// but `FSEntry.lookup` is `inout` too.
return cast(inout(FSEntry)) reduce!(
(FSEntry dir, segment) => dir ? dir.lookup(segment.name) : null)
(cast() (abs ? this.root : this.cwd), path.bySegment);
(cast() (abs ? this.root : this.cwd), segments);
}
}

Expand Down Expand Up @@ -333,6 +341,10 @@ public class FSEntry
import std.datetime.date;
SysTime DefaultTime = SysTime(DateTime(2020, 01, 01));

assert(n.length > 0,
"FSentry.this(%s, %s, %s) called with empty name"
.format(p.path(), t, n));

this.attributes.type = t;
this.parent = p;
this.name = n;
Expand Down

0 comments on commit f5d9aa9

Please sign in to comment.