Skip to content

Commit

Permalink
Test behaviour of fs.getName/getDir with relative paths
Browse files Browse the repository at this point in the history
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 "../..".
  • Loading branch information
SquidDev committed Aug 18, 2024
1 parent 34a2fd0 commit e57b6fe
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ public static String getDirectory(String path) {
}

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 {
Expand Down
35 changes: 35 additions & 0 deletions projects/core/src/test/resources/test-rom/spec/apis/fs_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit e57b6fe

Please sign in to comment.