handle str and pathlib.Path in getvcs()

This commit is contained in:
Hans-Christoph Steiner 2023-02-20 22:38:30 +01:00
parent d0d15a205f
commit 24df262f6b
2 changed files with 54 additions and 2 deletions

View File

@ -962,6 +962,11 @@ def setup_vcs(app):
def getvcs(vcstype, remote, local):
"""Return a vcs instance based on the arguments.
remote and local can be either a string or a pathlib.Path
"""
if vcstype == 'git':
return vcs_git(remote, local)
if vcstype == 'git-svn':
@ -971,7 +976,7 @@ def getvcs(vcstype, remote, local):
if vcstype == 'bzr':
return vcs_bzr(remote, local)
if vcstype == 'srclib':
if local != Path('build', 'srclib', remote):
if str(local) != os.path.join('build', 'srclib', str(remote)):
raise VCSException("Error: srclib paths are hard-coded!")
return getsrclib(remote, os.path.join('build', 'srclib'), raw=True)
if vcstype == 'svn':
@ -1989,13 +1994,18 @@ def getsrclib(spec, srclib_dir, basepath=False,
build=None):
"""Get the specified source library.
Returns the path to it. Normally this is the path to be used when
Return the path to it. Normally this is the path to be used when
referencing it, which may be a subdirectory of the actual project. If
you want the base directory of the project, pass 'basepath=True'.
spec and srclib_dir are both strings, not pathlib.Path.
"""
number = None
subdir = None
if not isinstance(spec, str):
spec = str(spec)
if not isinstance(srclib_dir, str):
spec = str(srclib_dir)
if raw:
name = spec
ref = None

View File

@ -58,6 +58,7 @@ class CommonTest(unittest.TestCase):
fdroidserver.common.config = None
fdroidserver.common.options = mock.Mock()
fdroidserver.common.options.verbose = False
fdroidserver.metadata.srclibs = None
self._td = mkdtemp()
self.testdir = self._td.name
@ -412,6 +413,47 @@ class CommonTest(unittest.TestCase):
vcs1 = fdroidserver.common.getvcs('git', git_url, gitrepo)
vcs1.gotorevision('0.3', refresh=False)
def test_setup_vcs_srclib(self):
app = fdroidserver.metadata.App(
{
'RepoType': 'srclib',
'Repo': 'TransportsRennes',
}
)
srclib = {
'RepoType': 'git',
'Repo': 'https://github.com/ybonnel/TransportsRennes',
}
fdroidserver.metadata.srclibs = {'TransportsRennes': srclib}
vcs, build_dir = fdroidserver.common.setup_vcs(app)
self.assertIsNotNone(vcs)
self.assertEqual(build_dir, Path('build/srclib/TransportsRennes'))
def test_getvcs_srclib(self):
vcstype = 'srclib'
remote = 'TransportsRennes'
local = 'build/srclib/' + remote
fdroidserver.metadata.srclibs = {
remote: {
'RepoType': 'git',
'Repo': 'https://github.com/ybonnel/TransportsRennes',
}
}
self.assertIsNotNone(fdroidserver.common.getvcs(vcstype, remote, local))
self.assertIsNotNone(fdroidserver.common.getvcs(vcstype, Path(remote), local))
self.assertIsNotNone(fdroidserver.common.getvcs(vcstype, remote, Path(local)))
self.assertIsNotNone(fdroidserver.common.getvcs(
vcstype, Path(remote), Path(local)
))
with self.assertRaises(VCSException):
fdroidserver.common.getvcs(vcstype, remote, 'bad')
with self.assertRaises(VCSException):
fdroidserver.common.getvcs(vcstype, remote, Path('bad'))
with self.assertRaises(VCSException):
fdroidserver.common.getvcs(vcstype, Path(remote), 'bad')
with self.assertRaises(VCSException):
fdroidserver.common.getvcs(vcstype, Path(remote), Path('bad'))
def test_fdroid_popen_stderr_redirect(self):
config = dict()
fdroidserver.common.fill_config_defaults(config)