Search code examples
c++googletestbazel

Can't open a file inside a google test


I have a file in the same directory as the test. Since I'm using Bazel as the build system (in Linux) I don't have to create the main and initialize the tests (In fact I don't know where the main function resides when using Bazel).

#include <string>
#include <fstream>
//Other include files
#include "gtest/gtest.h"

TEST(read_file_test, read_file) {
    std::string input_file("file_path");
    graph gr;
    graph_loader loader;

    ASSERT_TRUE(loader.load(gr, input_file));
}

The BUILD file for the tests:

cc_test(
    name = "loaderLib_test",
    srcs = ["loaderLib_test.cpp"],
    deps = [
        "//src/lib/loader:loaderLib",
        "@com_google_googletest//:gtest_main",
    ],
    data = glob(["resources/tests/input_graphs/graph_topology/**"]),
)

The WORKSPACE file:

load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")

git_repository(
    name = "com_google_googletest",
    remote = "https://github.com/google/googletest",
    tag = "release-1.8.1",
)

The directory structure:

.
├── README.md
├── WORKSPACE
├── bazel-bin -> /home/mmaghous/.cache/bazel/_bazel_mmaghous/35542ec7cbabc2e6f7475e3870a798d1/execroot/__main__/bazel-out/k8-fastbuild/bin
├── bazel-graph_processor -> /home/mmaghous/.cache/bazel/_bazel_mmaghous/35542ec7cbabc2e6f7475e3870a798d1/execroot/__main__
├── bazel-out -> /home/mmaghous/.cache/bazel/_bazel_mmaghous/35542ec7cbabc2e6f7475e3870a798d1/execroot/__main__/bazel-out
├── bazel-testlogs -> /home/mmaghous/.cache/bazel/_bazel_mmaghous/35542ec7cbabc2e6f7475e3870a798d1/execroot/__main__/bazel-out/k8-fastbuild/testlogs
├── resources
│   └── tests
│       └── input_graphs
│           ├── graph_data
│           │   └── G1_data.txt
│           └── graph_topology
│               ├── G1_notFully_notWeakly.txt
│               ├── G2_notFully_Weakly.txt
│               ├── G3_fully_weakly.txt
│               ├── G4_fully_weakly.txt
│               ├── G5_notFully_weakly.txt
│               ├── G6_notFully_weakly.txt
│               └── G7_notFully_notWeakly.txt
├── src
│   ├── lib
│   │   ├── algorithms
│   │   │   ├── BUILD
│   │   │   └── graph_algorithms.hpp
│   │   ├── graph
│   │   │   ├── BUILD
│   │   │   └── graph.hpp
│   │   └── loader
│   │       ├── BUILD
│   │       └── graph_loader.hpp
│   └── main
│       ├── BUILD
│       └── main.cpp
├── tests
│   ├── BUILD
│   ├── algorithmsLib_test.cpp
│   ├── graphLib_test.cpp
│   └── loaderLib_test.cpp
└── todo.md

So how should I reference the file if it's in the same folder as the test or any other folder?

BTW: Using the full path from the root of my file systems works fine.


Solution

  • The problem is that the glob is relative to the BUILD file like when you refer a file in srcs. The glob is expanding to an empty list because no file is matching. The easiest way to see it is putting the full path explicitly instead of the glob and Bazel will complain. You have two options, or you move the resources folder inside the tests folder or you create a BUILD file in the resources folders where you create a filegroup exposing the txt files and then in the test target you reference the filegroup target. https://docs.bazel.build/versions/master/be/general.html#filegroup

    In resources/BUILD

    filegroup(
        name = "resources",
        srcs = glob(["tests/input_graphs/graph_topology/**"]),
        visibility = ["//visibility:public"],
    )
    

    In tests/BUILD

    cc_test(
        name = "loaderLib_test",
        srcs = ["loaderLib_test.cpp"],
        deps = [
            "//src/lib/loader:loaderLib",
            "@com_google_googletest//:gtest_main",
        ],
        data = ["//resources"],
    )