From 273ca0c8da0d94f9d06ca16bd86c0301d9d06455 Mon Sep 17 00:00:00 2001 From: ed Date: Tue, 1 Jun 2021 05:49:41 +0200 Subject: [PATCH] run tests on commit --- scripts/install-githooks.sh | 12 ++++++++++++ scripts/run-tests.sh | 12 ++++++++++++ tests/test_httpcli.py | 15 +++++++++------ tests/test_vfs.py | 18 +++++++++--------- tests/util.py | 16 ++++++++++++---- 5 files changed, 54 insertions(+), 19 deletions(-) create mode 100755 scripts/install-githooks.sh create mode 100755 scripts/run-tests.sh diff --git a/scripts/install-githooks.sh b/scripts/install-githooks.sh new file mode 100755 index 00000000..465f4031 --- /dev/null +++ b/scripts/install-githooks.sh @@ -0,0 +1,12 @@ +#!/bin/bash +set -ex + +[ -e setup.py ] || .. +[ -e setup.py ] || { + echo u wot + exit 1 +} + +cd .git/hooks +rm -f pre-commit +ln -s ../../scripts/run-tests.sh pre-commit diff --git a/scripts/run-tests.sh b/scripts/run-tests.sh new file mode 100755 index 00000000..eb93d44b --- /dev/null +++ b/scripts/run-tests.sh @@ -0,0 +1,12 @@ +#!/bin/bash +set -ex + +pids=() +for py in python{2,3}; do + $py -m unittest discover -s tests >/dev/null & + pids+=($!) +done + +for pid in ${pids[@]}; do + wait $pid +done diff --git a/tests/test_httpcli.py b/tests/test_httpcli.py index d83be3a1..7d4edcc4 100644 --- a/tests/test_httpcli.py +++ b/tests/test_httpcli.py @@ -8,6 +8,7 @@ import time import shutil import pprint import tarfile +import tempfile import unittest from argparse import Namespace @@ -42,13 +43,15 @@ class Cfg(Namespace): class TestHttpCli(unittest.TestCase): - def test(self): - td = os.path.join(tu.get_ramdisk(), "vfs") - try: - shutil.rmtree(td) - except OSError: - pass + def setUp(self): + self.td = tu.get_ramdisk() + def tearDown(self): + os.chdir(tempfile.gettempdir()) + shutil.rmtree(self.td) + + def test(self): + td = os.path.join(self.td, "vfs") os.mkdir(td) os.chdir(td) diff --git a/tests/test_vfs.py b/tests/test_vfs.py index 04dd4331..633eca35 100644 --- a/tests/test_vfs.py +++ b/tests/test_vfs.py @@ -25,6 +25,13 @@ class Cfg(Namespace): class TestVFS(unittest.TestCase): + def setUp(self): + self.td = tu.get_ramdisk() + + def tearDown(self): + os.chdir(tempfile.gettempdir()) + shutil.rmtree(self.td) + def dump(self, vfs): print(json.dumps(vfs, indent=4, sort_keys=True, default=lambda o: o.__dict__)) @@ -55,12 +62,7 @@ class TestVFS(unittest.TestCase): pass def test(self): - td = os.path.join(tu.get_ramdisk(), "vfs") - try: - shutil.rmtree(td) - except OSError: - pass - + td = os.path.join(self.td, "vfs") os.mkdir(td) os.chdir(td) @@ -227,7 +229,7 @@ class TestVFS(unittest.TestCase): self.assertEqual(list(v1), list(v2)) # config file parser - cfg_path = os.path.join(tu.get_ramdisk(), "test.cfg") + cfg_path = os.path.join(self.td, "test.cfg") with open(cfg_path, "wb") as f: f.write( dedent( @@ -260,6 +262,4 @@ class TestVFS(unittest.TestCase): self.assertEqual(n.uwrite, ["asd"]) self.assertEqual(len(n.nodes), 0) - os.chdir(tempfile.gettempdir()) - shutil.rmtree(td) os.unlink(cfg_path) diff --git a/tests/util.py b/tests/util.py index 30864797..da5537b9 100644 --- a/tests/util.py +++ b/tests/util.py @@ -1,5 +1,6 @@ import os import time +import shutil import jinja2 import tempfile import subprocess as sp @@ -28,18 +29,25 @@ def chkcmd(*argv): def get_ramdisk(): + def subdir(top): + ret = os.path.join(top, "cptd-{}".format(os.getpid())) + shutil.rmtree(ret, True) + os.mkdir(ret) + return ret + for vol in ["/dev/shm", "/Volumes/cptd"]: # nosec (singleton test) if os.path.exists(vol): - return vol + return subdir(vol) if os.path.exists("/Volumes"): - devname, _ = chkcmd("hdiutil", "attach", "-nomount", "ram://32768") + # hdiutil eject /Volumes/cptd/ + devname, _ = chkcmd("hdiutil", "attach", "-nomount", "ram://65536") devname = devname.strip() print("devname: [{}]".format(devname)) for _ in range(10): try: _, _ = chkcmd("diskutil", "eraseVolume", "HFS+", "cptd", devname) - return "/Volumes/cptd" + return subdir("/Volumes/cptd") except Exception as ex: print(repr(ex)) time.sleep(0.25) @@ -50,7 +58,7 @@ def get_ramdisk(): try: os.mkdir(ret) finally: - return ret + return subdir(ret) class NullBroker(object):