diff options
Diffstat (limited to 'tools')
| -rwxr-xr-x | tools/permission_prompt_test.py | 143 | ||||
| -rw-r--r-- | tools/permission_prompt_test.ts | 21 | ||||
| -rwxr-xr-x | tools/test.py | 7 | ||||
| -rw-r--r-- | tools/third_party.py | 4 |
4 files changed, 173 insertions, 2 deletions
diff --git a/tools/permission_prompt_test.py b/tools/permission_prompt_test.py new file mode 100755 index 000000000..2bc24d12c --- /dev/null +++ b/tools/permission_prompt_test.py @@ -0,0 +1,143 @@ +#!/usr/bin/env python +import os +import pty +import select +import subprocess + +from util import build_path, executable_suffix + +PERMISSIONS_PROMPT_TEST_TS = "tools/permission_prompt_test.ts" + + +# This function is copied from: +# https://gist.github.com/hayd/4f46a68fc697ba8888a7b517a414583e +# https://stackoverflow.com/q/52954248/1240268 +def tty_capture(cmd, bytes_input): + """Capture the output of cmd with bytes_input to stdin, + with stdin, stdout and stderr as TTYs.""" + mo, so = pty.openpty() # provide tty to enable line-buffering + me, se = pty.openpty() + mi, si = pty.openpty() + fdmap = {mo: 'stdout', me: 'stderr', mi: 'stdin'} + + p = subprocess.Popen( + cmd, bufsize=1, stdin=si, stdout=so, stderr=se, close_fds=True) + os.write(mi, bytes_input) + + timeout = .04 # seconds + res = {'stdout': b'', 'stderr': b''} + while True: + ready, _, _ = select.select([mo, me], [], [], timeout) + if ready: + for fd in ready: + data = os.read(fd, 512) + if not data: + break + res[fdmap[fd]] += data + elif p.poll() is not None: # select timed-out + break # p exited + for fd in [si, so, se, mi, mo, me]: + os.close(fd) # can't do it sooner: it leads to errno.EIO error + p.wait() + return p.returncode, res['stdout'], res['stderr'] + + +class Prompt(object): + def __init__(self, deno_exe): + self.deno_exe = deno_exe + + def run(self, + arg, + bytes_input, + allow_write=False, + allow_net=False, + allow_env=False): + "Returns (return_code, stdout, stderr)." + cmd = [self.deno_exe, PERMISSIONS_PROMPT_TEST_TS, arg] + if allow_write: + cmd.append("--allow-write") + if allow_net: + cmd.append("--allow-net") + if allow_env: + cmd.append("--allow-env") + return tty_capture(cmd, bytes_input) + + def warm_up(self): + # ignore the ts compiling message + self.run('needsWrite', b'', allow_write=True) + + def test_write_yes(self): + code, stdout, stderr = self.run('needsWrite', b'y\n') + assert code == 0 + assert stdout == b'' + assert b'Deno requests write access' in stderr + + def test_write_arg(self): + code, stdout, stderr = self.run('needsWrite', b'', allow_write=True) + assert code == 0 + assert stdout == b'' + assert stderr == b'' + + def test_write_no(self): + code, stdout, stderr = self.run('needsWrite', b'N\n') + assert code == 1 + # FIXME this error message should be in stderr + assert b'PermissionDenied: permission denied' in stdout + assert b'Deno requests write access' in stderr + + def test_env_yes(self): + code, stdout, stderr = self.run('needsEnv', b'y\n') + assert code == 0 + assert stdout == b'' + assert b'Deno requests access to environment' in stderr + + def test_env_arg(self): + code, stdout, stderr = self.run('needsEnv', b'', allow_env=True) + assert code == 0 + assert stdout == b'' + assert stderr == b'' + + def test_env_no(self): + code, stdout, stderr = self.run('needsEnv', b'N\n') + assert code == 1 + # FIXME this error message should be in stderr + assert b'PermissionDenied: permission denied' in stdout + assert b'Deno requests access to environment' in stderr + + def test_net_yes(self): + code, stdout, stderr = self.run('needsEnv', b'y\n') + assert code == 0 + assert stdout == b'' + assert b'Deno requests access to environment' in stderr + + def test_net_arg(self): + code, stdout, stderr = self.run('needsNet', b'', allow_net=True) + assert code == 0 + assert stdout == b'' + assert stderr == b'' + + def test_net_no(self): + code, stdout, stderr = self.run('needsNet', b'N\n') + assert code == 1 + # FIXME this error message should be in stderr + assert b'PermissionDenied: permission denied' in stdout + assert b'Deno requests network access' in stderr + + +def permission_prompt_test(deno_exe): + p = Prompt(deno_exe) + p.warm_up() + p.test_write_yes() + p.test_write_arg() + p.test_write_no() + p.test_env_yes() + p.test_env_arg() + p.test_env_no() + p.test_net_yes() + p.test_net_arg() + p.test_net_no() + + +if __name__ == "__main__": + deno_exe = os.path.join(build_path(), "deno" + executable_suffix) + permission_prompt_test(deno_exe) diff --git a/tools/permission_prompt_test.ts b/tools/permission_prompt_test.ts new file mode 100644 index 000000000..cf8a09805 --- /dev/null +++ b/tools/permission_prompt_test.ts @@ -0,0 +1,21 @@ +import { args, listen, env, exit, makeTempDirSync } from "deno"; + +const name = args[1]; +const test = { + needsWrite: () => { + makeTempDirSync(); + }, + needsEnv: () => { + env().home; + }, + needsNet: () => { + listen("tcp", "127.0.0.1:4540"); + } +}[name]; + +if (!test) { + console.log("Unknown test:", name); + exit(1); +} + +test(); diff --git a/tools/test.py b/tools/test.py index fb0544256..e2d077245 100755 --- a/tools/test.py +++ b/tools/test.py @@ -60,6 +60,13 @@ def main(argv): check_output_test(deno_exe) + # TODO We currently skip testing the prompt in Windows completely. + # Windows does not support the pty module used for testing the permission + # prompt. + if os.name != 'nt': + from permission_prompt_test import permission_prompt_test + permission_prompt_test(deno_exe) + rmtree(deno_dir) deno_dir_test(deno_exe, deno_dir) diff --git a/tools/third_party.py b/tools/third_party.py index d6c97f13b..65ff437b7 100644 --- a/tools/third_party.py +++ b/tools/third_party.py @@ -125,8 +125,8 @@ def run_cargo(): # If the lockfile ends up in the git repo, it'll make cargo hang for everyone # else who tries to run sync_third_party. def delete_lockfile(): - lockfiles = find_exts( - path.join(rust_crates_path, "registry/index"), '.cargo-index-lock') + lockfiles = find_exts([path.join(rust_crates_path, "registry/index")], + ['.cargo-index-lock']) for lockfile in lockfiles: os.remove(lockfile) |
