Skip to content

Commit

Permalink
Delete the unused SearchUnaryOption function.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 5805419
Change-Id: Ia4020f4c1325877aebac43b7803f15f8a6ee3866
  • Loading branch information
tjgq authored and copybara-github committed Nov 8, 2023
1 parent ff0c35c commit 289d914
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 165 deletions.
63 changes: 0 additions & 63 deletions src/main/cpp/blaze_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -110,69 +110,6 @@ std::vector<std::string> GetAllUnaryOptionValues(
return values;
}

const char* SearchUnaryOption(const vector<string>& args,
const char *key, bool warn_if_dupe) {
if (args.empty()) {
return nullptr;
}

const char* value = nullptr;
bool found_dupe = false; // true if 'key' was found twice
vector<string>::size_type i = 0;

// Examine the first N-1 arguments. (N-1 because we examine the i'th and
// i+1'th together, in case a flag is defined "--name value" style and not
// "--name=value" style.)
for (; i < args.size() - 1; ++i) {
if (args[i] == "--") {
// If the current argument is "--", all following args are target names.
// If 'key' was not found, 'value' is nullptr and we can return that.
// If 'key' was found exactly once, then 'value' has the value and again
// we can return that.
// If 'key' was found more than once then we could not have reached this
// line, because we would have broken out of the loop when 'key' was found
// the second time.
return value;
}
const char* result = GetUnaryOption(args[i].c_str(),
args[i + 1].c_str(),
key);
if (result != nullptr) {
// 'key' was found and 'result' has its value.
if (value) {
// 'key' was found once before, because 'value' is not empty.
found_dupe = true;
break;
} else {
// 'key' was not found before, so store the value in 'value'.
value = result;
}
}
}

if (value) {
// 'value' is not empty, so 'key' was found at least once in the first N-1
// arguments.
if (warn_if_dupe) {
if (!found_dupe) {
// We did not find a duplicate in the first N-1 arguments. Examine the
// last argument, it may be a duplicate.
found_dupe = (GetUnaryOption(args[i].c_str(), nullptr, key) != nullptr);
}
if (found_dupe) {
BAZEL_LOG(WARNING) << key << " is given more than once, "
<< "only the first occurrence is used";
}
}
return value;
} else {
// 'value' is empty, so 'key' was not yet found in the first N-1 arguments.
// If 'key' is in the last argument, we'll parse and return the value from
// that, and if it isn't, we'll return NULL.
return GetUnaryOption(args[i].c_str(), nullptr, key);
}
}

