summaryrefslogtreecommitdiff
path: root/tools/fmt_test.py
diff options
context:
space:
mode:
authorAndy Hayden <andyhayden1@gmail.com>2019-05-30 13:40:40 -0700
committerRyan Dahl <ry@tinyclouds.org>2019-05-30 16:40:40 -0400
commit8fb44eba5bb9862de5fcc6c785eb6f21ecbd0aea (patch)
tree9c866283b36d8388af4fd0e72d2bf2da4b8b70e4 /tools/fmt_test.py
parent1540b36ce7a4740f4a87c564becca65ed8d97255 (diff)
chore: refactor python tests to use unittest (#2414)
Move every test to a method on DenoTestCase. test.py is a single TestSuite of every TestCase. Add a Spawn context manager for http_server, this is explicitly used where it's needed. Each python test file can now be run independently without needing to manually run http_server. Add --help and consistent flags using argparse for each python test, including --failfast. Use ColorTextTestRunner so that '... ok' is green.
Diffstat (limited to 'tools/fmt_test.py')
-rwxr-xr-xtools/fmt_test.py71
1 files changed, 35 insertions, 36 deletions
diff --git a/tools/fmt_test.py b/tools/fmt_test.py
index a4fb072f9..1d21fb740 100755
--- a/tools/fmt_test.py
+++ b/tools/fmt_test.py
@@ -1,47 +1,46 @@
#!/usr/bin/env python
# Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
+import json
import os
-import sys
-from util import mkdtemp, root_path, tests_path, run, green_ok
import shutil
-import json
+import sys
+
+from util import (DenoTestCase, mkdtemp, root_path, tests_path, run, test_main)
-def fmt_test(deno_exe):
- sys.stdout.write("fmt_test...")
- sys.stdout.flush()
- d = mkdtemp()
- try:
- fixed_filename = os.path.join(tests_path, "badly_formatted_fixed.js")
- src = os.path.join(tests_path, "badly_formatted.js")
- dst = os.path.join(d, "badly_formatted.js")
- shutil.copyfile(src, dst)
- # Set DENO_DIR to the temp dir so we test an initial fetch of prettier.
- # TODO(ry) This make the test depend on internet access which is not
- # ideal. We should have prettier in the repo already, and we could
- # fetch it instead through tools/http_server.py.
- deno_dir = d
+class FmtTest(DenoTestCase):
+ def test_fmt(self):
+ d = mkdtemp()
+ try:
+ fixed_filename = os.path.join(tests_path,
+ "badly_formatted_fixed.js")
+ src = os.path.join(tests_path, "badly_formatted.js")
+ dst = os.path.join(d, "badly_formatted.js")
+ shutil.copyfile(src, dst)
- # TODO(kt3k) The below line should be run([deno_exe, "fmt", dst], ...)
- # It should be updated when the below issue is addressed
- # https://github.com/denoland/deno_std/issues/330
- run([os.path.join(root_path, deno_exe), "fmt", "badly_formatted.js"],
- cwd=d,
- merge_env={"DENO_DIR": deno_dir})
- with open(fixed_filename) as f:
- expected = f.read()
- with open(dst) as f:
- actual = f.read()
- if expected != actual:
- print "Expected didn't match actual."
- print "expected: ", json.dumps(expected)
- print "actual: ", json.dumps(actual)
- sys.exit(1)
+ # Set DENO_DIR to the temp dir to test an initial fetch of prettier.
+ # TODO(ry) This make the test depend on internet access which is not
+ # ideal. We should have prettier in the repo already, and we could
+ # fetch it instead through tools/http_server.py.
+ deno_dir = d
- finally:
- shutil.rmtree(d)
- print green_ok()
+ # TODO(kt3k) Below can be run([deno_exe, "fmt", dst], ...)
+ # once the following issue is addressed:
+ # https://github.com/denoland/deno_std/issues/330
+ run([
+ os.path.join(root_path, self.deno_exe), "fmt",
+ "badly_formatted.js"
+ ],
+ cwd=d,
+ merge_env={"DENO_DIR": deno_dir})
+ with open(fixed_filename) as f:
+ expected = f.read()
+ with open(dst) as f:
+ actual = f.read()
+ self.assertEqual(expected, actual)
+ finally:
+ shutil.rmtree(d)
if __name__ == "__main__":
- fmt_test(sys.argv[1])
+ test_main()