File: main.py

package info (click to toggle)
zim 0.76.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,952 kB
  • sloc: python: 68,612; xml: 1,270; javascript: 512; sh: 101; makefile: 47
file content (340 lines) | stat: -rw-r--r-- 9,031 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340

# Copyright 2012-2022 Jaap Karssenberg <jaap.karssenberg@gmail.com>


import tests

from tests.config import EnvironmentConfigContext, ConfigManager

import sys
import io as StringIO
import threading
import time


from zim.main import *

import zim
import zim.main


from zim.notebook.info import NotebookInfo


class capture_stdout:

		def __enter__(self):
			self.real_stdout = sys.stdout
			sys.stdout = StringIO.StringIO()
			return sys.stdout

		def __exit__(self, type, value, traceback):
			sys.stdout = self.real_stdout


class TestParseCommand(tests.TestCase):

	def runTest(self):
		for command, klass in list(zim.main.commands.items()):
			obj = zim.main.build_command(['--%s' % command])
			self.assertIsInstance(obj, klass)

		obj = zim.main.build_command(['-v'])
		self.assertIsInstance(obj, VersionCommand)

		obj = zim.main.build_command(['-h'])
		self.assertIsInstance(obj, HelpCommand)

		obj = zim.main.build_command(['foo'])
		self.assertIsInstance(obj, GuiCommand)

		obj = zim.main.build_command(['--plugin', 'quicknote'])
		self.assertIsInstance(obj, Command)

		obj = zim.main.build_command(['--server', '--gui'])
		self.assertIsInstance(obj, ServerGuiCommand)


class TestVersion(tests.TestCase):

	def runTest(self):
		cmd = VersionCommand('version')
		with capture_stdout() as output:
			cmd.run()
		self.assertTrue(output.getvalue().startswith('zim'))


class TestHelp(tests.TestCase):

	def runTest(self):
		cmd = HelpCommand('help')
		with capture_stdout() as output:
			cmd.run()
		self.assertTrue(output.getvalue().startswith('usage:'))


class TestNotebookCommand(tests.TestCase):

	cmd_class = NotebookCommand

	def get_cmd(self, *args):
		cmd = self.cmd_class('gui')
		cmd.arguments = ('NOTEBOOK', '[PAGE]')
		cmd.parse_options(*args)
		return cmd

	def testPWDhandling(self):
		# check if PWD at "parse_options" is remembered after changing dir
		cmd = self.get_cmd('./Notes')

		pwd = os.getcwd()
		self.addCleanup(os.chdir, pwd)
		os.chdir('/')

		myinfo = NotebookInfo(pwd + '/Notes')
		notebookinfo, page = cmd.get_notebook_argument()
		self.assertEqual(notebookinfo, myinfo)

	#def testNotebookName(self):
	#	pass

	def testFile(self):
		folder = self.setUpFolder(mock=tests.MOCK_ALWAYS_REAL)
		file = folder.file('Foo.txt')
		file.touch()

		for arg in (file.path, file.uri):
			cmd = self.get_cmd(arg)
			myinfo = NotebookInfo(file.path)
			notebookinfo, page = cmd.get_notebook_argument()
			self.assertEqual(notebookinfo, myinfo)
			notebook, href = cmd.build_notebook()
			self.assertEqual(notebook.uri, folder.uri)
			self.assertEqual(href.names, 'Foo')

		# Test override by page argument
		cmd = self.get_cmd(file.path, 'Bar')
		notebook, href = cmd.build_notebook()
		self.assertEqual(notebook.uri, folder.uri)
		self.assertEqual(href.names, 'Bar')

	def testNotebookZimFile(self):
		folder = self.setUpFolder(mock=tests.MOCK_ALWAYS_REAL)
		config = folder.file('notebook.zim')
		config.touch()

		for arg in (config.path, config.uri):
			cmd = self.get_cmd(arg)
			myinfo = NotebookInfo(config.path)
			notebookinfo, page = cmd.get_notebook_argument()
			self.assertEqual(notebookinfo, myinfo)
			notebook, href = cmd.build_notebook()
			self.assertEqual(notebook.uri, folder.uri)
			self.assertIsNone(href)

	def testNestedNotebookZimFile(self):
		# Test specific case where parent folder also is a notebook - as in bug #2189
		parentfolder = self.setUpFolder(mock=tests.MOCK_ALWAYS_REAL)
		parentfolder.file('notebook.zim').touch()
		folder = parentfolder.folder('MyNotebook')
		config = folder.file('notebook.zim')
		config.touch()

		for arg in (config.path, config.uri):
			cmd = self.get_cmd(arg)
			myinfo = NotebookInfo(config.path)
			notebookinfo, page = cmd.get_notebook_argument()
			self.assertEqual(notebookinfo, myinfo)
			notebook, href = cmd.build_notebook()
			self.assertEqual(notebook.uri, folder.uri)
			self.assertIsNone(href)



class TestGuiCommand(TestNotebookCommand):

	cmd_class = GuiCommand