bool SearchNullaryOption(const vector<string>& args,
const string& flag_name,
const bool default_value) {
Expand Down
9 changes: 0 additions & 9 deletions src/main/cpp/blaze_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,6 @@ const char* GetUnaryOption(const char *arg,
// Returns false otherwise.
bool GetNullaryOption(const char *arg, const char *key);

// Searches for 'key' in 'args' using GetUnaryOption. Arguments found after '--'
// are omitted from the search.
// When 'warn_if_dupe' is true, the method checks if 'key' is specified more
// than once and prints a warning if so.
// Returns the value of the 'key' flag iff it occurs in args.
// Returns nullptr otherwise.
const char* SearchUnaryOption(const std::vector<std::string>& args,
const char* key, bool warn_if_dupe);

// Searches for 'key' in 'args' using GetUnaryOption. Arguments found after '--'
// are omitted from the search.
// If 'ignore_after_value' is not nullptr, all values matching the 'key'
Expand Down
114 changes: 21 additions & 93 deletions src/test/cpp/blaze_util_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include "src/main/cpp/blaze_util.h"

#include <fcntl.h>
#include <stddef.h>
#include <stdlib.h>
#include <unistd.h>

#include <string>
#include <utility>
#include <vector>

#include "src/main/cpp/blaze_util.h"
#include "src/main/cpp/blaze_util_platform.h"
#include "src/main/cpp/util/file.h"
#include "googlemock/include/gmock/gmock.h"
#include "googletest/include/gtest/gtest.h"
Expand Down Expand Up @@ -114,10 +114,6 @@ TEST_F(BlazeUtilTest, TestSearchNullaryEmptyCase) {
ASSERT_TRUE(SearchNullaryOption({}, "flag", true));
}

TEST_F(BlazeUtilTest, TestSearchUnaryEmptyCase) {
ASSERT_STREQ(nullptr, SearchUnaryOption({}, "--flag", false));
}

TEST_F(BlazeUtilTest, TestSearchNullaryForEmpty) {
ASSERT_TRUE(SearchNullaryOption({"bazel", "build", ":target"}, "", true));
ASSERT_FALSE(SearchNullaryOption({"bazel", "build", ":target"}, "", false));
Expand Down Expand Up @@ -166,197 +162,129 @@ TEST_F(BlazeUtilTest, TestSearchNullaryLastFlagWins) {
{"bazel", "--flag", "--noflag", "build"}, "flag", true));
}

TEST_F(BlazeUtilTest, TestSearchUnaryForEmpty) {
ASSERT_STREQ(nullptr, SearchUnaryOption({"bazel", "build", ":target"}, "",
false));
}

TEST_F(BlazeUtilTest, TestSearchUnaryFlagNotPresent) {
ASSERT_STREQ(nullptr,
SearchUnaryOption({"bazel", "build", ":target"}, "--flag",
false));
}

TEST_F(BlazeUtilTest, TestSearchUnaryStartupOptionWithEquals) {
ASSERT_STREQ("value",
SearchUnaryOption({"bazel", "--flag=value", "build", ":target"},
"--flag", false));
}

TEST_F(BlazeUtilTest, TestSearchUnaryStartupOptionWithoutEquals) {
ASSERT_STREQ("value",
SearchUnaryOption(
{"bazel", "--flag", "value", "build", ":target"}, "--flag",
false));
}

TEST_F(BlazeUtilTest, TestSearchUnaryCommandOptionWithEquals) {
ASSERT_STREQ("value",
SearchUnaryOption(
{"bazel", "build", ":target", "--flag", "value"}, "--flag",
false));
}

TEST_F(BlazeUtilTest, TestSearchUnaryCommandOptionWithoutEquals) {
ASSERT_STREQ("value",
SearchUnaryOption(
{"bazel", "build", ":target", "--flag=value"}, "--flag",
false));
}

TEST_F(BlazeUtilTest, TestSearchUnarySkipsAfterDashDashWithEquals) {
ASSERT_STREQ(nullptr,
SearchUnaryOption(
{"bazel", "build", ":target", "--", "--flag", "value"},
"--flag", false));
}

TEST_F(BlazeUtilTest, TestSearchUnarySkipsAfterDashDashWithoutEquals) {
ASSERT_STREQ(nullptr,
SearchUnaryOption(
{"bazel", "build", ":target", "--", "--flag=value"},
"--flag", false));
}

TEST_F(BlazeUtilTest, TestSearchUnaryCommandOptionWarnsAboutDuplicates) {
testing::internal::CaptureStderr();
for (int i = 0; i < 2; ++i) {
bool warn_if_dupe = (i == 0);
ASSERT_STREQ("v1",
SearchUnaryOption(
{"foo", "--flag", "v1", "bar", "--flag=v2"}, "--flag",
warn_if_dupe));

if (warn_if_dupe) {
std::string stderr_output = testing::internal::GetCapturedStderr();
EXPECT_THAT(stderr_output, HasSubstr("--flag is given more than once"));
}
}
}

void assert_equal_vector_char_pointer(std::vector<std::string> expected,
std::vector<std::string> actual) {
ASSERT_THAT(actual, testing::ContainerEq(expected));
}

TEST_F(BlazeUtilTest, TestSearchAllUnaryForEmpty) {
TEST_F(BlazeUtilTest, TestGetAllUnaryForEmpty) {
assert_equal_vector_char_pointer(
{}, GetAllUnaryOptionValues({"bazel", "build", ":target"}, ""));
}

TEST_F(BlazeUtilTest, TestSearchAllUnaryFlagNotPresent) {
TEST_F(BlazeUtilTest, TestGetAllUnaryFlagNotPresent) {
assert_equal_vector_char_pointer(
{}, GetAllUnaryOptionValues({"bazel", "build", ":target"}, "--flag"));
}

TEST_F(BlazeUtilTest, TestSearchAllUnaryOptionWithEquals) {
TEST_F(BlazeUtilTest, TestGetAllUnaryOptionWithEquals) {
assert_equal_vector_char_pointer(
{"value"}, GetAllUnaryOptionValues(
{"bazel", "--flag=value", "build", ":target"}, "--flag"));
}

TEST_F(BlazeUtilTest, TestSearchAllUnaryOptionWithEquals2) {
TEST_F(BlazeUtilTest, TestGetAllUnaryOptionWithEquals2) {
assert_equal_vector_char_pointer(
{"value1", "value2"},
GetAllUnaryOptionValues(
{"bazel", "--flag=value1", "--flag=value2", "build", ":target"},
"--flag"));
}

TEST_F(BlazeUtilTest, TestSearchAllUnaryOptionWithRepeatingFlag) {
TEST_F(BlazeUtilTest, TestGetAllUnaryOptionWithRepeatingFlag) {
assert_equal_vector_char_pointer(
{"--flag"}, GetAllUnaryOptionValues({"bazel", "--flag", "--flag",
"value1", "build", ":target"},
"--flag"));
}

TEST_F(BlazeUtilTest, TestSearchAllUnaryOptionWithRepeatingFlagOptions) {
TEST_F(BlazeUtilTest, TestGetAllUnaryOptionWithRepeatingFlagOptions) {
assert_equal_vector_char_pointer(
{"--flag"}, GetAllUnaryOptionValues(
{"bazel", "--flag", "--flag", "value1"}, "--flag"));
}

TEST_F(BlazeUtilTest, TestSearchAllUnaryOptionValuesWithEquals) {
TEST_F(BlazeUtilTest, TestGetAllUnaryOptionValuesWithEquals) {
assert_equal_vector_char_pointer(
{"--flag", "value1"},
GetAllUnaryOptionValues({"bazel", "--flag=--flag", "--flag", "value1"},
"--flag"));
}

TEST_F(BlazeUtilTest, TestSearchAllUnaryOptionWithEquals3) {
TEST_F(BlazeUtilTest, TestGetAllUnaryOptionWithEquals3) {
assert_equal_vector_char_pointer(
{"value1", "value2", "value3"},
GetAllUnaryOptionValues({"bazel", "--flag=value1", "--flag=value2",
"--flag=value3", "build", ":target"},
"--flag"));
}

TEST_F(BlazeUtilTest, TestSearchAllUnaryOptionWithoutEquals) {
TEST_F(BlazeUtilTest, TestGetAllUnaryOptionWithoutEquals) {
assert_equal_vector_char_pointer(
{"value"},
GetAllUnaryOptionValues({"bazel", "--flag", "value", "build", ":target"},
"--flag"));
}

TEST_F(BlazeUtilTest, TestSearchAllUnaryOptionWithoutEquals2) {
TEST_F(BlazeUtilTest, TestGetAllUnaryOptionWithoutEquals2) {
assert_equal_vector_char_pointer(
{"value1", "value2"},
GetAllUnaryOptionValues(
{"bazel", "--flag", "value1", "--flag", "value2", "build", ":target"},
"--flag"));
}

TEST_F(BlazeUtilTest, TestSearchAllUnaryCommandOptionWithEquals) {
TEST_F(BlazeUtilTest, TestGetAllUnaryCommandOptionWithEquals) {
assert_equal_vector_char_pointer(
{"value"},
GetAllUnaryOptionValues({"bazel", "build", ":target", "--flag", "value"},
"--flag"));
}

TEST_F(BlazeUtilTest, TestSearchAllUnaryCommandOptionWithEquals2) {
TEST_F(BlazeUtilTest, TestGetAllUnaryCommandOptionWithEquals2) {
assert_equal_vector_char_pointer(
{"value1", "value2"},
GetAllUnaryOptionValues(
{"bazel", "build", ":target", "--flag", "value1", "--flag", "value2"},
"--flag"));
}

TEST_F(BlazeUtilTest, TestSearchAllUnaryCommandOptionWithoutEquals) {
TEST_F(BlazeUtilTest, TestGetAllUnaryCommandOptionWithoutEquals) {
assert_equal_vector_char_pointer(
{"value"}, GetAllUnaryOptionValues(
{"bazel", "build", ":target", "--flag=value"}, "--flag"));
}

TEST_F(BlazeUtilTest, TestSearchAllUnaryCommandOptionWithoutEquals2) {
TEST_F(BlazeUtilTest, TestGetAllUnaryCommandOptionWithoutEquals2) {
assert_equal_vector_char_pointer(
{"value1", "value2"},
GetAllUnaryOptionValues(
{"bazel", "build", ":target", "--flag=value1", "--flag=value2"},
"--flag"));
}

TEST_F(BlazeUtilTest, TestSearchAllUnarySkipsAfterDashDashWithEquals) {
TEST_F(BlazeUtilTest, TestGetAllUnarySkipsAfterDashDashWithEquals) {
assert_equal_vector_char_pointer(
{},
GetAllUnaryOptionValues(
{"bazel", "build", ":target", "--", "--flag", "value"}, "--flag"));
}

TEST_F(BlazeUtilTest, TestSearchAllUnarySkipsAfterDashDashWithoutEquals) {
TEST_F(BlazeUtilTest, TestGetAllUnarySkipsAfterDashDashWithoutEquals) {
assert_equal_vector_char_pointer(
{}, GetAllUnaryOptionValues(
{"bazel", "build", ":target", "--", "--flag=value"}, "--flag"));
}

TEST_F(BlazeUtilTest, TestSearchAllUnaryCommandOptionWithIgnoreAfter) {
TEST_F(BlazeUtilTest, TestGetAllUnaryCommandOptionWithIgnoreAfter) {
assert_equal_vector_char_pointer(
{"value1", "/dev/null"},
GetAllUnaryOptionValues({"bazel", "build", ":target", "--flag", "value1",
"--flag", "/dev/null", "--flag", "value3"},
"--flag", "/dev/null"));
}

TEST_F(BlazeUtilTest, TestSearchAllUnaryCommandOptionWithIgnoreAfterDevNull) {
TEST_F(BlazeUtilTest, TestGetAllUnaryCommandOptionWithIgnoreAfterDevNull) {
assert_equal_vector_char_pointer(
{"/dev/null"}, GetAllUnaryOptionValues(
{"bazel", "build", ":target", "--flag", "/dev/null",
Expand Down

0 comments on commit 289d914

Please sign in to comment.