aboutsummaryrefslogtreecommitdiffstats
path: root/tests/mason-core/managers/composer_spec.lua
blob: 577becaa8a72bc094a277f780a935f239444fa31 (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
local Result = require "mason-core.result"
local composer = require "mason-core.managers.composer"
local installer = require "mason-core.installer"
local mock = require "luassert.mock"
local path = require "mason-core.path"
local spawn = require "mason-core.spawn"
local spy = require "luassert.spy"
local stub = require "luassert.stub"

describe("composer manager", function()
    it(
        "should call composer require",
        async_test(function()
            local handle = InstallHandleGenerator "dummy"
            local ctx = InstallContextGenerator(handle, { version = "42.13.37" })
            ctx.fs.file_exists = spy.new(mockx.returns(false))
            installer.prepare_installer(ctx)
            installer.exec_in_context(
                ctx,
                composer.packages { "main-package", "supporting-package", "supporting-package2" }
            )
            assert.spy(ctx.spawn.composer).was_called(2)
            assert.spy(ctx.spawn.composer).was_called_with {
                "init",
                "--no-interaction",
                "--stability=stable",
            }
            assert.spy(ctx.spawn.composer).was_called_with {
                "require",
                {
                    "main-package:42.13.37",
                    "supporting-package",
                    "supporting-package2",
                },
            }
        end)
    )

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

describe("composer version check", function()
    it(
        "should return current version",
        async_test(function()
            stub(spawn, "composer")
            spawn.composer.returns(Result.success {
                stdout = [[
                    {
                        "name": "vimeo/psalm",
                        "versions": [
                            "4.0.0"
                        ]
                    }
                ]],
            })

            local result = composer.get_installed_primary_package_version(
                mock.new {
                    primary_source = mock.new {
                        type = "composer",
                        package = "vimeo/psalm",
                    },
                },
                path.package_prefix "dummy"
            )

            assert.spy(spawn.composer).was_called(1)
            assert.spy(spawn.composer).was_called_with {
                "info",
                "--format=json",
                "vimeo/psalm",
                cwd = path.package_prefix "dummy",
            }
            assert.is_true(result:is_success())
            assert.equals("4.0.0", result:get_or_nil())
        end)
    )

    it(
        "should return outdated primary package",
        async_test(function()
            stub(spawn, "composer")
            spawn.composer.returns(Result.success {
                stdout = [[
                    {
                        "installed": [
                            {
                                "name": "vimeo/psalm",
                                "version": "4.0.0",
                                "latest": "4.22.0",
                                "latest-status": "semver-safe-update",
                                "description": "A static analysis tool for finding errors in PHP applications"
                            }
                        ]
                    }
                ]],
            })

            local result = composer.check_outdated_primary_package(
                mock.new {
                    primary_source = mock.new {
                        type = "composer",
                        package = "vimeo/psalm",
                    },
                },
                path.package_prefix "dummy"
            )

            assert.spy(spawn.composer).was_called(1)
            assert.spy(spawn.composer).was_called_with {
                "outdated",
                "--no-interaction",
                "--format=json",
                cwd = path.package_prefix "dummy",
            }
            assert.is_true(result:is_success())
            assert.same({
                name = "vimeo/psalm",
                current_version = "4.0.0",
                latest_version = "4.22.0",
            }, result:get_or_nil())
        end)
    )

    it(
        "should return failure if primary package is not outdated",
        async_test(function()
            stub(spawn, "composer")
            spawn.composer.returns(Result.success {
                stdout = [[{"installed": []}]],
            })

            local result = composer.check_outdated_primary_package(
                mock.new {
                    primary_source = mock.new {
                        type = "composer",
                        package = "vimeo/psalm",
                    },
                },
                path.package_prefix "dummy"
            )

            assert.is_true(result:is_failure())
            assert.equals("Primary package is not outdated.", result:err_or_nil())
        end)
    )
end)