'absolute' gives wrong result when CWD is '/'; crash in 'relative'
Closed this issue · 3 comments
jensjoha commented
Find below a test that fails on Linux as well as a proposed fix.
Only tested on Linux.
diff --git a/lib/path.dart b/lib/path.dart
index 8e87c33..f4748cb 100644
--- a/lib/path.dart
+++ b/lib/path.dart
@@ -91,10 +91,12 @@ String get current {
return _current;
} else {
var path = uri.toFilePath();
- // Remove trailing '/' or '\'.
+ // Remove trailing '/' or '\' unless it is the only thing left
+ // (for instance the root on Linux).
var lastIndex = path.length - 1;
assert(path[lastIndex] == '/' || path[lastIndex] == '\\');
- _current = path.substring(0, lastIndex);
+ if (lastIndex > 0) _current = path.substring(0, lastIndex);
+ else _current = path;
return _current;
}
}
diff --git a/test/io_test.dart b/test/io_test.dart
index f15f82d..251d91c 100644
--- a/test/io_test.dart
+++ b/test/io_test.dart
@@ -55,4 +55,30 @@ main() {
io.Directory.current = dir;
}
});
+
+ test('absolute works on root working directory', () {
+ var orgDir = io.Directory.current.path;
+ try {
+ while (io.Directory.current.parent != null &&
+ io.Directory.current.parent.path != io.Directory.current.path) {
+ io.Directory.current = io.Directory.current.parent;
+ }
+ var dir = io.Directory.current.path;
+
+ // "path.relative(path.absolute('foo/bar'), from: dir)" currently crashes
+ // on Linux.
+ expect(path.relative(path.absolute('foo/bar'), from: dir),
+ path.relative(path.absolute('foo/bar')));
+
+ // On Linux this currently compares "foo/bar" and "/foo/bar".
+ expect(path.normalize(path.absolute('foo/bar')),
+ equals(path.normalize(path.join(dir, '../foo/bar'))));
+
+ // On Linux this currently compares "foo/bar" and "/foo/bar".
+ expect(path.normalize(path.absolute('foo/bar')),
+ equals(path.normalize(path.context.join(dir, '../foo/bar'))));
+ } finally {
+ io.Directory.current = orgDir;
+ }
+ });
}
nex3 commented
Can you submit a pull request?
jensjoha commented
I have now tried. Hope I didn't step on any toes in the way I did it (I haven't used this workflow before).
nex3 commented
You did great, thanks!