Show More
@@ -1,48 +1,42 b'' | |||||
1 | import os |
|
1 | import os | |
2 | import glob |
|
2 | import glob | |
|
3 | import unittest | |||
|
4 | import silenttestrunner | |||
|
5 | ||||
3 | from mercurial.util import atomictempfile |
|
6 | from mercurial.util import atomictempfile | |
4 |
|
7 | |||
5 | # basic usage |
|
8 | class testatomictempfile(unittest.TestCase): | |
6 | def test1_simple(): |
|
9 | def test1_simple(self): | |
7 | if os.path.exists('foo'): |
|
10 | if os.path.exists('foo'): | |
8 | os.remove('foo') |
|
11 | os.remove('foo') | |
9 | file = atomictempfile('foo') |
|
12 | file = atomictempfile('foo') | |
10 | (dir, basename) = os.path.split(file._tempname) |
|
13 | (dir, basename) = os.path.split(file._tempname) | |
11 |
assert |
|
14 | self.assertFalse(os.path.isfile('foo')) | |
12 |
assert |
|
15 | self.assertTrue(basename in glob.glob('.foo-*')) | |
13 |
|
16 | |||
14 | file.write('argh\n') |
|
17 | file.write('argh\n') | |
15 | file.close() |
|
18 | file.close() | |
16 |
|
19 | |||
17 |
assert |
|
20 | self.assertTrue(os.path.isfile('foo')) | |
18 |
assert |
|
21 | self.assertTrue(basename not in glob.glob('.foo-*')) | |
19 | print 'OK' |
|
|||
20 |
|
22 | |||
21 | # discard() removes the temp file without making the write permanent |
|
23 | # discard() removes the temp file without making the write permanent | |
22 | def test2_discard(): |
|
24 | def test2_discard(self): | |
23 | if os.path.exists('foo'): |
|
25 | if os.path.exists('foo'): | |
24 | os.remove('foo') |
|
26 | os.remove('foo') | |
25 | file = atomictempfile('foo') |
|
27 | file = atomictempfile('foo') | |
26 | (dir, basename) = os.path.split(file._tempname) |
|
28 | (dir, basename) = os.path.split(file._tempname) | |
27 |
|
||||
28 | file.write('yo\n') |
|
|||
29 | file.discard() |
|
|||
30 |
|
29 | |||
31 | assert not os.path.isfile('foo') |
|
30 | file.write('yo\n') | |
32 | assert basename not in os.listdir('.') |
|
31 | file.discard() | |
33 | print 'OK' |
|
|||
34 |
|
32 | |||
35 | # if a programmer screws up and passes bad args to atomictempfile, they |
|
33 | self.assertFalse(os.path.isfile('foo')) | |
36 | # get a plain ordinary TypeError, not infinite recursion |
|
34 | self.assertTrue(basename not in os.listdir('.')) | |
37 | def test3_oops(): |
|
35 | ||
38 | try: |
|
36 | # if a programmer screws up and passes bad args to atomictempfile, they | |
39 | file = atomictempfile() |
|
37 | # get a plain ordinary TypeError, not infinite recursion | |
40 | except TypeError: |
|
38 | def test3_oops(self): | |
41 | print "OK" |
|
39 | self.assertRaises(TypeError, atomictempfile) | |
42 | else: |
|
|||
43 | print "expected TypeError" |
|
|||
44 |
|
40 | |||
45 | if __name__ == '__main__': |
|
41 | if __name__ == '__main__': | |
46 | test1_simple() |
|
42 | silenttestrunner.main(__name__) | |
47 | test2_discard() |
|
|||
48 | test3_oops() |
|
1 | NO CONTENT: file was removed |
|
NO CONTENT: file was removed |
General Comments 0
You need to be logged in to leave comments.
Login now