Search code examples
absolute-pathbazeltest-data

bazel testdata absolute path need '/external/'


I have a test:

cc_test(
    name = "common_test",
    linkstatic = 1,
    srcs = glob([
        "common/test/*.cpp",
    ]),
    linkopts = [
        "-L/usr/local/lib",
        "-L/usr/lib64",
        "-g",
        "-lcppunit",
        "-ldl",
        "-lz",
        "-ltbb",
        "-llz4",
    ],
    data= [
        "@:testdata"
    ]
)

my testdata is define in:

filegroup(
    name = "testdata",
    srcs = glob([
        "testdata/biz_test/*.json",
    ]),
    visibility = ["//visibility:public"]
)

the TEST_PATH get by here :

TESTBASE_BASE() {
    char cwdBuf[10240];
    if (getcwd(cwdBuf, 10240) == NULL) {
        std::cerr << "get cwd failed, error:" << strerror(errno) << std::endl;
        assert(false);
    }
    _testRootPath = cwdBuf;
}
std::string GET_TEST_DATA_PATH(std::string bazelTestPath = "") const {
    return _testRootPath + bazelTestPath + std::string("/testdata/");
}

in my testcode:

GET_TEST_DATA_PATH() +"/biz_info_test/" can't find the testdata
GET_TEST_DATA_PATH('/external/my_project') +"/biz_info_test/" is ok

Because I have a lot of projects to be changed to bazel compilation, I need a more general way to solve the testdata path problem.I dont want to write code like this:

GET_TEST_DATA_PATH('/external/my_project1') +"/biz_info_test/"
GET_TEST_DATA_PATH('/external/my_project2') +"/biz_info_test/"
GET_TEST_DATA_PATH('/external/my_project3') +"/biz_info_test/"

I hope bazel can do like this: It can find where is my WORKSPACE dir.

my_pj/
├── my_pj
│   └── BUILD
├── testdata
└── WORKSPACE
so my testdata dir is: _testRootPath + std::string("/testdata/")

Solution

  • This is a known issue and we are working on changing it to match your expectation. It is unfortunately a hard change because it impacts all the projects building with Bazel (including everything at Google).