def test_find_device_unknown_os(): """ Raises a NotImplementedError if the host OS is not supported. """ with mock.patch("os.name", "foo"): with pytest.raises(NotImplementedError) as ex: circup.find_device() assert ex.value.args[0] == 'OS "foo" not supported.'
def test_find_device_posix_missing(): """ Simulate being on os.name == 'posix' and a call to "mount" returns no records associated with an Adafruit device. """ with open("tests/mount_missing.txt", "rb") as fixture_file: fixture = fixture_file.read() with mock.patch("os.name", "posix"), mock.patch("circup.check_output", return_value=fixture): assert circup.find_device() is None
def test_find_device_posix_exists(): """ Simulate being on os.name == 'posix' and a call to "mount" returns a record indicating a connected device. """ with open("tests/mount_exists.txt", "rb") as fixture_file: fixture = fixture_file.read() with mock.patch("os.name", "posix"): with mock.patch("circup.check_output", return_value=fixture): assert circup.find_device() == "/media/ntoll/CIRCUITPY"
def test_find_device_posix_no_mount_command(): """ When the user doesn't have administrative privileges on OSX then the mount command isn't on their path. In which case, check circup uses the more explicit /sbin/mount instead. """ with open("tests/mount_exists.txt", "rb") as fixture_file: fixture = fixture_file.read() mock_check = mock.MagicMock(side_effect=[FileNotFoundError, fixture]) with mock.patch("os.name", "posix"), mock.patch("circup.check_output", mock_check): assert circup.find_device() == "/media/ntoll/CIRCUITPY" assert mock_check.call_count == 2 assert mock_check.call_args_list[0][0][0] == "mount" assert mock_check.call_args_list[1][0][0] == "/sbin/mount"
def test_find_device_nt_missing(): """ Simulate being on os.name == 'nt' and a disk with a volume name 'CIRCUITPY' does not exist for a device. """ mock_windll = mock.MagicMock() mock_windll.kernel32 = mock.MagicMock() mock_windll.kernel32.GetVolumeInformationW = mock.MagicMock() mock_windll.kernel32.GetVolumeInformationW.return_value = None fake_buffer = ctypes.create_unicode_buffer(1024) with mock.patch("os.name", "nt"), mock.patch( "os.path.exists", return_value=True ), mock.patch("ctypes.create_unicode_buffer", return_value=fake_buffer): ctypes.windll = mock_windll assert circup.find_device() is None
def test_find_device_nt_exists(): """ Simulate being on os.name == 'nt' and a disk with a volume name 'CIRCUITPY' exists indicating a connected device. """ mock_windll = mock.MagicMock() mock_windll.kernel32 = mock.MagicMock() mock_windll.kernel32.GetVolumeInformationW = mock.MagicMock() mock_windll.kernel32.GetVolumeInformationW.return_value = None fake_buffer = ctypes.create_unicode_buffer("CIRCUITPY") with mock.patch("os.name", "nt"), mock.patch( "os.path.exists", return_value=True ), mock.patch("ctypes.create_unicode_buffer", return_value=fake_buffer): ctypes.windll = mock_windll assert circup.find_device() == "A:\\"