Skip to content

Commit 2577dee

Browse files
committed
More review comments
1 parent f7172f4 commit 2577dee

File tree

5 files changed

+56
-12
lines changed

5 files changed

+56
-12
lines changed

include/vcpkg/base/contractual-constants.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,4 +590,7 @@ namespace vcpkg
590590
inline constexpr StringLiteral StatusInstalled = "installed";
591591
inline constexpr StringLiteral StatusNotInstalled = "not-installed";
592592
inline constexpr StringLiteral StatusPurge = "purge";
593+
594+
inline constexpr StringLiteral AppInsightsResponseItemsReceived = "itemsReceived";
595+
inline constexpr StringLiteral AppInsightsResponseItemsAccepted = "itemsAccepted";
593596
}

include/vcpkg/metrics.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,4 +199,7 @@ namespace vcpkg
199199

200200
void flush_global_metrics(const Filesystem&);
201201
bool curl_upload_metrics(const std::string& payload);
202+
203+
// exposed for testing
204+
bool parse_metrics_response(StringView response_body);
202205
}

src/vcpkg-test/metrics.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,3 +256,39 @@ TEST_CASE ("payload smoke test", "[metrics]")
256256
)json";
257257
REQUIRE(expected == actual);
258258
}
259+
260+
TEST_CASE ("parse metrics response", "[metrics]")
261+
{
262+
const std::string response = R"json(
263+
{
264+
"itemsReceived": 1,
265+
"itemsAccepted": 1,
266+
"errors": []
267+
}
268+
)json";
269+
auto parsed = parse_metrics_response(response);
270+
CHECK(parsed);
271+
272+
const std::string response_with_errors = R"json(
273+
{
274+
"itemsReceived": 2,
275+
"itemsAccepted": 1,
276+
"errors": [
277+
{
278+
"message": "Invalid payload"
279+
}
280+
]
281+
}
282+
)json";
283+
auto parsed_with_errors = parse_metrics_response(response_with_errors);
284+
CHECK(!parsed_with_errors);
285+
286+
const std::string response_with_errors2 = R"json(
287+
{
288+
"itemsReceived": 2,
289+
"errors": []
290+
}
291+
)json";
292+
auto parsed_with_errors2 = parse_metrics_response(response_with_errors2);
293+
CHECK(!parsed_with_errors2);
294+
}

src/vcpkg/base/downloads.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,13 @@ namespace vcpkg
362362
context.report_error(format_filesystem_call_error(ec, "fopen", {file}));
363363
return false;
364364
}
365-
auto file_size = fileptr.size(VCPKG_LINE_INFO);
365+
ec.clear();
366+
auto file_size = fileptr.size(ec);
367+
if (ec)
368+
{
369+
context.report_error(format_filesystem_call_error(ec, "fstat", {file}));
370+
return false;
371+
}
366372

367373
CurlEasyHandle handle;
368374
CURL* curl = handle.get();

src/vcpkg/metrics.cpp

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <vcpkg/base/chrono.h>
2+
#include <vcpkg/base/contractual-constants.h>
23
#include <vcpkg/base/curl.h>
34
#include <vcpkg/base/files.h>
45
#include <vcpkg/base/hash.h>
@@ -517,16 +518,11 @@ namespace vcpkg
517518
fs.write_contents(vcpkg_metrics_txt_path, payload, ec);
518519
if (ec) return;
519520

520-
const StringLiteral executableExtension =
521+
const Path temp_folder_path_exe = temp_folder_path / "vcpkg-" VCPKG_BASE_VERSION_AS_STRING
521522
#if defined(WIN32)
522-
".exe"
523-
#else
524-
""
523+
".exe"
525524
#endif
526525
;
527-
528-
const Path temp_folder_path_exe =
529-
temp_folder_path / "vcpkg-" VCPKG_BASE_VERSION_AS_STRING + executableExtension;
530526
fs.copy_file(get_exe_path_of_current_process(), temp_folder_path_exe, CopyOptions::skip_existing, ec);
531527
if (ec) return;
532528

@@ -547,14 +543,14 @@ namespace vcpkg
547543
return size * nmemb;
548544
}
549545

550-
static bool parse_metrics_response(StringView body)
546+
bool parse_metrics_response(StringView response_body)
551547
{
552-
auto maybe_json = Json::parse_object(body, "metrics_response");
548+
auto maybe_json = Json::parse_object(response_body, "metrics_response");
553549
auto json = maybe_json.get();
554550
if (!json) return false;
555551

556-
auto maybe_received = json->get("itemsReceived");
557-
auto maybe_accepted = json->get("itemsAccepted");
552+
auto maybe_received = json->get(vcpkg::AppInsightsResponseItemsReceived);
553+
auto maybe_accepted = json->get(vcpkg::AppInsightsResponseItemsAccepted);
558554
auto maybe_errors = json->get("errors");
559555

560556
if (maybe_received && maybe_accepted && maybe_errors && maybe_received->is_integer() &&

0 commit comments

Comments
 (0)