From 309cbdb8be2ee9a616b1fd8dd474d366987432ce Mon Sep 17 00:00:00 2001 From: JakobDev Date: Sat, 8 Jun 2019 15:49:42 +0200 Subject: [PATCH 1/6] Add `wget run` (#218) Equivalent to `pastebin run`, but allows running arbitrary URLs instead. Is this a little questionable? Yes - people shouldn't be downloading and running code from the internet. But hey, people do that already, so we might as well make it convenient. --- .../computercraft/lua/rom/help/wget.txt | 1 + .../assets/computercraft/lua/rom/motd.txt | 4 +- .../lua/rom/programs/http/wget.lua | 58 +++++++++++++------ .../assets/computercraft/lua/rom/startup.lua | 7 +++ .../test-rom/spec/http/wget_spec.lua | 53 +++++++++++++++++ 5 files changed, 104 insertions(+), 19 deletions(-) create mode 100644 src/test/resources/test-rom/spec/http/wget_spec.lua diff --git a/src/main/resources/assets/computercraft/lua/rom/help/wget.txt b/src/main/resources/assets/computercraft/lua/rom/help/wget.txt index 428ed5bab..65e580c08 100644 --- a/src/main/resources/assets/computercraft/lua/rom/help/wget.txt +++ b/src/main/resources/assets/computercraft/lua/rom/help/wget.txt @@ -5,3 +5,4 @@ ex: "wget http://pastebin.com/raw/CxaWmPrX test" will download the file from the URL http://pastebin.com/raw/CxaWmPrX, and save it as "test". "wget http://example.org/test.lua/?foo=bar#qzu" will download the file from the URL http://example.org/test.lua/?foo=bar#qzu and save it as "test.lua" "wget http://example.org/" will download the file from the URL http://example.org and save it as "example.org" +"wget run http://pastebin.com/raw/CxaWmPrX" will download the file from the URL http://pastebin.com/raw/CxaWmPrX and run it immediately. diff --git a/src/main/resources/assets/computercraft/lua/rom/motd.txt b/src/main/resources/assets/computercraft/lua/rom/motd.txt index afb322c11..d81dc2fd0 100644 --- a/src/main/resources/assets/computercraft/lua/rom/motd.txt +++ b/src/main/resources/assets/computercraft/lua/rom/motd.txt @@ -8,6 +8,8 @@ Run "list" or "ls" to see all files in a directory. You can delete files and directories with "delete" or "rm". Use "pastebin put" to upload a program to pastebin. Use "pastebin get" to download a program from pastebin. -Use "pastebin run" to run a program from pastebin without saving it. +Use "pastebin run" to run a program from pastebin. Use the "edit" program to create and edit your programs. You can copy files with "copy" or "cp". +You can use "wget run " to run a program from the internet. +You can use "wget" to download a file from the internet. diff --git a/src/main/resources/assets/computercraft/lua/rom/programs/http/wget.lua b/src/main/resources/assets/computercraft/lua/rom/programs/http/wget.lua index 08063c640..e05def30e 100644 --- a/src/main/resources/assets/computercraft/lua/rom/programs/http/wget.lua +++ b/src/main/resources/assets/computercraft/lua/rom/programs/http/wget.lua @@ -2,14 +2,24 @@ local function printUsage() print( "Usage:" ) print( "wget [filename]" ) + print( "wget run " ) end local tArgs = { ... } + +local run = false +if tArgs[1] == "run" then + table.remove( tArgs, 1 ) + run = true +end + if #tArgs < 1 then printUsage() return end +local url = table.remove( tArgs, 1 ) + if not http then printError( "wget requires http API" ) printError( "Set http_enable to true in ComputerCraft.cfg" ) @@ -22,6 +32,13 @@ local function getFilename( sUrl ) end local function get( sUrl ) + -- Check if the URL is valid + local ok, err = http.checkURL( url ) + if not ok then + printError( err or "Invalid URL." ) + return + end + write( "Connecting to " .. sUrl .. "... " ) local response = http.get( sUrl , nil , true ) @@ -37,29 +54,34 @@ local function get( sUrl ) return sResponse end --- Determine file to download -local sUrl = tArgs[1] +if run then + local res = get(url) + if not res then return end ---Check if the URL is valid -local ok, err = http.checkURL( sUrl ) -if not ok then - printError( err or "Invalid URL." ) - return -end + local func, err = load(res, getFilename(url), "t", _ENV) + if not func then + printError(err) + return + end -local sFile = tArgs[2] or getFilename( sUrl ) -local sPath = shell.resolve( sFile ) -if fs.exists( sPath ) then - print( "File already exists" ) - return -end + local ok, err = pcall(func, table.unpack(tArgs)) + if not ok then + printError( err ) + end +else + local sFile = tArgs[1] or getFilename( url ) + local sPath = shell.resolve( sFile ) + if fs.exists( sPath ) then + print( "File already exists" ) + return + end + + local res = get(url) + if not res then return end --- Do the get -local res = get( sUrl ) -if res then local file = fs.open( sPath, "wb" ) file.write( res ) file.close() - print( "Downloaded as "..sFile ) + print( "Downloaded as " .. sFile ) end diff --git a/src/main/resources/assets/computercraft/lua/rom/startup.lua b/src/main/resources/assets/computercraft/lua/rom/startup.lua index 9f6d74ade..2bc19adca 100644 --- a/src/main/resources/assets/computercraft/lua/rom/startup.lua +++ b/src/main/resources/assets/computercraft/lua/rom/startup.lua @@ -183,6 +183,12 @@ local function completeExec( shell, nIndex, sText, tPreviousText ) return completeMultipleChoice( sText, tCommands, true ) end end +local tWgetOptions = { "run" } +local function completeWget( shell, nIndex, sText, tPreviousText ) + if nIndex == 1 then + return completeMultipleChoice( sText, tWgetOptions, true ) + end +end shell.setCompletionFunction( "rom/programs/alias.lua", completeAlias ) shell.setCompletionFunction( "rom/programs/cd.lua", completeDir ) shell.setCompletionFunction( "rom/programs/copy.lua", completeEitherEither ) @@ -210,6 +216,7 @@ shell.setCompletionFunction( "rom/programs/fun/advanced/paint.lua", completeFile shell.setCompletionFunction( "rom/programs/http/pastebin.lua", completePastebin ) shell.setCompletionFunction( "rom/programs/rednet/chat.lua", completeChat ) shell.setCompletionFunction( "rom/programs/command/exec.lua", completeExec ) +shell.setCompletionFunction( "rom/programs/http/wget.lua", completeWget ) if turtle then local tGoOptions = { "left", "right", "forward", "back", "down", "up" } diff --git a/src/test/resources/test-rom/spec/http/wget_spec.lua b/src/test/resources/test-rom/spec/http/wget_spec.lua new file mode 100644 index 000000000..b6b88b34b --- /dev/null +++ b/src/test/resources/test-rom/spec/http/wget_spec.lua @@ -0,0 +1,53 @@ +local capture = require "test_helpers".capture_program + +describe("The wget program", function() + local function setup_request() + stub(_G, "http", { + checkURL = function() + return true + end, + get = function() + return { + readAll = function() + return [[print("Hello", ...)]] + end, + close = function() + end, + } + end + }) + end + + it("downloads one file", function() + setup_request() + capture(stub, "wget", "https://example.com") + + expect(fs.exists("/example.com")):eq(true) + end) + + it("downloads one file with given filename", function() + setup_request() + capture(stub, "wget", "https://example.com /test-files/download") + + expect(fs.exists("/test-files/download")):eq(true) + end) + + it("runs a program from the internet", function() + setup_request() + + expect(capture(stub, "wget", "run", "http://test.com", "a", "b", "c")) + :matches { ok = true, output = "Connecting to http://test.com... Success.\nHello a b c\n", error = "" } + end) + + it("displays its usage when given no arguments", function() + setup_request() + + expect(capture(stub, "wget")) + :matches { ok = true, output = "Usage:\nwget [filename]\nwget run \n", error = "" } + end) + + it("can be completed", function() + local complete = shell.getCompletionInfo()["rom/programs/http/wget.lua"].fnComplete + expect(complete(shell, 1, "", {})):same { "run " } + end) +end) From bc07dfad2ecb86853feaa92de7769d43f2c01c68 Mon Sep 17 00:00:00 2001 From: SquidDev Date: Wed, 12 Jun 2019 21:03:11 +0100 Subject: [PATCH 2/6] Make sure all writeDescription methods are pure We don't want to be firing block updates or anything from here! That runs the ricks of causing CMEs and the like. Fixes #245 --- .../computercraft/shared/computer/blocks/TileComputerBase.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/dan200/computercraft/shared/computer/blocks/TileComputerBase.java b/src/main/java/dan200/computercraft/shared/computer/blocks/TileComputerBase.java index c6f796ee4..f049ef940 100644 --- a/src/main/java/dan200/computercraft/shared/computer/blocks/TileComputerBase.java +++ b/src/main/java/dan200/computercraft/shared/computer/blocks/TileComputerBase.java @@ -453,7 +453,7 @@ public ClientComputer getClientComputer() protected void writeDescription( @Nonnull NBTTagCompound nbt ) { super.writeDescription( nbt ); - nbt.setInteger( "instanceID", createServerComputer().getInstanceID() ); + if( m_instanceID >= 0 ) nbt.setInteger( "instanceID", m_instanceID ); if( m_label != null ) nbt.setString( "label", m_label ); if( m_computerID >= 0 ) nbt.setInteger( "computerID", m_computerID ); } From f3798bfb639192d50831e39a91c3f93a27b3f048 Mon Sep 17 00:00:00 2001 From: JakobDev Date: Mon, 10 Jun 2019 15:31:38 +0200 Subject: [PATCH 3/6] Improve rename.lua's argument validation --- .../computercraft/lua/rom/programs/rename.lua | 9 ++++- .../test-rom/spec/programs/rename_spec.lua | 40 +++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 src/test/resources/test-rom/spec/programs/rename_spec.lua diff --git a/src/main/resources/assets/computercraft/lua/rom/programs/rename.lua b/src/main/resources/assets/computercraft/lua/rom/programs/rename.lua index f22561c26..4583adf62 100644 --- a/src/main/resources/assets/computercraft/lua/rom/programs/rename.lua +++ b/src/main/resources/assets/computercraft/lua/rom/programs/rename.lua @@ -7,8 +7,15 @@ end local sSource = shell.resolve( tArgs[1] ) local sDest = shell.resolve( tArgs[2] ) -if fs.exists( sDest ) then +if not fs.exists( sSource ) then + printError( "No matching files" ) + return +elseif fs.exists( sDest ) then printError( "Destination exists" ) + return +elseif fs.isReadOnly( sDest ) then + printError( "Destination is read-only" ) + return end fs.move( sSource, sDest ) diff --git a/src/test/resources/test-rom/spec/programs/rename_spec.lua b/src/test/resources/test-rom/spec/programs/rename_spec.lua new file mode 100644 index 000000000..2ab955dab --- /dev/null +++ b/src/test/resources/test-rom/spec/programs/rename_spec.lua @@ -0,0 +1,40 @@ +local capture = require "test_helpers".capture_program + +describe("The rename program", function() + local function touch(file) + io.open(file, "w"):close() + end + + it("can rename a file", function() + touch("/test-files/rename/a.txt") + + shell.run("rename /test-files/rename/a.txt /test-files/rename/b.txt") + + expect(fs.exists("/test-files/rename/a.txt")):eq(false) + expect(fs.exists("/test-files/rename/b.txt")):eq(true) + end) + + it("fails when renaming a file which doesn't exist", function() + expect(capture(stub, "rename nothing destination")) + :matches { ok = true, output = "", error = "No matching files\n" } + end) + + it("fails when overwriting an existing file", function() + touch("/test-files/rename/c.txt") + + expect(capture(stub, "rename /test-files/rename/c.txt /test-files/rename/c.txt")) + :matches { ok = true, output = "", error = "Destination exists\n" } + end) + + it("fails when copying to read-only locations", function() + touch("/test-files/rename/d.txt") + + expect(capture(stub, "rename /test-files/rename/d.txt /rom/test.txt")) + :matches { ok = true, output = "", error = "Destination is read-only\n" } + end) + + it("displays the usage when given no arguments", function() + expect(capture(stub, "rename")) + :matches { ok = true, output = "Usage: rename \n", error = "" } + end) +end) From 81daf8264769b9c26b96f4b24b7b7144d173e9e7 Mon Sep 17 00:00:00 2001 From: JakobDev Date: Mon, 10 Jun 2019 15:59:24 +0200 Subject: [PATCH 4/6] Add Checks to copy.lua --- .../computercraft/lua/rom/programs/copy.lua | 4 ++ .../test-rom/spec/programs/copy_spec.lua | 40 +++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 src/test/resources/test-rom/spec/programs/copy_spec.lua diff --git a/src/main/resources/assets/computercraft/lua/rom/programs/copy.lua b/src/main/resources/assets/computercraft/lua/rom/programs/copy.lua index 8158b84a5..70cde52a1 100644 --- a/src/main/resources/assets/computercraft/lua/rom/programs/copy.lua +++ b/src/main/resources/assets/computercraft/lua/rom/programs/copy.lua @@ -15,6 +15,10 @@ if #tFiles > 0 then elseif #tFiles == 1 then if fs.exists( sDest ) then printError( "Destination exists" ) + elseif fs.isReadOnly( sDest ) then + printError( "Destination is read-only" ) + elseif fs.getFreeSpace( sDest ) < fs.getSize( sFile ) then + printError( "Not enough space" ) else fs.copy( sFile, sDest ) end diff --git a/src/test/resources/test-rom/spec/programs/copy_spec.lua b/src/test/resources/test-rom/spec/programs/copy_spec.lua new file mode 100644 index 000000000..4ab043986 --- /dev/null +++ b/src/test/resources/test-rom/spec/programs/copy_spec.lua @@ -0,0 +1,40 @@ +local capture = require "test_helpers".capture_program + +describe("The copy program", function() + local function touch(file) + io.open(file, "w"):close() + end + + it("copies a file", function() + touch("/test-files/copy/a.txt") + + shell.run("copy /test-files/copy/a.txt /test-files/copy/b.txt") + + expect(fs.exists("/test-files/copy/a.txt")):eq(true) + expect(fs.exists("/test-files/copy/b.txt")):eq(true) + end) + + it("fails when copying a non-existent file", function() + expect(capture(stub, "copy nothing destination")) + :matches { ok = true, output = "", error = "No matching files\n" } + end) + + it("fails when overwriting an existing file", function() + touch("/test-files/copy/c.txt") + + expect(capture(stub, "copy /test-files/copy/c.txt /test-files/copy/c.txt")) + :matches { ok = true, output = "", error = "Destination exists\n" } + end) + + it("fails when copying into read-only locations", function() + touch("/test-files/copy/d.txt") + + expect(capture(stub, "copy /test-files/copy/d.txt /rom/test.txt")) + :matches { ok = true, output = "", error = "Destination is read-only\n" } + end) + + it("displays the usage when given no arguments", function() + expect(capture(stub, "copy")) + :matches { ok = true, output = "Usage: cp \n", error = "" } + end) +end) From 717ab690934276a2984cbe709550f960da932593 Mon Sep 17 00:00:00 2001 From: JakobDev Date: Fri, 14 Jun 2019 09:15:12 +0200 Subject: [PATCH 5/6] Add a few Checks (#248) - Add some basic tests for several built-in programs. - Preserve the state of the shell between tests --- src/test/resources/test-rom/mcfly.lua | 4 + .../test-rom/spec/programs/cd_spec.lua | 20 +++++ .../test-rom/spec/programs/edit_spec.lua | 11 +++ .../spec/programs/http/pastebin_spec.lua | 77 +++++++++++++++++++ .../spec/{ => programs}/http/wget_spec.lua | 0 .../test-rom/spec/programs/id_spec.lua | 11 +++ .../test-rom/spec/programs/motd_spec.lua | 14 ++++ .../test-rom/spec/programs/move_spec.lua | 26 +++++++ .../test-rom/spec/programs/set_spec.lua | 29 +++++++ .../test-rom/spec/programs/shell_spec.lua | 4 + .../test-rom/spec/programs/time_spec.lua | 12 +++ .../test-rom/spec/programs/type_spec.lua | 26 +++++++ 12 files changed, 234 insertions(+) create mode 100644 src/test/resources/test-rom/spec/programs/cd_spec.lua create mode 100644 src/test/resources/test-rom/spec/programs/edit_spec.lua create mode 100644 src/test/resources/test-rom/spec/programs/http/pastebin_spec.lua rename src/test/resources/test-rom/spec/{ => programs}/http/wget_spec.lua (100%) create mode 100644 src/test/resources/test-rom/spec/programs/id_spec.lua create mode 100644 src/test/resources/test-rom/spec/programs/motd_spec.lua create mode 100644 src/test/resources/test-rom/spec/programs/move_spec.lua create mode 100644 src/test/resources/test-rom/spec/programs/set_spec.lua create mode 100644 src/test/resources/test-rom/spec/programs/time_spec.lua create mode 100644 src/test/resources/test-rom/spec/programs/type_spec.lua diff --git a/src/test/resources/test-rom/mcfly.lua b/src/test/resources/test-rom/mcfly.lua index 02b4b26ed..38ebf9e83 100644 --- a/src/test/resources/test-rom/mcfly.lua +++ b/src/test/resources/test-rom/mcfly.lua @@ -49,6 +49,8 @@ local function push_state() term = term.current(), input = io.input(), output = io.output(), + dir = shell.dir(), + path = shell.path(), stubs = stubs, } end @@ -65,6 +67,8 @@ local function pop_state(state) term.redirect(state.term) io.input(state.input) io.output(state.output) + shell.setDir(state.dir) + shell.setPath(state.path) end local error_mt = { __tostring = function(self) return self.message end } diff --git a/src/test/resources/test-rom/spec/programs/cd_spec.lua b/src/test/resources/test-rom/spec/programs/cd_spec.lua new file mode 100644 index 000000000..2826b8007 --- /dev/null +++ b/src/test/resources/test-rom/spec/programs/cd_spec.lua @@ -0,0 +1,20 @@ +local capture = require "test_helpers".capture_program + +describe("The cd program", function() + + it("cd into a directory", function() + shell.run("cd /rom/programs") + + expect(shell.dir()):eq("rom/programs") + end) + + it("cd into a not existing directory", function() + expect(capture(stub, "cd /rom/nothing")) + :matches { ok = true, output = "Not a directory\n", error = "" } + end) + + it("displays the usage with no arguments", function() + expect(capture(stub, "cd")) + :matches { ok = true, output = "Usage: cd \n", error = "" } + end) +end) diff --git a/src/test/resources/test-rom/spec/programs/edit_spec.lua b/src/test/resources/test-rom/spec/programs/edit_spec.lua new file mode 100644 index 000000000..dc8bcf9d0 --- /dev/null +++ b/src/test/resources/test-rom/spec/programs/edit_spec.lua @@ -0,0 +1,11 @@ +local capture = require "test_helpers".capture_program + +describe("The edit program", function() + + it("displays its usage when given no argument", function() + multishell = nil + + expect(capture(stub, "edit")) + :matches { ok = true, output = "Usage: edit \n", error = "" } + end) +end) diff --git a/src/test/resources/test-rom/spec/programs/http/pastebin_spec.lua b/src/test/resources/test-rom/spec/programs/http/pastebin_spec.lua new file mode 100644 index 000000000..eb55f53d4 --- /dev/null +++ b/src/test/resources/test-rom/spec/programs/http/pastebin_spec.lua @@ -0,0 +1,77 @@ +local capture = require "test_helpers".capture_program + +describe("The pastebin program", function() + local function setup_request() + stub(_G, "http", { + checkURL = function() + return true + end, + get = function() + return { + readAll = function() + return [[print("Hello", ...)]] + end, + close = function() + end, + getResponseHeaders = function() + local tHeader = {} + tHeader["Content-Type"] = "text/plain; charset=utf-8" + return tHeader + end + } + end, + post = function() + return { + readAll = function() + return "https://pastebin.com/abcde" + end, + close = function() + end, + } + end + }) + end + + it("downloads one file", function() + setup_request() + capture(stub, "pastebin", "get", "abcde", "testdown") + + expect(fs.exists("/testdown")):eq(true) + end) + + it("runs a program from the internet", function() + setup_request() + + expect(capture(stub, "pastebin", "run", "abcde", "a", "b", "c")) + :matches { ok = true, output = "Connecting to pastebin.com... Success.\nHello a b c\n", error = "" } + end) + + it("upload a program to pastebin", function() + setup_request() + + local file = fs.open( "testup", "w" ) + file.close() + + expect(capture(stub, "pastebin", "put", "testup" )) + :matches { ok = true, output = "Connecting to pastebin.com... Success.\nUploaded as https://pastebin.com/abcde\nRun \"pastebin get abcde\" to download anywhere\n", error = "" } + end) + + it("upload a not existing program to pastebin", function() + setup_request() + + expect(capture(stub, "pastebin", "put", "nothing" )) + :matches { ok = true, output = "No such file\n", error = "" } + end) + + it("displays its usage when given no arguments", function() + setup_request() + + expect(capture(stub, "pastebin")) + :matches { ok = true, output = "Usages:\npastebin put \npastebin get \npastebin run \n", error = "" } + end) + + it("can be completed", function() + local complete = shell.getCompletionInfo()["rom/programs/http/pastebin.lua"].fnComplete + expect(complete(shell, 1, "", {})):same { "put ", "get ", "run " } + end) +end) diff --git a/src/test/resources/test-rom/spec/http/wget_spec.lua b/src/test/resources/test-rom/spec/programs/http/wget_spec.lua similarity index 100% rename from src/test/resources/test-rom/spec/http/wget_spec.lua rename to src/test/resources/test-rom/spec/programs/http/wget_spec.lua diff --git a/src/test/resources/test-rom/spec/programs/id_spec.lua b/src/test/resources/test-rom/spec/programs/id_spec.lua new file mode 100644 index 000000000..a4e4657d3 --- /dev/null +++ b/src/test/resources/test-rom/spec/programs/id_spec.lua @@ -0,0 +1,11 @@ +local capture = require "test_helpers".capture_program + +describe("The id program", function() + + it("displays computer id", function() + local id = os.getComputerID() + + expect(capture(stub, "id")) + :matches { ok = true, output = "This is computer #"..id.."\n", error = "" } + end) +end) diff --git a/src/test/resources/test-rom/spec/programs/motd_spec.lua b/src/test/resources/test-rom/spec/programs/motd_spec.lua new file mode 100644 index 000000000..052cd8fa4 --- /dev/null +++ b/src/test/resources/test-rom/spec/programs/motd_spec.lua @@ -0,0 +1,14 @@ +local capture = require "test_helpers".capture_program + +describe("The motd program", function() + + it("displays MODT", function() + local file = fs.open("/modt_check.txt","w") + file.write("Hello World!") + file.close() + settings.set("motd.path","/modt_check.txt") + + expect(capture(stub, "motd")) + :matches { ok = true, output = "Hello World!\n", error = "" } + end) +end) diff --git a/src/test/resources/test-rom/spec/programs/move_spec.lua b/src/test/resources/test-rom/spec/programs/move_spec.lua new file mode 100644 index 000000000..de1a36569 --- /dev/null +++ b/src/test/resources/test-rom/spec/programs/move_spec.lua @@ -0,0 +1,26 @@ +local capture = require "test_helpers".capture_program + +describe("The move program", function() + local function touch(file) + io.open(file, "w"):close() + end + + it("move a file", function() + touch("/test-files/move/a.txt") + + shell.run("move /test-files/move/a.txt /test-files/move/b.txt") + + expect(fs.exists("/test-files/move/a.txt")):eq(false) + expect(fs.exists("/test-files/move/b.txt")):eq(true) + end) + + it("try to move a not existing file", function() + expect(capture(stub, "move nothing destination")) + :matches { ok = true, output = "", error = "No matching files\n" } + end) + + it("displays the usage with no arguments", function() + expect(capture(stub, "move")) + :matches { ok = true, output = "Usage: mv \n", error = "" } + end) +end) diff --git a/src/test/resources/test-rom/spec/programs/set_spec.lua b/src/test/resources/test-rom/spec/programs/set_spec.lua new file mode 100644 index 000000000..dc81dcfe3 --- /dev/null +++ b/src/test/resources/test-rom/spec/programs/set_spec.lua @@ -0,0 +1,29 @@ +local capture = require "test_helpers".capture_program + +describe("The set program", function() + + it("displays all settings", function() + settings.clear() + settings.set("Test","Hello World!") + settings.set("123",456) + + expect(capture(stub, "set")) + :matches { ok = true, output = '"123" is 456\n"Test" is "Hello World!"\n', error = "" } + end) + + it("displays a single settings", function() + settings.clear() + settings.set("Test","Hello World!") + settings.set("123",456) + + expect(capture(stub, "set Test")) + :matches { ok = true, output = '"Test" is "Hello World!"\n', error = "" } + end) + + it("set a setting", function() + expect(capture(stub, "set Test Hello")) + :matches { ok = true, output = '"Test" set to "Hello"\n', error = "" } + + expect(settings.get("Test")):eq("Hello") + end) +end) diff --git a/src/test/resources/test-rom/spec/programs/shell_spec.lua b/src/test/resources/test-rom/spec/programs/shell_spec.lua index 9b9dc7188..cb35d70f6 100644 --- a/src/test/resources/test-rom/spec/programs/shell_spec.lua +++ b/src/test/resources/test-rom/spec/programs/shell_spec.lua @@ -27,6 +27,10 @@ describe("The shell", function() shell.setDir(shell.dir()) expect.error(shell.setDir, nil):eq("bad argument #1 (expected string, got nil)") end) + + it("not existing directory", function() + expect.error(shell.setDir, "/rom/nothing"):eq("Not a directory") + end) end) describe("shell.setPath", function() diff --git a/src/test/resources/test-rom/spec/programs/time_spec.lua b/src/test/resources/test-rom/spec/programs/time_spec.lua new file mode 100644 index 000000000..8ec10044e --- /dev/null +++ b/src/test/resources/test-rom/spec/programs/time_spec.lua @@ -0,0 +1,12 @@ +local capture = require "test_helpers".capture_program + +describe("The time program", function() + + it("displays time", function() + local time = textutils.formatTime(os.time()) + local day = os.day() + + expect(capture(stub, "time")) + :matches { ok = true, output = "The time is "..time.." on Day "..day.."\n", error = "" } + end) +end) diff --git a/src/test/resources/test-rom/spec/programs/type_spec.lua b/src/test/resources/test-rom/spec/programs/type_spec.lua new file mode 100644 index 000000000..78009c746 --- /dev/null +++ b/src/test/resources/test-rom/spec/programs/type_spec.lua @@ -0,0 +1,26 @@ +local capture = require "test_helpers".capture_program + +describe("The type program", function() + + it("displays the usage with no arguments", function() + expect(capture(stub, "type")) + :matches { ok = true, output = "Usage: type \n", error = "" } + end) + + it("displays the output for a file", function() + expect(capture(stub, "type /rom/startup.lua")) + :matches { ok = true, output = "file\n", error = "" } + end) + + it("displays the output for a directory", function() + expect(capture(stub, "type /rom")) + :matches { ok = true, output = "directory\n", error = "" } + end) + + it("displays the output for a not existing path", function() + expect(capture(stub, "type /rom/nothing")) + :matches { ok = true, output = "No such path\n", error = "" } + end) + +end) + From 29dce26bf62525ccca110c9c0dee91ec319be404 Mon Sep 17 00:00:00 2001 From: SquidDev Date: Fri, 14 Jun 2019 20:55:32 +0100 Subject: [PATCH 6/6] Clean up checkstyle warning Fixes #251, closes #252 --- config/checkstyle/suppressions.xml | 4 --- .../shared/computer/blocks/TileComputer.java | 2 +- .../computer/blocks/TileComputerBase.java | 26 +++++++++---------- .../shared/turtle/blocks/TileTurtle.java | 2 +- 4 files changed, 15 insertions(+), 19 deletions(-) diff --git a/config/checkstyle/suppressions.xml b/config/checkstyle/suppressions.xml index a6dd2983d..0cafe14ef 100644 --- a/config/checkstyle/suppressions.xml +++ b/config/checkstyle/suppressions.xml @@ -3,10 +3,6 @@ "-//Checkstyle//DTD SuppressionFilter Configuration 1.2//EN" "https://checkstyle.org/dtds/suppressions_1_2.dtd"> - - - diff --git a/src/main/java/dan200/computercraft/shared/computer/blocks/TileComputer.java b/src/main/java/dan200/computercraft/shared/computer/blocks/TileComputer.java index 210e15098..7769f174d 100644 --- a/src/main/java/dan200/computercraft/shared/computer/blocks/TileComputer.java +++ b/src/main/java/dan200/computercraft/shared/computer/blocks/TileComputer.java @@ -32,7 +32,7 @@ protected ServerComputer createComputer( int instanceID, int id ) ServerComputer computer = new ServerComputer( getWorld(), id, - m_label, + label, instanceID, family, ComputerCraft.terminalWidth_computer, diff --git a/src/main/java/dan200/computercraft/shared/computer/blocks/TileComputerBase.java b/src/main/java/dan200/computercraft/shared/computer/blocks/TileComputerBase.java index f049ef940..7034d91b7 100644 --- a/src/main/java/dan200/computercraft/shared/computer/blocks/TileComputerBase.java +++ b/src/main/java/dan200/computercraft/shared/computer/blocks/TileComputerBase.java @@ -47,7 +47,7 @@ public abstract class TileComputerBase extends TileGeneric implements IComputerT { private int m_instanceID = -1; private int m_computerID = -1; - protected String m_label = null; + protected String label = null; private boolean m_on = false; boolean m_startOn = false; private boolean m_fresh = false; @@ -190,7 +190,7 @@ public void update() m_fresh = false; m_computerID = computer.getID(); - m_label = computer.getLabel(); + label = computer.getLabel(); m_on = computer.isOn(); if( computer.hasOutputChanged() ) updateOutput(); @@ -212,9 +212,9 @@ public NBTTagCompound writeToNBT( NBTTagCompound nbt ) { nbt.setInteger( "computerID", m_computerID ); } - if( m_label != null ) + if( label != null ) { - nbt.setString( "label", m_label ); + nbt.setString( "label", label ); } nbt.setBoolean( "on", m_on ); return super.writeToNBT( nbt ); @@ -248,7 +248,7 @@ else if( nbt.hasKey( "userDir" ) ) m_computerID = id; // Load label - m_label = nbt.hasKey( "label" ) ? nbt.getString( "label" ) : null; + label = nbt.hasKey( "label" ) ? nbt.getString( "label" ) : null; // Load power state m_startOn = nbt.getBoolean( "on" ); @@ -361,7 +361,7 @@ public final int getComputerID() @Override public final String getLabel() { - return m_label; + return label; } @Override @@ -378,9 +378,9 @@ public final void setComputerID( int id ) @Override public final void setLabel( String label ) { - if( getWorld().isRemote || Objects.equals( m_label, label ) ) return; + if( getWorld().isRemote || Objects.equals( this.label, label ) ) return; - m_label = label; + this.label = label; ServerComputer computer = getServerComputer(); if( computer != null ) computer.setLabel( label ); markDirty(); @@ -454,7 +454,7 @@ protected void writeDescription( @Nonnull NBTTagCompound nbt ) { super.writeDescription( nbt ); if( m_instanceID >= 0 ) nbt.setInteger( "instanceID", m_instanceID ); - if( m_label != null ) nbt.setString( "label", m_label ); + if( label != null ) nbt.setString( "label", label ); if( m_computerID >= 0 ) nbt.setInteger( "computerID", m_computerID ); } @@ -463,7 +463,7 @@ protected void readDescription( @Nonnull NBTTagCompound nbt ) { super.readDescription( nbt ); m_instanceID = nbt.getInteger( "instanceID" ); - m_label = nbt.hasKey( "label" ) ? nbt.getString( "label" ) : null; + label = nbt.hasKey( "label" ) ? nbt.getString( "label" ) : null; m_computerID = nbt.hasKey( "computerID" ) ? nbt.getInteger( "computerID" ) : -1; } @@ -474,7 +474,7 @@ protected void transferStateFrom( TileComputerBase copy ) unload(); m_instanceID = copy.m_instanceID; m_computerID = copy.m_computerID; - m_label = copy.m_label; + label = copy.label; m_on = copy.m_on; m_startOn = copy.m_startOn; updateBlock(); @@ -493,13 +493,13 @@ public IPeripheral getPeripheral( @Nonnull EnumFacing side ) @Override public String getName() { - return hasCustomName() ? m_label : getBlockType().getTranslationKey(); + return hasCustomName() ? label : getBlockType().getTranslationKey(); } @Override public boolean hasCustomName() { - return !Strings.isNullOrEmpty( m_label ); + return !Strings.isNullOrEmpty( label ); } @Nonnull diff --git a/src/main/java/dan200/computercraft/shared/turtle/blocks/TileTurtle.java b/src/main/java/dan200/computercraft/shared/turtle/blocks/TileTurtle.java index b96dd6868..353ce8d5d 100644 --- a/src/main/java/dan200/computercraft/shared/turtle/blocks/TileTurtle.java +++ b/src/main/java/dan200/computercraft/shared/turtle/blocks/TileTurtle.java @@ -95,7 +95,7 @@ public boolean hasMoved() protected ServerComputer createComputer( int instanceID, int id ) { ServerComputer computer = new ServerComputer( - getWorld(), id, m_label, instanceID, getFamily(), + getWorld(), id, label, instanceID, getFamily(), ComputerCraft.terminalWidth_turtle, ComputerCraft.terminalHeight_turtle ); computer.setPosition( getPos() );