1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-16 10:54:14 -05:00
denoland-deno/tools/permission_prompt_test.py

145 lines
4.8 KiB
Python
Raw Normal View History

#!/usr/bin/env python
# -*- coding: utf-8 -*-
2019-01-22 04:03:30 +09:00
# Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import os
import unittest
from test_util import DenoTestCase, run_tests
from util import tty_capture
PERMISSIONS_PROMPT_TEST_TS = "tools/permission_prompt_test.ts"
PROMPT_PATTERN = b'⚠️'
FIRST_CHECK_FAILED_PATTERN = b'First check failed'
PERMISSION_DENIED_PATTERN = b'PermissionDenied: permission denied'
@unittest.skipIf(os.name == 'nt', "Unable to test tty on Windows")
class BasePromptTest(object):
def _run_deno(self, flags, args, bytes_input):
"Returns (return_code, stdout, stderr)."
2019-05-03 23:15:16 +02:00
cmd = [self.deno_exe, "run"] + flags + [PERMISSIONS_PROMPT_TEST_TS
] + args
return tty_capture(cmd, bytes_input)
def test_allow_flag(self):
test_type = self.test_type
code, stdout, stderr = self._run_deno(
2019-04-07 00:13:06 +02:00
["--allow-" + test_type], ["needs" + test_type.capitalize()], b'')
2019-02-09 19:31:03 +09:00
assert code == 0
assert not PROMPT_PATTERN in stderr
assert not FIRST_CHECK_FAILED_PATTERN in stdout
assert not PERMISSION_DENIED_PATTERN in stderr
2019-02-09 19:31:03 +09:00
def test_yes_yes(self):
test_type = self.test_type
code, stdout, stderr = self._run_deno(
[], ["needs" + test_type.capitalize()], b'y\ny\n')
2019-02-09 19:31:03 +09:00
assert code == 0
assert PROMPT_PATTERN in stderr
assert not FIRST_CHECK_FAILED_PATTERN in stdout
assert not PERMISSION_DENIED_PATTERN in stderr
2019-02-09 19:31:03 +09:00
def test_yes_no(self):
test_type = self.test_type
code, stdout, stderr = self._run_deno(
[], ["needs" + test_type.capitalize()], b'y\nn\n')
2019-02-09 19:31:03 +09:00
assert code == 1
assert PROMPT_PATTERN in stderr
assert not FIRST_CHECK_FAILED_PATTERN in stdout
assert PERMISSION_DENIED_PATTERN in stderr
2019-02-09 19:31:03 +09:00
def test_no_no(self):
test_type = self.test_type
code, stdout, stderr = self._run_deno(
[], ["needs" + test_type.capitalize()], b'n\nn\n')
assert code == 1
assert PROMPT_PATTERN in stderr
assert FIRST_CHECK_FAILED_PATTERN in stdout
assert PERMISSION_DENIED_PATTERN in stderr
def test_no_yes(self):
test_type = self.test_type
code, stdout, stderr = self._run_deno(
[], ["needs" + test_type.capitalize()], b'n\ny\n')
assert code == 0
assert PROMPT_PATTERN in stderr
assert FIRST_CHECK_FAILED_PATTERN in stdout
assert not PERMISSION_DENIED_PATTERN in stderr
def test_allow(self):
test_type = self.test_type
code, stdout, stderr = self._run_deno(
[], ["needs" + test_type.capitalize()], b'a\n')
assert code == 0
assert PROMPT_PATTERN in stderr
assert not FIRST_CHECK_FAILED_PATTERN in stdout
assert not PERMISSION_DENIED_PATTERN in stderr
def test_deny(self):
test_type = self.test_type
code, stdout, stderr = self._run_deno(
[], ["needs" + test_type.capitalize()], b'd\n')
assert code == 1
assert PROMPT_PATTERN in stderr
assert FIRST_CHECK_FAILED_PATTERN in stdout
assert PERMISSION_DENIED_PATTERN in stderr
def test_unrecognized_option(self):
test_type = self.test_type
code, stdout, stderr = self._run_deno(
[], ["needs" + test_type.capitalize()], b'e\na\n')
assert code == 0
assert PROMPT_PATTERN in stderr
assert not FIRST_CHECK_FAILED_PATTERN in stdout
assert not PERMISSION_DENIED_PATTERN in stderr
assert b'Unrecognized option' in stderr
def test_no_prompt(self):
test_type = self.test_type
code, stdout, stderr = self._run_deno(
2019-04-07 00:13:06 +02:00
["--no-prompt"], ["needs" + test_type.capitalize()], b'')
assert code == 1
assert not PROMPT_PATTERN in stderr
assert FIRST_CHECK_FAILED_PATTERN in stdout
assert PERMISSION_DENIED_PATTERN in stderr
def test_no_prompt_allow(self):
test_type = self.test_type
code, stdout, stderr = self._run_deno(
2019-04-07 00:13:06 +02:00
["--no-prompt", "--allow-" + test_type],
["needs" + test_type.capitalize()], b'')
2019-01-13 01:29:45 +09:00
assert code == 0
assert not PROMPT_PATTERN in stderr
assert not FIRST_CHECK_FAILED_PATTERN in stdout
assert not PERMISSION_DENIED_PATTERN in stderr
class ReadPromptTest(DenoTestCase, BasePromptTest):
test_type = "read"
class WritePromptTest(DenoTestCase, BasePromptTest):
test_type = "write"
class EnvPromptTest(DenoTestCase, BasePromptTest):
test_type = "env"
class NetPromptTest(DenoTestCase, BasePromptTest):
test_type = "net"
class RunPromptTest(DenoTestCase, BasePromptTest):
test_type = "run"
def permission_prompt_tests():
return BasePromptTest.__subclasses__()
2018-11-30 03:27:41 -05:00
if __name__ == "__main__":
run_tests()