mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2025-01-23 15:36:54 +00:00
Test behaviour of fs.getName/getDir with relative paths
It's not entirely clear what the correct behaviour of fs.getDir("..") should be, and there's not much consensus between various languages. I think the intended behaviour of this function is to move "up" one directory level in the path, meaning it should return "../..".
This commit is contained in:
parent
34a2fd039f
commit
e57b6fede2
@ -122,6 +122,10 @@ public class FileSystem {
|
||||
}
|
||||
|
||||
var lastSlash = path.lastIndexOf('/');
|
||||
|
||||
// If the trailing segment is a "..", then just append another one.
|
||||
if (path.substring(lastSlash < 0 ? 0 : lastSlash + 1).equals("..")) return path + "/..";
|
||||
|
||||
if (lastSlash >= 0) {
|
||||
return path.substring(0, lastSlash);
|
||||
} else {
|
||||
|
@ -160,6 +160,41 @@ describe("The fs library", function()
|
||||
end)
|
||||
end)
|
||||
|
||||
describe("fs.getName", function()
|
||||
it("returns 'root' for the empty path", function()
|
||||
expect(fs.getName("")):eq("root")
|
||||
expect(fs.getName("foo/..")):eq("root")
|
||||
end)
|
||||
|
||||
it("returns the file name", function()
|
||||
expect(fs.getName("foo/bar")):eq("bar")
|
||||
expect(fs.getName("foo/bar/")):eq("bar")
|
||||
expect(fs.getName("../foo")):eq("foo")
|
||||
end)
|
||||
|
||||
it("returns '..' for parent directories", function()
|
||||
expect(fs.getName("..")):eq("..")
|
||||
end)
|
||||
end)
|
||||
|
||||
describe("fs.getDir", function()
|
||||
it("returns '..' for the empty path", function()
|
||||
expect(fs.getDir("")):eq("..")
|
||||
expect(fs.getDir("foo/..")):eq("..")
|
||||
end)
|
||||
|
||||
it("returns the directory name", function()
|
||||
expect(fs.getDir("foo/bar")):eq("foo")
|
||||
expect(fs.getDir("foo/bar/")):eq("foo")
|
||||
expect(fs.getDir("../foo")):eq("..")
|
||||
end)
|
||||
|
||||
it("returns '..' for parent directories", function()
|
||||
expect(fs.getDir("..")):eq("../..")
|
||||
expect(fs.getDir("../..")):eq("../../..")
|
||||
end)
|
||||
end)
|
||||
|
||||
describe("fs.getSize", function()
|
||||
it("fails on non-existent nodes", function()
|
||||
expect.error(fs.getSize, "rom/x"):eq("/rom/x: No such file")
|
||||
|
Loading…
Reference in New Issue
Block a user