forked from conda/conda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_portability.py
78 lines (64 loc) · 3.33 KB
/
test_portability.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
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function, unicode_literals
from conda.core.portability import SHEBANG_REGEX, replace_long_shebang
from conda.models.enums import FileMode
from logging import getLogger
import re
from unittest import TestCase
log = getLogger(__name__)
class ReplaceShebangTests(TestCase):
def test_shebang_regex_matches(self):
shebang = b"#!/simple/shebang"
match = re.match(SHEBANG_REGEX, shebang, re.MULTILINE)
assert match.groups() == (b'#!/simple/shebang',
b'/simple/shebang',
b'')
# two lines
shebang = b"#!/simple/shebang\nsecond line\n"
match = re.match(SHEBANG_REGEX, shebang, re.MULTILINE)
assert match.groups() == (b'#!/simple/shebang',
b'/simple/shebang',
b'')
# with spaces
shebang = b"#!/simple/shebang\nsecond line\n"
match = re.match(SHEBANG_REGEX, shebang, re.MULTILINE)
assert match.groups() == (b'#!/simple/shebang',
b'/simple/shebang',
b'')
# with spaces
shebang = b"#! /simple/shebang\nsecond line\n"
match = re.match(SHEBANG_REGEX, shebang, re.MULTILINE)
assert match.groups() == (b'#! /simple/shebang',
b'/simple/shebang',
b'')
# with escaped spaces and flags
shebang = b"#!/simple/shebang/escaped\\ space --and --flags -x\nsecond line\n"
match = re.match(SHEBANG_REGEX, shebang, re.MULTILINE)
assert match.groups() == (b'#!/simple/shebang/escaped\\ space --and --flags -x',
b'/simple/shebang/escaped\\ space',
b' --and --flags -x')
def test_replace_long_shebang(self):
content_line = b"content line " * 5
# simple shebang no replacement
shebang = b"#!/simple/shebang/escaped\\ space --and --flags -x"
data = b'\n'.join((shebang, content_line, content_line, content_line))
new_data = replace_long_shebang(FileMode.text, data)
assert data == new_data
# long shebang with truncation
# executable name is 'escaped space'
shebang = b"#!/" + b"shebang/" * 20 + b"python" + b" --and --flags -x"
assert len(shebang) > 127
data = b'\n'.join((shebang, content_line, content_line, content_line))
new_data = replace_long_shebang(FileMode.text, data)
new_shebang = b"#!/usr/bin/env python --and --flags -x"
new_expected_data = b'\n'.join((new_shebang, content_line, content_line, content_line))
assert new_expected_data == new_data
# long shebang with truncation
# executable name is 'escaped space'
shebang = b"#!/" + b"shebang/" * 20 + b"escaped\\ space" + b" --and --flags -x"
assert len(shebang) > 127
data = b'\n'.join((shebang, content_line, content_line, content_line))
new_data = replace_long_shebang(FileMode.text, data)
new_shebang = b"#!/usr/bin/env escaped\\ space --and --flags -x"
new_expected_data = b'\n'.join((new_shebang, content_line, content_line, content_line))
assert new_expected_data == new_data