diff options
| author | William Boman <william@redwill.se> | 2024-01-06 09:02:01 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-01-06 09:02:01 +0100 |
| commit | 6c685476df4f202e371bdd3d726729d6f3f8b9f0 (patch) | |
| tree | 53c96e8418f2669bdd5d345325d8542107d03ea3 /tests/mason-core | |
| parent | fix(cargo): don't attempt to fetch versions when version targets commit SHA (... (diff) | |
| download | mason-6c685476df4f202e371bdd3d726729d6f3f8b9f0.tar mason-6c685476df4f202e371bdd3d726729d6f3f8b9f0.tar.gz mason-6c685476df4f202e371bdd3d726729d6f3f8b9f0.tar.bz2 mason-6c685476df4f202e371bdd3d726729d6f3f8b9f0.tar.lz mason-6c685476df4f202e371bdd3d726729d6f3f8b9f0.tar.xz mason-6c685476df4f202e371bdd3d726729d6f3f8b9f0.tar.zst mason-6c685476df4f202e371bdd3d726729d6f3f8b9f0.zip | |
feat: add support for openvsx sources (#1589)
Diffstat (limited to 'tests/mason-core')
8 files changed, 361 insertions, 121 deletions
diff --git a/tests/mason-core/installer/managers/build_spec.lua b/tests/mason-core/installer/managers/build_spec.lua index 73dd63c1..e709fdd0 100644 --- a/tests/mason-core/installer/managers/build_spec.lua +++ b/tests/mason-core/installer/managers/build_spec.lua @@ -1,5 +1,5 @@ local Result = require "mason-core.result" -local build = require "mason-core.installer.managers.build" +local common = require "mason-core.installer.managers.common" local installer = require "mason-core.installer" local match = require "luassert.match" local mock = require "luassert.mock" @@ -25,7 +25,7 @@ describe("build manager", function() ) local result = installer.exec_in_context(ctx, function() - return build.run { + return common.run_build_instruction { run = [[npm install && npm run compile]], env = { MASON_VERSION = "2023-03-09", @@ -57,7 +57,7 @@ describe("build manager", function() stub(ctx.spawn, "bash", mockx.returns(Result.success())) local result = installer.exec_in_context(ctx, function() - return build.run { + return common.run_build_instruction { run = "make", staged = false, } diff --git a/tests/mason-core/installer/managers/common_spec.lua b/tests/mason-core/installer/managers/common_spec.lua new file mode 100644 index 00000000..e72d7697 --- /dev/null +++ b/tests/mason-core/installer/managers/common_spec.lua @@ -0,0 +1,161 @@ +local Result = require "mason-core.result" +local _ = require "mason-core.functional" +local common = require "mason-core.installer.managers.common" +local installer = require "mason-core.installer" +local match = require "luassert.match" +local mock = require "luassert.mock" +local spy = require "luassert.spy" +local std = require "mason-core.installer.managers.std" +local stub = require "luassert.stub" + +describe("common manager :: download", function() + it("should parse download files from common structure", function() + local url_generator = _.format "https://example.com/%s" + + assert.same( + { + { + download_url = "https://example.com/abc.jar", + out_file = "abc.jar", + }, + }, + common.parse_downloads({ + file = "abc.jar", + }, url_generator) + ) + + assert.same( + { + { + download_url = "https://example.com/abc.jar", + out_file = "lib/abc.jar", + }, + }, + common.parse_downloads({ + file = "abc.jar:lib/", + }, url_generator) + ) + + assert.same( + { + { + download_url = "https://example.com/abc.jar", + out_file = "lib/abc.jar", + }, + { + download_url = "https://example.com/file.jar", + out_file = "lib/nested/new-name.jar", + }, + }, + common.parse_downloads({ + file = { "abc.jar:lib/", "file.jar:lib/nested/new-name.jar" }, + }, url_generator) + ) + end) + + it("should download files", function() + local ctx = create_dummy_context() + stub(std, "download_file", mockx.returns(Result.success())) + stub(std, "unpack", mockx.returns(Result.success())) + + local result = installer.exec_in_context(ctx, function() + return common.download_files(ctx, { + { out_file = "file.jar", download_url = "https://example.com/file.jar" }, + { out_file = "LICENSE.md", download_url = "https://example.com/LICENSE" }, + }) + end) + + assert.is_true(result:is_success()) + assert.spy(std.download_file).was_called(2) + assert.spy(std.download_file).was_called_with("https://example.com/file.jar", "file.jar") + assert.spy(std.download_file).was_called_with("https://example.com/LICENSE", "LICENSE.md") + assert.spy(std.unpack).was_called(2) + assert.spy(std.unpack).was_called_with "file.jar" + assert.spy(std.unpack).was_called_with "LICENSE.md" + end) + + it("should download files to specified directory", function() + local ctx = create_dummy_context() + stub(std, "download_file", mockx.returns(Result.success())) + stub(std, "unpack", mockx.returns(Result.success())) + stub(ctx.fs, "mkdirp") + + local result = installer.exec_in_context(ctx, function() + return common.download_files(ctx, { + { out_file = "lib/file.jar", download_url = "https://example.com/file.jar" }, + { out_file = "doc/LICENSE.md", download_url = "https://example.com/LICENSE" }, + { out_file = "nested/path/to/file", download_url = "https://example.com/some-file" }, + }) + end) + + assert.is_true(result:is_success()) + + assert.spy(ctx.fs.mkdirp).was_called(3) + assert.spy(ctx.fs.mkdirp).was_called_with(match.is_ref(ctx.fs), "lib") + assert.spy(ctx.fs.mkdirp).was_called_with(match.is_ref(ctx.fs), "doc") + assert.spy(ctx.fs.mkdirp).was_called_with(match.is_ref(ctx.fs), "nested/path/to") + end) +end) + +describe("common manager :: build", function() + it("should run build instruction", function() + local ctx = create_dummy_context() + local uv = require "mason-core.async.uv" + spy.on(ctx, "promote_cwd") + stub(uv, "write") + stub(uv, "shutdown") + stub(uv, "close") + local stdin = mock.new() + stub( + ctx.spawn, + "bash", ---@param args SpawnArgs + function(args) + args.on_spawn(mock.new(), { stdin }) + return Result.success() + end + ) + + local result = installer.exec_in_context(ctx, function() + return common.run_build_instruction { + run = [[npm install && npm run compile]], + env = { + MASON_VERSION = "2023-03-09", + SOME_VALUE = "here", + }, + } + end) + + assert.is_true(result:is_success()) + assert.spy(ctx.promote_cwd).was_called(0) + assert.spy(ctx.spawn.bash).was_called(1) + assert.spy(ctx.spawn.bash).was_called_with(match.tbl_containing { + on_spawn = match.is_function(), + env = match.same { + MASON_VERSION = "2023-03-09", + SOME_VALUE = "here", + }, + }) + assert.spy(uv.write).was_called(2) + assert.spy(uv.write).was_called_with(stdin, "set -euxo pipefail;\n") + assert.spy(uv.write).was_called_with(stdin, "npm install && npm run compile") + assert.spy(uv.shutdown).was_called_with(stdin) + assert.spy(uv.close).was_called_with(stdin) + end) + + it("should promote cwd if not staged", function() + local ctx = create_dummy_context() + stub(ctx, "promote_cwd") + stub(ctx.spawn, "bash", mockx.returns(Result.success())) + + local result = installer.exec_in_context(ctx, function() + return common.run_build_instruction { + run = "make", + staged = false, + } + end) + + assert.is_true(result:is_success()) + assert.spy(ctx.promote_cwd).was_called(1) + assert.spy(ctx.spawn.bash).was_called(1) + end) +end) diff --git a/tests/mason-core/installer/registry/providers/generic/build_spec.lua b/tests/mason-core/installer/registry/providers/generic/build_spec.lua index ccc77ac3..443cb99a 100644 --- a/tests/mason-core/installer/registry/providers/generic/build_spec.lua +++ b/tests/mason-core/installer/registry/providers/generic/build_spec.lua @@ -121,8 +121,8 @@ end) describe("generic provider :: build :: installing", function() it("should install", function() local ctx = create_dummy_context() - local build = require "mason-core.installer.managers.build" - stub(build, "run", mockx.returns(Result.success())) + local common = require "mason-core.installer.managers.common" + stub(common, "run_build_instruction", mockx.returns(Result.success())) local result = installer.exec_in_context(ctx, function() return generic.install(ctx, { @@ -134,8 +134,8 @@ describe("generic provider :: build :: installing", function() end) assert.is_true(result:is_success()) - assert.spy(build.run).was_called(1) - assert.spy(build.run).was_called_with { + assert.spy(common.run_build_instruction).was_called(1) + assert.spy(common.run_build_instruction).was_called_with { run = "make", env = { VALUE = "here" }, } diff --git a/tests/mason-core/installer/registry/providers/generic/download_spec.lua b/tests/mason-core/installer/registry/providers/generic/download_spec.lua index 1d3583f9..4bcb1976 100644 --- a/tests/mason-core/installer/registry/providers/generic/download_spec.lua +++ b/tests/mason-core/installer/registry/providers/generic/download_spec.lua @@ -2,6 +2,7 @@ local Purl = require "mason-core.purl" local Result = require "mason-core.result" local generic = require "mason-core.installer.registry.providers.generic" local installer = require "mason-core.installer" +local match = require "luassert.match" local stub = require "luassert.stub" ---@param overrides Purl @@ -17,6 +18,12 @@ describe("generic provider :: download :: parsing", function() it("should parse single download target", function() assert.same( Result.success { + downloads = { + { + out_file = "name.tar.gz", + download_url = "https://getpackage.org/downloads/1.2.0/name.tar.gz", + }, + }, download = { files = { ["name.tar.gz"] = [[https://getpackage.org/downloads/1.2.0/name.tar.gz]], @@ -36,6 +43,12 @@ describe("generic provider :: download :: parsing", function() it("should coalesce download target", function() assert.same( Result.success { + downloads = { + { + out_file = "name.tar.gz", + download_url = "https://getpackage.org/downloads/linux-aarch64/1.2.0/name.tar.gz", + }, + }, download = { target = "linux_arm64", files = { @@ -88,31 +101,33 @@ end) describe("generic provider :: download :: installing", function() it("should install generic packages", function() local ctx = create_dummy_context() - local std = require "mason-core.installer.managers.std" - stub(std, "download_file", mockx.returns(Result.success())) - stub(std, "unpack", mockx.returns(Result.success())) + local common = require "mason-core.installer.managers.common" + stub(common, "download_files", mockx.returns(Result.success())) local result = installer.exec_in_context(ctx, function() return generic.install(ctx, { + downloads = { + { + out_file = "name.tar.gz", + download_url = "https://getpackage.org/downloads/linux-aarch64/1.2.0/name.tar.gz", + }, + }, download = { + target = "linux_arm64", files = { ["name.tar.gz"] = [[https://getpackage.org/downloads/linux-aarch64/1.2.0/name.tar.gz]], - ["archive.tar.gz"] = [[https://getpackage.org/downloads/linux-aarch64/1.2.0/archive.tar.gz]], }, }, }) end) assert.is_true(result:is_success()) - assert.spy(std.download_file).was_called(2) - assert - .spy(std.download_file) - .was_called_with("https://getpackage.org/downloads/linux-aarch64/1.2.0/name.tar.gz", "name.tar.gz") - assert - .spy(std.download_file) - .was_called_with("https://getpackage.org/downloads/linux-aarch64/1.2.0/archive.tar.gz", "archive.tar.gz") - assert.spy(std.unpack).was_called(2) - assert.spy(std.unpack).was_called_with "name.tar.gz" - assert.spy(std.unpack).was_called_with "archive.tar.gz" + assert.spy(common.download_files).was_called(1) + assert.spy(common.download_files).was_called_with(match.is_ref(ctx), { + { + out_file = "name.tar.gz", + download_url = "https://getpackage.org/downloads/linux-aarch64/1.2.0/name.tar.gz", + }, + }) end) end) diff --git a/tests/mason-core/installer/registry/providers/github/build_spec.lua b/tests/mason-core/installer/registry/providers/github/build_spec.lua index b25c26d8..17667d2c 100644 --- a/tests/mason-core/installer/registry/providers/github/build_spec.lua +++ b/tests/mason-core/installer/registry/providers/github/build_spec.lua @@ -1,10 +1,6 @@ local Purl = require "mason-core.purl" local Result = require "mason-core.result" local github = require "mason-core.installer.registry.providers.github" -local installer = require "mason-core.installer" -local match = require "luassert.match" -local mock = require "luassert.mock" -local stub = require "luassert.stub" ---@param overrides Purl local function purl(overrides) @@ -60,39 +56,3 @@ describe("github provider :: build :: parsing", function() ) end) end) - -describe("github provider :: build :: installing", function() - it("should install github build sources", function() - local ctx = create_dummy_context() - local std = require "mason-core.installer.managers.std" - local build = require "mason-core.installer.managers.build" - stub(std, "clone", mockx.returns(Result.success())) - stub(build, "run", mockx.returns(Result.success())) - - local result = installer.exec_in_context(ctx, function() - return github.install(ctx, { - repo = "namespace/name", - rev = "2023-03-09", - build = { - run = [[npm install && npm run compile]], - env = { - MASON_VERSION = "2023-03-09", - SOME_VALUE = "here", - }, - }, - }, purl()) - end) - - assert.is_true(result:is_success()) - assert.spy(std.clone).was_called(1) - assert.spy(std.clone).was_called_with("namespace/name", { rev = "2023-03-09" }) - assert.spy(build.run).was_called(1) - assert.spy(build.run).was_called_with { - run = [[npm install && npm run compile]], - env = { - MASON_VERSION = "2023-03-09", - SOME_VALUE = "here", - }, - } - end) -end) diff --git a/tests/mason-core/installer/registry/providers/github/release_spec.lua b/tests/mason-core/installer/registry/providers/github/release_spec.lua index 909eb36f..a6648b33 100644 --- a/tests/mason-core/installer/registry/providers/github/release_spec.lua +++ b/tests/mason-core/installer/registry/providers/github/release_spec.lua @@ -1,10 +1,10 @@ local Purl = require "mason-core.purl" local Result = require "mason-core.result" +local common = require "mason-core.installer.managers.common" local github = require "mason-core.installer.registry.providers.github" local installer = require "mason-core.installer" local match = require "luassert.match" local registry_installer = require "mason-core.installer.registry" -local spy = require "luassert.spy" local stub = require "luassert.stub" ---@param overrides Purl @@ -285,6 +285,7 @@ describe("github provider :: release :: installing", function() local std = require "mason-core.installer.managers.std" stub(std, "download_file", mockx.returns(Result.success())) stub(std, "unpack", mockx.returns(Result.success())) + stub(common, "download_files", mockx.returns(Result.success())) local result = installer.exec_in_context(ctx, function() return github.install(ctx, { @@ -306,62 +307,16 @@ describe("github provider :: release :: installing", function() end) assert.is_true(result:is_success()) - assert.spy(std.download_file).was_called(2) - assert.spy(std.download_file).was_called_with( - "https://github.com/namespace/name/releases/download/2023-03-09/file-linux-amd64-2023-03-09.tar.gz", - "file-linux-amd64-2023-03-09.tar.gz" - ) - assert.spy(std.download_file).was_called_with( - "https://github.com/namespace/name/releases/download/2023-03-09/another-file-linux-amd64-2023-03-09.tar.gz", - "another-file-linux-amd64-2023-03-09.tar.gz" - ) - assert.spy(std.unpack).was_called(2) - assert.spy(std.unpack).was_called_with "file-linux-amd64-2023-03-09.tar.gz" - assert.spy(std.unpack).was_called_with "another-file-linux-amd64-2023-03-09.tar.gz" - end) - - it("should install github release assets into specified output directory", function() - local ctx = create_dummy_context() - local std = require "mason-core.installer.managers.std" - local download_file_cwd, unpack_cwd - stub(std, "download_file", function() - download_file_cwd = ctx.cwd:get() - return Result.success() - end) - stub(std, "unpack", function() - unpack_cwd = ctx.cwd:get() - return Result.success() - end) - stub(ctx.fs, "mkdirp") - spy.on(ctx, "chdir") - - local result = installer.exec_in_context(ctx, function() - return github.install(ctx, { - repo = "namespace/name", - asset = { - file = "file.zip", - }, - downloads = { - { - out_file = "out/dir/file.zip", - download_url = "https://github.com/namespace/name/releases/download/2023-03-09/file.zip", - }, - }, - }) - end) - - assert.is_true(result:is_success()) - assert.spy(ctx.fs.mkdirp).was_called(1) - assert.spy(ctx.fs.mkdirp).was_called_with(match.is_ref(ctx.fs), "out/dir") - assert.spy(ctx.chdir).was_called(1) - assert.spy(ctx.chdir).was_called_with(match.is_ref(ctx), "out/dir", match.is_function()) - assert.spy(std.download_file).was_called(1) - assert.is_true(match.matches "out/dir$"(download_file_cwd)) - assert - .spy(std.download_file) - .was_called_with("https://github.com/namespace/name/releases/download/2023-03-09/file.zip", "file.zip") - assert.spy(std.unpack).was_called(1) - assert.is_true(match.matches "out/dir$"(unpack_cwd)) - assert.spy(std.unpack).was_called_with "file.zip" + assert.spy(common.download_files).was_called(1) + assert.spy(common.download_files).was_called_with(match.is_ref(ctx), { + { + out_file = "file-linux-amd64-2023-03-09.tar.gz", + download_url = "https://github.com/namespace/name/releases/download/2023-03-09/file-linux-amd64-2023-03-09.tar.gz", + }, + { + out_file = "another-file-linux-amd64-2023-03-09.tar.gz", + download_url = "https://github.com/namespace/name/releases/download/2023-03-09/another-file-linux-amd64-2023-03-09.tar.gz", + }, + }) end) end) diff --git a/tests/mason-core/installer/registry/providers/openvsx_spec.lua b/tests/mason-core/installer/registry/providers/openvsx_spec.lua new file mode 100644 index 00000000..1452ea0f --- /dev/null +++ b/tests/mason-core/installer/registry/providers/openvsx_spec.lua @@ -0,0 +1,149 @@ +local Purl = require "mason-core.purl" +local Result = require "mason-core.result" +local common = require "mason-core.installer.managers.common" +local installer = require "mason-core.installer" +local match = require "luassert.match" +local openvsx = require "mason-core.installer.registry.providers.openvsx" +local stub = require "luassert.stub" + +---@param overrides Purl +local function purl(overrides) + local purl = Purl.parse("pkg:openvsx/namespace/name@1.10.1"):get_or_throw() + if not overrides then + return purl + end + return vim.tbl_deep_extend("force", purl, overrides) +end + +describe("openvsx provider :: download :: parsing", function() + it("should parse download source", function() + assert.same( + Result.success { + download = { + file = "file-1.10.1.jar", + }, + downloads = { + { + out_file = "file-1.10.1.jar", + download_url = "https://open-vsx.org/api/namespace/name/1.10.1/file/file-1.10.1.jar", + }, + }, + }, + openvsx.parse({ + download = { + file = "file-{{version}}.jar", + }, + }, purl()) + ) + end) + + it("should parse download source with multiple targets", function() + assert.same( + Result.success { + download = { + target = "linux_x64", + file = "file-linux-amd64-1.0.0.vsix", + }, + downloads = { + { + out_file = "file-linux-amd64-1.0.0.vsix", + download_url = "https://open-vsx.org/api/namespace/name/1.0.0/file/file-linux-amd64-1.0.0.vsix", + }, + }, + }, + openvsx.parse({ + download = { + { + target = "win_arm", + file = "file-win-arm-{{version}}.vsix", + }, + { + target = "linux_x64", + file = "file-linux-amd64-{{version}}.vsix", + }, + }, + }, purl { version = "1.0.0" }, { target = "linux_x64" }) + ) + end) + + it("should parse download source with output to different directory", function() + assert.same( + Result.success { + download = { + file = "out-dir/file-linux-amd64-1.10.1.vsix", + }, + downloads = { + { + out_file = "out-dir/file-linux-amd64-1.10.1.vsix", + download_url = "https://open-vsx.org/api/namespace/name/1.10.1/file/file-linux-amd64-1.10.1.vsix", + }, + }, + }, + openvsx.parse({ + download = { + file = "file-linux-amd64-{{version}}.vsix:out-dir/", + }, + }, purl(), { target = "linux_x64" }) + ) + end) + + it("should recognize target_platform when available", function() + assert.same( + Result.success { + download = { + file = "file-linux-1.10.1@win32-arm64.vsix", + target = "win_arm64", + target_platform = "win32-arm64", + }, + downloads = { + { + out_file = "file-linux-1.10.1@win32-arm64.vsix", + download_url = "https://open-vsx.org/api/namespace/name/win32-arm64/1.10.1/file/file-linux-1.10.1@win32-arm64.vsix", + }, + }, + }, + openvsx.parse({ + download = { + { + target = "win_arm64", + file = "file-linux-{{version}}@win32-arm64.vsix", + target_platform = "win32-arm64", + }, + }, + }, purl(), { target = "win_arm64" }) + ) + end) +end) + +describe("openvsx provider :: download :: installing", function() + it("should install openvsx assets", function() + local ctx = create_dummy_context() + local std = require "mason-core.installer.managers.std" + stub(std, "download_file", mockx.returns(Result.success())) + stub(std, "unpack", mockx.returns(Result.success())) + stub(common, "download_files", mockx.returns(Result.success())) + + local result = installer.exec_in_context(ctx, function() + return openvsx.install(ctx, { + download = { + file = "file-1.10.1.jar", + }, + downloads = { + { + out_file = "file-1.10.1.jar", + download_url = "https://open-vsx.org/api/namespace/name/1.10.1/file/file-1.10.1.jar", + }, + }, + }) + end) + + assert.is_true(result:is_success()) + assert.spy(common.download_files).was_called(1) + assert.spy(common.download_files).was_called_with(match.is_ref(ctx), { + { + out_file = "file-1.10.1.jar", + download_url = "https://open-vsx.org/api/namespace/name/1.10.1/file/file-1.10.1.jar", + }, + }) + end) +end) diff --git a/tests/mason-core/installer/registry/util_spec.lua b/tests/mason-core/installer/registry/util_spec.lua index 09918943..851164d0 100644 --- a/tests/mason-core/installer/registry/util_spec.lua +++ b/tests/mason-core/installer/registry/util_spec.lua @@ -7,7 +7,7 @@ local util = require "mason-core.installer.registry.util" describe("registry installer util", function() it("should coalesce single target", function() local source = { value = "here" } - local coalesced = util.coalesce_by_target(source, {}):get() + local coalesced = util.coalesce_by_target(source, {}):get_or_nil() assert.is_true(match.is_ref(source)(coalesced)) end) @@ -19,7 +19,7 @@ describe("registry installer util", function() value = "here", }, source, - }, { target = "VIC64" }):get() + }, { target = "VIC64" }):get_or_nil() assert.is_true(match.is_ref(source)(coalesced)) end) |
