File tree Expand file tree Collapse file tree 1 file changed +56
-0
lines changed Expand file tree Collapse file tree 1 file changed +56
-0
lines changed Original file line number Diff line number Diff line change
1
+ import os
2
+
3
+ import pytest
4
+
5
+
6
+ class MockFixture (object ):
7
+
8
+ def __init__ (self ):
9
+ self ._patches = []
10
+ self .patch = self ._PatchWrapper (self ._patches )
11
+
12
+ def stopall (self ):
13
+ for p in self ._patches :
14
+ p .stop ()
15
+ self ._patches [:] = []
16
+
17
+ class _PatchWrapper (object ):
18
+
19
+ def __init__ (self , patches ):
20
+ self ._patches = patches
21
+
22
+ def object (self , * args , ** kwargs ):
23
+ import mock
24
+ p = mock .patch .object (* args , ** kwargs )
25
+ mocked = p .start ()
26
+ self ._patches .append (p )
27
+ return mocked
28
+
29
+
30
+ @pytest .yield_fixture
31
+ def mock ():
32
+ result = MockFixture ()
33
+ yield result
34
+ result .stopall ()
35
+
36
+
37
+ class UnixFS (object ):
38
+
39
+ @classmethod
40
+ def rm (cls , filename ):
41
+ os .remove (filename )
42
+
43
+ @classmethod
44
+ def ls (cls , path ):
45
+ return os .listdir (path )
46
+
47
+
48
+ def test_fixture (mock , tmpdir ):
49
+ mock .patch .object (os , 'remove' )
50
+ (tmpdir / 'foo.txt' ).ensure ()
51
+ UnixFS .rm (tmpdir / 'foo.txt' )
52
+ os .remove .assert_called_once_with (tmpdir / 'foo.txt' )
53
+ assert os .path .isfile (str (tmpdir / 'foo.txt' ))
54
+ mock .stopall ()
55
+ UnixFS .rm (str (tmpdir / 'foo.txt' ))
56
+ assert not os .path .isfile (str (tmpdir / 'foo.txt' ))
You can’t perform that action at this time.
0 commit comments