diff options
| author | Nicole Mazzuca <t-nimaz@microsoft.com> | 2019-07-19 12:56:24 -0700 |
|---|---|---|
| committer | Nicole Mazzuca <t-nimaz@microsoft.com> | 2019-07-19 12:56:24 -0700 |
| commit | c55ea0a0d5229b9dd79aa8ea888f6c0408f9e5dd (patch) | |
| tree | 7b68d22558993584cab96d4f162aca454130fc74 | |
| parent | b3caf67749f21952e0157ba77ece755aa01b254a (diff) | |
| download | vcpkg-c55ea0a0d5229b9dd79aa8ea888f6c0408f9e5dd.tar.gz vcpkg-c55ea0a0d5229b9dd79aa8ea888f6c0408f9e5dd.zip | |
switch to new test framework
| -rw-r--r-- | toolsrc/CMakeLists.txt | 6 | ||||
| -rw-r--r-- | toolsrc/include/vcpkg-tests/util.h | 5 | ||||
| -rw-r--r-- | toolsrc/src/tests.files.cpp | 158 | ||||
| -rw-r--r-- | toolsrc/src/tests.strings.cpp | 41 | ||||
| -rw-r--r-- | toolsrc/src/vcpkg-tests/files.cpp | 124 | ||||
| -rw-r--r-- | toolsrc/src/vcpkg-tests/strings.cpp | 33 | ||||
| -rw-r--r-- | toolsrc/src/vcpkg-tests/util.cpp | 54 | ||||
| -rw-r--r-- | toolsrc/src/vcpkg/build.cpp | 48 | ||||
| -rw-r--r-- | toolsrc/vcpkgtest/vcpkgtest.vcxproj | 2 |
9 files changed, 223 insertions, 248 deletions
diff --git a/toolsrc/CMakeLists.txt b/toolsrc/CMakeLists.txt index ca23db413..6697a919e 100644 --- a/toolsrc/CMakeLists.txt +++ b/toolsrc/CMakeLists.txt @@ -52,7 +52,11 @@ add_executable(vcpkg-test EXCLUDE_FROM_ALL ${VCPKGTEST_SOURCES} ${VCPKGLIB_SOURC target_compile_definitions(vcpkg-test PRIVATE -DDISABLE_METRICS=${DISABLE_METRICS_VALUE})
target_include_directories(vcpkg-test PRIVATE include)
-foreach(TEST_NAME arguments chrono dependencies paragraph plan specifier supports)
+foreach(TEST_NAME
+ arguments chrono dependencies files
+ paragraph plan specifier statusparagraphs
+ strings supports update
+)
add_test(${TEST_NAME} vcpkg-test [${TEST_NAME}])
endforeach()
diff --git a/toolsrc/include/vcpkg-tests/util.h b/toolsrc/include/vcpkg-tests/util.h index fe4ee0eec..8a0486285 100644 --- a/toolsrc/include/vcpkg-tests/util.h +++ b/toolsrc/include/vcpkg-tests/util.h @@ -1,3 +1,4 @@ +#include <vcpkg/base/files.h> #include <vcpkg/statusparagraph.h> #include <memory> @@ -30,4 +31,8 @@ T&& unwrap(vcpkg::Optional<T>&& opt) return std::move(*opt.get()); } +extern const bool SYMLINKS_ALLOWED; + +extern const fs::path TEMPORARY_DIRECTORY; + } diff --git a/toolsrc/src/tests.files.cpp b/toolsrc/src/tests.files.cpp deleted file mode 100644 index 482675c34..000000000 --- a/toolsrc/src/tests.files.cpp +++ /dev/null @@ -1,158 +0,0 @@ -#include "tests.pch.h" - -#include <vcpkg/base/files.h> -#include <vcpkg/base/strings.h> - -#include <filesystem> // required for filesystem::create_{directory_}symlink -#include <iostream> -#include <random> - -#include <windows.h> - -using namespace Microsoft::VisualStudio::CppUnitTestFramework; - -namespace UnitTest1 -{ - class FilesTest : public TestClass<FilesTest> - { - using uid = std::uniform_int_distribution<std::uint64_t>; - - public: - FilesTest() - { - HKEY key; - const auto status = RegOpenKeyExW( - HKEY_LOCAL_MACHINE, LR"(SOFTWARE\Microsoft\Windows\CurrentVersion\AppModelUnlock)", 0, 0, &key); - - if (status == ERROR_FILE_NOT_FOUND) - { - ALLOW_SYMLINKS = false; - std::clog << "Symlinks are not allowed on this system\n"; - } - else - { - // if we get a permissions error, we still know that we're in developer mode - ALLOW_SYMLINKS = true; - } - - if (status == ERROR_SUCCESS) RegCloseKey(key); - } - - private: - TEST_METHOD(remove_all) - { - auto urbg = get_urbg(0); - - fs::path temp_dir; - - { - wchar_t* tmp = static_cast<wchar_t*>(calloc(32'767, 2)); - - if (!GetEnvironmentVariableW(L"TEMP", tmp, 32'767)) - { - Assert::Fail(L"GetEnvironmentVariable(\"TEMP\") failed"); - } - - temp_dir = tmp; - - std::string dir_name = "vcpkg-tmp-dir-"; - dir_name += get_random_filename(urbg); - - temp_dir /= dir_name; - } - - auto& fs = vcpkg::Files::get_real_filesystem(); - - std::clog << "temp dir is: " << temp_dir << '\n'; - - create_directory_tree(urbg, fs, 0, temp_dir); - - std::error_code ec; - fs::path fp; - fs.remove_all(temp_dir, ec, fp); - Assert::IsFalse(bool(ec)); - - Assert::IsFalse(fs.exists(temp_dir)); - } - - bool ALLOW_SYMLINKS; - - std::mt19937_64 get_urbg(std::uint64_t index) - { - // smallest prime > 2**63 - 1 - return std::mt19937_64{index + 9223372036854775837}; - } - - std::string get_random_filename(std::mt19937_64& urbg) { return vcpkg::Strings::b32_encode(uid{}(urbg)); } - - void create_directory_tree(std::mt19937_64& urbg, - vcpkg::Files::Filesystem& fs, - std::uint64_t depth, - const fs::path& base) - { - std::random_device rd; - constexpr std::uint64_t max_depth = 5; - constexpr std::uint64_t width = 5; - - // we want ~70% of our "files" to be directories, and then a third - // each of the remaining ~30% to be regular files, directory symlinks, - // and regular symlinks - constexpr std::uint64_t directory_min_tag = 0; - constexpr std::uint64_t directory_max_tag = 6; - constexpr std::uint64_t regular_file_tag = 7; - constexpr std::uint64_t regular_symlink_tag = 8; - constexpr std::uint64_t directory_symlink_tag = 9; - - // if we're at the max depth, we only want to build non-directories - std::uint64_t file_type; - if (depth < max_depth) - { - file_type = uid{directory_min_tag, regular_symlink_tag}(urbg); - } - else - { - file_type = uid{regular_file_tag, regular_symlink_tag}(urbg); - } - - if (!ALLOW_SYMLINKS && file_type > regular_file_tag) - { - file_type = regular_file_tag; - } - - std::error_code ec; - if (type <= directory_max_tag) - { - fs.create_directory(base, ec); - Assert::IsFalse(bool(ec)); - - for (int i = 0; i < width; ++i) - { - create_directory_tree(urbg, fs, depth + 1, base / get_random_filename(urbg)); - } - } - else if (type == regular_file_tag) - { - // regular file - fs.write_contents(base, "", ec); - } - else if (type == regular_symlink_tag) - { - // regular symlink - fs.write_contents(base, "", ec); - Assert::IsFalse(bool(ec)); - const std::filesystem::path basep = base.native(); - auto basep_link = basep; - basep_link.replace_filename(basep.filename().native() + L"-link"); - std::filesystem::create_symlink(basep, basep_link, ec); - } - else // type == directory_symlink_tag - { - // directory symlink - std::filesystem::path basep = base.native(); - std::filesystem::create_directory_symlink(basep / "..", basep, ec); - } - - Assert::IsFalse(bool(ec)); - } - }; -} diff --git a/toolsrc/src/tests.strings.cpp b/toolsrc/src/tests.strings.cpp deleted file mode 100644 index f541a203d..000000000 --- a/toolsrc/src/tests.strings.cpp +++ /dev/null @@ -1,41 +0,0 @@ -#include "tests.pch.h" - -#include <vcpkg/base/strings.h> - -#include <cstdint> -#include <utility> -#include <vector> - -using namespace Microsoft::VisualStudio::CppUnitTestFramework; - -namespace UnitTest1 -{ - class StringsTest : public TestClass<StringsTest> - { - TEST_METHOD(b64url_encode) - { - using u64 = std::uint64_t; - - std::vector<std::pair<std::uint64_t, std::string>> map; - - map.emplace_back(0, "AAAAAAAAAAAAA"); - map.emplace_back(1, "BAAAAAAAAAAAA"); - - map.emplace_back(u64(1) << 32, "AAAAAAEAAAAAA"); - map.emplace_back((u64(1) << 32) + 1, "BAAAAAEAAAAAA"); - - map.emplace_back(0xE4D0'1065'D11E'0229, "JRA4RIXMQAUJO"); - map.emplace_back(0xA626'FE45'B135'07FF, "77BKTYWI6XJMK"); - map.emplace_back(0xEE36'D228'0C31'D405, "FAVDDGAFSWN4O"); - map.emplace_back(0x1405'64E7'FE7E'A88C, "MEK5H774ELBIB"); - map.emplace_back(0xFFFF'FFFF'FFFF'FFFF, "777777777777P"); - - std::string result; - for (const auto& pr : map) - { - result = vcpkg::Strings::b32_encode(pr.first); - Assert::AreEqual(result, pr.second); - } - } - }; -} diff --git a/toolsrc/src/vcpkg-tests/files.cpp b/toolsrc/src/vcpkg-tests/files.cpp new file mode 100644 index 000000000..d2edffcff --- /dev/null +++ b/toolsrc/src/vcpkg-tests/files.cpp @@ -0,0 +1,124 @@ +#include <vcpkg-tests/catch.h> +#include <vcpkg-tests/util.h> + +#include <vcpkg/base/files.h> +#include <vcpkg/base/strings.h> + +#include <filesystem> // required for filesystem::create_{directory_}symlink +#include <iostream> +#include <random> + +#include <vector> + +using vcpkg::Test::SYMLINKS_ALLOWED; +using vcpkg::Test::TEMPORARY_DIRECTORY; + +namespace +{ + using uid = std::uniform_int_distribution<std::uint64_t>; + + std::mt19937_64 get_urbg(std::uint64_t index) + { + // smallest prime > 2**63 - 1 + return std::mt19937_64{index + 9223372036854775837}; + } + + std::string get_random_filename(std::mt19937_64& urbg) { return vcpkg::Strings::b32_encode(uid{}(urbg)); } + + void create_directory_tree(std::mt19937_64& urbg, + vcpkg::Files::Filesystem& fs, + std::uint64_t depth, + const fs::path& base) + { + std::random_device rd; + constexpr std::uint64_t max_depth = 5; + constexpr std::uint64_t width = 5; + + // we want ~70% of our "files" to be directories, and then a third + // each of the remaining ~30% to be regular files, directory symlinks, + // and regular symlinks + constexpr std::uint64_t directory_min_tag = 0; + constexpr std::uint64_t directory_max_tag = 6; + constexpr std::uint64_t regular_file_tag = 7; + constexpr std::uint64_t regular_symlink_tag = 8; + constexpr std::uint64_t directory_symlink_tag = 9; + + // if we're at the max depth, we only want to build non-directories + std::uint64_t file_type; + if (depth < max_depth) + { + file_type = uid{directory_min_tag, regular_symlink_tag}(urbg); + } + else + { + file_type = uid{regular_file_tag, regular_symlink_tag}(urbg); + } + + if (!SYMLINKS_ALLOWED && file_type > regular_file_tag) + { + file_type = regular_file_tag; + } + + std::error_code ec; + if (file_type <= directory_max_tag) + { + fs.create_directory(base, ec); + if (ec) { + INFO("File that failed: " << base); + REQUIRE_FALSE(ec); + } + + for (int i = 0; i < width; ++i) + { + create_directory_tree(urbg, fs, depth + 1, base / get_random_filename(urbg)); + } + } + else if (file_type == regular_file_tag) + { + // regular file + fs.write_contents(base, "", ec); + } + else if (file_type == regular_symlink_tag) + { + // regular symlink + fs.write_contents(base, "", ec); + REQUIRE_FALSE(ec); + const std::filesystem::path basep = base.native(); + auto basep_link = basep; + basep_link.replace_filename(basep.filename().native() + L"-link"); + std::filesystem::create_symlink(basep, basep_link, ec); + } + else // type == directory_symlink_tag + { + // directory symlink + std::filesystem::path basep = base.native(); + std::filesystem::create_directory_symlink(basep / "..", basep, ec); + } + + REQUIRE_FALSE(ec); + } +} + +TEST_CASE ("remove all", "[files]") +{ + auto urbg = get_urbg(0); + + fs::path temp_dir = TEMPORARY_DIRECTORY / get_random_filename(urbg); + + auto& fs = vcpkg::Files::get_real_filesystem(); + + std::error_code ec; + fs.create_directory(TEMPORARY_DIRECTORY, ec); + + REQUIRE_FALSE(ec); + + INFO("temp dir is: " << temp_dir); + + create_directory_tree(urbg, fs, 0, temp_dir); + + fs::path fp; + fs.remove_all(temp_dir, ec, fp); + REQUIRE_FALSE(ec); + + REQUIRE_FALSE(fs.exists(temp_dir)); +} diff --git a/toolsrc/src/vcpkg-tests/strings.cpp b/toolsrc/src/vcpkg-tests/strings.cpp new file mode 100644 index 000000000..3168a7c95 --- /dev/null +++ b/toolsrc/src/vcpkg-tests/strings.cpp @@ -0,0 +1,33 @@ +#include <vcpkg-tests/catch.h> + +#include <vcpkg/base/strings.h> + +#include <cstdint> +#include <utility> +#include <vector> + +TEST_CASE ("b32 encoding", "[strings]") +{ + using u64 = std::uint64_t; + + std::vector<std::pair<std::uint64_t, std::string>> map; + + map.emplace_back(0, "AAAAAAAAAAAAA"); + map.emplace_back(1, "BAAAAAAAAAAAA"); + + map.emplace_back(u64(1) << 32, "AAAAAAEAAAAAA"); + map.emplace_back((u64(1) << 32) + 1, "BAAAAAEAAAAAA"); + + map.emplace_back(0xE4D0'1065'D11E'0229, "JRA4RIXMQAUJO"); + map.emplace_back(0xA626'FE45'B135'07FF, "77BKTYWI6XJMK"); + map.emplace_back(0xEE36'D228'0C31'D405, "FAVDDGAFSWN4O"); + map.emplace_back(0x1405'64E7'FE7E'A88C, "MEK5H774ELBIB"); + map.emplace_back(0xFFFF'FFFF'FFFF'FFFF, "777777777777P"); + + std::string result; + for (const auto& pr : map) + { + result = vcpkg::Strings::b32_encode(pr.first); + REQUIRE(vcpkg::Strings::b32_encode(pr.first) == pr.second); + } +} diff --git a/toolsrc/src/vcpkg-tests/util.cpp b/toolsrc/src/vcpkg-tests/util.cpp index 54102f1aa..f14628379 100644 --- a/toolsrc/src/vcpkg-tests/util.cpp +++ b/toolsrc/src/vcpkg-tests/util.cpp @@ -1,10 +1,16 @@ #include <vcpkg-tests/catch.h> #include <vcpkg-tests/util.h> +#include <vcpkg/base/files.h> #include <vcpkg/statusparagraph.h> +#include <iostream> #include <memory> +#if defined(_WIN32) +#include <windows.h> +#endif + namespace vcpkg::Test { std::unique_ptr<vcpkg::StatusParagraph> make_status_pgh(const char* name, @@ -44,4 +50,52 @@ namespace vcpkg::Test return m_ret.value_or_exit(VCPKG_LINE_INFO); } + + #if defined(_WIN32) + + static bool system_allows_symlinks() { + HKEY key; + bool allow_symlinks = true; + + const auto status = RegOpenKeyExW( + HKEY_LOCAL_MACHINE, LR"(SOFTWARE\Microsoft\Windows\CurrentVersion\AppModelUnlock)", 0, 0, &key); + + if (status == ERROR_FILE_NOT_FOUND) + { + allow_symlinks = false; + std::clog << "Symlinks are not allowed on this system\n"; + } + + if (status == ERROR_SUCCESS) RegCloseKey(key); + + return allow_symlinks; + } + + static fs::path internal_temporary_directory() { + wchar_t* tmp = static_cast<wchar_t*>(std::calloc(32'767, 2)); + + if (!GetEnvironmentVariableW(L"TEMP", tmp, 32'767)) { + std::cerr << "No temporary directory found.\n"; + std::abort(); + } + + fs::path result = tmp; + std::free(tmp); + + return result / L"vcpkg-test"; + } + + #else + + constexpr static bool system_allows_symlinks() { + return true; + } + static fs::path internal_temporary_directory() { + return "/tmp/vcpkg-test"; + } + + #endif + + const bool SYMLINKS_ALLOWED = system_allows_symlinks(); + const fs::path TEMPORARY_DIRECTORY = internal_temporary_directory(); } diff --git a/toolsrc/src/vcpkg/build.cpp b/toolsrc/src/vcpkg/build.cpp index d9305b271..235adb819 100644 --- a/toolsrc/src/vcpkg/build.cpp +++ b/toolsrc/src/vcpkg/build.cpp @@ -1097,56 +1097,8 @@ namespace vcpkg::Build }
}
-<<<<<<< HEAD
- pre_build_info.triplet_abi_tag = [&]() {
- const auto& fs = paths.get_filesystem();
- static std::map<fs::path, std::string> s_hash_cache;
-
- auto it_hash = s_hash_cache.find(triplet_file_path);
- if (it_hash != s_hash_cache.end())
- {
- return it_hash->second;
- }
- auto hash = Hash::get_file_hash(fs, triplet_file_path, "SHA1");
-
- if (auto p = pre_build_info.external_toolchain_file.get())
- {
- hash += "-";
- hash += Hash::get_file_hash(fs, *p, "SHA1");
- }
- else if (pre_build_info.cmake_system_name.empty() || pre_build_info.cmake_system_name == "WindowsStore")
- {
- hash += "-";
- hash += Hash::get_file_hash(fs, paths.scripts / "toolchains" / "windows.cmake", "SHA1");
- }
- else if (pre_build_info.cmake_system_name == "Linux")
- {
- hash += "-";
- hash += Hash::get_file_hash(fs, paths.scripts / "toolchains" / "linux.cmake", "SHA1");
- }
- else if (pre_build_info.cmake_system_name == "Darwin")
- {
- hash += "-";
- hash += Hash::get_file_hash(fs, paths.scripts / "toolchains" / "osx.cmake", "SHA1");
- }
- else if (pre_build_info.cmake_system_name == "FreeBSD")
- {
- hash += "-";
- hash += Hash::get_file_hash(fs, paths.scripts / "toolchains" / "freebsd.cmake", "SHA1");
- }
- else if (pre_build_info.cmake_system_name == "Android")
- {
- hash += "-";
- hash += Hash::get_file_hash(fs, paths.scripts / "toolchains" / "android.cmake", "SHA1");
- }
-
- s_hash_cache.emplace(triplet_file_path, hash);
- return hash;
- }();
-=======
pre_build_info.triplet_abi_tag =
get_triplet_abi(paths, pre_build_info, triplet);
->>>>>>> trunk
return pre_build_info;
}
diff --git a/toolsrc/vcpkgtest/vcpkgtest.vcxproj b/toolsrc/vcpkgtest/vcpkgtest.vcxproj index 4e385f376..cf411d591 100644 --- a/toolsrc/vcpkgtest/vcpkgtest.vcxproj +++ b/toolsrc/vcpkgtest/vcpkgtest.vcxproj @@ -23,10 +23,12 @@ <ClCompile Include="..\src\vcpkg-tests\catch.cpp" />
<ClCompile Include="..\src\vcpkg-tests\chrono.cpp" />
<ClCompile Include="..\src\vcpkg-tests\dependencies.cpp" />
+ <ClCompile Include="..\src\vcpkg-tests\files.cpp" />
<ClCompile Include="..\src\vcpkg-tests\paragraph.cpp" />
<ClCompile Include="..\src\vcpkg-tests\plan.cpp" />
<ClCompile Include="..\src\vcpkg-tests\specifier.cpp" />
<ClCompile Include="..\src\vcpkg-tests\statusparagraphs.cpp" />
+ <ClCompile Include="..\src\vcpkg-tests\strings.cpp" />
<ClCompile Include="..\src\vcpkg-tests\supports.cpp" />
<ClCompile Include="..\src\vcpkg-tests\update.cpp" />
<ClCompile Include="..\src\vcpkg-tests\util.cpp" />
|
