aboutsummaryrefslogtreecommitdiffstats
path: root/tests/mason-core/installer/linker_spec.lua
blob: cc14294b5045256b55e165d42a2e763808e3d7e8 (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
local stub = require "luassert.stub"
local fs = require "mason-core.fs"
local path = require "mason-core.path"
local registry = require "mason-registry"

local WIN_CMD_SCRIPT = [[@ECHO off
GOTO start
:find_dp0
SET dp0=%%~dp0
EXIT /b
:start
SETLOCAL
CALL :find_dp0

endLocal & goto #_undefined_# 2>NUL || title %%COMSPEC%% & "%%dp0%%\%s" %%*]]

describe("installer", function()
    ---@module "mason-core.installer.linker"
    local linker
    ---@module "mason-core.platform"
    local platform

    before_each(function()
        package.loaded["mason-core.installer.platform"] = nil
        package.loaded["mason-core.installer.linker"] = nil
        platform = require "mason-core.platform"
        linker = require "mason-core.installer.linker"
    end)

    it(
        "should symlink executable on Unix",
        async_test(function()
            local dummy = registry.get_package "dummy"
            stub(fs.async, "file_exists")
            stub(fs.async, "symlink")
            stub(fs.async, "write_file")

            fs.async.file_exists.on_call_with(path.bin_prefix "my-executable").returns(false)
            fs.async.file_exists.on_call_with(path.bin_prefix "another-executable").returns(false)
            fs.async.file_exists
                .on_call_with(path.concat { dummy:get_install_path(), "nested", "path", "my-executable" })
                .returns(true)
            fs.async.file_exists
                .on_call_with(path.concat { dummy:get_install_path(), "another-executable" })
                .returns(true)

            local handle = InstallHandleGenerator "dummy"
            local ctx = InstallContextGenerator(handle)
            ctx:link_bin("my-executable", path.concat { "nested", "path", "my-executable" })
            ctx:link_bin("another-executable", "another-executable")
            linker.link(ctx)

            assert.spy(fs.async.write_file).was_called(0)
            assert.spy(fs.async.symlink).was_called(2)
            assert
                .spy(fs.async.symlink)
                .was_called_with("../packages/dummy/another-executable", path.bin_prefix "another-executable")
            assert
                .spy(fs.async.symlink)
                .was_called_with("../packages/dummy/nested/path/my-executable", path.bin_prefix "my-executable")
        end)
    )

    it(
        "should write executable wrapper on Windows",
        async_test(function()
            platform.is.mac = false
            platform.is.linux = false
            platform.is.unix = false
            platform.is.win = true

            local dummy = registry.get_package "dummy"
            stub(fs.async, "file_exists")
            stub(fs.async, "symlink")
            stub(fs.async, "write_file")

            fs.async.file_exists.on_call_with(path.bin_prefix "my-executable").returns(false)
            fs.async.file_exists.on_call_with(path.bin_prefix "another-executable").returns(false)
            fs.async.file_exists
                .on_call_with(path.concat { dummy:get_install_path(), "nested", "path", "my-executable" })
                .returns(true)
            fs.async.file_exists
                .on_call_with(path.concat { dummy:get_install_path(), "another-executable" })
                .returns(true)

            local handle = InstallHandleGenerator "dummy"
            local ctx = InstallContextGenerator(handle)
            ctx:link_bin("my-executable", path.concat { "nested", "path", "my-executable" })
            ctx:link_bin("another-executable", "another-executable")
            linker.link(ctx)

            assert.spy(fs.async.symlink).was_called(0)
            assert.spy(fs.async.write_file).was_called(2)
            assert
                .spy(fs.async.write_file)
                .was_called_with(path.bin_prefix "another-executable.cmd", WIN_CMD_SCRIPT:format "../packages/dummy/another-executable")
            assert
                .spy(fs.async.write_file)
                .was_called_with(
                    path.bin_prefix "my-executable.cmd",
                    WIN_CMD_SCRIPT:format "../packages/dummy/nested/path/my-executable"
                )
        end)
    )
end)