Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[regex-ripout] part 2/n #431

Merged
merged 17 commits into from
Mar 16, 2022
83 changes: 83 additions & 0 deletions azure-pipelines/end-to-end-tests-dir/autocomplete.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
. $PSScriptRoot/../end-to-end-tests-prelude.ps1

$publicCommands = @(
'install'
'search'
'remove'
'list'
'update'
'hash'
'help'
'integrate'
'export'
'edit'
'create'
'owns'
'cache'
'version'
'contact'
'upgrade'
)

$privateCommands = @(
'build'
'buildexternal'
'ci'
'depend-info'
'env'
'portsdiff'
)

function getOptionsForPrefix($prefix, $commands) {
$commands | Sort-Object | ? { $_.StartsWith($prefix) }
}
function arraysEqual($arr1, $arr2) {
if ($arr1.Length -ne $arr2.Length) {
$False
} else {
for ($i = 0; $i -lt $arr1.Length; ++$i) {
if ($arr1[$i] -ne $arr2[$i]) {
return $False
}
}
}
$True
}

$publicPrefixesToTest = @(
'in'
's'
'rem'
'h'
'upgra'
'e'
)
$privatePrefixesToTest = @(
'b'
'build'
'buildext'
'ci'
'dep'
'en'
'port'
'notprefix'
)

function testPrefixes($toTest, $commands) {
$toTest | % {
$expected = getOptionsForPrefix $_ $commands
$found = Run-Vcpkg autocomplete $_
Throw-IfFailed
if (-not (arraysEqual @($expected) @($found))) {
Write-Host "unexpected result from vcpkg autocomplete:
expected: `"$($expected -join '" "')`"
found : `"$($found -join '" "')`""
throw
}
}
}

testPrefixes $publicPrefixesToTest $publicCommands
testPrefixes $privatePrefixesToTest $privateCommands

# autocomplete is currently broken, so we only test the parts that are correct
69 changes: 6 additions & 63 deletions include/vcpkg/base/strings.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <limits.h>

#include <algorithm>
#include <charconv>
strega-nil marked this conversation as resolved.
Show resolved Hide resolved
#include <vector>

namespace vcpkg::Strings::details
Expand Down Expand Up @@ -236,74 +237,16 @@ namespace vcpkg::Strings

// Equivalent to one of the `::strto[T]` functions. Returns `nullopt` if there is an error.
template<class T>
Optional<T> strto(CStringView sv);
Optional<T> strto(StringView sv);

template<>
inline Optional<double> strto<double>(CStringView sv)
{
char* endptr = nullptr;
double res = strtod(sv.c_str(), &endptr);
if (endptr == sv.c_str())
{
// no digits
return nullopt;
}
// else, we may have HUGE_VAL but we expect the caller to deal with that
return res;
}

Optional<int> strto<int>(StringView);
template<>
inline Optional<long> strto<long>(CStringView sv)
{
char* endptr = nullptr;
long res = strtol(sv.c_str(), &endptr, 10);
if (endptr == sv.c_str())
{
// no digits
return nullopt;
}
if (errno == ERANGE)
{
// out of bounds
return nullopt;
}

return res;
}

Optional<long> strto<long>(StringView);
template<>
inline Optional<long long> strto<long long>(CStringView sv)
{
char* endptr = nullptr;
long long res = strtoll(sv.c_str(), &endptr, 10);
if (endptr == sv.c_str())
{
// no digits
return nullopt;
}
if (errno == ERANGE)
{
// out of bounds
return nullopt;
}

return res;
}

Optional<long long> strto<long long>(StringView);
template<>
inline Optional<int> strto<int>(CStringView sv)
{
auto res = strto<long>(sv);
if (auto r = res.get())
{
if (*r < INT_MIN || *r > INT_MAX)
{
return nullopt;
}
return static_cast<int>(*r);
}
return nullopt;
}
Optional<double> strto<double>(StringView);

const char* search(StringView haystack, StringView needle);

Expand Down
2 changes: 2 additions & 0 deletions include/vcpkg/export.ifw.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ namespace vcpkg::Export::IFW
Optional<std::string> maybe_installer_file_path;
};

std::string safe_rich_from_plain_text(StringView text);

