def test_memoize_filename_argument_glob(self): cache = wc_utils.cache.Cache(directory=os.path.join(self.dir, 'cache')) @cache.memoize(filename_args=[0], filename_kwargs=['input_2']) def func(input_1, input_2=''): print('func ran') with open(input_2, 'r') as file: return 2 * float(file.read()) fn_1 = os.path.join(self.dir, 'test_1') fn_2 = os.path.join(self.dir, 'test_2') with open(fn_1, 'w') as file: file.write('1') with open(fn_2, 'w') as file: file.write('2') with capturer.CaptureOutput(merged=False, relay=False) as captured: self.assertEqual( func(os.path.join(self.dir, 'test_*'), input_2=fn_2), 4) time.sleep(0.1) self.assertEqual(captured.stdout.get_text(), 'func ran') with capturer.CaptureOutput(merged=False, relay=False) as captured: self.assertEqual( func(os.path.join(self.dir, 'test_*'), input_2=fn_2), 4) self.assertEqual(captured.stdout.get_text(), '')
def test_version(self): with self.App(argv=['-v']) as app: with capturer.CaptureOutput(merged=False, relay=False) as captured: with self.assertRaises(SystemExit) as cm: app.run() self.assertEqual(cm.exception.code, 0) stdout = captured.stdout.get_text() self.assertIn('Test Simulator: 1.2.3', stdout) self.assertIn('CLI: 4.5.6', stdout) self.assertIn( 'Python: {}.{}.{}'.format(sys.version_info.major, sys.version_info.minor, sys.version_info.micro), stdout) self.assertEqual(captured.stderr.get_text(), '') with self.App(argv=['--version']) as app: with capturer.CaptureOutput(merged=False, relay=False) as captured: with self.assertRaises(SystemExit) as cm: app.run() self.assertEqual(cm.exception.code, 0) stdout = captured.stdout.get_text() self.assertIn('Test Simulator: 1.2.3', stdout) self.assertIn('CLI: 4.5.6', stdout) self.assertIn( 'Python: {}.{}.{}'.format(sys.version_info.major, sys.version_info.minor, sys.version_info.micro), stdout) self.assertEqual(captured.stderr.get_text(), '')
def test_memoize_with_explicit_name(self): cache = wc_utils.cache.Cache(directory=os.path.join(self.dir, 'cache')) @cache.memoize(name='func_1_2') def func_1(input): print('func ran') return 2 * input @cache.memoize(name='func_1_2') def func_2(input): print('func ran') return 2 * input @cache.memoize(name='func_3') def func_3(input): print('func ran') return 2 * input with capturer.CaptureOutput(merged=False, relay=False) as captured: self.assertEqual(func_1(1), 2) self.assertEqual(captured.stdout.get_text(), 'func ran') with capturer.CaptureOutput(merged=False, relay=False) as captured: self.assertEqual(func_2(1), 2) self.assertEqual(captured.stdout.get_text(), '') with capturer.CaptureOutput(merged=False, relay=False) as captured: self.assertEqual(func_3(1), 2) self.assertEqual(captured.stdout.get_text(), 'func ran') with self.assertRaises(TypeError): cache.memoize(name=func_3)
def test_validate(self): with capturer.CaptureOutput(merged=False, relay=False) as captured: with __main__.App( argv=['validate', '2 * abc_a + 3 * abc_b']) as app: # run app app.run() # test that the CLI produced the correct output self.assertEqual(captured.stdout.get_text(), 'Form is valid') self.assertEqual(captured.stderr.get_text(), '') with capturer.CaptureOutput(merged=False, relay=False) as captured: with __main__.App(argv=[ 'validate', 'bmp2_a + bmp2_a | x-link: [l-bond-atom: bmp2_a(1)-362S1 | l-displaced-atom: bmp2_a(1)-362H1 | r-bond-atom: bmp2_a(2)-362S1 | r-displaced-atom: bmp2_a(2)-362H1]' ]) as app: # run app app.run() # test that the CLI produced the correct output self.assertEqual(captured.stdout.get_text(), 'Form is valid') self.assertEqual(captured.stderr.get_text(), '') with self.assertRaisesRegex(SystemExit, '^Form is invalid'): with __main__.App(argv=['validate', 'HELLO']) as app: app.run() with self.assertRaisesRegex(SystemExit, '^Form is invalid'): with __main__.App(argv=[ 'validate', 'abc_a + abc_b | x-link: [l-bond-atom: abc_c(1)-2O1 | l-displaced-atom: abc_d(1)-2H1 | r-bond-atom: abc_b(1)-3C1 | r-displaced-atom: abc_b(1)-3H1 | r-displaced-atom: abc_b(1)-3O1]' ]) as app: app.run()
def test_run_process_in_container(self): mgr = self.mgr mgr.build_container() # not verbose mgr.config['verbose'] = False with capturer.CaptureOutput(relay=False) as capture_output: mgr.run_process_in_container(['echo', 'here']) self.assertEqual(capture_output.get_text(), '') # verbose mgr.config['verbose'] = True with capturer.CaptureOutput(relay=False) as capture_output: mgr.run_process_in_container(['echo', 'here']) self.assertEqual(capture_output.get_text(), 'here') # error mgr.config['verbose'] = False with self.assertRaisesRegex(wc_env_manager.WcEnvManagerError, ' exit code: 126'): mgr.run_process_in_container(['__undefined__']) # error, specified working directory mgr.config['verbose'] = False with self.assertRaisesRegex(wc_env_manager.WcEnvManagerError, ' working directory: /root'): mgr.run_process_in_container(['__undefined__'], work_dir='/root') # error, specified environment mgr.config['verbose'] = False with self.assertRaisesRegex(wc_env_manager.WcEnvManagerError, ' key: val'): mgr.run_process_in_container(['__undefined__'], env={'key': 'val'})
def test_validate(self): with capturer.CaptureOutput(merged=False, relay=False) as captured: with __main__.App(argv=['validate', 'dna', 'ACGT']) as app: # run app app.run() # test that the CLI produced the correct output self.assertEqual(captured.stdout.get_text(), 'Form is valid') self.assertEqual(captured.stderr.get_text(), '') with capturer.CaptureOutput(merged=False, relay=False) as captured: with __main__.App(argv=[ 'validate', 'dna', 'ACG[id: "ala" | structure: {}]T'.format(ala_inchi) ]) as app: # run app app.run() # test that the CLI produced the correct output self.assertEqual(captured.stdout.get_text(), 'Form is valid') self.assertEqual(captured.stderr.get_text(), '') with self.assertRaisesRegex(SystemExit, '^Form is invalid'): with __main__.App(argv=['validate', 'dna', 'ACGT[']) as app: # run app app.run()
def test_get_mol_wt(self): with capturer.CaptureOutput(merged=False, relay=False) as captured: with __main__.App(argv=[ 'get-molwt', 'abc_a + abc_b', '{abc_a:86, abc_b:57}' ]) as app: # run app app.run() # test that the CLI produced the correct output self.assertAlmostEqual(float(captured.stdout.get_text()), 143, places=0) self.assertEqual(captured.stderr.get_text(), '') with capturer.CaptureOutput(merged=False, relay=False) as captured: with __main__.App(argv=[ 'get-molwt', 'abc_a + abc_b | x-link: [l-bond-atom: abc_a(1)-2O1 | l-displaced-atom: abc_a(1)-2H1+1 | r-bond-atom: abc_b(1)-3C1 | r-displaced-atom: abc_b(1)-3H1 | r-displaced-atom: abc_b(1)-3O1]', '{abc_a:86, abc_b:57}' ]) as app: # run app app.run() # test that the CLI produced the correct output self.assertAlmostEqual(float(captured.stdout.get_text()), 125, places=0) self.assertEqual(captured.stderr.get_text(), '') with self.assertRaisesRegex(SystemExit, '^Form is invalid'): with __main__.App( argv=['get-molwt', 'HELLO', '{HELLO:86}']) as app: app.run() with self.assertRaisesRegex(SystemExit, '^Form is invalid'): with __main__.App(argv=[ 'get-molwt', 'abc_a + abc_b | x-link: [l-bond-atom: abc_c(1)-2O1 | l-displaced-atom: abc_d(1)-2H1 | r-bond-atom: abc_b(1)-3C1 | r-displaced-atom: abc_b(1)-3H1 | r-displaced-atom: abc_b(1)-3O1]', '{abc_a:86, abc_b:57}' ]) as app: app.run() with self.assertRaisesRegex(SystemExit, '^Cannot parse subunit_mol_wts:'): with __main__.App( argv=['get-molwt', 'abc_a + abc_b', '{abc_a:86, abc_b}' ]) as app: app.run() with self.assertRaisesRegex( SystemExit, '^Unable to calculate BcForm molecular weights:'): with __main__.App( argv=['get-molwt', 'abc_a + abc_b', '{abc_a:86}']) as app: app.run()
def test_raw_cli(self): with mock.patch('sys.argv', ['obj-tables', '--help']): with capturer.CaptureOutput(relay=False): with self.assertRaises(SystemExit) as context: __main__.main() self.assertRegex(context.Exception, 'usage: obj-tables') with mock.patch('sys.argv', ['obj-tables']): with capturer.CaptureOutput(relay=False) as capture_output: __main__.main() self.assertRegex(capture_output.get_text(), 'usage: obj-tables')
def test_run_process_on_host(self): mgr = self.mgr mgr.config['verbose'] = False with capturer.CaptureOutput(relay=False) as capture_output: mgr.run_process_on_host(['echo', 'here']) self.assertEqual(capture_output.get_text(), '') mgr.config['verbose'] = True with capturer.CaptureOutput(relay=False) as capture_output: mgr.run_process_on_host(['echo', 'here']) self.assertEqual(capture_output.get_text(), 'here')
def test_error(self): with capturer.CaptureOutput(merged=False, relay=False) as captured: with self.assertRaises(Exception): rna_seq_util.Kallisto()._run('index', ['__undefined__.fasta'], verbose=True) self.assertNotEqual(captured.stdout.get_text(), '') self.assertNotEqual(captured.stderr.get_text(), '') with capturer.CaptureOutput(merged=False, relay=False) as captured: with self.assertRaises(Exception): rna_seq_util.Kallisto()._run('index', ['__undefined__.fasta'], verbose=False) self.assertEqual(captured.stdout.get_text(), '') self.assertEqual(captured.stderr.get_text(), '')
def test_get_version(self): with capturer.CaptureOutput(relay=False, termination_delay=0.1) as capture_output: with __main__.App(argv=['-v']) as app: with self.assertRaises(SystemExit): app.run() self.assertEqual(capture_output.get_text(), obj_tables.__version__) with capturer.CaptureOutput(relay=False, termination_delay=0.1) as capture_output: with __main__.App(argv=['--version']) as app: with self.assertRaises(SystemExit): app.run() self.assertEqual(capture_output.get_text(), obj_tables.__version__)
def test_get_formula(self): with capturer.CaptureOutput(merged=False, relay=False) as captured: with __main__.App(argv=[ 'get-formula', 'abc_a + abc_b', '{abc_a:C5H10O, abc_b:C3H5O}' ]) as app: # run app app.run() # test that the CLI produced the correct output self.assertEqual(captured.stdout.get_text(), 'C8H15O2') self.assertEqual(captured.stderr.get_text(), '') with capturer.CaptureOutput(merged=False, relay=False) as captured: with __main__.App(argv=[ 'get-formula', 'abc_a + abc_b | x-link: [l-bond-atom: abc_a(1)-2O1 | l-displaced-atom: abc_a(1)-2H1+1 | r-bond-atom: abc_b(1)-3C1 | r-displaced-atom: abc_b(1)-3H1 | r-displaced-atom: abc_b(1)-3O1]', '{abc_a:C5H10O, abc_b:C3H5O}' ]) as app: # run app app.run() # test that the CLI produced the correct output self.assertEqual(captured.stdout.get_text(), 'C8H13O') self.assertEqual(captured.stderr.get_text(), '') with self.assertRaisesRegex(SystemExit, '^Form is invalid'): with __main__.App( argv=['get-formula', 'HELLO', '{HELLO:C5H10O}']) as app: app.run() with self.assertRaisesRegex(SystemExit, '^Form is invalid'): with __main__.App(argv=[ 'get-formula', 'abc_a + abc_b | x-link: [l-bond-atom: abc_c(1)-2O1 | l-displaced-atom: abc_d(1)-2H1 | r-bond-atom: abc_b(1)-3C1 | r-displaced-atom: abc_b(1)-3H1 | r-displaced-atom: abc_b(1)-3O1]', '{abc_a:C5H10O, abc_b:C3H5O}' ]) as app: app.run() with self.assertRaisesRegex(SystemExit, '^Cannot parse subunit_formulas:'): with __main__.App(argv=[ 'get-formula', 'abc_a + abc_b', '{abc_a:C5H10O, abc_b}' ]) as app: app.run() with self.assertRaisesRegex(SystemExit, '^Unable to calculate BcForm formula:'): with __main__.App( argv=['get-formula', 'abc_a + abc_b', '{abc_a:C5H10O}' ]) as app: app.run()
def test_version(self): with __main__.App(argv=['-v']) as app: with capturer.CaptureOutput(merged=False, relay=False) as captured: with self.assertRaises(SystemExit): app.run() self.assertIn(my_simulator.__version__, captured.stdout.get_text()) self.assertEqual(captured.stderr.get_text(), '') with __main__.App(argv=['--version']) as app: with capturer.CaptureOutput(merged=False, relay=False) as captured: with self.assertRaises(SystemExit): app.run() self.assertIn(my_simulator.__version__, captured.stdout.get_text()) self.assertEqual(captured.stderr.get_text(), '')
def test_memoize_tuple(self): cache = wc_utils.cache.Cache(directory=os.path.join(self.dir, 'cache')) @cache.memoize() def func(input): print('func ran') return input[0] + input[1] with capturer.CaptureOutput(merged=False, relay=False) as captured: self.assertEqual(func((1, 2)), 3) self.assertEqual(captured.stdout.get_text(), 'func ran') with capturer.CaptureOutput(merged=False, relay=False) as captured: self.assertEqual(func((1, 2)), 3) self.assertEqual(captured.stdout.get_text(), '')
def __init__(self, level=StandardOutputErrorCapturerLevel.c, relay=False, termination_delay=0.01, disabled=False): """ Args: level (:obj:`StandardOutputErrorCapturerLevel`, optional): level at which stdout/stderr should be captured relay (:obj:`bool`): if :obj:`True`, collect the standard output/error streams and continue to pass them along. if :obj:`False`, collect the stream, squash them, and do not pass them along. termination_delay (:obj:`float`, optional): The number of seconds to wait before terminating the output relay process. disabled (:obj:`bool`, optional): whether to capture standard output and error """ self.level = level self.relay = relay self.disabled = disabled if not self.disabled: if self.level >= StandardOutputErrorCapturerLevel.c and capturer: self._captured = capturer.CaptureOutput(merged=True, relay=relay, termination_delay=termination_delay) else: self._log = '' else: msg = ( 'Standard output and error could not be logged because capturer is not installed. ' 'To install capturer, install BioSimulators utils with the `logging` option ' '(`pip install biosimulators-utils[logging]`).' ) warn(msg, StandardOutputNotLoggedWarning)
def test(self): mgr = self.mgr config = mgr.config config['verbose'] = True mgr.login_docker_hub() # build base image #mgr.pull_image(config['base_image']['repo_unsquashed'], config['base_image']['tags']) #mgr.pull_image(config['base_image']['repo'], config['base_image']['tags']) mgr.build_base_image() mgr.push_image(config['base_image']['repo_unsquashed'], config['base_image']['tags']) mgr.push_image(config['base_image']['repo'], config['base_image']['tags']) # build image #mgr.pull_image(config['image']['repo'], config['image']['tags']) mgr.build_image() mgr.push_image(config['image']['repo'], config['image']['tags']) # build container mgr.build_container() mgr.setup_container() with capturer.CaptureOutput(relay=True) as capture_output: mgr.run_process_in_container(['wc-cli', '--help']) self.assertRegex(capture_output.get_text(), r'usage: wc-cli \[\-h\]')
def test_setup_container(self): mgr = self.mgr mgr.config['verbose'] = True temp_dir_name = tempfile.mkdtemp() git.Repo.clone_from('https://github.com/KarrLab/wc_utils.git', os.path.join(temp_dir_name, 'wc_utils')) git.Repo.clone_from('https://github.com/KarrLab/wc_kb.git', os.path.join(temp_dir_name, 'wc_kb')) mgr.config['container']['paths_to_mount'] = { temp_dir_name: { 'bind': '/root/host/Documents', 'mode': 'rw', } } mgr.config['container']['python_packages'] = ''' /root/host/Documents/wc_utils -e /root/host/Documents/wc_kb ''' mgr.build_container() with capturer.CaptureOutput(relay=False) as capture_output: mgr.setup_container() text = capture_output.get_text() self.assertRegex(text, r'Processing .*?wc_utils') self.assertRegex(text, r'Successfully installed .*?wc-utils-') mgr.run_process_in_container( ['rm', '-r', '/root/host/Documents/wc_kb/wc_kb.egg-info']) shutil.rmtree(temp_dir_name)
def test_get_notebooks(self): if os.path.isdir(os.path.expanduser('~/.wc/wc_sandbox/notebooks')): shutil.rmtree(os.path.expanduser('~/.wc/wc_sandbox/notebooks')) with __main__.App(argv=['install-packages']) as app: with mock.patch('subprocess.check_call', return_value=None): app.run() with __main__.App(argv=['get-notebooks']) as app: with capturer.CaptureOutput(merged=False, relay=False) as captured: app.run() text = captured.stdout.get_text() self.assertIn('Got notebooks', text) self.assertIn('- bpforms', text) self.assertTrue( os.path.isdir(os.path.expanduser('~/.wc/wc_sandbox/notebooks'))) self.assertTrue( os.path.isdir( os.path.expanduser('~/.wc/wc_sandbox/notebooks/bpforms'))) self.assertTrue( os.path.isfile( os.path.expanduser( '~/.wc/wc_sandbox/notebooks/bpforms/1. Introductory tutorial.ipynb' ))) with __main__.App(argv=['get-notebooks']) as app: app.run()
def test_build_base_image_verbose(self): mgr = self.mgr mgr.config['verbose'] = True with capturer.CaptureOutput(relay=False) as capture_output: mgr.build_base_image() self.assertRegex(capture_output.get_text(), r'Step 1/\d+ : FROM ubuntu')
def test_tool_help(self): config = { 'tool': { 'wc_kb': { 'label': 'kb', 'description': 'Framework for knowledge bases for whole-cell models', }, 'wc_lang': { 'label': 'lang', 'description': 'Framework for representing whole-cell models', }, }, 'model': {}, } with capturer.CaptureOutput(merged=False, relay=False) as captured: with __main__.App(argv=['tool'], config=config) as app: # run app app.run() # test that the CLI produced the correct output self.assertRegex(captured.stdout.get_text(), 'kb') self.assertRegex(captured.stdout.get_text(), 'lang') self.assertEqual(captured.stderr.get_text(), '')
def test_build_image(self): mgr = self.mgr mgr.config['verbose'] = True mgr.config['image']['tags'] = ['test'] temp_dir_name = tempfile.mkdtemp() with open(os.path.join(temp_dir_name, 'a'), 'w') as file: file.write('ABC') with open(os.path.join(temp_dir_name, 'b'), 'w') as file: file.write('DEF') os.mkdir(os.path.join(temp_dir_name, 'c')) mgr.config['image']['paths_to_copy'] = { 'a': { 'host': os.path.join(temp_dir_name, 'a'), 'image': '/tmp/a', }, 'b': { 'host': os.path.join(temp_dir_name, 'b'), 'image': '/tmp/b', }, 'c': { 'host': os.path.join(temp_dir_name, 'c'), 'image': '/tmp/c', }, } mgr.config['image']['python_packages'] = ''' git+https://github.com/KarrLab/wc_lang.git#egg=wc_lang-0.0.1[all] git+https://github.com/KarrLab/wc_utils.git#egg=wc_utils-0.0.1[all] ''' with capturer.CaptureOutput(relay=False) as capture_output: mgr.build_image() text = capture_output.get_text() self.assertRegex(text, 'Successfully installed .*?wc-lang-') self.assertRegex(text, 'Successfully installed .*?wc-utils-') self.assertRegex(text, 'Successfully built') mgr.config['container']['python_packages'] = '' mgr.config['container']['setup_script'] = '' mgr.build_container() mgr.copy_path_from_container('/tmp/a', os.path.join(temp_dir_name, 'a2')) mgr.copy_path_from_container('/tmp/b', os.path.join(temp_dir_name, 'b2')) mgr.copy_path_from_container('/tmp/c', os.path.join(temp_dir_name, 'c')) with open(os.path.join(temp_dir_name, 'a2'), 'r') as file: self.assertEqual(file.read(), 'ABC') with open(os.path.join(temp_dir_name, 'b2'), 'r') as file: self.assertEqual(file.read(), 'DEF') self.assertTrue(os.path.isdir(os.path.join(temp_dir_name, 'c'))) shutil.rmtree(temp_dir_name) mgr.stop_container() mgr.remove_containers() mgr.remove_image(mgr.config['image']['repo'], mgr.config['image']['tags'])
def test_stdout(self): sim = core.Simulation(k_syn=1, k_deg=0.25, verbose=True) with capturer.CaptureOutput() as captured: sim.run(value_init=4, time_max=10) stdout = captured.get_text() lines = stdout.rstrip().split('\n') for line in lines: self.assertRegex(line, r'^Time \d+: \d+ molecules$')
def test_tool_wc_lang(self): with capturer.CaptureOutput(merged=False, relay=False) as captured: with __main__.App(argv=['tool', 'lang', '--version']) as app: # run app app.run() self.assertRegex(captured.stdout.get_text(), r'^\d+\.\d+\.\d+[a-zA-Z0-9]*$') self.assertEqual(captured.stderr.get_text(), '')
def test_help(self): with biosimulators_utils.__main__.App(argv=[]) as app: with capturer.CaptureOutput(merged=False, relay=False) as captured: app.run() stdout = captured.stdout.get_text() self.assertTrue( stdout.startswith('usage: biosimulators-utils')) self.assertEqual(captured.stderr.get_text(), '')
def test_validate(self): with capturer.CaptureOutput(merged=False, relay=False) as captured: with __main__.App( argv=['validate', 'canonical_dna', 'ACGT']) as app: # run app app.run() # test that the CLI produced the correct output self.assertEqual(captured.stdout.get_text(), 'Form is valid') self.assertEqual(captured.stderr.get_text(), '') with capturer.CaptureOutput(merged=False, relay=False) as captured: with __main__.App(argv=[ 'validate', 'canonical_dna', ('ACG' '[id: "dI" | structure: "{}"' ' | l-bond-atom: P30' ' | l-displaced-atom: O33-1' ' | r-bond-atom: O34' ' | r-displaced-atom: H34' ' ]' 'T').format(dIMP_smiles) ]) as app: # run app app.run() # test that the CLI produced the correct output self.assertEqual(captured.stdout.get_text(), 'Form is valid') self.assertEqual(captured.stderr.get_text(), '') with self.assertRaisesRegex(SystemExit, '^Form is invalid'): with __main__.App( argv=['validate', 'canonical_dna', 'ACGT[']) as app: # run app app.run() with self.assertRaisesRegex(SystemExit, '^Form is invalid'): with __main__.App(argv=[ 'validate', 'canonical_dna', ('ACGT' '[id: "dI" | structure: "{}" | l-displaced-atom: O33-1 ]' ).format(dIMP_smiles) ]) as app: # run app app.run()
def test_es_check_svr(self): with capturer.CaptureOutput(merged=False, relay=False) as captured: with __main__.App(argv=['es-check-server']) as app: # run app app.run() # test that the CLI produced the correct output self.assertNotEqual(captured.stdout.get_text(), '') self.assertEqual(captured.stderr.get_text(), '')
def test_version(self): with biosimulators_utils.__main__.App(argv=['-v']) as app: with capturer.CaptureOutput(merged=False, relay=False) as captured: with self.assertRaises(SystemExit) as cm: app.run() self.assertEqual(cm.exception.code, 0) stdout = captured.stdout.get_text() self.assertEqual(stdout, biosimulators_utils.__version__) self.assertEqual(captured.stderr.get_text(), '') with biosimulators_utils.__main__.App(argv=['--version']) as app: with capturer.CaptureOutput(merged=False, relay=False) as captured: with self.assertRaises(SystemExit) as cm: app.run() self.assertEqual(cm.exception.code, 0) stdout = captured.stdout.get_text() self.assertEqual(stdout, biosimulators_utils.__version__) self.assertEqual(captured.stderr.get_text(), '')
def _run_job(db_path, builder_fn, job_id): # Workaround of the clash between jupyter & capturer sys.stdout = sys.__stdout__ sys.stderr = sys.__stderr__ start_time = time.time() cpt = None global _per_process_db try: if _per_process_db is None: _per_process_db = Database(db_path) job_setup, config, keys_to_job_ids = _per_process_db.set_running( job_id) cpt = capturer.CaptureOutput(relay=job_setup.relay) cpt.start_capture() if job_setup.timeout is not None: thread = threading.Thread( target=_run_job_timed, args=( _per_process_db, job_id, builder_fn, config, keys_to_job_ids, start_time, cpt, ), ) thread.daemon = True thread.start() thread.join(job_setup.timeout) if thread.is_alive(): t = JobTimeout(job_id, job_setup.timeout) _per_process_db.set_error(job_id, t.message(), time.time() - start_time) return t else: _run_job_timed( _per_process_db, job_id, builder_fn, config, keys_to_job_ids, start_time, cpt, ) return job_id except Exception as exception: t = JobError(job_id, str(exception), traceback.format_exc()) if _per_process_db: _per_process_db.set_error( job_id, t.message(), time.time() - start_time, cpt.get_bytes() if cpt else None, ) return t
def test_command_1(self): with capturer.CaptureOutput(merged=False, relay=False) as captured: with __main__.App(argv=['command-1']) as app: # run app app.run() # test that the CLI produced the correct output self.assertEqual(captured.stdout.get_text(), '...') self.assertEqual(captured.stderr.get_text(), '...')
def test_exec_archive(self): with self.App( argv=['-i', 'path to COMBINE/OMEX archive', '-o', '']) as app: with capturer.CaptureOutput(merged=False, relay=False) as captured: app.run() self.assertEqual(captured.stdout.get_text(), 'path to COMBINE/OMEX archive') self.assertEqual(captured.stderr.get_text(), '') with self.App( argv=['-i', '', '-o', 'path to directory to save outputs' ]) as app: with capturer.CaptureOutput(merged=False, relay=False) as captured: with self.assertRaises(SystemExit) as cm: app.run() self.assertEqual(cm.exception.code, 'path to directory to save outputs') self.assertEqual(captured.stdout.get_text(), '') self.assertEqual(captured.stderr.get_text(), '')