tests: Build file test with ctest
This changes test output slightly, in that files are now placed under subdirectories of $XDG_USER_DATA_DIR/ocarina-test/ instead of in this directory directly. This should help avoid conflicting files, and lets me use a single "rm" command to remove everything before testing. Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
parent
9f2811a343
commit
fc88f68d6b
|
@ -0,0 +1 @@
|
||||||
|
set(CTEST_CUSTOM_PRE_TEST "rm -rf $ENV{HOME}/.local/share/ocarina-test $ENV{HOME}/.cache/ocarina-test")
|
|
@ -19,7 +19,7 @@ def testing_group(group):
|
||||||
return False
|
return False
|
||||||
check_sanity = testing_group([])
|
check_sanity = testing_group([])
|
||||||
|
|
||||||
env.Append(CCFLAGS = "-DCONFIG_TESTING")
|
env.Append(CCFLAGS = [ "-DCONFIG_TESTING", "-DCONFIG_TESTING_DIR=\"\"" ])
|
||||||
env.UsePackage("glib-2.0")
|
env.UsePackage("glib-2.0")
|
||||||
test_o = env.Object("test", "test.c")
|
test_o = env.Object("test", "test.c")
|
||||||
|
|
||||||
|
|
|
@ -6,3 +6,4 @@ endfunction()
|
||||||
|
|
||||||
core_unit_test(Version)
|
core_unit_test(Version)
|
||||||
core_unit_test(String)
|
core_unit_test(String)
|
||||||
|
core_unit_test(File)
|
||||||
|
|
|
@ -25,7 +25,7 @@ Export("core_objs", "CoreTest")
|
||||||
|
|
||||||
|
|
||||||
core_objs += [ env.Object("../../core/string.c") ]
|
core_objs += [ env.Object("../../core/string.c") ]
|
||||||
res += [ CoreTest("file") ]
|
core_objs += [ env.Object("../../core/file.c") ]
|
||||||
res += [ CoreTest("date") ]
|
res += [ CoreTest("date") ]
|
||||||
res += [ CoreTest("idle") ]
|
res += [ CoreTest("idle") ]
|
||||||
res += [ CoreTest("database") ]
|
res += [ CoreTest("database") ]
|
||||||
|
|
|
@ -9,42 +9,30 @@
|
||||||
|
|
||||||
static void test_verify_constructor(struct file *file, gchar *fpath, gchar *ftmp)
|
static void test_verify_constructor(struct file *file, gchar *fpath, gchar *ftmp)
|
||||||
{
|
{
|
||||||
test_equal((void *)file->f_file, NULL);
|
g_assert_null(file->f_file);
|
||||||
test_equal(file_version(file), 0);
|
g_assert_cmpuint(file_version(file), ==, 0);
|
||||||
test_equal(file->f_mode, OPEN_READ);
|
g_assert_cmpuint(file->f_mode, ==, OPEN_READ);
|
||||||
test_str_equal(file_path(file), fpath);
|
g_assert_cmpstr_free(file_path(file), ==, fpath);
|
||||||
test_str_equal(file_write_path(file), ftmp);
|
g_assert_cmpstr_free(file_write_path(file), ==, ftmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void test_invalid_file(struct file *file)
|
static void test_invalid_file(gconstpointer path)
|
||||||
{
|
{
|
||||||
test_verify_constructor(file, "", "");
|
struct file file;
|
||||||
|
|
||||||
test_equal(file_open(file, OPEN_READ), (bool)false);
|
file_init(&file, (gchar *)path, 0, 0);
|
||||||
test_equal((void *)file->f_file, NULL);
|
test_verify_constructor(&file, "", "");
|
||||||
test_equal(file_open(file, OPEN_WRITE), (bool)false);
|
|
||||||
test_equal((void *)file->f_file, NULL);
|
|
||||||
test_equal(file->f_mode, OPEN_READ);
|
|
||||||
|
|
||||||
test_equal(file_exists(file), (bool)false);
|
g_assert_false(file_open(&file, OPEN_READ));
|
||||||
test_equal(test_data_file_exists(NULL), (bool)false);
|
g_assert_null(file.f_file);
|
||||||
|
g_assert_false(file_open(&file, OPEN_WRITE));
|
||||||
|
g_assert_null(file.f_file);
|
||||||
|
g_assert_cmpuint(file.f_mode, ==, OPEN_READ);
|
||||||
|
|
||||||
|
g_assert_false(file_exists(&file));
|
||||||
}
|
}
|
||||||
|
|
||||||
static void test_null()
|
static void __test_file_subprocess()
|
||||||
{
|
|
||||||
struct file fnull;
|
|
||||||
file_init(&fnull, NULL, 0, 0);
|
|
||||||
test_invalid_file(&fnull);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_empty()
|
|
||||||
{
|
|
||||||
struct file fempty;
|
|
||||||
file_init(&fempty, "", 0, 0);
|
|
||||||
test_invalid_file(&fempty);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_file()
|
|
||||||
{
|
{
|
||||||
struct file file = FILE_INIT("file.txt", 0, 0);
|
struct file file = FILE_INIT("file.txt", 0, 0);
|
||||||
gchar *basepath, *filepath, *realpath;
|
gchar *basepath, *filepath, *realpath;
|
||||||
|
@ -54,43 +42,52 @@ static void test_file()
|
||||||
realpath = g_strjoin("/", basepath, ".file.txt.tmp", NULL);
|
realpath = g_strjoin("/", basepath, ".file.txt.tmp", NULL);
|
||||||
|
|
||||||
test_verify_constructor(&file, filepath, realpath);
|
test_verify_constructor(&file, filepath, realpath);
|
||||||
test_equal(file_exists(&file), (bool)false);
|
g_assert_false(file_exists(&file));
|
||||||
test_equal(test_data_file_exists(NULL), (bool)false);
|
|
||||||
|
|
||||||
test_equal(file_open(&file, OPEN_READ), (bool)false);
|
g_assert_false(file_open(&file, OPEN_READ));
|
||||||
test_equal(file_open(&file, OPEN_WRITE), (bool)true);
|
g_assert_true(file_open(&file, OPEN_WRITE));
|
||||||
test_not_equal((void *)file.f_file, NULL);
|
g_assert_nonnull(file.f_file);
|
||||||
test_equal(file.f_mode, OPEN_WRITE);
|
g_assert_cmpuint(file.f_mode, ==, OPEN_WRITE);
|
||||||
test_equal(file_open(&file, OPEN_WRITE), (bool)false);
|
g_assert_false(file_open(&file, OPEN_WRITE));
|
||||||
|
|
||||||
test_equal(file_exists(&file), (bool)false);
|
g_assert_false(file_exists(&file));
|
||||||
file_close(&file);
|
file_close(&file);
|
||||||
test_equal((void *)file.f_file, NULL);
|
g_assert_null(file.f_file);
|
||||||
test_equal(file.f_mode, OPEN_WRITE);
|
g_assert_cmpuint(file.f_mode, ==, OPEN_WRITE);
|
||||||
|
g_assert_true(file_exists(&file));
|
||||||
test_equal(file_exists(&file), (bool)true);
|
|
||||||
test_equal(test_data_file_exists("file.txt"), (bool)true);
|
|
||||||
|
|
||||||
g_chmod(filepath, 0444);
|
g_chmod(filepath, 0444);
|
||||||
test_equal(file_open(&file, OPEN_WRITE), (bool)false);
|
g_assert_false(file_open(&file, OPEN_WRITE));
|
||||||
g_chmod(filepath, 0200);
|
g_chmod(filepath, 0200);
|
||||||
test_equal(file_open(&file, OPEN_READ), (bool)false);
|
g_assert_false(file_open(&file, OPEN_READ));
|
||||||
g_chmod(filepath, 0644);
|
g_chmod(filepath, 0644);
|
||||||
|
|
||||||
test_equal(file_open(&file, OPEN_READ), (bool)true);
|
g_assert_true(file_open(&file, OPEN_READ));
|
||||||
test_not_equal((void *)file.f_file, NULL);
|
g_assert_nonnull(file.f_file);
|
||||||
test_equal(file.f_mode, OPEN_READ);
|
g_assert_cmpuint(file.f_mode, ==, OPEN_READ);
|
||||||
test_equal(file_open(&file, OPEN_READ), (bool)false);
|
g_assert_false(file_open(&file, OPEN_READ));
|
||||||
|
|
||||||
test_equal(file_remove(&file), (bool)false);
|
g_assert_false(file_remove(&file));
|
||||||
test_equal(file_exists(&file), (bool)true);
|
g_assert_true(file_exists(&file));
|
||||||
file_close(&file);
|
file_close(&file);
|
||||||
test_equal(file_remove(&file), (bool)true);
|
g_assert_true(file_remove(&file));
|
||||||
test_equal(file_exists(&file), (bool)false);
|
g_assert_false(file_exists(&file));
|
||||||
|
|
||||||
g_free(filepath);
|
g_free(filepath);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void test_file()
|
||||||
|
{
|
||||||
|
if (g_test_subprocess()) {
|
||||||
|
__test_file_subprocess();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
g_test_trap_subprocess(NULL, 0, 0);
|
||||||
|
g_test_trap_assert_passed();
|
||||||
|
g_test_trap_assert_stderr("*file.txt: Permission denied*");
|
||||||
|
}
|
||||||
|
|
||||||
static void test_io()
|
static void test_io()
|
||||||
{
|
{
|
||||||
struct file fout = FILE_INIT("file.txt", 1, 1);
|
struct file fout = FILE_INIT("file.txt", 1, 1);
|
||||||
|
@ -98,55 +95,75 @@ static void test_io()
|
||||||
char *res = NULL;
|
char *res = NULL;
|
||||||
unsigned int i;
|
unsigned int i;
|
||||||
|
|
||||||
test_equal(file_open(&fout, OPEN_WRITE), (bool)true);
|
g_assert_true(file_open(&fout, OPEN_WRITE));
|
||||||
file_writef(&fout, "1 ABCDE\n");
|
file_writef(&fout, "1 ABCDE\n");
|
||||||
file_writef(&fout, "2 FGHIJ KLMNO\n");
|
file_writef(&fout, "2 FGHIJ KLMNO\n");
|
||||||
file_writef(&fout, "3 \n");
|
file_writef(&fout, "3 \n");
|
||||||
file_writef(&fout, "4 5 PQRST\n");
|
file_writef(&fout, "4 5 PQRST\n");
|
||||||
file_close(&fout);
|
file_close(&fout);
|
||||||
test_equal(file_exists(&fout), (bool)true);
|
g_assert_true(file_exists(&fout));
|
||||||
|
|
||||||
test_equal(file_open(&fin, OPEN_READ), (bool)true);
|
g_assert_true(file_open(&fin, OPEN_READ));
|
||||||
test_equal(file_version(&fin), 1);
|
g_assert_cmpuint(file_version(&fin), ==, 1);
|
||||||
|
|
||||||
test_equal(file_readf(&fin, "%u %ms\n", &i, &res), 2);
|
g_assert_cmpuint(file_readf(&fin, "%u %ms\n", &i, &res), ==, 2);
|
||||||
test_equal(i, 1);
|
g_assert_cmpuint(i, ==, 1);
|
||||||
test_equal(res, "ABCDE");
|
g_assert_cmpstr_free(res, ==, "ABCDE");
|
||||||
free(res);
|
|
||||||
|
|
||||||
test_equal(file_readf(&fin, "%u %m[^\n]\n", &i, &res), 2);
|
g_assert_cmpuint(file_readf(&fin, "%u %m[^\n]\n", &i, &res), ==, 2);
|
||||||
test_equal(i, 2);
|
g_assert_cmpuint(i, ==, 2);
|
||||||
test_equal(res, "FGHIJ KLMNO");
|
g_assert_cmpstr_free(res, ==, "FGHIJ KLMNO");
|
||||||
free(res);
|
|
||||||
|
|
||||||
test_equal(file_readf(&fin, "%u", &i), 1);
|
g_assert_cmpuint(file_readf(&fin, "%u", &i), ==, 1);
|
||||||
res = file_readl(&fin);
|
res = file_readl(&fin);
|
||||||
test_equal(i, 3);
|
g_assert_cmpuint(i, ==, 3);
|
||||||
test_equal(res, "");
|
g_assert_cmpstr_free(res, ==, "");
|
||||||
free(res);
|
|
||||||
|
|
||||||
test_equal(file_readf(&fin, "%u %m[^\n]", &i, &res), 2);
|
g_assert_cmpuint(file_readf(&fin, "%u %m[^\n]", &i, &res), ==, 2);
|
||||||
test_equal(i, 4);
|
g_assert_cmpuint(i, ==, 4);
|
||||||
test_equal(res, "5 PQRST");
|
g_assert_cmpstr_free(res, ==, "5 PQRST");
|
||||||
free(res);
|
|
||||||
|
|
||||||
file_close(&fin);
|
file_close(&fin);
|
||||||
test_equal(file_version(&fin), 2);
|
g_assert_cmpuint(file_version(&fin), ==, 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*static void test_versioning()
|
static void __test_versioning_subprocess(unsigned int out, unsigned int in)
|
||||||
{
|
{
|
||||||
struct file fout = FILE_INIT("file.txt", 0, 0);
|
struct file fout = FILE_INIT("file.txt", out, out);
|
||||||
struct file fin = FILE_INIT("file.txt", 2, 1);
|
struct file fin = FILE_INIT("file.txt", in, in);
|
||||||
|
|
||||||
test_equal(file_open(&fout, OPEN_WRITE), (bool)true);
|
g_assert_true(file_open(&fout, OPEN_WRITE));
|
||||||
file_writef(&fout, "abcdefghijklmnopqrstuvwxyz");
|
file_writef(&fout, "abcdefghijklmnopqrstuvwxyz");
|
||||||
file_close(&fout);
|
file_close(&fout);
|
||||||
test_equal(file_exists(&fout), (bool)true);
|
g_assert_true(file_exists(&fout));
|
||||||
|
|
||||||
test_equal(file_open(&fin, OPEN_READ), (bool)false);
|
g_assert_false(file_open(&fin, OPEN_READ));
|
||||||
test_equal((void *)fin.f_file, NULL);
|
g_assert_null(fin.f_file);
|
||||||
}*/
|
}
|
||||||
|
|
||||||
|
static void test_versioning_old()
|
||||||
|
{
|
||||||
|
if (g_test_subprocess()) {
|
||||||
|
__test_versioning_subprocess(0, 1);
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
|
g_test_trap_subprocess(NULL, 0, 0);
|
||||||
|
g_test_trap_assert_failed();
|
||||||
|
g_test_trap_assert_stderr("*file.txt: File too old to be upgraded.*");
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_versioning_new()
|
||||||
|
{
|
||||||
|
if (g_test_subprocess()) {
|
||||||
|
__test_versioning_subprocess(1, 0);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
g_test_trap_subprocess(NULL, 0, 0);
|
||||||
|
g_test_trap_assert_failed();
|
||||||
|
g_test_trap_assert_stderr("*file.txt: File too new to be opened.*");
|
||||||
|
}
|
||||||
|
|
||||||
static void test_cache()
|
static void test_cache()
|
||||||
{
|
{
|
||||||
|
@ -158,43 +175,47 @@ static void test_cache()
|
||||||
filepath = g_strjoin("/", basepath, "dir", "file.txt", NULL);
|
filepath = g_strjoin("/", basepath, "dir", "file.txt", NULL);
|
||||||
writepath = g_strjoin("/", basepath, "dir", ".file.txt.tmp", NULL);
|
writepath = g_strjoin("/", basepath, "dir", ".file.txt.tmp", NULL);
|
||||||
|
|
||||||
test_equal((void *)file.cf_file, NULL);
|
g_assert_null(file.cf_file);
|
||||||
test_equal(file.cf_name, "file.txt");
|
g_assert_cmpstr(file.cf_name, ==, "file.txt");
|
||||||
test_equal(file.cf_subdir, "dir");
|
g_assert_cmpstr(file.cf_subdir, ==, "dir");
|
||||||
|
|
||||||
test_str_equal(cache_file_path(&file), filepath);
|
g_assert_cmpstr_free(cache_file_path(&file), ==, filepath);
|
||||||
test_str_equal(cache_file_write_path(&file), writepath);
|
g_assert_cmpstr_free(cache_file_write_path(&file), ==, writepath);
|
||||||
|
|
||||||
/* Test writing data to a cache file. */
|
/* Test writing data to a cache file. */
|
||||||
test_equal(cache_file_exists(&file), (bool)false);
|
g_assert_false(cache_file_exists(&file));
|
||||||
test_equal(cache_file_open(&file, OPEN_READ), (bool)false);
|
g_assert_false(cache_file_open(&file, OPEN_READ));
|
||||||
test_equal(cache_file_open(&file, OPEN_WRITE), (bool)true);
|
g_assert_true(cache_file_open(&file, OPEN_WRITE));
|
||||||
test_not_equal((void *)file.cf_file, NULL);
|
g_assert_nonnull(file.cf_file);
|
||||||
test_equal(cache_file_open(&file, OPEN_WRITE), (bool)false);
|
g_assert_false(cache_file_open(&file, OPEN_WRITE));
|
||||||
|
|
||||||
test_equal(cache_file_exists(&file), (bool)false);
|
g_assert_false(cache_file_exists(&file));
|
||||||
test_equal(cache_file_write(&file, "abcde", 5), 5);
|
g_assert_cmpuint(cache_file_write(&file, "abcde", 5), ==, 5);
|
||||||
cache_file_close(&file);
|
cache_file_close(&file);
|
||||||
test_equal((void *)file.cf_file, NULL);
|
g_assert_null(file.cf_file);
|
||||||
test_equal(cache_file_exists(&file), (bool)true);
|
g_assert_true(cache_file_exists(&file));
|
||||||
|
|
||||||
/* Test importing a file into the cache. */
|
/* Test importing a file into the cache. */
|
||||||
test_equal(cache_file_exists(©), (bool)false);
|
g_assert_false(cache_file_exists(©));
|
||||||
test_equal(cache_file_import(©, filepath), (bool)false);
|
g_assert_false(cache_file_import(©, filepath));
|
||||||
test_equal(cache_file_exists(©), (bool)false);
|
g_assert_false(cache_file_exists(©));
|
||||||
test_equal(cache_file_open(©, OPEN_WRITE), (bool)true);
|
g_assert_true(cache_file_open(©, OPEN_WRITE));
|
||||||
test_equal(cache_file_import(©, NULL), (bool)false);
|
g_assert_false(cache_file_import(©, NULL));
|
||||||
test_equal(cache_file_import(©, filepath), (bool)true);
|
g_assert_true(cache_file_import(©, filepath));
|
||||||
test_equal(cache_file_exists(©), (bool)false);
|
g_assert_false(cache_file_exists(©));
|
||||||
cache_file_close(©);
|
cache_file_close(©);
|
||||||
test_equal(cache_file_exists(©), (bool)true);
|
g_assert_true(cache_file_exists(©));
|
||||||
}
|
}
|
||||||
|
|
||||||
DECLARE_UNIT_TESTS(
|
int main(int argc, char **argv)
|
||||||
UNIT_TEST("File (Path = NULL)", test_null),
|
{
|
||||||
UNIT_TEST("File (Path = \"\")", test_empty),
|
g_test_init(&argc, &argv, NULL);
|
||||||
UNIT_TEST("File (Path = \"file.txt\")", test_file),
|
g_test_add_data_func("/Core/File/Path = NULL", NULL, test_invalid_file);
|
||||||
UNIT_TEST("File I/O", test_io),
|
g_test_add_data_func("/Core/File/Path = \"\"", "", test_invalid_file);
|
||||||
//UNIT_TEST("File Versioning", test_versioning),
|
g_test_add_func("/Core/File/Path = \"file.txt\"", test_file);
|
||||||
UNIT_TEST("File Cache", test_cache),
|
g_test_add_func("/Core/File/IO", test_io);
|
||||||
);
|
g_test_add_func("/Core/File/Versioning/Too Old", test_versioning_old);
|
||||||
|
g_test_add_func("/Core/File/Versioning/Too New", test_versioning_new);
|
||||||
|
g_test_add_func("/Core/File/Cache", test_cache);
|
||||||
|
return g_test_run();
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue