21
21
import subprocess
22
22
import sys
23
23
import unittest
24
+ from pathlib import Path
24
25
from unittest .mock import Mock , patch
25
26
26
27
import pytest
@@ -828,37 +829,29 @@ def setUp(self):
828
829
del os .environ ["BEETSDIR" ]
829
830
830
831
# Also set APPDATA, the Windows equivalent of setting $HOME.
831
- appdata_dir = os .fsdecode (
832
- os .path .join (self .temp_dir , b"AppData" , b"Roaming" )
833
- )
832
+ appdata_dir = self .temp_dir_path / "AppData" / "Roaming"
834
833
835
834
self ._orig_cwd = os .getcwd ()
836
835
self .test_cmd = self ._make_test_cmd ()
837
836
commands .default_commands .append (self .test_cmd )
838
837
839
838
# Default user configuration
840
839
if platform .system () == "Windows" :
841
- self .user_config_dir = os .fsencode (
842
- os .path .join (appdata_dir , "beets" )
843
- )
840
+ self .user_config_dir = appdata_dir / "beets"
844
841
else :
845
- self .user_config_dir = os .path .join (
846
- self .temp_dir , b".config" , b"beets"
847
- )
848
- os .makedirs (syspath (self .user_config_dir ))
849
- self .user_config_path = os .path .join (
850
- self .user_config_dir , b"config.yaml"
851
- )
842
+ self .user_config_dir = self .temp_dir_path / ".config" / "beets"
843
+ self .user_config_dir .mkdir (parents = True , exist_ok = True )
844
+ self .user_config_path = self .user_config_dir / "config.yaml"
852
845
853
846
# Custom BEETSDIR
854
- self .beetsdir = os . path . join ( self .temp_dir , b "beetsdir")
855
- self .cli_config_path = os . path . join (
856
- os . fsdecode ( self . temp_dir ), "config.yaml"
857
- )
858
- os . makedirs ( syspath ( self .beetsdir ) )
847
+ self .beetsdir = self .temp_dir_path / "beetsdir"
848
+ self .beetsdir . mkdir ( parents = True , exist_ok = True )
849
+
850
+ self . env_config_path = str ( self . beetsdir / "config.yaml" )
851
+ self . cli_config_path = str ( self .temp_dir_path / "config.yaml" )
859
852
self .env_patcher = patch (
860
853
"os.environ" ,
861
- {"HOME" : os . fsdecode (self .temp_dir ), "APPDATA" : appdata_dir },
854
+ {"HOME" : str (self .temp_dir_path ), "APPDATA" : str ( appdata_dir ) },
862
855
)
863
856
self .env_patcher .start ()
864
857
@@ -957,9 +950,8 @@ def test_cli_config_file_overwrites_user_defaults(self):
957
950
assert config ["anoption" ].get () == "cli overwrite"
958
951
959
952
def test_cli_config_file_overwrites_beetsdir_defaults (self ):
960
- os .environ ["BEETSDIR" ] = os .fsdecode (self .beetsdir )
961
- env_config_path = os .path .join (self .beetsdir , b"config.yaml" )
962
- with open (env_config_path , "w" ) as file :
953
+ os .environ ["BEETSDIR" ] = str (self .beetsdir )
954
+ with open (self .env_config_path , "w" ) as file :
963
955
file .write ("anoption: value" )
964
956
965
957
with open (self .cli_config_path , "w" ) as file :
@@ -1006,39 +998,25 @@ def test_cli_config_paths_resolve_relative_to_user_dir(self):
1006
998
file .write ("statefile: state" )
1007
999
1008
1000
self .run_command ("--config" , self .cli_config_path , "test" , lib = None )
1009
- self .assert_equal_path (
1010
- util .bytestring_path (config ["library" ].as_filename ()),
1011
- os .path .join (self .user_config_dir , b"beets.db" ),
1012
- )
1013
- self .assert_equal_path (
1014
- util .bytestring_path (config ["statefile" ].as_filename ()),
1015
- os .path .join (self .user_config_dir , b"state" ),
1016
- )
1001
+ assert config ["library" ].as_path () == self .user_config_dir / "beets.db"
1002
+ assert config ["statefile" ].as_path () == self .user_config_dir / "state"
1017
1003
1018
1004
def test_cli_config_paths_resolve_relative_to_beetsdir (self ):
1019
- os .environ ["BEETSDIR" ] = os . fsdecode (self .beetsdir )
1005
+ os .environ ["BEETSDIR" ] = str (self .beetsdir )
1020
1006
1021
1007
with open (self .cli_config_path , "w" ) as file :
1022
1008
file .write ("library: beets.db\n " )
1023
1009
file .write ("statefile: state" )
1024
1010
1025
1011
self .run_command ("--config" , self .cli_config_path , "test" , lib = None )
1026
- self .assert_equal_path (
1027
- util .bytestring_path (config ["library" ].as_filename ()),
1028
- os .path .join (self .beetsdir , b"beets.db" ),
1029
- )
1030
- self .assert_equal_path (
1031
- util .bytestring_path (config ["statefile" ].as_filename ()),
1032
- os .path .join (self .beetsdir , b"state" ),
1033
- )
1012
+ assert config ["library" ].as_path () == self .beetsdir / "beets.db"
1013
+ assert config ["statefile" ].as_path () == self .beetsdir / "state"
1034
1014
1035
1015
def test_command_line_option_relative_to_working_dir (self ):
1036
1016
config .read ()
1037
1017
os .chdir (syspath (self .temp_dir ))
1038
1018
self .run_command ("--library" , "foo.db" , "test" , lib = None )
1039
- self .assert_equal_path (
1040
- config ["library" ].as_filename (), os .path .join (os .getcwd (), "foo.db" )
1041
- )
1019
+ assert config ["library" ].as_path () == Path .cwd () / "foo.db"
1042
1020
1043
1021
def test_cli_config_file_loads_plugin_commands (self ):
1044
1022
with open (self .cli_config_path , "w" ) as file :
@@ -1050,24 +1028,23 @@ def test_cli_config_file_loads_plugin_commands(self):
1050
1028
self .unload_plugins ()
1051
1029
1052
1030
def test_beetsdir_config (self ):
1053
- os .environ ["BEETSDIR" ] = os . fsdecode (self .beetsdir )
1031
+ os .environ ["BEETSDIR" ] = str (self .beetsdir )
1054
1032
1055
- env_config_path = os .path .join (self .beetsdir , b"config.yaml" )
1056
- with open (env_config_path , "w" ) as file :
1033
+ with open (self .env_config_path , "w" ) as file :
1057
1034
file .write ("anoption: overwrite" )
1058
1035
1059
1036
config .read ()
1060
1037
assert config ["anoption" ].get () == "overwrite"
1061
1038
1062
1039
def test_beetsdir_points_to_file_error (self ):
1063
- beetsdir = os . path . join (self .temp_dir , b "beetsfile" )
1040
+ beetsdir = str (self .temp_dir_path / "beetsfile" )
1064
1041
open (beetsdir , "a" ).close ()
1065
- os .environ ["BEETSDIR" ] = os . fsdecode ( beetsdir )
1042
+ os .environ ["BEETSDIR" ] = beetsdir
1066
1043
with pytest .raises (ConfigError ):
1067
1044
self .run_command ("test" )
1068
1045
1069
1046
def test_beetsdir_config_does_not_load_default_user_config (self ):
1070
- os .environ ["BEETSDIR" ] = os . fsdecode (self .beetsdir )
1047
+ os .environ ["BEETSDIR" ] = str (self .beetsdir )
1071
1048
1072
1049
with open (self .user_config_path , "w" ) as file :
1073
1050
file .write ("anoption: value" )
@@ -1076,35 +1053,22 @@ def test_beetsdir_config_does_not_load_default_user_config(self):
1076
1053
assert not config ["anoption" ].exists ()
1077
1054
1078
1055
def test_default_config_paths_resolve_relative_to_beetsdir (self ):
1079
- os .environ ["BEETSDIR" ] = os . fsdecode (self .beetsdir )
1056
+ os .environ ["BEETSDIR" ] = str (self .beetsdir )
1080
1057
1081
1058
config .read ()
1082
- self .assert_equal_path (
1083
- util .bytestring_path (config ["library" ].as_filename ()),
1084
- os .path .join (self .beetsdir , b"library.db" ),
1085
- )
1086
- self .assert_equal_path (
1087
- util .bytestring_path (config ["statefile" ].as_filename ()),
1088
- os .path .join (self .beetsdir , b"state.pickle" ),
1089
- )
1059
+ assert config ["library" ].as_path () == self .beetsdir / "library.db"
1060
+ assert config ["statefile" ].as_path () == self .beetsdir / "state.pickle"
1090
1061
1091
1062
def test_beetsdir_config_paths_resolve_relative_to_beetsdir (self ):
1092
- os .environ ["BEETSDIR" ] = os . fsdecode (self .beetsdir )
1063
+ os .environ ["BEETSDIR" ] = str (self .beetsdir )
1093
1064
1094
- env_config_path = os .path .join (self .beetsdir , b"config.yaml" )
1095
- with open (env_config_path , "w" ) as file :
1065
+ with open (self .env_config_path , "w" ) as file :
1096
1066
file .write ("library: beets.db\n " )
1097
1067
file .write ("statefile: state" )
1098
1068
1099
1069
config .read ()
1100
- self .assert_equal_path (
1101
- util .bytestring_path (config ["library" ].as_filename ()),
1102
- os .path .join (self .beetsdir , b"beets.db" ),
1103
- )
1104
- self .assert_equal_path (
1105
- util .bytestring_path (config ["statefile" ].as_filename ()),
1106
- os .path .join (self .beetsdir , b"state" ),
1107
- )
1070
+ assert config ["library" ].as_path () == self .beetsdir / "beets.db"
1071
+ assert config ["statefile" ].as_path () == self .beetsdir / "state"
1108
1072
1109
1073
1110
1074
class ShowModelChangeTest (IOMixin , unittest .TestCase ):
0 commit comments