From 9aa7103d4c97f46491d9830707be80a4cab911ee Mon Sep 17 00:00:00 2001 From: sigma67 Date: Sun, 9 Apr 2023 21:04:29 +0200 Subject: [PATCH] fix ytmusicapi setup, add setup test --- spotify_to_ytmusic/setup.py | 3 ++- tests/test_cli.py | 19 ++++++++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/spotify_to_ytmusic/setup.py b/spotify_to_ytmusic/setup.py index 51e6773..e0fba79 100644 --- a/spotify_to_ytmusic/setup.py +++ b/spotify_to_ytmusic/setup.py @@ -1,3 +1,4 @@ +import json import shutil import sys from pathlib import Path @@ -29,7 +30,7 @@ def setup(file: Optional[Path] = None): def setup_youtube(): settings = Settings() - settings["youtube"]["headers"] = ytmusicapi.setup_oauth() + settings["youtube"]["headers"] = json.dumps(ytmusicapi.setup_oauth()) settings.save() diff --git a/tests/test_cli.py b/tests/test_cli.py index a341371..f32006a 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -1,9 +1,12 @@ +import shutil import time import unittest from io import StringIO +from pathlib import Path from unittest import mock from spotify_to_ytmusic.main import get_args, main +from spotify_to_ytmusic.settings import Settings TEST_PLAYLIST = "https://open.spotify.com/playlist/4UzyZJfSQ4584FaWGwepfL" @@ -32,4 +35,18 @@ def test_create(self): with mock.patch("sys.argv", ["", "remove", "test"]), mock.patch('sys.stdout', new=StringIO()) as fakeOutput, mock.patch("builtins.input", side_effect="y"): main() - assert int(fakeOutput.getvalue().splitlines()[-1][0]) >= 2 + assert int(fakeOutput.getvalue().splitlines()[-1][0]) >= 2 # assert number of lines deleted + + def test_setup(self): + tmp_path = Path(__file__).parent.joinpath("settings.tmp") + example_path = Settings.filepath.parent.joinpath("settings.ini.example") + shutil.copy(example_path, tmp_path) + with mock.patch("sys.argv", ["", "setup"]), mock.patch("builtins.input", return_value="3"), mock.patch("spotify_to_ytmusic.settings.Settings.filepath", tmp_path): + main() + assert tmp_path.is_file() + tmp_path.unlink() + + with mock.patch("sys.argv", ["", "setup", "--file", example_path.as_posix()]), mock.patch("spotify_to_ytmusic.settings.Settings.filepath", tmp_path): + main() + assert tmp_path.is_file() + tmp_path.unlink()