fork/mons
git clone https://git.julia.blue/fork/mons
c43b1d6
misc-fixes
1import argparse 2import os 3import sys 4import urllib .request 5import zipfile 6from contextlib import contextmanager 7 8import pytest 9 10from mons .install import find_celeste_asm 11from mons .version import Version 12 13PLATFORM_MARKS = set ("darwin linux win32" .split ()) 14 15 16def path_exists (path ): 17if os .path .exists (path ): 18try : 19return find_celeste_asm (path ) 20except FileNotFoundError as e : 21raise argparse .ArgumentTypeError (e ) 22raise argparse .ArgumentTypeError (f"Path { path } does not exist." ) 23 24 25def pytest_addoption (parser :pytest .Parser ,pluginmanager ): 26parser .addoption ( 27"--mons-test-install" ,"--mons" ,type = path_exists ,dest = "mons_test_install" 28 ) 29 30 31def pytest_configure (config :pytest .Config ): 32for plat in PLATFORM_MARKS : 33config .addinivalue_line ( 34"markers" ,f" { plat } : mark this test as platform-specific" 35 ) 36config .addinivalue_line ("markers" ,"prioritize: prioritize this test" ) 37config .addinivalue_line ( 38"markers" ,"must_pass: this test must pass in order to continue" 39 ) 40config .addinivalue_line ( 41"markers" ,"data_file_zip: pass arguments to the data_file_zip fixture" 42 ) 43config .addinivalue_line ( 44"markers" , 45"data_file_download: pass arguments to the data_file_download fixture" , 46 ) 47config .addinivalue_line ( 48"markers" , 49"mock_filesystem: pass arguments to the mock_filesystem fixture" , 50 ) 51 52 53@ pytest . hookimpl ( tryfirst = True ) # pyright:ignore[reportUntypedFunctionDecorator] 54def pytest_collection_modifyitems (session ,config ,items ): 55items .sort (key = lambda i :0 if i .get_closest_marker ("prioritize" )else 1 ) 56 57 58must_pass_failed = None 59 60 61# https://stackoverflow.com/a/59392344 62def pytest_runtest_makereport (item ,call ): 63global must_pass_failed 64if not must_pass_failed and item .get_closest_marker (name = "must_pass" ): 65if call .excinfo is not None : 66must_pass_failed = item 67 68 69def pytest_runtest_setup (item ): 70# must_pass test checks 71if must_pass_failed is not None : 72pytest .skip (f"must_pass test failed ( { must_pass_failed . name } )" ) 73 74# platform-specific test checks 75supported_platforms = PLATFORM_MARKS .intersection ( 76mark .name for mark in item .iter_markers () 77 ) 78plat = sys .platform 79if supported_platforms and plat not in supported_platforms : 80pytest .skip ("cannot run on platform {}" .format (plat )) 81 82 83def pytest_make_parametrize_id (config ,val ,argname ): 84if isinstance (val ,Version ): 85return str (val ) 86 87 88@ pytest . fixture 89def data_file (request ,tmp_path ): 90data = request .param 91data_file = os .path .join (tmp_path ,"data_file" ) 92 93# indicator for missing file 94if data is None : 95yield data_file 96return 97 98with open (data_file ,"w" )as file : 99file .write (data ) 100yield data_file 101 102 103@ pytest . fixture 104def data_file_zip (request :pytest .FixtureRequest ,tmp_path ): 105marker = request .node .get_closest_marker ("data_file_zip" ) 106data = marker .args [0 ]if marker else request .param 107filenames = [] 108for i ,file in enumerate (data if isinstance (data ,list )else [data ]): 109filenames .append (os .path .join (tmp_path ,f"data_file_ { i } .zip" )) 110# indicator for missing file 111if data is None : 112continue 113 114with zipfile .ZipFile (filenames [i ],"w" )as zip : 115for filename ,filedata in file .items (): 116zip .writestr (filename ,filedata ) 117yield tuple (filenames ) 118 119 120@ pytest . fixture 121def data_file_download (request :pytest .FixtureRequest ,cache ): 122marker = request .node .get_closest_marker ("data_file_download" ) 123url ,filename = marker .args if marker else request .param 124dest = os .path .join (cache .mkdir ("mons_" + request .module .__name__ ),filename ) 125if not os .path .exists (dest ): 126urllib .request .urlretrieve (url ,dest ) 127assert os .path .isfile (dest ),"File download failed" 128yield dest 129 130 131@ pytest . fixture 132def test_name (request ): 133yield request .node .name 134 135 136@ pytest . fixture ( autouse = True ) 137def assertion_msg (): 138@contextmanager 139def assertion_msg (msg :str ): 140try : 141yield 142except AssertionError as e : 143e .args = (e .args [0 ]+ "\n" + msg ,) 144raise 145 146return assertion_msg 147 148 149@ pytest . fixture () 150def mock_filesystem (request ,tmp_path ): 151marker = request .node .get_closest_marker ("mock_filesystem" ) 152if marker : 153mockup = marker .args [0 ] 154walk = marker .kwargs .get ("walk" ,False ) 155else : 156mockup = request .param 157walk = False 158root = os .path .join (tmp_path ,"mock_fs" ) 159os .mkdir (root ) 160 161if not mockup : 162yield root 163return 164 165paths = list () 166 167def create_fs (root ,mockup ): 168if isinstance (mockup ,dict ): 169for dir ,contents in mockup .items (): 170path = os .path .join (root ,dir ) 171os .mkdir (path ) 172paths .append (path ) 173create_fs (path ,contents ) 174elif isinstance (mockup , (list ,tuple )): 175for node in mockup : 176create_fs (root ,node ) 177else : 178assert isinstance (mockup ,str ) 179path = os .path .join (root ,mockup ) 180if mockup .endswith ("/" ): 181os .mkdir (path ) 182else : 183with open (path ,"x" ): 184pass 185paths .append (path ) 186 187create_fs (root ,mockup ) 188 189# set consistent last modified/accessed times for created paths 190for i ,path in enumerate (paths ): 191os .utime (path , (i ,i )) 192 193yield os .walk (root )if walk else root