Commit 0c09fb9c authored by richardmaw-codethink's avatar richardmaw-codethink
Browse files

Merge branch 'Qinusty/unit-test-utils' into 'master'

Fix issue with _pretty_size with large numbers of bytes

See merge request !799
parents ec04446b d3a07e6b
Loading
Loading
Loading
Loading
Loading
+4 −3
Original line number Diff line number Diff line
@@ -634,7 +634,7 @@ def _parse_size(size, volume):

# _pretty_size()
#
# Converts a number of bytes into a string representation in KB, MB, GB, TB
# Converts a number of bytes into a string representation in KiB, MiB, GiB, TiB
# represented as K, M, G, T etc.
#
# Args:
@@ -646,10 +646,11 @@ def _parse_size(size, volume):
def _pretty_size(size, dec_places=0):
    psize = size
    unit = 'B'
    for unit in ('B', 'K', 'M', 'G', 'T'):
    units = ('B', 'K', 'M', 'G', 'T')
    for unit in units:
        if psize < 1024:
            break
        else:
        elif unit != units[-1]:
            psize /= 1024
    return "{size:g}{unit}".format(size=round(psize, dec_places), unit=unit)

+44 −0
Original line number Diff line number Diff line
from contextlib import contextmanager
import os


# MockAttributeResult
#
# A class to take a dictionary of kwargs and make them accessible via
# attributes of the object.
#
class MockAttributeResult(dict):
    __getattr__ = dict.get


# mock_statvfs():
#
# Gets a function which mocks statvfs and returns a statvfs result with the kwargs accessible.
#
# Returns:
#    func(path) -> object: object will have all the kwargs accessible via object.kwarg
#
# Example:
#    statvfs = mock_statvfs(f_blocks=10)
#    result = statvfs("regardless/of/path")
#    assert result.f_blocks == 10 # True
def mock_statvfs(**kwargs):
    def statvfs(path):
        return MockAttributeResult(kwargs)
    return statvfs


# monkey_patch()
#
# with monkey_patch("statvfs", custom_statvfs):
#    assert os.statvfs == custom_statvfs # True
# assert os.statvfs == custom_statvfs # False
#
@contextmanager
def monkey_patch(to_patch, patched_func):
    orig = getattr(os, to_patch)
    setattr(os, to_patch, patched_func)
    try:
        yield
    finally:
        setattr(os, to_patch, orig)

tests/utils/misc.py

0 → 100644
+30 −0
Original line number Diff line number Diff line
from buildstream import _yaml
from ..testutils import mock_os
from ..testutils.runcli import cli

import os
import pytest


KiB = 1024
MiB = (KiB * 1024)
GiB = (MiB * 1024)
TiB = (GiB * 1024)


def test_parse_size_over_1024T(cli, tmpdir):
    BLOCK_SIZE = 4096
    cli.configure({
        'cache': {
            'quota': 2048 * TiB
        }
    })
    project = tmpdir.join("main")
    os.makedirs(str(project))
    _yaml.dump({'name': 'main'}, str(project.join("project.conf")))

    bavail = (1025 * TiB) / BLOCK_SIZE
    patched_statvfs = mock_os.mock_statvfs(f_bavail=bavail, f_bsize=BLOCK_SIZE)
    with mock_os.monkey_patch("statvfs", patched_statvfs):
        result = cli.run(project, args=["build", "file.bst"])
        assert "1025T of available system storage" in result.stderr