1 /**
2  * @file
3  * Common code for file tests
4  *
5  * @authors
6  * Copyright (C) 2020 Richard Russon <rich@flatcap.org>
7  *
8  * @copyright
9  * This program is free software: you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License as published by the Free Software
11  * Foundation, either version 2 of the License, or (at your option) any later
12  * version.
13  *
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
17  * details.
18  *
19  * You should have received a copy of the GNU General Public License along with
20  * this program.  If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 #define TEST_NO_MAIN
24 #include "config.h"
25 #include "acutest.h"
26 #include <locale.h>
27 #include <sys/stat.h>
28 #include <sys/types.h>
29 #include <unistd.h>
30 #include "mutt/lib.h"
31 #include "config/lib.h"
32 #include "core/lib.h"
33 
34 #define TEST_DIR "NEOMUTT_TEST_DIR"
35 
36 #define CONFIG_INIT_TYPE(CS, NAME)                                             \
37   extern const struct ConfigSetType Cst##NAME;                                 \
38   cs_register_type(CS, &Cst##NAME)
39 
get_test_dir(void)40 static const char *get_test_dir(void)
41 {
42   return mutt_str_getenv(TEST_DIR);
43 }
44 
test_gen_path(char * buf,size_t buflen,const char * fmt)45 void test_gen_path(char *buf, size_t buflen, const char *fmt)
46 {
47   snprintf(buf, buflen, NONULL(fmt), NONULL(get_test_dir()));
48 }
49 
test_init(void)50 void test_init(void)
51 {
52   const char *path = get_test_dir();
53   bool success = false;
54 
55   TEST_CASE("Common setup");
56   if (!TEST_CHECK(path != NULL))
57   {
58     TEST_MSG("Environment variable '%s' isn't set\n", TEST_DIR);
59     goto done;
60   }
61 
62   size_t len = strlen(path);
63   if (!TEST_CHECK(path[len - 1] != '/'))
64   {
65     TEST_MSG("Environment variable '%s' mustn't end with a '/'\n", TEST_DIR);
66     goto done;
67   }
68 
69   struct stat st = { 0 };
70 
71   if (!TEST_CHECK(stat(path, &st) == 0))
72   {
73     TEST_MSG("Test dir '%s' doesn't exist\n", path);
74     goto done;
75   }
76 
77   if (!TEST_CHECK(S_ISDIR(st.st_mode) == true))
78   {
79     TEST_MSG("Test dir '%s' isn't a directory\n", path);
80     goto done;
81   }
82 
83   if (!TEST_CHECK((setlocale(LC_ALL, "C.UTF-8") != NULL) ||
84                   (setlocale(LC_ALL, "en_US.UTF-8") != NULL)))
85   {
86     TEST_MSG("Can't set locale to C.UTF-8 or en_US.UTF-8");
87     goto done;
88   }
89 
90   success = true;
91 done:
92   if (!success)
93     TEST_MSG("See: https://github.com/neomutt/neomutt-test-files#test-files\n");
94 }
95 
test_fini(void)96 void test_fini(void)
97 {
98   mutt_buffer_pool_free();
99 }
100 
test_neomutt_create(void)101 struct NeoMutt *test_neomutt_create(void)
102 {
103   struct ConfigSet *cs = cs_new(50);
104   CONFIG_INIT_TYPE(cs, Address);
105   CONFIG_INIT_TYPE(cs, Bool);
106   CONFIG_INIT_TYPE(cs, Enum);
107   CONFIG_INIT_TYPE(cs, Long);
108   CONFIG_INIT_TYPE(cs, Mbtable);
109   CONFIG_INIT_TYPE(cs, Number);
110   CONFIG_INIT_TYPE(cs, Path);
111   CONFIG_INIT_TYPE(cs, Quad);
112   CONFIG_INIT_TYPE(cs, Regex);
113   CONFIG_INIT_TYPE(cs, Slist);
114   CONFIG_INIT_TYPE(cs, Sort);
115   CONFIG_INIT_TYPE(cs, String);
116 
117   struct NeoMutt *n = neomutt_new(cs);
118 
119   return n;
120 }
121 
test_neomutt_destroy(struct NeoMutt ** ptr)122 void test_neomutt_destroy(struct NeoMutt **ptr)
123 {
124   struct NeoMutt *n = *ptr;
125 
126   struct ConfigSet *cs = n->sub->cs;
127   neomutt_free(ptr);
128   cs_free(&cs);
129 }
130 
index_shared_data_new(void)131 struct IndexSharedData *index_shared_data_new(void)
132 {
133   return NULL;
134 }
135 
add_panel_pager(struct MuttWindow * parent,bool status_on_top)136 struct MuttWindow *add_panel_pager(struct MuttWindow *parent, bool status_on_top)
137 {
138   return NULL;
139 }
140 
mutt_yesorno(const char * msg,enum QuadOption def)141 enum QuadOption mutt_yesorno(const char *msg, enum QuadOption def)
142 {
143   return MUTT_YES;
144 }
145