github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/test/util/test_util_runfiles.cc (about)

     1  // Copyright 2019 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  #include <iostream>
    16  #include <string>
    17  
    18  #include "test/util/fs_util.h"
    19  #include "test/util/test_util.h"
    20  #include "tools/cpp/runfiles/runfiles.h"
    21  
    22  namespace gvisor {
    23  namespace testing {
    24  
    25  std::string RunfilePath(std::string path) {
    26    static const bazel::tools::cpp::runfiles::Runfiles* const runfiles = [] {
    27      std::string error;
    28      auto* runfiles =
    29          bazel::tools::cpp::runfiles::Runfiles::CreateForTest(&error);
    30      if (runfiles == nullptr) {
    31        std::cerr << "Unable to find runfiles: " << error << std::endl;
    32      }
    33      return runfiles;
    34    }();
    35  
    36    if (!runfiles) {
    37      // Can't find runfiles? This probably won't work, but __main__/path is our
    38      // best guess.
    39      return JoinPath("__main__", path);
    40    }
    41  
    42    return runfiles->Rlocation(JoinPath("__main__", path));
    43  }
    44  
    45  }  // namespace testing
    46  }  // namespace gvisor