aboutsummaryrefslogtreecommitdiffstats
path: root/tests/core/fetch_spec.lua
blob: b4929167cabdaeba456d4a38eb16aead40e769cf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
local stub = require "luassert.stub"
local fetch = require "nvim-lsp-installer.core.fetch"
local spawn = require "nvim-lsp-installer.core.spawn"
local Result = require "nvim-lsp-installer.core.result"

describe("fetch", function()
    it(
        "should exhaust all candidates",
        async_test(function()
            stub(spawn, "wget")
            stub(spawn, "curl")
            spawn.wget.returns(Result.failure "wget failure")
            spawn.curl.returns(Result.failure "curl failure")

            local result = fetch "https://api.github.com"
            assert.is_true(result:is_failure())
            assert.spy(spawn.wget).was_called(1)
            assert.spy(spawn.curl).was_called(1)
            assert.spy(spawn.wget).was_called_with {
                {
                    "--header",
                    "User-Agent: nvim-lsp-installer (+https://github.com/williamboman/nvim-lsp-installer)",
                },
                "-nv",
                "-O",
                "-",
                "https://api.github.com",
            }
            assert.spy(spawn.curl).was_called_with {
                { "-H", "User-Agent: nvim-lsp-installer (+https://github.com/williamboman/nvim-lsp-installer)" },
                "-fsSL",
                vim.NIL,
                "https://api.github.com",
            }
        end)
    )

    it(
        "should return stdout",
        async_test(function()
            stub(spawn, "wget")
            spawn.wget.returns(Result.success {
                stdout = [[{"data": "here"}]],
            })
            local result = fetch "https://api.github.com/data"
            assert.is_true(result:is_success())
            assert.equals([[{"data": "here"}]], result:get_or_throw())
        end)
    )

    it(
        "should respect out_file opt",
        async_test(function()
            stub(spawn, "wget")
            stub(spawn, "curl")
            spawn.wget.returns(Result.failure "wget failure")
            spawn.curl.returns(Result.failure "curl failure")
            fetch("https://api.github.com/data", { out_file = "/test.json" })

            assert.spy(spawn.wget).was_called_with {
                {
                    "--header",
                    "User-Agent: nvim-lsp-installer (+https://github.com/williamboman/nvim-lsp-installer)",
                },
                "-nv",
                "-O",
                "/test.json",
                "https://api.github.com/data",
            }

            assert.spy(spawn.curl).was_called_with {
                { "-H", "User-Agent: nvim-lsp-installer (+https://github.com/williamboman/nvim-lsp-installer)" },
                "-fsSL",
                { "-o", "/test.json" },
                "https://api.github.com/data",
            }
        end)
    )
end)