void do_export(const std::vector<Dependencies::ExportPlanAction>& export_plan,
const std::string& export_id,
const Options& ifw_options,
Expand Down
10 changes: 8 additions & 2 deletions include/vcpkg/export.prefab.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ namespace vcpkg::Export::Prefab
std::string to_string();
void to_string(std::string& out);

friend bool operator==(const NdkVersion& lhs, const NdkVersion& rhs)
{
return lhs.m_major == rhs.m_major && lhs.m_minor == rhs.m_minor && lhs.m_patch == rhs.m_patch;
}
friend bool operator!=(const NdkVersion& lhs, const NdkVersion& rhs) { return !(lhs == rhs); }

private:
int m_major;
int m_minor;
Expand Down Expand Up @@ -74,6 +80,6 @@ namespace vcpkg::Export::Prefab
const VcpkgPaths& paths,
const Options& prefab_options,
const Triplet& triplet);
Optional<std::string> find_ndk_version(const std::string& content);
Optional<NdkVersion> to_version(const std::string& version);
Optional<StringView> find_ndk_version(StringView content);
Optional<NdkVersion> to_version(StringView version);
}
2 changes: 2 additions & 0 deletions include/vcpkg/metrics.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,7 @@ namespace vcpkg
void flush(Filesystem& fs);
};

Optional<StringView> find_first_nonzero_mac(StringView sv);

extern LockGuarded<Metrics> g_metrics;
}
3 changes: 3 additions & 0 deletions include/vcpkg/tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <vcpkg/fwd/tools.h>
#include <vcpkg/fwd/vcpkgpaths.h>

#include <vcpkg/base/optional.h>
#include <vcpkg/base/stringliteral.h>

#include <string>
Expand Down Expand Up @@ -44,5 +45,7 @@ namespace vcpkg
virtual const std::string& get_tool_version(const VcpkgPaths& paths, StringView tool) const = 0;
};

Optional<std::array<int, 3>> parse_tool_version_string(StringView string_version);

std::unique_ptr<ToolCache> get_tool_cache(RequireExactVersions abiToolVersionHandling);
}
120 changes: 120 additions & 0 deletions src/vcpkg-test/export.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
#include <catch2/catch.hpp>

#include <vcpkg/export.ifw.h>
#include <vcpkg/export.prefab.h>

using namespace vcpkg;

namespace IFW = Export::IFW;
namespace Prefab = Export::Prefab;

TEST_CASE ("safe_rich_from_plain_text", "[export]")
{
CHECK(IFW::safe_rich_from_plain_text("&") == "&amp;");
CHECK(IFW::safe_rich_from_plain_text("&asdf") == "&amp;asdf");
CHECK(IFW::safe_rich_from_plain_text("&#123") == "&amp;#123");
CHECK(IFW::safe_rich_from_plain_text("&#x1AfC") == "&amp;#x1AfC");

CHECK(IFW::safe_rich_from_plain_text("&;") == "&amp;;");
CHECK(IFW::safe_rich_from_plain_text("&#;") == "&amp;#;");
CHECK(IFW::safe_rich_from_plain_text("&#x;") == "&amp;#x;");

CHECK(IFW::safe_rich_from_plain_text("&asdf ;") == "&amp;asdf ;");
CHECK(IFW::safe_rich_from_plain_text("&#123a;") == "&amp;#123a;");
CHECK(IFW::safe_rich_from_plain_text("&#x1AfCx;") == "&amp;#x1AfCx;");
CHECK(IFW::safe_rich_from_plain_text("&#X123;") == "&amp;#X123;");

CHECK(IFW::safe_rich_from_plain_text("&asdf;") == "&asdf;");
CHECK(IFW::safe_rich_from_plain_text("&asdf_asdf123;") == "&asdf_asdf123;");
CHECK(IFW::safe_rich_from_plain_text("&#123;") == "&#123;");
CHECK(IFW::safe_rich_from_plain_text("&#x1AfC;") == "&#x1AfC;");
}

