Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aws
GitHub Repository: aws/aws-cli
Path: blob/develop/tests/unit/test_compat.py
1566 views
1
# Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
#
3
# Licensed under the Apache License, Version 2.0 (the "License"). You
4
# may not use this file except in compliance with the License. A copy of
5
# the License is located at
6
#
7
# http://aws.amazon.com/apache2.0/
8
#
9
# or in the "license" file accompanying this file. This file is
10
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11
# ANY KIND, either express or implied. See the License for the specific
12
# language governing permissions and limitations under the License.
13
import os
14
import signal
15
16
import pytest
17
18
from awscli.compat import ensure_text_type
19
from awscli.compat import compat_shell_quote
20
from awscli.compat import compat_open
21
from awscli.compat import get_popen_kwargs_for_pager_cmd
22
from awscli.compat import ignore_user_entered_signals
23
from awscli.testutils import mock, unittest, skip_if_windows, FileCreator
24
25
26
class TestEnsureText(unittest.TestCase):
27
def test_string(self):
28
value = 'foo'
29
response = ensure_text_type(value)
30
self.assertIsInstance(response, str)
31
self.assertEqual(response, 'foo')
32
33
def test_binary(self):
34
value = b'bar'
35
response = ensure_text_type(value)
36
self.assertIsInstance(response, str)
37
self.assertEqual(response, 'bar')
38
39
def test_unicode(self):
40
value = u'baz'
41
response = ensure_text_type(value)
42
self.assertIsInstance(response, str)
43
self.assertEqual(response, 'baz')
44
45
def test_non_ascii(self):
46
value = b'\xe2\x9c\x93'
47
response = ensure_text_type(value)
48
self.assertIsInstance(response, str)
49
self.assertEqual(response, u'\u2713')
50
51
def test_non_string_or_bytes_raises_error(self):
52
value = 500
53
with self.assertRaises(ValueError):
54
ensure_text_type(value)
55
56
57
@pytest.mark.parametrize(
58
"input_string, expected_output",
59
(
60
('', '""'),
61
('"', '\\"'),
62
('\\', '\\'),
63
('\\a', '\\a'),
64
('\\\\', '\\\\'),
65
('\\"', '\\\\\\"'),
66
('\\\\"', '\\\\\\\\\\"'),
67
('foo bar', '"foo bar"'),
68
('foo\tbar', '"foo\tbar"'),
69
)
70
)
71
def test_compat_shell_quote_windows(input_string, expected_output):
72
assert compat_shell_quote(input_string, "win32") == expected_output
73
74
75
@pytest.mark.parametrize(
76
"input_string, expected_output",
77
(
78
('', "''"),
79
('*', "'*'"),
80
('foo', 'foo'),
81
('foo bar', "'foo bar'"),
82
('foo\tbar', "'foo\tbar'"),
83
('foo\nbar', "'foo\nbar'"),
84
("foo'bar", '\'foo\'"\'"\'bar\'')
85
)
86
)
87
def test_comat_shell_quote_linux(input_string, expected_output):
88
assert compat_shell_quote(input_string, "linux2") == expected_output
89
90
91
@pytest.mark.parametrize(
92
"input_string, expected_output",
93
(
94
('', "''"),
95
('*', "'*'"),
96
('foo', 'foo'),
97
('foo bar', "'foo bar'"),
98
('foo\tbar', "'foo\tbar'"),
99
('foo\nbar', "'foo\nbar'"),
100
("foo'bar", '\'foo\'"\'"\'bar\'')
101
)
102
)
103
def test_comat_shell_quote_darwin(input_string, expected_output):
104
assert compat_shell_quote(input_string, "darwin") == expected_output
105
106
107
class TestGetPopenPagerCmd(unittest.TestCase):
108
@mock.patch('awscli.compat.is_windows', True)
109
@mock.patch('awscli.compat.default_pager', 'more')
110
def test_windows(self):
111
kwargs = get_popen_kwargs_for_pager_cmd()
112
self.assertEqual({'args': 'more', 'shell': True}, kwargs)
113
114
@mock.patch('awscli.compat.is_windows', True)
115
@mock.patch('awscli.compat.default_pager', 'more')
116
def test_windows_with_specific_pager(self):
117
kwargs = get_popen_kwargs_for_pager_cmd('less -R')
118
self.assertEqual({'args': 'less -R', 'shell': True}, kwargs)
119
120
@mock.patch('awscli.compat.is_windows', False)
121
@mock.patch('awscli.compat.default_pager', 'less -R')
122
def test_non_windows(self):
123
kwargs = get_popen_kwargs_for_pager_cmd()
124
self.assertEqual({'args': ['less', '-R']}, kwargs)
125
126
@mock.patch('awscli.compat.is_windows', False)
127
@mock.patch('awscli.compat.default_pager', 'less -R')
128
def test_non_windows_specific_pager(self):
129
kwargs = get_popen_kwargs_for_pager_cmd('more')
130
self.assertEqual({'args': ['more']}, kwargs)
131
132
133
class TestIgnoreUserSignals(unittest.TestCase):
134
@skip_if_windows("These signals are not supported for windows")
135
def test_ignore_signal_sigint(self):
136
with ignore_user_entered_signals():
137
try:
138
os.kill(os.getpid(), signal.SIGINT)
139
except KeyboardInterrupt:
140
self.fail('The ignore_user_entered_signals context '
141
'manager should have ignored')
142
143
@skip_if_windows("These signals are not supported for windows")
144
def test_ignore_signal_sigquit(self):
145
with ignore_user_entered_signals():
146
self.assertEqual(signal.getsignal(signal.SIGQUIT), signal.SIG_IGN)
147
os.kill(os.getpid(), signal.SIGQUIT)
148
149
@skip_if_windows("These signals are not supported for windows")
150
def test_ignore_signal_sigtstp(self):
151
with ignore_user_entered_signals():
152
self.assertEqual(signal.getsignal(signal.SIGTSTP), signal.SIG_IGN)
153
os.kill(os.getpid(), signal.SIGTSTP)
154
155
156
class TestCompatOpenWithAccessPermissions(unittest.TestCase):
157
def setUp(self):
158
self.files = FileCreator()
159
160
def tearDown(self):
161
self.files.remove_all()
162
163
@skip_if_windows('Permissions tests only supported on mac/linux')
164
def test_can_create_file_with_acess_permissions(self):
165
file_path = os.path.join(self.files.rootdir, "foo_600.txt")
166
with compat_open(file_path, access_permissions=0o600, mode='w') as f:
167
f.write('bar')
168
self.assertEqual(os.stat(file_path).st_mode & 0o777, 0o600)
169
170
def test_not_override_existing_file_access_permissions(self):
171
file_path = os.path.join(self.files.rootdir, "foo.txt")
172
with open(file_path, mode='w') as f:
173
f.write('bar')
174
expected_st_mode = os.stat(file_path).st_mode
175
176
with compat_open(file_path, access_permissions=0o600, mode='w') as f:
177
f.write('bar')
178
self.assertEqual(os.stat(file_path).st_mode, expected_st_mode)
179
180