Skip to content

Commit 7600965

Browse files
committed
add some tests
1 parent 9ac3854 commit 7600965

File tree

9 files changed

+243
-94
lines changed

9 files changed

+243
-94
lines changed

MANIFEST.in

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
include ohmyvim/config.ini

buildout.cfg

+3
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,7 @@ develop = .
88
recipe = z3c.recipe.scripts
99
eggs =
1010
oh-my-vim
11+
unittest2
12+
coverage
13+
nose
1114
interpreter = python

ohmyvim/config.ini

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[bundles]
2+
bufexplorer=https://github.com/vim-scripts/bufexplorer.zip.git
3+
django=https://github.com/vim-scripts/django.vim.git
4+
gundo=https://github.com/sjl/gundo.vim.git
5+
nerdtree=https://github.com/scrooloose/nerdtree.git
6+
python=https://github.com/vim-scripts/python.vim.git
7+
snipmate=https://github.com/msanders/snipmate.vim.git
8+
syntastic=https://github.com/scrooloose/syntastic.git
9+
vim-bundle-mako=https://github.com/sophacles/vim-bundle-mako.git
10+
vim-trailing-whitespace=https://github.com/bronson/vim-trailing-whitespace.git
11+
12+
[themes]
13+
github-theme=https://github.com/vim-scripts/github-theme.git

ohmyvim/scripts.py

+101-84
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from os.path import isfile
44
from os.path import basename
55
from os.path import expanduser
6+
from ConfigObject import ConfigObject
67
from urllib import urlopen
78
from subprocess import Popen
89
from subprocess import PIPE
@@ -26,42 +27,55 @@
2627

2728
class Manager(object):
2829

29-
runtime = expanduser('~/.vim/bundle')
30-
autoload = expanduser('~/.vim/autoload')
31-
ohmyvim = expanduser('~/.vim/ohmyvim')
32-
3330
dependencies = {
3431
'vim-pathogen': 'https://github.com/tpope/vim-pathogen.git',
3532
'oh-my-vim': 'https://github.com/gawel/oh-my-vim.git',
3633
}
3734

3835
def __init__(self):
36+
self.output = []
37+
38+
self.runtime = expanduser('~/.vim/bundle')
39+
self.autoload = expanduser('~/.vim/autoload')
40+
self.ohmyvim = expanduser('~/.vim/ohmyvim')
41+
3942
for dirname in (self.runtime, self.autoload,
4043
self.ohmyvim, expanduser('~/.vim/swp')):
4144
if not isdir(dirname):
4245
os.makedirs(dirname)
46+
4347
for name, url in self.dependencies.items():
4448
if not isdir(join(self.runtime, name)):
4549
Popen(['git', 'clone', '-q', url,
4650
join(self.runtime, name)]).wait()
51+
4752
if not isfile(join(self.ohmyvim, 'theme.vim')):
4853
with open(join(self.ohmyvim, 'theme.vim'), 'w') as fd:
4954
fd.write('')
55+
5056
if not isfile(join(self.ohmyvim, 'ohmyvim.vim')):
5157
with open(join(self.ohmyvim, 'ohmyvim.vim'), 'w') as fd:
5258
fd.write('source %s\n' % join(self.runtime, 'vim-pathogen',
5359
'autoload', 'pathogen.vim'))
5460
fd.write('call pathogen#runtime_append_all_bundles()\n')
5561
fd.write('source %s\n' % join(self.ohmyvim, 'theme.vim'))
56-
binary = os.path.abspath(sys.argv[0])
62+
63+
kw = dict(ohmyvim=join(self.ohmyvim, 'ohmyvim.vim'),
64+
binary=os.path.abspath(sys.argv[0]))
5765
if not isfile(expanduser('~/.vimrc')):
5866
with open(expanduser('~/.vimrc'), 'w') as fd:
59-
fd.write(VIMRC % locals())
67+
fd.write(VIMRC % kw)
6068
else:
6169
with open(expanduser('~/.vimrc')) as fd:
62-
if binary not in fd.read():
70+
if kw['binary'] not in fd.read():
6371
with open(expanduser('~/.vimrc'), 'a') as fd:
64-
fd.write(VIMRC % locals())
72+
fd.write(VIMRC % kw)
73+
74+
def log(self, value, *args):
75+
if args:
76+
value = value % args
77+
self.output.append(value)
78+
print(value)
6579

