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

[localization] more! remove system.print.h from vcpkg/base, tests #479

Merged
merged 4 commits into from
Apr 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions include/vcpkg/base/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ namespace vcpkg

namespace fmt
{
template<class Char>
struct formatter<vcpkg::LineInfo, Char>
template<>
struct formatter<vcpkg::LineInfo, char>
{
constexpr auto parse(format_parse_context& ctx) const -> decltype(ctx.begin())
{
Expand All @@ -43,13 +43,27 @@ namespace fmt
}
};

template<class Char>
struct formatter<vcpkg::StringView, Char> : formatter<string_view, Char>
template<>
struct formatter<vcpkg::StringView, char> : formatter<string_view, char>
{
template<class FormatContext>
auto format(vcpkg::StringView sv, FormatContext& ctx) const -> decltype(ctx.out())
{
return formatter<string_view, Char>::format(string_view(sv.data(), sv.size()), ctx);
return formatter<string_view, char>::format(string_view(sv.data(), sv.size()), ctx);
}
};

template<>
struct formatter<std::error_code, char> : formatter<std::string, char>
{
constexpr auto parse(format_parse_context& ctx) const -> decltype(ctx.begin())
{
return vcpkg::basic_format_parse_impl(ctx);
}
template<class FormatContext>
auto format(const std::error_code& ec, FormatContext& ctx) const -> decltype(ctx.out())
{
return formatter<std::string, char>::format(ec.message(), ctx);
}
};
}
9 changes: 9 additions & 0 deletions include/vcpkg/base/messages.h
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ namespace vcpkg::msg
DECLARE_MSG_ARG(count, "42");
DECLARE_MSG_ARG(elapsed, "3.532 min");
DECLARE_MSG_ARG(email, "[email protected]");
DECLARE_MSG_ARG(error_msg, "File Not Found");
DECLARE_MSG_ARG(exit_code, "127");
DECLARE_MSG_ARG(expected_version, "1.3.8");
DECLARE_MSG_ARG(new_scheme, "version");
Expand Down Expand Up @@ -305,12 +306,20 @@ namespace vcpkg::msg
"",
"error: cannot specify both --no-{option} and --{option}.");

inline void print_warning(const LocalizedString& s)
{
print(Color::warning, format(msgErrorMessage).append(s).appendnl());
}
template<class Message, class... Ts>
void print_warning(Message m, Ts... args)
{
print(Color::warning, format(msgWarningMessage).append(format(m, args...).appendnl()));
}

