未验证 提交 44a6efeb 编写于 作者: A Amador Pahim

Merge branch 'ldoktor-testtmpdir'

......@@ -47,6 +47,11 @@ else:
import unittest
#: Environment variable used to store the location of a tmpfile which is
#: preserved across all tests execution (usually in one job)
COMMON_TMPDIR_NAME = 'AVOCADO_TESTS_COMMON_TMPDIR'
class NameNotTestNameError(Exception):
"""
......@@ -286,7 +291,7 @@ class Test(unittest.TestCase):
Returns the path of the temporary directory that will stay the
same for all tests in a given Job.
"""
env_var = 'AVOCADO_TESTS_COMMON_TMPDIR'
env_var = COMMON_TMPDIR_NAME
path = os.environ.get(env_var)
if path is None:
msg = 'Environment Variable %s is not set.' % env_var
......
......@@ -20,6 +20,7 @@ import shutil
import tempfile
from avocado.core.plugin_interfaces import JobPre, JobPost
from avocado.core import test
class TestsTmpDir(JobPre, JobPost):
......@@ -28,7 +29,7 @@ class TestsTmpDir(JobPre, JobPost):
description = 'Creates a temporary directory for tests consumption'
def __init__(self):
self._varname = 'AVOCADO_TESTS_COMMON_TMPDIR'
self._varname = test.COMMON_TMPDIR_NAME
self._dirname = None
def pre(self, job):
......
......@@ -11,6 +11,7 @@ else:
import unittest
from avocado.core import exit_codes
from avocado.core import test
from avocado.utils import process
from avocado.utils import script
......@@ -23,23 +24,21 @@ import tempfile
from avocado import Test
class MyTest(Test):
def test1(self):
file = os.path.join(self.teststmpdir,
next(tempfile._get_candidate_names()))
open(file, "w+").close()
tempfile.mkstemp(dir=self.teststmpdir)
if len(os.listdir(self.teststmpdir)) != 2:
self.fail()
"""
SIMPLE_SCRIPT = """#!/bin/bash
mktemp ${AVOCADO_TESTS_COMMON_TMPDIR}/XXXXXX
if [ $(ls ${AVOCADO_TESTS_COMMON_TMPDIR} | wc -l) == 1 ]
mktemp ${{{0}}}/XXXXXX
if [ $(ls ${{{0}}} | wc -l) == 1 ]
then
exit 0
else
exit 1
fi
"""
""".format(test.COMMON_TMPDIR_NAME)
class TestsTmpDirTests(unittest.TestCase):
......@@ -55,20 +54,45 @@ class TestsTmpDirTests(unittest.TestCase):
INSTRUMENTED_SCRIPT)
self.instrumented_test.save()
def run_and_check(self, cmd_line, expected_rc):
def run_and_check(self, cmd_line, expected_rc, env=None):
os.chdir(basedir)
result = process.run(cmd_line, ignore_status=True)
result = process.run(cmd_line, ignore_status=True, env=env)
self.assertEqual(result.exit_status, expected_rc,
"Command %s did not return rc "
"%d:\n%s" % (cmd_line, expected_rc, result))
return result
@unittest.skipIf(test.COMMON_TMPDIR_NAME in os.environ,
"%s already set in os.environ"
% test.COMMON_TMPDIR_NAME)
def test_tests_tmp_dir(self):
"""
Tests whether automatically created teststmpdir is shared across
all tests.
"""
cmd_line = ("./scripts/avocado run --sysinfo=off "
"--job-results-dir %s %s %s" %
(self.tmpdir, self.simple_test, self.instrumented_test))
self.run_and_check(cmd_line, exit_codes.AVOCADO_ALL_OK)
def test_manualy_created(self):
"""
Tests whether manually set teststmpdir is used and not deleted by
avocado
"""
shared_tmp = tempfile.mkdtemp(dir=self.tmpdir)
cmd = ("./scripts/avocado run --sysinfo=off "
"--job-results-dir %s %%s" % self.tmpdir)
self.run_and_check(cmd % self.simple_test, exit_codes.AVOCADO_ALL_OK,
{test.COMMON_TMPDIR_NAME: shared_tmp})
self.run_and_check(cmd % self.instrumented_test,
exit_codes.AVOCADO_ALL_OK,
{test.COMMON_TMPDIR_NAME: shared_tmp})
content = os.listdir(shared_tmp)
self.assertEqual(len(content), 2, "The number of tests in manually "
"set teststmpdir is not 2 (%s):\n%s"
% (len(content), content))
def tearDown(self):
shutil.rmtree(self.tmpdir)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册