]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
Tests are now run on "make check". Added initial tests for lib-index.
authorTimo Sirainen <tss@iki.fi>
Sat, 9 May 2009 20:20:41 +0000 (16:20 -0400)
committerTimo Sirainen <tss@iki.fi>
Sat, 9 May 2009 20:20:41 +0000 (16:20 -0400)
Moved old tests away from tests/ to libraries' individual directories.

--HG--
branch : HEAD

25 files changed:
.hgignore
configure.in
src/Makefile.am
src/lib-imap/Makefile.am
src/lib-imap/test-imap.c [moved from src/tests/test-imap.c with 97% similarity]
src/lib-index/Makefile.am
src/lib-index/mail-index-transaction-export.c
src/lib-index/mail-index-transaction.c
src/lib-index/mail-transaction-log-append.c
src/lib-index/mail-transaction-log.h
src/lib-index/test-index.c [new file with mode: 0644]
src/lib-index/test-index.h [new file with mode: 0644]
src/lib-index/test-transaction-log-view.c [new file with mode: 0644]
src/lib-mail/Makefile.am
src/lib-mail/test-mail.c [moved from src/tests/test-mail.c with 97% similarity]
src/lib-test/Makefile.am [new file with mode: 0644]
src/lib-test/test-common.c [moved from src/tests/test-common.c with 58% similarity]
src/lib-test/test-common.h [moved from src/tests/test-common.h with 54% similarity]
src/lib-test/test-common.lo [new file with mode: 0644]
src/lib-test/test-common.o [new file with mode: 0644]
src/lib/Makefile.am
src/lib/test-istream.c [moved from src/tests/test-istream.c with 100% similarity]
src/lib/test-lib.c [moved from src/tests/test-lib.c with 99% similarity]
src/lib/test-lib.h [moved from src/tests/test-lib.h with 100% similarity]
src/tests/Makefile.am [deleted file]

index 8c5340ced12c316c533fca0ed363f9d52487a93b..7dbfc7d08624999a4669a60336dff7b42a56f834 100644 (file)
--- a/.hgignore
+++ b/.hgignore
@@ -61,9 +61,13 @@ src/lda/dovecot-lda
 src/dict/dict
 src/imap-login/imap-login
 src/imap/imap
+src/lib/test-lib
 src/lib/unicodemap.c
 src/lib/UnicodeData.txt
 src/lib-dict/dict-drivers-register.c
+src/lib-index/test-index
+src/lib-imap/test-imap
+src/lib-mail/test-mail
 src/lib-sql/sql-drivers-register.c
 src/lib-storage/register/mail-storage-register.c
 src/lib-storage/register/mailbox-list-register.c
@@ -76,9 +80,6 @@ src/plugins/expire/expire-tool
 src/plugins/fts-squat/squat-test
 src/pop3-login/pop3-login
 src/pop3/pop3
-src/tests/test-lib
-src/tests/test-mail
-src/tests/test-imap
 src/util/doveadm
 src/util/dovecotpw
 src/util/gdbhelper
index 02c4c11e2a74f30bf95240e6fbb68289c08a7ae7..11d46bc27123221347bdd340e975e47d08db9673 100644 (file)
@@ -2442,6 +2442,7 @@ src/lib-ntlm/Makefile
 src/lib-otp/Makefile
 src/lib-dovecot/Makefile
 src/lib-settings/Makefile
+src/lib-test/Makefile
 src/lib-storage/Makefile
 src/lib-storage/list/Makefile
 src/lib-storage/index/Makefile
@@ -2465,7 +2466,6 @@ src/login-common/Makefile
 src/master/Makefile
 src/pop3/Makefile
 src/pop3-login/Makefile
-src/tests/Makefile
 src/util/Makefile
 src/plugins/Makefile
 src/plugins/acl/Makefile
index 950496cf7fe980e278c95d35c8d984f30f1c164c..b5627da4e3884090a93a6d03c8dbcf2bd681e9bb 100644 (file)
@@ -9,6 +9,7 @@ LIBDOVECOT_SUBDIRS = \
        lib-settings
 
 SUBDIRS = \
