Commit f45ed0ab authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook GitHub Bot

let File tests not use /etc/hosts

Summary: Let `File` tests not use `/etc/hosts` since that file is not portable across all systems. Likewise the fs-util tests using `/usr/lib` and `/usr/bin`.

Reviewed By: chadaustin

Differential Revision: D26447117

fbshipit-source-id: 669a3d7e0ea4efd54ca2efd5c15d8a21227b5c8c
parent 993de579
......@@ -16,6 +16,10 @@
#include <folly/experimental/io/FsUtil.h>
#include <fstream>
#include <random>
#include <fmt/core.h>
#include <glog/logging.h>
#include <folly/portability/GTest.h>
......@@ -23,6 +27,23 @@
using namespace folly;
using namespace folly::fs;
namespace {
class TempDir {
public:
static fs::path make_path() {
std::random_device rng;
std::uniform_int_distribution<uint64_t> dist;
auto basename = "folly-unit-test-" + fmt::format("{:016x}", dist(rng));
return fs::canonical(fs::temp_directory_path()) / basename;
}
fs::path const path{make_path()};
TempDir() { fs::create_directory(path); }
~TempDir() { fs::remove_all(path); }
};
} // namespace
TEST(Simple, Path) {
path root("/");
path abs1("/hello/world");
......@@ -50,10 +71,16 @@ TEST(Simple, Path) {
}
TEST(Simple, CanonicalizeParent) {
path a("/usr/bin/tail");
path b("/usr/lib/../bin/tail");
path c("/usr/bin/DOES_NOT_EXIST_ASDF");
path d("/usr/lib/../bin/DOES_NOT_EXIST_ASDF");
TempDir tmpd;
path a = tmpd.path / "usr/bin/tail";
path b = tmpd.path / "usr/lib/../bin/tail";
path c = tmpd.path / "usr/bin/DOES_NOT_EXIST_ASDF";
path d = tmpd.path / "usr/lib/../bin/DOES_NOT_EXIST_ASDF";
create_directories(a.parent_path());
create_directories(a.parent_path() / "../lib");
std::ofstream(a.native()) << "foobar" << std::endl;
EXPECT_EQ(a, canonical(a));
EXPECT_EQ(a, canonical_parent(b));
......
......@@ -16,6 +16,11 @@
#include <folly/File.h>
#include <fstream>
#include <random>
#include <boost/filesystem.hpp>
#include <fmt/core.h>
#include <glog/logging.h>
#include <folly/String.h>
......@@ -24,7 +29,10 @@
using namespace folly;
namespace fs = boost::filesystem;
namespace {
void expectWouldBlock(ssize_t r) {
int savedErrno = errno;
EXPECT_EQ(-1, r);
......@@ -34,13 +42,31 @@ void expectOK(ssize_t r) {
int savedErrno = errno;
EXPECT_LE(0, r) << ": errno=" << errnoStr(savedErrno);
}
class TempDir {
public:
static fs::path make_path() {
std::random_device rng;
std::uniform_int_distribution<uint64_t> dist;
auto basename = "folly-unit-test-" + fmt::format("{:016x}", dist(rng));
return fs::canonical(fs::temp_directory_path()) / basename;
}
fs::path const path{make_path()};
TempDir() { fs::create_directory(path); }
~TempDir() { fs::remove_all(path); }
};
} // namespace
TEST(File, Simple) {
TempDir tmpd;
auto tmpf = tmpd.path / "foobar.txt";
std::ofstream{tmpf.native()} << "hello world" << std::endl;
// Open a file, ensure it's indeed open for reading
char buf = 'x';
{
File f("/etc/hosts");
File f(tmpf.string());
EXPECT_NE(-1, f.fd());
EXPECT_EQ(1, ::read(f.fd(), &buf, 1));
f.close();
......@@ -49,8 +75,12 @@ TEST(File, Simple) {
}
TEST(File, SimpleStringPiece) {
TempDir tmpd;
auto tmpf = tmpd.path / "foobar.txt";
std::ofstream{tmpf.native()} << "hello world" << std::endl;
char buf = 'x';
File f(StringPiece("/etc/hosts"));
File f(StringPiece(tmpf.string()));
EXPECT_NE(-1, f.fd());
EXPECT_EQ(1, ::read(f.fd(), &buf, 1));
f.close();
......@@ -139,7 +169,10 @@ TEST(File, Truthy) {
}
TEST(File, HelperCtor) {
File::makeFile(StringPiece("/etc/hosts")).then([](File&& f) {
TempDir tmpd;
auto tmpf = tmpd.path / "foobar.txt";
File::makeFile(StringPiece(tmpf.string())).then([](File&& f) {
char buf = 'x';
EXPECT_NE(-1, f.fd());
EXPECT_EQ(1, ::read(f.fd(), &buf, 1));
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment