[vcpkg_minimum_required] fix, add unit tests (#19257)

* fix vcpkg_minimum_required

drive-by scripts audit

* initial unit tests

* finish unit tests
This commit is contained in:
nicole mazzuca 2021-07-30 12:57:07 -07:00 коммит произвёл GitHub
Родитель fa1bbe097b
Коммит a5a70c7c3e
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
4 изменённых файлов: 86 добавлений и 22 удалений

Просмотреть файл

@ -15,31 +15,32 @@ The date-version to check against.
function(vcpkg_minimum_required)
cmake_parse_arguments(PARSE_ARGV 0 arg "" "VERSION" "")
if (NOT DEFINED VCPKG_BASE_VERSION)
message(FATAL_ERROR
"Your vcpkg executable is outdated and is not compatible with the current CMake scripts. "
"Please re-acquire vcpkg by running bootstrap-vcpkg."
if(NOT DEFINED VCPKG_BASE_VERSION)
message(FATAL_ERROR "Your vcpkg executable is outdated and is not compatible with the current CMake scripts.
Please re-acquire vcpkg by running bootstrap-vcpkg."
)
endif()
if(NOT DEFINED arg_VERSION)
message(FATAL_ERROR "VERSION must be specified")
endif()
set(vcpkg_date_regex "^[12][0-9][0-9][0-9]-[01][0-9]-[0-3][0-9]$")
if (NOT VCPKG_BASE_VERSION MATCHES "${vcpkg_date_regex}")
if(NOT "${VCPKG_BASE_VERSION}" MATCHES "${vcpkg_date_regex}")
message(FATAL_ERROR
"vcpkg internal failure; VCPKG_BASE_VERSION (${VCPKG_BASE_VERSION}) was not a valid date."
)
)
endif()
if (NOT arg_VERSION MATCHES "${vcpkg_date_regex}")
if(NOT "${arg_VERSION}" MATCHES "${vcpkg_date_regex}")
message(FATAL_ERROR
"VERSION parameter to vcpkg_minimum_required was not a valid date. "
"Comparing with vcpkg tool version ${VCPKG_BASE_VERSION}"
)
"VERSION (${arg_VERSION}) was not a valid date - expected something of the form 'YYYY-MM-DD'"
)
endif()
string(REPLACE "-" "." VCPKG_BASE_VERSION_as_dotted "${VCPKG_BASE_VERSION}")
string(REPLACE "-" "." arg_VERSION_as_dotted "${arg_VERSION}")
if (VCPKG_BASE_VERSION_as_dotted VERSION_LESS vcpkg_VERSION_as_dotted)
if("${VCPKG_BASE_VERSION_as_dotted}" VERSION_LESS "${arg_VERSION_as_dotted}")
message(FATAL_ERROR
"Your vcpkg executable is from ${VCPKG_BASE_VERSION} which is older than required by the caller "
"of vcpkg_minimum_required(VERSION ${arg_VERSION}). "

Просмотреть файл

@ -1,10 +1,9 @@
function(set_fatal_error)
if(ARGC EQUAL 0)
set(Z_VCPKG_UNIT_TEST_HAS_FATAL_ERROR "OFF" CACHE BOOL "" FORCE)
else()
set(Z_VCPKG_UNIT_TEST_HAS_FATAL_ERROR "ON" CACHE BOOL "" FORCE)
set(Z_VCPKG_UNIT_TEST_FATAL_ERROR "${ARGV0}" CACHE STRING "" FORCE)
endif()
function(set_fatal_error err)
set(Z_VCPKG_UNIT_TEST_HAS_FATAL_ERROR "ON" CACHE BOOL "" FORCE)
set(Z_VCPKG_UNIT_TEST_FATAL_ERROR "${err}" CACHE STRING "" FORCE)
endfunction()
function(unset_fatal_error)
set(Z_VCPKG_UNIT_TEST_HAS_FATAL_ERROR "OFF" CACHE BOOL "" FORCE)
endfunction()
function(set_has_error)
set(Z_VCPKG_UNIT_TEST_HAS_ERROR ON CACHE BOOL "" FORCE)
@ -22,12 +21,12 @@ macro(message level msg)
endmacro()
set(Z_VCPKG_UNIT_TEST_HAS_ERROR OFF CACHE BOOL "" FORCE)
set_fatal_error()
unset_fatal_error()
function(unit_test_check_variable_equal utcve_test utcve_variable utcve_value)
cmake_language(EVAL CODE "${utcve_test}")
if(Z_VCPKG_UNIT_TEST_HAS_FATAL_ERROR)
set_fatal_error()
unset_fatal_error()
set_has_error()
message(STATUS "${utcve_test} had an unexpected FATAL_ERROR;
expected: \"${utcve_value}\"")
@ -50,17 +49,28 @@ function(unit_test_check_variable_equal utcve_test utcve_variable utcve_value)
endif()
endfunction()
function(unit_test_ensure_success utcve_test)
cmake_language(EVAL CODE "${utcve_test}")
if(Z_VCPKG_UNIT_TEST_HAS_FATAL_ERROR)
set_has_error()
message(STATUS "${utcve_test} was expected to be successful.")
endif()
unset_fatal_error()
endfunction()
function(unit_test_ensure_fatal_error utcve_test)
cmake_language(EVAL CODE "${utcve_test}")
if(NOT Z_VCPKG_UNIT_TEST_HAS_FATAL_ERROR)
set_has_error()
message(STATUS "${utcve_test} was expected to be a FATAL_ERROR.")
endif()
set_fatal_error()
unset_fatal_error()
endfunction()
set(VCPKG_POLICY_EMPTY_PACKAGE enabled)
if("minimum-required" IN_LIST FEATURES)
include("${CMAKE_CURRENT_LIST_DIR}/test-vcpkg_minimum_required.cmake")
endif()
if("list" IN_LIST FEATURES)
include("${CMAKE_CURRENT_LIST_DIR}/test-vcpkg_list.cmake")
endif()

Просмотреть файл

@ -0,0 +1,49 @@
# -- error cases --
# VCPKG_BASE_VERSION not set - vcpkg version is too old
set(VCPKG_BASE_VERSION_backup "${VCPKG_BASE_VERSION}")
unset(VCPKG_BASE_VERSION)
unset(VCPKG_BASE_VERSION CACHE)
unit_test_ensure_fatal_error([[vcpkg_minimum_required(VERSION 2021-01-01)]])
unit_test_ensure_fatal_error([[vcpkg_minimum_required()]])
unit_test_ensure_fatal_error([[vcpkg_minimum_required(VERSION "")]])
unit_test_ensure_fatal_error([[vcpkg_minimum_required(VERSION "2021.01.01")]])
set(VCPKG_BASE_VERSION 2021-02-02)
# VERSION not passed
unit_test_ensure_fatal_error([[vcpkg_minimum_required()]])
# VERSION weird - empty
unit_test_ensure_fatal_error([[vcpkg_minimum_required(VERSION "")]])
# VERSION weird - dotted
unit_test_ensure_fatal_error([[vcpkg_minimum_required(VERSION 2021.01.01)]])
# VERSION weird - not a valid year
unit_test_ensure_fatal_error([[vcpkg_minimum_required(VERSION 3000-01-01)]])
# VERSION weird - list
unit_test_ensure_fatal_error([[vcpkg_minimum_required(VERSION "2021-01-01;2021-01-02")]])
# VERSION weird - small year
unit_test_ensure_fatal_error([[vcpkg_minimum_required(VERSION 21-01-01)]])
# VERSION weird - small month
unit_test_ensure_fatal_error([[vcpkg_minimum_required(VERSION 2021-1-01)]])
# VERSION weird - small day
unit_test_ensure_fatal_error([[vcpkg_minimum_required(VERSION 2021-01-1)]])
# VERSION too-new - later year, earlier month, earlier day
unit_test_ensure_fatal_error([[vcpkg_minimum_required(VERSION 2022-01-01)]])
# VERSION too-new - same year, later month, earlier day
unit_test_ensure_fatal_error([[vcpkg_minimum_required(VERSION 2021-03-01)]])
# VERSION too-new - same year, same month, later day
unit_test_ensure_fatal_error([[vcpkg_minimum_required(VERSION 2021-02-03)]])
# -- successes --
# same date
unit_test_ensure_success([[vcpkg_minimum_required(VERSION 2021-02-02)]])
# VERSION old - earlier year, later month, later day
unit_test_ensure_success([[vcpkg_minimum_required(VERSION 2020-03-03)]])
# VERSION old - same year, earlier month, later day
unit_test_ensure_success([[vcpkg_minimum_required(VERSION 2021-01-03)]])
# VERSION old - same year, same month, earlier day
unit_test_ensure_success([[vcpkg_minimum_required(VERSION 2021-02-01)]])
# reset to backup
unset(VCPKG_BASE_VERSION)
set(VCPKG_BASE_VERSION "${VCPKG_BASE_VERSION_backup}" CACHE STRING "")

Просмотреть файл

@ -5,7 +5,8 @@
"supports": "x64",
"default-features": [
"function-arguments",
"list"
"list",
"minimum-required"
],
"features": {
"function-arguments": {
@ -13,6 +14,9 @@
},
"list": {
"description": "Test the vcpkg_list function"
},
"minimum-required": {
"description": "Test the vcpkg_minimum_required function"
}
}
}