debiangittestrepo.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. # vim: set fileencoding=utf-8 :
  2. from .. import context
  3. import os
  4. import unittest
  5. import gbp.deb.git
  6. class DebianGitTestRepo(unittest.TestCase):
  7. """Scratch repo for a single unit test"""
  8. def setUp(self):
  9. self.tmpdir = context.new_tmpdir(__name__)
  10. repodir = self.tmpdir.join('test_repo')
  11. self.repo = gbp.deb.git.DebianGitRepository.create(repodir)
  12. def tearDown(self):
  13. context.teardown()
  14. def add_file(self, name, content=None, msg=None):
  15. """
  16. Add a single file with name I{name} and content I{content}. If
  17. I{content} is C{none} the content of the file is undefined.
  18. @param name: the file's path relativ to the git repo
  19. @type name: C{str}
  20. @param content: the file's content
  21. @type content: C{str}
  22. """
  23. path = os.path.join(self.repo.path, name)
  24. d = os.path.dirname(path)
  25. if not os.path.exists(d):
  26. os.makedirs(d)
  27. with open(path, 'w+') as f:
  28. content == None or f.write(content)
  29. self.repo.add_files(name, force=True)
  30. self.repo.commit_files(path, msg or "added %s" % name)