1 // Copyright 2019 The Bazel Authors. All rights reserved.
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 <stdio.h>
16
17 #include <memory>
18 #include <string>
19
20 #include "tools/cpp/runfiles/runfiles.h"
21
22 using bazel::tools::cpp::runfiles::Runfiles;
23
main(int argc,char ** argv)24 int main(int argc, char** argv) {
25 std::string error;
26 std::unique_ptr<Runfiles> runfiles(Runfiles::Create(argv[0], &error));
27 if (runfiles == nullptr) {
28 fprintf(stderr, "ERROR(" __FILE__ ":%d): Could not init runfiles\n",
29 __LINE__);
30 return 1;
31 }
32
33 // Even though this cc_binary rule has no data-dependencies, the
34 // native_binary that wraps a copy of this binary does, so we have some
35 // runfiles.
36 std::string path =
37 runfiles->Rlocation("bazel_skylib/tests/native_binary/testdata.txt");
38 if (path.empty()) {
39 fprintf(stderr, "ERROR(" __FILE__ ":%d): Could not find runfile\n",
40 __LINE__);
41 }
42
43 FILE* f = fopen(path.c_str(), "rt");
44 char buf[6];
45 size_t s = fread(buf, 1, 5, f);
46 fclose(f);
47 buf[5] = 0;
48 if (s != 5 || std::string("hello") != std::string(buf, 5)) {
49 fprintf(stderr, "ERROR(" __FILE__ ":%d): bad runfile contents (%s)\n",
50 __LINE__, buf);
51 return 1;
52 }
53
54 return 0;
55 }
56
57