~eda-qa/mortoray.com/trunk

« back to all changes in this revision

Viewing changes to src/shelljob/test/test_namedtempfile.py

  • Committer: edA-qa mort-ora-y
  • Date: 2014-05-01 02:16:13 UTC
  • Revision ID: eda-qa@disemia.com-20140501021613-3mmhtx06rxc4bxk2
roughly put tests in py.text

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
import os
2
2
from shelljob import fs, proc
3
3
 
4
 
with fs.NamedTempFile() as nm:
5
 
        name = nm
6
 
        f = open(nm,"w")
7
 
        f.write( "Hello" )
8
 
        f.close()
9
 
        print( nm )
10
 
        
11
 
        f = open(nm,"r")
12
 
        assert f.read() == "Hello"
13
 
        
14
 
assert not os.path.exists(name)
15
 
 
16
 
with fs.NamedTempFile( prefix = 'PRE', suffix = '.END' ) as nm:
17
 
        assert nm.find( 'PRE' ) > 0 # can't be first due to directory
18
 
        assert nm.endswith( '.END' )
19
 
        name = nm
20
 
assert not os.path.exists(name)
21
 
 
22
 
# ensure deleted on exception
23
 
try:
24
 
        with fs.NamedTempFile as nm:
25
 
                name = nm
26
 
                raise Exception("oops")
27
 
except Exception, e:
28
 
        assert not os.path.exists(name)
29
 
 
30
 
# Example from docs
31
 
with fs.NamedTempFile() as nm:
32
 
        proc.call( "curl http://mortoray.com/ -o {}".format( nm ) )
33
 
        html = open(nm,'r').read()
34
 
        print( len(html) )
35
 
        
36
 
        name = nm
37
 
assert not os.path.exists(name)
 
4
def test_basic():
 
5
        with fs.NamedTempFile() as nm:
 
6
                name = nm
 
7
                f = open(nm,"w")
 
8
                f.write( "Hello" )
 
9
                f.close()
 
10
                print( nm )
 
11
                
 
12
                f = open(nm,"r")
 
13
                assert f.read() == "Hello"
 
14
                
 
15
        assert not os.path.exists(name)
 
16
 
 
17
def test_affix():
 
18
        with fs.NamedTempFile( prefix = 'PRE', suffix = '.END' ) as nm:
 
19
                assert nm.find( 'PRE' ) > 0 # can't be first due to directory
 
20
                assert nm.endswith( '.END' )
 
21
                name = nm
 
22
        assert not os.path.exists(name)
 
23
 
 
24
def test_exception():
 
25
        # ensure deleted on exception
 
26
        try:
 
27
                with fs.NamedTempFile() as nm:
 
28
                        name = nm
 
29
                        raise Exception("oops")
 
30
        except Exception, e:
 
31
                assert not os.path.exists(name)
 
32
 
 
33
def test_example():
 
34
        # Example from docs
 
35
        with fs.NamedTempFile() as nm:
 
36
                proc.call( "curl http://mortoray.com/ -o {}".format( nm ) )
 
37
                html = open(nm,'r').read()
 
38
                print( len(html) )
 
39
                
 
40
                name = nm
 
41
        assert not os.path.exists(name)