2013-03-11 10:40:09 +01:00
# vim:fileencoding=utf-8:noet
2013-02-24 11:03:14 +01:00
''' Dynamic configuration files tests. '''
2014-08-31 20:55:26 +02:00
from __future__ import ( unicode_literals , division , absolute_import , print_function )
2013-02-24 11:03:14 +01:00
import sys
import os
import json
2014-09-21 13:52:58 +02:00
import logging
2014-08-31 20:55:26 +02:00
import tests . vim as vim_module
2013-03-23 14:51:02 +01:00
from tests . lib import Args , urllib_read , replace_attr
2013-03-03 17:40:35 +01:00
from tests import TestCase
2013-02-24 11:03:14 +01:00
2014-09-21 13:52:58 +02:00
from powerline import NotInterceptedError
2014-09-21 13:49:00 +02:00
from powerline . segments . common import wthr
2013-02-24 11:03:14 +01:00
2014-09-21 13:52:58 +02:00
2013-02-24 11:03:14 +01:00
VBLOCK = chr ( ord ( ' V ' ) - 0x40 )
SBLOCK = chr ( ord ( ' S ' ) - 0x40 )
2014-09-21 13:52:58 +02:00
class FailingLogger ( logging . Logger ) :
def exception ( self , * args , * * kwargs ) :
super ( FailingLogger , self ) . exception ( * args , * * kwargs )
raise NotInterceptedError ( ' Unexpected exception occurred ' )
def get_logger ( stream = None ) :
log_format = ' %(asctime)s : %(levelname)s : %(message)s '
formatter = logging . Formatter ( log_format )
level = logging . WARNING
handler = logging . StreamHandler ( stream )
handler . setLevel ( level )
handler . setFormatter ( formatter )
logger = FailingLogger ( ' powerline ' )
logger . setLevel ( level )
logger . addHandler ( handler )
return logger
2014-09-04 19:32:26 +02:00
class TestVimConfig ( TestCase ) :
2013-02-24 11:03:14 +01:00
def test_vim ( self ) :
from powerline . vim import VimPowerline
cfg_path = os . path . join ( os . path . dirname ( os . path . dirname ( __file__ ) ) , ' powerline ' , ' config_files ' )
2013-08-20 16:48:49 +02:00
buffers = (
( ( ' bufoptions ' , ) , { ' buftype ' : ' help ' } ) ,
( ( ' bufname ' , ' [Command Line] ' ) , { } ) ,
( ( ' bufoptions ' , ) , { ' buftype ' : ' quickfix ' } ) ,
2014-01-11 08:51:58 +01:00
( ( ' bufname ' , ' NERD_tree_1 ' ) , { } ) ,
( ( ' bufname ' , ' __Gundo__ ' ) , { } ) ,
( ( ' bufname ' , ' __Gundo_Preview__ ' ) , { } ) ,
2014-11-08 11:19:02 +01:00
# No Command-T tests here: requires +ruby or emulation
# No tabline here: tablines are tested separately
2013-08-20 16:48:49 +02:00
)
2013-02-24 11:03:14 +01:00
with open ( os . path . join ( cfg_path , ' config.json ' ) , ' r ' ) as f :
2013-08-20 16:48:49 +02:00
local_themes_raw = json . load ( f ) [ ' ext ' ] [ ' vim ' ] [ ' local_themes ' ]
2014-09-20 15:48:15 +02:00
# Don’ t run tests on external/plugin segments
2014-01-11 08:51:58 +01:00
local_themes = dict ( ( k , v ) for ( k , v ) in local_themes_raw . items ( ) )
2014-11-08 11:19:02 +01:00
# See end of the buffers definition above for `- 2`
self . assertEqual ( len ( buffers ) , len ( local_themes ) - 2 )
2013-02-24 11:03:14 +01:00
outputs = { }
i = 0
2013-03-25 05:59:37 +01:00
2013-05-17 06:51:33 +02:00
with vim_module . _with ( ' split ' ) :
2014-09-21 13:52:58 +02:00
with VimPowerline ( logger = get_logger ( ) ) as powerline :
2013-05-19 10:57:33 +02:00
def check_output ( mode , args , kwargs ) :
2013-05-17 06:51:33 +02:00
if mode == ' nc ' :
window = vim_module . windows [ 0 ]
window_id = 2
else :
vim_module . _start_mode ( mode )
window = vim_module . current . window
window_id = 1
winnr = window . number
out = powerline . render ( window , window_id , winnr )
if out in outputs :
self . fail ( ' Duplicate in set # {0} ( {1} ) for mode {2!r} (previously defined in set # {3} ( {4!r} ) for mode {5!r} ) ' . format ( i , ( args , kwargs ) , mode , * outputs [ out ] ) )
outputs [ out ] = ( i , ( args , kwargs ) , mode )
2013-05-19 11:17:30 +02:00
with vim_module . _with ( ' bufname ' , ' /tmp/foo.txt ' ) :
2014-08-22 18:39:31 +02:00
out = powerline . render ( vim_module . current . window , 1 , vim_module . current . window . number , is_tabline = True )
2014-08-03 07:07:31 +02:00
outputs [ out ] = ( - 1 , ( None , None ) , ' tab ' )
2014-09-05 20:13:51 +02:00
with vim_module . _with ( ' globals ' , powerline_config_paths = [ cfg_path ] ) :
2013-05-17 06:51:33 +02:00
exclude = set ( ( ' no ' , ' v ' , ' V ' , VBLOCK , ' s ' , ' S ' , SBLOCK , ' R ' , ' Rv ' , ' c ' , ' cv ' , ' ce ' , ' r ' , ' rm ' , ' r? ' , ' ! ' ) )
try :
for mode in [ ' n ' , ' nc ' , ' no ' , ' v ' , ' V ' , VBLOCK , ' s ' , ' S ' , SBLOCK , ' i ' , ' R ' , ' Rv ' , ' c ' , ' cv ' , ' ce ' , ' r ' , ' rm ' , ' r? ' , ' ! ' ] :
2013-05-19 10:57:33 +02:00
check_output ( mode , None , None )
2013-05-17 06:51:33 +02:00
for args , kwargs in buffers :
i + = 1
if mode in exclude :
continue
with vim_module . _with ( * args , * * kwargs ) :
2013-05-19 10:57:33 +02:00
check_output ( mode , args , kwargs )
2013-05-17 06:51:33 +02:00
finally :
vim_module . _start_mode ( ' n ' )
2013-02-24 11:03:14 +01:00
2014-09-04 19:32:26 +02:00
@classmethod
def setUpClass ( cls ) :
sys . path . insert ( 0 , os . path . abspath ( os . path . join ( os . path . dirname ( __file__ ) , ' path ' ) ) )
@classmethod
def tearDownClass ( cls ) :
sys . path . pop ( 0 )
class TestConfig ( TestCase ) :
2013-03-02 16:05:08 +01:00
def test_tmux ( self ) :
2013-03-02 21:02:07 +01:00
from powerline . segments import common
2013-03-02 16:05:08 +01:00
from imp import reload
2013-03-02 21:02:07 +01:00
reload ( common )
2013-03-02 16:05:08 +01:00
from powerline . shell import ShellPowerline
2013-03-23 14:51:02 +01:00
with replace_attr ( common , ' urllib_read ' , urllib_read ) :
2014-09-21 13:52:58 +02:00
with ShellPowerline ( Args ( ext = [ ' tmux ' ] ) , logger = get_logger ( ) , run_once = False ) as powerline :
2013-03-25 05:59:37 +01:00
powerline . render ( )
2014-09-21 13:52:58 +02:00
with ShellPowerline ( Args ( ext = [ ' tmux ' ] ) , logger = get_logger ( ) , run_once = False ) as powerline :
2013-03-25 05:59:37 +01:00
powerline . render ( )
2013-03-11 19:09:34 +01:00
def test_zsh ( self ) :
from powerline . shell import ShellPowerline
2014-08-15 17:15:49 +02:00
args = Args ( last_pipe_status = [ 1 , 0 ] , jobnum = 0 , ext = [ ' shell ' ] , renderer_module = ' .zsh ' )
2014-02-16 17:52:22 +01:00
segment_info = { ' args ' : args }
2014-09-21 13:52:58 +02:00
with ShellPowerline ( args , logger = get_logger ( ) , run_once = False ) as powerline :
2014-02-16 17:52:22 +01:00
powerline . render ( segment_info = segment_info )
2014-09-21 13:52:58 +02:00
with ShellPowerline ( args , logger = get_logger ( ) , run_once = False ) as powerline :
2014-02-16 17:52:22 +01:00
powerline . render ( segment_info = segment_info )
segment_info [ ' local_theme ' ] = ' select '
2014-09-21 13:52:58 +02:00
with ShellPowerline ( args , logger = get_logger ( ) , run_once = False ) as powerline :
2014-02-16 17:52:22 +01:00
powerline . render ( segment_info = segment_info )
segment_info [ ' local_theme ' ] = ' continuation '
segment_info [ ' parser_state ' ] = ' if cmdsubst '
2014-09-21 13:52:58 +02:00
with ShellPowerline ( args , logger = get_logger ( ) , run_once = False ) as powerline :
2014-02-16 17:52:22 +01:00
powerline . render ( segment_info = segment_info )
2013-03-11 19:09:34 +01:00
def test_bash ( self ) :
from powerline . shell import ShellPowerline
2015-01-06 18:58:46 +01:00
args = Args ( last_exit_code = 1 , jobnum = 0 , ext = [ ' shell ' ] , renderer_module = ' .bash ' , config_override = { ' ext ' : { ' shell ' : { ' theme ' : ' default_leftonly ' } } } )
2014-09-21 13:52:58 +02:00
with ShellPowerline ( args , logger = get_logger ( ) , run_once = False ) as powerline :
2013-03-30 18:55:00 +01:00
powerline . render ( segment_info = { ' args ' : args } )
2014-09-21 13:52:58 +02:00
with ShellPowerline ( args , logger = get_logger ( ) , run_once = False ) as powerline :
2013-03-30 18:55:00 +01:00
powerline . render ( segment_info = { ' args ' : args } )
2013-03-11 19:09:34 +01:00
def test_ipython ( self ) :
2014-08-19 17:44:07 +02:00
from powerline . ipython import IPythonPowerline
2013-03-11 19:09:34 +01:00
2014-08-19 17:44:07 +02:00
class IpyPowerline ( IPythonPowerline ) :
2015-01-06 19:02:13 +01:00
config_paths = None
2013-03-11 19:09:34 +01:00
config_overrides = None
theme_overrides = { }
2014-08-06 11:47:11 +02:00
segment_info = Args ( prompt_count = 1 )
2014-09-21 13:52:58 +02:00
with IpyPowerline ( logger = get_logger ( ) ) as powerline :
2014-08-06 11:47:11 +02:00
for prompt_type in [ ' in ' , ' in2 ' ] :
2014-08-20 19:12:10 +02:00
powerline . render ( is_prompt = True , matcher_info = prompt_type , segment_info = segment_info )
powerline . render ( is_prompt = True , matcher_info = prompt_type , segment_info = segment_info )
2014-09-21 13:52:58 +02:00
with IpyPowerline ( logger = get_logger ( ) ) as powerline :
2014-08-06 11:47:11 +02:00
for prompt_type in [ ' out ' , ' rewrite ' ] :
2014-08-20 19:12:10 +02:00
powerline . render ( is_prompt = False , matcher_info = prompt_type , segment_info = segment_info )
powerline . render ( is_prompt = False , matcher_info = prompt_type , segment_info = segment_info )
2013-03-11 19:09:34 +01:00
def test_wm ( self ) :
from powerline . segments import common
from imp import reload
reload ( common )
2013-03-11 19:22:18 +01:00
from powerline import Powerline
2014-09-21 13:49:00 +02:00
with replace_attr ( wthr , ' urllib_read ' , urllib_read ) :
2014-09-21 13:52:58 +02:00
Powerline ( logger = get_logger ( ) , ext = ' wm ' , renderer_module = ' pango_markup ' , run_once = True ) . render ( )
2013-03-11 19:09:34 +01:00
reload ( common )
2013-03-02 16:05:08 +01:00
2013-02-24 11:03:14 +01:00
old_cwd = None
2014-08-29 20:38:26 +02:00
saved_get_config_paths = None
2013-02-24 11:03:14 +01:00
def setUpModule ( ) :
global old_cwd
2014-08-29 20:38:26 +02:00
global saved_get_config_paths
import powerline
saved_get_config_paths = powerline . get_config_paths
path = os . path . join ( os . path . dirname ( os . path . dirname ( __file__ ) ) , ' powerline ' , ' config_files ' )
powerline . get_config_paths = lambda : [ path ]
2013-02-24 11:03:14 +01:00
old_cwd = os . getcwd ( )
def tearDownModule ( ) :
global old_cwd
2014-08-29 20:38:26 +02:00
global saved_get_config_paths
import powerline
powerline . get_config_paths = saved_get_config_paths
2013-02-24 11:03:14 +01:00
os . chdir ( old_cwd )
old_cwd = None
2013-03-03 17:40:35 +01:00
if __name__ == ' __main__ ' :
from tests import main
main ( )