def test_isortcfg_not_found(self): file_path = self._given_a_file_in_test_dir( 'from sys import pid\n' 'import threading', isort_config='force_single_line=True') # remove the .isort.cfg file isortcfg_path = file_path.split('/')[:-1] isortcfg_path = '{0}/.isort.cfg'.format('/'.join(isortcfg_path)) os.remove(isortcfg_path) with OutputCapture(): checker = Flake8Isort(None, file_path) checker.config_file = True ret = list(checker.run()) self.assertEqual(len(ret), 1) self.assertEqual(ret[0][0], 0) self.assertEqual(ret[0][1], 0) self.assertIn( 'I002 ', ret[0][2], )
def test_query(self): with TestingDB("empty.db") as db, Replace( "timewalk.arguments.time", test_time(delta=50, delta_type="seconds")): start_time = datetime(2001, 1, 1) codefiles = ("python.py", "go.go", "swift.swift") for file in codefiles: argv = [ "record", "--file", os.path.join("tests", "samples", "codefiles", file), "--database", db.path, "--config", "tests/samples/configs/empty.ini" ] retval = execute(argv) argv = [ "query", "--database", db.path, ] with OutputCapture() as o, LogCapture() as l: retval = execute(argv) output_text = o.captured l.check() try: actual = json.loads(output_text) except json.JSONDecodeError as e: print(o.captured) raise e expected = [ { "start": int(start_time.replace(tzinfo=timezone.utc).timestamp()), "end": int(start_time.replace(tzinfo=timezone.utc).timestamp()) + 50 * (len(codefiles) - 1), "duration": (50 * (len(codefiles) - 1)), "invoker": {}, "project": {}, "language": {"Go": 50, "Swift": 50}, }, ] self.assertListEqual(expected, actual)
def test_report(self): with TestingDB("empty.db") as db, Replace( "timewalk.arguments.time", test_time(None)) as d: codefiles = ("python.py", "go.go", "swift.swift") seconds = [0, 20, 50] for file, t in zip(codefiles, seconds): start_date = datetime(2001, 1, 1, 0, 0, t, 0) d.set(start_date) argv = [ "record", "--file", os.path.join("tests", "samples", "codefiles", file), "--database", db.path, "--project", "My Plugin", "--invoker", '"testAgent/1.0.0-alpha timewalk-testAgent/0.5.0"', "--config", "tests/samples/configs/empty.ini" ] retval = execute(argv) start_date = datetime(2001, 1, 1, 0, 0, 55, 0) d.set(start_date) argv = [ "report", "--database", db.path, ] with OutputCapture() as o, LogCapture() as l: retval = execute(argv) output_text = o.captured with open("./tests/samples/output/test_report.txt", "r") as f: sevendays = timedelta(days=7) expected_start = datetime.fromtimestamp( (start_date - sevendays).replace(tzinfo=timezone.utc).timestamp()).strftime("%b %d, %Y %H:%M:%S") expected_end = datetime.fromtimestamp( start_date.replace(tzinfo=timezone.utc).timestamp()).strftime("%b %d, %Y %H:%M:%S") expected = f.read().format(expected_start, expected_end) self.assertEqual(output_text, expected)
def kef_to_ph5(ph5path, nickname, commonpath, keflist, ex=None, das_sn_list=[]): """ Add kef to ph5file or to experiment (if ex is passed). (The task of deleting table before adding the table should happen before calling this function. If it is required to have a delete function for all, it should be written in nuke_table.py) :para ph5path: path to ph5 file :type ph5path: string :para commonpath: common part of paths to kef files :type commonpath: string :para keflist: A list of different parts of paths to kef files :type keflist: list of string :para ex: ph5 experiment from caller :para ex: ph5 experiment object :result: the tables in the kef files will be added to ph5 file or the reference ex (if passed) """ if ex is None: with OutputCapture(): kef2ph5.EX = initialize_ex(nickname, ph5path, True) else: kef2ph5.EX = ex # create nodes for das for sn in das_sn_list: kef2ph5.EX.ph5_g_receivers.newdas(sn) kef2ph5.PH5 = os.path.join(ph5path, nickname) kef2ph5.TRACE = False for kef in keflist: kef2ph5.KEFFILE = os.path.join(commonpath, kef) kef2ph5.populateTables() if ex is None: kef2ph5.EX.ph5close()
def test_main_exception(self): with TempDirectory() as d: from testfixtures import OutputCapture # setup db db_path = d.getpath('sqlite.db') conn = sqlite3.connect(db_path) # don't create the table so we get at exception conn.commit() # setup config config = d.write( 'config.ini', ''' [main] db = %s log = %s ''' % (db_path, d.getpath('script.log')), 'ascii') # setup file to read source = d.write('bad.txt', 'some text', 'ascii') with Replacer() as r: r.replace('sys.argv', ['script.py', config, source]) with OutputCapture() as output: main() self.assertTrue('OperationalError' in output.captured, output.captured)
def test_random_parallel(self) -> None: @config(random=True) def _roll() -> float: return np.random.random() @config(top=True, parallel=True) def top() -> None: # pylint: disable=unused-variable results = parallel(2, _roll) print(sorted(results)) np.random.seed(11) sys.argv += ['top', '--random_seed', '17'] with OutputCapture() as output: main(ArgumentParser(description='Test')) np.random.seed(17) first = np.random.random() np.random.seed(18) second = np.random.random() results = sorted([first, second]) output.compare('%s\n' % results)
def test_sorted_incorrectly_multiple(self): (file_path, lines) = self.write_python_file( 'from __future__ import division\n' 'import os\n' 'from sys import pid\n' 'import threading\n' '\n' 'import isort\n' '\n\n\n' 'def func()\n', ) with OutputCapture(): checker = Flake8Isort(None, file_path, lines) ret = list(checker.run()) self.assertEqual(len(ret), 3) self.assertEqual(ret[0][0], 2) self.assertEqual(ret[0][1], 0) self.assertTrue(ret[0][2].startswith('I003 ')) self.assertEqual(ret[1][0], 4) self.assertEqual(ret[1][1], 0) self.assertTrue(ret[1][2].startswith('I001 ')) self.assertEqual(ret[2][0], 9) self.assertEqual(ret[2][1], 0) self.assertTrue(ret[2][2].startswith('I004 '))
def run(self): settings_file = self.search_isort_config() if self.config_file and not settings_file: yield 0, 0, self.no_config_msg, type(self) else: if self.filename is not self.stdin_display_name: file_path = self.filename else: file_path = None with OutputCapture() as buffer: sort_result = SortImports( file_path=file_path, file_contents=''.join(self.lines), check=True, settings_path=settings_file, show_diff=True, ) traceback = self._format_isort_output(buffer) for line_num, message in self.sortimports_linenum_msg(sort_result): if self.show_traceback: message += traceback yield line_num, 0, message, type(self)
def evaluate_bash_block(example): command, output = example.parsed def sample_script(dotted, func): sample = __import__('sample.' + dotted) obj = sample for name in dotted.split('.'): obj = getattr(obj, name) getattr(obj, func)() def do_nothing(*args): pass commands = { 'bin/db': (partial(sample_script, 'db', 'scripts'), False), 'bin/run': (partial(sample_script, 'run', 'main'), False), 'DB_URL=mysql://scott:tiger@localhost/test': (do_nothing, False), } command, args = command.split(None, 1) func, needs_ver = commands[command] with Replacer() as r: r.replace('sys.argv', [command] + args.split()) try: with OutputCapture() as o: func() except SystemExit as e: # pragma: no cover print("Output:") print(o.output.getvalue()) raise except Exception as e: # pragma: no cover actual = '...traceback...\n' + repr(e) else: actual = o.output.getvalue() actual = actual.replace(example.namespace['dir'].path, '').strip() compare(actual, expected=output.rstrip())
def test_sorted_incorrectly_multiple(self): file_path = self._given_a_file_in_test_dir( 'from __future__ import division\n' 'import os\n' 'from sys import pid\n' 'import threading\n' '\n' 'import isort\n' '\n\n\n' 'def func()\n', isort_config='') with OutputCapture(): checker = Flake8Isort(None, file_path) ret = list(checker.run()) self.assertEqual(len(ret), 3) self.assertEqual(ret[0][0], 2) self.assertEqual(ret[0][1], 0) self.assertTrue(ret[0][2].startswith('I003 ')) self.assertEqual(ret[1][0], 4) self.assertEqual(ret[1][1], 0) self.assertTrue(ret[1][2].startswith('I001 ')) self.assertEqual(ret[2][0], 9) self.assertEqual(ret[2][1], 0) self.assertTrue(ret[2][2].startswith('I004 '))
def test_get_args(self): testargs = [ 'ph5_validate', '-n', 'master.ph5', '-p', self.tmpdir, '-l', 'WARN' ] with patch.object(sys, 'argv', testargs): with OutputCapture() as out: self.assertRaises(SystemExit, ph5validate.get_args) output = out.captured.strip().split('\n') self.assertEqual( output[1], "ph5_validate: error: argument -l/--level: invalid choice: " "'WARN' (choose from 'ERROR', 'WARNING', 'INFO')") testargs = [ 'ph5_validate', '-n', 'master.ph5', '-p', self.tmpdir, '-l', 'WARNING' ] with patch.object(sys, 'argv', testargs): ret = ph5validate.get_args() self.assertEqual(ret.level, 'WARNING') self.assertEqual(ret.nickname, 'master.ph5') self.assertEqual(ret.outfile, 'ph5_validate.log') self.assertEqual(ret.ph5path, self.tmpdir) self.assertEqual(ret.verbose, False)
def test_analysis_should_return_false_if_trailing_spaces_are_found(self): self.given_a_file_in_test_dir('invalid.py', INVALID_CODE) with OutputCapture(): self.assertFalse(CleanLines(self.options).run())
def test_main(self): # check external links created data_nodes = [ 'Das_g_0000SV01', 'Das_g_0000SV02', 'Das_g_0000SV03', 'Das_g_0000SV04', 'Das_g_0000SV05', 'Das_g_0000SV06', 'Das_g_0000SV07', 'Das_g_0000SV08', 'Das_g_0000SV09', 'Das_g_0000SV10', 'Das_g_0000SV11', 'Das_g_0000SV12', 'Das_g_0000SV13', 'Das_g_0000SV14', 'Das_g_0000SV15', 'Das_g_0000SV16', 'Das_g_0000SV17', 'Das_g_0000SV18', 'Das_g_0000SV19', 'Das_g_0000SV20', 'Das_g_0000SV21', 'Das_g_0000SV22', 'Das_g_0000SV23', 'Das_g_0000SV24', 'Das_g_0000SV25', 'Das_g_0000SV26', 'Das_g_0000SV27', 'Das_g_0000SV28', 'Das_g_0000SV29', 'Das_g_0000SV30', 'Das_g_0000SV31', 'Das_g_0000SV32', 'Das_g_0000SV33', 'Das_g_0000SV34', 'Das_g_0000SV35', 'Das_g_0000SV36', 'Das_g_0000SV37', 'Das_g_0000SV38', 'Das_g_0000SV39', 'Das_g_0000SV40', 'Das_g_0000SV41', 'Das_g_0000SV42', 'Das_g_0000SV43', 'Das_g_0000SV44', 'Das_g_0000SV45', 'Das_g_0000SV46', 'Das_g_0000SV47', 'Das_g_0000SV48', 'Das_g_0000SV49', 'Das_g_0000SV50', 'Das_g_0000SV51', 'Das_g_0000SV52', 'Das_g_0000SV53', 'Das_g_0000SV54', 'Das_g_0000SV55', 'Das_g_0000SV56', 'Das_g_0000SV57', 'Das_g_0000SV58', 'Das_g_0000SV59', 'Das_g_0000SV60' ] testargs = ['initialize_ph5', '-n', 'master.ph5'] with patch.object(sys, 'argv', testargs): initialize_ph5.main() # add seg2 to ph5 testargs = [ 'seg2toph5', '-n', 'master.ph5', '-r', os.path.join(self.home, "ph5/test_data/seg2/15001.dat") ] with patch.object(sys, 'argv', testargs): with OutputCapture(): with LogCapture(): seg2toph5.main() self.ph5object = ph5api.PH5(path=self.tmpdir, nickname='master.ph5') target_p1 = 'miniPH5_00001.ph5:/Experiment_g/Maps_g/' targets = [target_p1 + n for n in data_nodes] node = self.ph5object.ph5.get_node("/Experiment_g/Maps_g/") i = 0 ret_targets = [] for n in self.ph5object.ph5.list_nodes(node): if hasattr(n, 'target'): ret_targets.append(n.target) i += 1 self.assertEqual(ret_targets, targets) target_p1 = 'miniPH5_00001.ph5:/Experiment_g/Receivers_g/' targets = [target_p1 + n for n in data_nodes] node = self.ph5object.ph5.get_node("/Experiment_g/Receivers_g/") i = 0 ret_targets = [] for n in self.ph5object.ph5.list_nodes(node): if hasattr(n, 'target'): ret_targets.append(n.target) i += 1 self.assertEqual(ret_targets, targets)
def test_empty_file(self): (file_path, lines) = self.write_python_file('\n\n', ) with OutputCapture(): checker = Flake8Isort(None, file_path, lines) ret = list(checker.run()) self.assertEqual(ret, [])
def test_compare_strips(self): with OutputCapture() as o: print(' Bar! ') o.compare('Bar!')
def test_unicode(self): with OutputCapture() as o: print(u'\u65e5', file=sys.stdout) o.compare(u'\u65e5\n')
def test_analysis_should_return_true(self): self.given_a_file_in_test_dir('valid.pt', VALID_CODE) with OutputCapture(): self.assertTrue(ZPTLint(self.options).run())
def test_analysis_should_return_false_when_error_found(self): self.given_a_file_in_test_dir('invalid.pt', INVALID_CODE) with OutputCapture(): self.assertFalse(ZPTLint(self.options).run())
def test_analysis_should_raise_systemexit_0_in_console_script(self): with OutputCapture(): with self.assertRaisesRegexp(SystemExit, '0'): console_script(self.options)
def test_analysis_should_return_true_when_oserror(self): # The options are fake, so the function should raise an OSError # but return True. self.options['bin-directory'] = 'FAKE_DIR' with OutputCapture(): self.assertTrue(Flake8(self.options).run())
def test_command_help(self): argv = ["record", "--help"] with OutputCapture() as o: execute(argv) with open("tests/samples/output/test_command_help.txt", "r") as f: self.assertEqual(o.captured, f.read())
def test_no_git_folder(self): with OutputCapture() as out: self.code_analysis.install_pre_commit_hook() found = out.captured.find('Unable to create git pre-commit hook,') self.assertTrue(found == 0)
def test_analysis_should_return_false_if_unused_imports_are_found(self): self.given_a_file_in_test_dir('invalid.py', INVALID_CODE) with OutputCapture(): self.assertFalse(ImportChecker(self.options).run())
def test_hooks_folder_being_created(self): os.makedirs('{0}/.git'.format(self.test_dir)) with OutputCapture() as out: self.code_analysis.install_pre_commit_hook() out.compare('Install Git pre-commit hook.') self.assertTrue(os.path.exists('{0}/.git/hooks'.format(self.test_dir)))
def test_analysis_should_return_false_when_error_found(self): self.given_a_file_in_test_dir('incorrect.py', INVALID_CODE) with OutputCapture(): self.assertFalse(Flake8(self.options).run())
def test_analysis_should_return_true_for_valid_files(self): self.given_a_file_in_test_dir('valid.py', VALID_CODE) with OutputCapture(): self.assertTrue(CleanLines(self.options).run())
def test_analysis_should_return_true(self): self.given_a_file_in_test_dir('correct.py', VALID_CODE) with OutputCapture(): self.assertTrue(Flake8(self.options).run())
def test_analysis_should_return_false_if_file_with_tabs_is_found(self): self.given_a_file_in_test_dir('invalid.xml', INVALID_TABS) with OutputCapture(): self.assertFalse(CleanLines(self.options).run())
def test_analysis_should_raise_systemexit_1_in_console_script(self): self.given_a_file_in_test_dir('invalid.py', INVALID_CODE) with OutputCapture(): with self.assertRaisesRegexp(SystemExit, '1'): console_script(self.options)
def test_analysis_should_return_true_for_non_watched_file_with_tabs(self): self.given_a_file_in_test_dir('invalid.abc', INVALID_TABS) with OutputCapture(): self.assertTrue(CleanLines(self.options).run())