summaryrefslogtreecommitdiffstats
path: root/tests/test_python/test_lib.py
blob: 4c854365913c17471e8be865988d3efc8cf00e01 (plain)
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
# vim:fileencoding=utf-8:noet
from __future__ import (unicode_literals, division, absolute_import, print_function)

import threading
import os
import sys
import re
import shutil
import unicodedata

from time import sleep
from subprocess import call, PIPE

from powerline.lib import add_divider_highlight_group
from powerline.lib.dict import mergedicts, REMOVE_THIS_KEY
from powerline.lib.humanize_bytes import humanize_bytes
from powerline.lib.vcs import guess, get_fallback_create_watcher
from powerline.lib.threaded import ThreadedSegment, KwThreadedSegment
from powerline.lib.monotonic import monotonic
from powerline.lib.vcs.git import git_directory
from powerline.lib.shell import run_cmd

import powerline.lib.unicode as plu

from tests.modules.lib import Pl, replace_attr
from tests.modules import TestCase, SkipTest


try:
	__import__('bzrlib')
except ImportError:
	use_bzr = False
else:
	use_bzr = True

try:
	__import__('hglib')
except ImportError:
	use_mercurial = False
else:
	use_mercurial = True


GIT_REPO = 'git_repo'
HG_REPO = 'hg_repo'
BZR_REPO = 'bzr_repo'


def thread_number():
	return len(threading.enumerate())


class TestShell(TestCase):
	def test_run_cmd(self):
		pl = Pl()
		self.assertEqual(run_cmd(pl, ['xxx_nonexistent_command_xxx']), None)
		self.assertEqual(len(pl.exceptions), 1)
		pl = Pl()
		self.assertEqual(run_cmd(pl, ['echo', '  test  ']), 'test')
		self.assertFalse(pl)
		self.assertEqual(run_cmd(pl, ['echo', '  test  '], strip=True), 'test')
		self.assertFalse(pl)
		self.assertEqual(run_cmd(pl, ['echo', '  test  '], strip=False), '  test  \n')
		self.assertFalse(pl)
		self.assertEqual(run_cmd(pl, ['cat'], stdin='test'), 'test')
		self.assertFalse(pl)
		self.assertEqual(run_cmd(pl, ['sh', '-c', 'cat >&2'], stdin='test'), '')
		self.assertFalse(pl)


