xref: /aosp_15_r20/external/openscreen/platform/test/paths_posix.cc (revision 3f982cf4871df8771c9d4abe6e9a6f8d829b2736)
1 // Copyright 2020 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "platform/test/paths.h"
6 #include "platform/test/paths_internal.h"
7 #include "util/osp_logging.h"
8 
9 namespace openscreen {
10 namespace {
11 
ReadTestDataPath()12 std::string ReadTestDataPath() {
13   std::string exe_path = GetExePath();
14   OSP_DCHECK(!exe_path.empty());
15 
16   // NOTE: This assumes that the executable is two directories above the source
17   // root (e.g. out/Debug/unittests).  This is the standard layout GN expects
18   // but is also assumed by Chromium infra.
19   int slashes_found = 0;
20   int i = exe_path.size() - 1;
21   for (; i >= 0; --i) {
22     slashes_found += exe_path[i] == '/';
23     if (slashes_found == 3) {
24       break;
25     }
26   }
27   OSP_DCHECK_EQ(slashes_found, 3);
28 
29   return exe_path.substr(0, i + 1) + OPENSCREEN_TEST_DATA_DIR;
30 }
31 
32 }  // namespace
33 
GetTestDataPath()34 const std::string& GetTestDataPath() {
35   static std::string data_path = ReadTestDataPath();
36   return data_path;
37 }
38 
39 }  // namespace openscreen
40