0

I'm writing a module that integrates one system with git. I'm writing tests and want to have test repository inside tests directory, so I could run unittests on it.

Module and project structure looks like this:

myproject/
  mymodule/
    some_dir/
    tests/
      __init__.py
      testrepo/
        /.git
      test_some.py
  /.git

Now while I'm developing, it is working. I can run tests using testrepo. Though I noticed, when I committed, git started treating testrepo as subproject automatically. So basically it does not track all changes that occurred. If actual code changes, then it recognizes as subproject/submodule change. But if I let say add new branch, those changes are not recognized (unless I would check out to it etc).

So I'm wondering what could be best way to use testrepo for unittests. I want it to be in source control, so whole structure would be intact with unittests.

If understood correctly from how can I add git submodule into git repo as normal directory? it is not really possible (only by somewhat hacking git name back and forth) to treat sub git repository as normal repository.

So how I could preserve all changes in sub repository, so I would need to just pull myproject and get testrepo with all branches etc?

Or I can only use it as real submodule and need to initialize it after cloning myproject?

Update

If I use testrepo as real submodule, then my tests stop working, because it is no longer recognized as normal git repo.

Community
  • 1
  • 1
Andrius
  • 19,658
  • 37
  • 143
  • 243

1 Answers1

0

So after trying submodule and real git repo inside repo, I went with using simple repo set up and tear down between tests.

I bet this is not very optimal solution, but for now I do not see any better one (the faster might be to somehow hide git as normal directory and when running tests, change it to be recognized as git repo and after tests, hide it again: though kind of hacky).

If anyone has better ideas, please write it as an answer.

My implementation sample (it is built using setUpClass and removed using tearDownClass):

# -*- coding: utf-8 -*-
import os
import sh
import shutil


class cd:
    """Context manager for changing the current working directory"""
    def __init__(self, newPath):
        self.newPath = os.path.expanduser(newPath)

    def __enter__(self):
        self.savedPath = os.getcwd()
        os.chdir(self.newPath)

    def __exit__(self, etype, value, traceback):
        os.chdir(self.savedPath)


def build_test_repo(dir_path):
    """Build testrepo for unittests."""
    def git_add_file(g, txt):
        """create, add and commit dummy file."""
        with open('%s.txt' % txt, 'w') as f:
            f.write('%s content' % txt)
        # add and commit
        g.add('%s.txt' % txt)
        g.commit('-m', '[ADD] %s.txt' % txt)

    path = "%s/%s" % (dir_path, 'testrepo')
    os.makedirs(path)
    # change dir to initialize repo. We use context manager, so after
    # setting up repo, we would get back to previous working directory.
    with cd(path):
        # git from shell
        g = sh.git
        g.init()
        git_add_file(g, 't0')
        # create f1 branch
        g.checkout('-b', 'f1')
        # add and commit for f1
        git_add_file(g, 't1')
        # create f2 branch from master
        g.checkout('master')
        g.checkout('-b', 'f2')
        git_add_file(g, 't2')
        # create copy of master without any difference.
        g.checkout('master')
        g.checkout('-b', 'master_copy')
Andrius
  • 19,658
  • 37
  • 143
  • 243