def test_exec_shell(): if not mod.is_ag_installed(): raise SkipTest("ag not installed") with setup_files() as tmp: result = mod.exec_shell(["ag", "dir/[bB]", "--workers=1"], cwd=tmp) eq_(result, 'dir/B file:1:name: dir/B file\n' 'dir/b.txt:1:name: dir/b.txt\n') eq_(result.err, None) eq_(result.returncode, 0)
def test_ag(): if not mod.is_ag_installed(): raise SkipTest("ag not installed") @gentest def test(command, message="", project_path="/", selection=""): cfg = {"command.ag.options": "--workers=1"} state = "window project(/) editor(/dir/b.txt)*" with test_app(state, cfg) as app, setup_files(test_app(app).tmp) as tmp: editor = app.windows[0].current_editor if selection: editor.document.text_storage[:] = selection editor.text_view = TestConfig(selectedRange=lambda:(0, len(selection))) bar = CommandTester(mod.ag, editor=editor, output=True) bar(command) output = bar.output if output is not None: output = output.replace("xt://open/%s/" % tmp, "xt://open/") if "Traceback (most recent call last):" in output: print(output) assert "Traceback (most recent call last):" not in message eq_(output, markup(message)) eq_(test_app(app).state, state) yield test("ag ([bB]|size:\ 10)", "[B file](xt://open/dir/B%20file)\n" "[1](xt://open/dir/B%20file?goto=1):name: dir/[B](xt://open/dir/B%20file?goto=1.10.1) file\n" "[2](xt://open/dir/B%20file?goto=2):[size: 10](xt://open/dir/B%20file?goto=2.0.8)\n" "\n" "[b.txt](xt://open/dir/b.txt)\n" "[1](xt://open/dir/b.txt?goto=1):name: dir/[b](xt://open/dir/b.txt?goto=1.10.1).txt\n") yield test("ag dir/[bB] ..", "[dir/B file](xt://open/dir/../dir/B%20file)\n" "[1](xt://open/dir/../dir/B%20file?goto=1):name: [dir/B](xt://open/dir/../dir/B%20file?goto=1.6.5) file\n" "\n" "[dir/b.txt](xt://open/dir/../dir/b.txt)\n" "[1](xt://open/dir/../dir/b.txt?goto=1):name: [dir/b](xt://open/dir/../dir/b.txt?goto=1.6.5).txt\n") yield test("ag dir/[bB] .. --after=1", "[dir/B file](xt://open/dir/../dir/B%20file)\n" "[1](xt://open/dir/../dir/B%20file?goto=1):name: [dir/B](xt://open/dir/../dir/B%20file?goto=1.6.5) file\n" "[2](xt://open/dir/../dir/B%20file?goto=2):size: 10\n\n" "\n" "[dir/b.txt](xt://open/dir/../dir/b.txt)\n" "[1](xt://open/dir/../dir/b.txt?goto=1):name: [dir/b](xt://open/dir/../dir/b.txt?goto=1.6.5).txt\n" "[2](xt://open/dir/../dir/b.txt?goto=2):size: 9\n\n") yield test("ag dir/b .. -i", "[dir/B file](xt://open/dir/../dir/B%20file)\n" "[1](xt://open/dir/../dir/B%20file?goto=1):name: [dir/B](xt://open/dir/../dir/B%20file?goto=1.6.5) file\n" "\n" "[dir/b.txt](xt://open/dir/../dir/b.txt)\n" "[1](xt://open/dir/../dir/b.txt?goto=1):name: [dir/b](xt://open/dir/../dir/b.txt?goto=1.6.5).txt\n") yield test("ag ..", "[dir/B file](xt://open/dir/../dir/B%20file)\n" "[1](xt://open/dir/../dir/B%20file?goto=1):name: [dir/B ](xt://open/dir/../dir/B%20file?goto=1.6.6)file\n", selection="dir/B ") yield test("ag xyz", "no match for pattern: xyz")
def test_exec_shell(): if not mod.is_ag_installed(): raise SkipTest("ag not installed") with setup_files() as tmp: result = mod.exec_shell(["ag", "dir/[bB]", "--workers=1"], cwd=tmp) assert_same_items(result.split("\n"), [ 'dir/B file:1:name: dir/B file', 'dir/b.txt:1:name: dir/b.txt', '', ]) eq_(result.err, None) eq_(result.returncode, 0)
def test_pathfind(): if not is_ag_installed(): raise SkipTest("ag not installed") filesystem = [ ".git/file.txt", # excluded by default "dir/a_file.txt", "dir/file.pyc", "dir/file.txt", "dir/file_b.txt", "dir/file/txt", "file.doc", "file.txt", ] @gentest @test_app("window project(/dir) editor*") def test(app, command, files=None, selection=(0, 0)): app.config.extend("ag", { "path": config.String("ag"), "options": config.String(""), }) tmp = test_app(app).tmp os.mkdir(join(tmp, ".git")) os.mkdir(join(tmp, "dir")) os.mkdir(join(tmp, "dir", "file")) for path in filesystem: assert not isabs(path), path with open(join(tmp, path), "w") as fh: fh.write(" ") with open(join(tmp, ".gitignore"), "w") as fh: fh.write("*.pyc\n") if command: parts = command.split(' ') if len(parts) > 2: if parts[2].startswith("/"): parts[2] = join(tmp, parts[2].lstrip("/")) assert all(p.startswith(tmp + "/") for p in parts if p.startswith("/")), parts command = " ".join(parts) print(command) editor = app.windows[0].current_editor if editor.document is not None: editor.document.text_storage[:] = "from file.txt import txt" m = Mocker() view = editor.text_view = m.mock() (view.selectedRange() << selection).count(0, 4) with m: bar = CommandTester(mod.pathfind, editor=editor, output=True) bar(command) output = bar.output if files is None: assert output is None, output else: if output is not None: output = output.replace(tmp + "/", "/") expect = " ".join("<a href='xt://open/{0}{1}'>{1}</a>".format( "/" if f.startswith("/") else "/dir/", f[1:] if f.startswith("/") else f, ) for f in files) eq_(" ".join(sorted(x for x in output.split("<br />") if x)), expect) file_txt = ["a_file.txt", "file.txt", "file/txt"] # simulate invoke with hotkey yield test(None, file_txt, selection=(5, 8)) yield test("pathfind", file_txt, selection=(5, 8)) base_test = test for cfg in [None, "window project(/dir)* editor"]: test = base_test if cfg is None else partial(base_test, init_state=cfg) yield test("pathfind file.txt", file_txt) yield test("pathfind file\\.txt", ["a_file.txt", "file.txt"]) yield test("pathfind file\\. /", [ "/dir/a_file.txt", "/dir/file.txt", "/file.doc", "/file.txt", ]) yield test("pathfind file\\. / unrestricted", [ "/.git/file.txt", "/dir/a_file.txt", "/dir/file.pyc", "/dir/file.txt", "/file.doc", "/file.txt", ])