Thanks to visit codestin.com
Credit goes to github.com

Skip to content

feat: fix compilation problems on MSVC #233

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

Merged
merged 3 commits into from
Feb 8, 2021
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ cmake-build-*/
# Ignore Visual Studio Code files
.vsbuild/
.vscode/
build/

6 changes: 6 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ add_library(
site/tips_retry/tips_retry.cc
site/tips_scopes/tips_scopes.cc)
functions_framework_cpp_add_common_options(functions_framework_examples)
if (MSVC)
set_property(
SOURCE site/tips_gcp_apis/tips_gcp_apis.cc site/env_vars/env_vars.cc
APPEND
PROPERTY COMPILE_DEFINITIONS _CRT_SECURE_NO_WARNINGS)
endif ()
target_link_libraries(
functions_framework_examples fmt::fmt functions-framework-cpp::framework
google-cloud-cpp::pubsub google-cloud-cpp::storage)
Expand Down
4 changes: 4 additions & 0 deletions examples/site/testing_http/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ if (BUILD_TESTING)
string(REPLACE ".cc" "" target "${target}")
add_executable("${target}" ${fname})
functions_framework_cpp_add_common_options(${target})
if (MSVC)
target_compile_definitions(${target}
PRIVATE "_CRT_SECURE_NO_WARNINGS")
endif ()
target_link_libraries(
${target}
PRIVATE functions_framework_examples
Expand Down
9 changes: 7 additions & 2 deletions examples/site/testing_http/http_integration_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ HttpResponse HttpGet(std::string const& url, std::string const& payload);

char const* argv0 = nullptr;

auto ExePath(bfs::path const& filename) {
static auto const kPath = std::vector<bfs::path>{
bfs::canonical(argv0).make_preferred().parent_path()};
return bp::search_path(filename, kPath);
}

class HttpIntegrationTest : public ::testing::Test {
protected:
void SetUp() override {
Expand All @@ -47,8 +53,7 @@ class HttpIntegrationTest : public ::testing::Test {
return;
}
curl_global_init(CURL_GLOBAL_ALL);
auto const exe = bfs::path(argv0).parent_path() / "http_integration_server";
auto server = bp::child(exe, "--port=8030");
auto server = bp::child(ExePath("http_integration_server"), "--port=8030");
url_ = "http://localhost:8030";
ASSERT_TRUE(WaitForServerReady(url_));
process_ = std::move(server);
Expand Down
12 changes: 8 additions & 4 deletions examples/site/testing_pubsub/pubsub_integration_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,19 @@ HttpResponse HttpEvent(std::string const& url, std::string const& payload);

char const* argv0 = nullptr;

auto ExePath(bfs::path const& filename) {
static auto const kPath = std::vector<bfs::path>{
bfs::canonical(argv0).make_preferred().parent_path()};
return bp::search_path(filename, kPath);
}

class PubsubIntegrationTest : public ::testing::Test {
protected:
void SetUp() override {
curl_global_init(CURL_GLOBAL_ALL);

auto const exe =
bfs::path(argv0).parent_path() / "pubsub_integration_server";
auto server =
bp::child(exe, "--port=8040", (bp::std_out & bp::std_err) > child_log_);
auto server = bp::child(ExePath("pubsub_integration_server"), "--port=8040",
(bp::std_out & bp::std_err) > child_log_);
ASSERT_TRUE(WaitForServerReady("http://localhost:8040/"));
process_ = std::move(server);
}
Expand Down
11 changes: 8 additions & 3 deletions examples/site/testing_storage/storage_integration_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,20 @@ HttpResponse HttpEvent(std::string const& url, std::string const& payload);

char const* argv0 = nullptr;

auto ExePath(bfs::path const& filename) {
static auto const kPath = std::vector<bfs::path>{
bfs::canonical(argv0).make_preferred().parent_path()};
return bp::search_path(filename, kPath);
}

class StorageIntegrationTest : public ::testing::Test {
protected:
void SetUp() override {
curl_global_init(CURL_GLOBAL_ALL);

auto const exe =
bfs::path(argv0).parent_path() / "storage_integration_server";
auto server =
bp::child(exe, "--port=8050", (bp::std_out & bp::std_err) > child_log_);
bp::child(ExePath("storage_integration_server"), "--port=8050",
(bp::std_out & bp::std_err) > child_log_);
ASSERT_TRUE(WaitForServerReady("http://localhost:8050/"));
process_ = std::move(server);
}
Expand Down
13 changes: 7 additions & 6 deletions examples/site_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
// limitations under the License.

#include "google/cloud/functions/internal/parse_cloud_event_json.h"
#include "google/cloud/functions/internal/setenv.h"
#include "google/cloud/functions/cloud_event.h"
#include "google/cloud/functions/http_request.h"
#include "google/cloud/functions/http_response.h"
#include <gmock/gmock.h>
#include <nlohmann/json.hpp>
#include <stdlib.h> // NOLINT - we need the POSIX header, for setenv.

namespace gcf = ::google::cloud::functions;
extern gcf::HttpResponse hello_world_content(gcf::HttpRequest request);
Expand Down Expand Up @@ -286,11 +286,11 @@ TEST(ExamplesSiteTest, ConceptsStateless) {
}

TEST(ExamplesSiteTest, EnvVars) {
unsetenv("FOO");
google::cloud::functions_internal::SetEnv("FOO", std::nullopt);
auto actual = env_vars(gcf::HttpRequest{});
EXPECT_THAT(actual.payload(), AllOf(HasSubstr("FOO"), HasSubstr("not set")));

setenv("FOO", "test-value", 1);
google::cloud::functions_internal::SetEnv("FOO", "test-value");
actual = env_vars(gcf::HttpRequest{});
EXPECT_THAT(actual.payload(), HasSubstr("test-value"));
}
Expand All @@ -309,16 +309,17 @@ TEST(ExamplesSiteTest, TipsGcpApis) {
GTEST_SKIP();
#endif // __has_feature(thread_sanitizer)
#endif // defined(__has_feature)
unsetenv("GCP_PROJECT");
google::cloud::functions_internal::SetEnv("GCP_PROJECT", std::nullopt);
EXPECT_THROW(tips_gcp_apis(gcf::HttpRequest{}), std::runtime_error);

setenv("GCP_PROJECT", "test-unused", 1);
google::cloud::functions_internal::SetEnv("GCP_PROJECT", "test-unused");
EXPECT_THROW(tips_gcp_apis(gcf::HttpRequest{}), std::exception);
EXPECT_THROW(
tips_gcp_apis(gcf::HttpRequest{}.set_payload(nlohmann::json({}).dump())),
std::runtime_error);

setenv("PUBSUB_EMULATOR_HOST", "localhost:1", 1);
google::cloud::functions_internal::SetEnv("PUBSUB_EMULATOR_HOST",
"localhost:1");
auto const actual = tips_gcp_apis(gcf::HttpRequest{}.set_payload(
nlohmann::json({{"topic", "test-unused"}}).dump()));
EXPECT_EQ(actual.result(), gcf::HttpResponse::kInternalServerError);
Expand Down
8 changes: 8 additions & 0 deletions google/cloud/functions/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ add_library(
internal/parse_cloud_event_json.h
internal/parse_options.cc
internal/parse_options.h
internal/setenv.cc
internal/setenv.h
internal/version_info.h
internal/wrap_request.cc
internal/wrap_request.h
Expand All @@ -56,6 +58,12 @@ add_library(
version.cc
version.h)
functions_framework_cpp_add_common_options(functions_framework_cpp)
if (MSVC)
set_property(
SOURCE internal/parse_options.cc
APPEND
PROPERTY COMPILE_DEFINITIONS "_CRT_SECURE_NO_WARNINGS")
endif ()
target_include_directories(functions_framework_cpp
PUBLIC $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}>)
target_include_directories(functions_framework_cpp SYSTEM
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,14 @@ char const* argv0 = nullptr;
auto constexpr kServer = "echo_server";
auto constexpr kConformanceServer = "http_conformance";

auto ExePath(bfs::path const& filename) {
static auto const kPath = std::vector<bfs::path>{
bfs::canonical(argv0).make_preferred().parent_path()};
return bp::search_path(filename, kPath);
}

TEST(RunIntegrationTest, Basic) {
auto const exe = bfs::path(argv0).parent_path() / kServer;
auto server = bp::child(exe, "--port=8010");
auto server = bp::child(ExePath(kServer), "--port=8010");
auto result = WaitForServerReady("localhost", "8010");
ASSERT_EQ(result, 0);

Expand Down Expand Up @@ -121,9 +126,9 @@ TEST(RunIntegrationTest, Basic) {
}

TEST(RunIntegrationTest, ExceptionLogsToStderr) {
auto const exe = bfs::path(argv0).parent_path() / kServer;
bp::ipstream child_stderr;
auto server = bp::child(exe, "--port=8010", bp::std_err > child_stderr);
auto server =
bp::child(ExePath(kServer), "--port=8010", bp::std_err > child_stderr);
auto result = WaitForServerReady("localhost", "8010");
ASSERT_EQ(result, 0);

Expand All @@ -145,11 +150,11 @@ TEST(RunIntegrationTest, ExceptionLogsToStderr) {
}

TEST(RunIntegrationTest, OutputIsFlushed) {
auto const exe = bfs::path(argv0).parent_path() / kServer;
bp::ipstream child_stderr;
bp::ipstream child_stdout;
auto server = bp::child(exe, "--port=8010", bp::std_err > child_stderr,
bp::std_out > child_stdout);
auto server =
bp::child(ExePath(kServer), "--port=8010", bp::std_err > child_stderr,
bp::std_out > child_stdout);
auto result = WaitForServerReady("localhost", "8010");
ASSERT_EQ(result, 0);

Expand All @@ -176,8 +181,7 @@ TEST(RunIntegrationTest, OutputIsFlushed) {
}

TEST(RunIntegrationTest, ConformanceSmokeTest) {
auto const exe = bfs::path(argv0).parent_path() / kConformanceServer;
auto server = bp::child(exe, "--port=8010");
auto server = bp::child(ExePath(kConformanceServer), "--port=8010");
auto result = WaitForServerReady("localhost", "8010");
ASSERT_EQ(result, 0);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,14 @@ char const* argv0 = nullptr;
auto constexpr kServer = "cloud_event_handler";
auto constexpr kConformanceServer = "cloud_event_conformance";

auto ExePath(bfs::path const& filename) {
static auto const kPath = std::vector<bfs::path>{
bfs::canonical(argv0).make_preferred().parent_path()};
return bp::search_path(filename, kPath);
}

TEST(RunIntegrationTest, Basic) {
auto const exe = bfs::path(argv0).parent_path() / kServer;
auto server = bp::child(exe, "--port=8020");
auto server = bp::child(ExePath(kServer), "--port=8020");
auto result = WaitForServerReady("localhost", "8020");
ASSERT_EQ(result, 0);

Expand Down Expand Up @@ -202,8 +207,7 @@ TEST(RunIntegrationTest, Basic) {
}

TEST(RunIntegrationTest, Batch) {
auto const exe = bfs::path(argv0).parent_path() / kServer;
auto server = bp::child(exe, "--port=8020");
auto server = bp::child(ExePath(kServer), "--port=8020");
auto result = WaitForServerReady("localhost", "8020");
ASSERT_EQ(result, 0);

Expand All @@ -218,8 +222,7 @@ TEST(RunIntegrationTest, Batch) {
}

TEST(RunIntegrationTest, Binary) {
auto const exe = bfs::path(argv0).parent_path() / kServer;
auto server = bp::child(exe, "--port=8020");
auto server = bp::child(ExePath(kServer), "--port=8020");
auto result = WaitForServerReady("localhost", "8020");
ASSERT_EQ(result, 0);

Expand All @@ -234,9 +237,9 @@ TEST(RunIntegrationTest, Binary) {
}

TEST(RunIntegrationTest, ExceptionLogsToStderr) {
auto const exe = bfs::path(argv0).parent_path() / kServer;
bp::ipstream child_stderr;
auto server = bp::child(exe, "--port=8020", bp::std_err > child_stderr);
auto server =
bp::child(ExePath(kServer), "--port=8020", bp::std_err > child_stderr);
auto result = WaitForServerReady("localhost", "8020");
ASSERT_EQ(result, 0);

Expand All @@ -258,11 +261,11 @@ TEST(RunIntegrationTest, ExceptionLogsToStderr) {
}

TEST(RunIntegrationTest, OutputIsFlushed) {
auto const exe = bfs::path(argv0).parent_path() / kServer;
bp::ipstream child_stderr;
bp::ipstream child_stdout;
auto server = bp::child(exe, "--port=8020", bp::std_err > child_stderr,
bp::std_out > child_stdout);
auto server =
bp::child(ExePath(kServer), "--port=8020", bp::std_err > child_stderr,
bp::std_out > child_stdout);
auto result = WaitForServerReady("localhost", "8020");
ASSERT_EQ(result, 0);

Expand All @@ -289,8 +292,7 @@ TEST(RunIntegrationTest, OutputIsFlushed) {
}

TEST(RunIntegrationTest, ConformanceSmokeTest) {
auto const exe = bfs::path(argv0).parent_path() / kConformanceServer;
auto server = bp::child(exe, "--port=8020");
auto server = bp::child(ExePath(kConformanceServer), "--port=8020");
auto result = WaitForServerReady("localhost", "8020");
ASSERT_EQ(result, 0);

Expand Down
11 changes: 9 additions & 2 deletions google/cloud/functions/internal/compiler_info_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@

#include "google/cloud/functions/internal/compiler_info.h"
#include <gmock/gmock.h>
#include <regex>

namespace google::cloud::functions_internal {
inline namespace FUNCTIONS_FRAMEWORK_CPP_NS {
namespace {

using ::testing::Contains;

TEST(CompilerInfo, CompilerId) {
auto const cn = CompilerId();
EXPECT_NE(cn, "");
Expand All @@ -30,8 +33,12 @@ TEST(CompilerInfo, CompilerId) {
TEST(CompilerInfo, CompilerVersion) {
auto const cv = CompilerVersion();
EXPECT_NE(cv, "");
// Look for something that looks vaguely like an X.Y version number.
EXPECT_THAT(cv, ::testing::ContainsRegex(R"([0-9]+.[0-9]+)"));
// Look for something that looks vaguely like an X.Y version number. Cannot
// use ::testing::ContainsRegex() because that does not work when GTest is
// compiled under MSVC.
EXPECT_THAT(cv, Contains('.'));
EXPECT_TRUE(std::regex_search(cv, std::regex(R"([0-9]+\.[0-9]+)")))
<< "version=" << cv;
}

TEST(CompilerInfo, LanguageVersion) {
Expand Down
9 changes: 6 additions & 3 deletions google/cloud/functions/internal/framework_impl_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ TEST(FrameworkTest, Http) {
argc, argv, std::move(f), [&shutdown]() { return shutdown.load(); },
[&port_p](int port) mutable { port_p.set_value(port); });
};
auto done = std::async(std::launch::async, run, kTestArgc, kTestArgv, hello);
auto done = std::async(std::launch::async, run, static_cast<int>(kTestArgc),
kTestArgv, hello);

auto port = port_f.get();
auto actual = HttpGet("localhost", std::to_string(port), "/say/hello");
Expand Down Expand Up @@ -146,7 +147,8 @@ TEST(FrameworkTest, CloudEvent) {
argc, argv, std::move(f), [&shutdown]() { return shutdown.load(); },
[&port_p](int port) mutable { port_p.set_value(port); });
};
auto done = std::async(std::launch::async, run, kTestArgc, kTestArgv, hello);
auto done = std::async(std::launch::async, run, static_cast<int>(kTestArgc),
kTestArgv, hello);

auto port = port_f.get();
auto actual = CloudEventGet("localhost", std::to_string(port), "/");
Expand All @@ -163,7 +165,8 @@ TEST(FrameworkTest, CloudEvent) {

TEST(FrameworkTest, CloudEventInvalidPort) {
auto const exit_code = ::google::cloud::functions::Run(
kTestInvalidArgc, kTestInvalidArgv, functions::UserCloudEventFunction{});
static_cast<int>(kTestInvalidArgc), kTestInvalidArgv,
functions::UserCloudEventFunction{});
EXPECT_NE(exit_code, 0);
}

Expand Down
Loading