mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2025-01-07 16:00:31 +00:00
Fix a couple of bugs with wget
- Provide a friendly message when writing to a file fails. - Correctly write empty files Fixes #832, closes #833
This commit is contained in:
parent
de6f27ceaf
commit
a735f23e1f
@ -51,7 +51,7 @@ local function get(sUrl)
|
|||||||
|
|
||||||
local sResponse = response.readAll()
|
local sResponse = response.readAll()
|
||||||
response.close()
|
response.close()
|
||||||
return sResponse
|
return sResponse or ""
|
||||||
end
|
end
|
||||||
|
|
||||||
if run then
|
if run then
|
||||||
@ -79,7 +79,12 @@ else
|
|||||||
local res = get(url)
|
local res = get(url)
|
||||||
if not res then return end
|
if not res then return end
|
||||||
|
|
||||||
local file = fs.open(sPath, "wb")
|
local file, err = fs.open(sPath, "wb")
|
||||||
|
if not file then
|
||||||
|
printError("Cannot save file: " .. err)
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
file.write(res)
|
file.write(res)
|
||||||
file.close()
|
file.close()
|
||||||
|
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
local capture = require "test_helpers".capture_program
|
local capture = require "test_helpers".capture_program
|
||||||
|
|
||||||
describe("The wget program", function()
|
describe("The wget program", function()
|
||||||
local function setup_request()
|
local default_contents = [[print("Hello", ...)]]
|
||||||
|
local function setup_request(contents)
|
||||||
stub(_G, "http", {
|
stub(_G, "http", {
|
||||||
checkURL = function()
|
checkURL = function()
|
||||||
return true
|
return true
|
||||||
@ -9,7 +10,7 @@ describe("The wget program", function()
|
|||||||
get = function()
|
get = function()
|
||||||
return {
|
return {
|
||||||
readAll = function()
|
readAll = function()
|
||||||
return [[print("Hello", ...)]]
|
return contents
|
||||||
end,
|
end,
|
||||||
close = function()
|
close = function()
|
||||||
end,
|
end,
|
||||||
@ -19,28 +20,52 @@ describe("The wget program", function()
|
|||||||
end
|
end
|
||||||
|
|
||||||
it("downloads one file", function()
|
it("downloads one file", function()
|
||||||
setup_request()
|
fs.delete("/example.com")
|
||||||
|
setup_request(default_contents)
|
||||||
|
|
||||||
capture(stub, "wget", "https://example.com")
|
capture(stub, "wget", "https://example.com")
|
||||||
|
|
||||||
expect(fs.exists("/example.com")):eq(true)
|
expect(fs.exists("/example.com")):eq(true)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
it("downloads one file with given filename", function()
|
it("downloads one file with given filename", function()
|
||||||
setup_request()
|
fs.delete("/test-files/download")
|
||||||
|
setup_request(default_contents)
|
||||||
|
|
||||||
capture(stub, "wget", "https://example.com /test-files/download")
|
capture(stub, "wget", "https://example.com /test-files/download")
|
||||||
|
|
||||||
expect(fs.exists("/test-files/download")):eq(true)
|
expect(fs.exists("/test-files/download")):eq(true)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
it("downloads empty files", function()
|
||||||
|
fs.delete("/test-files/download")
|
||||||
|
setup_request(nil)
|
||||||
|
|
||||||
|
capture(stub, "wget", "https://example.com", "/test-files/download")
|
||||||
|
|
||||||
|
expect(fs.exists("/test-files/download")):eq(true)
|
||||||
|
expect(fs.getSize("/test-files/download")):eq(0)
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("cannot save to rom", function()
|
||||||
|
setup_request(default_contents)
|
||||||
|
|
||||||
|
expect(capture(stub, "wget", "https://example.com", "/rom/a-file.txt")):matches {
|
||||||
|
ok = true,
|
||||||
|
output = "Connecting to https://example.com... Success.\n",
|
||||||
|
error = "Cannot save file: /rom/a-file.txt: Access denied\n",
|
||||||
|
}
|
||||||
|
end)
|
||||||
|
|
||||||
it("runs a program from the internet", function()
|
it("runs a program from the internet", function()
|
||||||
setup_request()
|
setup_request(default_contents)
|
||||||
|
|
||||||
expect(capture(stub, "wget", "run", "http://test.com", "a", "b", "c"))
|
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 = "" }
|
:matches { ok = true, output = "Connecting to http://test.com... Success.\nHello a b c\n", error = "" }
|
||||||
end)
|
end)
|
||||||
|
|
||||||
it("displays its usage when given no arguments", function()
|
it("displays its usage when given no arguments", function()
|
||||||
setup_request()
|
setup_request(default_contents)
|
||||||
|
|
||||||
expect(capture(stub, "wget"))
|
expect(capture(stub, "wget"))
|
||||||
:matches { ok = true, output = "Usage:\nwget <url> [filename]\nwget run <url>\n", error = "" }
|
:matches { ok = true, output = "Usage:\nwget <url> [filename]\nwget run <url>\n", error = "" }
|
||||||
|
Loading…
Reference in New Issue
Block a user