Skip to content

Commit dc32f37

Browse files
committed
firebase_test_framework.cc: Provide an easy way to edit the command-line arguments in integration tests
1 parent 4a34edd commit dc32f37

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

testing/test_framework/src/firebase_test_framework.cc

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
#include "firebase_test_framework.h" // NOLINT
1616

1717
#include <cstdio>
18+
#include <cstring>
19+
#include <string>
20+
#include <vector>
1821

1922
#include "firebase/future.h"
2023

@@ -294,7 +297,57 @@ std::ostream& operator<<(std::ostream& os, const Variant& v) {
294297
}
295298
} // namespace firebase
296299

300+
namespace {
301+
302+
/**
303+
* Makes changes to argc and argv before passing them to `InitGoogleTest`.
304+
*
305+
* This function is a convenience function for developers to edit during
306+
* development/debugging to customize the the arguments specified to googletest
307+
* when directly specifying command-line arguments is not available, such as on
308+
* Android and iOS. For example, to debug a specific test, add the
309+
* --gtest_filter argument, and to list all tests add the --gtest_list_tests
310+
* argument.
311+
*
312+
* @param argc A pointer to the `argc` that will be specified to
313+
* `InitGoogleTest`; the integer to which this pointer points will be updated
314+
* with the new length of `argv`.
315+
* @param argv The `argv` that contains the arguments that would have otherwise
316+
* been specified to `InitGoogleTest()`; they will not be modified.
317+
*
318+
* @return The new `argv` to be specified to `InitGoogleTest()`.
319+
*/
320+
char** EditMainArgsForGoogleTest(int* argc, char* argv[]) {
321+
// Put the args into a vector of strings because modifying string objects in
322+
// a vector is far easier than modifying a char** array.
323+
std::vector<std::string> args_vector;
324+
for (int i = 0; i < *argc; ++i) {
325+
args_vector.push_back(argv[i]);
326+
}
327+
328+
// This is where you can add elements to the `args_vector` vector that will be
329+
// specified to googletest.
330+
// e.g. args_vector.push_back("--gtest_list_tests");
331+
332+
// Write the elements of the vector back into argv and modify argc.
333+
// The memory leaks produced below are acceptable because they would last the
334+
// entire lifetime of the application anyways.
335+
char** new_argv = new char*[args_vector.size()];
336+
for (int i = 0; i < args_vector.size(); ++i) {
337+
const char* arg = args_vector[i].c_str();
338+
char* arg_copy = new char[std::strlen(arg) + 1];
339+
std::strcpy(arg_copy, arg);
340+
new_argv[i] = arg_copy;
341+
}
342+
343+
*argc = static_cast<int>(args_vector.size());
344+
return new_argv;
345+
}
346+
347+
} // namespace
348+
297349
extern "C" int common_main(int argc, char* argv[]) {
350+
argv = EditMainArgsForGoogleTest(&argc, argv);
298351
::testing::InitGoogleTest(&argc, argv);
299352
firebase_test_framework::FirebaseTest::SetArgs(argc, argv);
300353
app_framework::SetLogLevel(app_framework::kDebug);

0 commit comments

Comments
 (0)