[flang-commits] [flang] b564b12 - [flang][driver] Refactor one unit-test case to use fixtures (nfc)
Andrzej Warzynski via flang-commits
flang-commits at lists.llvm.org
Wed Jan 20 11:36:50 PST 2021
Author: Andrzej Warzynski
Date: 2021-01-20T19:36:38Z
New Revision: b564b12bc665c5b9d7148422e4a65871dd31b912
URL: https://github.com/llvm/llvm-project/commit/b564b12bc665c5b9d7148422e4a65871dd31b912
DIFF: https://github.com/llvm/llvm-project/commit/b564b12bc665c5b9d7148422e4a65871dd31b912.diff
LOG: [flang][driver] Refactor one unit-test case to use fixtures (nfc)
Move the unit test from InputOutputTest.cpp to FrontendActionTest.cpp
and re-implement it in terms of the FrontendActionTest fixture. This is
just a small code clean-up and a continuation of:
* https://reviews.llvm.org/D93544
Moving forward, we should try be implementing all unit-test cases for
Flang's frontend actions in terms of FrontendActionTest.
Reviewed By: sameeranjoshi
Differential Revision: https://reviews.llvm.org/D94922
Added:
Modified:
flang/unittests/Frontend/CMakeLists.txt
flang/unittests/Frontend/CompilerInstanceTest.cpp
flang/unittests/Frontend/FrontendActionTest.cpp
Removed:
flang/unittests/Frontend/InputOutputTest.cpp
################################################################################
diff --git a/flang/unittests/Frontend/CMakeLists.txt b/flang/unittests/Frontend/CMakeLists.txt
index fb8160bb023c..7b507b326621 100644
--- a/flang/unittests/Frontend/CMakeLists.txt
+++ b/flang/unittests/Frontend/CMakeLists.txt
@@ -1,6 +1,5 @@
add_flang_unittest(FlangFrontendTests
CompilerInstanceTest.cpp
- InputOutputTest.cpp
FrontendActionTest.cpp
)
diff --git a/flang/unittests/Frontend/CompilerInstanceTest.cpp b/flang/unittests/Frontend/CompilerInstanceTest.cpp
index df4bbb557c4d..b56473ea44e3 100644
--- a/flang/unittests/Frontend/CompilerInstanceTest.cpp
+++ b/flang/unittests/Frontend/CompilerInstanceTest.cpp
@@ -9,7 +9,7 @@
#include "flang/Frontend/CompilerInstance.h"
#include "flang/Frontend/TextDiagnosticPrinter.h"
#include "clang/Basic/DiagnosticOptions.h"
-#include "llvm/Support//FileSystem.h"
+#include "llvm/Support/FileSystem.h"
#include "gtest/gtest.h"
diff --git a/flang/unittests/Frontend/FrontendActionTest.cpp b/flang/unittests/Frontend/FrontendActionTest.cpp
index 2e8bacddaf58..fba46690171d 100644
--- a/flang/unittests/Frontend/FrontendActionTest.cpp
+++ b/flang/unittests/Frontend/FrontendActionTest.cpp
@@ -6,7 +6,6 @@
//
//===----------------------------------------------------------------------===//
-#include "gtest/gtest.h"
#include "flang/Frontend/CompilerInstance.h"
#include "flang/Frontend/CompilerInvocation.h"
#include "flang/Frontend/FrontendOptions.h"
@@ -14,6 +13,8 @@
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/raw_ostream.h"
+#include "gtest/gtest.h"
+
using namespace Fortran::frontend;
namespace {
@@ -79,6 +80,31 @@ class FrontendActionTest : public ::testing::Test {
}
};
+TEST_F(FrontendActionTest, TestInputOutput) {
+ // Populate the input file with the pre-defined input and flush it.
+ *(inputFileOs_) << "End Program arithmetic";
+ inputFileOs_.reset();
+
+ // Set-up the action kind.
+ compInst_.invocation().frontendOpts().programAction_ = InputOutputTest;
+
+ // Set-up the output stream. Using output buffer wrapped as an output
+ // stream, as opposed to an actual file (or a file descriptor).
+ llvm::SmallVector<char, 256> outputFileBuffer;
+ std::unique_ptr<llvm::raw_pwrite_stream> outputFileStream(
+ new llvm::raw_svector_ostream(outputFileBuffer));
+ compInst_.set_outputStream(std::move(outputFileStream));
+
+ // Execute the action.
+ bool success = ExecuteCompilerInvocation(&compInst_);
+
+ // Validate the expected output.
+ EXPECT_TRUE(success);
+ EXPECT_TRUE(!outputFileBuffer.empty());
+ EXPECT_TRUE(llvm::StringRef(outputFileBuffer.data())
+ .startswith("End Program arithmetic"));
+}
+
TEST_F(FrontendActionTest, PrintPreprocessedInput) {
// Populate the input file with the pre-defined input and flush it.
*(inputFileOs_) << "#ifdef NEW\n"
diff --git a/flang/unittests/Frontend/InputOutputTest.cpp b/flang/unittests/Frontend/InputOutputTest.cpp
deleted file mode 100644
index 882182fb1db3..000000000000
--- a/flang/unittests/Frontend/InputOutputTest.cpp
+++ /dev/null
@@ -1,76 +0,0 @@
-//===- unittests/Frontend/OutputStreamTest.cpp --- FrontendAction tests --===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-
-#include "gtest/gtest.h"
-#include "flang/Frontend/CompilerInstance.h"
-#include "flang/Frontend/CompilerInvocation.h"
-#include "flang/Frontend/FrontendOptions.h"
-#include "flang/FrontendTool/Utils.h"
-#include "llvm/Support/FileSystem.h"
-#include "llvm/Support/raw_ostream.h"
-
-using namespace Fortran::frontend;
-
-namespace {
-
-TEST(FrontendAction, TestInputOutputTestAction) {
- std::string inputFile = "io-file-test.f";
- std::error_code ec;
-
- // 1. Create the input file for the file manager
- // AllSources (which is used to manage files inside every compiler instance),
- // works with paths. This means that it requires a physical file. Create one.
- std::unique_ptr<llvm::raw_fd_ostream> os{
- new llvm::raw_fd_ostream(inputFile, ec, llvm::sys::fs::OF_None)};
- if (ec)
- FAIL() << "Failed to create the input file";
-
- // Populate the input file with the pre-defined input and flush it.
- *(os) << "End Program arithmetic";
- os.reset();
-
- // Get the path of the input file
- llvm::SmallString<64> cwd;
- if (std::error_code ec = llvm::sys::fs::current_path(cwd))
- FAIL() << "Failed to obtain the current working directory";
- std::string testFilePath(cwd.c_str());
- testFilePath += "/" + inputFile;
-
- // 2. Prepare the compiler (CompilerInvocation + CompilerInstance)
- CompilerInstance compInst;
- compInst.CreateDiagnostics();
- auto invocation = std::make_shared<CompilerInvocation>();
- invocation->frontendOpts().programAction_ = InputOutputTest;
- compInst.set_invocation(std::move(invocation));
- compInst.frontendOpts().inputs_.push_back(
- FrontendInputFile(/*File=*/testFilePath, Language::Fortran));
-
- // 3. Set-up the output stream. Using output buffer wrapped as an output
- // stream, as opposed to an actual file (or a file descriptor).
- llvm::SmallVector<char, 256> outputFileBuffer;
- std::unique_ptr<llvm::raw_pwrite_stream> outputFileStream(
- new llvm::raw_svector_ostream(outputFileBuffer));
- compInst.set_outputStream(std::move(outputFileStream));
-
- // 4. Run the earlier defined FrontendAction
- bool success = ExecuteCompilerInvocation(&compInst);
-
- EXPECT_TRUE(success);
- EXPECT_TRUE(!outputFileBuffer.empty());
- EXPECT_TRUE(llvm::StringRef(outputFileBuffer.data())
- .startswith("End Program arithmetic"));
-
- // 5. Clear the input and the output files. Since we used an output buffer,
- // there are no physical output files to delete.
- ec = llvm::sys::fs::remove(inputFile);
- if (ec)
- FAIL() << "Failed to delete the test file";
-
- compInst.ClearOutputFiles(/*EraseFiles=*/false);
-}
-} // namespace
More information about the flang-commits
mailing list