1
0
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:
Jonathan Coates 2024-08-18 11:38:10 +01:00
parent 34a2fd039f
commit e57b6fede2
No known key found for this signature in database
GPG Key ID: B9E431FF07C98D06
2 changed files with 39 additions and 0 deletions

View File

@ -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 {

View File

@ -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")