2018-10-27 09:11:39 -04:00
|
|
|
#!/usr/bin/env python
|
2019-02-07 16:19:50 -05:00
|
|
|
# -*- coding: utf-8 -*-
|
2019-01-21 14:03:30 -05:00
|
|
|
# Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2018-10-27 09:11:39 -04:00
|
|
|
import os
|
2019-05-27 09:27:55 -04:00
|
|
|
import unittest
|
2018-10-27 09:11:39 -04:00
|
|
|
|
2019-06-03 12:35:55 -04:00
|
|
|
from test_util import DenoTestCase, run_tests
|
|
|
|
from util import tty_capture
|
2018-10-27 09:11:39 -04:00
|
|
|
|
|
|
|
PERMISSIONS_PROMPT_TEST_TS = "tools/permission_prompt_test.ts"
|
|
|
|
|
2019-03-18 16:46:23 -04:00
|
|
|
PROMPT_PATTERN = b'⚠️'
|
|
|
|
FIRST_CHECK_FAILED_PATTERN = b'First check failed'
|
|
|
|
PERMISSION_DENIED_PATTERN = b'PermissionDenied: permission denied'
|
|
|
|
|
2018-10-27 09:11:39 -04:00
|
|
|
|
2019-05-30 16:40:40 -04:00
|
|
|
@unittest.skipIf(os.name == 'nt', "Unable to test tty on Windows")
|
|
|
|
class BasePromptTest(object):
|
2019-05-27 09:27:55 -04:00
|
|
|
def _run_deno(self, flags, args, bytes_input):
|
2018-10-27 09:11:39 -04:00
|
|
|
"Returns (return_code, stdout, stderr)."
|
2019-05-03 17:15:16 -04:00
|
|
|
cmd = [self.deno_exe, "run"] + flags + [PERMISSIONS_PROMPT_TEST_TS
|
|
|
|
] + args
|
2018-10-27 09:11:39 -04:00
|
|
|
return tty_capture(cmd, bytes_input)
|
|
|
|
|
2019-05-27 09:27:55 -04:00
|
|
|
def test_allow_flag(self):
|
|
|
|
test_type = self.test_type
|
|
|
|
code, stdout, stderr = self._run_deno(
|
2019-04-06 18:13:06 -04:00
|
|
|
["--allow-" + test_type], ["needs" + test_type.capitalize()], b'')
|
2019-02-09 05:31:03 -05:00
|
|
|
assert code == 0
|
2019-03-18 16:46:23 -04:00
|
|
|
assert not PROMPT_PATTERN in stderr
|
|
|
|
assert not FIRST_CHECK_FAILED_PATTERN in stdout
|
|
|
|
assert not PERMISSION_DENIED_PATTERN in stderr
|
2019-02-09 05:31:03 -05:00
|
|
|
|
2019-05-27 09:27:55 -04: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 05:31:03 -05:00
|
|
|
assert code == 0
|
2019-03-18 16:46:23 -04:00
|
|
|
assert PROMPT_PATTERN in stderr
|
|
|
|
assert not FIRST_CHECK_FAILED_PATTERN in stdout
|
|
|
|
assert not PERMISSION_DENIED_PATTERN in stderr
|
2019-02-09 05:31:03 -05:00
|
|
|
|
2019-05-27 09:27:55 -04: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 05:31:03 -05:00
|
|
|
assert code == 1
|
2019-03-18 16:46:23 -04:00
|
|
|
assert PROMPT_PATTERN in stderr
|
|
|
|
assert not FIRST_CHECK_FAILED_PATTERN in stdout
|
|
|
|
assert PERMISSION_DENIED_PATTERN in stderr
|
2019-02-09 05:31:03 -05:00
|
|
|
|
2019-05-27 09:27:55 -04: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')
|
2019-03-13 12:43:47 -04:00
|
|
|
assert code == 1
|
2019-03-18 16:46:23 -04:00
|
|
|
assert PROMPT_PATTERN in stderr
|
|
|
|
assert FIRST_CHECK_FAILED_PATTERN in stdout
|
|
|
|
assert PERMISSION_DENIED_PATTERN in stderr
|
2019-03-13 12:43:47 -04:00
|
|
|
|
2019-05-27 09:27:55 -04:00
|
|
|
def test_no_yes(self):
|
|
|
|
test_type = self.test_type
|
|
|
|
code, stdout, stderr = self._run_deno(
|
|
|
|
[], ["needs" + test_type.capitalize()], b'n\ny\n')
|
2018-10-27 09:11:39 -04:00
|
|
|
assert code == 0
|
|
|
|
|
2019-03-18 16:46:23 -04:00
|
|
|
assert PROMPT_PATTERN in stderr
|
|
|
|
assert FIRST_CHECK_FAILED_PATTERN in stdout
|
|
|
|
assert not PERMISSION_DENIED_PATTERN in stderr
|
2018-10-27 09:11:39 -04:00
|
|
|
|
2019-05-27 09:27:55 -04:00
|
|
|
def test_allow(self):
|
|
|
|
test_type = self.test_type
|
|
|
|
code, stdout, stderr = self._run_deno(
|
|
|
|
[], ["needs" + test_type.capitalize()], b'a\n')
|
2018-10-27 09:11:39 -04:00
|
|
|
assert code == 0
|
2019-03-18 16:46:23 -04:00
|
|
|
assert PROMPT_PATTERN in stderr
|
|
|
|
assert not FIRST_CHECK_FAILED_PATTERN in stdout
|
|
|
|
assert not PERMISSION_DENIED_PATTERN in stderr
|
2018-10-27 09:11:39 -04:00
|
|
|
|
2019-05-27 09:27:55 -04:00
|
|
|
def test_deny(self):
|
|
|
|
test_type = self.test_type
|
|
|
|
code, stdout, stderr = self._run_deno(
|
|
|
|
[], ["needs" + test_type.capitalize()], b'd\n')
|
2019-03-13 12:43:47 -04:00
|
|
|
assert code == 1
|
2019-03-18 16:46:23 -04:00
|
|
|
assert PROMPT_PATTERN in stderr
|
|
|
|
assert FIRST_CHECK_FAILED_PATTERN in stdout
|
|
|
|
assert PERMISSION_DENIED_PATTERN in stderr
|
2019-03-13 12:43:47 -04:00
|
|
|
|
2019-05-27 09:27:55 -04:00
|
|
|
def test_unrecognized_option(self):
|
|
|
|
test_type = self.test_type
|
|
|
|
code, stdout, stderr = self._run_deno(
|
|
|
|
[], ["needs" + test_type.capitalize()], b'e\na\n')
|
2018-10-27 09:11:39 -04:00
|
|
|
assert code == 0
|
2019-03-18 16:46:23 -04:00
|
|
|
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
|
|
|
|
|
2019-05-27 09:27:55 -04:00
|
|
|
def test_no_prompt(self):
|
|
|
|
test_type = self.test_type
|
|
|
|
code, stdout, stderr = self._run_deno(
|
2019-04-06 18:13:06 -04:00
|
|
|
["--no-prompt"], ["needs" + test_type.capitalize()], b'')
|
2018-10-27 09:11:39 -04:00
|
|
|
assert code == 1
|
2019-03-18 16:46:23 -04:00
|
|
|
assert not PROMPT_PATTERN in stderr
|
|
|
|
assert FIRST_CHECK_FAILED_PATTERN in stdout
|
|
|
|
assert PERMISSION_DENIED_PATTERN in stderr
|
|
|
|
|
2019-05-27 09:27:55 -04:00
|
|
|
def test_no_prompt_allow(self):
|
|
|
|
test_type = self.test_type
|
|
|
|
code, stdout, stderr = self._run_deno(
|
2019-04-06 18:13:06 -04:00
|
|
|
["--no-prompt", "--allow-" + test_type],
|
|
|
|
["needs" + test_type.capitalize()], b'')
|
2019-01-12 11:29:45 -05:00
|
|
|
assert code == 0
|
2019-03-18 16:46:23 -04:00
|
|
|
assert not PROMPT_PATTERN in stderr
|
|
|
|
assert not FIRST_CHECK_FAILED_PATTERN in stdout
|
|
|
|
assert not PERMISSION_DENIED_PATTERN in stderr
|
2019-03-13 12:43:47 -04:00
|
|
|
|
2018-10-27 09:11:39 -04:00
|
|
|
|
2019-05-30 16:40:40 -04:00
|
|
|
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"
|
2019-05-27 09:27:55 -04:00
|
|
|
|
|
|
|
|
2019-05-30 16:40:40 -04:00
|
|
|
class RunPromptTest(DenoTestCase, BasePromptTest):
|
|
|
|
test_type = "run"
|
2018-10-27 09:11:39 -04:00
|
|
|
|
|
|
|
|
2019-05-30 16:40:40 -04:00
|
|
|
def permission_prompt_tests():
|
|
|
|
return BasePromptTest.__subclasses__()
|
2018-11-30 03:27:41 -05:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2019-06-03 12:35:55 -04:00
|
|
|
run_tests()
|