aboutsummaryrefslogtreecommitdiffstats
path: root/tests/server_spec.lua
blob: 41a4075083993566e3b48101ed1bd50b4a41c0e7 (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
local spy = require "luassert.spy"
local lsp_installer = require "nvim-lsp-installer"
local server = require "nvim-lsp-installer.server"
local a = require "nvim-lsp-installer.core.async"
local context = require "nvim-lsp-installer.installers.context"
local fs = require "nvim-lsp-installer.fs"

local function timestamp()
    local seconds, microseconds = vim.loop.gettimeofday()
    return (seconds * 1000) + math.floor(microseconds / 1000)
end

describe("server", function()
    it(
        "calls registered on_ready handlers upon successful installation",
        async_test(function()
            local on_ready_handler = spy.new()
            local generic_handler = spy.new()

            lsp_installer.on_server_ready(generic_handler)

            local srv = ServerGenerator {
                name = "on_ready_fixture",
                root_dir = server.get_server_root_path "on_ready_fixture",
            }
            srv:on_ready(on_ready_handler)
            srv:install()
            assert.wait_for(function()
                assert.spy(on_ready_handler).was_called(1)
                assert.spy(generic_handler).was_called(1)
                assert.spy(generic_handler).was_called_with(srv)
            end)
            assert.is_true(srv:is_installed())
        end)
    )

    it(
        "doesn't call on_ready handler when server fails installation",
        async_test(function()
            local on_ready_handler = spy.new()
            local generic_handler = spy.new()

            lsp_installer.on_server_ready(generic_handler)

            local srv = FailingServerGenerator {
                name = "on_ready_fixture_failing",
                root_dir = server.get_server_root_path "on_ready_fixture_failing",
            }
            srv:on_ready(on_ready_handler)
            srv:install()
            a.sleep(500)
            assert.spy(on_ready_handler).was_not_called()
            assert.spy(generic_handler).was_not_called()
            assert.is_false(srv:is_installed())
        end)
    )

    -- it(
    --     "should be able to run async installer functions",
    --     async_test(function()
    --         local srv = ServerGenerator {
    --             name = "async_installer_fixture",
    --             root_dir = server.get_server_root_path "async_installer_fixture",
    --             async = true,
    --             installer = function()
    --                 a.sleep(130)
    --             end,
    --         }
    --         local start = timestamp()
    --         srv:install()
    --         a.sleep(100)
    --         assert.wait_for(function()
    --             assert.is_true(srv:is_installed())
    --         end)
    --         local stop = timestamp()
    --         assert.is_true(stop - start >= 100)
    --     end)
    -- )

    it(
        "should remove directories upon installation failure",
        async_test(function()
            local srv = FailingServerGenerator {
                name = "remove_dirs_failure",
                root_dir = server.get_server_root_path "remove_dirs_failure",
                installer = {
                    -- 1. sleep 500ms
                    function(_, callback)
                        vim.defer_fn(function()
                            callback(true)
                        end, 500)
                    end,
                    -- 2. promote install dir
                    context.promote_install_dir(),
                    -- 3. fail
                    function(_, callback)
                        callback(false)
                    end,
                },
            }
            srv:install()

            -- 1. installation started
            a.sleep(50)
            assert.is_true(fs.dir_exists(srv:get_tmp_install_dir()))

            -- 2. install dir promoted
            a.sleep(500)
            assert.is_false(fs.dir_exists(srv:get_tmp_install_dir()))

            -- 3. installation failed
            a.sleep(200)

            assert.is_false(srv:is_installed())
            assert.is_false(fs.dir_exists(srv:get_tmp_install_dir()))
        end)
    )
end)