Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
6db47423
Commit
6db47423
authored
Sep 13, 2013
by
Eli Bendersky
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #18945: Add tests for tempfile name collision handling.
Patch by Vlad Shcherbina
parent
8f648e4f
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
65 additions
and
22 deletions
+65
-22
Lib/test/test_tempfile.py
Lib/test/test_tempfile.py
+65
-22
No files found.
Lib/test/test_tempfile.py
View file @
6db47423
...
@@ -7,6 +7,7 @@ import signal
...
@@ -7,6 +7,7 @@ import signal
import
sys
import
sys
import
re
import
re
import
warnings
import
warnings
import
contextlib
import
unittest
import
unittest
from
test
import
support
from
test
import
support
...
@@ -255,6 +256,22 @@ class TestGetCandidateNames(BaseTestCase):
...
@@ -255,6 +256,22 @@ class TestGetCandidateNames(BaseTestCase):
self
.
assertTrue
(
a
is
b
)
self
.
assertTrue
(
a
is
b
)
@
contextlib
.
contextmanager
def
_inside_empty_temp_dir
():
dir
=
tempfile
.
mkdtemp
()
try
:
with
support
.
swap_attr
(
tempfile
,
'tempdir'
,
dir
):
yield
finally
:
support
.
rmtree
(
dir
)
def
_mock_candidate_names
(
*
names
):
return
support
.
swap_attr
(
tempfile
,
'_get_candidate_names'
,
lambda
:
iter
(
names
))
class
TestMkstempInner
(
BaseTestCase
):
class
TestMkstempInner
(
BaseTestCase
):
"""Test the internal function _mkstemp_inner."""
"""Test the internal function _mkstemp_inner."""
...
@@ -372,31 +389,36 @@ class TestMkstempInner(BaseTestCase):
...
@@ -372,31 +389,36 @@ class TestMkstempInner(BaseTestCase):
os
.
lseek
(
f
.
fd
,
0
,
os
.
SEEK_SET
)
os
.
lseek
(
f
.
fd
,
0
,
os
.
SEEK_SET
)
self
.
assertEqual
(
os
.
read
(
f
.
fd
,
20
),
b"blat"
)
self
.
assertEqual
(
os
.
read
(
f
.
fd
,
20
),
b"blat"
)
def
default_mkstemp_inner
(
self
):
return
tempfile
.
_mkstemp_inner
(
tempfile
.
gettempdir
(),
tempfile
.
template
,
''
,
tempfile
.
_bin_openflags
)
def
test_collision_with_existing_file
(
self
):
# _mkstemp_inner tries another name when a file with
# the chosen name already exists
with
_inside_empty_temp_dir
(),
\
_mock_candidate_names
(
'aaa'
,
'aaa'
,
'bbb'
):
(
fd1
,
name1
)
=
self
.
default_mkstemp_inner
()
os
.
close
(
fd1
)
self
.
assertTrue
(
name1
.
endswith
(
'aaa'
))
(
fd2
,
name2
)
=
self
.
default_mkstemp_inner
()
os
.
close
(
fd2
)
self
.
assertTrue
(
name2
.
endswith
(
'bbb'
))
def
test_collision_with_existing_directory
(
self
):
def
test_collision_with_existing_directory
(
self
):
# _mkstemp_inner tries another name when a directory with
# _mkstemp_inner tries another name when a directory with
# the chosen name already exists
# the chosen name already exists
container_dir
=
tempfile
.
mkdtemp
()
with
_inside_empty_temp_dir
(),
\
try
:
_mock_candidate_names
(
'aaa'
,
'aaa'
,
'bbb'
):
def
mock_get_candidate_names
():
dir
=
tempfile
.
mkdtemp
()
return
iter
([
'aaa'
,
'aaa'
,
'bbb'
])
self
.
assertTrue
(
dir
.
endswith
(
'aaa'
))
with
support
.
swap_attr
(
tempfile
,
'_get_candidate_names'
,
(
fd
,
name
)
=
self
.
default_mkstemp_inner
()
mock_get_candidate_names
):
os
.
close
(
fd
)
dir
=
tempfile
.
mkdtemp
(
dir
=
container_dir
)
self
.
assertTrue
(
name
.
endswith
(
'bbb'
))
self
.
assertTrue
(
dir
.
endswith
(
'aaa'
))
flags
=
tempfile
.
_bin_openflags
(
fd
,
name
)
=
tempfile
.
_mkstemp_inner
(
container_dir
,
tempfile
.
template
,
''
,
flags
)
try
:
self
.
assertTrue
(
name
.
endswith
(
'bbb'
))
finally
:
os
.
close
(
fd
)
os
.
unlink
(
name
)
finally
:
support
.
rmtree
(
container_dir
)
class
TestGetTempPrefix
(
BaseTestCase
):
class
TestGetTempPrefix
(
BaseTestCase
):
...
@@ -553,6 +575,27 @@ class TestMkdtemp(BaseTestCase):
...
@@ -553,6 +575,27 @@ class TestMkdtemp(BaseTestCase):
finally
:
finally
:
os
.
rmdir
(
dir
)
os
.
rmdir
(
dir
)
def
test_collision_with_existing_file
(
self
):
# mkdtemp tries another name when a file with
# the chosen name already exists
with
_inside_empty_temp_dir
(),
\
_mock_candidate_names
(
'aaa'
,
'aaa'
,
'bbb'
):
file
=
tempfile
.
NamedTemporaryFile
(
delete
=
False
)
file
.
close
()
self
.
assertTrue
(
file
.
name
.
endswith
(
'aaa'
))
dir
=
tempfile
.
mkdtemp
()
self
.
assertTrue
(
dir
.
endswith
(
'bbb'
))
def
test_collision_with_existing_directory
(
self
):
# mkdtemp tries another name when a directory with
# the chosen name already exists
with
_inside_empty_temp_dir
(),
\
_mock_candidate_names
(
'aaa'
,
'aaa'
,
'bbb'
):
dir1
=
tempfile
.
mkdtemp
()
self
.
assertTrue
(
dir1
.
endswith
(
'aaa'
))
dir2
=
tempfile
.
mkdtemp
()
self
.
assertTrue
(
dir2
.
endswith
(
'bbb'
))
class
TestMktemp
(
BaseTestCase
):
class
TestMktemp
(
BaseTestCase
):
"""Test mktemp()."""
"""Test mktemp()."""
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment