[init] Remove duplicated checks for comps

Check once before iterating through the variants. This greatly
simplifies the tests as each function now has one less code path.

Signed-off-by: Lubomír Sedlář <lsedlar@redhat.com>
This commit is contained in:
Lubomír Sedlář 2016-03-10 15:04:52 +01:00
parent 1c84dc6ea0
commit 1efd4a9873
2 changed files with 38 additions and 86 deletions

View File

@ -159,23 +159,24 @@ class InitPhase(PhaseBase):
return False return False
def run(self): def run(self):
# write global comps and arch comps if self.compose.has_comps:
write_global_comps(self.compose) # write global comps and arch comps
for arch in self.compose.get_arches(): write_global_comps(self.compose)
write_arch_comps(self.compose, arch) for arch in self.compose.get_arches():
write_arch_comps(self.compose, arch)
# create comps repos # create comps repos
for arch in self.compose.get_arches(): for arch in self.compose.get_arches():
create_comps_repo(self.compose, arch) create_comps_repo(self.compose, arch)
# write variant comps # write variant comps
for variant in self.compose.get_variants(): for variant in self.compose.get_variants():
should_preserve = variant.uid in self.compose.conf.get('keep_original_comps', []) should_preserve = variant.uid in self.compose.conf.get('keep_original_comps', [])
for arch in variant.arches: for arch in variant.arches:
if should_preserve: if should_preserve:
copy_variant_comps(self.compose, arch, variant) copy_variant_comps(self.compose, arch, variant)
else: else:
write_variant_comps(self.compose, arch, variant) write_variant_comps(self.compose, arch, variant)
# download variants.xml / product.xml? # download variants.xml / product.xml?
@ -184,9 +185,6 @@ class InitPhase(PhaseBase):
def write_global_comps(compose): def write_global_comps(compose):
if not compose.has_comps:
return
comps_file_global = compose.paths.work.comps(arch="global") comps_file_global = compose.paths.work.comps(arch="global")
msg = "Writing global comps file: %s" % comps_file_global msg = "Writing global comps file: %s" % comps_file_global
@ -210,9 +208,6 @@ def write_global_comps(compose):
def write_arch_comps(compose, arch): def write_arch_comps(compose, arch):
if not compose.has_comps:
return
comps_file_arch = compose.paths.work.comps(arch=arch) comps_file_arch = compose.paths.work.comps(arch=arch)
msg = "Writing comps file for arch '%s': %s" % (arch, comps_file_arch) msg = "Writing comps file for arch '%s': %s" % (arch, comps_file_arch)
@ -227,9 +222,6 @@ def write_arch_comps(compose, arch):
def write_variant_comps(compose, arch, variant): def write_variant_comps(compose, arch, variant):
if not compose.has_comps:
return
comps_file = compose.paths.work.comps(arch=arch, variant=variant) comps_file = compose.paths.work.comps(arch=arch, variant=variant)
msg = "Writing comps file (arch: %s, variant: %s): %s" % (arch, variant, comps_file) msg = "Writing comps file (arch: %s, variant: %s): %s" % (arch, variant, comps_file)
@ -257,18 +249,12 @@ def write_variant_comps(compose, arch, variant):
def copy_variant_comps(compose, arch, variant): def copy_variant_comps(compose, arch, variant):
if not compose.has_comps:
return
global_comps = compose.paths.work.comps(arch="global") global_comps = compose.paths.work.comps(arch="global")
comps_file = compose.paths.work.comps(arch=arch, variant=variant) comps_file = compose.paths.work.comps(arch=arch, variant=variant)
shutil.copy(global_comps, comps_file) shutil.copy(global_comps, comps_file)
def create_comps_repo(compose, arch): def create_comps_repo(compose, arch):
if not compose.has_comps:
return
createrepo_c = compose.conf.get("createrepo_c", True) createrepo_c = compose.conf.get("createrepo_c", True)
createrepo_checksum = compose.conf["createrepo_checksum"] createrepo_checksum = compose.conf["createrepo_checksum"]
repo = CreaterepoWrapper(createrepo_c=createrepo_c) repo = CreaterepoWrapper(createrepo_c=createrepo_c)

View File

