Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

# -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*- 

# 

# Copyright (C) 2015 Canonical Ltd 

# 

# This program is free software: you can redistribute it and/or modify 

# it under the terms of the GNU General Public License version 3 as 

# published by the Free Software Foundation. 

# 

# This program is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 

# GNU General Public License for more details. 

# 

# You should have received a copy of the GNU General Public License 

# along with this program.  If not, see <http://www.gnu.org/licenses/>. 

 

import io 

import logging 

from unittest import mock 

 

from snapcraft import ( 

    log, 

    tests 

) 

 

 

@mock.patch('os.isatty', return_value=True) 

@mock.patch('sys.stdout', new_callable=io.StringIO) 

@mock.patch('sys.stderr', new_callable=io.StringIO) 

class LogTestCase(tests.TestCase): 

 

    def test_configure_must_send_messages_to_stdout( 

            self, mock_stderr, mock_stdout, mock_isatty): 

        logger_name = self.id() 

        log.configure(logger_name) 

        logger = logging.getLogger(logger_name) 

        # Overwrite the level to log everything. 

        logger.setLevel(logging.DEBUG) 

 

        logger.debug('Test debug') 

        logger.info('Test info') 

        logger.warning('Test warning') 

 

        expected_out = ( 

            '\033[1mTest debug\033[0m\n' 

            '\033[1mTest info\033[0m\n' 

            '\033[1mTest warning\033[0m\n') 

        self.assertEqual(expected_out, mock_stdout.getvalue()) 

        self.assertEqual('', mock_stderr.getvalue()) 

 

    def test_configure_must_send_errors_to_stderr( 

            self, mock_stderr, mock_stdout, mock_isatty): 

        logger_name = self.id() 

        log.configure(logger_name) 

        logger = logging.getLogger(logger_name) 

        # Overwrite the level to log everything. 

        logger.setLevel(logging.DEBUG) 

 

        logger.error('Test error') 

        logger.critical('Test critical') 

 

        expected_err = ( 

            '\033[1mTest error\033[0m\n' 

            '\033[1mTest critical\033[0m\n') 

        self.assertEqual(expected_err, mock_stderr.getvalue()) 

        self.assertEqual('', mock_stdout.getvalue()) 

 

    def test_configure_must_log_info_and_higher( 

            self, mock_stderr, mock_stdout, mock_isatty): 

        logger_name = self.id() 

        log.configure(logger_name) 

        logger = logging.getLogger(logger_name) 

 

        logger.debug('Test debug') 

        logger.info('Test info') 

        logger.warning('Test warning') 

        logger.error('Test error') 

        logger.critical('Test critical') 

 

        expected_out = ( 

            '\033[1mTest info\033[0m\n' 

            '\033[1mTest warning\033[0m\n') 

        expected_err = ( 

            '\033[1mTest error\033[0m\n' 

            '\033[1mTest critical\033[0m\n') 

        self.assertEqual(expected_out, mock_stdout.getvalue()) 

        self.assertEqual(expected_err, mock_stderr.getvalue())