fork/mons
git clone https://git.julia.blue/fork/mons
f0f1db8
main
1import os 2import typing as t 3import zipfile 4 5import pytest 6import yaml 7 8import mons .commands .mods 9import mons .install 10from mons .mons import cli as mons_cli 11 12 13@ pytest . fixture ( autouse = True ) 14def temp_mod_folder (monkeypatch ,tmp_path ): 15mod_folder = os .path .join (tmp_path ,"Mods" ) 16os .mkdir (mod_folder ) 17monkeypatch .setattr (mons .install .Install ,"mod_folder" ,mod_folder ) 18return mod_folder 19 20 21mod_db = {} 22dep_graph = {} 23installed_mods :t .Dict [str ,t .Dict [str ,str ]]= {} 24 25 26@ pytest . fixture ( autouse = True ) 27def fake_mod_db (monkeypatch ,temp_mod_folder ): 28monkeypatch .setattr (mons .commands .mods ,"fetch_mod_db" ,lambda * args :mod_db ) 29monkeypatch .setattr ( 30mons .commands .mods ,"fetch_dependency_graph" ,lambda * args :dep_graph 31 ) 32 33 34def fake_mod_db_request (request ,temp_mod_folder ,monkeypatch ): 35monkeypatch .setattr (mons .commands .mods ,"fetch_mod_db" ,lambda * args :request .param ) 36 37 38@ pytest . fixture 39def fake_mods (temp_mod_folder ): 40for filename ,data in installed_mods .items (): 41mod_file = os .path .join (temp_mod_folder ,filename ) 42with zipfile .ZipFile (mod_file ,"w" )as zip : 43for filename ,filedata in data .items (): 44zip .writestr (filename ,filedata ) 45return None 46 47 48@ pytest . fixture 49def fake_mods_request (request ,temp_mod_folder ): 50mods = request .param 51for mod in mods : 52filepath = os .path .join (temp_mod_folder ,f" { mod } .zip" ) 53with zipfile .ZipFile (filepath ,"w" )as zip : 54zip .writestr ( 55"everest.yaml" , 56yaml .dump ( 57 [ 58 { 59"Name" :mod , 60"Version" :"2.2.2" , 61 } 62 ] 63 ), 64 ) 65return mods 66 67 68mod_db .update ( 69 { 70"Installed" : { 71"URL" :"" , 72"Version" :"1.0.0" , 73"GameBananaId" :12345 , 74"LastUpdate" :0 , 75 }, 76"Outdated" : { 77"URL" :"" , 78"Version" :"1.0.0" , 79"GameBananaId" :12345 , 80"LastUpdate" :0 , 81 }, 82"Missing" : { 83"URL" :"" , 84"Version" :"1.0.0" , 85"GameBananaId" :12345 , 86"LastUpdate" :0 , 87 }, 88 } 89) 90 91 92def test_mods_add_none (runner_result ,test_install ): 93with runner_result (mons_cli , ["mods" ,"add" ,test_install ,"--yes" ])as result : 94assert result .exit_code == 0 95assert "No mods to add." in result .output 96 97 98@ pytest . mark . parametrize ( 99"data_file_zip" , 100[ 101{ 102"everest.yaml" : yaml . dump ( 103[ 104{ 105"Name" : "TestMod" , 106"Version" : "1.0.0" , 107} 108] 109) 110} 111], 112indirect = ( "data_file_zip" ,), 113) 114def test_mods_add_zip (runner_result ,test_install ,data_file_zip ,temp_mod_folder ): 115with runner_result ( 116mons_cli , ["mods" ,"add" ,test_install ,* data_file_zip ,"--yes" ] 117 )as result : 118assert result .exit_code == 0 119assert "TestMod" in result .output 120assert "Installed mods" in result .output 121 122assert os .path .isfile (os .path .join (temp_mod_folder ,"TestMod.zip" )),os .listdir ( 123temp_mod_folder 124 ) 125 126 127@ pytest . mark . data_file_zip ( 128{ 129"everest.yaml" : yaml . dump ( 130[ 131{ 132"Name" : "TestMod" , 133"Version" : "1.0.0" , 134} 135] 136) 137} 138) 139def test_mods_add_stdin (runner_result ,test_install ,data_file_zip ,temp_mod_folder ): 140with open (data_file_zip [0 ])as stdin ,runner_result ( 141mons_cli , ["mods" ,"add" ,test_install ,"-" ,"--yes" ],input = stdin 142 )as result : 143assert result .exit_code == 0 144assert "TestMod" in result .output 145assert "Installed mods" in result .output 146 147assert os .path .isfile (os .path .join (temp_mod_folder ,"TestMod.zip" )),os .listdir ( 148temp_mod_folder 149 ) 150 151 152@ pytest . mark . data_file_zip ( 153{ 154"everest.yaml" : yaml . dump ( 155[ 156{ 157"Name" : "TestMod" , 158"Version" : "1.0.0" , 159} 160] 161) 162} 163) 164def test_mods_add_blacklisted ( 165runner_result ,test_install ,data_file_zip ,temp_mod_folder 166): 167blacklist = os .path .join (temp_mod_folder ,"blacklist.txt" ) 168with open (blacklist ,"w" )as blacklist_file : 169blacklist_file .write ("TestMod.zip" ) 170 171with runner_result ( 172mons_cli , ["mods" ,"add" ,test_install ,* data_file_zip ,"--yes" ] 173 )as result : 174assert result .exit_code == 0 175assert ( 176"The following mods will be automatically removed from the blacklist" 177in result .output 178 ) 179 180with open (blacklist )as blacklist_file : 181contents = blacklist_file .read () 182assert "#TestMod.zip" in contents ,contents 183 184 185@ pytest . mark . data_file_zip ( 186{ 187"everest.yaml" : yaml . dump ( 188[ 189{ 190"Name" : "TestMod" , 191"Version" : "1.0.0" , 192} 193] 194) 195} 196) 197def test_mods_add_skip_unzipped ( 198runner_result ,test_install ,data_file_zip ,temp_mod_folder 199): 200mod_dir = os .path .join (temp_mod_folder ,"TestMod" ) 201os .mkdir (mod_dir ) 202with open (os .path .join (mod_dir ,"everest.yaml" ),"w" )as testmodyaml : 203testmodyaml .write (yaml .dump ([{"Name" :"TestMod" ,"Version" :"1.0.0" }])) 204 205with runner_result ( 206mons_cli , ["mods" ,"add" ,test_install ,* data_file_zip ,"--yes" ] 207 )as result : 208assert result .exit_code == 0 209assert "Unzipped mods will not be updated" in result .output 210 211 212@ pytest . mark . data_file_zip ({ "everest.yaml" : yaml . dump ( "hi!" )}) 213def test_mods_add_invalid_mod ( 214runner_result ,test_install ,data_file_zip ,temp_mod_folder 215): 216with runner_result ( 217mons_cli , 218 ["mods" ,"add" ,test_install ,f"file:// { data_file_zip [ 0 ] } " ,"--force" ], 219input = "NotAMod.zip\n" , 220 )as result : 221assert result .exit_code == 0 222assert "The following mods could not be resolved" in result .output 223 224mod_added = os .path .isfile (os .path .join (temp_mod_folder ,"NotAMod.zip" )) 225assert mod_added ,os .listdir (temp_mod_folder ) 226 227 228installed_mods .update ( 229 { 230"Remove.zip" : { 231"everest.yaml" :yaml .dump ( 232 [ 233 { 234"Name" :"Remove" , 235"Version" :"1.0.0" , 236 } 237 ] 238 ) 239 }, 240"Remove_Recurse.zip" : { 241"everest.yaml" :yaml .dump ( 242 [ 243 { 244"Name" :"Remove_Recurse" , 245"Version" :"1.0.0" , 246"Dependencies" : [ 247 {"Name" :"Remove_Recurse_Dep" ,"Version" :"1.0.0" }, 248 {"Name" :"Remove_Recurse_Shared" ,"Version" :"1.0.0" }, 249 ], 250 } 251 ] 252 ) 253 }, 254"Remove_Recurse_Dep.zip" : { 255"everest.yaml" :yaml .dump ( 256 [ 257 { 258"Name" :"Remove_Recurse_Dep" , 259"Version" :"1.0.0" , 260 } 261 ] 262 ) 263 }, 264"Remove_Recurse_Shared.zip" : { 265"everest.yaml" :yaml .dump ( 266 [ 267 { 268"Name" :"Remove_Recurse_Shared" , 269"Version" :"1.0.0" , 270 } 271 ] 272 ) 273 }, 274"Remove_Other.zip" : { 275"everest.yaml" :yaml .dump ( 276 [ 277 { 278"Name" :"Remove_Other" , 279"Version" :"1.0.0" , 280"Dependencies" : [ 281 {"Name" :"Remove_Recurse_Shared" ,"Version" :"1.0.0" } 282 ], 283 } 284 ] 285 ) 286 }, 287 } 288) 289 290 291def test_mods_remove (runner_result ,test_install ,temp_mod_folder ,fake_mods ): 292with runner_result ( 293mons_cli , ["mods" ,"remove" ,test_install ,"Remove" ,"--force" ] 294 )as result : 295assert result .exit_code == 0 296 297assert not os .path .exists (os .path .join (temp_mod_folder ,"Remove.zip" )),os .listdir ( 298temp_mod_folder 299 ) 300 301 302def test_mods_remove_recurse (runner_result ,test_install ,temp_mod_folder ,fake_mods ): 303with runner_result ( 304mons_cli , 305 ["mods" ,"remove" ,test_install ,"--recurse" ,"Remove_Recurse" ,"--force" ], 306 )as result : 307assert result .exit_code == 0 308 309for filename in ["Remove_Recurse.zip" ,"Remove_Recurse_Dep.zip" ]: 310filepath = os .path .join (temp_mod_folder ,filename ) 311assert not os .path .exists (filepath ),os .listdir (temp_mod_folder ) 312 313filepath = os .path .join (temp_mod_folder ,"Remove_Recurse_Shared.zip" ) 314assert os .path .exists (filepath ),os .listdir (temp_mod_folder ) 315 316 317def test_mods_remove_none (runner_result ,test_install ,temp_mod_folder ,fake_mods ): 318with runner_result ( 319mons_cli , ["mods" ,"remove" ,test_install ,"Non-Existent-Mod" ,"--force" ] 320 )as result : 321assert result .exit_code == 0 322assert "No mods to remove." in result .output 323 324 325def test_mods_update (runner_result ,test_install ,temp_mod_folder ,fake_mods ): 326with runner_result (mons_cli , ["mods" ,"update" ,test_install ])as result : 327assert result .exit_code == 0 328assert "All mods up to date" in result .output 329 330 331def test_mods_list (runner_result ,test_install ,temp_mod_folder ,fake_mods ): 332with runner_result (mons_cli , ["mods" ,"list" ,test_install ])as result : 333assert result .exit_code == 0 334# strip .zip from filenames 335assert all (mod [:- 4 ]in result .output for mod in installed_mods .keys ())