|
16 | 16 | import sys |
17 | 17 | import time |
18 | 18 | import unittest |
19 | | -from datetime import datetime |
| 19 | +from datetime import datetime, timedelta |
20 | 20 | from io import StringIO |
21 | 21 |
|
22 | 22 | import pytest |
@@ -1179,6 +1179,60 @@ def test_update(self): |
1179 | 1179 | catalog = read_po(infp) |
1180 | 1180 | assert len(catalog) == 4 # Catalog was updated |
1181 | 1181 |
|
| 1182 | + def test_update_pot_creation_date(self): |
| 1183 | + template = Catalog() |
| 1184 | + template.add("1") |
| 1185 | + template.add("2") |
| 1186 | + template.add("3") |
| 1187 | + tmpl_file = os.path.join(i18n_dir, 'temp-template.pot') |
| 1188 | + with open(tmpl_file, "wb") as outfp: |
| 1189 | + write_po(outfp, template) |
| 1190 | + po_file = os.path.join(i18n_dir, 'temp1.po') |
| 1191 | + self.cli.run(sys.argv + ['init', |
| 1192 | + '-l', 'fi', |
| 1193 | + '-o', po_file, |
| 1194 | + '-i', tmpl_file |
| 1195 | + ]) |
| 1196 | + with open(po_file) as infp: |
| 1197 | + catalog = read_po(infp) |
| 1198 | + assert len(catalog) == 3 |
| 1199 | + original_catalog_creation_date = catalog.creation_date |
| 1200 | + |
| 1201 | + # Update the template creation date |
| 1202 | + template.creation_date -= timedelta(minutes=3) |
| 1203 | + with open(tmpl_file, "wb") as outfp: |
| 1204 | + write_po(outfp, template) |
| 1205 | + |
| 1206 | + self.cli.run(sys.argv + ['update', |
| 1207 | + '-l', 'fi_FI', |
| 1208 | + '-o', po_file, |
| 1209 | + '-i', tmpl_file]) |
| 1210 | + |
| 1211 | + with open(po_file) as infp: |
| 1212 | + catalog = read_po(infp) |
| 1213 | + # We didn't ignore the creation date, so expect a diff |
| 1214 | + assert catalog.creation_date != original_catalog_creation_date |
| 1215 | + |
| 1216 | + # Reset the "original" |
| 1217 | + original_catalog_creation_date = catalog.creation_date |
| 1218 | + |
| 1219 | + # Update the template creation date again |
| 1220 | + # This time, pass the ignore flag and expect the times are different |
| 1221 | + template.creation_date -= timedelta(minutes=5) |
| 1222 | + with open(tmpl_file, "wb") as outfp: |
| 1223 | + write_po(outfp, template) |
| 1224 | + |
| 1225 | + self.cli.run(sys.argv + ['update', |
| 1226 | + '-l', 'fi_FI', |
| 1227 | + '-o', po_file, |
| 1228 | + '-i', tmpl_file, |
| 1229 | + '--ignore-pot-creation-date']) |
| 1230 | + |
| 1231 | + with open(po_file) as infp: |
| 1232 | + catalog = read_po(infp) |
| 1233 | + # We ignored creation date, so it should not have changed |
| 1234 | + assert catalog.creation_date == original_catalog_creation_date |
| 1235 | + |
1182 | 1236 | def test_check(self): |
1183 | 1237 | template = Catalog() |
1184 | 1238 | template.add("1") |
@@ -1237,6 +1291,54 @@ def test_check(self): |
1237 | 1291 | '-o', po_file, |
1238 | 1292 | '-i', tmpl_file]) |
1239 | 1293 |
|
| 1294 | + def test_check_pot_creation_date(self): |
| 1295 | + template = Catalog() |
| 1296 | + template.add("1") |
| 1297 | + template.add("2") |
| 1298 | + template.add("3") |
| 1299 | + tmpl_file = os.path.join(i18n_dir, 'temp-template.pot') |
| 1300 | + with open(tmpl_file, "wb") as outfp: |
| 1301 | + write_po(outfp, template) |
| 1302 | + po_file = os.path.join(i18n_dir, 'temp1.po') |
| 1303 | + self.cli.run(sys.argv + ['init', |
| 1304 | + '-l', 'fi_FI', |
| 1305 | + '-o', po_file, |
| 1306 | + '-i', tmpl_file |
| 1307 | + ]) |
| 1308 | + |
| 1309 | + # Update the catalog file |
| 1310 | + self.cli.run(sys.argv + ['update', |
| 1311 | + '-l', 'fi_FI', |
| 1312 | + '-o', po_file, |
| 1313 | + '-i', tmpl_file]) |
| 1314 | + |
| 1315 | + # Run a check without introducing any changes to the template |
| 1316 | + self.cli.run(sys.argv + ['update', |
| 1317 | + '--check', |
| 1318 | + '-l', 'fi_FI', |
| 1319 | + '-o', po_file, |
| 1320 | + '-i', tmpl_file]) |
| 1321 | + |
| 1322 | + # Run a check after changing the template creation date |
| 1323 | + template.creation_date = datetime.now() - timedelta(minutes=5) |
| 1324 | + with open(tmpl_file, "wb") as outfp: |
| 1325 | + write_po(outfp, template) |
| 1326 | + |
| 1327 | + # Should fail without --ignore-pot-creation-date flag |
| 1328 | + with pytest.raises(BaseError): |
| 1329 | + self.cli.run(sys.argv + ['update', |
| 1330 | + '--check', |
| 1331 | + '-l', 'fi_FI', |
| 1332 | + '-o', po_file, |
| 1333 | + '-i', tmpl_file]) |
| 1334 | + # Should pass with --ignore-pot-creation-date flag |
| 1335 | + self.cli.run(sys.argv + ['update', |
| 1336 | + '--check', |
| 1337 | + '-l', 'fi_FI', |
| 1338 | + '-o', po_file, |
| 1339 | + '-i', tmpl_file, |
| 1340 | + '--ignore-pot-creation-date']) |
| 1341 | + |
1240 | 1342 | def test_update_init_missing(self): |
1241 | 1343 | template = Catalog() |
1242 | 1344 | template.add("1") |
|
0 commit comments