This repository was archived by the owner on Oct 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbats.py
92 lines (67 loc) · 2.36 KB
/
bats.py
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
import os
import sublime
import sublime_plugin
def file_executable(file):
return os.path.exists(file) and os.access(file, os.X_OK)
# See https://stackoverflow.com/a/9877856
def which(cmd):
path = os.environ['PATH']
for p in path.split(os.path.pathsep):
p = os.path.join(p, cmd)
if file_executable(p):
return p
class Bats():
def __init__(self, window):
self.window = window
self.view = window.active_view()
if not self.view:
raise ValueError('view not found')
def run(self, working_dir=None, file=None):
self.window.run_command('exec', {'kill': True})
env = {}
cmd = []
try:
if not working_dir:
working_dir = os.path.dirname(self.view.file_name())
if not working_dir:
raise ValueError('working directory not found')
if not os.path.isdir(working_dir):
raise ValueError('working directory does not exist or is not a valid directory')
if env is None:
env = os.environ.copy()
bats_executable = self.get_bats_executable()
cmd.append(bats_executable)
file = self.view.file_name()
if not file:
raise ValueError('test file \'%s\' not found' % file)
cmd.append(file)
except ValueError as e:
sublime.status_message('Bats: {}'.format(e))
print('Bats: {}'.format(e))
return
except Exception as e:
sublime.status_message('Bats: {}'.format(e))
print('Bats: \'{}\''.format(e))
raise e
self.window.run_command('exec', {
'cmd': cmd,
'env': env,
'quiet': True,
'shell': False,
'word_wrap': False,
'working_dir': working_dir
})
def cancel(self):
self.window.run_command('exec', {'kill': True})
def get_bats_executable(self):
bats_executable = which('bats')
if bats_executable:
return bats_executable
else:
raise ValueError('bats not found')
class BatsTestCommand(sublime_plugin.WindowCommand):
def run(self):
Bats(self.window).run()
class BatsTestCancelCommand(sublime_plugin.WindowCommand):
def run(self):
Bats(self.window).cancel()