class TestThreaded(TestCase):
	def test_threaded_segment(self):
		log = []
		pl = Pl()
		updates = [(None,)]
		lock = threading.Lock()
		event = threading.Event()
		block_event = threading.Event()

		class TestSegment(ThreadedSegment):
			interval = 10

			def set_state(self, **kwargs):
				event.clear()
				log.append(('set_state', kwargs))
				return super(TestSegment, self).set_state(**kwargs)

			def update(self, update_value):
				block_event.wait()
				event.set()
				# Make sleep first to prevent some race conditions
				log.append(('update', update_value))
				with lock:
					ret = updates[0]
				if isinstance(ret, Exception):
					raise ret
				else:
					return ret[0]

			def render(self, update, **kwargs):
				log.append(('render', update, kwargs))
				if isinstance(update, Exception):
					raise update
				else:
					return update

		# Non-threaded tests
		segment = TestSegment()
		block_event.set()
		updates[0] = (None,)
		self.assertEqual(segment(pl=pl), None)
		self.assertEqual(thread_number(), 1)
		self.assertEqual(log, [
			('set_state', {}),
			('update', None),
			('render', None, {'pl': pl, 'update_first': True}),
		])
		log[:] = ()

		segment = TestSegment()
		block_event.set()
		updates[0] = ('abc',)
		self.assertEqual(segment(pl=pl), 'abc')
		self.assertEqual(thread_number(), 1)
		self.assertEqual(log, [
			('set_state', {}),
			('update', None),
			('render', 'abc', {'pl': pl, 'update_first': True}),
		])
		log[:] = ()

		segment = TestSegment()
		block_event.set()
		updates[0] = ('abc',)
		self.assertEqual(segment(pl=pl, update_first=False), 'abc')
		self.assertEqual(thread_number(), 1)
		self.assertEqual(log, [
			('set_state', {}),
			('update', None),
			('render', 'abc', {'pl': pl, 'update_first': False}),
		])
		log[:] = ()

		segment = TestSegment()
		block_event.set()
		updates[0] = ValueError('abc')
		self.assertEqual(segment(pl=pl), None)
		self.assertEqual(thread_number(), 1)
		self.assertEqual(len(pl.exceptions), 1)
		self.assertEqual(log, [
			('set_state', {}),
			('update', None),
		])
		log[:] = ()
		pl.exceptions[:] = ()

		segment = TestSegment()
		block_event.set()
		updates[0] = (TypeError('def'),)
		self.assertRaises(TypeError, segment, pl=pl)
		self.assertEqual(thread_number(), 1)
		self.assertEqual(log, [
			('set_state', {}),
			('update', None),
			('render', updates[0][0], {'pl': pl, 'update_first': True}),
		])
		log[:] = ()

		# Threaded tests
		segment = TestSegment()
		block_event.clear()
		kwargs = {'pl': pl, 'update_first': False, 'other': 1}
		with lock:
			updates[0] = ('abc',)
		segment.startup(**kwargs)
		ret = segment(**kwargs)
		self.assertEqual(thread_number(), 2)
		block_event.set()
		event.wait()
		segment.shutdown_event.set()
		segment.thread.join()
		self.assertEqual(ret, None)
		self.assertEqual(log, [
			('set_state', {'update_first': False, 'other': 1}),
			('render', None, {'pl': pl, 'update_first': False, 'other': 1}),
			('update', None),
		])
		log[:] = ()

		segment = TestSegment()
		block_event.set()
		kwargs = {'pl': pl, 'update_first': True, 'other': 1}
		with lock:
			updates[0] = ('def',)
		segment.startup(**kwargs)
		ret = segment(**kwargs)
		self.assertEqual(thread_number(), 2)
		segment.shutdown_event.set()
		segment.thread.join()
		self.assertEqual(ret, 'def')
		self.assertEqual(log, [
			('set_state', {'update_first': True, 'other': 1}),
			('update', None),
			('render', 'def', {'pl': pl, 'update_first': True, 'other': 1}),
		])
		log[:] = ()

		segment = TestSegment()
		block_event.set()
		kwargs = {'pl': pl, 'update_first': True, 'interval': 0.2}
		with lock:
			updates[0] = ('abc',)
		segment.startup(**kwargs)
		start = monotonic()
		ret1 = segment(**kwargs)
		with lock:
			updates[0] = ('def',)
		self.assertEqual(thread_number(), 2)
		sleep(0.5)
		ret2 = segment(**kwargs)
		segment.shutdown_event.set()
		segment.thread.join()
		end = monotonic()
		duration = end - start
		self.assertEqual(ret1, 'abc')
		self.assertEqual(ret2, 'def')
		self.assertEqual(log[:5], [
			('set_state', {'update_first': True, 'interval': 0.2}),
			('update', None),
			('render', 'abc', {'pl': pl, 'update_first': True, 'interval': 0.2}),
			('update', 'abc'),
			('update', 'def'),
		])
		num_runs = len([e for e in log if e[0] == 'update'])
		self.assertAlmostEqual(duration / 0.2, num_runs, delta=1)
		log[:] = ()

		segment = TestSegment()
		block_event.set()
		kwargs = {'pl': pl, 'update_first': True, 'interval': 0.2}
		with lock:
			updates[0] = ('ghi',)
		segment.startup(**kwargs)
		start = monotonic()
		ret1 = segment(**kwargs)
		with lock:
			updates[0] = TypeError('jkl')
		self.assertEqual(thread_number(), 2)
		sleep(0.5)
		ret2 = segment(**kwargs)
		segment.shutdown_event.set()
		segment.thread.join()
		end = monotonic()
		duration = end - start
		self.assertEqual(ret1, 'ghi')
		self.assertEqual(ret2, None)
		self.assertEqual(log[:5], [
			('set_state', {'update_first': True, 'interval': 0.2}),
			('update', None),
			('render', 'ghi', {'pl': pl, 'update_first': True, 'interval': 0.2}),
			('update', 'ghi'),
			('update', 'ghi'),
		])
		num_runs = len([e for e in log if e[0] == 'update'])
		self.assertAlmostEqual(duration / 0.2, num_runs, delta=1)
		self.assertEqual(num_runs - 1, len(pl.exceptions))
		log[:] = ()

	def test_kw_threaded_segment(self):
		log = []
		pl = Pl()
		event = threading.Event()

		class TestSegment(KwThreadedSegment):
			interval = 10

			@staticmethod
			def key(_key=(None,), **kwargs):
				log.append(('key', _key, kwargs))
				return _key

			def compute_state(self, key):
				event.set()
				sleep(0.1)
				log.append(('compute_state', key))
				ret = key
				if isinstance(ret, Exception):
					raise ret
				else:
					return ret[0]

			def render_one(self, state, **kwargs):
				log.append(('render_one', state, kwargs))
				if isinstance(state, Exception):
					raise state
				else:
					return state

		# Non-threaded tests
		segment = TestSegment()
		event.clear()
		self.assertEqual(segment(pl=pl), None)
		self.assertEqual(thread_number(), 1)
		self.assertEqual(log, [
			('key', (None,), {'pl': pl}),
			('compute_state', (None,)),
			('render_one', None, {'pl': pl}),
		])
		log[:] = ()

		segment = TestSegment()
		kwargs = {'pl': pl, '_key': ('abc',), 'update_first': False}
		event.clear()
		self.assertEqual(segment(**kwargs), 'abc')
		kwargs.update(_key=('def',))
		self.assertEqual(segment(**kwargs), 'def')
		self.assertEqual(thread_number(), 1)
		self.assertEqual(log, [
			('key', ('abc',), {'pl': pl}),
			('compute_state', ('abc',)),
			('render_one', 'abc', {'pl': pl, '_key': ('abc',)}),
			('key', ('def',), {'pl': pl}),
			('compute_state', ('def',)),
			('render_one', 'def', {'pl': pl, '_key': ('def',)}),
		])
		log[:] = ()

		segment = TestSegment()
		kwargs = {'pl': pl, '_key': ValueError('xyz'), 'update_first': False}
		event.clear()
		self.assertEqual(segment(**kwargs), None)
		self.assertEqual(thread_number(), 1)
		self.assertEqual(log, [
			('key', kwargs['_key'], {'pl': pl}),
			('compute_state', kwargs['_key']),
		])
		log[:] = ()

		segment = TestSegment()
		kwargs = {'pl': pl, '_key': (ValueError('abc'),), 'update_first': False}
		event.clear()
		self.assertRaises(ValueError, segment, **kwargs)
		self.assertEqual(thread_number(), 1)
		self.assertEqual(log, [
			('key', kwargs['_key'], {'pl': pl}),
			('compute_state', kwargs['_key']),
			('render_one', kwargs['_key'][0], {'pl': pl, '_key': kwargs['_key']}),
		])
		log[:] = ()

		# Threaded tests
		segment = TestSegment()
		kwargs = {'pl': pl, 'update_first': False, '_key': ('_abc',)}
		event.clear()
		segment.startup(**kwargs)
		ret = segment(**kwargs)
		self.assertEqual(thread_number(), 2)
		segment.shutdown_event.set()
		segment.thread.join()
		self.assertEqual(ret, None)
		self.assertEqual(log[:2], [
			('key', kwargs['_key'], {'pl': pl}),
			('render_one', None, {'pl': pl, '_key': kwargs['_key']}),
		])
		self.assertLessEqual(len(log), 3)
		if len(log) > 2:
			self.assertEqual(log[2], ('compute_state', kwargs['_key']))
		log[:] = ()

		segment = TestSegment()
		kwargs = {'pl': pl, 'update_first': True, '_key': ('_abc',)}
		event.clear()
		segment.startup(**kwargs)
		ret1 = segment(**kwargs)
		kwargs.update(_key=('_def',))
		ret2 = segment(**kwargs)
		self.assertEqual(thread_number(), 2)
		segment.shutdown_event.set()
		segment.thread.join()
		self.assertEqual(ret1, '_abc')
		self.assertEqual(ret2, '_def')
		self.assertEqual(log, [
			('key', ('_abc',), {'pl': pl}),
			('compute_state', ('_abc',)),
			('render_one', '_abc', {'pl': pl, '_key': ('_abc',)}),
			('key', ('_def',), {'pl': pl}),
			('compute_state', ('_def',)),
			('render_one', '_def', {'pl': pl, '_key': ('_def',)}),
		])
		log[:] = ()


