Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing # in the filename tag when __FILE__ is a filename #2328

Open
ivan236634452 opened this issue Dec 8, 2021 · 0 comments
Open

Missing # in the filename tag when __FILE__ is a filename #2328

ivan236634452 opened this issue Dec 8, 2021 · 0 comments

Comments

@ivan236634452
Copy link

@ivan236634452 ivan236634452 commented Dec 8, 2021

Describe the bug
When compiler flags are such that __FILE__ macro is replaced with a relative file path (on MSVC no /Z7, /Zi or /ZI and no /FC) then it might be replaced with just a filename with no path separators. In this case tags for tests with --filenames-as-tags do not contain '#' symbol.

The bug is present in the 2.x branch, but not in the main branch where the relevant code has been redesigned.
The culprit is the following code in catch_session.cpp:

void applyFilenamesAsTags(Catch::IConfig const& config) {
    auto& tests = const_cast<std::vector<TestCase>&>(getAllTestCasesSorted(config));
    for (auto& testCase : tests) {
        auto tags = testCase.tags;

        std::string filename = testCase.lineInfo.file;
        auto lastSlash = filename.find_last_of("\\/");
        if (lastSlash != std::string::npos) {
            filename.erase(0, lastSlash);
            filename[0] = '#';
        }

        auto lastDot = filename.find_last_of('.');
        if (lastDot != std::string::npos) {
            filename.erase(lastDot);
        }

        tags.push_back(std::move(filename));
        setTags(testCase, tags);
    }
}

Adding the following nonintrusive but uninspired adjustment resolves the issue:

void applyFilenamesAsTags(Catch::IConfig const& config) {
    auto& tests = const_cast<std::vector<TestCase>&>(getAllTestCasesSorted(config));
    for (auto& testCase : tests) {
        auto tags = testCase.tags;

        std::string filename = testCase.lineInfo.file;
        auto lastSlash = filename.find_last_of("\\/");
        if (lastSlash != std::string::npos) {
            filename.erase(0, lastSlash);
            filename[0] = '#';
        }
        else
        {
            filename.insert(0, "#");
        }

        auto lastDot = filename.find_last_of('.');
        if (lastDot != std::string::npos) {
            filename.erase(lastDot);
        }

        tags.push_back(std::move(filename));
        setTags(testCase, tags);
    }
}

Expected behavior
Symbol # always prefixes filename tags.

Reproduction steps
Compiler flags (MSVC): no /Z7, /Zi, /ZI, /FC
Catch flags: [#test_file] --filenames-as-tags
Place test_file.cpp with any test cases in the root project directory and try to run them.
In the current 2.x branch no tests would be found.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants