MCPcopy Index your code
hub / github.com/pyinvoke/invoke / MockSubprocess

Class MockSubprocess

tests/_util.py:110–147  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

108
109
110class MockSubprocess:
111 def __init__(self, out="", err="", exit=0, isatty=None, autostart=True):
112 self.out_file = BytesIO(out.encode())
113 self.err_file = BytesIO(err.encode())
114 self.exit = exit
115 self.isatty = isatty
116 if autostart:
117 self.start()
118
119 def start(self):
120 # Start patchin'
121 self.popen = patch("invoke.runners.Popen")
122 Popen = self.popen.start()
123 self.read = patch("os.read")
124 read = self.read.start()
125 self.sys_stdin = patch("sys.stdin", new_callable=BytesIO)
126 sys_stdin = self.sys_stdin.start()
127 # Setup mocks
128 process = Popen.return_value
129 process.returncode = self.exit
130 process.stdout.fileno.return_value = 1
131 process.stderr.fileno.return_value = 2
132 # If requested, mock isatty to fake out pty detection
133 if self.isatty is not None:
134 sys_stdin.isatty = Mock(return_value=self.isatty)
135
136 def fakeread(fileno, count):
137 fd = {1: self.out_file, 2: self.err_file}[fileno]
138 return fd.read(count)
139
140 read.side_effect = fakeread
141 # Return the Popen mock as it's sometimes wanted inside tests
142 return Popen
143
144 def stop(self):
145 self.popen.stop()
146 self.read.stop()
147 self.sys_stdin.stop()
148
149
150def mock_subprocess(out="", err="", exit=0, isatty=None, insert_Popen=False):

Callers 1

wrapperFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…