class TestLib(TestCase):
	def test_mergedicts(self):
		d = {}
		mergedicts(d, {'abc': {'def': 'ghi'}})
		self.assertEqual(d, {'abc': {'def': 'ghi'}})
		mergedicts(d, {'abc': {'def': {'ghi': 'jkl'}}})
		self.assertEqual(d, {'abc': {'def': {'ghi': 'jkl'}}})
		mergedicts(d, {})
		self.assertEqual(d, {'abc': {'def': {'ghi': 'jkl'}}})
		mergedicts(d, {'abc': {'mno': 'pqr'}})
		self.assertEqual(d, {'abc': {'def': {'ghi': 'jkl'}, 'mno': 'pqr'}})
		mergedicts(d, {'abc': {'def': REMOVE_THIS_KEY}})
		self.assertEqual(d, {'abc': {'mno': 'pqr'}})

	def test_add_divider_highlight_group(self):
		def decorated_function_name(**kwargs):
			return str(kwargs)
		func = add_divider_highlight_group('hl_group')(decorated_function_name)
		self.assertEqual(func.__name__, 'decorated_function_name')
		self.assertEqual(func(kw={}), [{'contents': repr({str('kw'): {}}), 'divider_highlight_group': 'hl_group'}])

	def test_humanize_bytes(self):
		self.assertEqual(humanize_bytes(0), '0 B')
		self.assertEqual(humanize_bytes(1), '1 B')
		self.assertEqual(humanize_bytes(1, suffix='bit'), '1 bit')
		self.assertEqual(humanize_bytes(1000, si_prefix=True), '1 kB')
		self.assertEqual(humanize_bytes(1024, si_prefix=True), '1 kB')
		self.assertEqual(humanize_bytes(1000000000, si_prefix=True), '1.00 GB')
		self.assertEqual(humanize_bytes(1000000000, si_prefix=False), '953.7 MiB')


