Skip to content

Commit 1813a77

Browse files
committed
Fix git store to check if all the required fields are present
1 parent 38438a5 commit 1813a77

File tree

2 files changed

+69
-18
lines changed

2 files changed

+69
-18
lines changed

osc/git_scm/store.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,11 +240,33 @@ def assert_is_project(self):
240240
msg = f"Directory '{self.abspath}' is not a Git SCM working copy of a project"
241241
raise oscerr.NoWorkingCopy(msg)
242242

243+
missing = []
244+
for name in ["apiurl", "project"]:
245+
if not getattr(self, name):
246+
missing.append(name)
247+
if missing:
248+
msg = (
249+
f"Git SCM project working copy doesn't have the following metadata set: {', '.join(missing)}\n"
250+
"Use 'git-obs meta pull' or 'git-obs meta set' to fix that"
251+
)
252+
raise oscerr.NoWorkingCopy(msg)
253+
243254
def assert_is_package(self):
244255
if not self.is_package:
245256
msg = f"Directory '{self.abspath}' is not a Git SCM working copy of a package"
246257
raise oscerr.NoWorkingCopy(msg)
247258

259+
missing = []
260+
for name in ["apiurl", "project", "package"]:
261+
if not getattr(self, name):
262+
missing.append(name)
263+
if missing:
264+
msg = (
265+
f"Git SCM package working copy doesn't have the following metadata set: {', '.join(missing)}\n"
266+
"Use 'git-obs meta pull' or 'git-obs meta set' to fix that"
267+
)
268+
raise oscerr.NoWorkingCopy(msg)
269+
248270
# APIURL
249271

250272
@property
@@ -415,6 +437,12 @@ def __init__(self, path: str, check: bool = True, *, cached: bool = True):
415437
self.cached = cached
416438
self._cache = {}
417439

440+
if check:
441+
if self.is_project:
442+
self.assert_is_project()
443+
else:
444+
self.assert_is_package()
445+
418446
def _resolve_meta(self, field_name: str, *, allow_none: bool = False):
419447
result = None
420448

tests/test_git_scm_store.py

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
import unittest
88

99
import osc.conf
10+
from osc import oscerr
1011
from osc.git_scm.store import GitStore
12+
from osc.git_scm.store import LocalGitStore
1113
from osc.util import yaml as osc_yaml
1214

1315
from .common import patch
@@ -41,31 +43,31 @@ def tearDown(self):
4143
pass
4244

4345
def test_package(self):
44-
store = GitStore(self.tmpdir)
46+
store = GitStore(self.tmpdir, check=False)
4547
self.assertEqual(store.package, "my-package")
4648

4749
def test_project(self):
48-
store = GitStore(self.tmpdir)
50+
store = GitStore(self.tmpdir, check=False)
4951
self.assertEqual(store.project, None)
5052

5153
def test_last_buildroot(self):
52-
store = GitStore(self.tmpdir)
54+
store = GitStore(self.tmpdir, check=False)
5355
self.assertEqual(store.last_buildroot, None)
5456
store.last_buildroot = ("repo", "arch", "vm_type")
5557

56-
store = GitStore(self.tmpdir)
58+
store = GitStore(self.tmpdir, check=False)
5759
self.assertEqual(store.last_buildroot, ("repo", "arch", "vm_type"))
5860

5961
def test_last_buildroot_empty_vm_type(self):
60-
store = GitStore(self.tmpdir)
62+
store = GitStore(self.tmpdir, check=False)
6163
self.assertEqual(store.last_buildroot, None)
6264
store.last_buildroot = ("repo", "arch", None)
6365

64-
store = GitStore(self.tmpdir)
66+
store = GitStore(self.tmpdir, check=False)
6567
self.assertEqual(store.last_buildroot, ("repo", "arch", None))
6668

6769
def test_scmurl(self):
68-
store = GitStore(self.tmpdir)
70+
store = GitStore(self.tmpdir, check=False)
6971
self.assertEqual(store.scmurl, "https://example.com/packages/my-package.git")
7072

7173

@@ -95,7 +97,7 @@ def tearDown(self):
9597
except OSError:
9698
pass
9799

98-
def _git_init(self, path, separate_git_dir=None):
100+
def _git_init(self, path, *, separate_git_dir=None):
99101
os.makedirs(path, exist_ok=True)
100102
git_init_cmd = ["git", "init", "-q"]
101103
if separate_git_dir:
@@ -104,11 +106,30 @@ def _git_init(self, path, separate_git_dir=None):
104106
subprocess.check_output(["git", "checkout", "-b", "factory", "-q"], cwd=path)
105107
subprocess.check_output(["git", "remote", "add", "origin", "https://example.com/packages/my-package.git"], cwd=path)
106108

