} END_TEST START_TEST(test_file_invariants) { fail_unless(girara_file_open(NULL, NULL) == NULL, NULL); fail_unless(girara_file_open("somefile", NULL) == NULL, NULL); fail_unless(girara_file_open(NULL, "r") == NULL, NULL); fail_unless(girara_file_read_line(NULL) == NULL, NULL); fail_unless(girara_file_read(NULL) == NULL, NULL); } END_TEST
char* girara_file_read(const char* path) { if (path == NULL) { return NULL; } FILE* file = girara_file_open(path, "r"); if (file == NULL) { return NULL; } char* content = girara_file_read2(file); fclose(file); return content; }
} END_TEST START_TEST(test_file_read) { static const char CONTENT[] = "test1\ntest2\ntest3"; static const char* LINES[] = { "test1", "test2", "test3" }; static size_t NUMLINES = 3; gchar* path = NULL; int fd = g_file_open_tmp("girara.test.XXXXXX", &path, NULL); fail_unless(fd != -1, "Failed to open temporary file.", NULL); fail_unless(g_strcmp0(path, "") != 0, "Failed to open temporary file.", NULL); GError* error = NULL; if (g_file_set_contents(path, CONTENT, -1, &error) == FALSE) { fail_unless(false, "Couldn't set content: %s", error->message, NULL); g_error_free(error); } char* content = girara_file_read(path); fail_unless(g_strcmp0(content, CONTENT) == 0, "Reading file failed", NULL); free(content); FILE* file = girara_file_open(path, "r"); fail_unless(file != NULL, NULL); for (size_t i = 0; i != NUMLINES; ++i) { char* line = girara_file_read_line(file); fail_unless(g_strcmp0(line, LINES[i]) == 0, "Line doesn't match (got: %s, expected: %s)", line, LINES[i], NULL); free(line); } fclose(file); close(fd); fail_unless(g_remove(path) == 0, "Failed to remove temporary file.", NULL); g_free(path); } END_TEST