fish-shell/tests/pexpects/private_mode.py
Fabian Boehm e66f6878b5 Make tests usable with path with spaces
This is somewhat subtle:

The #RUN line in a littlecheck file will be run by a posix shell,
which means the substitutions will also be mangled by it.

Now, we *have* shell-quoted them, but unfortunately what we need is to
quote them for inside a pre-existing layer of quotes, e.g.

    # RUN: fish -C 'set -g fish %fish'

here, %fish can't be replaced with `'path with spaces/fish'`, because
that ends up as

    # RUN: fish -C 'set -g fish 'path with spaces/fish''

which is just broken.

So instead, we pass it as a variable to that fish:

    # RUN: fish=%fish fish...

In addition, we need to not mangle the arguments in our test_driver.

For that, because we insist on posix shell, which has only one array,
and we source a file, we *need* to stop having that file use
arguments.

Which is okay - test_env.sh could previously be used to start a test,
and now it no longer can because that is test_*driver*.sh's job.

For the interactive tests, it's slightly different:

pexpect.spawn(foo) is sensitive to shell metacharacters like space.

So we shell-quote it.

But if you pass any args to pexpect.spawn, it no longer uses a shell,
and so we cannot shell-quote it.

There could be a better way to fix this?
2025-01-01 16:45:43 +01:00

71 lines
1.7 KiB
Python

#!/usr/bin/env python3
import os
import time
from pexpect_helper import SpawnedProc
sp = SpawnedProc()
sendline, sleep, expect_prompt, expect_str = (
sp.sendline,
sp.sleep,
sp.expect_prompt,
sp.expect_str,
)
# Helper to sendline and add to our view of history.
recorded_history = []
private_mode_active = False
fish_path = os.environ.get("fish")
# Send a line and record it in our history array if private mode is not active.
def sendline_record(s):
sendline(s)
if not private_mode_active:
recorded_history.append(s)
expect_prompt()
# Start off with no history.
sendline(r" builtin history clear; builtin history save")
expect_prompt()
# Ensure that fish_private_mode can be changed - see #7589.
sendline_record(r"echo before_private_mode")
expect_prompt("before_private_mode")
sendline(r" builtin history save")
expect_prompt()
# Enter private mode.
sendline_record(r"set -g fish_private_mode 1")
expect_prompt()
private_mode_active = True
sendline_record(r"echo check2 $fish_private_mode")
expect_prompt("check2 1")
# Nothing else gets added.
sendline_record(r"true")
expect_prompt()
sendline_record(r"false")
expect_prompt()
# Leave private mode. The command to leave it is still private.
sendline_record(r"set -ge fish_private_mode")
expect_prompt()
private_mode_active = False
# New commands get added.
sendline_record(r"set alpha beta")
expect_prompt()
# Check our history is what we expect.
# We have to wait for the time to tick over, else our item risks being discarded.
now = time.time()
start = int(now)
while now - start < 1:
sleep(now - start)
now = time.time()
sendline(r" builtin history save ; $fish -c 'string join \n -- $history'")
expect_prompt("\r\n".join(reversed(recorded_history)))