title: Unittesting Python code which uses subprocess.Popen
body: <p>I have a Python project in which I read external files, process them, and write the results to a new file. The input files can either be read directly, or extracted from a git repository using <code>git show</code>. The function to call <code>git show</code> and return stdout looks like this:</p>
  
  <pre><code>def git_show(fname, rev):
      '''Runs git show and returns stdout'''
      process = subprocess.Popen(['git', 'show', '{}:{}'.format(rev, fname)],
                                 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
      stdout, stderr = process.communicate()
      ret_code = process.wait()
      if ret_code:
          raise Exception(stderr)
      return stdout
  </code></pre>
  
  <p>I have unittests which test the whole processing part of the program, i.e., everything apart from reading and writing the files. However, I have stumbled upon (and fixed) issues regarding the encoding of the returned string from <code>git_show()</code>, depending Python version, and quite possibly OS and the actual file to read.</p>
  
  <p>I would like to set up a unittest for <code>git_show()</code> so I can make sure the whole application works, from input to output. However, as far as I know, this is not possible without having an actual git repository to test on. The whole package is version managed with git, and I expect that if I have a git repository inside a git repository that might lead to problems on its own, and a voice in my head tells me that might not be the best solution anyway.</p>
  
  <p>How can one best achieve unittesting code which gets input from <code>git show</code> (and in general, the command line / <code>Popen.communicate()</code>)?</p>