Add a version comparison call with tests

This commit is contained in:
Jason Perkins 2015-06-19 16:48:45 -04:00 committed by Damien Courtois
parent 8574e25f9d
commit c3c288a139
3 changed files with 242 additions and 3 deletions

View File

@ -5,9 +5,10 @@
premake = premake or {}
premake.modules = {}
premake.extensions = premake.modules
local p = premake
-- Keep track of warnings that have been shown, so they don't get shown twice
@ -86,10 +87,76 @@
---
-- Clears the list of already fired warning messages, allowing them
-- to be fired again.
-- Compare a version string of the form "major.minor.patch.dev" against a
-- version comparision string. Comparisions take the form of ">=5.0" (5.0 or
-- later), "5.0" (5.0 or later), ">=5.0 <6.0" (5.0 or later but not 6.0 or
-- later).
--
-- @param version
-- The version to be tested.
-- @param checks
-- The comparision string to be evaluated.
-- @return
-- True if the comparisions pass, false if any fail.
---
function p.checkVersion(version, checks)
local function parse(str)
local major, minor, patch, dev = str:match("^(%d+)%.?(%d*)%.?(%d*)(.-)$")
major = tonumber(major) or 0
minor = tonumber(minor) or 0
patch = tonumber(patch) or 0
dev = dev or ""
return { major, minor, patch, dev }
end
local function compare(a, b)
for i=1,4 do
if a[i] > b[i] then return 1 end
if a[i] < b[i] then return -1 end
end
return 0
end
local function eq(r) return r == 0 end
local function le(r) return r <= 0 end
local function lt(r) return r < 0 end
local function ge(r) return r >= 0 end
local function gt(r) return r > 0 end
version = parse(version)
checks = string.explode(checks, " ", true)
for i = 1, #checks do
local check = checks[i]
local func
if check:startswith(">=") then
func = ge
check = check:sub(3)
elseif check:startswith(">") then
func = gt
check = check:sub(2)
elseif check:startswith("<=") then
func = le
check = check:sub(3)
elseif check:startswith("<") then
func = lt
check = check:sub(2)
else
func = ge
end
check = parse(check)
if not func(compare(version, check)) then
return false
end
end
return true
end
function premake.clearWarnings()
_warnings = {}
end

View File

@ -15,6 +15,7 @@ return {
"base/test_table.lua",
"base/test_tree.lua",
"base/test_uuid.lua",
"base/test_versions.lua",
-- Solution object tests
"solution/test_eachconfig.lua",

View File

@ -0,0 +1,171 @@
--
-- tests/base/test_versions.lua
-- Verify the version comparisons.
-- Copyright (c) 2015 Jason Perkins and the Premake project
--
local suite = test.declare("premake_versions")
local p = premake
--
-- If only major version is specified, anything after should pass.
--
function suite.pass_majorOnly_sameMajor()
test.istrue(p.checkVersion("1.0.0", "1"))
end
function suite.pass_majorOnly_laterMajor()
test.istrue(p.checkVersion("2.0.0", "1"))
end
function suite.pass_majorOnly_laterMinor()
test.istrue(p.checkVersion("1.1.0", "1"))
end
function suite.pass_majorOnly_laterPatch()
test.istrue(p.checkVersion("1.0.1", "1"))
end
function suite.pass_majorOnly_alpha()
test.istrue(p.checkVersion("1.0.0.alpha1", "1"))
end
function suite.pass_majorOnly_dev()
test.istrue(p.checkVersion("1.0.0.dev", "1"))
end
function suite.fail_earlierMajor()
test.isfalse(p.checkVersion("0.9.0", "1"))
end
--
-- If major and minor are specified, anything after should pass
--
function suite.pass_majorMinor_sameMajorMinor()
test.istrue(p.checkVersion("1.1.0", "1.1"))
end
function suite.pass_majorMinor_sameMajorLaterMinor()
test.istrue(p.checkVersion("1.2.0", "1.1"))
end
function suite.pass_majorMinor_sameMajorLaterPath()
test.istrue(p.checkVersion("1.1.1", "1.1"))
end
function suite.pass_majorMinor_laterMajorSameMinor()
test.istrue(p.checkVersion("2.0.0", "1.1"))
end
function suite.pass_majorMinor_laterMajorEarlierMinor()
test.istrue(p.checkVersion("2.0.0", "1.1"))
end
function suite.pass_majorMinor_laterMajorLaterMinor()
test.istrue(p.checkVersion("2.2.0", "1.1"))
end
function suite.fail_majorMinor_sameMajorEarlierMinor()
test.isfalse(p.checkVersion("1.0.0", "1.1"))
end
function suite.fail_majorMinor_earlierMajor()
test.isfalse(p.checkVersion("0.9.0", "1.1"))
end
--
-- Alpha comes before beta comes before dev
--
function suite.pass_alphaBeforeBeta()
test.istrue(p.checkVersion("1.0.0.beta1", "1.0.0.alpha1"))
end
function suite.fail_alphaBeforeBeta()
test.isfalse(p.checkVersion("1.0.0.alpha1", "1.0.0.beta1"))
end
function suite.pass_betaBeforeDev()
test.istrue(p.checkVersion("1.0.0.dev", "1.0.0.beta1"))
end
function suite.fail_betaBeforeDev()
test.isfalse(p.checkVersion("1.0.0.beta1", "1.0.0.dev"))
end
--
-- Check ">=" operator
--
function suite.pass_ge_sameMajorMinorPatch()
test.istrue(p.checkVersion("1.1.0", ">=1.1"))
end
function suite.pass_ge_sameMajorMinorLaterPatch()
test.istrue(p.checkVersion("1.1.1", ">=1.1"))
end
function suite.pass_ge_laterMajorEarlierMinor()
test.istrue(p.checkVersion("2.0.1", ">=1.1"))
end
function suite.pass_ge_sameMajorLaterMinor()
test.istrue(p.checkVersion("1.2.1", ">=1.1"))
end
function suite.fail_ge_earlierMajor()
test.isfalse(p.checkVersion("0.1.1", ">=1.1"))
end
function suite.fail_ge_earlierMinor()
test.isfalse(p.checkVersion("1.0.1", ">=1.1"))
end
--
-- Check ">" operator
--
function suite.pass_gt_sameMajorMinorLaterPatch()
test.istrue(p.checkVersion("1.1.1", ">1.1"))
end
function suite.pass_gt_laterMajor()
test.istrue(p.checkVersion("2.0.1", ">1.1"))
end
function suite.pass_gt_laterMinor()
test.istrue(p.checkVersion("1.2.1", ">1.1"))
end
function suite.fail_gt_sameMajorMinorPatch()
test.isfalse(p.checkVersion("1.1.0", ">1.1"))
end
function suite.fail_gt_earlierMajor()
test.isfalse(p.checkVersion("0.1.1", ">1.1"))
end
function suite.fail_gt_earlierMinor()
test.isfalse(p.checkVersion("1.0.1", ">1.1"))
end
--
-- Check multiple conditions
--
function suite.pass_onMultipleConditions()
test.istrue(p.checkVersion("1.2.0.0", ">=1.0 <2.0"))
end
function suite.fail_onMultipleConditions()
test.isfalse(p.checkVersion("2.2.0.0", ">=1.0 <2.0"))
end