aboutsummaryrefslogtreecommitdiffstats
path: root/tests/core/process_spec.lua
blob: 3c76b462c21bd575d126dfc62e9a5f59922c1a0d (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
local spy = require "luassert.spy"
local process = require "nvim-lsp-installer.process"
local async = require "plenary.async"

describe("process.attempt", function()
    it("should attempt job variants until first successful one", function()
        local on_finish = spy.new()
        local on_iterate = spy.new()
        local success = function(cb)
            cb(true)
        end
        local fail = function(cb)
            cb(false)
        end

        local job1 = spy.new(fail)
        local job2 = spy.new(fail)
        local job3 = spy.new(success)
        local job4 = spy.new(success)
        local job5 = spy.new(fail)

        process.attempt {
            jobs = { job1, job2, job3, job4, job5 },
            on_iterate = on_iterate,
            on_finish = on_finish,
        }

        assert.spy(job1).was_called(1)
        assert.spy(job2).was_called(1)
        assert.spy(job3).was_called(1)
        assert.spy(job4).was_called(0)
        assert.spy(job5).was_called(0)
        assert.spy(on_iterate).was_called(2)
        assert.spy(on_finish).was_called(1)
        assert.spy(on_finish).was_called_with(true)
    end)

    it("should call on_finish with false if all variants fail", function()
        local on_finish = spy.new()
        local on_iterate = spy.new()
        local fail = function(cb)
            cb(false)
        end

        local job1 = spy.new(fail)
        local job2 = spy.new(fail)
        local job3 = spy.new(fail)

        process.attempt {
            jobs = { job1, job2, job3 },
            on_iterate = on_iterate,
            on_finish = on_finish,
        }

        assert.spy(job1).was_called(1)
        assert.spy(job2).was_called(1)
        assert.spy(job3).was_called(1)
        assert.spy(on_iterate).was_called(2)
        assert.spy(on_finish).was_called(1)
        assert.spy(on_finish).was_called_with(false)
    end)
end)

describe("process.spawn", function()
    -- Unix only
    it(
        "should spawn command and feed output to sink",
        async_test(function()
            local stdio = process.in_memory_sink()
            local callback = spy.new()
            process.spawn("env", {
                args = {},
                env = {
                    "HELLO=world",
                    "MY_ENV=var",
                },
                stdio_sink = stdio.sink,
            }, callback)

            assert.wait_for(function()
                assert.spy(callback).was_called(1)
                assert.spy(callback).was_called_with(true, 0)
                assert.equal(table.concat(stdio.buffers.stdout, ""), "HELLO=world\nMY_ENV=var\n")
            end)
        end)
    )
end)

describe("process.chain", function()
    -- Unix only
    it(
        "should chain commands",
        async_test(function()
            local stdio = process.in_memory_sink()
            local callback = spy.new()

            local c = process.chain {
                env = {
                    "HELLO=world",
                    "MY_ENV=var",
                },
                stdio_sink = stdio.sink,
            }

            c.run("env", {})
            c.run("bash", { "-c", "echo Hello $HELLO" })
            c.spawn(callback)

            assert.wait_for(function()
                assert.spy(callback).was_called(1)
                assert.spy(callback).was_called_with(true)
                assert.equal(table.concat(stdio.buffers.stdout, ""), "HELLO=world\nMY_ENV=var\nHello world\n")
            end)
        end)
    )

    -- Unix only
    it(
        "should abort chain commands should one fail",
        async_test(function()
            local stdio = process.in_memory_sink()
            local callback = spy.new()

            local c = process.chain {
                env = {
                    "HELLO=world",
                    "MY_ENV=var",
                },
                stdio_sink = stdio.sink,
            }

            c.run("env", {})
            c.run("bash", { "-c", ">&2 echo Uh oh; exit 1" })
            c.run("bash", { "-c", "echo Hello $HELLO" })
            c.spawn(callback)

            assert.wait_for(function()
                assert.spy(callback).was_called(1)
                assert.spy(callback).was_called_with(false)
                assert.equal(table.concat(stdio.buffers.stdout, ""), "HELLO=world\nMY_ENV=var\n")
                assert.equal(table.concat(stdio.buffers.stderr, ""), "Uh oh\n")
            end)
        end)
    )
end)