diff --git a/test/test-fs-event.c b/test/test-fs-event.c index 40d80d23fa..fe7bce2afc 100644 --- a/test/test-fs-event.c +++ b/test/test-fs-event.c @@ -38,26 +38,6 @@ 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; @@ -180,11 +160,14 @@ 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 */ - cleanup_watch_dir(); + 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"); r = uv_fs_event_init(loop, &fs_event, "watch_dir", fs_event_cb_dir, 0); @@ -201,15 +184,22 @@ TEST_IMPL(fs_event_watch_dir) { ASSERT(close_cb_called == 2); /* Cleanup */ - cleanup_watch_dir(); + 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); + 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"); @@ -228,7 +218,10 @@ TEST_IMPL(fs_event_watch_file) { ASSERT(close_cb_called == 2); /* Cleanup */ - cleanup_watch_dir(); + 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); + return 0; } @@ -271,11 +264,13 @@ 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 */ - cleanup_watch_dir(); + uv_fs_unlink(loop, &fs_req, "watch_dir/file1", NULL); + uv_fs_rmdir(loop, &fs_req, "watch_dir", NULL); create_dir(loop, "watch_dir"); create_file(loop, "watch_dir/file1"); @@ -294,7 +289,8 @@ TEST_IMPL(fs_event_no_callback_on_close) { ASSERT(close_cb_called == 1); /* Cleanup */ - cleanup_watch_dir(); + r = uv_fs_unlink(loop, &fs_req, "watch_dir/file1", NULL); + r = uv_fs_rmdir(loop, &fs_req, "watch_dir", NULL); return 0; } @@ -342,12 +338,11 @@ 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"); @@ -364,7 +359,10 @@ TEST_IMPL(fs_event_close_with_pending_event) { ASSERT(close_cb_called == 1); /* Clean up */ - cleanup_watch_dir(); + 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); return 0; } @@ -397,12 +395,11 @@ 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"); @@ -426,7 +423,18 @@ TEST_IMPL(fs_event_close_in_callback) { ASSERT(fs_event_cb_called == 3); /* Clean up */ - cleanup_watch_dir(); + 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); return 0; }