inline void print_error(const LocalizedString& s)
{
print(Color::error, format(msgErrorMessage).append(s).appendnl());
}
template<class Message, class... Ts>
void print_error(Message m, Ts... args)
{
Expand Down
6 changes: 6 additions & 0 deletions locales/messages.en.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"ExpectedPortName": "expected a port name here",
"ExpectedTripletName": "expected a triplet name here",
"FailedToProvisionCe": "Failed to provision vcpkg-ce.",
"FailedToStoreBackToMirror": "failed to store back to mirror:",
"ForceSystemBinariesOnWeirdPlatforms": "Environment variable VCPKG_FORCE_SYSTEM_BINARIES must be set on arm, s390x, and ppc64le platforms.",
"FormattedParseMessageExpression": " on expression: {value}",
"GenerateMsgErrorParsingFormatArgs": "error: parsing format string for {value}:",
Expand All @@ -63,6 +64,8 @@
"IllegalPlatformSpec": "error: Platform qualifier is not allowed in this context",
"InternalErrorMessage": "internal error: ",
"InternalErrorMessageContact": "Please open an issue at https://github.com/microsoft/vcpkg/issues/new?template=other-type-of-bug-report.md&labels=category:vcpkg-bug with detailed steps to reproduce the problem.",
"JsonErrorFailedToParse": "failed to parse {path}:",
"JsonErrorFailedToRead": "failed to read {path}: {error_msg}",
"LicenseExpressionContainsExtraPlus": "SPDX license expression contains an extra '+'. These are only allowed directly after a license identifier.",
"LicenseExpressionContainsInvalidCharacter": "SPDX license expression contains an invalid character (0x{value:02X} '{value}').",
"LicenseExpressionContainsUnicode": "SPDX license expression contains a unicode character (U+{value:04X} '{pretty_value}'), but these expressions are ASCII-only.",
Expand Down Expand Up @@ -91,6 +94,7 @@
"ResultsHeader": "RESULTS",
"SeeURL": "See {url} for more information.",
"SuggestNewVersionScheme": "Use the version scheme \"{new_scheme}\" instead of \"{old_scheme}\" in port \"{package_name}\".\nUse `--{option}` to disable this check.",
"UnexpectedErrorDuringBulkDownload": "an unexpected error occurred during bulk download.",
"UnknownBaselineFileContent": "unrecognizable baseline entry; expected 'port:triplet=(fail|skip)'",
"UnsupportedSystemName": "Error: Could not map VCPKG_CMAKE_SYSTEM_NAME '{system_name}' to a vcvarsall platform. Supported system names are '', 'Windows' and 'WindowsStore'.",
"UnsupportedToolchain": "Error: in triplet {triplet}: Unable to find a valid toolchain combination.\n The requested target architecture was {arch}\n The selected Visual Studio instance is at {path}\n The available toolchain combinations are {list}\n",
Expand All @@ -110,6 +114,8 @@
"VersionInvalidRelaxed": "`{version}` is not a valid relaxed version (semver with arbitrary numeric element count).",
"VersionInvalidSemver": "`{version}` is not a valid semantic version, consult <https://semver.org>.",
"VersionSpecMismatch": "error: Failed to load port because version specs did not match\n Path: {path}\n Expected: {expected_version}\n Actual: {actual_version}",
"WaitingForChildrenToExit": "Waiting for child processes to exit...",
"WaitingToTakeFilesystemLock": "waiting to take filesystem lock on {path}...",
"WarningMessage": "warning: ",
"WarningsTreatedAsErrors": "previous warnings being interpreted as errors"
}
9 changes: 9 additions & 0 deletions locales/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
"ExpectedPortName": "expected a port name here",
"ExpectedTripletName": "expected a triplet name here",
"FailedToProvisionCe": "Failed to provision vcpkg-ce.",
"FailedToStoreBackToMirror": "failed to store back to mirror:",
"ForceSystemBinariesOnWeirdPlatforms": "Environment variable VCPKG_FORCE_SYSTEM_BINARIES must be set on arm, s390x, and ppc64le platforms.",
"FormattedParseMessageExpression": " on expression: {value}",
"_FormattedParseMessageExpression.comment": "Example of {value} is 'x64 & windows'\n",
Expand All @@ -107,6 +108,10 @@
"IllegalPlatformSpec": "error: Platform qualifier is not allowed in this context",
"InternalErrorMessage": "internal error: ",
"InternalErrorMessageContact": "Please open an issue at https://github.com/microsoft/vcpkg/issues/new?template=other-type-of-bug-report.md&labels=category:vcpkg-bug with detailed steps to reproduce the problem.",
"JsonErrorFailedToParse": "failed to parse {path}:",
"_JsonErrorFailedToParse.comment": "example of {path} is '/foo/bar'.\n",
"JsonErrorFailedToRead": "failed to read {path}: {error_msg}",
"_JsonErrorFailedToRead.comment": "example of {path} is '/foo/bar'.\nexample of {error_msg} is 'File Not Found'.\n",
"LicenseExpressionContainsExtraPlus": "SPDX license expression contains an extra '+'. These are only allowed directly after a license identifier.",
"LicenseExpressionContainsInvalidCharacter": "SPDX license expression contains an invalid character (0x{value:02X} '{value}').",
"_LicenseExpressionContainsInvalidCharacter.comment": "example of {value:02X} is '7B'\nexample of {value} is '{'\n",
Expand Down Expand Up @@ -152,6 +157,7 @@
"_SeeURL.comment": "example of {url} is 'https://github.com/microsoft/vcpkg'.\n",
"SuggestNewVersionScheme": "Use the version scheme \"{new_scheme}\" instead of \"{old_scheme}\" in port \"{package_name}\".\nUse `--{option}` to disable this check.",
"_SuggestNewVersionScheme.comment": "example of {new_scheme} is 'version'.\nexample of {old_scheme} is 'version-string'.\nexample of {package_name} is 'zlib'.\nexample of {option} is 'editable'.\n",
"UnexpectedErrorDuringBulkDownload": "an unexpected error occurred during bulk download.",
"UnknownBaselineFileContent": "unrecognizable baseline entry; expected 'port:triplet=(fail|skip)'",
"UnsupportedSystemName": "Error: Could not map VCPKG_CMAKE_SYSTEM_NAME '{system_name}' to a vcvarsall platform. Supported system names are '', 'Windows' and 'WindowsStore'.",
"_UnsupportedSystemName.comment": "example of {system_name} is 'Darwin'.\n",
Expand Down Expand Up @@ -182,6 +188,9 @@
"_VersionInvalidSemver.comment": "example of {version} is '1.3.8'.\n",
"VersionSpecMismatch": "error: Failed to load port because version specs did not match\n Path: {path}\n Expected: {expected_version}\n Actual: {actual_version}",
"_VersionSpecMismatch.comment": "example of {path} is '/foo/bar'.\nexample of {expected_version} is '1.3.8'.\nexample of {actual_version} is '1.3.8'.\n",
"WaitingForChildrenToExit": "Waiting for child processes to exit...",
"WaitingToTakeFilesystemLock": "waiting to take filesystem lock on {path}...",
"_WaitingToTakeFilesystemLock.comment": "example of {path} is '/foo/bar'.\n",
"WarningMessage": "warning: ",
"WarningsTreatedAsErrors": "previous warnings being interpreted as errors"
}
53 changes: 40 additions & 13 deletions src/vcpkg-fuzz/main.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <vcpkg/base/checks.h>
#include <vcpkg/base/json.h>
#include <vcpkg/base/messages.h>
#include <vcpkg/base/stringview.h>
#include <vcpkg/base/system.print.h>
#include <vcpkg/base/unicode.h>

