Skip to content

Commit

Permalink
Merge pull request #2273 from hzeller/feature-20241002-newline-roundtrip
Browse files Browse the repository at this point in the history
Test that various newline characters roundtrip to files properly.
  • Loading branch information
hzeller authored Oct 3, 2024
2 parents 86fbbdd + b01c282 commit 56d75de
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions common/util/file_util_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,13 @@ TEST(FileUtil, GetContentAsMemBlock) {
EXPECT_FALSE(result.status().ok());

const std::string test_file = file::JoinPath(testing::TempDir(), "blockfile");
const absl::string_view test_content = "Some file content";
EXPECT_OK(file::SetContents(test_file, test_content));
constexpr absl::string_view kTestContent = "Some file content\nbaz\r\n";
EXPECT_OK(file::SetContents(test_file, kTestContent));

result = file::GetContentAsMemBlock(test_file);
EXPECT_OK(result.status());
auto &block = *result;
EXPECT_EQ(block->AsStringView(), test_content);
EXPECT_EQ(block->AsStringView(), kTestContent);
}

TEST(FileUtil, JoinPath) {
Expand Down Expand Up @@ -164,12 +164,19 @@ TEST(FileUtil, StatusErrorReporting) {
<< "expect filename prefixed, but got " << content_or.status();

// Write/read roundtrip
const std::string test_file = file::JoinPath(testing::TempDir(), "test-err");
const std::string test_file = file::JoinPath(testing::TempDir(), "test-trip");
unlink(test_file.c_str()); // Remove file if left from previous test.
EXPECT_OK(file::SetContents(test_file, "foo"));
// Add a bunch of text 'special' characcters to make sure even on Windows
// they roundtrip correctly.
constexpr absl::string_view kTestContent = "foo\nbar\r\nbaz\rquux";
EXPECT_OK(file::SetContents(test_file, kTestContent));

// Writing again, should not append, but re-write.
EXPECT_OK(file::SetContents(test_file, kTestContent));

content_or = file::GetContentAsString(test_file);
EXPECT_OK(content_or.status());
EXPECT_EQ(*content_or, "foo");
EXPECT_EQ(*content_or, kTestContent);

const std::string test_dir = file::JoinPath(testing::TempDir(), "test-dir");
ASSERT_OK(file::CreateDir(test_dir));
Expand Down

0 comments on commit 56d75de

Please sign in to comment.