forked from adamwathan/sublime-phpunit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsublime-phpunit.py
More file actions
139 lines (102 loc) · 5.23 KB
/
sublime-phpunit.py
File metadata and controls
139 lines (102 loc) · 5.23 KB
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
import os
import sys
import shlex
import ntpath
import subprocess
import sublime
import sublime_plugin
class PhpunitTestCommand(sublime_plugin.WindowCommand):
def get_paths(self):
file_name = self.window.active_view().file_name()
phpunit_config_path = self.find_phpunit_config(file_name)
directory = os.path.dirname(os.path.realpath(file_name))
file_name = file_name.replace(' ', '\ ')
phpunit_config_path = phpunit_config_path.replace(' ', '\ ')
phpunit_bin = self.find_phpunit_bin(phpunit_config_path)
active_view = self.window.active_view()
return file_name, phpunit_config_path, phpunit_bin, active_view, directory
def get_current_function(self, view):
sel = view.sel()[0]
function_regions = view.find_by_selector('entity.name.function')
cf = None
for r in reversed(function_regions):
if r.a < sel.a:
cf = view.substr(r)
break
return cf
def find_phpunit_config(self, file_name):
phpunit_config_path = file_name
found = False
while found == False:
phpunit_config_path = os.path.abspath(os.path.join(phpunit_config_path, os.pardir))
found = os.path.isfile(phpunit_config_path + '/phpunit.xml') or phpunit_config_path == '/'
return phpunit_config_path
def find_phpunit_bin(self, directory):
search_paths = [
'vendor/bin/phpunit',
'vendor/bin/phpunit/phpunit/phpunit',
]
found = False;
for path in search_paths:
if False == found:
binpath = os.path.realpath(directory + "/" + path)
if os.path.isfile(binpath):
found = True
if False == found:
binpath = 'phpunit'
return binpath
def run_in_terminal(self, command):
settings = sublime.load_settings("Preferences.sublime-settings")
terminal_setting = settings.get('phpunit-sublime-terminal', 'Terminal')
osascript_command = 'osascript '
if terminal_setting == 'iTerm':
osascript_command += '"' + os.path.dirname(os.path.realpath(__file__)) + '/open_iterm.applescript"'
osascript_command += ' "' + command + '"'
else:
osascript_command += '"' + os.path.dirname(os.path.realpath(__file__)) + '/run_command.applescript"'
osascript_command += ' "' + command + '"'
osascript_command += ' "PHPUnit Tests"'
os.system(osascript_command)
class RunPhpunitTestCommand(PhpunitTestCommand):
def run(self, *args, **kwargs):
file_name, phpunit_config_path, phpunit_bin, active_view, directory = self.get_paths()
self.run_in_terminal('cd ' + phpunit_config_path + ' && ' + phpunit_bin + ' ' + file_name)
class RunAllPhpunitTestsCommand(PhpunitTestCommand):
def run(self, *args, **kwargs):
file_name, phpunit_config_path, phpunit_bin, active_view, directory = self.get_paths()
self.run_in_terminal('cd ' + phpunit_config_path + ' && ' + phpunit_bin)
class RunSinglePhpunitTestCommand(PhpunitTestCommand):
def run(self, *args, **kwargs):
file_name, phpunit_config_path, phpunit_bin, active_view, directory = self.get_paths()
current_function = self.get_current_function(active_view)
self.run_in_terminal('cd ' + phpunit_config_path + ' && phpunit ' + file_name + " --filter '/::" + current_function + "$/'")
class RunPhpunitTestsInDirCommand(PhpunitTestCommand):
def run(self, *args, **kwargs):
file_name, phpunit_config_path, phpunit_bin, active_view, directory = self.get_paths()
self.run_in_terminal('cd ' + phpunit_config_path + ' && ' + phpunit_bin + ' ' + directory)
class FindMatchingTestCommand(sublime_plugin.WindowCommand):
def path_leaf(self, path):
head, tail = ntpath.split(path)
return tail or ntpath.basename(head)
def run(self, *args, **kwargs):
file_name = self.window.active_view().file_name()
file_name = self.path_leaf(file_name)
file_name = file_name[0:file_name.find('.')]
tab_target = 0
if 'Test' not in file_name:
file_name = file_name + 'Test'
else:
# Strip 'Test' and add '.' to force matching the non-test file
file_name = file_name[0:file_name.find('Test')] + '.'
tab_target = 1
# Big dirty macro-ish hack. Eventually I should just open the file in some sort of
# logical way.
self.window.run_command("set_layout", {"cells": [[0, 0, 1, 1], [1, 0, 2, 1]], "cols": [0.0, 0.5, 1.0], "rows": [0.0, 1.0]})
self.window.run_command("focus_group", {"group": tab_target})
self.window.run_command("show_overlay", {"overlay": "goto", "text": file_name, "show_files": "true"})
self.window.run_command("move", {"by": "lines", "forward": False})
# This is a dirty hack to get it to switch files... Can't simulate 'Enter'
# but triggering the overlay again to close it seems to have the same effect.
self.window.run_command("show_overlay", {"overlay": "goto", "show_files": "true"})
self.window.run_command("focus_group", {"group": 0})
self.window.run_command("focus_group", {"group": tab_target})