import subprocess import sys from unittest import mock
import mozunit import pytest from mozpower import MozPower from mozpower.mozpower import MissingProcessorInfoError, OsCpuComboMissingError
def test_mozpower_android_init_failure(): """Tests that the MozPower object fails when the android
flag is set. Remove this test once android is implemented. """ with pytest.raises(NotImplementedError):
MozPower(android=True)
def test_mozpower_oscpu_combo_missing_error(): """Tests that the error OsCpuComboMissingError is raised
when we can't find a OS, and CPU combination (and, therefore, cannot
find a power measurer). """ with mock.patch.object(
MozPower, "_get_os", return_value="Not-An-OS"
) as _, mock.patch.object(
MozPower, "_get_processor_info", return_value="Not-A-Processor"
) as _: with pytest.raises(OsCpuComboMissingError):
MozPower()
def test_mozpower_processor_info_missing_error(): """Tests that the error MissingProcessorInfoError is raised
when failures occur during processor information parsing. """ # The builtins module name differs between python 2 and 3
builtins_name = "__builtin__" if sys.version_info[0] == 3:
builtins_name = "builtins"
def os_side_effect_true(*args, **kwargs): """Used as a passing side effect for os.path.exists calls.""" returnTrue
def os_side_effect_false(*args, **kwargs): """Used as a failing side effect for os.path.exists calls.""" returnFalse
def subprocess_side_effect_fail(*args, **kwargs): """Used to mock a failure in subprocess.check_output calls.""" raise subprocess.CalledProcessError(1, "Testing failure")
# Test failures in macos processor information parsing with mock.patch.object(MozPower, "_get_os", return_value="Darwin") as _: with mock.patch("os.path.exists") as os_mock:
os_mock.side_effect = os_side_effect_false
# Check that we fail properly if the processor # information file doesn't exist. with pytest.raises(MissingProcessorInfoError):
MozPower()
# Check that we fail properly when an error occurs # in the subprocess call.
os_mock.side_effect = os_side_effect_true with mock.patch("subprocess.check_output") as subprocess_mock:
subprocess_mock.side_effect = subprocess_side_effect_fail with pytest.raises(MissingProcessorInfoError):
MozPower()
# Test failures in linux processor information parsing with mock.patch.object(MozPower, "_get_os", return_value="Linux") as _: with mock.patch("os.path.exists") as os_mock:
os_mock.side_effect = os_side_effect_false
# Check that we fail properly if the processor # information file doesn't exist. with pytest.raises(MissingProcessorInfoError):
MozPower()
# Check that we fail properly when the model cannot be found # with by searching for 'model name'.
os_mock.side_effect = os_side_effect_true with mock.patch( "%s.open" % builtins_name, mock.mock_open(read_data="")
) as _: with pytest.raises(MissingProcessorInfoError):
MozPower()
def test_mozpower_oscpu_combo(mozpower_obj): """Tests that the correct class is instantiated for a given
OS and CPU combination (MacIntelPower in this case). """ assert mozpower_obj.measurer.__class__.__name__ == "MacIntelPower" assert (
mozpower_obj.measurer._os == "darwin"and mozpower_obj.measurer._cpu == "intel"
)
def test_mozpower_measuring(mozpower_obj): """Tests that measurers are properly called with each method.""" with mock.patch( "mozpower.macintelpower.MacIntelPower.initialize_power_measurements"
) as _, mock.patch( "mozpower.macintelpower.MacIntelPower.finalize_power_measurements"
) as _, mock.patch( "mozpower.macintelpower.MacIntelPower.get_perfherder_data"
) as _:
mozpower_obj.initialize_power_measurements()
mozpower_obj.measurer.initialize_power_measurements.assert_called()
def test_mozpower_measuring_with_no_measurer(mozpower_obj): """Tests that no errors occur when the measurer is None, and the
initialize, finalize, and get_perfherder_data functions are called. """ with mock.patch( "mozpower.macintelpower.MacIntelPower.initialize_power_measurements"
) as _, mock.patch( "mozpower.macintelpower.MacIntelPower.finalize_power_measurements"
) as _, mock.patch( "mozpower.macintelpower.MacIntelPower.get_perfherder_data"
) as _:
measurer = mozpower_obj.measurer
mozpower_obj.measurer = None
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.