test: improve clean-up in test-fs-event

Failed tests would leave behind extra files, and some tests weren't cleaning up
properly in the first place - this adds a cleanup_watch_dir() helper method to
make all the fs-event tests more consistent.
This commit is contained in:
Charlie McConnell 2012-06-25 14:00:15 -07:00 committed by Ben Noordhuis
parent a416a585b2
commit 7573f4a4c4

View File

@ -38,6 +38,26 @@ static int close_cb_called = 0;
static int fs_event_cb_called = 0;
static int timer_cb_touch_called = 0;
static void cleanup_watch_dir() {
uv_loop_t* loop = uv_default_loop();
uv_fs_t readdir_req;
int i, r;
char *buffer, *name;
r = uv_fs_readdir(loop, &readdir_req, "watch_dir", 0, NULL);
buffer = readdir_req.ptr;
uv_chdir("watch_dir");
for (i = 0; i < readdir_req.result; i++) {
name = buffer;
r = remove(name);
ASSERT(r == 0);
buffer += strlen(name) + 1;
}
uv_chdir("..");
r = remove("watch_dir");
ASSERT(r == 0 || uv_last_error(loop).code == UV_ENOENT);
}
static void create_dir(uv_loop_t* loop, const char* name) {
int r;
uv_fs_t req;
@ -160,14 +180,11 @@ static void timer_cb_touch(uv_timer_t* timer, int status) {
}
TEST_IMPL(fs_event_watch_dir) {
uv_fs_t fs_req;
uv_loop_t* loop = uv_default_loop();
int r;
/* Setup */
uv_fs_unlink(loop, &fs_req, "watch_dir/file1", NULL);
uv_fs_unlink(loop, &fs_req, "watch_dir/file2", NULL);
uv_fs_rmdir(loop, &fs_req, "watch_dir", NULL);
cleanup_watch_dir();
create_dir(loop, "watch_dir");
r = uv_fs_event_init(loop, &fs_event, "watch_dir", fs_event_cb_dir, 0);
@ -184,22 +201,15 @@ TEST_IMPL(fs_event_watch_dir) {
ASSERT(close_cb_called == 2);
/* Cleanup */
r = uv_fs_unlink(loop, &fs_req, "watch_dir/file1", NULL);
r = uv_fs_unlink(loop, &fs_req, "watch_dir/file2", NULL);
r = uv_fs_rmdir(loop, &fs_req, "watch_dir", NULL);
cleanup_watch_dir();
return 0;
}
TEST_IMPL(fs_event_watch_file) {
uv_fs_t fs_req;
uv_loop_t* loop = uv_default_loop();
int r;
/* Setup */
uv_fs_unlink(loop, &fs_req, "watch_dir/file1", NULL);
uv_fs_unlink(loop, &fs_req, "watch_dir/file2", NULL);
uv_fs_rmdir(loop, &fs_req, "watch_dir", NULL);
create_dir(loop, "watch_dir");
create_file(loop, "watch_dir/file1");
create_file(loop, "watch_dir/file2");
@ -218,10 +228,7 @@ TEST_IMPL(fs_event_watch_file) {
ASSERT(close_cb_called == 2);
/* Cleanup */
r = uv_fs_unlink(loop, &fs_req, "watch_dir/file1", NULL);
r = uv_fs_unlink(loop, &fs_req, "watch_dir/file2", NULL);
r = uv_fs_rmdir(loop, &fs_req, "watch_dir", NULL);
cleanup_watch_dir();
return 0;
}
@ -264,13 +271,11 @@ TEST_IMPL(fs_event_watch_file_current_dir) {
TEST_IMPL(fs_event_no_callback_on_close) {
uv_fs_t fs_req;
uv_loop_t* loop = uv_default_loop();
int r;
/* Setup */
uv_fs_unlink(loop, &fs_req, "watch_dir/file1", NULL);
uv_fs_rmdir(loop, &fs_req, "watch_dir", NULL);
cleanup_watch_dir();
create_dir(loop, "watch_dir");
create_file(loop, "watch_dir/file1");
@ -289,8 +294,7 @@ TEST_IMPL(fs_event_no_callback_on_close) {
ASSERT(close_cb_called == 1);
/* Cleanup */
r = uv_fs_unlink(loop, &fs_req, "watch_dir/file1", NULL);
r = uv_fs_rmdir(loop, &fs_req, "watch_dir", NULL);
cleanup_watch_dir();
return 0;
}
@ -338,11 +342,12 @@ TEST_IMPL(fs_event_immediate_close) {
TEST_IMPL(fs_event_close_with_pending_event) {
uv_loop_t* loop;
uv_fs_t fs_req;
int r;
loop = uv_default_loop();
cleanup_watch_dir();
create_dir(loop, "watch_dir");
create_file(loop, "watch_dir/file");
@ -359,10 +364,7 @@ TEST_IMPL(fs_event_close_with_pending_event) {
ASSERT(close_cb_called == 1);
/* Clean up */
r = uv_fs_unlink(loop, &fs_req, "watch_dir/file", NULL);
ASSERT(r == 0);
r = uv_fs_rmdir(loop, &fs_req, "watch_dir", NULL);
ASSERT(r == 0);
cleanup_watch_dir();
return 0;
}
@ -395,11 +397,12 @@ static void fs_event_cb_close(uv_fs_event_t* handle, const char* filename,
TEST_IMPL(fs_event_close_in_callback) {
uv_loop_t* loop;
uv_fs_t fs_req;
int r;
loop = uv_default_loop();
cleanup_watch_dir();
create_dir(loop, "watch_dir");
create_file(loop, "watch_dir/file1");
create_file(loop, "watch_dir/file2");
@ -423,18 +426,7 @@ TEST_IMPL(fs_event_close_in_callback) {
ASSERT(fs_event_cb_called == 3);
/* Clean up */
r = uv_fs_unlink(loop, &fs_req, "watch_dir/file1", NULL);
ASSERT(r == 0);
r = uv_fs_unlink(loop, &fs_req, "watch_dir/file2", NULL);
ASSERT(r == 0);
r = uv_fs_unlink(loop, &fs_req, "watch_dir/file3", NULL);
ASSERT(r == 0);
r = uv_fs_unlink(loop, &fs_req, "watch_dir/file4", NULL);
ASSERT(r == 0);
r = uv_fs_unlink(loop, &fs_req, "watch_dir/file5", NULL);
ASSERT(r == 0);
r = uv_fs_rmdir(loop, &fs_req, "watch_dir", NULL);
ASSERT(r == 0);
cleanup_watch_dir();
return 0;
}