Exemple #1
0
def test_mplug_init_getworkdir(mock_files, monkeypatch):
    """Check that the mpv configuration directory is detected correctly
    based on the existence of different environment variables."""
    datadir = "some datadir"
    wrong_dir = "wrong directory"
    # MPV_HOME and XDG_CONFIG_HOME should not matter for this
    monkeypatch.setenv("MPV_HOME", wrong_dir)
    monkeypatch.setenv("XDG_CONFIG_HOME", wrong_dir)
    # Use XDG_DATA_HOME if present
    monkeypatch.setenv("XDG_DATA_HOME", datadir)
    monkeypatch.delenv("APPDATA", raising=False)
    mpl = mplug.MPlug()
    assert mpl.workdir == Path(datadir) / "mplug"

    # Use APPDATA if present
    monkeypatch.setenv("APPDATA", datadir)
    monkeypatch.delenv("XDG_DATA_HOME", raising=False)
    mpl = mplug.MPlug()
    assert mpl.workdir == Path(datadir) / "mplug"

    # Fallback to ~/.mplug
    monkeypatch.delenv("XDG_DATA_HOME", raising=False)
    monkeypatch.delenv("APPDATA", raising=False)
    mpl = mplug.MPlug()
    assert mpl.workdir == Path.home() / ".mplug"
Exemple #2
0
def test_mplug_init_invalid_state(mock_files):
    """Test initialisation when the state-file is corrupt."""
    mock_files.json_load.side_effect = [None, json.JSONDecodeError("", "", 0)]
    with pytest.raises(SystemExit) as pytest_wrapped_e:
        mplug.MPlug()
    assert pytest_wrapped_e.type == SystemExit
    assert pytest_wrapped_e.value.code != 0
Exemple #3
0
def test_mplug_init_getmpvdir(mock_files, monkeypatch):
    """Check that the mplug working directory is set correctly
    based on the existence of different environment variables."""
    mpv_home_dir = "some_mpv_home_dir"
    wrong_dir = "wrong directory"
    # MPV_HOME set, others unset -> use MPV_HOME
    monkeypatch.setenv("MPV_HOME", mpv_home_dir)
    monkeypatch.delenv("XDG_CONFIG_HOME", raising=False)
    monkeypatch.delenv("APPDATA", raising=False)
    monkeypatch.delenv("XDG_DATA_HOME", raising=False)
    mpl = mplug.MPlug()
    assert mpl.mpvdir == Path(mpv_home_dir)

    # MPV_HOME set, others also set -> use MPV_HOME
    monkeypatch.setenv("MPV_HOME", mpv_home_dir)
    monkeypatch.setenv("XDG_CONFIG_HOME", wrong_dir)
    monkeypatch.setenv("APPDATA", wrong_dir)
    monkeypatch.setenv("XDG_DATA_HOME", wrong_dir)
    mpl = mplug.MPlug()
    assert mpl.mpvdir == Path(mpv_home_dir)

    # MPV_HOME unset, XDG_CONFIG_HOME set -> use XDG_CONFIG_HOME
    monkeypatch.delenv("MPV_HOME", raising=False)
    monkeypatch.setenv("XDG_CONFIG_HOME", mpv_home_dir)
    monkeypatch.setenv("APPDATA", wrong_dir)
    mpl = mplug.MPlug()
    assert mpl.mpvdir == Path(mpv_home_dir) / "mpv"

    # MPV_HOME unset, APPDATA set -> use APPDATA
    monkeypatch.delenv("MPV_HOME", raising=False)
    monkeypatch.delenv("XDG_CONFIG_HOME", raising=False)
    monkeypatch.setenv("APPDATA", mpv_home_dir)
    mpl = mplug.MPlug()
    assert mpl.mpvdir == Path(mpv_home_dir) / "mpv"

    # nothing set -> fallback ~/.mpv
    monkeypatch.delenv("MPV_HOME", raising=False)
    monkeypatch.delenv("XDG_CONFIG_HOME", raising=False)
    monkeypatch.delenv("APPDATA", raising=False)
    mpl = mplug.MPlug()
    assert mpl.mpvdir == Path.home() / ".mpv"
Exemple #4
0
def fixture_init_mplug(mock_files):
    """Return initialised MPlug.

    This fixture is basically the same as test_mplug_init_up2date."""
    mpl = mplug.MPlug()
    status_file = mpl.statefile
    assert mpl.script_directory == {}
    assert mpl.installed_plugins == {}
    yield mpl
    mock_files.open.reset_mock()
    mpl.save_state_to_disk()
    mock_files.json_dump.assert_called_once()
    mock_files.open.assert_called_once_with(status_file, "w")
Exemple #5
0
def test_mplug_init_up2date(mock_files):
    """Initialise MPlug.

    Case: script directory exists and is up to date
    """
    mpl = mplug.MPlug()
    script_dir_file = mpl.directory_folder / mpl.directory_filename
    status_file = mpl.statefile
    assert mpl.script_directory == {}
    assert mpl.installed_plugins == {}
    mock_files.json_load.assert_called()
    mock_files.open.assert_has_calls(
        [call(script_dir_file), call(status_file)], any_order=True
    )
Exemple #6
0
def test_mplug_init_new(mock_files):
    """Initialise MPlug.

    Case: script directory does not yet exists
    """
    mock_files.Path_exists.return_value = False
    mpl = mplug.MPlug()
    script_dir_file = mpl.directory_folder / mpl.directory_filename
    status_file = mpl.statefile
    assert mpl.script_directory == {}
    assert mpl.installed_plugins == {}
    mock_files.json_load.assert_called()
    mock_files.open.assert_has_calls(
        [call(script_dir_file), call(status_file)], any_order=True
    )
Exemple #7
0
def test_mplug_init_outdated(mock_files):
    """Initialise MPlug.

    Case: script directory exists but is outdated
    """
    mock_files.Path_stat().st_mtime = 0
    mpl = mplug.MPlug()
    script_dir_file = mpl.directory_folder / mpl.directory_filename
    status_file = mpl.statefile
    assert mpl.script_directory == {}
    assert mpl.installed_plugins == {}
    mock_files.json_load.assert_called()
    mock_files.open.assert_has_calls(
        [call(script_dir_file), call(status_file)], any_order=True
    )
Exemple #8
0
def test_mplug_init_no_state_file(mock_files):
    """Test initialisation when the state-file is missing."""
    mock_files.json_load.side_effect = [None, FileNotFoundError]
    mpl = mplug.MPlug()
    assert mpl.installed_plugins == {}