From cca654bd475bf7401bfce76afdcb6bd62c1cad24 Mon Sep 17 00:00:00 2001 From: Etienne Wodey Date: Mon, 21 Jun 2021 10:39:39 +0200 Subject: [PATCH] test_device_db: fix on Windows (tempfile access limitations) Signed-off-by: Etienne Wodey --- artiq/test/test_device_db.py | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/artiq/test/test_device_db.py b/artiq/test/test_device_db.py index a05093aee..cde57e200 100644 --- a/artiq/test/test_device_db.py +++ b/artiq/test/test_device_db.py @@ -1,5 +1,6 @@ """Test device DB interface""" +import os import unittest import tempfile from pathlib import Path @@ -22,19 +23,27 @@ device_db = { } """ +# tempfile.NamedTemporaryFile: +# use delete=False and manual cleanup +# for Windows compatibility + class TestDeviceDBImport(unittest.TestCase): def test_no_device_db_in_file(self): - with tempfile.NamedTemporaryFile(mode="w+", suffix=".py") as f: + with tempfile.NamedTemporaryFile(mode="w+", suffix=".py", delete=False) as f: + print(f.name) print("", file=f, flush=True) with self.assertRaisesRegex(KeyError, "device_db"): DeviceDB(f.name) + os.unlink(f.name) + def test_import_same_level(self): with tempfile.TemporaryDirectory() as tmpdir: # make sure both files land in the same directory - args = dict(mode="w+", suffix=".py", dir=tmpdir) + # tempfiles are cleanup together with tmpdir + args = dict(mode="w+", suffix=".py", dir=tmpdir, delete=False) with tempfile.NamedTemporaryFile( **args ) as fileA, tempfile.NamedTemporaryFile(**args) as fileB: @@ -58,11 +67,17 @@ device_db["new_core_alias"] = "core" class TestDeviceDB(unittest.TestCase): def setUp(self): - self.ddb_file = tempfile.NamedTemporaryFile(mode="w+", suffix=".py") + self.ddb_file = tempfile.NamedTemporaryFile( + mode="w+", suffix=".py", delete=False + ) print(DUMMY_DDB_FILE, file=self.ddb_file, flush=True) self.ddb = DeviceDB(self.ddb_file.name) + def tearDown(self): + self.ddb_file.close() + os.unlink(self.ddb_file.name) + def test_get(self): core = self.ddb.get("core") self.assertEqual(core["class"], "Core")