def test_Config_init(): with tempdir() as tmp: path = join(tmp, const.CONFIG_FILENAME) with open(path, "w") as f: f.write("key: value\n") conf = mod.Config(path, SCHEMA) eq_(conf["key"], "value")
def test(expect, old_value=None): if isinstance(expect, Exception): exc = type(expect) exc_val = expect elif expect == old_value: exc = IOError exc_val = IOError("fail") else: exc = exc_val = None with tempdir() as tmp: path = os.path.join(tmp, "file.txt") if old_value is not None: with open(path, "x") as fh: fh.write(old_value) with assert_raises(exc, msg=exc_val): with mod.atomicfile(path) as fh: fh.write("new") if exc_val is not None: raise exc_val files = os.listdir(tmp) if exc is None: with open(path) as fh: eq_(fh.read(), expect) assert files == ["file.txt"], files elif old_value is not None: assert files == ["file.txt"], files with open(path) as fh: eq_(fh.read(), old_value) else: assert not files, files
def test_Config_non_ascii(): with tempdir() as tmp: path = join(tmp, const.CONFIG_FILENAME) with open(path, "w", encoding="utf-8") as f: f.write("key: ↑ \u4500 ↑\n") conf = mod.Config(path, SCHEMA) eq_(conf["key"], "↑ \u4500 ↑")
def test_setup_profile_at_file(): with tempdir() as tmp: path = os.path.join(tmp, 'profile') with open(path, 'w') as fh: pass app = Application(path) eq_(app.setup_profile(), False) assert os.path.isfile(path), path
def test(appends, lookups): with tempdir() as tmp: history = mod.History(tmp, 3, 5) for item in appends: history.append(item) history = mod.History(tmp, 3, 5) eq_(list(enumerate(history)), lookups)
def test_CommandBar_get_history_concurrently(): with tempdir() as tmp: history = mod.CommandHistory(tmp) for item in reversed("abc"): history.append(item) editor = type("FakeEditor", (object, ), {})() commander = TextCommandController(history) bar1 = mod.CommandBar(editor, commander) bar2 = mod.CommandBar(editor, commander) bar3 = mod.CommandBar(editor, commander) bar4 = mod.CommandBar(editor, commander) eq_(bar1.get_history("x"), "a") eq_(bar2.get_history(""), "a") eq_(bar2.get_history("y"), "b") eq_(bar3.get_history(""), "a") eq_(bar3.get_history("a"), "b") eq_(bar3.get_history("z"), "c") # <-- "z" will move to 0 (with "b") history.append("b") # current index "a", "x" in new command buffer eq_(bar1.get_history("a"), "c") eq_(bar1.get_history("c"), None) eq_(bar1.get_history("c", True), "a") eq_(bar1.get_history("a", True), "b") eq_(bar1.get_history("b", True), "x") eq_(bar1.get_history("x", True), None) # current index "b", "y" at 0 eq_(bar2.get_history("B"), "y") # <-- "B" now at 0 eq_(bar2.get_history("y"), "c") eq_(bar2.get_history("c"), None) eq_(bar2.get_history("c", True), "y") eq_(bar2.get_history("y", True), "B") eq_(bar2.get_history("B", True), "") eq_(bar2.get_history("", True), None) # current index "c", "z" at 1 eq_(bar3.get_history("c"), None) eq_(bar3.get_history("C", True), "a") eq_(bar3.get_history("a"), "C") eq_(bar3.get_history("C", True), "a") eq_(bar3.get_history("a", True), "z") eq_(bar3.get_history("z", True), "") # <-- "z" moved to 0 eq_(bar3.get_history("", True), None) eq_(bar4.get_history("A", True), None) eq_(bar4.get_history("A"), "b") eq_(bar4.get_history("b"), "a") eq_(bar4.get_history("a"), "c") eq_(bar4.get_history("c"), None) eq_(bar4.get_history("c", True), "a") eq_(bar4.get_history("a", True), "b") eq_(bar4.get_history("b", True), "A") eq_(bar4.get_history("A", True), None)
def test_CommandBar_get_history_concurrently(): with tempdir() as tmp: history = mod.CommandHistory(tmp) for item in reversed(["abc", "amx", "x12", "amber", "x123", "y1"]): history.append(item) window = type("FakeWindow", (object,), {})() commander = CommandManager(history) bar1 = mod.CommandBar(window, commander) bar2 = mod.CommandBar(window, commander) bar3 = mod.CommandBar(window, commander) bar4 = mod.CommandBar(window, commander) eq_(bar1.get_history("x"), "x12") eq_(bar2.get_history(""), "abc") eq_(bar2.get_history("y"), "y1") eq_(bar3.get_history(""), "abc") eq_(bar3.get_history("abc"), "amx") eq_(bar3.get_history("z"), None) history.append("z1") # current index "a", "x" in new command buffer eq_(bar1.get_history("x12"), "x123") eq_(bar1.get_history("x123"), None) eq_(bar1.get_history("x123", True), "x12") eq_(bar1.get_history("x12", True), "x") eq_(bar1.get_history("x", True), None) # current index 5 ("y1"), "y" at 0 eq_(bar2.get_history("y1"), None) eq_(bar2.get_history("y1", True), "y") eq_(bar2.get_history("y", True), None) eq_(bar2.get_history("a", True), None) # <-- "a" now at 0 eq_(bar2.get_history("a"), "amx") eq_(bar2.get_history("amx", True), "a") eq_(bar2.get_history("a", True), None) # current index 1 ("amx", "z") eq_(bar3.get_history("c"), None) eq_(bar3.get_history("z", True), "z1") eq_(bar3.get_history("z1"), "z") eq_(bar3.get_history("z", True), "z1") eq_(bar3.get_history("z1", True), None) eq_(bar3.get_history("z", True), None) # <-- "z1" -> "z" eq_(bar3.get_history("z"), "z") eq_(bar4.get_history("", True), None) eq_(bar4.get_history(""), "z1") eq_(bar4.get_history("z1"), "abc") eq_(bar4.get_history("abc"), "amx") eq_(bar4.get_history("amx"), "x12") eq_(bar4.get_history("x12", True), "amx") eq_(bar4.get_history("amx", True), "abc") eq_(bar4.get_history("abc", True), "z1") eq_(bar4.get_history("z1", True), "") eq_(bar4.get_history("", True), None)
def test_CommandBar_get_history_concurrently(): with tempdir() as tmp: history = mod.CommandHistory(tmp) for item in reversed("abc"): history.append(item) window = type("FakeWindow", (object,), {})() commander = CommandManager(history) bar1 = mod.CommandBar(window, commander) bar2 = mod.CommandBar(window, commander) bar3 = mod.CommandBar(window, commander) bar4 = mod.CommandBar(window, commander) eq_(bar1.get_history("x"), "a") eq_(bar2.get_history(""), "a") eq_(bar2.get_history("y"), "b") eq_(bar3.get_history(""), "a") eq_(bar3.get_history("a"), "b") eq_(bar3.get_history("z"), "c") # <-- "z" will move to 0 (with "b") history.append("b") # current index "a", "x" in new command buffer eq_(bar1.get_history("a"), "c") eq_(bar1.get_history("c"), None) eq_(bar1.get_history("c", True), "a") eq_(bar1.get_history("a", True), "b") eq_(bar1.get_history("b", True), "x") eq_(bar1.get_history("x", True), None) # current index "b", "y" at 0 eq_(bar2.get_history("B"), "y") # <-- "B" now at 0 eq_(bar2.get_history("y"), "c") eq_(bar2.get_history("c"), None) eq_(bar2.get_history("c", True), "y") eq_(bar2.get_history("y", True), "B") eq_(bar2.get_history("B", True), "") eq_(bar2.get_history("", True), None) # current index "c", "z" at 1 eq_(bar3.get_history("c"), None) eq_(bar3.get_history("C", True), "a") eq_(bar3.get_history("a"), "C") eq_(bar3.get_history("C", True), "a") eq_(bar3.get_history("a", True), "z") eq_(bar3.get_history("z", True), "") # <-- "z" moved to 0 eq_(bar3.get_history("", True), None) eq_(bar4.get_history("A", True), None) eq_(bar4.get_history("A"), "b") eq_(bar4.get_history("b"), "a") eq_(bar4.get_history("a"), "c") eq_(bar4.get_history("c"), None) eq_(bar4.get_history("c", True), "a") eq_(bar4.get_history("a", True), "b") eq_(bar4.get_history("b", True), "A") eq_(bar4.get_history("A", True), None)
def test(name, expect, appends='abcdefghiabca'): with tempdir() as tmp: history = mod.History(tmp, 3, 5) for i, item in enumerate(appends): history.append(item + " " + str(i)) history = mod.History(tmp, 3, 5) result = next(history.iter_matching(name), None) eq_(result, expect)
def test(config_data, error): with tempdir() as tmp, CaptureLog(mod) as log: path = join(tmp, const.CONFIG_FILENAME) with open(path, "w") as f: f.write(config_data) conf = mod.Config(tmp, SCHEMA) with assert_raises(KeyError): conf["key"] regex = Regex("cannot load [^:]+/config\.yaml: {}".format(error)) eq_(log.data, {"error": [regex]})
def test_Config_reload(): with tempdir() as tmp: with open(join(tmp, const.CONFIG_FILENAME), "w") as f: f.write("key: value\n") conf = mod.Config(tmp, SCHEMA) eq_(conf["key"], "value") with open(join(tmp, const.CONFIG_FILENAME), "w") as f: f.write("answer: 42\n") conf.reload() eq_(conf["answer"], 42) assert "key" not in conf, conf
def test_CommandBar_reset(): with tempdir() as tmp: editor = type("Editor", (object, ), {})() history = mod.CommandHistory(tmp) commander = mod.TextCommandController(history) bar = mod.CommandBar(editor, commander) eq_(bar.get_history(""), None) assert bar.history_view is not None assert bar.history_view in history.views bar.reset() eq_(bar.history_view, None) assert bar.history_view not in history.views
def test_CommandBar_reset(): with tempdir() as tmp: window = type("Window", (object,), {})() history = mod.CommandHistory(tmp) commander = mod.CommandManager(history) bar = mod.CommandBar(window, commander) eq_(bar.get_history(""), None) assert bar.history_view is not None assert bar.history_view in history.views bar.reset() eq_(bar.history_view, None) assert bar.history_view not in history.views
def test_CommandBar_reset(): with tempdir() as tmp: editor = type("Editor", (object,), {})() history = mod.CommandHistory(tmp) commander = mod.TextCommandController(history) bar = mod.CommandBar(editor, commander) eq_(bar.get_history(""), None) assert bar.history_view is not None assert bar.history_view in history.views bar.reset() eq_(bar.history_view, None) assert bar.history_view not in history.views
def test(nav): with tempdir() as tmp: history = mod.CommandHistory(tmp) for item in reversed("abc"): history.append(item) editor = type("FakeEditor", (object, ), {})() commander = TextCommandController(history) bar = mod.CommandBar(editor, commander) for input, direction, history in nav: dirchar = "v" if direction else "A" print("{}({!r}, {!r})".format(dirchar, input, history)) eq_(bar.get_history(input, forward=direction), history)
def test(nav): with tempdir() as tmp: history = mod.CommandHistory(tmp) for item in reversed("abc"): history.append(item) window = type("FakeWindow", (object,), {})() commander = CommandManager(history) bar = mod.CommandBar(window, commander) for input, direction, history in nav: dirchar = "v" if direction else "A" print("{}({!r}, {!r})".format(dirchar, input, history)) eq_(bar.get_history(input, forward=direction), history)
def test(original, config="editor"): diffed = [] text_name = "file.txt" rm = [] def diff_stub(path1, path2, diff_program, remove=None): for path in remove: os.remove(path) eq_(path1, path_1) eq_(path2, path_2) eq_(diff_program, "opendiff") diffed.append(1) with tempdir() as tmp, test_app(config) as app, \ replattr(mod, "external_diff", diff_stub): window = app.windows[0] editor = text_editor = window.projects[0].editors[0] if config == "editor": path = editor.file_path = join(tmp, "file.txt") with open(path, mode="w", encoding="utf8") as fh: fh.write("abc") m = Mocker() text_view = editor.text_view = m.mock(TextView) path_1 = editor.file_path if original: make_dirty(editor.document) name_ext = splitext(test_app(app).name(editor)) path_2 = Regex(r"/file-.*\.txt$") args = None text_view.string() >> "def" elif len(window.selected_items) == 2: editor2 = window.selected_items[1] editor2.text_view = m.mock(TextView) name_ext = splitext(test_app(app).name(editor)[7:-1]) path_1 = Regex(r"/{}-.*{}$".format(*name_ext)) name_ext = splitext(test_app(app).name(editor2)[7:-1]) path_2 = Regex(r"/{}-.*{}$".format(*name_ext)) args = None text_view.string() >> "def" editor2.text_view.string() >> "ghi" else: path_1 = join(tmp, "other.txt") path_2 = editor.file_path with open(path_1, mode="w", encoding="utf8") as fh: fh.write("other") assert " " not in path_1, path_1 args = mod.diff.arg_parser.parse(path_1) with m: mod.diff(editor, args) assert diffed
def test(states): with tempdir() as tmp: state_path = os.path.join(tmp, const.STATE_DIR) if states: # setup previous state m = Mocker() app = Application(tmp) def iter_editors(): for ident in states: yield TestConfig(state=[ident]) m.method(app.iter_editors)() >> iter_editors() with m: app.save_editor_states() assert os.listdir(state_path), state_path app = Application(tmp) result = list(app.iter_saved_editor_states()) eq_(result, [[id] for id in states])
def test(c): with tempdir() as tmp: sf = SyntaxFactory() filename = os.path.join(tmp, "file") with open(filename, "w", encoding="utf-8") as fh: if c.error is not Exception: fh.write("\n".join("{} = {!r}".format(k, v) for k, v in c.info.items())) else: fh.write("raise {}".format(c.error.__name__)) values = dict(defaults) values.update(c.info) if c.error is None: res = sf.load_definition(filename) for name, value in values.items(): eq_(getattr(res, name), value, name) else: assert_raises(c.error, sf.load_definition, filename)
def test_external_diff(): popen_called = [] def popen(command, **kw): popen_called.append(True) print(command) eq_(sum(1 for c in command if c == ";"), 1, command) args = shlex.split(command.split(";")[0]) print(args) eq_(args, ["true", "--arg", path, path]) check_call(command, **kw) assert not exists(path), "test file not removed: {}".format(path) with tempdir() as tmp, replattr(mod, "Popen", popen): path = join(tmp, "file.txt") with open(path, mode="w") as fh: fh.write("abc") mod.external_diff(path, path, "true --arg", [path]) assert popen_called, "diff command was not executed"
def test(with_id=True, fail=False): with tempdir() as tmp: state_path = os.path.join(tmp, const.STATE_DIR) editor = TestConfig(state=[42], id=9) args = (editor.id,) if with_id else () app = Application(tmp) state_name = app.save_editor_state(editor, *args) if fail: editor = editor(state="should not be written") def dump_fail(state, fh=None): if fh is not None: fh.write("should not be seen") raise Exception("dump fail!") with replattr(mod, "dump_yaml", dump_fail, sigcheck=False): state_name = app.save_editor_state(editor, *args) assert os.path.isdir(state_path), state_path with open(os.path.join(state_path, state_name)) as f: eq_(load_yaml(f), [42])
def test(path, saved=True): with tempdir() as tmp, test_app() as app: path, begin_content = setup_path(tmp, path) doc = app.document_with_path(path) end_content = "modified" m = Mocker() undo = doc.undo_manager = m.mock(mod.UndoManager) if saved: m.method(doc.update_syntaxer)() undo.savepoint() doc.text = end_content with m: if saved: doc.save() eq_(get_content(doc.file_path), end_content) else: with assert_raises(mod.Error): doc.save() eq_(get_content(doc.file_path), begin_content)
def test(c): with tempdir() as tmp: sf = SyntaxFactory() filename = os.path.join(tmp, "file") with open(filename, "w", encoding="utf-8") as fh: if c.error is not Exception: fh.write("\n".join("{} = {!r}".format(k, v) for k, v in c.info.items())) else: fh.write("raise {}".format(c.error.__name__)) values = dict(defaults) values.update(c.info) if c.error is None: res = sf.load_definition(filename) for name, value in values.items(): if name == "filepatterns": value = set(value) eq_(getattr(res, name), value, name) else: assert_raises(c.error, sf.load_definition, filename)
def setup_files(tmp=None): def do_setup(tmp): os.mkdir(join(tmp, "dir")) for path in [ "dir/a.txt", "dir/b.txt", "dir/B file", ]: assert not isabs(path), path with open(join(tmp, path), "w") as fh: fh.write("name: {}\nsize: {}".format(path, len(path))) assert " " not in tmp, tmp if tmp is None: with tempdir() as tmp: do_setup(tmp) yield tmp else: do_setup(tmp) yield tmp
def test(with_id=True, fail=False): with tempdir() as tmp: state_path = os.path.join(tmp, const.STATE_DIR) editor = TestConfig(state=[42], id=9) args = (editor.id, ) if with_id else () app = Application(tmp) state_name = app.save_editor_state(editor, *args) if fail: editor = editor(state="should not be written") def dump_fail(state, fh=None): if fh is not None: fh.write("should not be seen") raise Exception("dump fail!") with replattr(mod, "dump_yaml", dump_fail, sigcheck=False): state_name = app.save_editor_state(editor, *args) assert os.path.isdir(state_path), state_path with open(os.path.join(state_path, state_name)) as f: eq_(load_yaml(f), [42])
def test_CommandBar_history_reset_on_execute(): from editxt.document import TextDocumentView from editxt.textcommand import CommandHistory, TextCommandController with tempdir() as tmp: m = Mocker() editor = m.mock() history = CommandHistory(tmp) commander = TextCommandController(history) bar = mod.CommandBar(editor, commander) args = ["cmd"] view = editor.current_view >> m.mock(TextDocumentView) view.text_view >> '<view>' @command def cmd(textview, sender, args): pass commander.add_command(cmd, None, None) with m: bar.get_history("cmd") bar.execute("cmd") eq_(bar.history_view, None) eq_(list(history), ["cmd"])
def test(files, lookups): # :param files: List of items representing history files, each # being a list of commands. # :param lookups: List of tuples (<history index>, <command text>), ...] index = [] name = "test" pattern = mod.History.FILENAME_PATTERN with tempdir() as tmp: for i, value in enumerate(files): index.append(pattern.format(name, i)) if value is not None: path = join(tmp, pattern.format(name, i)) with open(path, "w", encoding="utf-8") as fh: for command in reversed(value): fh.write(json.dumps(command) + "\n") path = join(tmp, mod.History.INDEX_FILENAME) with open(path, "w", encoding="utf-8") as fh: json.dump(index, fh) history = mod.History(tmp, 3, 5, name=name) eq_(list(enumerate(history)), lookups)
def test_CommandBar_history_reset_on_execute(): from editxt.editor import Editor from editxt.textcommand import CommandHistory, CommandManager with tempdir() as tmp: m = Mocker() window = m.mock() history = CommandHistory(tmp) commander = CommandManager(history) bar = mod.CommandBar(window, commander) args = ["cmd"] editor = m.mock(Editor) (window.current_editor << editor).count(2) @command def cmd(editor, args): pass commander.add_command(cmd, None, None) with m: bar.get_history("cmd") bar.execute("cmd") eq_(bar.history_view, None) eq_(list(history), ["cmd"])
def test(states, error=None): with tempdir() as tmp: state_path = os.path.join(tmp, const.STATE_DIR) if states: # setup previous state m = Mocker() app = Application(tmp) def iter_windows(): for ident in states: yield TestConfig(state=[ident]) m.method(app.iter_windows)() >> iter_windows() with m: app.save_window_states() assert os.listdir(state_path), state_path if error is not None: fail_path = os.path.join( state_path, const.EDITOR_STATE.format(error)) with open(fail_path, "w") as fh: fh.write('!!invalid "yaml"') app = Application(tmp) result = list(app.iter_saved_window_states()) eq_(result, [[id] if error != i else mod.StateLoadFailure(fail_path) for i, id in enumerate(states)])
def test(c): with tempdir() as tmp: state_path = os.path.join(tmp, const.STATE_DIR) if c.previous: # setup previous state m = Mocker() app = Application(tmp) mock_editors(m.method(app.iter_editors), c.previous) with m: app.save_editor_states() assert os.listdir(state_path), state_path m = Mocker() app = Application(tmp) mock_editors(m.method(app.iter_editors), c.editors) with m: app.save_editor_states() assert os.path.isdir(state_path), state_path states = sorted(os.listdir(state_path)) eq_(len(states), len(c.editors), states) for ident, state in zip(c.editors, states): with open(os.path.join(state_path, state)) as f: eq_(load_yaml(f), [ident])
def test(c): with tempdir() as tmp: state_path = os.path.join(tmp, const.STATE_DIR) if c.previous: # setup previous state m = Mocker() app = Application(tmp) mock_windows(m.method(app.iter_windows), c.previous) with m: app.save_window_states() assert os.listdir(state_path), state_path m = Mocker() app = Application(tmp) mock_windows(m.method(app.iter_windows), c.windows) with m: app.save_window_states() assert os.path.isdir(state_path), state_path states = sorted(set(os.listdir(state_path)) - {const.CLOSED_DIR}) eq_(len(states), len(c.windows), states) for ident, state in zip(c.windows, states): with open(os.path.join(state_path, state)) as f: eq_(load_yaml(f), [ident])
def test(setup): with tempdir() as tmp, test_app() as app: docs = app.documents eq_(len(docs), 0) path = setup(realpath(tmp), app, docs) if isinstance(path, tuple): path, expected_path = path else: expected_path = path doc = app.document_with_path(path) try: assert isinstance(doc, TextDocument) if os.path.exists(path): assert samefile(path, doc.file_path), (path, doc.file_path) eq_(expected_path, doc.file_path) else: eq_(expected_path, doc.file_path) eq_(doc.app, app) finally: doc.close() finalize = getattr(setup, "finalize", None) if finalize is not None: finalize(tmp, app, docs) eq_(len(docs), 0)
def test_Config_init_with_no_file(): with tempdir() as tmp: conf = mod.Config(tmp, SCHEMA) with assert_raises(KeyError): conf["key"]
def replace_history(): with tempdir() as tmp: history = CommandHistory(tmp) with replattr(editxt.app.text_commander, "history", history): yield history
def test_Config_init(): with tempdir() as tmp: with open(join(tmp, const.CONFIG_FILENAME), "w") as f: f.write("key: value\n") conf = mod.Config(tmp, SCHEMA) eq_(conf["key"], "value")
def test_setup_profile_parent_missing(): with tempdir() as tmp: path = os.path.join(tmp, 'missing', 'profile') app = Application(path) eq_(app.setup_profile(), False) assert not os.path.exists(path), path
def test_Config_init_with_no_file(): with tempdir() as tmp: path = join(tmp, const.CONFIG_FILENAME) conf = mod.Config(path, SCHEMA) with assert_raises(KeyError): conf["key"]
def test_setup_profile_parent_exists(): with tempdir() as tmp: path = os.path.join(tmp, 'profile') app = Application(path) eq_(app.setup_profile(), True) assert os.path.exists(path), path
def test_setup_profile_exists(): with tempdir() as tmp: app = Application(tmp) eq_(app.setup_profile(), True)