#include <vcpkg/platform-expression.h>
Expand All @@ -16,6 +16,18 @@ using namespace vcpkg;

namespace
{
DECLARE_AND_REGISTER_MESSAGE(FuzzInvalidKind, (msg::value), "example of {value} is 'utf-8'", "invalid kind: {}");
DECLARE_AND_REGISTER_MESSAGE(FuzzExpectedOneOf,
(),
"the list after the colon should stay the same, they're literal values",
"expected one of: utf-8, json, platform-expr");
DECLARE_AND_REGISTER_MESSAGE(FuzzUnknownOption, (msg::option), "", "unknown option: --{option}");

DECLARE_AND_REGISTER_MESSAGE(FuzzHelpUsage, (), "", "usage: vcpkg-fuzz --kind=<kind>");
DECLARE_AND_REGISTER_MESSAGE(FuzzHelpInput, (), "", "accepts input on stdin.");
DECLARE_AND_REGISTER_MESSAGE(FuzzHelpOptions, (), "", "options:");
DECLARE_AND_REGISTER_MESSAGE(FuzzHelpOptionKind, (), "", "one of {{utf-8, json, platform-expr}}");

enum class FuzzKind
{
None,
Expand Down Expand Up @@ -68,14 +80,16 @@ namespace
}
else
{
print2(Color::error, "Invalid kind: ", value, "\n");
print2(Color::error, " Expected one of: utf-8, json, platform-expr\n\n");
msg::print_error(msg::format(msgFuzzInvalidKind, msg::value = value)
.appendnl()
.append_indent()
.append(msgFuzzExpectedOneOf));
print_help_and_exit(true);
}
}
else
{
print2("Unknown option: ", key, "\n\n");
msg::print_error(msgFuzzUnknownOption, msg::option = key);
print_help_and_exit(true);
}
}
Expand All @@ -99,19 +113,30 @@ namespace

[[noreturn]] void print_help_and_exit(bool invalid = false)
{
constexpr auto help =
R"(
Usage: vcpkg-fuzz <options>
auto color = invalid ? Color::error : Color::none;

Accepts input on stdin.
auto message = msg::format(msgFuzzHelpUsage).appendnl().appendnl();
message.append(msgFuzzHelpInput).appendnl().appendnl();
message.append(msgFuzzHelpOptions).appendnl();

Options:
--kind=... One of {utf-8, json}
)";
struct
{
StringLiteral option;
LocalizedString help;
} options[] = {
{"kind", msg::format(msgFuzzHelpOptionKind)},
};

auto color = invalid ? Color::error : Color::success;
for (const auto& option : options)
{
auto start_option = fmt::format(" --{}=...", option.option);
message.append_raw(start_option)
.append_raw(std::string(30 - start_option.size(), ' '))
.append(option.help)
.appendnl();
}

print2(color, help);
msg::print(color, message);
if (invalid)
{
Checks::exit_fail(VCPKG_LINE_INFO);
Expand Down Expand Up @@ -176,6 +201,8 @@ Accepts input on stdin.

int main(int argc, char** argv)
{
msg::threadunsafe_initialize_context();

auto args = FuzzArgs(argc, argv);

if (args.kind == FuzzKind::None)
Expand Down
7 changes: 4 additions & 3 deletions src/vcpkg-test/manifests.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include <catch2/catch.hpp>

#include <vcpkg/base/json.h>
#include <vcpkg/base/system.print.h>
#include <vcpkg/base/util.h>

#include <vcpkg/paragraphs.h>
Expand All @@ -25,8 +24,10 @@ static Json::Object parse_json_object(StringView sv)
}
else
{
vcpkg::print2("Error found while parsing JSON document:\n", sv, '\n');
Checks::exit_with_message(VCPKG_LINE_INFO, json.error()->format());
INFO("Error found while parsing JSON document:");
INFO(sv.to_string());
FAIL(json.error()->format());
return Json::Object{};
}
}

