Merge lp:~dholbach/help-app/1428677 into lp:~ubuntu-touch-coreapps-drivers/help-app/trunk

Proposed by Daniel Holbach
Status: Superseded
Proposed branch: lp:~dholbach/help-app/1428677
Merge into: lp:~ubuntu-touch-coreapps-drivers/help-app/trunk
Diff against target: 267 lines (+175/-17)
7 files modified
Makefile (+4/-1)
edit-here/Makefile (+3/-0)
edit-here/run-tests (+14/-0)
edit-here/tests/test_files.py (+20/-0)
edit-here/tests/test_links.py (+75/-0)
edit-here/tests/test_translations.py (+30/-0)
edit-here/translations.py (+29/-16)
To merge this branch: bzr merge lp:~dholbach/help-app/1428677
Reviewer Review Type Date Requested Status
Ubuntu Core Apps Drivers Pending
Review via email: mp+252301@code.launchpad.net

This proposal has been superseded by a proposal from 2015-03-09.

To post a comment you must log in.
lp:~dholbach/help-app/1428677 updated
87. By Daniel Holbach

remove unused code

88. By Daniel Holbach

explain what we do in terms of testing

Unmerged revisions

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'Makefile'
2--- Makefile 2015-02-26 16:26:57 +0000
3+++ Makefile 2015-03-09 14:34:15 +0000
4@@ -7,6 +7,9 @@
5 $(foreach fn, $(ignored), $(shell rm -r $(fn);))
6 endif
7
8+check:
9+ make -C edit-here check
10+
11 click: html
12 cd app && click build . && mv *.click ..
13
14@@ -19,4 +22,4 @@
15 update-pot:
16 cd edit-here && ./generate-pot
17
18-.PHONY: click html web update-pot clean
19+.PHONY: click html web update-pot clean check
20
21=== modified file 'edit-here/Makefile'
22--- edit-here/Makefile 2015-03-06 10:32:59 +0000
23+++ edit-here/Makefile 2015-03-09 14:34:15 +0000
24@@ -60,6 +60,9 @@
25 @echo 'Set the DEBUG variable to 1 to enable debugging, e.g. make DEBUG=1 html'
26 @echo ' '
27
28+check:
29+ ./run-tests
30+
31 web:
32 ./generate-translations
33 $(PELICAN) $(INPUTDIR) -o $(OUTPUTDIR_WEB) -s $(CONFFILE) $(PELICANOPTS) -t $(THEMEDIR_WEB)
34
35=== added file 'edit-here/run-tests'
36--- edit-here/run-tests 1970-01-01 00:00:00 +0000
37+++ edit-here/run-tests 2015-03-09 14:34:15 +0000
38@@ -0,0 +1,14 @@
39+#!/usr/bin/python3
40+
41+import sys
42+import unittest
43+
44+test_directory = 'tests/'
45+test_filename = 'test_*'
46+if len(sys.argv) > 1:
47+ test_filename = sys.argv[1]
48+
49+suite = unittest.TestLoader().discover(test_directory, pattern=test_filename)
50+res = unittest.TextTestRunner(verbosity=2).run(suite)
51+if res.failures or res.errors:
52+ sys.exit(1)
53
54=== added directory 'edit-here/tests'
55=== added file 'edit-here/tests/test_files.py'
56--- edit-here/tests/test_files.py 1970-01-01 00:00:00 +0000
57+++ edit-here/tests/test_files.py 2015-03-09 14:34:15 +0000
58@@ -0,0 +1,20 @@
59+import os
60+from unittest import TestCase
61+
62+import translations
63+
64+
65+class HelpTestCase(TestCase):
66+ def __init__(self, *args):
67+ self.translations = translations.Translations()
68+ TestCase.__init__(self, *args)
69+
70+ def test_doc_files_size_not_0(self):
71+ sizes = [os.stat(fn).st_size
72+ for fn in self.translations.documents.docs]
73+ self.assertNotIn(0, sizes)
74+
75+ def test_po_files_size_not_0(self):
76+ sizes = [os.stat(fn).st_size
77+ for fn in self.translations.po.langs]
78+ self.assertNotIn(0, sizes)
79
80=== added file 'edit-here/tests/test_links.py'
81--- edit-here/tests/test_links.py 1970-01-01 00:00:00 +0000
82+++ edit-here/tests/test_links.py 2015-03-09 14:34:15 +0000
83@@ -0,0 +1,75 @@
84+import codecs
85+from html.parser import HTMLParser
86+import os
87+import shutil
88+import subprocess
89+import tempfile
90+from unittest import TestCase
91+import urllib.parse
92+import urllib.request
93+from urllib.request import FileHandler
94+
95+
96+def require_build(build):
97+ tempdir = tempfile.mkdtemp()
98+ ret = subprocess.call(['make', '-es', build],
99+ env={'OUTPUTDIR': tempdir})
100+ return (ret, tempdir)
101+
102+
103+def clean_tempdir(tempdir):
104+ if os.path.exists(tempdir):
105+ shutil.rmtree(tempdir)
106+
107+
108+class MyHTMLParser(HTMLParser):
109+ links = []
110+
111+ def handle_starttag(self, tag, attrs):
112+ if tag == "a":
113+ for name, value in attrs:
114+ if name == "href":
115+ url = urllib.parse.urlparse(value)
116+ if not url.netloc or url.netloc == 'localhost':
117+ self.links += [value]
118+
119+ def reload(self):
120+ links = self.links
121+ self.links = []
122+ return links
123+
124+
125+class HelpTestCase(TestCase):
126+ def __init__(self, *args):
127+ self.pwd = os.getcwd()
128+ self.htmlparser = MyHTMLParser()
129+ self.fh = FileHandler()
130+ TestCase.__init__(self, *args)
131+
132+ def __del__(self):
133+ os.chdir(self.pwd)
134+
135+ def _test_local_links(self, build):
136+ (ret, tempdir) = require_build(build)
137+ if ret:
138+ return False
139+ for dirpath, dirnames, filenames in os.walk(tempdir):
140+ for fn in filenames:
141+ full_fn = os.path.join(dirpath, fn)
142+ os.chdir(dirpath)
143+ if full_fn.endswith('.html'):
144+ html = codecs.open(full_fn, encoding='utf-8').read()
145+ self.htmlparser.feed(html)
146+ links = self.htmlparser.reload()
147+ for link in links:
148+ rel_path = os.path.relpath(link, full_fn)
149+ path = os.path.normpath(os.path.join(full_fn, rel_path))
150+ self.assertTrue(os.path.exists(path))
151+ clean_tempdir(tempdir)
152+ return True
153+
154+ def test_local_phone_links(self):
155+ return self._test_local_links('html')
156+
157+ def test_local_web_links(self):
158+ return self._test_local_links('web')
159
160=== added file 'edit-here/tests/test_translations.py'
161--- edit-here/tests/test_translations.py 1970-01-01 00:00:00 +0000
162+++ edit-here/tests/test_translations.py 2015-03-09 14:34:15 +0000
163@@ -0,0 +1,30 @@
164+from unittest import TestCase
165+
166+import translations
167+
168+
169+class HelpTestCase(TestCase):
170+ def __init__(self, *args):
171+ self.translations = translations.Translations()
172+ TestCase.__init__(self, *args)
173+
174+ def test_first_line_of_docs_is_title_line(self):
175+ po = self.translations.po
176+ self.assertTrue(po.gettextize(self.translations.documents))
177+ results = []
178+ for entry, first_line in po.pot_file_ob.find_title_lines():
179+ results += [entry.msgid == first_line]
180+ self.assertNotIn(False, results)
181+
182+ def test_translated_filenames_in_markdown_links(self):
183+ po = self.translations.po
184+ for po_fn in po.langs:
185+ po.load_pofile(po_fn)
186+ pofile = po.langs[po_fn]['pofile']
187+ for entry in pofile.find_in_msgid('{filename}'):
188+ (link_msgid, link_msgstr) = \
189+ pofile.find_link_in_markdown_message(entry)
190+ self.assertNotIn(link_msgid, link_msgstr)
191+ self.assertEqual(1, len(link_msgstr))
192+ self.assertIn(po.langs[po_fn]['gettext_code'],
193+ link_msgstr[0])
194
195=== modified file 'edit-here/translations.py'
196--- edit-here/translations.py 2015-03-05 09:32:33 +0000
197+++ edit-here/translations.py 2015-03-09 14:34:15 +0000
198@@ -106,29 +106,37 @@
199 results += [entry]
200 return results
201
202- def replace_title_lines(self):
203- results = {}
204+ def find_title_lines(self):
205+ results = []
206 for entry in self.find_in_msgid('Title: '):
207 if entry.msgid.startswith('Title: '):
208 where = entry.occurrences[0][0]
209- first_line = open(where).readline().strip()
210- if entry.msgid != first_line:
211- print('Title line "%s" found, but not on the first line '
212- 'of "%s".' % (entry.msgid, entry.linenum))
213- return False
214- entry.msgid = entry.msgid.replace('Title: ', '')
215- fn = entry.occurrences[0][0]
216- results[fn] = entry.msgid
217- if self.po_fn.endswith('.po'):
218- entry.msgstr = ''
219+ first_line = codecs.open(where, encoding='utf-8').readline().strip()
220+ results += [(entry, first_line)]
221+ return results
222+
223+ def replace_title_lines(self):
224+ for entry, first_line in self.find_title_lines():
225+ if entry.msgid != first_line:
226+ print('Title line "%s" found, but not on the first line '
227+ 'of "%s".' % (entry.msgid, entry.linenum))
228+ return False
229+ entry.msgid = entry.msgid.replace('Title: ', '')
230+ if self.po_fn.endswith('.po'):
231+ entry.msgstr = ''
232 self.save()
233 return True
234
235+ def find_link_in_markdown_message(self, entry):
236+ link_regex = r'\[.+?\]\(\{filename\}(.+?)\)'
237+ link_msgid = re.findall(link_regex, entry.msgid)[0]
238+ link_msgstr = list(re.findall(link_regex, entry.msgstr))
239+ return (link_msgid, link_msgstr)
240+
241 def rewrite_links(self, documents, bcp47):
242- link_regex = r'\[.+?\]\(\{filename\}(.+?)\)'
243 for entry in self.find_in_msgid('{filename}'):
244- link_msgid = re.findall(link_regex, entry.msgid)[0]
245- link_msgstr = list(re.findall(link_regex, entry.msgstr))
246+ (link_msgid, link_msgstr) = \
247+ self.find_link_in_markdown_message(entry)
248 translated_doc_fn = os.path.basename(
249 documents.translated_doc_fn(link_msgid, bcp47))
250 if not link_msgstr:
251@@ -178,10 +186,15 @@
252 if not self.langs[po_fn]['pofile']:
253 self.langs[po_fn]['pofile'] = POFile(po_fn)
254
255- def generate_pot_file(self, documents):
256+ def gettextize(self, documents):
257 if not self.po4a.gettextize(documents.docs, self.pot_fn):
258 return False
259 self.pot_file_ob.reread()
260+ return True
261+
262+ def generate_pot_file(self, documents):
263+ if not self.gettextize(documents):
264+ return False
265 if not self.pot_file_ob.replace_title_lines():
266 return False
267 for po_fn in self.langs:

Subscribers

People subscribed via source and target branches