]> git.saurik.com Git - apt.git/blob - test/libapt/getlanguages_test.cc
Merge remote-tracking branch 'donkult/debian/sid' into debian/sid
[apt.git] / test / libapt / getlanguages_test.cc
1 #include <config.h>
2
3 #include <apt-pkg/aptconfiguration.h>
4 #include <apt-pkg/configuration.h>
5 #include <apt-pkg/fileutl.h>
6
7 #include <algorithm>
8 #include <iostream>
9 #include <string>
10 #include <vector>
11
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <fcntl.h>
15
16 #include <gtest/gtest.h>
17
18 #include "file-helpers.h"
19
20 TEST(LanguagesTest,Environment)
21 {
22 _config->Clear();
23
24 char const* env[2];
25 env[0] = "de_DE.UTF-8";
26 env[1] = "";
27
28 std::vector<std::string> vec = APT::Configuration::getLanguages(false, false, env);
29 ASSERT_EQ(3 ,vec.size());
30 EXPECT_EQ("de_DE", vec[0]);
31 EXPECT_EQ("de", vec[1]);
32 EXPECT_EQ("en", vec[2]);
33
34 // Special: Check if the cache is actually in use
35 env[0] = "en_GB.UTF-8";
36 vec = APT::Configuration::getLanguages(false, true, env);
37 ASSERT_EQ(3, vec.size());
38 EXPECT_EQ("de_DE", vec[0]);
39 EXPECT_EQ("de", vec[1]);
40 EXPECT_EQ("en", vec[2]);
41
42 env[0] = "en_GB.UTF-8";
43 vec = APT::Configuration::getLanguages(false, false, env);
44 ASSERT_EQ(2, vec.size());
45 EXPECT_EQ("en_GB", vec[0]);
46 EXPECT_EQ("en", vec[1]);
47
48 // esperanto
49 env[0] = "eo.UTF-8";
50 vec = APT::Configuration::getLanguages(false, false, env);
51 ASSERT_EQ(2, vec.size());
52 EXPECT_EQ("eo", vec[0]);
53 EXPECT_EQ("en", vec[1]);
54
55 env[0] = "tr_DE@euro";
56 vec = APT::Configuration::getLanguages(false, false, env);
57 EXPECT_EQ(3, vec.size());
58 EXPECT_EQ("tr_DE", vec[0]);
59 EXPECT_EQ("tr", vec[1]);
60 EXPECT_EQ("en", vec[2]);
61
62 env[0] = "de_NO";
63 env[1] = "de_NO:en_GB:nb_NO:nb:no_NO:no:nn_NO:nn:da:sv:en";
64 vec = APT::Configuration::getLanguages(false, false, env);
65 EXPECT_EQ(6, vec.size());
66 EXPECT_EQ("de_NO", vec[0]);
67 EXPECT_EQ("de", vec[1]);
68 EXPECT_EQ("en_GB", vec[2]);
69 EXPECT_EQ("nb_NO", vec[3]);
70 EXPECT_EQ("nb", vec[4]);
71 EXPECT_EQ("en", vec[5]);
72
73 env[0] = "pt_PR.UTF-8";
74 env[1] = "";
75 vec = APT::Configuration::getLanguages(false, false, env);
76 EXPECT_EQ(3, vec.size());
77 EXPECT_EQ("pt_PR", vec[0]);
78 EXPECT_EQ("pt", vec[1]);
79 EXPECT_EQ("en", vec[2]);
80
81 env[0] = "ast_DE.UTF-8";
82 vec = APT::Configuration::getLanguages(false, false, env); // bogus, but syntactical correct
83 EXPECT_EQ(3, vec.size());
84 EXPECT_EQ("ast_DE", vec[0]);
85 EXPECT_EQ("ast", vec[1]);
86 EXPECT_EQ("en", vec[2]);
87
88 env[0] = "C";
89 vec = APT::Configuration::getLanguages(false, false, env);
90 EXPECT_EQ(1, vec.size());
91 EXPECT_EQ("en", vec[0]);
92
93 _config->Set("Acquire::Languages", "none");
94 env[0] = "C";
95 vec = APT::Configuration::getLanguages(false, false, env);
96 EXPECT_TRUE(vec.empty());
97
98 _config->Set("Acquire::Languages", "environment");
99 env[0] = "C";
100 vec = APT::Configuration::getLanguages(false, false, env);
101 EXPECT_EQ(1, vec.size());
102 EXPECT_EQ("en", vec[0]);
103
104 _config->Set("Acquire::Languages", "de");
105 env[0] = "C";
106 vec = APT::Configuration::getLanguages(false, false, env);
107 EXPECT_EQ(1, vec.size());
108 EXPECT_EQ("de", vec[0]);
109
110 _config->Set("Acquire::Languages", "fr");
111 env[0] = "ast_DE.UTF-8";
112 vec = APT::Configuration::getLanguages(false, false, env);
113 EXPECT_EQ(1, vec.size());
114 EXPECT_EQ("fr", vec[0]);
115
116 _config->Set("Acquire::Languages", "environment,en");
117 env[0] = "de_DE.UTF-8";
118 vec = APT::Configuration::getLanguages(false, false, env);
119 EXPECT_EQ(3, vec.size());
120 EXPECT_EQ("de_DE", vec[0]);
121 EXPECT_EQ("de", vec[1]);
122 EXPECT_EQ("en", vec[2]);
123 _config->Set("Acquire::Languages", "");
124
125 _config->Set("Acquire::Languages::1", "environment");
126 _config->Set("Acquire::Languages::2", "en");
127 env[0] = "de_DE.UTF-8";
128 vec = APT::Configuration::getLanguages(false, false, env);
129 EXPECT_EQ(3, vec.size());
130 EXPECT_EQ("de_DE", vec[0]);
131 EXPECT_EQ("de", vec[1]);
132 EXPECT_EQ("en", vec[2]);
133
134 _config->Set("Acquire::Languages::3", "de");
135 env[0] = "de_DE.UTF-8";
136 vec = APT::Configuration::getLanguages(false, false, env);
137 EXPECT_EQ(3, vec.size());
138 EXPECT_EQ("de_DE", vec[0]);
139 EXPECT_EQ("de", vec[1]);
140 EXPECT_EQ("en", vec[2]);
141
142 _config->Clear();
143 }
144
145 TEST(LanguagesTest,TranslationFiles)
146 {
147 _config->Clear();
148 _config->Set("Acquire::Languages::1", "environment");
149 _config->Set("Acquire::Languages::2", "en");
150 _config->Set("Acquire::Languages::3", "de");
151
152 char const* env[2];
153 env[0] = "de_DE.UTF-8";
154 env[1] = "";
155
156 std::string tempdir;
157 createTemporaryDirectory("languages", tempdir);
158
159 #define createTranslation(lang) \
160 createFile(tempdir, std::string("/ftp.de.debian.org_debian_dists_sid_main_i18n_Translation-").append(lang));
161
162 createTranslation("tr");
163 createTranslation("pt");
164 createTranslation("se~");
165 createTranslation("st.bak");
166 createTranslation("ast_DE");
167 createTranslation("tlh%5fDE");
168
169 _config->Set("Dir::State::lists", tempdir);
170 std::vector<std::string> vec = APT::Configuration::getLanguages(true, false, env);
171 EXPECT_EQ(8, vec.size());
172 EXPECT_EQ("de_DE", vec[0]);
173 EXPECT_EQ("de", vec[1]);
174 EXPECT_EQ("en", vec[2]);
175 EXPECT_EQ("none", vec[3]);
176 EXPECT_NE(vec.end(), std::find(vec.begin(), vec.end(), "pt"));
177 EXPECT_NE(vec.end(), std::find(vec.begin(), vec.end(), "tr"));
178 EXPECT_NE(vec.end(), std::find(vec.begin(), vec.end(), "ast_DE"));
179 EXPECT_NE(vec.end(), std::find(vec.begin(), vec.end(), "tlh_DE"));
180 EXPECT_NE(vec[4], vec[5]);
181 EXPECT_NE(vec[4], vec[6]);
182 EXPECT_NE(vec[4], vec[7]);
183 EXPECT_NE(vec[5], vec[6]);
184 EXPECT_NE(vec[5], vec[7]);
185 EXPECT_NE(vec[6], vec[7]);
186
187 _config->Set("Acquire::Languages", "none");
188 vec = APT::Configuration::getLanguages(true, false, env);
189 EXPECT_EQ(1, vec.size());
190 EXPECT_EQ("none", vec[0]);
191 _config->Set("Acquire::Languages", "");
192
193 _config->Set("Dir::State::lists", "/non-existing-dir");
194 _config->Set("Acquire::Languages::1", "none");
195 env[0] = "de_DE.UTF-8";
196 vec = APT::Configuration::getLanguages(false, false, env);
197 EXPECT_TRUE(vec.empty());
198 env[0] = "de_DE.UTF-8";
199 vec = APT::Configuration::getLanguages(true, false, env);
200 EXPECT_EQ(2, vec.size());
201 EXPECT_EQ("en", vec[0]);
202 EXPECT_EQ("de", vec[1]);
203
204 _config->Set("Acquire::Languages::1", "fr");
205 _config->Set("Acquire::Languages", "de_DE");
206 env[0] = "de_DE.UTF-8";
207 vec = APT::Configuration::getLanguages(false, false, env);
208 EXPECT_EQ(1, vec.size());
209 EXPECT_EQ("de_DE", vec[0]);
210
211 _config->Set("Acquire::Languages", "none");
212 env[0] = "de_DE.UTF-8";
213 vec = APT::Configuration::getLanguages(true, false, env);
214 EXPECT_EQ(1, vec.size());
215 EXPECT_EQ("none", vec[0]);
216
217 _config->Set("Acquire::Languages", "");
218 //FIXME: Remove support for this deprecated setting
219 _config->Set("APT::Acquire::Translation", "ast_DE");
220 env[0] = "de_DE.UTF-8";
221 vec = APT::Configuration::getLanguages(true, false, env);
222 EXPECT_EQ(2, vec.size());
223 EXPECT_EQ("ast_DE", vec[0]);
224 EXPECT_EQ("en", vec[1]);
225 _config->Set("APT::Acquire::Translation", "none");
226 env[0] = "de_DE.UTF-8";
227 vec = APT::Configuration::getLanguages(true, false, env);
228 EXPECT_EQ(1, vec.size());
229 EXPECT_EQ("en", vec[0]);
230
231
232 EXPECT_EQ(0, system(std::string("rm -rf ").append(tempdir).c_str()));
233 _config->Clear();
234 }