diff options
| author | William Boman <william@redwill.se> | 2022-03-26 13:41:50 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-03-26 13:41:50 +0100 |
| commit | 212d17a039da449043b67529c29851db37acc236 (patch) | |
| tree | 38411b14487895cef0d7648e198b79fd28793fe6 /tests/core/managers/git_spec.lua | |
| parent | run autogen_metadata.lua (diff) | |
| download | mason-212d17a039da449043b67529c29851db37acc236.tar mason-212d17a039da449043b67529c29851db37acc236.tar.gz mason-212d17a039da449043b67529c29851db37acc236.tar.bz2 mason-212d17a039da449043b67529c29851db37acc236.tar.lz mason-212d17a039da449043b67529c29851db37acc236.tar.xz mason-212d17a039da449043b67529c29851db37acc236.tar.zst mason-212d17a039da449043b67529c29851db37acc236.zip | |
add async managers (#536)
Diffstat (limited to 'tests/core/managers/git_spec.lua')
| -rw-r--r-- | tests/core/managers/git_spec.lua | 179 |
1 files changed, 179 insertions, 0 deletions
diff --git a/tests/core/managers/git_spec.lua b/tests/core/managers/git_spec.lua new file mode 100644 index 00000000..2f6c6ace --- /dev/null +++ b/tests/core/managers/git_spec.lua @@ -0,0 +1,179 @@ +local spy = require "luassert.spy" +local mock = require "luassert.mock" +local spawn = require "nvim-lsp-installer.core.spawn" +local Result = require "nvim-lsp-installer.core.result" + +local git = require "nvim-lsp-installer.core.managers.git" +local Optional = require "nvim-lsp-installer.core.optional" + +describe("git manager", function() + ---@type InstallContext + local ctx + before_each(function() + ctx = InstallContextGenerator { + spawn = mock.new { + git = mockx.returns {}, + }, + } + end) + + it( + "should fail if no git repo provided", + async_test(function() + local err = assert.has_errors(function() + git.clone {}(ctx) + end) + assert.equals("No git URL provided.", err) + assert.spy(ctx.spawn.git).was_not_called() + end) + ) + + it( + "should clone provided repo", + async_test(function() + git.clone { "https://github.com/williamboman/nvim-lsp-installer.git" }(ctx) + assert.spy(ctx.spawn.git).was_called(1) + assert.spy(ctx.spawn.git).was_called_with { + "clone", + "--depth", + "1", + "https://github.com/williamboman/nvim-lsp-installer.git", + ".", + } + end) + ) + + it( + "should fetch and checkout revision if requested", + async_test(function() + ctx.requested_version = Optional.of "1337" + git.clone { "https://github.com/williamboman/nvim-lsp-installer.git" }(ctx) + assert.spy(ctx.spawn.git).was_called(3) + assert.spy(ctx.spawn.git).was_called_with { + "clone", + "--depth", + "1", + "https://github.com/williamboman/nvim-lsp-installer.git", + ".", + } + assert.spy(ctx.spawn.git).was_called_with { + "fetch", + "--depth", + "1", + "origin", + "1337", + } + assert.spy(ctx.spawn.git).was_called_with { "checkout", "FETCH_HEAD" } + end) + ) + + it( + "should provide receipt information", + async_test(function() + git.clone { "https://github.com/williamboman/nvim-lsp-installer.git" }(ctx) + assert.equals( + vim.inspect { + type = "git", + remote = "https://github.com/williamboman/nvim-lsp-installer.git", + }, + vim.inspect(ctx.receipt.primary_source) + ) + assert.is_true(#ctx.receipt.secondary_sources == 0) + end) + ) +end) + +describe("git version check", function() + it( + "should return current version", + async_test(function() + spawn.git = spy.new(function() + return Result.success { + stdout = [[19c668c]], + } + end) + + local result = git.get_installed_revision "/tmp/install/dir" + + assert.spy(spawn.git).was_called(1) + assert.spy(spawn.git).was_called_with { "rev-parse", "--short", "HEAD", cwd = "/tmp/install/dir" } + assert.is_true(result:is_success()) + assert.equals("19c668c", result:get_or_nil()) + + spawn.git = nil + end) + ) + + it( + "should check for outdated git clone", + async_test(function() + spawn.git = spy.new(function() + return Result.success { + stdout = [[728307a74cd5f2dec7ca2ca164785c25673d6328 +19c668cd10695b243b09452f0dfd53570c1a2e7d]], + } + end) + + local result = git.check_outdated_git_clone( + mock.new { + primary_source = mock.new { + type = "git", + remote = "https://github.com/williamboman/nvim-lsp-installer.git", + }, + }, + "/tmp/install/dir" + ) + + assert.spy(spawn.git).was_called(2) + assert.spy(spawn.git).was_called_with { + "fetch", + "origin", + "HEAD", + cwd = "/tmp/install/dir", + } + assert.spy(spawn.git).was_called_with { + "rev-parse", + "FETCH_HEAD", + "HEAD", + cwd = "/tmp/install/dir", + } + assert.is_true(result:is_success()) + assert.equals( + vim.inspect { + name = "https://github.com/williamboman/nvim-lsp-installer.git", + current_version = "19c668cd10695b243b09452f0dfd53570c1a2e7d", + latest_version = "728307a74cd5f2dec7ca2ca164785c25673d6328", + }, + vim.inspect(result:get_or_nil()) + ) + + spawn.git = nil + end) + ) + + it( + "should return failure if clone is not outdated", + async_test(function() + spawn.git = spy.new(function() + return Result.success { + stdout = [[19c668cd10695b243b09452f0dfd53570c1a2e7d +19c668cd10695b243b09452f0dfd53570c1a2e7d]], + } + end) + + local result = git.check_outdated_git_clone( + mock.new { + primary_source = mock.new { + type = "git", + remote = "https://github.com/williamboman/nvim-lsp-installer.git", + }, + }, + "/tmp/install/dir" + ) + + assert.is_true(result:is_failure()) + assert.equals("Git clone is up to date.", result:err_or_nil()) + spawn.git = nil + end) + ) +end) |
