From e0d3374c30a1f1aab5cb1a258250f66f9e55e04e Mon Sep 17 00:00:00 2001 From: William Boman Date: Fri, 31 May 2024 14:52:20 +0200 Subject: [PATCH] fix: check for empty string and add pep440 spec --- lua/mason/providers/client/pypi.lua | 2 +- lua/mason/providers/registry-api/init.lua | 2 +- tests/mason-core/pep440_spec.lua | 22 ++++++++++++++++++++++ 3 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 tests/mason-core/pep440_spec.lua diff --git a/lua/mason/providers/client/pypi.lua b/lua/mason/providers/client/pypi.lua index 585eb8123..08e4dbaeb 100644 --- a/lua/mason/providers/client/pypi.lua +++ b/lua/mason/providers/client/pypi.lua @@ -57,7 +57,7 @@ return { :map_catching(vim.json.decode) :map(_.path { "info", "requires_python" }) :and_then(function(requires_python) - if type(requires_python) ~= "string" then + if type(requires_python) ~= "string" or requires_python == "" then return Result.failure "Package does not specify supported Python versions." else return Result.success(requires_python) diff --git a/lua/mason/providers/registry-api/init.lua b/lua/mason/providers/registry-api/init.lua index 8999e327a..26e1c0cbf 100644 --- a/lua/mason/providers/registry-api/init.lua +++ b/lua/mason/providers/registry-api/init.lua @@ -38,7 +38,7 @@ return { .get({ package = pkg, version = version }) :map(_.prop "requires_python") :and_then(function(requires_python) - if type(requires_python) ~= "string" then + if type(requires_python) ~= "string" or requires_python == "" then return Result.failure "Package does not specify supported Python versions." else return Result.success(requires_python) diff --git a/tests/mason-core/pep440_spec.lua b/tests/mason-core/pep440_spec.lua new file mode 100644 index 000000000..c8ff98801 --- /dev/null +++ b/tests/mason-core/pep440_spec.lua @@ -0,0 +1,22 @@ +local pep440 = require "mason-core.pep440" + +describe("pep440 version checking", function() + it("should check single version specifier", function() + assert.is_false(pep440.check_version("3.5.0", ">=3.6")) + assert.is_true(pep440.check_version("3.6.0", ">=3.6")) + assert.is_false(pep440.check_version("3.6.0", ">=3.6.1")) + end) + + it("should check version specifier with lower and upper bound", function() + assert.is_true(pep440.check_version("3.8.0", ">=3.8,<3.12")) + assert.is_false(pep440.check_version("3.12.0", ">=3.8,<3.12")) + assert.is_true(pep440.check_version("3.12.0", ">=3.8,<4.0.0")) + end) + + it("should check multiple specifiers with different constraints", function() + assert.is_false(pep440.check_version("3.5.0", "!=4.0,<=4.0,>=3.8")) + assert.is_false(pep440.check_version("4.0.0", "!=4.0,<=4.0,>=3.8")) + assert.is_true(pep440.check_version("3.8.1", "!=4.0,<=4.0,>=3.8")) + assert.is_true(pep440.check_version("3.12.0", "!=4.0,<=4.0,>=3.8")) + end) +end)