@@ -20,13 +20,10 @@ def pytest_addoption(parser):
2020 )
2121
2222
23- def pytest_collect_file (path , parent ):
23+ def pytest_collect_file (file_path , path , parent ):
2424 config = parent .config
2525 if config .option .black and path .ext == ".py" :
26- if hasattr (BlackItem , "from_parent" ):
27- return BlackItem .from_parent (parent , fspath = path )
28- else :
29- return BlackItem (path , parent )
26+ return BlackFile .from_parent (parent , path = file_path )
3027
3128
3229def pytest_configure (config ):
@@ -42,10 +39,17 @@ def pytest_unconfigure(config):
4239 config .cache .set (HISTKEY , config ._blackmtimes )
4340
4441
45- class BlackItem (pytest .Item , pytest .File ):
46- def __init__ (self , fspath , parent ):
47- super (BlackItem , self ).__init__ (fspath , parent )
48- self ._nodeid += "::BLACK"
42+ class BlackFile (pytest .File ):
43+ def collect (self ):
44+ """ returns a list of children (items and collectors)
45+ for this collection node.
46+ """
47+ yield BlackItem .from_parent (self , name = "black" )
48+
49+
50+ class BlackItem (pytest .Item ):
51+ def __init__ (self , ** kwargs ):
52+ super (BlackItem , self ).__init__ (** kwargs )
4953 self .add_marker ("black" )
5054 try :
5155 with open ("pyproject.toml" ) as toml_file :
@@ -61,16 +65,16 @@ def __init__(self, fspath, parent):
6165 def setup (self ):
6266 pytest .importorskip ("black" )
6367 mtimes = getattr (self .config , "_blackmtimes" , {})
64- self ._blackmtime = self .fspath . mtime ()
65- old = mtimes .get (str (self .fspath ), 0 )
68+ self ._blackmtime = self .path . stat (). st_mtime
69+ old = mtimes .get (str (self .path ), 0 )
6670 if self ._blackmtime == old :
6771 pytest .skip ("file(s) previously passed black format checks" )
6872
6973 if self ._skip_test ():
7074 pytest .skip ("file(s) excluded by pyproject.toml" )
7175
7276 def runtest (self ):
73- cmd = [sys .executable , "-m" , "black" , "--check" , "--diff" , "--quiet" , str (self .fspath )]
77+ cmd = [sys .executable , "-m" , "black" , "--check" , "--diff" , "--quiet" , str (self .path )]
7478 try :
7579 subprocess .run (
7680 cmd , check = True , stdout = subprocess .PIPE , universal_newlines = True
@@ -79,40 +83,34 @@ def runtest(self):
7983 raise BlackError (e )
8084
8185 mtimes = getattr (self .config , "_blackmtimes" , {})
82- mtimes [str (self .fspath )] = self ._blackmtime
86+ mtimes [str (self .path )] = self ._blackmtime
8387
8488 def repr_failure (self , excinfo ):
8589 if excinfo .errisinstance (BlackError ):
8690 return excinfo .value .args [0 ].stdout
8791 return super (BlackItem , self ).repr_failure (excinfo )
8892
8993 def reportinfo (self ):
90- return (self .fspath , - 1 , "Black format check" )
94+ return (self .path , - 1 , "Black format check" )
9195
9296 def _skip_test (self ):
9397 return self ._excluded () or (not self ._included ())
9498
9599 def _included (self ):
96100 if "include" not in self .pyproject :
97101 return True
98- return re .search (self .pyproject ["include" ], str (self .fspath ))
102+ return re .search (self .pyproject ["include" ], str (self .path ))
99103
100104 def _excluded (self ):
101105 if "exclude" not in self .pyproject :
102106 return False
103- return re .search (self .pyproject ["exclude" ], str (self .fspath ))
107+ return re .search (self .pyproject ["exclude" ], str (self .path ))
104108
105109 def _re_fix_verbose (self , regex ):
106110 if "\n " in regex :
107111 regex = "(?x)" + regex
108112 return re .compile (regex )
109113
110- def collect (self ):
111- """ returns a list of children (items and collectors)
112- for this collection node.
113- """
114- return (self ,)
115-
116114
117115class BlackError (Exception ):
118116 pass
0 commit comments