+       lib-test \
        $(LIBDOVECOT_SUBDIRS) \
        lib-dovecot \
        lib-index \
@@ -30,6 +31,10 @@ SUBDIRS = \
        lmtp \
        log \
        config \
-       tests \
        util \
        plugins
+
+test:
+       for dir in $(SUBDIRS); do \
+         cd $$dir && if ! $(MAKE) test; then exit 1; fi; cd ..; \
+       done
index 4920bc9625923c38708d6d1f58d0abb25c4cded2..61e1eb8dfcabae6c0d93894fcc88a765c4bb66ea 100644 (file)
@@ -2,6 +2,7 @@ noinst_LTLIBRARIES = libimap.la
 
 AM_CPPFLAGS = \
        -I$(top_srcdir)/src/lib \
+       -I$(top_srcdir)/src/lib-test \
        -I$(top_srcdir)/src/lib-charset \
        -I$(top_srcdir)/src/lib-mail
 
@@ -38,3 +39,23 @@ if INSTALL_HEADERS
 else
   noinst_HEADERS = $(headers)
 endif
+
+test_programs = test-imap
+noinst_PROGRAMS = $(test_programs)
+
+test_libs = \
+       libimap.la \
+       ../lib-test/libtest.la \
+       ../lib/liblib.la
+
+test_imap_SOURCES = \
+       test-imap.c
+
+test_imap_LDADD = $(test_libs)
+test_imap_DEPENDENCIES = $(test_libs)
+
+check: check-am check-test
+check-test: $(test_programs)
+       for bin in $(test_programs); do \
+         if ! ./$$bin; then exit 1; fi \
+       done
similarity index 97%
rename from src/tests/test-imap.c
rename to src/lib-imap/test-imap.c
index 7605f100e1a1c73f9ce935226b84276f1a441be2..195d27ab77c8f4563c7ee627eedfcd042b2f3639 100644 (file)
@@ -168,9 +168,11 @@ end:
 
 int main(void)
 {
-       test_init();
+       static void (*test_functions[])(void) = {
+               test_imap_match,
+               test_imap_utf7,
 
-       test_imap_match();
-       test_imap_utf7();
-       return test_deinit();
+               NULL
+       };
+       return test_run(test_functions);
 }
index 92b4ae8703ea3bcafe9975697de2364ed0f35fd1..e453001e762258cd5d347f2f555c69145523b62f 100644 (file)
@@ -2,6 +2,7 @@ noinst_LTLIBRARIES = libindex.la
 
 AM_CPPFLAGS = \
        -I$(top_srcdir)/src/lib \
+       -I$(top_srcdir)/src/lib-test \
        -I$(top_srcdir)/src/lib-mail
 
 libindex_la_SOURCES = \
@@ -54,9 +55,34 @@ headers = \
         mailbox-list-index.h \
         mailbox-list-index-private.h
 
+test_programs = test-index
+noinst_PROGRAMS = $(test_programs)
+
+test_libs = \
+       libindex.la \
+       ../lib-test/libtest.la \
+       ../lib-mail/libmail.la \
+       ../lib/liblib.la
+
+test_index_SOURCES = \
+       test-index.c \
+       test-transaction-log-view.c
+
+test_headers = \
+       test-index.h
+
+test_index_LDADD = $(test_libs)
+test_index_DEPENDENCIES = $(test_libs)
+
+check: check-am check-test
+check-test: $(test_programs)
+       for bin in $(test_programs); do \
+         if ! ./$$bin; then exit 1; fi \
+       done
+
 if INSTALL_HEADERS
   pkginc_libdir=$(pkgincludedir)
-  pkginc_lib_HEADERS = $(headers)
+  pkginc_lib_HEADERS = $(headers) $(test_headers)
 else
-  noinst_HEADERS = $(headers)
+  noinst_HEADERS = $(headers) $(test_headers)
 endif
