test_plugin_diff.py 2.6 KB
Newer Older
A
Amador Pahim 已提交
1 2 3 4
import glob
import os
import tempfile
import shutil
5
import unittest
A
Amador Pahim 已提交
6 7 8 9 10 11 12 13 14

from avocado.core import exit_codes
from avocado.utils import process
from avocado.utils import script


basedir = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', '..')
basedir = os.path.abspath(basedir)

15 16
AVOCADO = os.environ.get("UNITTEST_AVOCADO_CMD", "./scripts/avocado")

A
Amador Pahim 已提交
17 18 19 20 21 22

class DiffTests(unittest.TestCase):

    def setUp(self):
        self.tmpdir = tempfile.mkdtemp(prefix='avocado_' + __name__)
        test = script.make_script(os.path.join(self.tmpdir, 'test'), 'exit 0')
23
        cmd_line = ('%s run %s '
A
Amador Pahim 已提交
24 25
                    '--external-runner /bin/bash '
                    '--job-results-dir %s --sysinfo=off --json -' %
26
                    (AVOCADO, test, self.tmpdir))
A
Amador Pahim 已提交
27 28 29 30 31
        expected_rc = exit_codes.AVOCADO_ALL_OK
        self.run_and_check(cmd_line, expected_rc)
        self.jobdir = ''.join(glob.glob(os.path.join(self.tmpdir, 'job-*')))

        self.tmpdir2 = tempfile.mkdtemp(prefix='avocado_' + __name__)
32
        cmd_line = ('%s run %s '
A
Amador Pahim 已提交
33 34
                    '--external-runner /bin/bash '
                    '--job-results-dir %s --sysinfo=off --json -' %
35
                    (AVOCADO, test, self.tmpdir2))
A
Amador Pahim 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48
        expected_rc = exit_codes.AVOCADO_ALL_OK
        self.run_and_check(cmd_line, expected_rc)
        self.jobdir2 = ''.join(glob.glob(os.path.join(self.tmpdir2, 'job-*')))

    def run_and_check(self, cmd_line, expected_rc):
        os.chdir(basedir)
        result = process.run(cmd_line, ignore_status=True)
        self.assertEqual(result.exit_status, expected_rc,
                         "Command %s did not return rc "
                         "%d:\n%s" % (cmd_line, expected_rc, result))
        return result

    def test_diff(self):
49 50
        cmd_line = ('%s diff %s %s' %
                    (AVOCADO, self.jobdir, self.jobdir2))
A
Amador Pahim 已提交
51 52
        expected_rc = exit_codes.AVOCADO_ALL_OK
        result = self.run_and_check(cmd_line, expected_rc)
53 54 55
        self.assertIn(b"# COMMAND LINE", result.stdout)
        self.assertIn(b"-./scripts/avocado run", result.stdout)
        self.assertIn(b"+./scripts/avocado run", result.stdout)
A
Amador Pahim 已提交
56 57

    def test_diff_nocmdline(self):
58 59
        cmd_line = ('%s diff %s %s --diff-filter nocmdline' %
                    (AVOCADO, self.jobdir, self.jobdir2))
A
Amador Pahim 已提交
60 61
        expected_rc = exit_codes.AVOCADO_ALL_OK
        result = self.run_and_check(cmd_line, expected_rc)
62
        self.assertNotIn(b"# COMMAND LINE", result.stdout)
A
Amador Pahim 已提交
63 64 65 66 67 68 69 70

    def tearDown(self):
        shutil.rmtree(self.tmpdir)
        shutil.rmtree(self.tmpdir2)


if __name__ == '__main__':
    unittest.main()