File: test_commands.py

package info (click to toggle)
testpath 0.6.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 216 kB
  • sloc: python: 509; makefile: 151; sh: 2
file content (45 lines) | stat: -rw-r--r-- 1,378 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
import os
from subprocess import call, check_output
import unittest

from testpath.commands import *

class CommandsTests(unittest.TestCase):
    def test_assert_calls(self):
        initial_path = os.environ['PATH']        
        
        with assert_calls('foobar'):
            call(['foobar'])
        
        with self.assertRaises(AssertionError):
            with assert_calls('foo'):
                pass
        
        # The context manager should clean up $PATH again
        self.assertEqual(os.environ['PATH'], initial_path)
    
    def test_assert_calls_with_args(self):
        with assert_calls('foo', ['bar', 'baz']):
            call(['foo', 'bar', 'baz'])
        
        with self.assertRaises(AssertionError):
            with assert_calls('cheese', ['crackers']):
                call(['cheese', 'biscuits'])
                call(['cheese', 'wine'])

    def test_assert_calls_twice(self):
        with assert_calls('git'):
            call(['git'])

        with self.assertRaises(AssertionError):
            with assert_calls('git'):
                pass

def test_fixed_output():
    t = 'Sat 24 Apr 17:11:58 BST 2021\n'
    with MockCommand.fixed_output('date', t) as mock_date:
        stdout = check_output(['date'])

    mock_date.assert_called([])
    assert len(mock_date.get_calls()) == 1
    assert stdout.decode('ascii', 'replace') == t