Add tests for file open in append/append_existing mode

This commit is contained in:
Richard Hodges
2021-12-09 17:23:43 +01:00
parent fb8f57a9e5
commit 88d5deec0f
2 changed files with 30 additions and 0 deletions

View File

@ -1,3 +1,4 @@
* Add tests for file open in append mode.
* Fix file open with append/append_existing flag on Windows.
--------------------------------------------------------------------------------

View File

@ -17,7 +17,9 @@
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/fstream.hpp>
#include <cstdio>
#include <fstream>
#include <string>
#include <iterator>
#include <type_traits>
namespace boost {
@ -100,6 +102,20 @@ test_file()
BEAST_EXPECT(! fs::exists(path));
};
auto const consume_file =
[](fs::path const& path)
{
// no exceptions - failure will result in an empty string
std::ifstream in;
in.open(path.native());
noskipws(in);
auto s = std::string(
std::istream_iterator<char>(in),
std::istream_iterator<char>());
in.close();
return s;
};
temp_path path;
// bad file descriptor
@ -230,7 +246,14 @@ test_file()
f.open(path, file_mode::append, ec);
BEAST_EXPECT(! ec);
BEAST_EXPECT(fs::exists(path));
static const std::string extra = "the";
f.write(extra.c_str(), extra.size(), ec);
BEAST_EXPECT(!ec);
f.close(ec);
auto s = consume_file(path);
BEAST_EXPECT(s == "the");
}
{
File f;
error_code ec;
@ -238,6 +261,12 @@ test_file()
f.open(path, file_mode::append, ec);
BEAST_EXPECT(! ec);
BEAST_EXPECT(fs::exists(path));
static const std::string extra = " cat";
f.write(extra.c_str(), extra.size(), ec);
BEAST_EXPECT(!ec);
f.close(ec);
auto s = consume_file(path);
BEAST_EXPECTS(s == "the cat", s);
}
remove(path);
}