Skip to content
This repository has been archived by the owner on May 4, 2018. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
AvianFlu authored and bnoordhuis committed Jun 25, 2012
1 parent a416a58 commit 7573f4a
Showing 1 changed file with 31 additions and 39 deletions.
70 changes: 31 additions & 39 deletions test/test-fs-event.c
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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");
Expand All @@ -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;
}

Expand Down Expand Up @@ -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");

Expand All @@ -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;
}
Expand Down Expand Up @@ -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");

Expand All @@ -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;
}
Expand Down Expand Up @@ -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");
Expand All @@ -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;
}
Expand Down

0 comments on commit 7573f4a

Please sign in to comment.