gaps: move gaps.within() to method

This commit is contained in:
Dan Bungert 2022-08-02 17:06:16 -06:00
parent 324ff0bc8f
commit fe0745a83e
3 changed files with 18 additions and 18 deletions

View File

@ -70,6 +70,17 @@ class Gap:
usable=self.usable)
return (first_gap, rest_gap)
def within(self):
"""Find the first gap that is contained wholly inside the supplied
gap."""
gap_end = self.offset + self.size
for pg in parts_and_gaps(self.device):
if isinstance(pg, Gap):
pg_end = pg.offset + pg.size
if pg.offset >= self.offset and pg_end <= gap_end:
return pg
return None
@functools.singledispatch
def parts_and_gaps(device):
@ -258,17 +269,6 @@ def at_offset(device, offset):
return None
def within(device, gap):
"""Find the first gap that is contained wholly inside the supplied gap."""
gap_end = gap.offset + gap.size
for pg in parts_and_gaps(device):
if isinstance(pg, Gap):
pg_end = pg.offset + pg.size
if pg.offset >= gap.offset and pg_end <= gap_end:
return pg
return None
def after(device, offset):
"""Find the first gap that is after this offset."""
for pg in parts_and_gaps(device):

View File

@ -121,7 +121,7 @@ class TestWithin(unittest.TestCase):
def test_identity(self):
d = make_disk()
[gap] = gaps.parts_and_gaps(d)
self.assertEqual(gap, gaps.within(d, gap))
self.assertEqual(gap, gap.within())
def test_front_used(self):
m, d = make_model_and_disk(size=200 << 20)
@ -130,7 +130,7 @@ class TestWithin(unittest.TestCase):
[orig_g1, p1, orig_g2] = gaps.parts_and_gaps(d)
make_partition(m, d, offset=0, size=20 << 20)
[p1, g1, p2, g2] = gaps.parts_and_gaps(d)
self.assertEqual(g1, gaps.within(d, orig_g1))
self.assertEqual(g1, orig_g1.within())
def test_back_used(self):
m, d = make_model_and_disk(size=200 << 20)
@ -139,7 +139,7 @@ class TestWithin(unittest.TestCase):
[orig_g1, p1, orig_g2] = gaps.parts_and_gaps(d)
make_partition(m, d, offset=80 << 20, size=20 << 20)
[g1, p1, p2, g2] = gaps.parts_and_gaps(d)
self.assertEqual(g1, gaps.within(d, orig_g1))
self.assertEqual(g1, orig_g1.within())
def test_front_and_back_used(self):
m, d = make_model_and_disk(size=200 << 20)
@ -149,7 +149,7 @@ class TestWithin(unittest.TestCase):
make_partition(m, d, offset=0, size=20 << 20)
make_partition(m, d, offset=80 << 20, size=20 << 20)
[p1, g1, p2, p3, g2] = gaps.parts_and_gaps(d)
self.assertEqual(g1, gaps.within(d, orig_g1))
self.assertEqual(g1, orig_g1.within())
def test_multi_gap(self):
m, d = make_model_and_disk(size=200 << 20)
@ -158,7 +158,7 @@ class TestWithin(unittest.TestCase):
[orig_g1, p1, orig_g2] = gaps.parts_and_gaps(d)
make_partition(m, d, offset=20 << 20, size=20 << 20)
[g1, p1, g2, p2, g3] = gaps.parts_and_gaps(d)
self.assertEqual(g1, gaps.within(d, orig_g1))
self.assertEqual(g1, orig_g1.within())
def test_later_part_of_disk(self):
m, d = make_model_and_disk(size=200 << 20)
@ -167,7 +167,7 @@ class TestWithin(unittest.TestCase):
[orig_g1, p1, orig_g2] = gaps.parts_and_gaps(d)
make_partition(m, d, offset=120 << 20, size=20 << 20)
[g1, p1, g2, p2, g3] = gaps.parts_and_gaps(d)
self.assertEqual(g2, gaps.within(d, orig_g2))
self.assertEqual(g2, orig_g2.within())
class TestAfter(unittest.TestCase):

View File

@ -153,7 +153,7 @@ class FilesystemController(SubiquityController, FilesystemManipulator):
return gaps.largest_gap(disk)
else:
# find what's left of the gap after adding boot
gap = gaps.within(disk, gap)
gap = gap.within()
if gap is None:
raise Exception('failed to locate gap after adding boot')
return gap