width_data = {
	'N': 1,          # Neutral
	'Na': 1,         # Narrow
	'A': 1,          # Ambiguous
	'H': 1,          # Half-width
	'W': 2,          # Wide
	'F': 2,          # Fullwidth
}


class TestUnicode(TestCase):
	def assertStringsIdentical(self, s1, s2):
		self.assertTrue(type(s1) is type(s2), msg='string types differ')
		self.assertEqual(s1, s2)

	def test_unicode(self):
		self.assertTrue(type('abc') is plu.unicode)

	def test_unichr(self):
		self.assertStringsIdentical('\U0010FFFF', plu.unichr(0x10FFFF))
		self.assertStringsIdentical('\uFFFF', plu.unichr(0xFFFF))
		self.assertStringsIdentical('\x20', plu.unichr(0x20))

	def test_u(self):
		self.assertStringsIdentical('Test', plu.u('Test'))
		self.assertStringsIdentical('Test', plu.u(b'Test'))
		self.assertStringsIdentical('«»', plu.u(b'\xC2\xAB\xC2\xBB'))
		self.assertRaises(UnicodeDecodeError, plu.u, b'\xFF')

	def test_tointiter(self):
		self.assertEqual([1, 2, 3], list(plu.tointiter(b'\x01\x02\x03')))

	def test_decode_error(self):
		self.assertStringsIdentical('<FF>', b'\xFF'.decode('utf-8', 'powerline_decode_error'))
		self.assertStringsIdentical('abc', b'abc'.decode('utf-8', 'powerline_decode_error'))

	def test_register_strwidth_error(self):
		ename = plu.register_strwidth_error(lambda s: 3)
		self.assertStringsIdentical(b'???', 'A'.encode('latin1', ename))
		self.assertStringsIdentical(b'abc', 'abc'.encode('latin1', ename))

	def test_out_u(self):
		self.assertStringsIdentical('abc', plu.out_u('abc'))
		self.assertStringsIdentical('abc', plu.out_u(b'abc'))
		self.assertRaises(TypeError, plu.out_u, None)

	def test_safe_unicode(self):
		self.assertStringsIdentical('abc', plu.safe_unicode('abc'))
		self.assertStringsIdentical('abc', plu.safe_unicode(b'abc'))
		self.assertStringsIdentical('«»', plu.safe_unicode(b'\xc2\xab\xc2\xbb'))
		with replace_attr(plu, 'get_preferred_output_encoding', lambda: 'latin1'):
			self.assertStringsIdentical('ÿ', plu.safe_unicode(b'\xFF'))
		self.assertStringsIdentical('None', plu.safe_unicode(None))

		class FailingStr(object):
			def __str__(self):
				raise NotImplementedError('Fail!')

		self.assertStringsIdentical('Fail!', plu.safe_unicode(FailingStr()))

	def test_FailedUnicode(self):
		self.assertTrue(isinstance(plu.FailedUnicode('abc'), plu.unicode))
		self.assertEqual('abc', plu.FailedUnicode('abc'))

	def test_string(self):
		self.assertStringsIdentical(str('abc'), plu.string('abc'))
		self.assertStringsIdentical(str('abc'), plu.string(b'abc'))

	def test_surrogate_pair_to_character(self):
		self.assertEqual(0x1F48E, plu.surrogate_pair_to_character(0xD83D, 0xDC8E))

	def test_strwidth_ucs_4(self):
		self.assertEqual(4, plu.strwidth_ucs_4(width_data, 'abcd'))
		self.assertEqual(4, plu.strwidth_ucs_4(width_data, 'AB'))
		if sys.maxunicode < 0x10FFFF:
			raise SkipTest('Can only test strwidth_ucs_4 in UCS-4 Pythons')

		self.assertEqual(1, plu.strwidth_ucs_4(width_data, '\U0001F063'))

	def test_strwidth_ucs_2(self):
		self.assertEqual(4, plu.strwidth_ucs_2(width_data, 'abcd'))
		self.assertEqual(4, plu.strwidth_ucs_2(width_data, 'AB'))
		if not sys.maxunicode < 0x10FFFF:
			raise SkipTest('Can only test strwidth_ucs_2 in UCS-2 Pythons')
		self.assertEqual(1, plu.strwidth_ucs_2(width_data, '\ud83c\udc30'))