@ -34,6 +34,7 @@ class TestInitPhase(PungiTestCase):
@mock.patch('pungi.phases.init.write_prepopulate_file') @mock.patch('pungi.phases.init.write_prepopulate_file')
def test_run(self, write_prepopulate, write_variant, create_comps, write_arch, write_global): def test_run(self, write_prepopulate, write_variant, create_comps, write_arch, write_global):
compose = DummyCompose(self.topdir, {}) compose = DummyCompose(self.topdir, {})
compose.has_comps = True
phase = init.InitPhase(compose) phase = init.InitPhase(compose)
phase.run() phase.run()
@ -61,6 +62,7 @@ class TestInitPhase(PungiTestCase):
compose = DummyCompose(self.topdir, { compose = DummyCompose(self.topdir, {
'keep_original_comps': ['Everything'], 'keep_original_comps': ['Everything'],
}) })
compose.has_comps = True
phase = init.InitPhase(compose) phase = init.InitPhase(compose)
phase.run() phase.run()
@ -78,6 +80,26 @@ class TestInitPhase(PungiTestCase):
[mock.call(compose, 'x86_64', compose.variants['Everything']), [mock.call(compose, 'x86_64', compose.variants['Everything']),
mock.call(compose, 'amd64', compose.variants['Everything'])]) mock.call(compose, 'amd64', compose.variants['Everything'])])
@mock.patch('pungi.phases.init.copy_variant_comps')
@mock.patch('pungi.phases.init.write_global_comps')
@mock.patch('pungi.phases.init.write_arch_comps')
@mock.patch('pungi.phases.init.create_comps_repo')
@mock.patch('pungi.phases.init.write_variant_comps')
@mock.patch('pungi.phases.init.write_prepopulate_file')
def test_run_without_comps(self, write_prepopulate, write_variant, create_comps,
write_arch, write_global, copy_comps):
compose = DummyCompose(self.topdir, {})
compose.has_comps = False
phase = init.InitPhase(compose)
phase.run()
self.assertEqual(write_global.mock_calls, [])
self.assertEqual(write_prepopulate.mock_calls, [mock.call(compose)])
self.assertItemsEqual(write_arch.mock_calls, [])
self.assertItemsEqual(create_comps.mock_calls, [])
self.assertItemsEqual(write_variant.mock_calls, [])
self.assertItemsEqual(copy_comps.mock_calls, [])
def test_validate_keep_original_comps_missing(self): def test_validate_keep_original_comps_missing(self):
compose = DummyCompose(self.topdir, MIN_CONFIG) compose = DummyCompose(self.topdir, MIN_CONFIG)
phase = init.InitPhase(compose) phase = init.InitPhase(compose)
@ -98,19 +120,9 @@ class TestInitPhase(PungiTestCase):
class TestWriteArchComps(PungiTestCase): class TestWriteArchComps(PungiTestCase):
@mock.patch('pungi.phases.init.run')
def test_does_not_run_without_comps(self, run):
compose = DummyCompose(self.topdir, {})
compose.has_comps = False
init.write_arch_comps(compose, 'x86_64')
self.assertEqual(run.mock_calls, [])
@mock.patch('pungi.phases.init.run') @mock.patch('pungi.phases.init.run')
def test_run(self, run): def test_run(self, run):
compose = DummyCompose(self.topdir, {}) compose = DummyCompose(self.topdir, {})
compose.has_comps = True
compose.DEBUG = False compose.DEBUG = False
init.write_arch_comps(compose, 'x86_64') init.write_arch_comps(compose, 'x86_64')
@ -123,7 +135,6 @@ class TestWriteArchComps(PungiTestCase):
@mock.patch('pungi.phases.init.run') @mock.patch('pungi.phases.init.run')
def test_run_in_debug(self, run): def test_run_in_debug(self, run):
compose = DummyCompose(self.topdir, {}) compose = DummyCompose(self.topdir, {})
compose.has_comps = True
compose.DEBUG = True compose.DEBUG = True
touch(self.topdir + '/work/x86_64/comps/comps-x86_64.xml') touch(self.topdir + '/work/x86_64/comps/comps-x86_64.xml')
@ -134,21 +145,11 @@ class TestWriteArchComps(PungiTestCase):
class TestCreateCompsRepo(PungiTestCase): class TestCreateCompsRepo(PungiTestCase):
@mock.patch('pungi.phases.init.run')
def test_does_not_run_without_comps(self, run):
compose = DummyCompose(self.topdir, {})
compose.has_comps = False
init.create_comps_repo(compose, 'x86_64')
self.assertEqual(run.mock_calls, [])
@mock.patch('pungi.phases.init.run') @mock.patch('pungi.phases.init.run')
def test_run(self, run): def test_run(self, run):
compose = DummyCompose(self.topdir, { compose = DummyCompose(self.topdir, {
'createrepo_checksum': 'sha256', 'createrepo_checksum': 'sha256',
}) })
compose.has_comps = True
compose.DEBUG = False compose.DEBUG = False
init.create_comps_repo(compose, 'x86_64') init.create_comps_repo(compose, 'x86_64')
@ -167,7 +168,6 @@ class TestCreateCompsRepo(PungiTestCase):
compose = DummyCompose(self.topdir, { compose = DummyCompose(self.topdir, {
'createrepo_checksum': 'sha256', 'createrepo_checksum': 'sha256',
}) })
compose.has_comps = True
compose.DEBUG = True compose.DEBUG = True
os.makedirs(self.topdir + '/work/x86_64/comps_repo/repodata') os.makedirs(self.topdir + '/work/x86_64/comps_repo/repodata')
@ -178,22 +178,10 @@ class TestCreateCompsRepo(PungiTestCase):
class TestWriteGlobalComps(PungiTestCase): class TestWriteGlobalComps(PungiTestCase):
@mock.patch('shutil.copy2')
@mock.patch('pungi.phases.init.get_file_from_scm')
def test_does_not_run_without_comps(self, get_file, copy2):
compose = DummyCompose(self.topdir, {'comps_file': 'some-file.xml'})
compose.has_comps = False
init.write_global_comps(compose)
self.assertEqual(get_file.mock_calls, [])
self.assertEqual(copy2.mock_calls, [])
@mock.patch('shutil.copy2') @mock.patch('shutil.copy2')
@mock.patch('pungi.phases.init.get_file_from_scm') @mock.patch('pungi.phases.init.get_file_from_scm')
def test_run_in_debug(self, get_file, copy2): def test_run_in_debug(self, get_file, copy2):
compose = DummyCompose(self.topdir, {'comps_file': 'some-file.xml'}) compose = DummyCompose(self.topdir, {'comps_file': 'some-file.xml'})
compose.has_comps = True
compose.DEBUG = True compose.DEBUG = True
touch(self.topdir + '/work/global/comps/comps-global.xml') touch(self.topdir + '/work/global/comps/comps-global.xml')
@ -205,7 +193,6 @@ class TestWriteGlobalComps(PungiTestCase):
@mock.patch('pungi.phases.init.get_file_from_scm') @mock.patch('pungi.phases.init.get_file_from_scm')
def test_run_local_file(self, get_file): def test_run_local_file(self, get_file):
compose = DummyCompose(self.topdir, {'comps_file': 'some-file.xml'}) compose = DummyCompose(self.topdir, {'comps_file': 'some-file.xml'})
compose.has_comps = True
compose.DEBUG = False compose.DEBUG = False
def gen_file(src, dest, logger=None): def gen_file(src, dest, logger=None):
@ -221,20 +208,10 @@ class TestWriteGlobalComps(PungiTestCase):
class TestWriteVariantComps(PungiTestCase): class TestWriteVariantComps(PungiTestCase):
@mock.patch('pungi.phases.init.run')
def test_does_not_run_without_comps(self, run):
compose = DummyCompose(self.topdir, {})
compose.has_comps = False
init.write_variant_comps(compose, 'x86_64', compose.variants['Server'])
self.assertEqual(run.mock_calls, [])
@mock.patch('pungi.phases.init.run') @mock.patch('pungi.phases.init.run')
@mock.patch('pungi.phases.init.CompsWrapper') @mock.patch('pungi.phases.init.CompsWrapper')
def test_run(self, CompsWrapper, run): def test_run(self, CompsWrapper, run):
compose = DummyCompose(self.topdir, {}) compose = DummyCompose(self.topdir, {})
compose.has_comps = True
compose.DEBUG = False compose.DEBUG = False
variant = compose.variants['Server'] variant = compose.variants['Server']
@ -257,7 +234,6 @@ class TestWriteVariantComps(PungiTestCase):
@mock.patch('pungi.phases.init.CompsWrapper') @mock.patch('pungi.phases.init.CompsWrapper')
def test_run_in_debug(self, CompsWrapper, run): def test_run_in_debug(self, CompsWrapper, run):
compose = DummyCompose(self.topdir, {}) compose = DummyCompose(self.topdir, {})
compose.has_comps = True
compose.DEBUG = True compose.DEBUG = True
variant = compose.variants['Server'] variant = compose.variants['Server']
touch(self.topdir + '/work/x86_64/comps/comps-Server.x86_64.xml') touch(self.topdir + '/work/x86_64/comps/comps-Server.x86_64.xml')
@ -276,19 +252,9 @@ class TestWriteVariantComps(PungiTestCase):
class TestCopyVariantComps(PungiTestCase): class TestCopyVariantComps(PungiTestCase):
@mock.patch('shutil.copy')
def test_does_not_run_without_comps(self, copy):
compose = DummyCompose(self.topdir, {})
compose.has_comps = False
init.copy_variant_comps(compose, 'x86_64', compose.variants['Server'])
self.assertEqual(copy.mock_calls, [])
@mock.patch('shutil.copy') @mock.patch('shutil.copy')
def test_run(self, copy): def test_run(self, copy):
compose = DummyCompose(self.topdir, {}) compose = DummyCompose(self.topdir, {})
compose.has_comps = True
variant = compose.variants['Server'] variant = compose.variants['Server']
init.copy_variant_comps(compose, 'x86_64', variant) init.copy_variant_comps(compose, 'x86_64', variant)