def test_watchdog_app_paths(tmp): class MyApp(WatchdogApp): class Meta: watchdog_paths = [ (tmp.dir), (tmp.dir, WatchdogEventHandler) ] WatchdogEventHandler.on_any_event = Mock() with MyApp() as app: app.run() WatchdogEventHandler.on_any_event.reset_mock() # trigger an event f = open(fs.join(tmp.dir, 'test.file'), 'w') f.write('test data') f.close() time.sleep(1) # 3 separate calls, called twice each because tmp.dir appears # twice in the watchdog_paths list # File created # Dir modified # File modified assert WatchdogEventHandler.on_any_event.call_count == 6
def __init__(self, app): """Load or copy config file.""" self._config_file = fs.join(app.user_dir, self.FILE_NAME) if not os.path.exists(self._config_file): copy2(resource_path('regions.yml'), self._config_file) self._config = yaml_load(self._config_file)
def resource_path(*path_parts: str) -> str: """ Resolves path to a resource. """ if '..' in path_parts: raise ValueError('parent directory references are forbidden') tank_src = os.path.dirname(os.path.dirname(fs.abspath(__file__))) return fs.join(tank_src, 'resources', *path_parts)
def __init__(self, app): self._app = app self.config_file = fs.join(app.user_dir, 'bindings.yml') # TODO atomically if not os.path.exists(self.config_file): # setting up the default config copy2(resource_path('bindings.yml'), self.config_file) # TODO validate self._config = yaml_load(self.config_file)
def test_jinja2_filesystemloader(tmp, rando): with Jinja2App() as app: app._meta.template_dirs = [tmp.dir] # make sure it doesn't load from the tests directory module regardless app._meta.template_module = 'some.bogus.module.path' tests_dir = os.path.dirname(os.path.dirname(__file__)) from_file = fs.join(tests_dir, 'data', 'templates', 'test_template_parent.jinja2') to_file = fs.join(tmp.dir, 'test_template_parent.jinja2') copyfile(from_file, to_file) from_file = fs.join(tests_dir, 'data', 'templates', 'test_template_child.jinja2') to_file = fs.join(tmp.dir, 'test_template_child.jinja2') copyfile(from_file, to_file) res = app.render(dict(foo=rando), 'test_template_child.jinja2') jinja2_res = "foo equals %s\n" % rando assert res == jinja2_res
def test_watchdog_default_event_handler(tmp): WatchdogEventHandler.on_any_event = Mock() with WatchdogApp() as app: app.watchdog.add(tmp.dir) app.run() f = open(fs.join(tmp.dir, 'test.file'), 'w') f.write('test data') f.close() time.sleep(1) # 3 separate calls: # File created # Dir modified # File modified assert WatchdogEventHandler.on_any_event.call_count == 3
def test_watchdog(tmp): # The exception is getting raised, but for some reason it's not being # caught by a with raises() block, so I'm mocking it out instead. MyEventHandler.on_any_event = Mock() with WatchdogApp() as app: app.watchdog.add(tmp.dir, event_handler=MyEventHandler) app.run() file_path = fs.join(tmp.dir, 'test.file') # trigger an event f = open(file_path, 'w') f.write('test data') f.close() time.sleep(1) # 3 separate calls: # File created # Dir modified # File modified assert MyEventHandler.on_any_event.call_count == 3
def test_watchdog_app_paths(tmp): class MyApp(WatchdogApp): class Meta: watchdog_paths = [(tmp.dir), (tmp.dir, WatchdogEventHandler)] WatchdogEventHandler.on_any_event = Mock() with MyApp() as app: app.run() WatchdogEventHandler.on_any_event.reset_mock() # trigger an event f = open(fs.join(tmp.dir, 'test.file'), 'w') f.write('test data') f.close() time.sleep(1) # 3 separate calls, called twice each because tmp.dir appears # twice in the watchdog_paths list # File created # Dir modified # File modified assert WatchdogEventHandler.on_any_event.call_count == 6
def test_ensure_dir_exists(tmp, rando): fs.ensure_dir_exists(fs.join(tmp.dir, rando)) assert os.path.exists(fs.join(tmp.dir, rando)) with raises(AssertionError, match='(.*)exists but is not a directory(.*)'): fs.ensure_dir_exists(tmp.file)
def test_join(tmp, rando): full_path = os.path.abspath(os.path.join(tmp.dir, rando)) assert fs.join(tmp.dir, rando) == full_path
def user_dir(self) -> str: return fs.abspath(fs.join(pathlib.Path.home(), '.tank'))
def installation_dir(self) -> str: return fs.abspath(fs.join(self.user_dir, 'bin'))
def test_read_data(): filename = fs.join("..", "..", "README.md") with raises(SpiralError, match=".*Unrecognised extension.*"): read_data(filename)
def test_ensure_parent_dir_exists(tmp, rando): fs.ensure_parent_dir_exists(fs.join(tmp.dir, 'parent', rando)) assert os.path.exists(fs.join(tmp.dir, 'parent'))
from unittest.mock import patch from cement.utils.test import TestApp from cement.utils import fs CONFIG_PARSED = dict( section=dict( subsection=dict( list=['item1', 'item2', 'item3', 'item4'], key='value'), key1='ok1', key2='ok2', ), ) CONFIG = fs.join(os.path.dirname(__file__), '..', 'data', 'config', 'config.json') class JsonApp(TestApp): class Meta: extensions = ['json'] output_handler = 'json' config_handler = 'json' config_files = [CONFIG] argv = ['-o', 'json'] meta_defaults = {'output.json': {'overridable': True}} def test_json(): with JsonApp() as app: app.run()
import os # from dotenv import dotenv_values from cement import App, TestApp, init_defaults from cement.core.exc import CaughtSignal from cement.utils import fs from .core.exc import PivotalTrackerError from .controllers.base import Base from .controllers.pivotal import Pivotal # from pymongo import MongoClient CONFIG = fs.join(os.path.dirname(__file__), "..", "config", "pt.yml") # def load_variables(app): # app.log.info("Loading variables") # secrets = dotenv_values(fs.join(os.path.dirname(__file__), "..", "config", ".env")) # app.extend("secrets", secrets) class PivotalTracker(App): """Pivotal Tracker primary application.""" class Meta: label = "pt" config_files = [CONFIG] # call sys.exit() on close exit_on_close = True # load additional framework extensions extensions = ["yaml", "colorlog", "jinja2", "tabulate"]