Skip to content

Commit 5960e39

Browse files
haydry
authored andcommitted
make tests quieter (#2468)
Don't mix every http request in with the tests output. Don't print that the file servers are starting unless -vv flag is passed. Capture the output of run with run_output which returns stdout, stderr and exit_code. Test against this rather than relying on sys.exit.
1 parent 4ea2df6 commit 5960e39

14 files changed

+117
-63
lines changed

.appveyor.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ build_script:
196196
test_script:
197197
- python tools\lint.py
198198
- python tools\test_format.py
199-
- ps: Exec { & python tools\test.py -v --build-dir $env:DENO_BUILD_PATH }
199+
- ps: Exec { & python tools\test.py --build-dir $env:DENO_BUILD_PATH }
200200

201201
after_test:
202202
# Delete the the rollup cache, which is unreliable, so that it doesn't get

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ script:
7373
- ./tools/lint.py
7474
- ./tools/test_format.py
7575
- ./tools/build.py -C target/release
76-
- DENO_BUILD_MODE=release ./tools/test.py -v
76+
- DENO_BUILD_MODE=release ./tools/test.py
7777

7878
jobs:
7979
fast_finish: true

tools/benchmark.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import json
1212
import time
1313
import shutil
14-
from util import run, run_output, root_path, build_path, executable_suffix
14+
from util import root_path, run, run_output, build_path, executable_suffix
1515
import tempfile
1616
import http_server
1717
import throughput_benchmark
@@ -212,7 +212,8 @@ def main(argv):
212212
print "Usage: tools/benchmark.py [build_dir]"
213213
sys.exit(1)
214214

215-
sha1 = run_output(["git", "rev-parse", "HEAD"]).strip()
215+
sha1 = run_output(["git", "rev-parse", "HEAD"],
216+
exit_on_fail=True).out.strip()
216217
http_server.spawn()
217218

218219
deno_exe = os.path.join(build_dir, "deno")

tools/deno_dir_test.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import os
66

77
from test_util import DenoTestCase, run_tests
8-
from util import mkdtemp, rmtree, run
8+
from util import mkdtemp, rmtree, run_output
99

1010

1111
class TestDenoDir(DenoTestCase):
@@ -38,7 +38,8 @@ def test_deno_dir(self):
3838
def run_deno(self, deno_dir=None):
3939
cmd = [self.deno_exe, "run", "tests/002_hello.ts"]
4040
deno_dir_env = {"DENO_DIR": deno_dir} if deno_dir is not None else None
41-
run(cmd, quiet=True, env=deno_dir_env)
41+
res = run_output(cmd, quiet=True, env=deno_dir_env)
42+
self.assertEqual(res.code, 0)
4243

4344

4445
if __name__ == '__main__':

tools/fetch_test.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,23 @@
22
# Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
33
import os
44
import shutil
5+
import sys
56

67
import http_server
78
from test_util import DenoTestCase, run_tests
89
from util import mkdtemp, tests_path, run_output
910

1011

11-
class FetchTest(DenoTestCase):
12+
class TestFetch(DenoTestCase):
1213
def test_fetch(self):
1314
deno_dir = mkdtemp()
1415
try:
1516
t = os.path.join(tests_path, "006_url_imports.ts")
16-
output = run_output([self.deno_exe, "fetch", t],
17+
result = run_output([self.deno_exe, "fetch", t],
18+
quiet=True,
1719
merge_env={"DENO_DIR": deno_dir})
18-
assert output == ""
20+
self.assertEqual(result.out, "")
21+
self.assertEqual(result.code, 0)
1922
# Check that we actually did the prefetch.
2023
os.path.exists(
2124
os.path.join(

tools/fmt_test.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
import shutil
55

66
from test_util import DenoTestCase, run_tests
7-
from util import mkdtemp, root_path, tests_path, run
7+
from util import mkdtemp, root_path, tests_path, run_output
88

99

10-
class FmtTest(DenoTestCase):
10+
class TestFmt(DenoTestCase):
1111
def test_fmt(self):
1212
d = mkdtemp()
1313
try:
@@ -26,12 +26,15 @@ def test_fmt(self):
2626
# TODO(kt3k) Below can be run([deno_exe, "fmt", dst], ...)
2727
# once the following issue is addressed:
2828
# https://github.com/denoland/deno_std/issues/330
29-
run([
29+
result = run_output([
3030
os.path.join(root_path, self.deno_exe), "fmt",
3131
"badly_formatted.js"
3232
],
33-
cwd=d,
34-
merge_env={"DENO_DIR": deno_dir})
33+
cwd=d,
34+
merge_env={"DENO_DIR": deno_dir},
35+
exit_on_fail=True,
36+
quiet=True)
37+
self.assertEqual(result.code, 0)
3538
with open(fixed_filename) as f:
3639
expected = f.read()
3740
with open(dst) as f:

tools/http_server.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,17 @@
1717
ANOTHER_REDIRECT_PORT = 4547
1818
DOUBLE_REDIRECTS_PORT = 4548
1919

20+
QUIET = '-v' not in sys.argv and '--verbose' not in sys.argv
2021

21-
class ContentTypeHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
22+
23+
class QuietSimpleHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
24+
def log_request(self, code='-', size='-'):
25+
if not QUIET:
26+
SimpleHTTPServer.SimpleHTTPRequestHandler.log_request(
27+
self, code, size)
28+
29+
30+
class ContentTypeHandler(QuietSimpleHTTPRequestHandler):
2231
def do_GET(self):
2332
if "multipart_form_data.txt" in self.path:
2433
self.protocol_version = 'HTTP/1.1'
@@ -102,15 +111,16 @@ def server():
102111
})
103112
SocketServer.TCPServer.allow_reuse_address = True
104113
s = SocketServer.TCPServer(("", PORT), Handler)
105-
print "Deno test server http://localhost:%d/" % PORT
114+
if not QUIET:
115+
print "Deno test server http://localhost:%d/" % PORT
106116
return RunningServer(s, start(s))
107117

108118

109119
def base_redirect_server(host_port, target_port, extra_path_segment=""):
110120
os.chdir(root_path)
111121
target_host = "http://localhost:%d" % target_port
112122

113-
class RedirectHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
123+
class RedirectHandler(QuietSimpleHTTPRequestHandler):
114124
def do_GET(self):
115125
self.send_response(301)
116126
self.send_header('Location',
@@ -120,8 +130,9 @@ def do_GET(self):
120130
Handler = RedirectHandler
121131
SocketServer.TCPServer.allow_reuse_address = True
122132
s = SocketServer.TCPServer(("", host_port), Handler)
123-
print "redirect server http://localhost:%d/ -> http://localhost:%d/" % (
124-
host_port, target_port)
133+
if not QUIET:
134+
print "redirect server http://localhost:%d/ -> http://localhost:%d/" % (
135+
host_port, target_port)
125136
return RunningServer(s, start(s))
126137

127138

@@ -153,7 +164,8 @@ def start(s):
153164
def spawn():
154165
servers = (server(), redirect_server(), another_redirect_server(),
155166
double_redirects_server())
156-
sleep(1) # TODO I'm too lazy to figure out how to do this properly.
167+
while any(not s.thread.is_alive() for s in servers):
168+
sleep(0.01)
157169
try:
158170
yield
159171
finally:

tools/setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#!/usr/bin/env python
22
# Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
33
import third_party
4-
from util import build_mode, build_path, enable_ansi_colors, root_path, run
5-
from util import shell_quote, run_output
4+
from util import (build_mode, build_path, enable_ansi_colors, root_path, run,
5+
shell_quote)
66
import os
77
import re
88
import sys

tools/target_test.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import sys
33

44
from test_util import DenoTestCase, run_tests
5-
from util import executable_suffix, run, tests_path, run_output
5+
from util import executable_suffix, tests_path, run, run_output
66

77

88
class TestTarget(DenoTestCase):
@@ -20,7 +20,7 @@ def _test(self, executable):
2020
"Test executable runs and exits with code 0."
2121
bin_file = os.path.join(self.build_dir, executable + executable_suffix)
2222
self.check_exists(bin_file)
23-
run([bin_file])
23+
run([bin_file], quiet=True)
2424

2525
def test_libdeno(self):
2626
self._test("libdeno_test")
@@ -35,26 +35,31 @@ def test_core_http_benchmark(self):
3535
self._test("deno_core_http_bench_test")
3636

3737
def test_ts_library_builder(self):
38-
run([
38+
result = run_output([
3939
"node", "./node_modules/.bin/ts-node", "--project",
4040
"tools/ts_library_builder/tsconfig.json",
4141
"tools/ts_library_builder/test.ts"
42-
])
42+
],
43+
quiet=True)
44+
self.assertEqual(result.code, 0)
45+
assert "ts_library_builder ok" in result.out
4346

4447
def test_no_color(self):
4548
t = os.path.join(tests_path, "no_color.js")
46-
output = run_output([self.deno_exe, "run", t],
47-
merge_env={"NO_COLOR": "1"})
48-
assert output.strip() == "noColor true"
49+
result = run_output([self.deno_exe, "run", t],
50+
merge_env={"NO_COLOR": "1"},
51+
quiet=True)
52+
assert result.out.strip() == "noColor true"
4953
t = os.path.join(tests_path, "no_color.js")
50-
output = run_output([self.deno_exe, "run", t])
51-
assert output.strip() == "noColor false"
54+
result = run_output([self.deno_exe, "run", t], quiet=True)
55+
assert result.out.strip() == "noColor false"
5256

5357
def test_exec_path(self):
5458
cmd = [self.deno_exe, "run", "tests/exec_path.ts"]
55-
output = run_output(cmd)
56-
assert self.deno_exe in output.strip()
59+
result = run_output(cmd, quiet=True)
60+
assert self.deno_exe in result.out.strip()
61+
self.assertEqual(result.code, 0)
5762

5863

59-
if __name__ == "main":
64+
if __name__ == "__main__":
6065
run_tests()

tools/test.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
from benchmark_test import TestBenchmark
88
from deno_dir_test import TestDenoDir
9-
from fetch_test import FetchTest
10-
from fmt_test import FmtTest
9+
from fetch_test import TestFetch
10+
from fmt_test import TestFmt
1111
from integration_tests import TestIntegrations
1212
from repl_test import TestRepl
1313
from setup_test import TestSetup
@@ -21,7 +21,7 @@
2121

2222
import http_server
2323
from util import (enable_ansi_colors, build_path, RESET, FG_RED, FG_GREEN,
24-
executable_suffix, run, run_output, rmtree, tests_path)
24+
executable_suffix, rmtree, tests_path)
2525
from test_util import parse_test_args, run_tests
2626

2727

@@ -40,8 +40,8 @@ def main():
4040
TestUtil,
4141
TestTarget,
4242
JsUnitTests,
43-
FetchTest,
44-
FmtTest,
43+
TestFetch,
44+
TestFmt,
4545
TestIntegrations,
4646
TestRepl,
4747
TestDenoDir,

tools/test_format.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@
99

1010
def main():
1111
util.run([sys.executable, "tools/format.py"])
12-
output = util.run_output(
13-
["git", "status", "-uno", "--porcelain", "--ignore-submodules"])
14-
if len(output) > 0:
12+
result = util.run_output(
13+
["git", "status", "-uno", "--porcelain", "--ignore-submodules"],
14+
exit_on_fail=True)
15+
if result.out:
1516
print "Run tools/format.py "
16-
print output
17+
print result.out
1718
sys.exit(1)
1819

1920

tools/test_util.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
# Runs the full test suite.
44
# Usage: ./tools/test.py out/Debug
55
import argparse
6+
import contextlib
67
import os
78
import sys
89
import unittest
910

1011
from util import (enable_ansi_colors, build_path, RESET, FG_RED, FG_GREEN,
11-
executable_suffix, run, run_output, rmtree, tests_path)
12+
executable_suffix, rmtree, tests_path)
1213

1314

1415
class DenoTestCase(unittest.TestCase):
@@ -22,32 +23,31 @@ def setUpClass(cls):
2223

2324
# overload the test result class
2425
class ColorTextTestResult(unittest.TextTestResult):
26+
@contextlib.contextmanager
27+
def color(self, code):
28+
self.stream.write(code)
29+
try:
30+
yield
31+
finally:
32+
self.stream.write(RESET)
33+
2534
def getDescription(self, test):
2635
name = str(test)
2736
if name.startswith("test_"):
2837
name = name[5:]
2938
return name
3039

3140
def addSuccess(self, test):
32-
if self.showAll:
33-
self.stream.write(FG_GREEN)
34-
super(ColorTextTestResult, self).addSuccess(test)
35-
if self.showAll:
36-
self.stream.write(RESET)
41+
with self.color(FG_GREEN):
42+
super(ColorTextTestResult, self).addSuccess(test)
3743

3844
def addError(self, test, err):
39-
if self.showAll:
40-
self.stream.write(FG_RED)
41-
super(ColorTextTestResult, self).addError(test, err)
42-
if self.showAll:
43-
self.stream.write(RESET)
45+
with self.color(FG_RED):
46+
super(ColorTextTestResult, self).addError(test, err)
4447

4548
def addFailure(self, test, err):
46-
if self.showAll:
47-
self.stream.write(FG_RED)
48-
super(ColorTextTestResult, self).addFailure(test, err)
49-
if self.showAll:
50-
self.stream.write(RESET)
49+
with self.color(FG_RED):
50+
super(ColorTextTestResult, self).addFailure(test, err)
5151

5252

5353
class ColorTextTestRunner(unittest.TextTestRunner):
@@ -133,7 +133,7 @@ def run_tests(test_cases=None):
133133
suite = unittest.TestSuite(filtered_tests)
134134

135135
runner = ColorTextTestRunner(
136-
verbosity=args.verbose + 1, failfast=args.failfast)
136+
verbosity=args.verbose + 2, failfast=args.failfast)
137137

138138
result = runner.run(suite)
139139
if not result.wasSuccessful():

0 commit comments

Comments
 (0)