class TestVCS(TestCase):
	def do_branch_rename_test(self, repo, q):
		st = monotonic()
		while monotonic() - st < 1:
			# Give inotify time to deliver events
			ans = repo.branch()
			if hasattr(q, '__call__'):
				if q(ans):
					break
			else:
				if ans == q:
					break
			sleep(0.01)
		if hasattr(q, '__call__'):
			self.assertTrue(q(ans))
		else:
			self.assertEqual(ans, q)

	def test_git(self):
		create_watcher = get_fallback_create_watcher()
		repo = guess(path=GIT_REPO, create_watcher=create_watcher)
		self.assertNotEqual(repo, None)
		self.assertEqual(repo.branch(), 'master')
		self.assertEqual(repo.status(), None)
		self.assertEqual(repo.status('file'), None)
		with open(os.path.join(GIT_REPO, 'file'), 'w') as f:
			f.write('abc')
			f.flush()
			self.assertEqual(repo.status(), '  U')
			self.assertEqual(repo.status('file'), '??')
			call(['git', 'add', '.'], cwd=GIT_REPO)
			self.assertEqual(repo.status(), ' I ')
			self.assertEqual(repo.status('file'), 'A ')
			f.write('def')
			f.flush()
			self.assertEqual(repo.status(), 'DI ')
			self.assertEqual(repo.status('file'), 'AM')
		os.remove(os.path.join(GIT_REPO, 'file'))
		# Test changing branch
		self.assertEqual(repo.branch(), 'master')
		try:
			call(['git', 'branch', 'branch1'], cwd=GIT_REPO)
			call(['git', 'checkout', '-q', 'branch1'], cwd=GIT_REPO)
			self.do_branch_rename_test(repo, 'branch1')
			call(['git', 'branch', 'branch2'], cwd=GIT_REPO)
			call(['git', 'checkout', '-q', 'branch2'], cwd=GIT_REPO)
			self.do_branch_rename_test(repo, 'branch2')
			call(['git', 'checkout', '-q', '--detach', 'branch1'], cwd=GIT_REPO)
			self.do_branch_rename_test(repo, lambda b: re.match(r'^[a-f0-9]+$', b))
		finally:
			call(['git', 'checkout', '-q', 'master'], cwd=GIT_REPO)
		# Test stashing
		self.assertEqual(repo.stash(), 0)

		def stash_save():
			with open(os.path.join(GIT_REPO, 'file'), 'w') as f:
				f.write('abc')
			return call(['git', 'stash', '-u'], cwd=GIT_REPO, stdout=PIPE)

		def stash_drop():
			return call(['git', 'stash', 'drop'], cwd=GIT_REPO, stdout=PIPE)

		def stash_list():
			return call(['git', 'stash', 'list'], cwd=GIT_REPO, stdout=PIPE)

		try:
			stash_save()
			self.assertEqual(repo.stash(), 1)
			stash_save()
			self.assertEqual(repo.stash(), 2)
			stash_drop()
			self.assertEqual(repo.stash(), 1)
			stash_drop()
			self.assertEqual(repo.stash(), 0)
		finally:
			while stash_list():
			    stash_drop()

	def test_git_sym(self):
		create_watcher = get_fallback_create_watcher()
		dotgit = os.path.join(GIT_REPO, '.git')
		spacegit = os.path.join(GIT_REPO, ' .git ')
		os.rename(dotgit, spacegit)
		try:
			with open(dotgit, 'w') as F:
				F.write('gitdir:  .git \n')
			gitdir = git_directory(GIT_REPO)
			self.assertTrue(os.path.isdir(gitdir))
			self.assertEqual(gitdir, os.path.abspath(spacegit))
			repo = guess(path=GIT_REPO, create_watcher=create_watcher)
			self.assertEqual(repo.branch(), 'master')
		finally:
			os.remove(dotgit)
			os.rename(spacegit, dotgit)

	def test_mercurial(self):
		if not use_mercurial:
			raise SkipTest('Mercurial is not available')
		create_watcher = get_fallback_create_watcher()
		repo = guess(path=HG_REPO, create_watcher=create_watcher)
		self.assertNotEqual(repo, None)
		self.assertEqual(repo.branch(), 'default')
		self.assertEqual(repo.status(), None)
		with open(os.path.join(HG_REPO, 'file'), 'w') as f:
			f.write('abc')
			f.flush()
			self.assertEqual(repo.status(), ' U')
			self.assertEqual(repo.status('file'), 'U')
			call(['hg', 'add', '.'], cwd=HG_REPO, stdout=PIPE)
			self.assertEqual(repo.status(), 'D ')
			self.assertEqual(repo.status('file'), 'A')
		os.remove(os.path.join(HG_REPO, 'file'))

	def test_bzr(self):
		if not use_bzr:
			raise SkipTest('Bazaar is not available')
		create_watcher = get_fallback_create_watcher()
		repo = guess(path=BZR_REPO, create_watcher=create_watcher)
		self.assertNotEqual(repo, None, 'No bzr repo found. Do you have bzr installed?')
		self.assertEqual(repo.branch(), 'test_powerline')
		self.assertEqual(repo.status(), None)
		with open(os.path.join(BZR_REPO, 'file'), 'w') as f:
			f.write('abc')
		self.assertEqual(repo.status(), ' U')
		self.assertEqual(repo.status('file'), '? ')
		call(['bzr', 'add', '-q', '.'], cwd=BZR_REPO, stdout=PIPE)
		self.assertEqual(repo.status(), 'D ')
		self.assertEqual(repo.status('file'), '+N')
		call(['bzr', 'commit', '-q', '-m', 'initial commit'], cwd=BZR_REPO)
		self.assertEqual(repo.status(), None)
		with open(os.path.join(BZR_REPO, 'file'), 'w') as f:
			f.write('def')
		self.assertEqual(repo.status(), 'D ')
		self.assertEqual(repo.status('file'), ' M')
		self.assertEqual(repo.status('notexist'), None)
		with open(os.path.join(BZR_REPO, 'ignored'), 'w') as f:
			f.write('abc')
		self.assertEqual(repo.status('ignored'), '? ')
		# Test changing the .bzrignore file should update status
		with open(os.path.join(BZR_REPO, '.bzrignore'), 'w') as f:
			f.write('ignored')
		self.assertEqual(repo.status('ignored'), None)
		# Test changing the dirstate file should invalidate the cache for
		# all files in the repo
		with open(os.path.join(BZR_REPO, 'file2'), 'w') as f:
			f.write('abc')
		call(['bzr', 'add', 'file2'], cwd=BZR_REPO, stdout=PIPE)
		call(['bzr', 'commit', '-q', '-m', 'file2 added'], cwd=BZR_REPO)
		with open(os.path.join(BZR_REPO, 'file'), 'a') as f:
			f.write('hello')
		with open(os.path.join(BZR_REPO, 'file2'), 'a') as f:
			f.write('hello')
		self.assertEqual(repo.status('file'), ' M')
		self.assertEqual(repo.status('file2'), ' M')
		call(['bzr', 'commit', '-q', '-m', 'multi'], cwd=BZR_REPO)
		self.assertEqual(repo.status('file'), None)
		self.assertEqual(repo.status('file2'), None)

		# Test changing branch
		call(['bzr', 'nick', 'branch1'], cwd=BZR_REPO, stdout=PIPE, stderr=PIPE)
		self.do_branch_rename_test(repo, 'branch1')

		# Test branch name/status changes when swapping repos
		for x in ('b1', 'b2'):
			d = os.path.join(BZR_REPO, x)
			os.mkdir(d)
			call(['bzr', 'init', '-q'], cwd=d)
			call(['bzr', 'nick', '-q', x], cwd=d)
			repo = guess(path=d, create_watcher=create_watcher)
			self.assertEqual(repo.branch(), x)
			self.assertFalse(repo.status())
			if x == 'b1':
				open(os.path.join(d, 'dirty'), 'w').close()
				self.assertTrue(repo.status())
		os.rename(os.path.join(BZR_REPO, 'b1'), os.path.join(BZR_REPO, 'b'))
		os.rename(os.path.join(BZR_REPO, 'b2'), os.path.join(BZR_REPO, 'b1'))
		os.rename(os.path.join(BZR_REPO, 'b'), os.path.join(BZR_REPO, 'b2'))
		for x, y in (('b1', 'b2'), ('b2', 'b1')):
			d = os.path.join(BZR_REPO, x)
			repo = guess(path=d, create_watcher=create_watcher)
			self.do_branch_rename_test(repo, y)
			if x == 'b1':
				self.assertFalse(repo.status())
			else:
				self.assertTrue(repo.status())

	@classmethod
	def setUpClass(cls):
		cls.powerline_old_cwd = os.getcwd()
		os.chdir(os.path.dirname(os.path.dirname(__file__)))
		call(['git', 'init', '--quiet', GIT_REPO])
		assert os.path.isdir(GIT_REPO)
		call(['git', 'config', '--local', 'user.name', 'Foo'], cwd=GIT_REPO)
		call(['git', 'config', '--local', 'user.email', 'bar@example.org'], cwd=GIT_REPO)
		call(['git', 'commit', '--allow-empty', '--message', 'Initial commit', '--quiet'], cwd=GIT_REPO)
		if use_mercurial:
			cls.powerline_old_HGRCPATH = os.environ.get('HGRCPATH')
			os.environ['HGRCPATH'] = ''
			call(['hg', 'init', HG_REPO])
			with open(os.path.join(HG_REPO, '.hg', 'hgrc'), 'w') as hgrc:
				hgrc.write('[ui]\n')
				hgrc.write('username = Foo <bar@example.org>\n')
		if use_bzr:
			call(['bzr', 'init', '--quiet', BZR_REPO])
			call(['bzr', 'config', 'email=Foo <bar@example.org>'], cwd=BZR_REPO)
			call(['bzr', 'config', 'nickname=test_powerline'], cwd=BZR_REPO)
			call(['bzr', 'config', 'create_signatures=0'], cwd=BZR_REPO)

	@classmethod
	def tearDownClass(cls):
		for repo_dir in [GIT_REPO] + ([HG_REPO] if use_mercurial else []) + ([BZR_REPO] if use_bzr else []):
			shutil.rmtree(repo_dir)
		if use_mercurial:
			if cls.powerline_old_HGRCPATH is None:
				os.environ.pop('HGRCPATH')
			else:
				os.environ['HGRCPATH'] = cls.powerline_old_HGRCPATH
		os.chdir(cls.powerline_old_cwd)


if __name__ == '__main__':
	from tests.modules import main
	main()