aboutsummaryrefslogtreecommitdiffstats
path: root/tests/mason-core/managers/go_spec.lua
blob: e841c1f38dccf607d6ac991ce16c15fc4ddff18d (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
local mock = require "luassert.mock"
local stub = require "luassert.stub"
local spy = require "luassert.spy"
local Result = require "mason-core.result"
local go = require "mason-core.managers.go"
local spawn = require "mason-core.spawn"
local installer = require "mason-core.installer"
local path = require "mason-core.path"

describe("go manager", function()
    it(
        "should call go install",
        async_test(function()
            local handle = InstallHandleGenerator "dummy"
            local ctx = InstallContextGenerator(handle, { requested_version = "42.13.37" })
            installer.run_installer(ctx, go.packages { "main-package", "supporting-package", "supporting-package2" })
            assert.spy(ctx.spawn.go).was_called(3)
            assert.spy(ctx.spawn.go).was_called_with {
                "install",
                "-v",
                "main-package@42.13.37",
                env = { GOBIN = path.package_build_prefix "dummy" },
            }
            assert.spy(ctx.spawn.go).was_called_with {
                "install",
                "-v",
                "supporting-package@latest",
                env = { GOBIN = path.package_build_prefix "dummy" },
            }
            assert.spy(ctx.spawn.go).was_called_with {
                "install",
                "-v",
                "supporting-package2@latest",
                env = { GOBIN = path.package_build_prefix "dummy" },
            }
        end)
    )

    it(
        "should provide receipt information",
        async_test(function()
            local handle = InstallHandleGenerator "dummy"
            local ctx = InstallContextGenerator(handle, { requested_version = "42.13.37" })
            installer.run_installer(ctx, go.packages { "main-package", "supporting-package", "supporting-package2" })
            assert.same({
                type = "go",
                package = "main-package",
            }, ctx.receipt.primary_source)
            assert.same({
                {
                    type = "go",
                    package = "supporting-package",
                },
                {
                    type = "go",
                    package = "supporting-package2",
                },
            }, ctx.receipt.secondary_sources)
        end)
    )
end)

describe("go version check", function()
    local go_version_output = [[
gopls: go1.18
        path    golang.org/x/tools/cmd
        mod     golang.org/x/tools/cmd        v0.8.1  h1:q5nDpRopYrnF4DN/1o8ZQ7Oar4Yd4I5OtGMx5RyV2/8=
        dep     github.com/google/go-cmp        v0.5.7  h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o=
        dep     mvdan.cc/xurls/v2       v2.4.0  h1:tzxjVAj+wSBmDcF6zBB7/myTy3gX9xvi8Tyr28AuQgc=
        build   -compiler=gc
        build   GOOS=darwin
]]

    it("should parse go version output", function()
        local parsed = go.parse_mod_version_output(go_version_output)
        assert.same({
            path = { ["golang.org/x/tools/cmd"] = "" },
            mod = { ["golang.org/x/tools/cmd"] = "v0.8.1" },
            dep = { ["github.com/google/go-cmp"] = "v0.5.7", ["mvdan.cc/xurls/v2"] = "v2.4.0" },
            build = { ["-compiler=gc"] = "", ["GOOS=darwin"] = "" },
        }, parsed)
    end)

    it(
        "should return current version",
        async_test(function()
            spawn.go = spy.new(function()
                return Result.success { stdout = go_version_output }
            end)

            local result = go.get_installed_primary_package_version(
                mock.new {
                    primary_source = mock.new {
                        type = "go",
                        package = "golang.org/x/tools/cmd/gopls/...",
                    },
                },
                path.package_prefix "dummy"
            )

            assert.spy(spawn.go).was_called(1)
            assert.spy(spawn.go).was_called_with {
                "version",
                "-m",
                "gopls",
                cwd = path.package_prefix "dummy",
            }
            assert.is_true(result:is_success())
            assert.equals("v0.8.1", result:get_or_nil())

            spawn.go = nil
        end)
    )

    it(
        "should return outdated primary package",
        async_test(function()
            stub(spawn, "go")
            spawn.go
                .on_call_with({
                    "list",
                    "-json",
                    "-m",
                    "golang.org/x/tools/cmd@latest",
                    cwd = path.package_prefix "dummy",
                })
                .returns(Result.success {
                    stdout = ([[
            {
                "Path": %q,
                "Version": "v2.0.0"
            }
            ]]):format(path.package_prefix "dummy"),
                })
            spawn.go
                .on_call_with({
                    "version",
                    "-m",
                    "gopls",
                    cwd = path.package_prefix "dummy",
                })
                .returns(Result.success {
                    stdout = go_version_output,
                })

            local result = go.check_outdated_primary_package(
                mock.new {
                    primary_source = mock.new {
                        type = "go",
                        package = "golang.org/x/tools/cmd/gopls/...",
                    },
                },
                path.package_prefix "dummy"
            )

            assert.is_true(result:is_success())
            assert.same({
                name = "golang.org/x/tools/cmd",
                current_version = "v0.8.1",
                latest_version = "v2.0.0",
            }, result:get_or_nil())

            spawn.go = nil
        end)
    )

    it("should parse package mod names", function()
        assert.equals("github.com/cweill/gotests", go.parse_package_mod "github.com/cweill/gotests/...")
        assert.equals("golang.org/x/tools/gopls", go.parse_package_mod "golang.org/x/tools/gopls/...")
        assert.equals("golang.org/x/crypto", go.parse_package_mod "golang.org/x/crypto/...")
        assert.equals("github.com/go-delve/delve", go.parse_package_mod "github.com/go-delve/delve/cmd/dlv")
        assert.equals("mvdan.cc/sh/v3", go.parse_package_mod "mvdan.cc/sh/v3/cmd/shfmt")
    end)
end)