6680
def get_plugins(self):
6781
plugins = []
@@ -82,14 +96,18 @@ def search(self, args):
8296
if not terms:
8397
terms = ['language%3AVimL']
8498
terms = '%20'.join(terms)
85-
webbrowser.open_new(("https://github.com/search?"
86-
"langOverride=&repo=&start_value=1&"
87-
"type=Repositories&language=VimL&q=") + terms)
99+
url = ("https://github.com/search?"
100+
"langOverride=&repo=&start_value=1&"
101+
"type=Repositories&language=VimL&q=") + terms
102+
if '__test__' not in os.environ:
103+
webbrowser.open_new(url)
104+
else:
105+
self.log(url)
88106

89107
def list(self, args):
90108
for plugin, dirname, themes in self.get_plugins():
91-
if args.raw:
92-
print plugin
109+
if args.complete:
110+
self.log(plugin)
93111
else:
94112
os.chdir(dirname)
95113
p = Popen(['git', 'remote', '-v'], stdout=PIPE)
@@ -98,9 +116,9 @@ def list(self, args):
98116
remote = remote.split('\t')[1].split(' ')[0]
99117
if args.urls:
100118
if plugin not in self.dependencies:
101-
print remote
119+
self.log(remote)
102120
else:
103-
print '* %s (%s)' % (plugin, remote)
121+
self.log('* %s (%s)', plugin, remote)
104122

105123
def install_url(self, url):
106124
url = url.strip()
@@ -112,94 +130,90 @@ def install_url(self, url):
112130
name = basename(url)[:-4]
113131
dirname = join(self.runtime, name)
114132
if os.path.isdir(dirname):
115-
print '%s already installed. Upgrading...' % name
133+
self.log('%s already installed. Upgrading...', name)
116134
os.chdir(dirname)
117135
Popen(['git', 'pull', '-n']).wait()
118136
else:
119-
print 'Installing bundle %s...' % name
137+
self.log('Installing bundle %s...', name)
120138
Popen(['git', 'clone', '-q', url, dirname]).wait()
121139
if isfile(join(dirname, 'requires.txt')):
122140
with open(join(dirname, 'requires.txt')) as fd:
123141
dependencies = [d for d in fd.readlines()]
124142
else:
125-
print '%s is not a git url' % url
143+
self.log('%s is not a git url', url)
126144
return dirname, dependencies
127145

128146
def install(self, args):
129-
dependencies = set()
130-
for url in args.url:
131-
if url.endswith('requires.txt'):
132-
if isfile(url):
133-
with open(url) as fd:
147+
filename = join(os.path.dirname(__file__), 'config.ini')
148+
config = ConfigObject(filename=filename)
149+
if args.complete:
150+
for name in sorted(config.bundles.keys()):
151+
self.log(name)
152+
for name in sorted(config.themes.keys()):
153+
self.log(name)
154+
else:
155+
dependencies = set()
156+
for url in args.url:
157+
url = config.bundles.get(url, url)
158+
url = config.themes.get(url, url)
159+
if url.endswith('.txt'):
160+
if isfile(url):
161+
with open(url) as fd:
162+
dependencies = [d for d in fd.readlines()]
163+
elif url.startswith('http'):
164+
fd = urlopen(url)
134165
dependencies = [d for d in fd.readlines()]
135-
elif url.startswith('http'):
136-
fd = urlopen(url)
137-
dependencies = [d for d in fd.readlines()]
138-
else:
139-
_, deps = self.install_url(url)
140-
for d in deps:
141-
if d.strip():
142-
dependencies.add(d)
143-
if dependencies:
144-
print 'Processing dependencies...'
145-
for url in dependencies:
146-
self.install_url(url)
166+
else:
167+
_, deps = self.install_url(url)
168+
for d in deps:
169+
if d.strip():
170+
dependencies.add(d)
171+
if dependencies:
172+
self.log('Processing dependencies...')
173+
for url in dependencies:
174+
self.install_url(url)
147175

148176
def upgrade(self, args):
149-
if not args.bundle:
150-
print 'all'
151177
for plugin, dirname, themes in self.get_plugins():
152178
if plugin in args.bundle or 'all' in args.bundle:
153-
print 'Upgrading %s...' % plugin
179+
self.log('Upgrading %s...', plugin)
154180
os.chdir(dirname)
155181
Popen(['git', 'pull', '-n']).wait()
156-
elif args.raw:
157-
print plugin
158182

159183
def remove(self, args):
160-
for plugin, dirname, themes in self.get_plugins():
161-
if not args.bundle:
162-
print plugin
163-
elif plugin in args.bundle:
164-
if plugin in self.dependencies:
165-
print "Don't remove %s!" % plugin
166-
print 'Removing %s...' % plugin
167-
dirname = join(self.runtime, plugin)
168-
if isdir(join(dirname, '.git')):
169-
shutil.rmtree(dirname)
184+
if args.bundle:
185+
for plugin, dirname, themes in self.get_plugins():
186+
if plugin in args.bundle:
187+
if plugin in self.dependencies:
188+
self.log("Don't remove %s!", plugin)
189+
self.log('Removing %s...', plugin)
190+
dirname = join(self.runtime, plugin)
191+
if isdir(join(dirname, '.git')):
192+
shutil.rmtree(dirname)
170193

