|
10 | 10 | import pexpect |
11 | 11 | import pytest |
12 | 12 | import yaml |
| 13 | +from coverage.tracer import CTracer |
13 | 14 | from pexpect.popen_spawn import PopenSpawn |
14 | 15 | from plumbum import local |
15 | 16 |
|
|
89 | 90 | "[[ _copier_conf.answers_file ]].tmpl": "[[_copier_answers|to_nice_yaml]]", |
90 | 91 | } |
91 | 92 |
|
| 93 | +PATH_TREE: Mapping[StrOrPath, str | bytes] = { |
| 94 | + "copier.yml": ( |
| 95 | + f"""\ |
| 96 | + _templates_suffix: {SUFFIX_TMPL} |
| 97 | + _envops: {BRACKET_ENVOPS_JSON} |
| 98 | + current_location: |
| 99 | + type: path |
| 100 | + default: /dev/warppipe0 |
| 101 | +
|
| 102 | + star_location: |
| 103 | + type: path |
| 104 | + help: "Location of a bonus star" |
| 105 | + """ |
| 106 | + ), |
| 107 | + "[[ _copier_conf.answers_file ]].tmpl": "[[_copier_answers|to_nice_yaml]]", |
| 108 | +} |
| 109 | + |
92 | 110 |
|
93 | 111 | @pytest.mark.parametrize( |
94 | 112 | "name, args", |
@@ -156,6 +174,50 @@ def test_copy_default_advertised( |
156 | 174 | assert load_answersfile_data(".").get("_commit") == "v2" |
157 | 175 |
|
158 | 176 |
|
| 177 | +@pytest.mark.skipif( |
| 178 | + condition=platform.system() == "Windows", |
| 179 | + reason="pexpect.spawn does not work on Windows", |
| 180 | +) |
| 181 | +def test_path_completion(tmp_path_factory: pytest.TempPathFactory) -> None: |
| 182 | + """Test that file paths can handle tab completion.""" |
| 183 | + from pexpect.pty_spawn import spawn as pexpect_spawn |
| 184 | + |
| 185 | + src, dst, completedir = map(tmp_path_factory.mktemp, ("src", "dst", "my-directory")) |
| 186 | + with local.cwd(src): |
| 187 | + build_file_tree(PATH_TREE) |
| 188 | + git_save(tag="v1") |
| 189 | + git("commit", "--allow-empty", "-m", "v2") |
| 190 | + git("tag", "v2") |
| 191 | + with local.cwd(dst): |
| 192 | + # Disable subprocess timeout if debugging (except coverage) |
| 193 | + # See https://stackoverflow.com/a/67065084/1468388 |
| 194 | + tracer = getattr(sys, "gettrace", lambda: None)() |
| 195 | + timeout = 10 if not isinstance(tracer, (CTracer, type(None))) else None |
| 196 | + |
| 197 | + # Copy the v1 template |
| 198 | + cmd = COPIER_PATH + ("copy", str(src), ".", "--vcs-ref=v1") |
| 199 | + tui = pexpect_spawn(cmd[0], list(cmd[1:]), timeout=timeout) |
| 200 | + |
| 201 | + # Check that default values are maintained |
| 202 | + expect_prompt(tui, "current_location", "path") |
| 203 | + tui.sendline() |
| 204 | + |
| 205 | + # Check tab completion of a filesystem path (/path/to/my-direct<TAB> should |
| 206 | + # complete to /path/to/my-directory0) |
| 207 | + expect_prompt(tui, "star_location", "path", help="Location of a bonus star") |
| 208 | + tui.send(str(completedir)[:-4]) |
| 209 | + tui.send(Keyboard.Tab) |
| 210 | + tui.sendline() |
| 211 | + tui.sendline() |
| 212 | + |
| 213 | + tui.expect_exact(pexpect.EOF) |
| 214 | + |
| 215 | + answers = load_answersfile_data(".") |
| 216 | + assert answers.get("_commit") == "v1" |
| 217 | + assert answers.get("current_location") == "/dev/warppipe0" |
| 218 | + assert answers.get("star_location") == str(completedir) |
| 219 | + |
| 220 | + |
159 | 221 | @pytest.mark.parametrize( |
160 | 222 | "name, args", |
161 | 223 | [ |
|
0 commit comments