mirror of
https://github.com/facebook/sapling.git
synced 2024-10-06 06:47:41 +03:00
add create_git_repo() and create_hg_repo() methods to EdenTestCase
Summary: This slightly refactors the way that EdenTestCase and EdenRepoTest initialize repositories. This removes the `create_repo()` method from EdenTestCase and replaces it with separate `create_hg_repo()` and `create_git_repo()` methods. The `EdenRepoTest` subclasses now provide alternate `create_repo()` implementations instead of `get_repo_class()`. This cleans up the code a bit, since `create_repo()` no longer takes different arguments based on what type of repository is being created. This also will make it easier in upcoming diffs to further customize the logic that occurs in `create_hg_repo()`. Reviewed By: chadaustin Differential Revision: D7512320 fbshipit-source-id: d268b0ac0ffb33f3dfd34f2dd1917d57033c81aa
This commit is contained in:
parent
56c3a28517
commit
d3f0166b6c
@ -109,7 +109,7 @@ class EdenHgTestCase(testcase.EdenTestCase):
|
||||
|
||||
def create_backing_repo(self):
|
||||
hgrc = self.get_hgrc()
|
||||
repo = self.create_repo('main', hgrepo.HgRepository, hgrc=hgrc)
|
||||
repo = self.create_hg_repo('main', hgrc=hgrc)
|
||||
self.populate_backing_repo(repo)
|
||||
return repo
|
||||
|
||||
|
@ -21,7 +21,7 @@ class PullTest(EdenHgTestCase):
|
||||
hgrc['paths'] = {
|
||||
'default': self.server_repo.path,
|
||||
}
|
||||
repo = self.create_repo('main', hgrepo.HgRepository, hgrc=hgrc)
|
||||
repo = self.create_hg_repo('main', hgrc=hgrc)
|
||||
self.populate_backing_repo(repo)
|
||||
return repo
|
||||
|
||||
@ -44,9 +44,7 @@ class PullTest(EdenHgTestCase):
|
||||
'autocreatetrees': 'True',
|
||||
}
|
||||
|
||||
repo = self.create_repo('server_repo',
|
||||
hgrepo.HgRepository,
|
||||
hgrc=hgrc)
|
||||
repo = self.create_hg_repo('server_repo', hgrc=hgrc)
|
||||
|
||||
# Create a commit in the server repository
|
||||
repo.write_file('hello.txt', 'hola')
|
||||
|
@ -27,7 +27,7 @@ class HgError(CommandError):
|
||||
|
||||
|
||||
class HgRepository(repobase.Repository):
|
||||
def __init__(self, path: str) -> None:
|
||||
def __init__(self, path: str, system_hgrc: Optional[str] = None) -> None:
|
||||
'''
|
||||
If hgrc is specified, it will be used as the value of the HGRCPATH
|
||||
environment variable when `hg` is run.
|
||||
@ -41,7 +41,10 @@ class HgRepository(repobase.Repository):
|
||||
self.hg_environment['HGPLAIN'] = '1'
|
||||
# Set HGRCPATH to make sure we aren't affected by the local system's
|
||||
# mercurial settings from /etc/mercurial/
|
||||
self.hg_environment['HGRCPATH'] = ''
|
||||
if system_hgrc:
|
||||
self.hg_environment['HGRCPATH'] = system_hgrc
|
||||
else:
|
||||
self.hg_environment['HGRCPATH'] = ''
|
||||
self.hg_bin = FindExe.HG
|
||||
|
||||
def hg(
|
||||
|
@ -8,6 +8,7 @@
|
||||
# of patent rights can be found in the PATENTS file in the same directory.
|
||||
|
||||
import atexit
|
||||
import configparser
|
||||
import errno
|
||||
import inspect
|
||||
import logging
|
||||
@ -164,7 +165,7 @@ class EdenTestCase(TestParent):
|
||||
os.mkdir(self.home_dir)
|
||||
old_home = os.getenv('HOME')
|
||||
|
||||
def restore_home():
|
||||
def restore_home() -> None:
|
||||
if old_home is None:
|
||||
del os.environ['HOME']
|
||||
else:
|
||||
@ -232,25 +233,21 @@ class EdenTestCase(TestParent):
|
||||
'''
|
||||
return None
|
||||
|
||||
def create_repo(
|
||||
self, name: str, repo_class: Type[repobase.Repository], **kwargs: Any
|
||||
) -> repobase.Repository:
|
||||
'''
|
||||
Create a new repository.
|
||||
|
||||
Arguments:
|
||||
- name
|
||||
The repository name. This determines the repository location inside
|
||||
the self.repos_dir directory. The full repository path can be
|
||||
accessed as repo.path on the returned repo object.
|
||||
- repo_class
|
||||
The repository class object, such as hgrepo.HgRepository or
|
||||
gitrepo.GitRepository.
|
||||
'''
|
||||
def create_hg_repo(
|
||||
self, name: str, hgrc: Optional[configparser.ConfigParser] = None
|
||||
) -> hgrepo.HgRepository:
|
||||
repo_path = os.path.join(self.repos_dir, name)
|
||||
os.mkdir(repo_path)
|
||||
repo = repo_class(repo_path)
|
||||
repo.init(**kwargs)
|
||||
repo = hgrepo.HgRepository(repo_path)
|
||||
repo.init(hgrc=hgrc)
|
||||
|
||||
return repo
|
||||
|
||||
def create_git_repo(self, name: str) -> gitrepo.GitRepository:
|
||||
repo_path = os.path.join(self.repos_dir, name)
|
||||
os.mkdir(repo_path)
|
||||
repo = gitrepo.GitRepository(repo_path)
|
||||
repo.init()
|
||||
|
||||
return repo
|
||||
|
||||
@ -320,7 +317,7 @@ class EdenRepoTest(EdenTestCase):
|
||||
super().setup_eden_test()
|
||||
|
||||
self.repo_name = 'main'
|
||||
self.repo = self.create_repo(self.repo_name, self.get_repo_class())
|
||||
self.repo = self.create_repo(self.repo_name)
|
||||
self.populate_repo()
|
||||
self.report_time('repository setup done')
|
||||
|
||||
@ -332,9 +329,18 @@ class EdenRepoTest(EdenTestCase):
|
||||
raise NotImplementedError('individual test classes must implement '
|
||||
'populate_repo()')
|
||||
|
||||
def get_repo_class(self) -> Type[repobase.Repository]:
|
||||
def create_repo(self, name: str) -> Type[repobase.Repository]:
|
||||
'''
|
||||
Create a new repository.
|
||||
|
||||
Arguments:
|
||||
- name
|
||||
The repository name. This determines the repository location inside
|
||||
the self.repos_dir directory. The full repository path can be
|
||||
accessed as repo.path on the returned repo object.
|
||||
'''
|
||||
raise NotImplementedError('test subclasses must implement '
|
||||
'get_repo_class(). This is normally '
|
||||
'create_repo(). This is normally '
|
||||
'implemented automatically by '
|
||||
'@eden_repo_test')
|
||||
|
||||
@ -398,19 +404,16 @@ def test_replicator(
|
||||
def _replicate_eden_repo_test(
|
||||
test_class: Type[EdenRepoTest]
|
||||
) -> Iterable[Tuple[str, Type[EdenRepoTest]]]:
|
||||
repo_types = [
|
||||
(hgrepo.HgRepository, 'Hg'),
|
||||
(gitrepo.GitRepository, 'Git'),
|
||||
]
|
||||
|
||||
for (repo_class, suffix) in repo_types:
|
||||
# Define a new class that derives from the input class
|
||||
# as well as the repo-specific parent class type
|
||||
class RepoSpecificTest(test_class):
|
||||
def get_repo_class(self) -> Type[repobase.Repository]:
|
||||
return repo_class
|
||||
class HgRepoTest(test_class):
|
||||
def create_repo(self, name: str) -> repobase.Repository:
|
||||
return self.create_hg_repo(name)
|
||||
|
||||
yield suffix, RepoSpecificTest
|
||||
class GitRepoTest(test_class):
|
||||
def create_repo(self, name: str) -> repobase.Repository:
|
||||
return self.create_git_repo(name)
|
||||
|
||||
return [('Hg', HgRepoTest), ('Git', GitRepoTest)]
|
||||
|
||||
|
||||
# A decorator function used to create EdenHgTest and EdenGitTest
|
||||
|
@ -61,7 +61,7 @@ class RCTest(testcase.EdenRepoTest):
|
||||
msg='The client directory should have been restored')
|
||||
|
||||
def test_override_system_config(self) -> None:
|
||||
system_repo = self.create_repo('system_repo', self.get_repo_class())
|
||||
system_repo = self.create_repo('system_repo')
|
||||
|
||||
system_repo.write_file('hello.txt', 'hola\n')
|
||||
system_repo.commit('Initial commit.')
|
||||
|
@ -44,8 +44,8 @@ class RemountTest(testcase.EdenRepoTest):
|
||||
def test_git_and_hg(self) -> None:
|
||||
# Create git and hg repositories for mounting
|
||||
repo_names = {'git': 'git_repo', 'hg': 'hg_repo'}
|
||||
git_repo = self.create_repo(repo_names['git'], gitrepo.GitRepository)
|
||||
hg_repo = self.create_repo(repo_names['hg'], hgrepo.HgRepository)
|
||||
git_repo = self.create_git_repo(repo_names['git'])
|
||||
hg_repo = self.create_hg_repo(repo_names['hg'])
|
||||
|
||||
git_repo.write_file('hello', 'hola\n')
|
||||
git_repo.commit('Initial commit.')
|
||||
|
@ -53,8 +53,8 @@ type = hg
|
||||
self.assertEqual(expected, self._list_repos())
|
||||
|
||||
def test_add_multiple(self) -> None:
|
||||
hg_repo = self.create_repo('hg_repo', hgrepo.HgRepository)
|
||||
git_repo = self.create_repo('git_repo', gitrepo.GitRepository)
|
||||
hg_repo = self.create_hg_repo('hg_repo')
|
||||
git_repo = self.create_git_repo('git_repo')
|
||||
|
||||
self.eden.add_repository('hg1', hg_repo.path)
|
||||
self.assertEqual(['hg1'], self._list_repos())
|
||||
|
Loading…
Reference in New Issue
Block a user