class TestGuiStart(tests.TestCase):

	## TODO: test default notebook logic when no argument

	def setUp(self):
		file = ConfigManager.get_config_file('notebooks.list')
		file.remove()

	def runTest(self):
		from zim.gui.mainwindow import MainWindow

		## Without argument should prompt
		def testAddNotebookDialog(dialog):
			self.assertIn(dialog.__class__.__name__,
				('AddNotebookDialog', 'NotebookDialog')
			)

		cmd = GuiCommand('gui')
		with tests.DialogContext(testAddNotebookDialog):
			cmd.run() # Exits without running due to no notebook given in dialog

		### Try again with argument
		folder = self.setUpFolder(mock=tests.MOCK_ALWAYS_REAL)
		folder.touch()

		cmd = GuiCommand('gui')
		cmd.parse_options(folder.path)
		with tests.WindowContext(MainWindow):
			with tests.LoggingFilter('zim', 'Exception while loading plugin:'):
				window = cmd.run()
				self.addCleanup(window.destroy)

		self.assertEqual(window.__class__.__name__, 'MainWindow')
		self.assertEqual(window.notebook.uri, folder.uri)
		self.assertGreaterEqual(len(ConfigManager.preferences['General']['plugins']), 3)
		self.assertGreaterEqual(len(window.pageview.__zim_extension_objects__), 3)

		with tests.WindowContext(MainWindow):
			window2 = cmd.run()
		self.assertIs(window2, window)
			# Ensure repeated calling gives unique window


class TestGuiListCommand(tests.TestCase):

	# NotebookDialog has it's own't test cases, which also cover the
	# AddNotebookDialog and prompt_notebook, so here we can mock it and
	# just test it is called properly from the command class

	def setUp(self):
		from zim.notebook import NotebookInfo

		self.folder = self.setUpFolder(mock=tests.MOCK_ALWAYS_REAL)
		self.folder.touch()

		import zim.gui.notebookdialog
		orig = zim.gui.notebookdialog.prompt_notebook
		def restore():
			zim.gui.notebookdialog.prompt_notebook = orig
		self.addCleanup(restore)

		def mock():
			return NotebookInfo(self.folder.uri, name='Test')

		zim.gui.notebookdialog.prompt_notebook = mock

	def runTest(self):
		from zim.gui.mainwindow import MainWindow

		cmd = GuiCommand('gui')
		cmd.parse_options('--list')

		with tests.WindowContext(MainWindow):
			with tests.LoggingFilter('zim', 'Exception while loading plugin:'):
				window = cmd.run()
				self.addCleanup(window.destroy)


class TestManual(tests.TestCase):

	def runTest(self):
		from zim.gui.mainwindow import MainWindow

		cmd = ManualCommand('manual')
		with tests.WindowContext(MainWindow):
			with tests.LoggingFilter('zim', 'Exception while loading plugin:'):
				window = cmd.run()
				self.addCleanup(window.destroy)

		self.assertEqual(window.__class__.__name__, 'MainWindow')


@tests.slowTest
class TestServer(tests.TestCase):

	def runTest(self):
		from urllib.request import urlopen
		from urllib.error import URLError

		dir = self.setUpFolder(mock=tests.MOCK_ALWAYS_REAL)
		dir.touch()
		cmd = ServerCommand('server')
		cmd.parse_options(dir.path)
		t = threading.Thread(target=cmd.run)
		t.start()

		for i in range(30):
			try:
				re = urlopen('http://localhost:8080')
				self.assertEqual(re.getcode(), 200)
				break
			except URLError:
				time.sleep(1) # give more time to startup server
		else:
			assert False, 'Failed to start server within 10 seconds'

		cmd.server.shutdown()
		t.join()


class TestServerGui(tests.TestCase):

	def runTest(self):
		cmd = ServerGuiCommand('server')
		window = cmd.run()
		self.addCleanup(window.destroy)
		self.assertEqual(window.__class__.__name__, 'ServerWindow')


## ExportCommand() is tested in tests/export.py

import os

class TestZimScript(tests.TestCase):

	def testEnvironINI(self):
		# Ensure restoring old environment
		orig_environ = os.environ.copy()
		def restore_environ():
			os.environ.clear()
			os.environ.update(orig_environ)
		self.addCleanup(restore_environ)

		# Setup tmp file
		folder = self.setUpFolder(mock=tests.MOCK_ALWAYS_REAL)
		folder.touch()

		# Import the script - which is not a module ...
		globals = {}
		scriptfile = 'zim.py'
		with open(scriptfile) as f:
			code = compile(f.read(), scriptfile, 'exec')
			exec(code, globals)
		init_environment = globals['init_environment']

		# Test missing file is silent, and no chance to data dir
		os.environ['XDG_DATA_DIRS'] = 'TEST'
		init_environment(folder.path)
		self.assertEqual(os.environ['XDG_DATA_DIRS'], 'TEST')

		# Test with existing data dir
		data_dir = folder.folder('share')
		data_dir.touch()
		init_environment(folder.path)
		self.assertEqual(os.environ['XDG_DATA_DIRS'], 'TEST' + os.pathsep + os.path.normpath(data_dir.path))

		# Setup file
		file = folder.file('environ.ini')
		file.write(
			'[Environment]\n'
			'MYHOME=../home\n'
			'MYPATH=${PATH}' + os.pathsep + './bin\n'
		)
		# write tmp file
		# 	- abs path
		#   - rel path
		#   - os.pathsep

		# Test with file in place
		init_environment(folder.path)
		self.assertEqual(os.environ['MYHOME'], os.path.normpath(folder.parent().folder('home').path))
		self.assertEqual(os.environ['MYPATH'], os.environ['PATH'] + os.pathsep + os.path.normpath(folder.folder('bin').path))