-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcucumber_scenario_finder.py
86 lines (71 loc) · 2.93 KB
/
cucumber_scenario_finder.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
# -*- coding: utf-8 -*-
import sublime, sublime_plugin
import os
import re
import codecs
class CucumberBaseCommand(sublime_plugin.WindowCommand, object):
def __init__(self, window):
sublime_plugin.WindowCommand.__init__(self, window)
self.load_settings()
def load_settings(self):
self.settings = sublime.load_settings("CucumberScenarioFinder.sublime-settings")
self.features_path = self.settings.get('cucumber_features_path') # Default is "features"
self.scenario_pattern = self.settings.get('cucumber_scenario_pattern') # Default is '.*_steps.*\.rb'
def find_scenario_in_file(self, root, f_name):
pattern = re.compile(r'(.*Scenario:.*)')
step_file_path = os.path.join(root, f_name)
with codecs.open(step_file_path, encoding='utf-8') as f:
index = 0
for line in f:
match = re.match(pattern, line)
if match:
self.scenarios.append((match.group().strip(), index, step_file_path))
index += 1
def find_all_scenarios(self, file_name=None):
self.scenarios = []
folders = self.window.folders()
if file_name == None:
for folder in folders:
for path in os.listdir(folder) + ['.']:
full_path = os.path.join(folder, path)
if path == self.features_path:
self.step_files = []
for root, dirs, files in os.walk(full_path, followlinks=True):
for f_name in files:
if re.match(self.scenario_pattern, f_name):
self.step_files.append((f_name, os.path.join(root, f_name)))
self.find_scenario_in_file(root, f_name)
else:
root = os.path.join(file_name)
self.find_scenario_in_file(root, file_name)
def scenario_found(self, index):
if index >= 0:
file_path = self.scenarios[index][2]
view = self.window.open_file(file_path)
self.active_ref = (view, self.scenarios[index][1])
self.mark_scenario()
def mark_scenario(self):
view = self.window.active_view()
if view.is_loading():
sublime.set_timeout(self.mark_scenario, 50)
else:
view.run_command("goto_line", {"line": self.active_ref[1]+1} )
class CucumberScenarioFinderCommand(CucumberBaseCommand):
def __init__(self, window):
CucumberBaseCommand.__init__(self, window)
def run(self, file_name=None):
self.list_scenarios()
def list_scenarios(self):
self.find_all_scenarios()
scenarios_only = [x[0] for x in self.scenarios]
self.window.show_quick_panel(scenarios_only, self.scenario_found)
class CucumberScenarioFinderLocalCommand(CucumberBaseCommand):
def __init__(self, window):
CucumberBaseCommand.__init__(self, window)
def run(self, file_name=None):
self.list_scenarios()
def list_scenarios(self):
view = self.window.active_view()
self.find_all_scenarios(view.file_name())
scenarios_only = [x[0] for x in self.scenarios]
self.window.show_quick_panel(scenarios_only, self.scenario_found)