Adam Lesinski | ea78979 | 2016-11-10 14:33:11 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <libgen.h> |
| 18 | |
| 19 | #include <iostream> |
| 20 | #include <memory> |
| 21 | #include <string> |
| 22 | |
| 23 | #include "android-base/file.h" |
| 24 | #include "android-base/strings.h" |
| 25 | #include "gtest/gtest.h" |
| 26 | |
| 27 | #include "TestHelpers.h" |
| 28 | |
| 29 | // Extract the directory of the current executable path. |
| 30 | static std::string GetExecutableDir() { |
| 31 | const std::string path = android::base::GetExecutablePath(); |
| 32 | std::unique_ptr<char, decltype(&std::free)> mutable_path = { |
| 33 | strdup(path.c_str()), std::free}; |
| 34 | std::string executable_dir = dirname(mutable_path.get()); |
| 35 | return executable_dir; |
| 36 | } |
| 37 | |
| 38 | int main(int argc, char** argv) { |
| 39 | ::testing::InitGoogleTest(&argc, argv); |
| 40 | |
| 41 | // Set the default test data path to be the executable path directory. |
| 42 | android::SetTestDataPath(GetExecutableDir()); |
| 43 | |
| 44 | const char* command = argv[0]; |
| 45 | ++argv; |
| 46 | --argc; |
| 47 | |
| 48 | while (argc > 0) { |
| 49 | const std::string arg = *argv; |
| 50 | if (android::base::StartsWith(arg, "--testdata=")) { |
| 51 | android::SetTestDataPath(arg.substr(strlen("--testdata="))); |
| 52 | } else if (arg == "-h" || arg == "--help") { |
| 53 | std::cerr |
| 54 | << "\nAdditional options specific to this test:\n" |
| 55 | " --testdata=[PATH]\n" |
| 56 | " Specify the location of test data used within the tests.\n"; |
| 57 | return 1; |
| 58 | } else { |
| 59 | std::cerr << command << ": Unrecognized argument '" << *argv << "'.\n"; |
| 60 | return 1; |
| 61 | } |
| 62 | |
| 63 | --argc; |
| 64 | ++argv; |
| 65 | } |
| 66 | |
| 67 | std::cerr << "using --testdata=" << android::GetTestDataPath() << "\n"; |
| 68 | return RUN_ALL_TESTS(); |
| 69 | } |