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

fix(file_handler): avoid writing extra newline character #2335

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 1 addition & 10 deletions src/file_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,7 @@ namespace file_handler {
}

std::ifstream in(path);

std::string input;
std::string base64_cert;

while (!in.eof()) {
std::getline(in, input);
base64_cert += input + '\n';
}

return base64_cert;
return std::string { (std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>() };
}

/**
Expand Down
50 changes: 45 additions & 5 deletions tests/unit/test_file_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,54 @@

#include <tests/conftest.cpp>

TEST(FileHandlerTests, WriteFileTest) {
EXPECT_EQ(file_handler::write_file("write_file_test.txt", "test"), 0);
class FileHandlerTests: public virtual BaseTest, public ::testing::WithParamInterface<std::tuple<int, std::string>> {
protected:
void
SetUp() override {
BaseTest::SetUp();
}

void
TearDown() override {
BaseTest::TearDown();
}
};
INSTANTIATE_TEST_SUITE_P(
TestFiles,
FileHandlerTests,
::testing::Values(
std::make_tuple(0, ""), // empty file
std::make_tuple(1, "a"), // single character
std::make_tuple(2, "Mr. Blue Sky - Electric Light Orchestra"), // single line
std::make_tuple(3, R"(
Morning! Today's forecast calls for blue skies
The sun is shining in the sky
There ain't a cloud in sight
It's stopped raining
Everybody's in the play
And don't you know, it's a beautiful new day
Hey, hey, hey!
Running down the avenue
See how the sun shines brightly in the city
All the streets where once was pity
Mr. Blue Sky is living here today!
Hey, hey, hey!
)") // multi-line
));

TEST_P(FileHandlerTests, WriteFileTest) {
auto [fileNum, content] = GetParam();
std::string fileName = "write_file_test_" + std::to_string(fileNum) + ".txt";
EXPECT_EQ(file_handler::write_file(fileName.c_str(), content), 0);
}

TEST(FileHandlerTests, ReadFileTest) {
// read file from WriteFileTest
EXPECT_EQ(file_handler::read_file("write_file_test.txt"), "test\n"); // sunshine adds a newline
TEST_P(FileHandlerTests, ReadFileTest) {
auto [fileNum, content] = GetParam();
std::string fileName = "write_file_test_" + std::to_string(fileNum) + ".txt";
EXPECT_EQ(file_handler::read_file(fileName.c_str()), content);
}

TEST(FileHandlerTests, ReadMissingFileTest) {
// read missing file
EXPECT_EQ(file_handler::read_file("non-existing-file.txt"), "");
}
Loading