| 1 |
# -*- coding: UTF-8 -*- |
| 2 |
""" |
| 3 |
|
| 4 |
Regular expressions for winpath: |
| 5 |
http://regexlib.com/Search.aspx?k=file+name |
| 6 |
|
| 7 |
^(([a-zA-Z]:|\\)\\)?(((\.)|(\.\.)|([^\\/:\*\?"\|<>\. ](([^\\/:\*\?"\|<>\. ])|([^\\/:\*\?"\|<>]*[^\\/:\*\?"\|<>\. ]))?))\\)*[^\\/:\*\?"\|<>\. ](([^\\/:\*\?"\|<>\. ])|([^\\/:\*\?"\|<>]*[^\\/:\*\?"\|<>\. ]))?$ |
| 8 |
|
| 9 |
https://github.com/kgryte/regex-filename-windows/blob/master/lib/index.js |
| 10 |
REGEX: /^([a-zA-Z]:|[\\\/]{2}[^\\\/]+[\\\/]+[^\\\/]+|)([\\\/]|)([\s\S]*?)((?:\.{1,2}|[^\\\/]+?|)(\.[^.\/\\]*|))(?:[\\\/]*)$/ |
| 11 |
Splits a Windows filename. |
| 12 |
Modified from Node.js [source]{@link https://github.com/nodejs/node/blob/1a3b295d0f46b2189bd853800b1e63ab4d106b66/lib/path.js#L65}. |
| 13 |
""" |
| 14 |
|
| 15 |
from __future__ import absolute_import |
| 16 |
from behave4cmd0.command_shell_proc import \ |
| 17 |
BehaveWinCommandOutputProcessor, \ |
| 18 |
TracebackLineNormalizer, ExceptionWithPathNormalizer |
| 19 |
import re |
| 20 |
import pytest |
| 21 |
|
| 22 |
# ----------------------------------------------------------------------------- |
| 23 |
# IMPLEMENTATION |
| 24 |
# ----------------------------------------------------------------------------- |
| 25 |
# winpath_pattern = ur"^($([A-Za-z]:(\[^\]+)*)|((\[^\]+)*)|([^\]+\[^\]+)*)$" |
| 26 |
winpath_pattern = u"^([A-Za-z]:(\\[\w\.\-]+)+)|((\\[\w\.\-]+)*)|(\s[\w\.\-]+([\w\.\-]+)*)$" |
| 27 |
winpath_re = re.compile(winpath_pattern, re.UNICODE) |
| 28 |
|
| 29 |
class PathNormalizer(object): |
| 30 |
def normalize(self, output): |
| 31 |
pattern = u'^.*$' |
| 32 |
def __call__(self, output): |
| 33 |
pass |
| 34 |
|
| 35 |
# ----------------------------------------------------------------------------- |
| 36 |
# TEST CANDIDATES: |
| 37 |
# ----------------------------------------------------------------------------- |
| 38 |
line_processor_configerrors = [ |
| 39 |
ExceptionWithPathNormalizer( |
| 40 |
u"ConfigError: No steps directory in '(?P<path>.*)'", |
| 41 |
"ConfigError: No steps directory in"), |
| 42 |
BehaveWinCommandOutputProcessor.line_processors[1], |
| 43 |
] |
| 44 |
|
| 45 |
line_processor_parsererrors = [ |
| 46 |
ExceptionWithPathNormalizer( |
| 47 |
u'ParserError: Failed to parse "(?P<path>.*)"', |
| 48 |
u'ParserError: Failed to parse'), |
| 49 |
BehaveWinCommandOutputProcessor.line_processors[2], |
| 50 |
] |
| 51 |
|
| 52 |
line_processor_ioerrors = [ |
| 53 |
ExceptionWithPathNormalizer( |
| 54 |
# ur"No such file or directory: '(?P<path>.*)'", |
| 55 |
# u"No such file or directory:"), # IOError |
| 56 |
# ur"Error: \\[Errno 2\\] No such file or directory: '(?P<path>.*)'", |
| 57 |
u"No such file or directory: '(?P<path>.*)'", |
| 58 |
u"[Errno 2] No such file or directory:"), # IOError |
| 59 |
BehaveWinCommandOutputProcessor.line_processors[3], |
| 60 |
] |
| 61 |
|
| 62 |
line_processor_traceback = [ |
| 63 |
ExceptionWithPathNormalizer( |
| 64 |
'^\s*File "(?P<path>.*)", line \d+, in ', |
| 65 |
' File "'), |
| 66 |
BehaveWinCommandOutputProcessor.line_processors[4], |
| 67 |
] |
| 68 |
|
| 69 |
# ----------------------------------------------------------------------------- |
| 70 |
# TEST SUITE |
| 71 |
# ----------------------------------------------------------------------------- |
| 72 |
xfail = pytest.mark.xfail() |
| 73 |
|
| 74 |
class TestWinpathRegex(object): |
| 75 |
|
| 76 |
@pytest.mark.parametrize("winpath", [ |
| 77 |
u'C:\\foo\\bar\\alice.txt', |
| 78 |
u'C:\\foo\\bar', |
| 79 |
u'C:\\alice.txt', |
| 80 |
u'C:\\.verbose', |
| 81 |
u'.\\foo\\bar\\alice.txt', |
| 82 |
u'..\\foo\\..\\bar\\alice.txt', |
| 83 |
u'foo\\bar\\alice.txt', |
| 84 |
u'alice.txt', |
| 85 |
]) |
| 86 |
def test_match__with_valid_winpath(self, winpath): |
| 87 |
mo = winpath_re.match(winpath) |
| 88 |
assert mo is not None |
| 89 |
|
| 90 |
@xfail |
| 91 |
@pytest.mark.parametrize("winpath", [ |
| 92 |
u'2:\\foo\\bar\\alice.txt', |
| 93 |
u'C:\\bar\\alice.txt', |
| 94 |
]) |
| 95 |
def test_match__with_invalid_winpath(self, winpath): |
| 96 |
mo = winpath_re.match(winpath) |
| 97 |
assert mo is None |
| 98 |
|
| 99 |
class TestPathNormalizer(object): |
| 100 |
@pytest.mark.parametrize("output, expected", [ |
| 101 |
(u"ConfigError: No steps directory in 'C:\\one\\two\\three.txt'", |
| 102 |
u"ConfigError: No steps directory in 'C:/one/two/three.txt'"), |
| 103 |
]) |
| 104 |
def test_call__with_pattern1(self, output, expected): |
| 105 |
for line_processor in line_processor_configerrors: |
| 106 |
actual = line_processor(output) |
| 107 |
assert actual == expected |
| 108 |
|
| 109 |
# ParserError: Failed to parse "(?P<path2>.*)" |
| 110 |
@pytest.mark.parametrize("output, expected", [ |
| 111 |
(u'ParserError: Failed to parse "C:\\one\\two\\three.txt"', |
| 112 |
u'ParserError: Failed to parse "C:/one/two/three.txt"'), |
| 113 |
]) |
| 114 |
def test_call__with_pattern2(self, output, expected): |
| 115 |
for line_processor in line_processor_parsererrors: |
| 116 |
actual = line_processor(output) |
| 117 |
assert actual == expected |
| 118 |
|
| 119 |
@pytest.mark.parametrize("output, expected", [ |
| 120 |
(u"Error: [Errno 2] No such file or directory: 'C:\\one\\two\\three.txt'", |
| 121 |
u"Error: [Errno 2] No such file or directory: 'C:/one/two/three.txt'"), |
| 122 |
]) |
| 123 |
def test_call__with_pattern3(self, output, expected): |
| 124 |
for index, line_processor in enumerate(line_processor_ioerrors): |
| 125 |
actual = line_processor(output) |
| 126 |
assert actual == expected, "line_processor %s" % index |
| 127 |
|
| 128 |
@pytest.mark.parametrize("output, expected", [ |
| 129 |
(u' File "C:\\one\\two\\three.txt", line 123, in xxx_some_method', |
| 130 |
u' File "C:/one/two/three.txt", line 123, in xxx_some_method'), |
| 131 |
]) |
| 132 |
def test_call__with_pattern4(self, output, expected): |
| 133 |
for index, line_processor in enumerate(line_processor_traceback): |
| 134 |
actual = line_processor(output) |
| 135 |
assert actual == expected, "line_processor %s" % index |