107-
def _osc_init(self, path, project, package=None):
109+
def _setup_project(self, path, *, apiurl="https://api.example.com", project=None):
110+
store = LocalGitStore(path, check=False)
111+
store._type = "project"
112+
if apiurl:
113+
store.apiurl = apiurl
114+
if project:
115+
store.project = project
116+
117+
def _setup_package(self, path, *, apiurl="https://api.example.com", project=None, package=None):
118+
store = LocalGitStore(path, check=False)
119+
store._type = "package"
120+
if apiurl:
121+
store.apiurl = apiurl
122+
if project:
123+
store.project = project
124+
if package:
125+
store.package = package
126+
127+
def _osc_init(self, path, *, apiurl="https://api.example.com", project=None, package=None):
108128
from osc.store import Store
109129

110130
os.makedirs(path, exist_ok=True)
111131
store = Store(path, check=False)
132+
store.apiurl = apiurl
112133
store.project = project
113134
store.package = package
114135

@@ -120,7 +141,10 @@ def test_pkg_no_project(self):
120141
pkg_path = os.path.join(self.tmpdir, "pkg")
121142
self._git_init(pkg_path)
122143

123-
store = GitStore(pkg_path)
144+
with self.assertRaises(oscerr.NoWorkingCopy):
145+
GitStore(pkg_path)
146+
147+
store = GitStore(pkg_path, check=False)
124148
self.assertEqual(store.project, None)
125149
self.assertEqual(store.package, "my-package")
126150

@@ -236,8 +260,8 @@ def test_manifest_apiurl_project(self):
236260
def test_pkg_git_project(self):
237261
prj_path = os.path.join(self.tmpdir, "project")
238262
self._git_init(prj_path)
263+
self._setup_project(prj_path, project="PROJ")
239264
self._write(os.path.join(prj_path, "_config"))
240-
self._write(os.path.join(prj_path, "project.build"), "PROJ")
241265

242266
pkg_path = os.path.join(prj_path, "package")
243267
self._git_init(pkg_path)
@@ -249,8 +273,8 @@ def test_pkg_git_project(self):
249273
def test_pkg_git_project_with_config_without_pbuild(self):
250274
prj_path = os.path.join(self.tmpdir, "project")
251275
self._git_init(prj_path)
276+
self._setup_project(prj_path, project="PROJ")
252277
self._write(os.path.join(prj_path, "_config"))
253-
self._write(os.path.join(prj_path, "project.build"), "PROJ")
254278

255279
pkg_path = os.path.join(prj_path, "package")
256280
self._git_init(pkg_path)
@@ -262,8 +286,8 @@ def test_pkg_git_project_with_config_without_pbuild(self):
262286
def test_pkg_git_project_without_config_with_pbuild(self):
263287
prj_path = os.path.join(self.tmpdir, "project")
264288
self._git_init(prj_path)
289+
self._setup_project(prj_path, project="PROJ")
265290
self._write(os.path.join(prj_path, "_pbuild"))
266-
self._write(os.path.join(prj_path, "project.build"), "PROJ")
267291

268292
pkg_path = os.path.join(prj_path, "package")
269293
self._git_init(pkg_path)
@@ -275,14 +299,14 @@ def test_pkg_git_project_without_config_with_pbuild(self):
275299
def test_pkg_separate_git_dir_git_project(self):
276300
prj_path = os.path.join(self.tmpdir, "project")
277301
self._git_init(prj_path)
302+
self._setup_project(prj_path, project="PROJ")
278303
self._write(os.path.join(prj_path, "_config"))
279-
self._write(os.path.join(prj_path, "project.build"), "PROJ")
280304

281305
# .git is not a directory, it's a file that contains "gitdir: <path>"
282306
pkg_path = os.path.join(prj_path, "package")
283307
self._git_init(pkg_path, separate_git_dir="../package-git-dir")
284308

285-
store = GitStore(pkg_path)
309+
store = GitStore(pkg_path, check=False)
286310
self.assertEqual(store.project, "PROJ")
287311
self.assertEqual(store.package, "my-package")
288312

@@ -298,10 +322,9 @@ def test_pkg_git_with_no_project(self):
298322
pkg_path = os.path.join(git_subdir_path, "package")
299323
self._git_init(pkg_path)
300324

301-
store = GitStore(pkg_path)
302325
# the parent directory contains arbitrary git repo -> no project
303-
self.assertEqual(store.project, None)
304-
self.assertEqual(store.package, "my-package")
326+
with self.assertRaises(oscerr.NoWorkingCopy):
327+
GitStore(pkg_path)
305328

306329

307330
if __name__ == "__main__":

0 commit comments

Comments
 (0)