fork/mons
git clone https://git.julia.blue/fork/mons
e96fbdd
fixes
1import json 2import os 3import pathlib 4from contextlib import contextmanager 5 6import click 7import pytest 8from click .testing import CliRunner 9 10from mons import downloading 11from mons import sources 12from mons .mons import cli as mons_cli 13 14 15def pytest_collection_modifyitems (session ,config ,items ): 16module = pathlib .Path (os .path .dirname (__file__ )) 17for item in items : 18if module == item .path .parent : 19item .add_marker (pytest .mark .integration_test ) 20 21 22def get_commands (cli ,* ,prefix = "" ): 23for cmd in cli .commands .values (): 24if hasattr (cmd ,"commands" ): 25yield from get_commands (cmd ,prefix = prefix + f" { cmd . name } " ) 26else : 27cmd .qualified_name = prefix + cmd .name 28yield cmd 29 30 31@ pytest . fixture ( 32scope = "module" , 33params = list(get_commands( mons_cli )), 34ids = lambda cmd : cmd . qualified_name , 35) 36def command (request ): 37yield request .param 38 39 40@ pytest . fixture ( scope = "function" ) 41def ctx (runner ): 42@ click . command 43def cli (): 44pass 45 46ctx = click .Context (cli ) 47with ctx : 48yield ctx 49 50 51@ pytest . fixture ( scope = "function" ) 52def runner (): 53""" 54runner.isolated_filesystem isn't necessary because mons never operates directly on the working directory 55but if it did: 56 57``` 58with runner.isolated_filesystem(temp_dir=tmp_path): 59yield runner 60``` 61""" 62return CliRunner () 63 64 65@ pytest . fixture 66def runner_result (runner ,assertion_msg ): 67@contextmanager 68def runner_result (* args ,** kwargs ): 69result = runner .invoke (* args ,** kwargs ) 70error_msg = "=" * 10 + "\nCOMMAND OUTPUT\n\n" + result .output + "=" * 10 71with assertion_msg (error_msg ): 72yield result 73 74return runner_result 75 76 77@ pytest . fixture ( autouse = True ) 78def mock_sources (monkeypatch ,request :pytest .FixtureRequest ): 79_open_url = downloading .open_url 80 81def mocked_open_url (request ,* args ,** kwargs ): 82if isinstance (request ,str )and request .startswith ("file://" ): 83return _open_url (request ,* args ,** kwargs ) 84pytest .fail ( 85f"Attempted to make a forbidden request. Currently patched: { implemented } " 86 ) 87 88implemented = list () 89for module in (downloading ,sources ): 90monkeypatch .setattr ( 91module , 92"open_url" , 93mocked_open_url , 94 ) 95 96data_path = os .path .join (os .path .dirname (request .path ),"_data" ) 97requesters = [attr for attr in dir (sources )if attr .startswith ("fetch_" )] 98 99for attr in requesters : 100module_patch = getattr (request .module ,attr ,None ) 101if module_patch : 102monkeypatch .setattr (sources ,attr ,module_patch ) 103implemented .append (attr ) 104elif os .path .exists (data_path )and attr [len ("fetch_" ) :]in os .listdir ( 105data_path 106 ): 107data_file = os .path .join (data_path ,attr [len ("fetch_" ) :]) 108 109def read_data_file (* args ,** kwargs ): 110with open (data_file )as f : 111return json .load (f ) 112 113monkeypatch .setattr (sources ,attr ,read_data_file ) 114implemented .append (attr )