Files
carbon-lang/testing/file_test/file_test_base_test.cpp
T
Jon Ross-Perkins 8aa3d960f5 Merge toolchain file_test children in order to improve linking. (#3206)
Specifically this should improve linking by producing one large binary
instead of one-per-directory. The inclusion of the driver hits the size
issue. Separating out things which have more llvm deps has been
discussed, but I'm not doing that here because I think the semantics
layer will need to depend on clang for interop, and we'd lose a lot of
the benefits that way. Also, having just one place to look seems
simpler.

Includes supporting changes to file_test infrastructure, the most
significant of which is probably passing tests via file instead of a
really large args thing, using a custom rule to do that. That's because
dealing with the layered filegroups that allow the toolchain setup is
more complicated, and this approach scales well.

Combined test time is ~9s, so not sharding right now.

I wasn't sure if people would prefer having the autoupdate script under
testing, so I left it alone for now.
2023-09-11 17:54:41 +00:00

114 lines
3.6 KiB
C++

// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#include "testing/file_test/file_test_base.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "common/ostream.h"
#include "llvm/ADT/StringExtras.h"
namespace Carbon::Testing {
namespace {
using ::testing::Eq;
class FileTestBaseTest : public FileTestBase {
public:
using FileTestBase::FileTestBase;
auto Run(const llvm::SmallVector<llvm::StringRef>& test_args,
llvm::vfs::InMemoryFileSystem& fs, llvm::raw_pwrite_stream& stdout,
llvm::raw_pwrite_stream& stderr) -> ErrorOr<bool> override {
llvm::ArrayRef<llvm::StringRef> args = test_args;
llvm::ListSeparator sep;
stdout << args.size() << " args: ";
for (auto arg : args) {
stdout << sep << "`" << arg << "`";
}
stdout << "\n";
auto filename = std::filesystem::path(test_name().str()).filename();
if (filename == "args.carbon") {
// 'args.carbon' has custom arguments, so don't do regular argument
// validation for it.
return true;
}
if (args.empty() || args.front() != "default_args") {
return ErrorBuilder() << "missing `default_args` argument";
}
args = args.drop_front();
for (auto arg : args) {
if (!fs.exists(arg)) {
return ErrorBuilder() << "Missing file: " << arg;
}
}
if (filename == "example.carbon") {
int delta_line = 10;
stdout << "something\n"
<< "\n"
<< "example.carbon:" << delta_line + 1 << ": Line delta\n"
<< "example.carbon:" << delta_line << ": Negative line delta\n"
<< "+*[]{}\n"
<< "Foo baz\n";
return true;
} else if (filename == "fail_example.carbon") {
stderr << "Oops\n";
return false;
} else if (filename == "two_files.carbon" ||
filename == "not_split.carbon") {
for (auto arg : args) {
// Describe file contents to stdout to validate splitting.
auto file = fs.getBufferForFile(arg, /*FileSize=*/-1,
/*RequiresNullTerminator=*/false);
if (file.getError()) {
return Error(file.getError().message());
}
llvm::StringRef content = file.get()->getBuffer();
stdout << arg << ":1: starts with \"";
stdout.write_escaped(content.take_front(40));
stdout << "\", length " << content.count('\n') << " lines\n";
}
return true;
} else if (filename == "alternating_files.carbon") {
stdout << "unattached message 1\n"
<< "a.carbon:2: message 2\n"
<< "b.carbon:5: message 3\n"
<< "a.carbon:2: message 4\n"
<< "b.carbon:5: message 5\n"
<< "unattached message 6\n";
stderr << "unattached message 1\n"
<< "a.carbon:2: message 2\n"
<< "b.carbon:5: message 3\n"
<< "a.carbon:2: message 4\n"
<< "b.carbon:5: message 5\n"
<< "unattached message 6\n";
return true;
} else if (filename == "unattached_multi_file.carbon") {
stdout << "unattached message 1\n"
<< "unattached message 2\n";
stderr << "unattached message 3\n"
<< "unattached message 4\n";
return true;
} else {
return ErrorBuilder() << "Unexpected file: " << filename;
}
}
auto GetDefaultArgs() -> llvm::SmallVector<std::string> override {
return {"default_args", "%s"};
}
};
} // namespace
CARBON_FILE_TEST_FACTORY(FileTestBaseTest);
} // namespace Carbon::Testing