Expand Down
7 changes: 1 addition & 6 deletions src/vcpkg-test/specifier.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include <catch2/catch.hpp>

#include <vcpkg/base/system.print.h>
#include <vcpkg/base/util.h>

#include <vcpkg/packagespec.h>
Expand Down Expand Up @@ -86,11 +85,7 @@ TEST_CASE ("specifier parsing", "[specifier]")

SECTION ("parsed specifier wildcard feature")
{
auto maybe_spec = vcpkg::parse_qualified_specifier("zlib[*]");
print2(maybe_spec.error());
REQUIRE(maybe_spec);

auto& spec = *maybe_spec.get();
auto spec = Test::unwrap(vcpkg::parse_qualified_specifier("zlib[*]"));
REQUIRE(spec.features.value_or(std::vector<std::string>{}) == std::vector<std::string>{"*"});
}

Expand Down
11 changes: 6 additions & 5 deletions src/vcpkg/base/checks.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <vcpkg/base/checks.h>
#include <vcpkg/base/messages.h>
#include <vcpkg/base/stringview.h>
#include <vcpkg/base/system.debug.h>
#include <vcpkg/base/system.print.h>

#include <stdlib.h>

Expand Down Expand Up @@ -78,7 +78,8 @@ namespace vcpkg

[[noreturn]] void Checks::exit_with_message(const LineInfo& line_info, StringView error_message)
{
print2(Color::error, error_message, '\n');
msg::write_unlocalized_text_to_stdout(Color::error, error_message);
msg::write_unlocalized_text_to_stdout(Color::error, "\n");
exit_fail(line_info);
}
[[noreturn]] void Checks::msg_exit_with_message(const LineInfo& line_info, const LocalizedString& error_message)
Expand All @@ -89,8 +90,7 @@ namespace vcpkg

[[noreturn]] void Checks::exit_with_message_and_line(const LineInfo& line_info, StringView error_message)
{
msg::print(Color::error, locale_invariant_lineinfo(line_info));
print2(Color::error, error_message, '\n');
msg::println(Color::error, locale_invariant_lineinfo(line_info).append_raw(error_message));
exit_fail(line_info);
}

Expand Down Expand Up @@ -137,7 +137,8 @@ namespace vcpkg

[[noreturn]] void Checks::exit_maybe_upgrade(const LineInfo& line_info, StringView error_message)
{
print2(Color::error, error_message, '\n');
msg::write_unlocalized_text_to_stdout(Color::error, error_message);
msg::write_unlocalized_text_to_stdout(Color::error, "\n");
display_upgrade_message();
exit_fail(line_info);
}
Expand Down
11 changes: 8 additions & 3 deletions src/vcpkg/base/downloads.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include <vcpkg/base/lockguarded.h>
#include <vcpkg/base/system.debug.h>
#include <vcpkg/base/system.h>
#include <vcpkg/base/system.print.h>
#include <vcpkg/base/system.process.h>
#include <vcpkg/base/system.proxy.h>
#include <vcpkg/base/util.h>
Expand All @@ -26,6 +25,11 @@ namespace
"=== curl output ===\n"
"{actual}\n"
"=== end curl output ===\n");
DECLARE_AND_REGISTER_MESSAGE(UnexpectedErrorDuringBulkDownload,
(),
"",
"an unexpected error occurred during bulk download.");
DECLARE_AND_REGISTER_MESSAGE(FailedToStoreBackToMirror, (), "", "failed to store back to mirror:");
}

namespace vcpkg
Expand Down Expand Up @@ -372,7 +376,7 @@ namespace vcpkg
if (start_size + url_pairs.size() > out->size())
{
// curl stopped before finishing all downloads; retry after some time
print2(Color::warning, "Warning: an unexpected error occurred during bulk download.\n");
msg::print_warning(msgUnexpectedErrorDuringBulkDownload);
std::this_thread::sleep_for(std::chrono::milliseconds(i));
url_pairs =
View<std::pair<std::string, Path>>{url_pairs.begin() + out->size() - start_size, url_pairs.end()};
Expand Down Expand Up @@ -731,7 +735,8 @@ namespace vcpkg
auto maybe_push = put_file_to_mirror(fs, download_path, *hash);
if (!maybe_push.has_value())
{
print2(Color::warning, "Warning: failed to store back to mirror:\n", maybe_push.error());
msg::print_warning(msgFailedToStoreBackToMirror);
msg::write_unlocalized_text_to_stdout(Color::warning, maybe_push.error());
}
}
return *url;
Expand Down
Loading