From 1acb8441ec8f5fc61c471c16a1b1cf6a1491c20f Mon Sep 17 00:00:00 2001 From: Jonathan Coates Date: Tue, 1 Nov 2022 19:22:07 +0000 Subject: [PATCH] Add a couple of tests for file autocompletion --- doc/stub/fs.lua | 42 ++++++++++++++++--- .../resources/data/computercraft/lua/bios.lua | 11 ++--- .../resources/test-rom/spec/apis/fs_spec.lua | 40 ++++++++++++++++++ 3 files changed, 82 insertions(+), 11 deletions(-) diff --git a/doc/stub/fs.lua b/doc/stub/fs.lua index e2d8f0741..a393d7cd2 100644 --- a/doc/stub/fs.lua +++ b/doc/stub/fs.lua @@ -22,13 +22,43 @@ directory exist) and one without it (meaning this entry is an immediate completion candidate). `include_dirs` can be set to @{false} to only include those with a trailing slash. -@tparam string path The path to complete. -@tparam string location The location where paths are resolved from. -@tparam[opt] boolean include_files When @{false}, only directories will be -included in the returned list. -@tparam[opt] boolean include_dirs When @{false}, "raw" directories will not be -included in the returned list. +@tparam[1] string path The path to complete. +@tparam[1] string location The location where paths are resolved from. +@tparam[1,opt=true] boolean include_files When @{false}, only directories will +be included in the returned list. +@tparam[1,opt=true] boolean include_dirs When @{false}, "raw" directories will +not be included in the returned list. + +@tparam[2] string path The path to complete. +@tparam[2] string location The location where paths are resolved from. +@tparam[2] { + include_dirs? = boolean, include_files? = boolean, + include_hidden? = boolean +} options +This table form is an expanded version of the previous syntax. The +`include_files` and `include_dirs` arguments from above are passed in as fields. + +This table also accepts the following options: + - `include_hidden`: Whether to include hidden files (those starting with `.`) + by default. They will still be shown when typing a `.`. + @treturn { string... } A list of possible completion candidates. @since 1.74 +@changed 1.101.0 +@usage Complete files in the root directory. + + read(nil, nil, function(str) + return fs.complete(str, "", true, false) + end) + +@usage Complete files in the root directory, hiding hidden files by default. + + read(nil, nil, function(str) + return fs.complete(str, "", { + include_files = true, + include_dirs = false, + included_hidden = false, + }) + end) ]] function complete(path, location, include_files, include_dirs) end diff --git a/src/main/resources/data/computercraft/lua/bios.lua b/src/main/resources/data/computercraft/lua/bios.lua index 1b8a73f61..a653c97a6 100644 --- a/src/main/resources/data/computercraft/lua/bios.lua +++ b/src/main/resources/data/computercraft/lua/bios.lua @@ -3,7 +3,7 @@ -- Ideally we'd use require, but that is part of the shell, and so is not -- available to the BIOS or any APIs. All APIs load this using dofile, but that -- has not been defined at this point. -local expect +local expect, field do local h = fs.open("rom/modules/main/cc/expect.lua", "r") @@ -11,7 +11,8 @@ do h.close() if not f then error(err) end - expect = f().expect + local res = f() + expect, field = res.expect, res.field end if _VERSION == "Lua 5.1" then @@ -718,9 +719,9 @@ function fs.complete(sPath, sLocation, bIncludeFiles, bIncludeDirs) expect(2, sLocation, "string") local bIncludeHidden = nil if type(bIncludeFiles) == "table" then - bIncludeDirs = bIncludeFiles.include_dirs - bIncludeHidden = bIncludeFiles.include_hidden - bIncludeFiles = bIncludeFiles.include_files + bIncludeDirs = field(bIncludeFiles, "include_dirs", "boolean", "nil") + bIncludeHidden = field(bIncludeFiles, "include_hidden", "boolean", "nil") + bIncludeFiles = field(bIncludeFiles, "include_files", "boolean", "nil") else expect(3, bIncludeFiles, "boolean", "nil") expect(4, bIncludeDirs, "boolean", "nil") diff --git a/src/test/resources/test-rom/spec/apis/fs_spec.lua b/src/test/resources/test-rom/spec/apis/fs_spec.lua index 32503598e..7bcab9e86 100644 --- a/src/test/resources/test-rom/spec/apis/fs_spec.lua +++ b/src/test/resources/test-rom/spec/apis/fs_spec.lua @@ -10,6 +10,46 @@ describe("The fs library", function() expect.error(fs.complete, "", "", 1):eq("bad argument #3 (expected boolean, got number)") expect.error(fs.complete, "", "", true, 1):eq("bad argument #4 (expected boolean, got number)") end) + + describe("include_hidden", function() + local dir = "tmp/hidden" + local function setup_tree() + fs.delete(dir) + fs.makeDir(dir) + fs.open(dir .. "/file.txt", "w").close() + fs.open(dir .. "/.hidden.txt", "w").close() + end + + it("hides hidden files", function() + setup_tree() + local opts = { include_files = true, include_dirs = false, include_hidden = false } + + expect(fs.complete("", dir, opts)):same { "../", "file.txt" } + expect(fs.complete(dir .. "/", "", opts)):same { "file.txt" } + end) + + it("shows hidden files when typing a dot", function() + setup_tree() + local opts = { include_files = true, include_dirs = false, include_hidden = false } + + expect(fs.complete(".", dir, opts)):same { "./", "hidden.txt" } + expect(fs.complete(dir .. "/.", "", opts)):same { "hidden.txt" } + + -- Also test + expect(fs.complete(dir .. "/file", "", opts)):same { ".txt" } + expect(fs.complete(dir .. "/file.", "", opts)):same { "txt" } + expect(fs.complete("file", dir, opts)):same { ".txt" } + expect(fs.complete("file.", dir, opts)):same { "txt" } + end) + + it("shows hidden files when include_hidden is true", function() + setup_tree() + local opts = { include_files = true, include_dirs = false, include_hidden = true } + + expect(fs.complete("", dir, opts)):same { "../", ".hidden.txt", "file.txt" } + expect(fs.complete(dir .. "/", "", opts)):same { ".hidden.txt", "file.txt" } + end) + end) end) describe("fs.isDriveRoot", function()