Skip to content

Commit 6b1cc6a

Browse files
authored
Normalize paths to avoid case-sensitivity on Windows (#451)
* Normalize paths to avoid case-sensitivity on Windows Signed-off-by: sschulz92 <bastie92_spam@gmx.de> * Adjust formatting to retrigger tests Signed-off-by: sschulz92 <bastie92_spam@gmx.de> * Increase plugin version Signed-off-by: sschulz92 <bastie92_spam@gmx.de> --------- Signed-off-by: sschulz92 <bastie92_spam@gmx.de>
1 parent 6d08d84 commit 6b1cc6a

File tree

3 files changed

+50
-9
lines changed

3 files changed

+50
-9
lines changed

getgauge/registry.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -171,34 +171,37 @@ def is_continue_on_failure(self, func, exception):
171171
def get_step_positions(self, file_name):
172172
positions = []
173173
for step, infos in self.__steps_map.items():
174-
positions = positions + [{'stepValue': step, 'span': i.span}
175-
for i in infos if i.file_name == file_name]
174+
positions.extend(
175+
[{'stepValue': step, 'span': i.span} for i in infos if paths_equal(i.file_name, file_name)]
176+
)
176177
return positions
177178

178179
def _get_all_hooks(self, file_name):
179180
all_hooks = []
180181
for hook in self.hooks:
181-
all_hooks = all_hooks + \
182-
[h for h in getattr(self, "__{}".format(hook))
183-
if h.file_name == file_name]
182+
all_hooks.extend(
183+
[h for h in getattr(self, "__{}".format(hook)) if paths_equal(h.file_name, file_name)]
184+
)
184185
return all_hooks
185186

186187
def get_all_methods_in(self, file_name):
187188
methods = []
188189
for _, infos in self.__steps_map.items():
189-
methods = methods + [i for i in infos if i.file_name == file_name]
190+
methods.extend(
191+
[i for i in infos if paths_equal(i.file_name, file_name)]
192+
)
190193
return methods + self._get_all_hooks(file_name)
191194

192195
def is_file_cached(self, file_name):
193196
for _, infos in self.__steps_map.items():
194-
if any(i.file_name == file_name for i in infos):
197+
if any(paths_equal(i.file_name, file_name) for i in infos):
195198
return True
196199
return False
197200

198201
def remove_steps(self, file_name):
199202
new_map = {}
200203
for step, infos in self.__steps_map.items():
201-
filtered_info = [i for i in infos if i.file_name != file_name]
204+
filtered_info = [i for i in infos if not paths_equal(i.file_name, file_name)]
202205
if len(filtered_info) > 0:
203206
new_map[step] = filtered_info
204207
self.__steps_map = new_map
@@ -209,6 +212,11 @@ def clear(self):
209212
setattr(self, '__{}'.format(hook), [])
210213

211214

215+
def paths_equal(first_file_path, second_file_path):
216+
""" Normalize paths in order to compare them. """
217+
return os.path.normcase(str(first_file_path)) == os.path.normcase(str(second_file_path))
218+
219+
212220
def _filter_hooks(tags, hooks):
213221
filtered_hooks = []
214222
for hook in hooks:

python.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"id": "python",
3-
"version": "0.4.11",
3+
"version": "0.4.12",
44
"description": "Python support for gauge",
55
"run": {
66
"windows": [

tests/test_registry.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import re
2+
import sys
23
import unittest
34

45
from getgauge.registry import Registry
@@ -361,6 +362,38 @@ def test_Registry_get_all_methods_in_should_give_all_the_methods_define_in_that_
361362
self.assertEqual(3, len(registry.get_all_methods_in("foo.py")))
362363
self.assertEqual(2, len(registry.get_all_methods_in("bar.py")))
363364

365+
@unittest.skipIf(not sys.platform.startswith("win"), "Test is designed to cover Windows like paths")
366+
def test_Registry_get_all_methods_in_should_handle_paths_case_sensitive(self):
367+
lower_c_drive = 'c:/random/path/foo.py'
368+
upper_c_drive = 'C:/random/path/foo.py'
369+
370+
step_infos = [
371+
{'text': 'Foo', 'func': 'func1', 'file_name': lower_c_drive},
372+
{'text': 'Foo <>', 'func': 'func2', 'file_name': upper_c_drive}
373+
]
374+
for info in step_infos:
375+
registry.add_step(info['text'], info['func'], info['file_name'])
376+
377+
""" Note: we should find both steps regardless the different spelling as the path is in fact equal! """
378+
self.assertEqual(2, len(registry.get_all_methods_in(lower_c_drive)))
379+
self.assertEqual(2, len(registry.get_all_methods_in(upper_c_drive)))
380+
381+
@unittest.skipIf(sys.platform.startswith("win"), "Fails on Windows due to case sensitivity")
382+
def test_Registry_get_all_methods_in_should_handle_paths_case_sensitive_on_mac(self):
383+
path1 = '/random/path/foo.py'
384+
path2 = '/random/PATH/foo.py'
385+
386+
step_infos = [
387+
{'text': 'Foo', 'func': 'func1', 'file_name': path1},
388+
{'text': 'Foo <>', 'func': 'func2', 'file_name': path2}
389+
]
390+
for info in step_infos:
391+
registry.add_step(info['text'], info['func'], info['file_name'])
392+
393+
""" Note: since the paths are in fact different, they should be treated as different paths! """
394+
self.assertEqual(1, len(registry.get_all_methods_in(path1)))
395+
self.assertEqual(1, len(registry.get_all_methods_in(path2)))
396+
364397
def tearDown(self):
365398
global registry
366399
registry = Registry()

0 commit comments

Comments
 (0)