index 9fb2a1ffde8d767b7faef7542f9642c9995ef165..d1df5fcfb6876413b12f33b5fd374ac3cbc52a63 100644 (file)
@@ -16,7 +16,8 @@ static void
 log_append_buffer(struct mail_index_export_context *ctx,
                  const buffer_t *buf, enum mail_transaction_type type)
 {
-       mail_transaction_log_append_add(ctx->append_ctx, type, buf);
+       mail_transaction_log_append_add(ctx->append_ctx, type,
+                                       buf->data, buf->used);
 }
 
 static const buffer_t *
index c082910339186dd8380c5237489d6943b05681c2..eafc3093cc5f74bbf11439e625a0369be5325915 100644 (file)
@@ -267,12 +267,13 @@ mail_transaction_log_file_refresh(struct mail_index_transaction *t,
 static int mail_index_transaction_commit_real(struct mail_index_transaction *t)
 {
        struct mail_transaction_log *log = t->view->index->log;
+       bool external = (t->flags & MAIL_INDEX_TRANSACTION_FLAG_EXTERNAL) != 0;
        struct mail_transaction_log_append_ctx *ctx;
        uint32_t log_seq1, log_seq2;
        uoff_t log_offset1, log_offset2;
        int ret;
 
-       if (mail_transaction_log_append_begin(t, &ctx) < 0)
+       if (mail_transaction_log_append_begin(log->index, external, &ctx) < 0)
                return -1;
        ret = mail_transaction_log_file_refresh(t, ctx);
        if (ret > 0) {
index 013c9f80e3714e540cab5b5c80482ebda8ba6392..c844997ece2d04349a851217a7c25f78ef553e07 100644 (file)
@@ -8,14 +8,14 @@
 
 void mail_transaction_log_append_add(struct mail_transaction_log_append_ctx *ctx,
                                     enum mail_transaction_type type,
-                                    const buffer_t *buf)
+                                    const void *data, size_t size)
 {
        struct mail_transaction_header hdr;
 
        i_assert((type & MAIL_TRANSACTION_TYPE_MASK) != 0);
-       i_assert((buf->used % 4) == 0);
+       i_assert((size % 4) == 0);
 
-       if (buf->used == 0)
+       if (size == 0)
                return;
 
        memset(&hdr, 0, sizeof(hdr));
@@ -24,15 +24,15 @@ void mail_transaction_log_append_add(struct mail_transaction_log_append_ctx *ctx
                hdr.type |= MAIL_TRANSACTION_EXPUNGE_PROT;
        if (ctx->external)
                hdr.type |= MAIL_TRANSACTION_EXTERNAL;
-       hdr.size = sizeof(hdr) + buf->used;
+       hdr.size = sizeof(hdr) + size;
        hdr.size = mail_index_uint32_to_offset(hdr.size);
 
        buffer_append(ctx->output, &hdr, sizeof(hdr));
-       buffer_append(ctx->output, buf->data, buf->used);
+       buffer_append(ctx->output, data, size);
 
-       if (mail_transaction_header_has_modseq(buf->data,
-                               CONST_PTR_OFFSET(buf->data, sizeof(hdr)),
-                               ctx->new_highest_modseq))
+       if (mail_transaction_header_has_modseq(data,
+                                       CONST_PTR_OFFSET(data, sizeof(hdr)),
+                                       ctx->new_highest_modseq))
                ctx->new_highest_modseq++;
 }
 
@@ -158,7 +158,7 @@ log_append_sync_offset_if_needed(struct mail_transaction_log_append_ctx *ctx)
        buffer_append(buf, &offset, sizeof(offset));
 
        mail_transaction_log_append_add(ctx, MAIL_TRANSACTION_HEADER_UPDATE,
-                                       buf);
+                                       buf->data, buf->used);
 }
 
 static int
@@ -189,13 +189,11 @@ mail_transaction_log_append_locked(struct mail_transaction_log_append_ctx *ctx)
        return 0;
 }
 
