def test_tickets(monkeypatch, capsys): monkeypatch.setattr(MockContext, 'run', mock_run) c = MockContext() monkeypatch.setattr(session, 'get_regfile_name', lambda x: '') session.tickets(c, 'project') assert capsys.readouterr().out == 'wrong project name\n' monkeypatch.setattr(session, 'get_regfile_name', lambda x: '/tmp/regfile_test') monkeypatch.setattr(session.os.path, 'exists', lambda x: False) session.tickets(c, 'project') assert capsys.readouterr().out == "tickets I'm working on: none\n" monkeypatch.setattr(session.os.path, 'exists', lambda x: True) with open('/tmp/regfile_test', 'w') as f: f.write("1\n2\n3") session.tickets(c, 'project') assert capsys.readouterr().out == "tickets I'm working on: 1, 2, 3\n"
def test_dump_mongo(monkeypatch, capsys): def mock_mkdir(self, *args, **kwargs): print('called mkdir with args', args, kwargs) monkeypatch.setattr(db.pathlib.Path, 'mkdir', mock_mkdir) monkeypatch.setattr(db.datetime, 'datetime', MockDatetime) monkeypatch.setattr(MockContext, 'run', mock_run) c = MockContext() db.dump_mongo(c) assert capsys.readouterr().out == ( "called mkdir with args () {'parents': True, 'exist_ok': True}\n" 'mongodump -o ~/mongodump/20200101-000000/\n') db.dump_mongo(c, 'database') assert capsys.readouterr().out == ( "called mkdir with args () {'parents': True, 'exist_ok': True}\n" 'mongodump -d database -o ~/mongodump/20200101-000000/\n')
def test_it_runs_federalist_script_when_it_exists(self, patch_clone_dir): package_json_contents = json.dumps({ 'scripts': { 'federalist': 'echo hi', }, }) create_file(patch_clone_dir / PACKAGE_JSON, package_json_contents) ctx = MockContext(run={ 'npm run federalist': Result(), }) tasks.run_federalist_script(ctx, branch='branch', owner='owner', repository='repo', site_prefix='site/prefix', base_url='/site/prefix')
def test_it_is_callable(self, s3_conn): ctx = MockContext() # Create mock for default 404 page request with requests_mock.mock() as m: m.get(('https://raw.githubusercontent.com' '/18F/federalist-404-page/master/' '404-federalist-client.html'), text='default 404 page') publish(ctx, base_url='/site/prefix', site_prefix='site/prefix', bucket=TEST_BUCKET, cache_control='max-age: boop', aws_region=TEST_REGION)
def test_it_is_callable(self, patch_clone_dir): ctx = MockContext(run=[ Result('gem install jekyll result'), Result('jekyll version result'), Result('jekyll build result'), ]) contents = 'hi: test' create_file(patch_clone_dir / JEKYLL_CONFIG_YML, contents) tasks.build_jekyll(ctx, branch='branch', owner='owner', repository='repo', site_prefix='site/prefix', config='boop: beep', base_url='/site/prefix')
def test_repo_overzicht(monkeypatch, capsys): def mock_repolist_hg(*args): print('called make_repolist_hg()') return {'.hg': 'outdict_hg'} def mock_repolist_git(*args): print('called make_repolist_git()') return {'.git': 'outdict_git'} def mock_repo_ovz(*args): print('called make_repo_ovz with args', args) def mock_repocsv(*args): print('called make_repocsv with args', args) monkeypatch.setattr(repo, 'make_repolist_hg', mock_repolist_hg) monkeypatch.setattr(repo, 'make_repolist_git', mock_repolist_git) monkeypatch.setattr(repo, 'make_repo_ovz', mock_repo_ovz) monkeypatch.setattr(repo, 'make_repocsv', mock_repocsv) monkeypatch.setattr(MockContext, 'run', run_in_dir) c = MockContext() monkeypatch.setattr(repo.os, 'listdir', lambda x: ['oink']) monkeypatch.setattr(repo.os.path, 'isdir', lambda x: False) assert repo.repo_overzicht(c, 'name', 'path/to/repo', 'txt') is None monkeypatch.setattr(repo.os.path, 'isdir', lambda x: True) assert repo.repo_overzicht(c, 'name', 'path/to/repo', 'txt') is None # assert capsys.readouterr().out == "" monkeypatch.setattr(repo.os, 'listdir', lambda x: ['.hg']) assert repo.repo_overzicht(c, 'name', 'path/to/repo', 'txt') == 'path/to/.overzicht' assert capsys.readouterr().out == ( "called make_repolist_hg()\n" "called make_repo_ovz with args ({'.hg': 'outdict_hg'}," " 'path/to/.overzicht/name_repo.ovz')\n") monkeypatch.setattr(repo.os, 'listdir', lambda x: ['.git']) assert repo.repo_overzicht(c, 'name', 'path/to/repo', 'txt') == 'path/to/.overzicht' assert capsys.readouterr().out == ( "called make_repolist_git()\n" "called make_repo_ovz with args ({'.git': 'outdict_git'}," " 'path/to/.overzicht/name_repo.ovz')\n") assert repo.repo_overzicht(c, 'name', 'path/to/repo', 'csv') == 'path/to/.overzicht' assert capsys.readouterr().out == ( "called make_repolist_git()\n" "called make_repocsv with args ({'.git': 'outdict_git'}," " 'path/to/.overzicht/name_repo.csv')\n")
def test_dump_pg(monkeypatch, capsys): def mock_mkdir(self, *args, **kwargs): print('called mkdir with args', args, kwargs) monkeypatch.setattr(db.pathlib.Path, 'mkdir', mock_mkdir) monkeypatch.setattr(db.datetime, 'datetime', MockDatetime) monkeypatch.setattr(MockContext, 'run', mock_run) c = MockContext() db.dump_pg(c, '') assert capsys.readouterr().out == ( "called mkdir with args () {'parents': True, 'exist_ok': True}\n" 'pg_dumpall -f ~/pgdump/20200101/all_000000.sql\n') db.dump_pg(c, 'database') assert capsys.readouterr().out == ( "called mkdir with args () {'parents': True, 'exist_ok': True}\n" 'pg_dump database -f ~/pgdump/20200101/database_000000.sql\n')
def repeat_True_does_not_consume_results(self): mc = MockContext( repeat=True, run=dict( singleton=True, # will repeat wassup=Result("yo"), # ditto iterable=[Result("tick"), Result("tock")], # will not ), ) assert mc.run("singleton").ok assert mc.run("singleton").ok # not consumed assert mc.run("wassup").ok assert mc.run("wassup").ok # not consumed assert mc.run("iterable").stdout == "tick" assert mc.run("iterable").stdout == "tock" assert mc.run("iterable").stdout == "tick" # not consumed assert mc.run("iterable").stdout == "tock"
def test_check_local(monkeypatch, capsys): monkeypatch.setattr(MockContext, 'run', mock_run) c = MockContext() monkeypatch.setattr(repo, '_check', mock_check_nok) repo.check_local(c) assert capsys.readouterr( ).out == "call _check() with args () {'dry_run': False}\n" monkeypatch.setattr(repo, '_check', mock_check_ok) repo.check_local(c) assert capsys.readouterr().out == ( "call _check() with args () {'dry_run': False}\n" "use 'check-repo <reponame>' to inspect changes\n" " 'binfab repo.check-local-notes` for remarks\n") monkeypatch.setattr(repo, '_check', mock_check) repo.check_local(c, dry_run=True) assert capsys.readouterr( ).out == "call _check() with args () {'dry_run': True}\n"
def test_it_is_callable(self): ctx = MockContext(run=[ Result('tar result'), Result('chmod result'), Result('hugo version result'), Result('hugo build result'), ]) with requests_mock.Mocker() as m: m.get('https://github.com/gohugoio/hugo/releases/download' '/v0.23/hugo_0.23_Linux-64bit.tar.gz') build_hugo(ctx, branch='branch', owner='owner', repository='repo', site_prefix='site/prefix', base_url='/site/prefix', hugo_version='0.23')
def test_start(monkeypatch, capsys): def mock_report(*args): print('called report_result') monkeypatch.setattr(tasks_cherrypy, '_get_cherry_parms', mock_get_parms) monkeypatch.setattr(tasks_cherrypy, 'report_result', mock_report) monkeypatch.setattr(MockContext, 'run', mock_run) c = MockContext() tasks_cherrypy.start(c) assert capsys.readouterr().out == ( 'sudo /usr/bin/cherryd -c conf -d -p pid -i prog\n' 'called report_result\n' 'sudo /usr/bin/cherryd -c conf -d -p pid -i prog\n' 'called report_result\n') tasks_cherrypy.start(c, 'name') assert capsys.readouterr().out == ( 'sudo /usr/bin/cherryd -c conf -d -p pid -i prog\n' 'called report_result\n')
def test_it_is_callable(self, fs): ctx = MockContext(run=[ Result('gem install jekyll result'), Result('jekyll version result'), Result('jekyll build result'), ]) with Patcher() as patcher: patcher.fs.CreateFile('/tmp/site_repo/_config.yml', contents='hi: test') build_jekyll(ctx, branch='branch', owner='owner', repository='repo', site_prefix='site/prefix', config='boop: beep', base_url='/site/prefix')
def test_with_failing_task_raises_exception(self, capsys): @task def my_cleanup(ctx): print("CALLED: my_cleanup task (entered)") raise RuntimeError("OOPS: my_cleanup fails") print("CALLED: my_cleanup task (exited)") cleanup_tasks = Collection("cleanup_tasks") cleanup_tasks.add_task(my_cleanup, name="my_cleanup") ctx = MockContext() with pytest.raises(RuntimeError, match="OOPS: my_cleanup fails"): execute_cleanup_tasks(ctx, cleanup_tasks) captured = capsys.readouterr() expected = "CLEANUP TASK: my-cleanup" assert expected in captured.out assert "CALLED: my_cleanup task (entered)" in captured.out assert "CALLED: my_cleanup task (exited)" not in captured.out
def test_prep(monkeypatch, capsys): def mock_get_name(*args): print('called get_project_name() with args', *args) return 'project_name' def mock_get_dir(*args): print('called get_project_dir() with args', *args) return 'project_dir' monkeypatch.setattr(session, 'get_project_name', mock_get_name) monkeypatch.setattr(session, 'get_project_dir', mock_get_dir) monkeypatch.setattr(session, 'DEVEL', 'devpath') monkeypatch.setattr(MockContext, 'run', run_in_dir) c = MockContext() session.prep(c, '111') assert capsys.readouterr().out == ( 'called get_project_name() with args 111\n' 'called get_project_dir() with args project_name\n' 'hg incoming -v devpath/_111 in project_dir\n')
def test_restart(monkeypatch, capsys): def mock_stop(*args): print('called cherrypy.stop') def mock_start(*args): print('called cherrypy.start') monkeypatch.setattr(tasks_cherrypy, '_get_cherry_parms', mock_get_parms) monkeypatch.setattr(tasks_cherrypy, 'start', mock_start) monkeypatch.setattr(tasks_cherrypy, 'stop', mock_stop) monkeypatch.setattr(MockContext, 'run', mock_run) c = MockContext() tasks_cherrypy.restart(c) assert capsys.readouterr().out == ( 'called cherrypy.stop\ncalled cherrypy.start\n' 'called cherrypy.stop\ncalled cherrypy.start\n') tasks_cherrypy.restart(c, 'name') assert capsys.readouterr( ).out == 'called cherrypy.stop\ncalled cherrypy.start\n'
def test_search(monkeypatch, capsys): def mock_rebuild(*args): print('called rebuild_filenamelist') monkeypatch.setattr(repo, 'FILELIST', '/tmp/filelist') monkeypatch.setattr(repo, 'rebuild_filenamelist', mock_rebuild) monkeypatch.setattr(MockContext, 'run', mock_run) c = MockContext() if os.path.exists(repo.FILELIST): os.remove(repo.FILELIST) repo.search(c) assert capsys.readouterr().out == ( 'called rebuild_filenamelist\n' 'afrift -m multi /tmp/filelist -e py -P\n') repo.search(c, rebuild=True) assert capsys.readouterr().out == ( 'called rebuild_filenamelist\n' 'afrift -m multi /tmp/filelist -e py -P\n') repo.search(c, 'name') assert capsys.readouterr().out == ( 'called rebuild_filenamelist\n' 'afrift -m multi /tmp/filelist -e py -PN -s name\n') repo.search(c, 'name', rebuild=True) assert capsys.readouterr().out == ( 'called rebuild_filenamelist\n' 'afrift -m multi /tmp/filelist -e py -PN -s name\n') with open(repo.FILELIST, 'w') as f: f.write('') repo.search(c) assert capsys.readouterr( ).out == 'afrift -m multi /tmp/filelist -e py -P\n' repo.search(c, rebuild=True) assert capsys.readouterr().out == ( 'called rebuild_filenamelist\n' 'afrift -m multi /tmp/filelist -e py -P\n') repo.search(c, 'name') assert capsys.readouterr( ).out == 'afrift -m multi /tmp/filelist -e py -PN -s name\n' repo.search(c, 'name', rebuild=True) assert capsys.readouterr().out == ( 'called rebuild_filenamelist\n' 'afrift -m multi /tmp/filelist -e py -PN -s name\n')
def test_create_bin_shortcuts(monkeypatch, capsys): def mock_chdir(*args): print('change to directory:', args[0]) def mock_symlink(*args): print('make symlink from {} to {}'.format(args[0], args[1])) monkeypatch.setattr(tasks, 'HERE', '/tmp') monkeypatch.setattr(os, 'chdir', mock_chdir) monkeypatch.setattr(os, 'symlink', mock_symlink) monkeypatch.setattr(tasks.settings, 'symlinks_bin', (('target1', 'path/to/source1'), ('target2', 'path/to/source2'))) monkeypatch.setattr(MockContext, 'run', mock_run) c = MockContext() tasks.create_bin_shortcuts(c) assert capsys.readouterr().out == ( 'change to directory: {}\n' 'make symlink from path/to/source1 to target1\n' 'make symlink from path/to/source2 to target2\n').format('/tmp')
def test_it_is_callable(self): os.environ['AWS_ACCESS_KEY_ID'] = 'fake_access_key' os.environ['AWS_SECRET_ACCESS_KEY'] = 'fake_secret_key' ctx = MockContext() aws_region = 'region' bucket = 'bucket' conn = boto3.resource('s3', region_name=aws_region) # We need to create the bucket since this is all in # Moto's 'virtual' AWS account conn.create_bucket(Bucket=bucket) publish(ctx, base_url='/site/prefix', site_prefix='site/prefix', bucket=bucket, cache_control='max-age: boop', aws_region=aws_region, dry_run=True)
def test_jekyll_build_is_called_correctly(self, patch_clone_dir): ctx = MockContext() ctx.run = Mock() conf_path = patch_clone_dir / JEKYLL_CONFIG_YML conf_contents = 'hi: test' create_file(conf_path, conf_contents) tasks.build_jekyll(ctx, branch='branch', owner='owner', repository='repo', site_prefix='site/prefix', config='boop: beep', base_url='/site/prefix') assert ctx.run.call_count == 3 jekyll_build_call_args = ctx.run.call_args_list[2] args, kwargs = jekyll_build_call_args # Make sure the call to jekyll build is correct assert args[0] == 'jekyll build --destination /work/site_repo/_site' # Make sure the env is as expected assert kwargs['env'] == { 'BRANCH': 'branch', 'OWNER': 'owner', 'REPOSITORY': 'repo', 'SITE_PREFIX': 'site/prefix', 'BASEURL': '/site/prefix', 'LANG': 'en_US.UTF-8', 'JEKYLL_ENV': 'production' } # Check that the config file has had baseurl, branch, and custom # config added with conf_path.open() as f: assert f.read() == ('hi: test\nbaseurl: /site/prefix\n' 'branch: branch\nboop: beep\n')
def test_overview(monkeypatch, capsys): def mock_repo_overzicht(c, *args): print('call overzicht met args', args) return 'output directory' # monkeypatch.setattr(repo.os.path, 'isdir', lambda x: False) c = MockContext() repo.overview(c, 'proj', 'x') == '' assert capsys.readouterr().out == 'wrong spec for output type\n' monkeypatch.setattr(repo, 'get_project_root', lambda x: 'project_root') monkeypatch.setattr(repo.os, 'listdir', lambda x: ['oink']) monkeypatch.setattr(repo, 'repo_overzicht', mock_repo_overzicht) repo.overview(c, outtype='txt') assert capsys.readouterr().out == ( "call overzicht met args ('oink', 'project_root/oink', 'txt')\n" "output in output directory\n") repo.overview(c, 'name,proj', 'csv') assert capsys.readouterr().out == ( "call overzicht met args ('name', 'project_root/name', 'csv')\n" "call overzicht met args ('proj', 'project_root/proj', 'csv')\n" "output in output directory\n")
def test_get_repofiles(monkeypatch, capsys): def mock_run(self, *args, **kwargs): print(*args, 'in', self.cwd) return types.SimpleNamespace( stdout='file1\nfile2.py\nfile3.json\nfile4.py\n') monkeypatch.setattr(repo, 'get_project_dir', lambda x: 'path/to/repo') monkeypatch.setattr(MockContext, 'run', mock_run) c = MockContext() assert repo.get_repofiles(c, '.') == (os.getcwd(), ['file2.py', 'file4.py']) assert capsys.readouterr( ).out == 'git ls-tree -r --name-only master in {}\n'.format(os.getcwd()) assert repo.get_repofiles(c, 'x') == ('path/to/repo', ['file2.py', 'file4.py']) assert capsys.readouterr( ).out == 'git ls-tree -r --name-only master in path/to/repo\n' monkeypatch.setattr(repo, 'get_project_dir', lambda x: '') assert repo.get_repofiles(c, 'x') == ('', []) assert capsys.readouterr().out == 'not a code repository\n'
def test_newproject(monkeypatch, capsys): def mock_copytree(*args): print('call copytree for `{}` to `{}`'.format(*args)) def mock_rename(*args): print('call rename of `{}` to `{}`'.format(*args)) monkeypatch.setattr(session.os.path, 'exists', lambda x: True) monkeypatch.setattr(MockContext, 'run', mock_run) c = MockContext() session.newproject(c, 'name') assert capsys.readouterr( ).out == 'sorry, this project name is already in use\n' monkeypatch.setattr(session.os.path, 'exists', lambda x: False) monkeypatch.setattr(session.shutil, 'copytree', mock_copytree) monkeypatch.setattr(session.os, 'rename', mock_rename) session.newproject(c, 'name') assert capsys.readouterr().out == ( 'call copytree for `/home/albert/projects/skeleton`' ' to `/home/albert/projects/name`\n' 'call rename of `/home/albert/projects/name/projectname`' ' to `/home/albert/projects/name/name`\n')
def test_start(monkeypatch, capsys): def mock_report(*args): print('called report_result') monkeypatch.setattr(tasks_django, 'django_project_path', { 'site': {}, 'stuff': {} }) monkeypatch.setattr(tasks_django, '_get_django_args', mock_get_parms) monkeypatch.setattr(tasks_django, 'report_result', mock_report) monkeypatch.setattr(MockContext, 'run', mock_run) c = MockContext() tasks_django.start(c) assert capsys.readouterr().out == ( 'sudo /usr/bin/gunicorn -D -b unix:sock -p pid' ' site.wsgi:application {}\ncalled report_result\n' 'sudo /usr/bin/gunicorn -D -b unix:sock -p pid' ' stuff.wsgi:application {}\ncalled report_result\n') tasks_django.start(c, 'name') assert capsys.readouterr().out == ( 'sudo /usr/bin/gunicorn -D -b unix:sock -p pid' ' name.wsgi:application {}\ncalled report_result\n')
def test_cryptic(self): """ Test that we can encrypt and decrypt data with our RSA PKI """ c = MockContext() tasks.key_gen(c, bucket_name, quadrant, region) # Test encrypt and decrypt plain_text = "hello world 1234567890!@#$%^&*()" capturedOutput = StringIO() sys.stdout = capturedOutput tasks.cryptic(c, quadrant, plain_text) cypher_text = capturedOutput.getvalue() sys.stdout = sys.__stdout__ capturedOutput = StringIO() sys.stdout = capturedOutput tasks.cryptic(c, quadrant, False, cypher_text) decrypt_text = capturedOutput.getvalue().rstrip() sys.stdout = sys.__stdout__ self.assertEqual(plain_text, decrypt_text)
def test_with_two_tasks(self, capsys): @task def my_cleanup1(ctx): print("CALLED: my_cleanup1 task") @task def my_cleanup2(ctx): print("CALLED: my_cleanup2 task") cleanup_tasks = Collection("cleanup_tasks") cleanup_tasks.add_task(my_cleanup1) cleanup_tasks.add_task(my_cleanup2) ctx = MockContext() execute_cleanup_tasks(ctx, cleanup_tasks) captured = capsys.readouterr() expected1 = "CALLED: my_cleanup1 task" expected2 = "CALLED: my_cleanup2 task" assert "CLEANUP TASK: my-cleanup1" in captured.out assert expected1 in captured.out assert "CLEANUP TASK: my-cleanup2" in captured.out assert expected1 in captured.out
def test_jekyll_build_is_called_correctly(self, fs): ctx = MockContext(run=[ Result('gem install jekyll result'), Result('jekyll version result'), Result('jekyll build result'), ]) ctx.run = Mock() with Patcher() as patcher: patcher.fs.CreateFile('/tmp/site_repo/_config.yml', contents='hi: test') build_jekyll(ctx, branch='branch', owner='owner', repository='repo', site_prefix='site/prefix', config='boop: beep', base_url='/site/prefix') assert ctx.run.call_count == 3 jekyll_build_call_args = ctx.run.call_args_list[2] args, kwargs = jekyll_build_call_args # Make sure the call to jekyll build is correct assert args[0] == 'jekyll build --destination /tmp/site_repo/_site' # Make sure the env is as expected assert kwargs['env'] == { 'BRANCH': 'branch', 'OWNER': 'owner', 'REPOSITORY': 'repo', 'SITE_PREFIX': 'site/prefix', 'BASEURL': '/site/prefix', 'LANG': 'en_US.UTF-8', 'JEKYLL_ENV': 'production' }
def test_cleanup(monkeypatch, capsys): def mock_remove(*args): print('call remove of `{}`'.format(*args)) def mock_rmtree(*args): print('call rmtree of `{}`'.format(*args)) monkeypatch.setattr(session, 'get_project_name', lambda x: 'projname') monkeypatch.setattr(session.os, 'remove', mock_remove) monkeypatch.setattr(session.shutil, 'rmtree', mock_rmtree) regfile = '/tmp/session_test_regfile' monkeypatch.setattr(session, 'get_regfile_name', lambda x: regfile) with open(regfile, 'w') as f: f.write('testname1\nprojname\ntestname2\n') c = MockContext() session.cleanup(c, 'projname') assert capsys.readouterr().out == ( 'call remove of `/home/albert/bin/.sessions/projname`\n' 'call rmtree of `/home/albert/devel/_projname`\n') with open(regfile) as f: data = f.read() assert data == 'testname1\ntestname2\n' with open(regfile, 'w') as f: f.write('projname\n') session.cleanup(c, 'projname') assert capsys.readouterr().out == ( 'call remove of `/home/albert/bin/.sessions/projname`\n' 'call rmtree of `/home/albert/devel/_projname`\n' 'call remove of `/tmp/session_test_regfile`\n') with open(regfile, 'w') as f: f.write('testname1\ntestname2\n') session.cleanup(c, 'projname') assert capsys.readouterr().out == ( 'call remove of `/home/albert/bin/.sessions/projname`\n' 'call rmtree of `/home/albert/devel/_projname`\n') with open(regfile) as f: data = f.read() assert data == 'testname1\ntestname2\n'
def test_it_calls_publish_to_s3(self, monkeypatch, s3_conn): mock_publish_to_s3 = Mock() monkeypatch.setattr('publishing.s3publisher.publish_to_s3', mock_publish_to_s3) ctx = MockContext() kwargs = dict( base_url='/site/prefix', site_prefix='site/prefix', bucket=TEST_BUCKET, cache_control='max-age: boop', aws_region=TEST_REGION, ) publish(ctx, **kwargs) mock_publish_to_s3.assert_called_once() # check that the `directory` kwarg is a string, not a Path _, actual_kwargs = mock_publish_to_s3.call_args_list[0] assert type(actual_kwargs['directory']) == str assert actual_kwargs['directory'] == str(SITE_BUILD_DIR_PATH)
def test_with_failing_task_raises_failure_should_be_gracefully_ignored( self, error_class, capsys): @task def my_cleanup(ctx): print("CALLED: my_cleanup task (entered)") message = "OOPS: my_cleanup fails" if error_class is Exit: raise Exit(message, code=100) else: raise error_class(Result(message, exited=101)) print("CALLED: my_cleanup task (exited)") cleanup_tasks = Collection("cleanup_tasks") cleanup_tasks.add_task(my_cleanup, name="my_cleanup") ctx = MockContext() execute_cleanup_tasks(ctx, cleanup_tasks) captured = capsys.readouterr() expected = "FAILURE in CLEANUP TASK: my-cleanup (GRACEFULLY-IGNORED)" assert expected in captured.out assert "CLEANUP TASKS: 1 failure(s) occured" in captured.out assert "CALLED: my_cleanup task (entered)" in captured.out assert "CALLED: my_cleanup task (exited)" not in captured.out
def test_restart(monkeypatch, capsys): def mock_stop(*args): print('called django.stop') def mock_start(*args): print('called django.start') monkeypatch.setattr(tasks_django, 'django_project_path', { 'site': {}, 'stuff': {} }) monkeypatch.setattr(tasks_django, '_get_django_args', mock_get_parms) monkeypatch.setattr(tasks_django, 'start', mock_start) monkeypatch.setattr(tasks_django, 'stop', mock_stop) monkeypatch.setattr(MockContext, 'run', mock_run) c = MockContext() tasks_django.restart(c) assert capsys.readouterr().out == ( 'called django.stop\ncalled django.start\n' 'called django.stop\ncalled django.start\n') tasks_django.restart(c, 'name') assert capsys.readouterr( ).out == 'called django.stop\ncalled django.start\n'