old_contents: """
  Test the management of tulip.__version__
  
  WARNING: version_test() and possibly other routines in this file will
  temporarily replace tulip/commit_hash.txt, if present.  If the test
  fails, your tulip installation could be left with a broken version
  label.  This comment especially matters for out-of-source testing.
  """
  
  import os
  import os.path
  import imp
  
  
  SAMPLE_FILE_TOP = """# DO NOT EDIT!  This file was automatically generated by setup.py of TuLiP
  """
  SAMPLE_COMMIT_HASH = """71aefd0659904ccdb8553e5d5f2436933ea42728
  """
  
  def version_test():
      import tulip
      tul_path = os.path.dirname(tulip.__file__)
      filename = os.path.join(tul_path, "commit_hash.txt")
      if os.path.exists(filename):
          commit_hash_backup = open(filename, "r").read()
      else:
          commit_hash_backup = None
  
      # Release
      with open(filename, "w") as f:
          f.write(SAMPLE_FILE_TOP)
  
      ver = imp.load_module("version", *imp.find_module("version", [tul_path]))
      assert ver.version == \
          '.'.join([str(x) for x in ver.version_info])
  
      # Dev release
      with open(filename, "a") as f:
          f.write(SAMPLE_COMMIT_HASH)
  
      ver = imp.load_module("version", *imp.find_module("version", [tul_path]))
      release_str = '.'.join([str(x) for x in ver.version_info])
      assert ver.version == release_str+".dev0+"+SAMPLE_COMMIT_HASH.strip()
  
      # Unknown dev
      os.remove(filename)
      ver = imp.load_module("version", *imp.find_module("version", [tul_path]))
      assert ver.version == release_str+".dev0+Unknown"
  
      # Restore original, if present
      if commit_hash_backup is not None:
          with open(filename, "w") as f:
              f.write(commit_hash_backup)
  
new_contents: """Test the management of `tulip.__version__`."""
  import imp
  import os
  import os.path
  
  import git
  import mock
  from nose.tools import assert_raises
  from setuptools.version import pkg_resources
  
  import tulip
  import tulip._version
  
  
  def test_tulip_has_pep440_version():
      """Check that `tulip.__version__` complies to PEP440."""
      version = tulip.__version__
      assert version is not None, version
      version_ = tulip._version.version
      assert version == version_, (version, version_)
      assert_pep440(version)
  
  
  @mock.patch('git.Repo')
  def test_git_version(mock_repo):
      """Mock `git` repository for testing `setup.git_version`."""
      path = os.path.realpath(__file__)
      path = os.path.dirname(path)
      path = os.path.dirname(path)  # parent dir
      path = os.path.join(path, 'setup.py')
      setup = imp.load_source('setup', path)
      # mocking
      version = '0.1.2'
      instance = mock_repo.return_value
      instance.head.commit.hexsha = '0123'
      # dirty repo
      v = setup.git_version(version)
      assert_pep440(v)
      assert 'dev' in v, v
      assert 'dirty' in v, v
      # not dirty, not tagged
      instance.is_dirty.return_value = False
      instance.git.describe.side_effect = git.GitCommandError(0, 0)
      v = setup.git_version(version)
      assert_pep440(v)
      assert 'dev' in v, v
      assert 'dirty' not in v, v
      # tagged as version that matches `setup.py`
      instance.git.describe.side_effect = None
      instance.git.describe.return_value = 'v0.1.2'
      v = setup.git_version(version)
      assert_pep440(v)
      assert v == '0.1.2', v
      # tagged as wrong version
      instance.git.describe.return_value = 'v0.1.3'
      with assert_raises(AssertionError):
          setup.git_version(version)
      # release: no repo
      mock_repo.side_effect = Exception('no repo found')
      with assert_raises(Exception):
          setup.git_version(version)
  
  
  def assert_pep440(version):
      """Raise `AssertionError` if `version` violates PEP440."""
      v = pkg_resources.parse_version(version)
      assert isinstance(v, pkg_resources.SetuptoolsVersion), v
  
  
  if __name__ == '__main__':
      test_git_version()
  
subject: rewrite version tests, for `git_version`