diff --git a/projects/core/src/main/java/dan200/computercraft/core/filesystem/FileSystem.java b/projects/core/src/main/java/dan200/computercraft/core/filesystem/FileSystem.java index 811b7df1b..75a4ce1f8 100644 --- a/projects/core/src/main/java/dan200/computercraft/core/filesystem/FileSystem.java +++ b/projects/core/src/main/java/dan200/computercraft/core/filesystem/FileSystem.java @@ -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 { diff --git a/projects/core/src/test/resources/test-rom/spec/apis/fs_spec.lua b/projects/core/src/test/resources/test-rom/spec/apis/fs_spec.lua index f9fe7aaa6..e6af8d55c 100644 --- a/projects/core/src/test/resources/test-rom/spec/apis/fs_spec.lua +++ b/projects/core/src/test/resources/test-rom/spec/apis/fs_spec.lua @@ -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")