TEST_CASE ("find_ndk_version", "[export]")
{
auto result = Prefab::find_ndk_version(R"(
Pkg.Desc = Android NDK
Pkg.Revision = 23.1.7779620
)");
REQUIRE(result.has_value());
CHECK(*result.get() == "23.1.7779620");

result = Prefab::find_ndk_version(R"(
Pkg.Desc = Android NDK
Pkg.Revision = 23.1.7779620
Pkg.Blah = doopadoopa
Pkg.Revision = foobar
)");
REQUIRE(result.has_value());
CHECK(*result.get() == "23.1.7779620");

result = Prefab::find_ndk_version(R"(
Pkg.Desc = Android NDK
Pkg.Revision = 1.2.3.4.5
)");
REQUIRE(result.has_value());
CHECK(*result.get() == "1.2.3.4.5");

result = Prefab::find_ndk_version(R"(
Pkg.Revision = 1.2
)");
REQUIRE(result.has_value());
CHECK(*result.get() == "1.2");

result = Prefab::find_ndk_version(R"(
Pkg.Revision `=
Pkg.Revision = 1.2.3
)");
REQUIRE(result.has_value());
CHECK(*result.get() == "1.2.3");

result = Prefab::find_ndk_version(R"(
Pkg.Revision = foobar
Pkg.Revision = 1.2.3
)");
REQUIRE(result.has_value());
CHECK(*result.get() == "1.2.3");

result = Prefab::find_ndk_version(R"(
Pkg.Desc = Android NDK
)");
CHECK_FALSE(result.has_value());

result = Prefab::find_ndk_version(R"(
Pkg.Desc = Android NDK
Pkg.Revision `=
)");
CHECK_FALSE(result.has_value());

result = Prefab::find_ndk_version(R"(
Pkg.Desc = Android NDK
Pkg.Revision = foobar
)");
CHECK_FALSE(result.has_value());
}

TEST_CASE ("Prefab::to_version", "[export]")
{
auto result = Prefab::to_version("1.2.3");
REQUIRE(result.has_value());
CHECK(*result.get() == Prefab::NdkVersion{1, 2, 3});

result = Prefab::to_version("20.180.2134324");
REQUIRE(result.has_value());
CHECK(*result.get() == Prefab::NdkVersion{20, 180, 2134324});

result = Prefab::to_version("1.2.3 ");
CHECK_FALSE(result.has_value());

result = Prefab::to_version(" 1.2.3");
CHECK_FALSE(result.has_value());

result = Prefab::to_version("1.2.3.4");
CHECK_FALSE(result.has_value());

result = Prefab::to_version("1.2");
CHECK_FALSE(result.has_value());

result = Prefab::to_version("100000000000.2.3");
CHECK_FALSE(result.has_value());
}
35 changes: 35 additions & 0 deletions src/vcpkg-test/metrics.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <catch2/catch.hpp>

#include <vcpkg/metrics.h>

using namespace vcpkg;

TEST_CASE ("find_first_nonzero_mac", "[metrics]")
{
auto result = find_first_nonzero_mac(R"()");
CHECK_FALSE(result.has_value());

result = find_first_nonzero_mac(R"(12-34-56-78-90-ab)");
REQUIRE(result.has_value());
CHECK(*result.get() == "12-34-56-78-90-ab");

result = find_first_nonzero_mac(R"(12-34-56-78-90-AB)");
REQUIRE(result.has_value());
CHECK(*result.get() == "12-34-56-78-90-AB");

result = find_first_nonzero_mac(R"(12-34-56-78-90-AB CD-EF-01-23-45-67)");
REQUIRE(result.has_value());
CHECK(*result.get() == "12-34-56-78-90-AB");

result = find_first_nonzero_mac(R"(00-00-00-00-00-00 CD-EF-01-23-45-67)");
REQUIRE(result.has_value());
CHECK(*result.get() == "CD-EF-01-23-45-67");

result = find_first_nonzero_mac(R"(asdfa00-00-00-00-00-00 jiojCD-EF-01-23-45-67-89)");
REQUIRE(result.has_value());
CHECK(*result.get() == "CD-EF-01-23-45-67");

result = find_first_nonzero_mac(R"(afCD-EF-01-23-45-67)");
REQUIRE(result.has_value());
CHECK(*result.get() == "CD-EF-01-23-45-67");
}
Loading