-int mail_transaction_log_append_begin(struct mail_index_transaction *t,
+int mail_transaction_log_append_begin(struct mail_index *index, bool external,
                                      struct mail_transaction_log_append_ctx **ctx_r)
 {
        struct mail_transaction_log_append_ctx *ctx;
-       struct mail_index *index;
 
-       index = mail_index_view_get_index(t->view);
        if (!index->log_locked) {
                if (mail_transaction_log_lock_head(index->log) < 0)
                        return -1;
@@ -203,7 +201,7 @@ int mail_transaction_log_append_begin(struct mail_index_transaction *t,
        ctx = i_new(struct mail_transaction_log_append_ctx, 1);
        ctx->log = index->log;
        ctx->output = buffer_create_dynamic(default_pool, 1024);
-       ctx->external = (t->flags & MAIL_INDEX_TRANSACTION_FLAG_EXTERNAL) != 0;
+       ctx->external = external;
 
        *ctx_r = ctx;
        return 0;
index 3aaa0380eef825c47de573eb6f7c0b23f4c0bb71..2a44cd43c45497e9bda114e1b7741434892c4d30 100644 (file)
@@ -224,11 +224,11 @@ mail_transaction_log_view_is_corrupted(struct mail_transaction_log_view *view);
 
 void mail_transaction_log_views_close(struct mail_transaction_log *log);
 
-int mail_transaction_log_append_begin(struct mail_index_transaction *t,
+int mail_transaction_log_append_begin(struct mail_index *index, bool external,
                                      struct mail_transaction_log_append_ctx **ctx_r);
 void mail_transaction_log_append_add(struct mail_transaction_log_append_ctx *ctx,
                                     enum mail_transaction_type type,
-                                    const buffer_t *buf);
+                                    const void *data, size_t size);
 int mail_transaction_log_append_commit(struct mail_transaction_log_append_ctx **ctx);
 
 /* Lock transaction log for index synchronization. Log cannot be read or
diff --git a/src/lib-index/test-index.c b/src/lib-index/test-index.c
new file mode 100644 (file)
index 0000000..b55a8a1
--- /dev/null
@@ -0,0 +1,12 @@
+/* Copyright (c) 2009 Dovecot authors, see the included COPYING file */
+
+#include "test-index.h"
+
+int main(void)
+{
+       static void (*test_functions[])(void) = {
+               test_transaction_log_view,
+               NULL
+       };
+       return test_run(test_functions);
+}
diff --git a/src/lib-index/test-index.h b/src/lib-index/test-index.h
new file mode 100644 (file)
index 0000000..b6d1ac0
--- /dev/null
@@ -0,0 +1,9 @@
+#ifndef TEST_INDEX_H
+#define TEST_INDEX_H
+
+#include "lib.h"
+#include "test-common.h"
+
+void test_transaction_log_view(void);
+
+#endif
diff --git a/src/lib-index/test-transaction-log-view.c b/src/lib-index/test-transaction-log-view.c
new file mode 100644 (file)
index 0000000..ab76866
--- /dev/null
@@ -0,0 +1,148 @@
+/* Copyright (c) 2009 Dovecot authors, see the included COPYING file */
+
+#include "test-index.h"
+#include "array.h"
+#include "mail-index-private.h"
+#include "mail-transaction-log-view-private.h"
+
+static struct mail_transaction_log *log;
+static struct mail_transaction_log_view *view;
+
+static void
+test_transaction_log_file_add(uint32_t file_seq)
+{
+       struct mail_transaction_log_file **p, *file;
+
+       file = i_new(struct mail_transaction_log_file, 1);
+       file->hdr.file_seq = file_seq;
+       file->hdr.hdr_size = file->sync_offset = sizeof(file->hdr);
+       file->hdr.prev_file_seq = file_seq - 1;
+       file->hdr.indexid = 1;
+       file->log = log;
+       file->fd = -1;
+
+       /* files must be sorted by file_seq */
+       for (p = &log->files; *p != NULL; p = &(*p)->next) {
+               if ((*p)->hdr.file_seq > file->hdr.file_seq)
+                       break;
+               i_assert((*p)->hdr.file_seq < file->hdr.file_seq);
+       }
+       *p = file;
+       log->head = file;
+}
+
+static bool view_is_file_refed(uint32_t file_seq)
+{
+       struct mail_transaction_log_file *const *files;
+       unsigned int i, count;
+       bool ret = FALSE;
+
+       files = array_get(&view->file_refs, &count);
+       for (i = 0; i < count; i++) {
+               if (files[i]->hdr.file_seq == file_seq) {
+                       i_assert(!ret); /* could be a test too.. */
+                       ret = TRUE;
+               }
+       }
+       return ret;
+}
+
+void test_transaction_log_view(void)
+{
+       const struct mail_transaction_header *hdr;
+       struct mail_transaction_log_append_ctx *append_ctx;
+       struct mail_index_record append_rec;
+       const struct mail_index_record *rec;
+       const void *data;
+       uint32_t seq;
+       uoff_t offset, last_log_size;
+       bool reset;
+
+       test_begin("init");
+       log = i_new(struct mail_transaction_log, 1);
+       log->index = i_new(struct mail_index, 1);
+       log->index->log = log;
+       log->index->log_locked = TRUE;
+       test_transaction_log_file_add(1);
+       test_transaction_log_file_add(2);
+       test_transaction_log_file_add(3);
+
+       /* add an append record to the 3rd log file */
+       memset(&append_rec, 0, sizeof(append_rec));
+       append_rec.uid = 1;
+       test_assert(mail_transaction_log_append_begin(log->index, TRUE, &append_ctx) == 0);
+       mail_transaction_log_append_add(append_ctx, MAIL_TRANSACTION_APPEND,
+                                       &append_rec, sizeof(append_rec));
+       test_assert(mail_transaction_log_append_commit(&append_ctx) == 0);
+       last_log_size = sizeof(struct mail_transaction_log_header) +
+               sizeof(struct mail_transaction_header) + sizeof(append_rec);
+
+       view = mail_transaction_log_view_open(log);
+       test_assert(view != NULL && log->views == view &&
+                   !view_is_file_refed(1) && !view_is_file_refed(2) &&
+                   view_is_file_refed(3));
+       test_end();
+
+       /* we have files 1-3 opened */
+       test_begin("set all");
+       test_assert(mail_transaction_log_view_set(view, 0, 0, (uint32_t)-1, (uoff_t)-1, &reset) == 1 &&
+                   reset && view_is_file_refed(1) && view_is_file_refed(2) &&
+                   view_is_file_refed(3) &&
+                   !mail_transaction_log_view_is_corrupted(view));
+       mail_transaction_log_view_get_prev_pos(view, &seq, &offset);
+       test_assert(seq == 1 && offset == sizeof(struct mail_transaction_log_header));
+       test_assert(mail_transaction_log_view_next(view, &hdr, &data) == 1);
+       test_assert(hdr->type == (MAIL_TRANSACTION_APPEND | MAIL_TRANSACTION_EXTERNAL));
+       rec = data;
+       test_assert(memcmp(rec, &append_rec, sizeof(*rec)) == 0);
+       test_assert(mail_transaction_log_view_next(view, &hdr, &data) == 0);
+       test_assert(mail_transaction_log_view_is_last(view));
+       mail_transaction_log_view_get_prev_pos(view, &seq, &offset);
+       test_assert(seq == 3 && offset == last_log_size);
+       test_end();
+
+       test_begin("set first");
+       test_assert(mail_transaction_log_view_set(view, 0, 0, 0, 0, &reset) == 1);
+       mail_transaction_log_view_get_prev_pos(view, &seq, &offset);
+       test_assert(seq == 1 && offset == sizeof(struct mail_transaction_log_header));
+       test_assert(mail_transaction_log_view_next(view, &hdr, &data) == 0);
+       mail_transaction_log_view_get_prev_pos(view, &seq, &offset);
+       test_assert(seq == 1 && offset == sizeof(struct mail_transaction_log_header));
+       test_end();
+
+       test_begin("set end");
+       test_assert(mail_transaction_log_view_set(view, 3, last_log_size, (uint32_t)-1, (uoff_t)-1, &reset) == 1);
+       mail_transaction_log_view_get_prev_pos(view, &seq, &offset);
+       test_assert(seq == 3 && offset == last_log_size);
+       test_assert(mail_transaction_log_view_next(view, &hdr, &data) == 0);
+       mail_transaction_log_view_get_prev_pos(view, &seq, &offset);
+       test_assert(seq == 3 && offset == last_log_size);
+       test_end();
+
+       test_begin("log clear");
+       mail_transaction_log_view_clear(view, 2);
+       test_assert(!view_is_file_refed(1) && view_is_file_refed(2) &&
+                   view_is_file_refed(3));
+       test_end();
+
+       /* --- first file has been removed --- */
+
+       test_begin("removed first");
+       mail_transaction_logs_clean(log);
+       test_assert(log->files->hdr.file_seq == 2);
+       test_end();
+
+       test_begin("set 2-3");
+       test_assert(mail_transaction_log_view_set(view, 2, 0, (uint32_t)-1, (uoff_t)-1, &reset) == 1);
+       test_end();
+
+       test_begin("missing log handing");
+       test_assert(mail_transaction_log_view_set(view, 0, 0, (uint32_t)-1, (uoff_t)-1, &reset) == 0);
+       test_end();
+
+       test_begin("closed log handling");
+       view->log = NULL;
+       test_assert(mail_transaction_log_view_set(view, 0, 0, (uint32_t)-1, (uoff_t)-1, &reset) == -1);
+       view->log = log;
+       test_end();
+}
index 96558dfad0395f34d608d8a30ba0ba802ba598e1..3bf92bcc433d30d5d6dcddcf409aec351dd6fd9f 100644 (file)
@@ -2,6 +2,7 @@ noinst_LTLIBRARIES = libmail.la
 
 AM_CPPFLAGS = \
        -I$(top_srcdir)/src/lib \
+       -I$(top_srcdir)/src/lib-test \
        -I$(top_srcdir)/src/lib-charset
 
 libmail_la_SOURCES = \
@@ -47,3 +48,23 @@ if INSTALL_HEADERS
 else
   noinst_HEADERS = $(headers)
 endif
+
+test_programs = test-mail
+noinst_PROGRAMS = $(test_programs)
+
+test_libs = \
+       libmail.la \
+       ../lib-test/libtest.la \
+       ../lib/liblib.la
+
+test_mail_SOURCES = \
+       test-mail.c
+
+test_mail_LDADD = $(test_libs)
+test_mail_DEPENDENCIES = $(test_libs)
+
+check: check-am check-test
+check-test: $(test_programs)
+       for bin in $(test_programs); do \
+         if ! ./$$bin; then exit 1; fi \
+       done
similarity index 97%
rename from src/tests/test-mail.c
rename to src/lib-mail/test-mail.c
index ab2b135c6b4a4d01eda4eaebd298cc442088d2bd..339052a73927cd3c272ee2df62adf1ccaeeb4e9b 100644 (file)
@@ -356,12 +356,14 @@ static void test_istream_filter(void)
 
 int main(void)
 {
-       test_init();
-
-       test_message_address();
-       test_message_date_parse();
-       test_message_parser();
-       test_rfc2231_parser();
-       test_istream_filter();
-       return test_deinit();
+       static void (*test_functions[])(void) = {
+               test_message_address,
+               test_message_date_parse,
+               test_message_parser,
+               test_rfc2231_parser,
+               test_istream_filter,
+
+               NULL
+       };
+       return test_run(test_functions);
 }
diff --git a/src/lib-test/Makefile.am b/src/lib-test/Makefile.am
new file mode 100644 (file)
index 0000000..0be31e3
--- /dev/null
@@ -0,0 +1,18 @@
+noinst_LTLIBRARIES = libtest.la
+
+AM_CPPFLAGS = \
+       -I$(top_srcdir)/src/lib \
+       -I$(top_srcdir)/src/lib-charset
+
+libtest_la_SOURCES = \
+       test-common.c
+
+headers = \
+       test-common.h
+
+if INSTALL_HEADERS
+  pkginc_libdir=$(pkgincludedir)
+  pkginc_lib_HEADERS = $(headers)
+else
+  noinst_HEADERS = $(headers)
+endif
similarity index 58%
rename from src/tests/test-common.c
rename to src/lib-test/test-common.c
index 630cc7124fb7d5bfd43db96eef481a3533551976..641c4b167fef02d5fec02e51f28663dba4c78519 100644 (file)
@@ -6,8 +6,10 @@
 
 #include <stdio.h>
 
-#define OUT_NAME_ALIGN 30
+#define OUT_NAME_ALIGN 70
 
+static char *test_prefix;
+static bool test_success;
 static unsigned int failure_count;
 static unsigned int total_count;
 
@@ -47,6 +49,28 @@ void test_istream_set_allow_eof(struct istream *input, bool allow)
        input->real_stream->read = allow ? test_read : test_noread;
 }
 
+void test_begin(const char *name)
+{
+       i_assert(test_prefix == NULL);
+       test_prefix = i_strdup(name);
+       test_success = TRUE;
+}
+
+void test_assert_failed(const char *code, const char *file, unsigned int line)
+{
+       printf("%s:%u: Assert failed: %s\n", file, line, code);
+       test_success = FALSE;
+}
+
+void test_end(void)
+{
+       i_assert(test_prefix != NULL);
+
+       test_out("", test_success);
+       i_free_and_null(test_prefix);
+       test_success = FALSE;
+}
+
 void test_out(const char *name, bool success)
 {
        test_out_reason(name, success, NULL);
@@ -54,17 +78,31 @@ void test_out(const char *name, bool success)
 
 void test_out_reason(const char *name, bool success, const char *reason)
 {
-       int i;
-
-       fputs(name, stdout);
-       putchar(' ');
-       for (i = strlen(name) + 1; i < OUT_NAME_ALIGN; i++)
+       int i = 0;
+
+       if (test_prefix != NULL) {
+               fputs(test_prefix, stdout);
+               i += strlen(test_prefix);
+               if (*name != '\0') {
+                       putchar(':');
+                       i++;
+               }
+               putchar(' ');
+               i++;
+       }
+       if (*name != '\0') {
+               fputs(name, stdout);
+               putchar(' ');
+               i += strlen(name) + 1;
+       }
+       for (; i < OUT_NAME_ALIGN; i++)
                putchar('.');
        fputs(" : ", stdout);
        if (success)
                fputs("ok", stdout);
        else {
                fputs("FAILED", stdout);
+               test_success = FALSE;
                failure_count++;
        }
        if (reason != NULL && *reason != '\0')
@@ -73,16 +111,31 @@ void test_out_reason(const char *name, bool success, const char *reason)
        total_count++;
 }
 
-void test_init(void)
+static void test_init(void)
 {
+       test_prefix = NULL;
        failure_count = 0;
        total_count = 0;
 
        lib_init();
 }
 
-int test_deinit(void)
+static int test_deinit(void)
 {
+       i_assert(test_prefix == NULL);
        printf("%u / %u tests failed\n", failure_count, total_count);
        return failure_count == 0 ? 0 : 1;
 }
+
+int test_run(void (*test_functions[])(void))
+{
+       unsigned int i;
+
+       test_init();
+       for (i = 0; test_functions[i] != NULL; i++) {
+               T_BEGIN {
+                       test_functions[i]();
+               } T_END;
+       }
+       return test_deinit();
+}
similarity index 54%
rename from src/tests/test-common.h
rename to src/lib-test/test-common.h
index a97b8d466b7ec2dfc3e5097be9f37100d8c8a4bf..4978ee2a4e751d1abab0fb97e8f4024bfcfee3d7 100644 (file)
@@ -5,10 +5,16 @@ struct istream *test_istream_create(const char *data);
 void test_istream_set_size(struct istream *input, uoff_t size);
 void test_istream_set_allow_eof(struct istream *input, bool allow);
 
+void test_begin(const char *name);
+#define test_assert(code) STMT_START { \
+       if (!(code)) test_assert_failed(#code, __FILE__, __LINE__); \
+       } STMT_END
+void test_assert_failed(const char *code, const char *file, unsigned int line);
+void test_end(void);
+
 void test_out(const char *name, bool success);
 void test_out_reason(const char *name, bool success, const char *reason);
 
-void test_init(void);
-int test_deinit(void);
+int test_run(void (*test_functions[])(void));
 
 #endif
diff --git a/src/lib-test/test-common.lo b/src/lib-test/test-common.lo
new file mode 100644 (file)
index 0000000..da3d93e
--- /dev/null
@@ -0,0 +1,12 @@
+# test-common.lo - a libtool object file
+# Generated by ltmain.sh (GNU libtool) 2.2.6 Debian-2.2.6a-1ubuntu1
+#
+# Please DO NOT delete this file!
+# It is necessary for linking the library.
+
+# Name of the PIC object.
+pic_object='.libs/test-common.o'
+
+# Name of the non-PIC object
+non_pic_object='test-common.o'
+
diff --git a/src/lib-test/test-common.o b/src/lib-test/test-common.o
new file mode 100644 (file)
index 0000000..8faad3e
Binary files /dev/null and b/src/lib-test/test-common.o differ
index f8cf132ca5248bc467b5b0df50389ee6e9dea060..0453b9a71ea827c35e948417941b412b022e46cd 100644 (file)
@@ -198,9 +198,36 @@ headers = \
        var-expand.h \
        write-full.h
 
+test_programs = test-lib
+noinst_PROGRAMS = $(test_programs)
+
+test_lib_CPPFLAGS = \
+       -I$(top_srcdir)/src/lib-test
+
+test_libs = \
+       ../lib-test/libtest.la \
+       liblib.la
+
+test_lib_SOURCES = \
+       test-lib.c \
+       test-istream.c
+
+test_headers = \
+       test-lib.h
+
+test_lib_LDADD = $(test_libs)
+test_lib_DEPENDENCIES = $(test_libs)
+
+check: check-am check-test
+check-test: $(test_programs)
+       for bin in $(test_programs); do \
+         if ! ./$$bin; then exit 1; fi \
+       done
+
 if INSTALL_HEADERS
   pkginc_libdir=$(pkgincludedir)
   pkginc_lib_HEADERS = $(headers)
+  noinst_HEADERS = $(test_headers)
 else
-  noinst_HEADERS = $(headers)
+  noinst_HEADERS = $(headers) $(test_headers)
 endif
similarity index 99%
rename from src/tests/test-lib.c
rename to src/lib/test-lib.c
index f937fc7623105c4c9b5434087e07b2030dd9ff68..ea19d241ee060c0e76f98e54b54bab147c4ad80c 100644 (file)
@@ -919,13 +919,5 @@ int main(void)
 
                test_istreams
        };
-       unsigned int i;
-
-       test_init();
-       for (i = 0; i < N_ELEMENTS(test_functions); i++) {
-               T_BEGIN {
-                       test_functions[i]();
-               } T_END;
-       }
-       return test_deinit();
+       return test_run(test_functions);
 }
similarity index 100%
rename from src/tests/test-lib.h
rename to src/lib/test-lib.h
diff --git a/src/tests/Makefile.am b/src/tests/Makefile.am
deleted file mode 100644 (file)
index f57a4b9..0000000
+++ /dev/null
@@ -1,40 +0,0 @@
-noinst_PROGRAMS = test-lib test-mail test-imap
-
-noinst_LIBRARIES = libtest.a
-
-AM_CPPFLAGS = \
-       -I$(top_srcdir)/src/lib \
-       -I$(top_srcdir)/src/lib-mail \
-       -I$(top_srcdir)/src/lib-imap \
-       -I$(top_srcdir)/src/lib-index \
-       -I$(top_srcdir)/src/lib-storage
-
-libtest_a_SOURCES = \
-       test-common.c
-
-libs = \
-       libtest.a \
-       $(LIBDOVECOT)
-
-test_lib_SOURCES = \
-       test-istream.c \
-       test-lib.c
-
-test_lib_LDADD = $(libs)
-test_lib_DEPENDENCIES = $(libs)
-
-test_mail_SOURCES = \
-       test-mail.c
-
-test_mail_LDADD = $(libs)
-test_mail_DEPENDENCIES = $(libs)
-
-test_imap_SOURCES = \
-       test-imap.c
-
-noinst_HEADERS = \
-       test-common.h \
-       test-lib.h
-
-test_imap_LDADD = $(libs)
-test_imap_DEPENDENCIES = $(libs)