proxygen
FileTest.cpp File Reference

Go to the source code of this file.

Macros

#define EXPECT_CONTAINS(haystack, needle)
 

Functions

 TEST (File, Simple)
 
 TEST (File, SimpleStringPiece)
 
 TEST (File, OwnsFd)
 
 TEST (File, Release)
 
 TEST (File, UsefulError)
 
 TEST (File, Truthy)
 
 TEST (File, HelperCtor)
 

Macro Definition Documentation

#define EXPECT_CONTAINS (   haystack,
  needle 
)
Value:
EXPECT_NE(::std::string::npos, ::folly::StringPiece(haystack).find(needle)) \
<< "Haystack: '" << haystack << "'\nNeedle: '" << needle << "'";
const string needle
#define EXPECT_NE(val1, val2)
Definition: gtest.h:1926

Definition at line 100 of file FileTest.cpp.

Referenced by TEST().

Function Documentation

TEST ( File  ,
Simple   
)

Definition at line 37 of file FileTest.cpp.

References folly::File::close(), EXPECT_EQ, EXPECT_NE, f, folly::File::fd(), and fizz::detail::read().

37  {
38  // Open a file, ensure it's indeed open for reading
39  char buf = 'x';
40  {
41  File f("/etc/hosts");
42  EXPECT_NE(-1, f.fd());
43  EXPECT_EQ(1, ::read(f.fd(), &buf, 1));
44  f.close();
45  EXPECT_EQ(-1, f.fd());
46  }
47 }
auto f
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
size_t read(T &out, folly::io::Cursor &cursor)
Definition: Types-inl.h:258
#define EXPECT_NE(val1, val2)
Definition: gtest.h:1926
TEST ( File  ,
SimpleStringPiece   
)

Definition at line 49 of file FileTest.cpp.

References folly::File::close(), EXPECT_EQ, EXPECT_NE, f, folly::File::fd(), and fizz::detail::read().

49  {
50  char buf = 'x';
51  File f(StringPiece("/etc/hosts"));
52  EXPECT_NE(-1, f.fd());
53  EXPECT_EQ(1, ::read(f.fd(), &buf, 1));
54  f.close();
55  EXPECT_EQ(-1, f.fd());
56 }
auto f
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
size_t read(T &out, folly::io::Cursor &cursor)
Definition: Types-inl.h:258
#define EXPECT_NE(val1, val2)
Definition: gtest.h:1926
Range< const char * > StringPiece
TEST ( File  ,
OwnsFd   
)

Definition at line 58 of file FileTest.cpp.

References folly::netops::close(), EXPECT_EQ, f, folly::File::fd(), folly::gen::move, pipe(), and fizz::detail::read().

58  {
59  // Wrap a file descriptor, make sure that ownsFd works
60  // We'll test that the file descriptor is closed by closing the writing
61  // end of a pipe and making sure that a non-blocking read from the reading
62  // end returns 0.
63 
64  char buf = 'x';
65  int p[2];
66  expectOK(::pipe(p));
67  int flags = ::fcntl(p[0], F_GETFL);
68  expectOK(flags);
69  expectOK(::fcntl(p[0], F_SETFL, flags | O_NONBLOCK));
70  expectWouldBlock(::read(p[0], &buf, 1));
71  {
72  File f(p[1]);
73  EXPECT_EQ(p[1], f.fd());
74  }
75  // Ensure that moving the file doesn't close it
76  {
77  File f(p[1]);
78  EXPECT_EQ(p[1], f.fd());
79  File f1(std::move(f));
80  EXPECT_EQ(-1, f.fd());
81  EXPECT_EQ(p[1], f1.fd());
82  }
83  expectWouldBlock(::read(p[0], &buf, 1)); // not closed
84  {
85  File f(p[1], true);
86  EXPECT_EQ(p[1], f.fd());
87  }
88  ssize_t r = ::read(p[0], &buf, 1); // eof
89  expectOK(r);
90  EXPECT_EQ(0, r);
91  ::close(p[0]);
92 }
auto f
flags
Definition: http_parser.h:127
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
size_t read(T &out, folly::io::Cursor &cursor)
Definition: Types-inl.h:258
int close(NetworkSocket s)
Definition: NetOps.cpp:90
void pipe(CPUExecutor cpu, IOExecutor io)
TEST ( File  ,
Release   
)

Definition at line 94 of file FileTest.cpp.

References folly::File::release().

94  {
95  File in(STDOUT_FILENO, false);
96  CHECK_EQ(STDOUT_FILENO, in.release());
97  CHECK_EQ(-1, in.release());
98 }
TEST ( File  ,
UsefulError   
)

Definition at line 104 of file FileTest.cpp.

References EXPECT_CONTAINS.

104  {
105  try {
106  File("does_not_exist.txt", 0, 0666);
107  } catch (const std::runtime_error& e) {
108  EXPECT_CONTAINS(e.what(), "does_not_exist.txt");
109  EXPECT_CONTAINS(e.what(), "0666");
110  }
111 }
#define EXPECT_CONTAINS(haystack, needle)
Definition: FileTest.cpp:100
TEST ( File  ,
Truthy   
)

Definition at line 113 of file FileTest.cpp.

References ADD_FAILURE, EXPECT_FALSE, EXPECT_TRUE, shell_builder::temp, and folly::File::temporary().

113  {
114  File temp = File::temporary();
115 
116  EXPECT_TRUE(bool(temp));
117 
118  if (temp) {
119  ;
120  } else {
121  ADD_FAILURE();
122  }
123 
124  if (File file = File::temporary()) {
125  ;
126  } else {
127  ADD_FAILURE();
128  }
129 
130  EXPECT_FALSE(bool(File()));
131  if (File()) {
132  ADD_FAILURE();
133  }
134  if (File notOpened = File()) {
135  ADD_FAILURE();
136  }
137 }
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
#define ADD_FAILURE()
Definition: gtest.h:1808
TEST ( File  ,
HelperCtor   
)

Definition at line 139 of file FileTest.cpp.

References EXPECT_EQ, EXPECT_NE, f, folly::File::makeFile(), and fizz::detail::read().

139  {
140  File::makeFile(StringPiece("/etc/hosts")).then([](File&& f) {
141  char buf = 'x';
142  EXPECT_NE(-1, f.fd());
143  EXPECT_EQ(1, ::read(f.fd(), &buf, 1));
144  f.close();
145  EXPECT_EQ(-1, f.fd());
146  });
147 }
auto f
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
size_t read(T &out, folly::io::Cursor &cursor)
Definition: Types-inl.h:258
#define EXPECT_NE(val1, val2)
Definition: gtest.h:1926
Range< const char * > StringPiece