File: test_Application.py

package info (click to toggle)
python-biopython 1.78%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 65,756 kB
  • sloc: python: 221,141; xml: 178,777; ansic: 13,369; sql: 1,208; makefile: 131; sh: 70
file content (125 lines) | stat: -rw-r--r-- 4,331 bytes parent folder | download
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# Copyright 2013 by Peter Cock.  All rights reserved.
# This code is part of the Biopython distribution and governed by its
# license.  Please see the LICENSE file that should have been included
# as part of this package.

"""Bio.Application related tests for command line application wrappers.

This is intended to check generic things like argument parsing, and
stdin/stdout/stderr handling.
"""

import os
import unittest

from Bio.Application import AbstractCommandline, _Argument


class EchoApp(AbstractCommandline):
    """Minimal command line wrapper for echo command."""

    def __init__(self, cmd="echo", **kwargs):
        """Initialize wrapper for echo command."""
        self.parameters = [_Argument(["text"], "Text to echo")]
        AbstractCommandline.__init__(self, cmd, **kwargs)


class TestApp(unittest.TestCase):
    def test_echo(self):
        cline = EchoApp(text="Hello World")
        stdout, stderr = cline()
        self.assertEqual(stderr, "")
        self.assertEqual(stdout, "Hello World\n")

    def test_echo_capture_both(self):
        cline = EchoApp(text="Hello World")
        stdout, stderr = cline(stdout=True, stderr=True)
        self.assertEqual(stderr, "")
        self.assertEqual(stdout, "Hello World\n")

    def test_echo_capture_stdout(self):
        cline = EchoApp(text="Hello World")
        stdout, stderr = cline(stdout=True, stderr=False)
        self.assertEqual(stderr, None)
        self.assertEqual(stdout, "Hello World\n")

    def test_echo_capture_stderr(self):
        cline = EchoApp(text="Hello World")
        stdout, stderr = cline(stdout=False, stderr=True)
        self.assertEqual(stderr, "")
        self.assertEqual(stdout, None)

    def test_echo_capture_neither(self):
        cline = EchoApp(text="Hello World")
        stdout, stderr = cline(stdout=False, stderr=False)
        self.assertEqual(stderr, None)
        self.assertEqual(stdout, None)

    def test_echo_file_stdout(self):
        cline = EchoApp(text="Hello World")
        tmp = "echo_stdout.tmp"
        if os.path.isfile(tmp):
            os.remove(tmp)
        stdout, stderr = cline(stdout=tmp)
        self.assertEqual(stderr, "")
        self.assertEqual(stdout, None)
        self.assertTrue(os.path.isfile(tmp))
        with open(tmp) as handle:
            contents = handle.read()
        self.assertEqual(contents, "Hello World\n")
        os.remove(tmp)

    def test_echo_file_stderr(self):
        cline = EchoApp(text="Hello World")
        tmp = "echo_stderr.tmp"
        if os.path.isfile(tmp):
            os.remove(tmp)
        stdout, stderr = cline(stderr=tmp)
        self.assertEqual(stderr, None)
        self.assertEqual(stdout, "Hello World\n")
        self.assertTrue(os.path.isfile(tmp))
        with open(tmp) as handle:
            contents = handle.read()
        self.assertEqual(contents, "")
        os.remove(tmp)

    def test_echo_file_same(self):
        cline = EchoApp(text="Hello World")
        tmp = "echo_stdout_stderr.tmp"
        if os.path.isfile(tmp):
            os.remove(tmp)
        stdout, stderr = cline(stdout=tmp, stderr=tmp)
        self.assertEqual(stderr, None)
        self.assertEqual(stdout, None)
        self.assertTrue(os.path.isfile(tmp))
        with open(tmp) as handle:
            contents = handle.read()
        self.assertEqual(contents, "Hello World\n")  # stdout + stderr
        os.remove(tmp)

    def test_echo_file_both(self):
        cline = EchoApp(text="Hello World")
        tmp = "echo_stdout.tmp"
        if os.path.isfile(tmp):
            os.remove(tmp)
        tmp2 = "echo_stderr.tmp"
        if os.path.isfile(tmp2):
            os.remove(tmp2)
        stdout, stderr = cline(stdout=tmp, stderr=tmp2)
        self.assertEqual(stderr, None)
        self.assertEqual(stdout, None)
        self.assertTrue(os.path.isfile(tmp), tmp)
        with open(tmp) as handle:
            contents = handle.read()
        self.assertEqual(contents, "Hello World\n")  # stdout
        os.remove(tmp)
        self.assertTrue(os.path.isfile(tmp2), tmp2)
        with open(tmp2) as handle:
            contents = handle.read()
        self.assertEqual(contents, "")  # stderr
        os.remove(tmp2)


if __name__ == "__main__":
    runner = unittest.TextTestRunner(verbosity=2)
    unittest.main(testRunner=runner)