From 5b72b6c2da65472e1de7e9180e116c1e5beadb20 Mon Sep 17 00:00:00 2001 From: Dominic Hamon Date: Tue, 18 Aug 2020 10:02:20 +0100 Subject: [PATCH] Remove "BENCHMARK_" prefix from env var version of command line flags (#997) As noted in #995, this causes issues when the command line flag already starts with "benchmark_", which they all do. Not caught by tests as the test flags didn't start with "benchmark". Fixes #995 --- src/commandlineflags.cc | 2 +- test/commandlineflags_gtest.cc | 144 ++++++++++++++++----------------- 2 files changed, 73 insertions(+), 73 deletions(-) diff --git a/src/commandlineflags.cc b/src/commandlineflags.cc index 3380a127..0648fe3a 100644 --- a/src/commandlineflags.cc +++ b/src/commandlineflags.cc @@ -88,7 +88,7 @@ static std::string FlagToEnvVar(const char* flag) { for (size_t i = 0; i != flag_str.length(); ++i) env_var += static_cast(::toupper(flag_str.c_str()[i])); - return "BENCHMARK_" + env_var; + return env_var; } } // namespace diff --git a/test/commandlineflags_gtest.cc b/test/commandlineflags_gtest.cc index 36bdb445..656020f2 100644 --- a/test/commandlineflags_gtest.cc +++ b/test/commandlineflags_gtest.cc @@ -26,175 +26,175 @@ int unsetenv(const char* name) { #endif // BENCHMARK_OS_WINDOWS TEST(BoolFromEnv, Default) { - ASSERT_EQ(unsetenv("BENCHMARK_NOT_IN_ENV"), 0); + ASSERT_EQ(unsetenv("NOT_IN_ENV"), 0); EXPECT_EQ(BoolFromEnv("not_in_env", true), true); } TEST(BoolFromEnv, False) { - ASSERT_EQ(setenv("BENCHMARK_IN_ENV", "0", 1), 0); + ASSERT_EQ(setenv("IN_ENV", "0", 1), 0); EXPECT_EQ(BoolFromEnv("in_env", true), false); - unsetenv("BENCHMARK_IN_ENV"); + unsetenv("IN_ENV"); - ASSERT_EQ(setenv("BENCHMARK_IN_ENV", "N", 1), 0); + ASSERT_EQ(setenv("IN_ENV", "N", 1), 0); EXPECT_EQ(BoolFromEnv("in_env", true), false); - unsetenv("BENCHMARK_IN_ENV"); + unsetenv("IN_ENV"); - ASSERT_EQ(setenv("BENCHMARK_IN_ENV", "n", 1), 0); + ASSERT_EQ(setenv("IN_ENV", "n", 1), 0); EXPECT_EQ(BoolFromEnv("in_env", true), false); - unsetenv("BENCHMARK_IN_ENV"); + unsetenv("IN_ENV"); - ASSERT_EQ(setenv("BENCHMARK_IN_ENV", "NO", 1), 0); + ASSERT_EQ(setenv("IN_ENV", "NO", 1), 0); EXPECT_EQ(BoolFromEnv("in_env", true), false); - unsetenv("BENCHMARK_IN_ENV"); + unsetenv("IN_ENV"); - ASSERT_EQ(setenv("BENCHMARK_IN_ENV", "No", 1), 0); + ASSERT_EQ(setenv("IN_ENV", "No", 1), 0); EXPECT_EQ(BoolFromEnv("in_env", true), false); - unsetenv("BENCHMARK_IN_ENV"); + unsetenv("IN_ENV"); - ASSERT_EQ(setenv("BENCHMARK_IN_ENV", "no", 1), 0); + ASSERT_EQ(setenv("IN_ENV", "no", 1), 0); EXPECT_EQ(BoolFromEnv("in_env", true), false); - unsetenv("BENCHMARK_IN_ENV"); + unsetenv("IN_ENV"); - ASSERT_EQ(setenv("BENCHMARK_IN_ENV", "F", 1), 0); + ASSERT_EQ(setenv("IN_ENV", "F", 1), 0); EXPECT_EQ(BoolFromEnv("in_env", true), false); - unsetenv("BENCHMARK_IN_ENV"); + unsetenv("IN_ENV"); - ASSERT_EQ(setenv("BENCHMARK_IN_ENV", "f", 1), 0); + ASSERT_EQ(setenv("IN_ENV", "f", 1), 0); EXPECT_EQ(BoolFromEnv("in_env", true), false); - unsetenv("BENCHMARK_IN_ENV"); + unsetenv("IN_ENV"); - ASSERT_EQ(setenv("BENCHMARK_IN_ENV", "FALSE", 1), 0); + ASSERT_EQ(setenv("IN_ENV", "FALSE", 1), 0); EXPECT_EQ(BoolFromEnv("in_env", true), false); - unsetenv("BENCHMARK_IN_ENV"); + unsetenv("IN_ENV"); - ASSERT_EQ(setenv("BENCHMARK_IN_ENV", "False", 1), 0); + ASSERT_EQ(setenv("IN_ENV", "False", 1), 0); EXPECT_EQ(BoolFromEnv("in_env", true), false); - unsetenv("BENCHMARK_IN_ENV"); + unsetenv("IN_ENV"); - ASSERT_EQ(setenv("BENCHMARK_IN_ENV", "false", 1), 0); + ASSERT_EQ(setenv("IN_ENV", "false", 1), 0); EXPECT_EQ(BoolFromEnv("in_env", true), false); - unsetenv("BENCHMARK_IN_ENV"); + unsetenv("IN_ENV"); - ASSERT_EQ(setenv("BENCHMARK_IN_ENV", "OFF", 1), 0); + ASSERT_EQ(setenv("IN_ENV", "OFF", 1), 0); EXPECT_EQ(BoolFromEnv("in_env", true), false); - unsetenv("BENCHMARK_IN_ENV"); + unsetenv("IN_ENV"); - ASSERT_EQ(setenv("BENCHMARK_IN_ENV", "Off", 1), 0); + ASSERT_EQ(setenv("IN_ENV", "Off", 1), 0); EXPECT_EQ(BoolFromEnv("in_env", true), false); - unsetenv("BENCHMARK_IN_ENV"); + unsetenv("IN_ENV"); - ASSERT_EQ(setenv("BENCHMARK_IN_ENV", "off", 1), 0); + ASSERT_EQ(setenv("IN_ENV", "off", 1), 0); EXPECT_EQ(BoolFromEnv("in_env", true), false); - unsetenv("BENCHMARK_IN_ENV"); + unsetenv("IN_ENV"); } TEST(BoolFromEnv, True) { - ASSERT_EQ(setenv("BENCHMARK_IN_ENV", "1", 1), 0); + ASSERT_EQ(setenv("IN_ENV", "1", 1), 0); EXPECT_EQ(BoolFromEnv("in_env", false), true); - unsetenv("BENCHMARK_IN_ENV"); + unsetenv("IN_ENV"); - ASSERT_EQ(setenv("BENCHMARK_IN_ENV", "Y", 1), 0); + ASSERT_EQ(setenv("IN_ENV", "Y", 1), 0); EXPECT_EQ(BoolFromEnv("in_env", false), true); - unsetenv("BENCHMARK_IN_ENV"); + unsetenv("IN_ENV"); - ASSERT_EQ(setenv("BENCHMARK_IN_ENV", "y", 1), 0); + ASSERT_EQ(setenv("IN_ENV", "y", 1), 0); EXPECT_EQ(BoolFromEnv("in_env", false), true); - unsetenv("BENCHMARK_IN_ENV"); + unsetenv("IN_ENV"); - ASSERT_EQ(setenv("BENCHMARK_IN_ENV", "YES", 1), 0); + ASSERT_EQ(setenv("IN_ENV", "YES", 1), 0); EXPECT_EQ(BoolFromEnv("in_env", false), true); - unsetenv("BENCHMARK_IN_ENV"); + unsetenv("IN_ENV"); - ASSERT_EQ(setenv("BENCHMARK_IN_ENV", "Yes", 1), 0); + ASSERT_EQ(setenv("IN_ENV", "Yes", 1), 0); EXPECT_EQ(BoolFromEnv("in_env", false), true); - unsetenv("BENCHMARK_IN_ENV"); + unsetenv("IN_ENV"); - ASSERT_EQ(setenv("BENCHMARK_IN_ENV", "yes", 1), 0); + ASSERT_EQ(setenv("IN_ENV", "yes", 1), 0); EXPECT_EQ(BoolFromEnv("in_env", false), true); - unsetenv("BENCHMARK_IN_ENV"); + unsetenv("IN_ENV"); - ASSERT_EQ(setenv("BENCHMARK_IN_ENV", "T", 1), 0); + ASSERT_EQ(setenv("IN_ENV", "T", 1), 0); EXPECT_EQ(BoolFromEnv("in_env", false), true); - unsetenv("BENCHMARK_IN_ENV"); + unsetenv("IN_ENV"); - ASSERT_EQ(setenv("BENCHMARK_IN_ENV", "t", 1), 0); + ASSERT_EQ(setenv("IN_ENV", "t", 1), 0); EXPECT_EQ(BoolFromEnv("in_env", false), true); - unsetenv("BENCHMARK_IN_ENV"); + unsetenv("IN_ENV"); - ASSERT_EQ(setenv("BENCHMARK_IN_ENV", "TRUE", 1), 0); + ASSERT_EQ(setenv("IN_ENV", "TRUE", 1), 0); EXPECT_EQ(BoolFromEnv("in_env", false), true); - unsetenv("BENCHMARK_IN_ENV"); + unsetenv("IN_ENV"); - ASSERT_EQ(setenv("BENCHMARK_IN_ENV", "True", 1), 0); + ASSERT_EQ(setenv("IN_ENV", "True", 1), 0); EXPECT_EQ(BoolFromEnv("in_env", false), true); - unsetenv("BENCHMARK_IN_ENV"); + unsetenv("IN_ENV"); - ASSERT_EQ(setenv("BENCHMARK_IN_ENV", "true", 1), 0); + ASSERT_EQ(setenv("IN_ENV", "true", 1), 0); EXPECT_EQ(BoolFromEnv("in_env", false), true); - unsetenv("BENCHMARK_IN_ENV"); + unsetenv("IN_ENV"); - ASSERT_EQ(setenv("BENCHMARK_IN_ENV", "ON", 1), 0); + ASSERT_EQ(setenv("IN_ENV", "ON", 1), 0); EXPECT_EQ(BoolFromEnv("in_env", false), true); - unsetenv("BENCHMARK_IN_ENV"); + unsetenv("IN_ENV"); - ASSERT_EQ(setenv("BENCHMARK_IN_ENV", "On", 1), 0); + ASSERT_EQ(setenv("IN_ENV", "On", 1), 0); EXPECT_EQ(BoolFromEnv("in_env", false), true); - unsetenv("BENCHMARK_IN_ENV"); + unsetenv("IN_ENV"); - ASSERT_EQ(setenv("BENCHMARK_IN_ENV", "on", 1), 0); + ASSERT_EQ(setenv("IN_ENV", "on", 1), 0); EXPECT_EQ(BoolFromEnv("in_env", false), true); - unsetenv("BENCHMARK_IN_ENV"); + unsetenv("IN_ENV"); #ifndef BENCHMARK_OS_WINDOWS - ASSERT_EQ(setenv("BENCHMARK_IN_ENV", "", 1), 0); + ASSERT_EQ(setenv("IN_ENV", "", 1), 0); EXPECT_EQ(BoolFromEnv("in_env", false), true); - unsetenv("BENCHMARK_IN_ENV"); + unsetenv("IN_ENV"); #endif } TEST(Int32FromEnv, NotInEnv) { - ASSERT_EQ(unsetenv("BENCHMARK_NOT_IN_ENV"), 0); + ASSERT_EQ(unsetenv("NOT_IN_ENV"), 0); EXPECT_EQ(Int32FromEnv("not_in_env", 42), 42); } TEST(Int32FromEnv, InvalidInteger) { - ASSERT_EQ(setenv("BENCHMARK_IN_ENV", "foo", 1), 0); + ASSERT_EQ(setenv("IN_ENV", "foo", 1), 0); EXPECT_EQ(Int32FromEnv("in_env", 42), 42); - unsetenv("BENCHMARK_IN_ENV"); + unsetenv("IN_ENV"); } TEST(Int32FromEnv, ValidInteger) { - ASSERT_EQ(setenv("BENCHMARK_IN_ENV", "42", 1), 0); + ASSERT_EQ(setenv("IN_ENV", "42", 1), 0); EXPECT_EQ(Int32FromEnv("in_env", 64), 42); - unsetenv("BENCHMARK_IN_ENV"); + unsetenv("IN_ENV"); } TEST(DoubleFromEnv, NotInEnv) { - ASSERT_EQ(unsetenv("BENCHMARK_NOT_IN_ENV"), 0); + ASSERT_EQ(unsetenv("NOT_IN_ENV"), 0); EXPECT_EQ(DoubleFromEnv("not_in_env", 0.51), 0.51); } TEST(DoubleFromEnv, InvalidReal) { - ASSERT_EQ(setenv("BENCHMARK_IN_ENV", "foo", 1), 0); + ASSERT_EQ(setenv("IN_ENV", "foo", 1), 0); EXPECT_EQ(DoubleFromEnv("in_env", 0.51), 0.51); - unsetenv("BENCHMARK_IN_ENV"); + unsetenv("IN_ENV"); } TEST(DoubleFromEnv, ValidReal) { - ASSERT_EQ(setenv("BENCHMARK_IN_ENV", "0.51", 1), 0); + ASSERT_EQ(setenv("IN_ENV", "0.51", 1), 0); EXPECT_EQ(DoubleFromEnv("in_env", 0.71), 0.51); - unsetenv("BENCHMARK_IN_ENV"); + unsetenv("IN_ENV"); } TEST(StringFromEnv, Default) { - ASSERT_EQ(unsetenv("BENCHMARK_NOT_IN_ENV"), 0); + ASSERT_EQ(unsetenv("NOT_IN_ENV"), 0); EXPECT_STREQ(StringFromEnv("not_in_env", "foo"), "foo"); } TEST(StringFromEnv, Valid) { - ASSERT_EQ(setenv("BENCHMARK_IN_ENV", "foo", 1), 0); + ASSERT_EQ(setenv("IN_ENV", "foo", 1), 0); EXPECT_STREQ(StringFromEnv("in_env", "bar"), "foo"); - unsetenv("BENCHMARK_IN_ENV"); + unsetenv("IN_ENV"); } } // namespace