mirror of
				https://github.com/SquidDev-CC/CC-Tweaked
				synced 2025-11-04 07:32:59 +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:
		@@ -51,7 +51,7 @@ local function get(sUrl)
 | 
			
		||||
 | 
			
		||||
    local sResponse = response.readAll()
 | 
			
		||||
    response.close()
 | 
			
		||||
    return sResponse
 | 
			
		||||
    return sResponse or ""
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
if run then
 | 
			
		||||
@@ -79,7 +79,12 @@ else
 | 
			
		||||
    local res = get(url)
 | 
			
		||||
    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.close()
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -1,7 +1,8 @@
 | 
			
		||||
local capture = require "test_helpers".capture_program
 | 
			
		||||
 | 
			
		||||
describe("The wget program", function()
 | 
			
		||||
    local function setup_request()
 | 
			
		||||
    local default_contents = [[print("Hello", ...)]]
 | 
			
		||||
    local function setup_request(contents)
 | 
			
		||||
        stub(_G, "http", {
 | 
			
		||||
            checkURL = function()
 | 
			
		||||
                return true
 | 
			
		||||
@@ -9,7 +10,7 @@ describe("The wget program", function()
 | 
			
		||||
            get = function()
 | 
			
		||||
                return {
 | 
			
		||||
                    readAll = function()
 | 
			
		||||
                        return [[print("Hello", ...)]]
 | 
			
		||||
                        return contents
 | 
			
		||||
                    end,
 | 
			
		||||
                    close = function()
 | 
			
		||||
                    end,
 | 
			
		||||
@@ -19,28 +20,52 @@ describe("The wget program", function()
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    it("downloads one file", function()
 | 
			
		||||
        setup_request()
 | 
			
		||||
        fs.delete("/example.com")
 | 
			
		||||
        setup_request(default_contents)
 | 
			
		||||
 | 
			
		||||
        capture(stub, "wget", "https://example.com")
 | 
			
		||||
 | 
			
		||||
        expect(fs.exists("/example.com")):eq(true)
 | 
			
		||||
    end)
 | 
			
		||||
 | 
			
		||||
    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")
 | 
			
		||||
 | 
			
		||||
        expect(fs.exists("/test-files/download")):eq(true)
 | 
			
		||||
    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()
 | 
			
		||||
        setup_request()
 | 
			
		||||
        setup_request(default_contents)
 | 
			
		||||
 | 
			
		||||
        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()
 | 
			
		||||
        setup_request(default_contents)
 | 
			
		||||
 | 
			
		||||
        expect(capture(stub, "wget"))
 | 
			
		||||
            :matches { ok = true, output = "Usage:\nwget <url> [filename]\nwget run <url>\n", error = "" }
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user