Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(38999)

Side by Side Diff: Lib/test/test_commands.py

Issue 5178: Add context manager for temporary directory
Patch Set: Created 8 years, 11 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Lib/test/test_bsddb3.py ('k') | Lib/test/test_shelve.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 ''' 1 '''
2 Tests for commands module 2 Tests for commands module
3 Nick Mathewson 3 Nick Mathewson
4 ''' 4 '''
5 import unittest 5 import unittest
6 import os, tempfile, re 6 import os, tempfile, re
7 import warnings 7 import warnings
8 8
9 warnings.filterwarnings('ignore', r".*commands.getstatus.. is deprecated", 9 warnings.filterwarnings('ignore', r".*commands.getstatus.. is deprecated",
10 DeprecationWarning) 10 DeprecationWarning)
(...skipping 10 matching lines...) Expand all
21 if os.name != 'posix': 21 if os.name != 'posix':
22 raise TestSkipped('Not posix; skipping test_commands') 22 raise TestSkipped('Not posix; skipping test_commands')
23 23
24 24
25 class CommandTests(unittest.TestCase): 25 class CommandTests(unittest.TestCase):
26 26
27 def test_getoutput(self): 27 def test_getoutput(self):
28 self.assertEquals(getoutput('echo xyzzy'), 'xyzzy') 28 self.assertEquals(getoutput('echo xyzzy'), 'xyzzy')
29 self.assertEquals(getstatusoutput('echo xyzzy'), (0, 'xyzzy')) 29 self.assertEquals(getstatusoutput('echo xyzzy'), (0, 'xyzzy'))
30 30
31 # we use mkdtemp in the next line to create an empty directory 31 # we make an empty directory under our exclusive control;
32 # under our exclusive control; from that, we can invent a pathname 32 # from that, we can invent a pathname that we _know_ won't
33 # that we _know_ won't exist. This is guaranteed to fail. 33 # exist. This is guaranteed to fail.
34 dir = None 34 with tempfile.TemporaryDirectory() as tmpdir:
35 try: 35 name = os.path.join(tmpdir, "foo")
36 dir = tempfile.mkdtemp()
37 name = os.path.join(dir, "foo")
38
39 status, output = getstatusoutput('cat ' + name) 36 status, output = getstatusoutput('cat ' + name)
40 self.assertNotEquals(status, 0) 37 self.assertNotEquals(status, 0)
41 finally:
42 if dir is not None:
43 os.rmdir(dir)
44 38
45 def test_getstatus(self): 39 def test_getstatus(self):
46 # This pattern should match 'ls -ld /.' on any posix 40 # This pattern should match 'ls -ld /.' on any posix
47 # system, however perversely configured. Even on systems 41 # system, however perversely configured. Even on systems
48 # (e.g., Cygwin) where user and group names can have spaces: 42 # (e.g., Cygwin) where user and group names can have spaces:
49 # drwxr-xr-x 15 Administ Domain U 4096 Aug 12 12:50 / 43 # drwxr-xr-x 15 Administ Domain U 4096 Aug 12 12:50 /
50 # drwxr-xr-x 15 Joe User My Group 4096 Aug 12 12:50 / 44 # drwxr-xr-x 15 Joe User My Group 4096 Aug 12 12:50 /
51 # Note that the first case above has a space in the group name 45 # Note that the first case above has a space in the group name
52 # while the second one has a space in both names. 46 # while the second one has a space in both names.
53 pat = r'''d......... # It is a directory. 47 pat = r'''d......... # It is a directory.
54 \+? # It may have ACLs. 48 \+? # It may have ACLs.
55 \s+\d+ # It has some number of links. 49 \s+\d+ # It has some number of links.
56 [^/]* # Skip user, group, size, and date. 50 [^/]* # Skip user, group, size, and date.
57 /\. # and end with the name of the file. 51 /\. # and end with the name of the file.
58 ''' 52 '''
59 53
60 self.assert_(re.match(pat, getstatus("/."), re.VERBOSE)) 54 self.assert_(re.match(pat, getstatus("/."), re.VERBOSE))
61 55
62 56
63 def test_main(): 57 def test_main():
64 run_unittest(CommandTests) 58 run_unittest(CommandTests)
65 reap_children() 59 reap_children()
66 60
67 61
68 if __name__ == "__main__": 62 if __name__ == "__main__":
69 test_main() 63 test_main()
OLDNEW
« no previous file with comments | « Lib/test/test_bsddb3.py ('k') | Lib/test/test_shelve.py » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+