File: test_main.py

package info (click to toggle)
tap.py 3.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 476 kB
  • sloc: python: 1,808; makefile: 164; sh: 40
file content (61 lines) | stat: -rw-r--r-- 2,035 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import argparse
import os
from unittest import mock

from tap.loader import Loader
from tap.main import build_suite, get_status, main, main_module, parse_args
from tap.tests import TestCase


class TestMain(TestCase):
    """Tests for tap.main"""

    def test_exits_with_error(self):
        """The main function returns an error status if there were failures."""
        argv = ["/bin/fake", "fake.tap"]
        stream = open(os.devnull, "w")  # noqa: SIM115

        status = main(argv, stream=stream)

        self.assertEqual(1, status)

    def test_get_successful_status(self):
        result = mock.Mock()
        result.wasSuccessful.return_value = True
        self.assertEqual(0, get_status(result))

    @mock.patch.object(Loader, "load_suite_from_stdin")
    def test_build_suite_from_stdin(self, load_suite_from_stdin):
        args = mock.Mock()
        args.files = []
        expected_suite = mock.Mock()
        load_suite_from_stdin.return_value = expected_suite
        suite = build_suite(args)
        self.assertEqual(expected_suite, suite)

    @mock.patch.object(Loader, "load_suite_from_stdin")
    def test_build_suite_from_stdin_dash(self, load_suite_from_stdin):
        argv = ["/bin/fake", "-"]
        args = parse_args(argv)
        expected_suite = mock.Mock()
        load_suite_from_stdin.return_value = expected_suite
        suite = build_suite(args)
        self.assertEqual(expected_suite, suite)

    @mock.patch("tap.main.sys.stdin")
    @mock.patch("tap.main.sys.exit")
    @mock.patch.object(argparse.ArgumentParser, "print_help")
    def test_when_no_pipe_to_stdin(self, print_help, sys_exit, mock_stdin):
        argv = ["/bin/fake"]
        mock_stdin.isatty = mock.Mock(return_value=True)
        parse_args(argv)
        self.assertTrue(print_help.called)
        self.assertTrue(sys_exit.called)


class TestMainModule(TestCase):
    @mock.patch("tap.main.unittest")
    def test_main_set_to_stream(self, mock_unittest):
        main_module()

        assert mock_unittest.main.called