无法在 google 测试中打开文件

Can't open a file inside a google test

我在测试的目录下有一个文件。 由于我使用 Bazel 作为构建系统(在 Linux 中),我不必创建 main 和初始化测试(事实上,我不知道使用 Bazel 时 main 函数所在的位置)。

#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));
}

用于测试的 BUILD 文件:

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/**"]),
)

WORKSPACE 文件:

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",
)

目录结构:

.
├── 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

如果文件与测试或任何其他文件夹位于同一文件夹中,我应该如何引用该文件?

顺便说一句:使用我文件系统根目录的完整路径工作正常。

问题是 glob 是相对于 BUILD 文件的,就像您在 srcs 中引用文件一样。 glob 正在扩展为一个空列表,因为没有文件匹配。查看它的最简单方法是显式放置完整路径而不是 glob,Bazel 会抱怨。 您有两个选择,或者您将资源文件夹移动到测试文件夹中,或者您在资源文件夹中创建一个 BUILD 文件,您在其中创建一个公开 txt 文件的文件组,然后在测试目标中引用该文件组目标。 https://docs.bazel.build/versions/master/be/general.html#filegroup

resources/BUILD

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

tests/BUILD

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