171194
def theme(self, args):
172195
theme = args.theme
173196
if theme:
174-
if theme.startswith('http'):
175-
theme_dir, _ = self.install_url(theme)
176-
for plugin, dirname, themes in self.get_plugins():
177-
if theme_dir == dirname and len(themes) == 1:
178-
theme = themes[0]
179-
print 'Activate %s theme...' % theme
180-
with open(join(self.ohmyvim, 'theme.vim'), 'w') as fd:
181-
fd.write(':colo %s\n' % theme)
182-
else:
183-
for plugin, dirname, themes in self.get_plugins():
184-
if theme in themes:
185-
print 'Activate %s theme...' % theme
186-
with open(join(self.ohmyvim, 'theme.vim'), 'w') as fd:
187-
fd.write(':colo %s\n' % theme)
188-
return
189-
for plugin, dirname, themes in self.get_plugins():
190-
if isdir(join(dirname, '.git')):
191-
os.chdir(dirname)
192-
p = Popen(['git', 'remote', '-v'], stdout=PIPE)
193-
p.wait()
194-
remote = p.stdout.read().split('\n')[0]
195-
remote = remote.split('\t')[1].split(' ')[0]
196-
if themes:
197-
if args.raw:
198-
for theme in themes:
199-
print theme
200-
else:
201-
print '* %s (%s)' % (plugin, remote)
202-
print '\t- %s' % ', '.join(themes)
197+
for plugin, dirname, themes in self.get_plugins():
198+
if theme in themes:
199+
self.log('Activate %s theme...', theme)
200+
with open(join(self.ohmyvim, 'theme.vim'), 'w') as fd:
201+
fd.write(':colo %s\n' % theme)
202+
else:
203+
for plugin, dirname, themes in self.get_plugins():
204+
if isdir(join(dirname, '.git')):
205+
os.chdir(dirname)
206+
p = Popen(['git', 'remote', '-v'], stdout=PIPE)
207+
p.wait()
208+
remote = p.stdout.read().split('\n')[0]
209+
remote = remote.split('\t')[1].split(' ')[0]
210+
if themes:
211+
if args.complete:
212+
for theme in themes:
213+
self.log(theme)
214+
else:
215+
self.log('* %s (%s)', plugin, remote)
216+
self.log('\t- %s', ', '.join(themes))
203217

204218
def profiles(self, args):
205219
profiles = join(self.runtime, 'oh-my-vim', 'profiles')
@@ -214,9 +228,9 @@ def profiles(self, args):
214228
if line.startswith('"'):
215229
desc += line.strip(' "\n')
216230
if desc:
217-
print '* %s - %s' % (name, desc)
231+
self.log('* %s - %s', name, desc)
218232
else:
219-
print '* %s' % name
233+
self.log('* %s', name)
220234

221235

222236
def main(*args):
@@ -233,16 +247,16 @@ def main(*args):
233247
p.set_defaults(action=manager.search)
234248

235249
p = subparsers.add_parser('list')
236-
p.add_argument('--raw', action='store_true', default=False)
250+
p.add_argument('--complete', action='store_true', default=False)
237251
p.add_argument('-u', '--urls', action='store_true', default=False)
238252
p.set_defaults(action=manager.list)
239253

240254
p = subparsers.add_parser('install', help='install a script or bundle')
255+
p.add_argument('--complete', action='store_true', default=False)
241256
p.add_argument('url', nargs='*', default='')
242257
p.set_defaults(action=manager.install)
243258

244259
p = subparsers.add_parser('upgrade', help='upgrade bundles')
245-
p.add_argument('--raw', action='store_true', default=False)
246260
p.add_argument('bundle', nargs='*', default='')
247261
p.set_defaults(action=manager.upgrade)
248262

@@ -251,7 +265,7 @@ def main(*args):
251265
p.set_defaults(action=manager.remove)
252266

253267
p = subparsers.add_parser('theme', help='list or activate a theme')
254-
p.add_argument('--raw', action='store_true', default=False)
268+
p.add_argument('--complete', action='store_true', default=False)
255269
p.add_argument('theme', nargs='?', default='')
256270
p.set_defaults(action=manager.theme)
257271

@@ -262,4 +276,7 @@ def main(*args):
262276
args = parser.parse_args(args)
263277
else:
264278
args = parser.parse_args()
279+
265280
args.action(args)
281+
282+
return manager.output

0 commit comments

Comments
 (0)