def test_start_log_popen_on_unix(self, mock_open, mock_popen): ''' Test that start_log() uses popen() to initialise its log on Unix machines. ''' gs.start_log(mode='develop', vers='') mock_popen.assert_called_with('tee -a sconstruct.log', 'wb') gs.start_log(mode='develop', vers='', log='test_log.txt') mock_popen.assert_called_with('tee -a test_log.txt', 'wb')
def test_start_log_open_on_windows(self, mock_open, mock_popen): ''' Test that start_log() uses open() to initialise its log on Windows machines. ''' gs.start_log(mode='develop', vers='') mock_open.assert_called_with('sconstruct.log', 'ab') gs.start_log(mode='develop', vers='', log='test_log.txt') mock_open.assert_called_with('test_log.txt', 'ab') mock_popen.assert_not_called()
def test_start_log_open_on_windows(self, mock_lfs, mock_open, mock_popen): ''' Test that start_log() uses open() to initialise its log on Windows machines. ''' gs.start_log() mock_open.assert_called_with('sconstruct.log', 'wb') gs.start_log('test_log.txt') mock_open.assert_called_with('test_log.txt', 'wb') mock_popen.assert_not_called()
def test_start_log_stdout_on_windows(self, mock_lfs): ''' Test that start_log() leads stdout to be captured in a log file on Windows machines. ''' initial_stdout = sys.stdout gs.start_log() print "Test message" sys.stdout.close() sys.stdout = initial_stdout with open('sconstruct.log', 'rU') as f: log_contents = f.read() self.assertEqual("Test message", log_contents.strip())
def test_start_log_other_os(self): ''' Test start_log()'s behaviour when run on a platform other than Windows, Darwin, or Linux. (We don't expect it to change sys.stdout, but we expect it to set sys.stderr to sys.stdout.) ''' initial_stderr = sys.stderr initial_stdout = sys.stdout gs.start_log(mode='develop', vers='') self.assertEqual(initial_stdout, sys.stderr) self.assertEqual(initial_stdout, sys.stdout) test_file = mock.MagicMock() sys.stdout = test_file gs.start_log(mode='develop', vers='') self.assertEqual(sys.stderr, test_file)
def test_start_log_stdout_on_windows(self): ''' Test that start_log() leads stdout to be captured in a log file on Windows machines. ''' initial_stdout = sys.stdout test = "Test message" gs.start_log(mode='develop', vers='') print test sys.stdout.close() sys.stdout = initial_stdout with open('sconstruct.log', 'rU') as f: log_contents = f.read() message_match = '^\*\*\* New build: \{[0-9\s\-:]+\} \*\*\*\n%s$' % test self.assertTrue(re.search(message_match, log_contents.strip()))
def test_start_log_stdout_on_unix(self, mock_lfs): ''' Test that start_log() leads stdout to be captured in a log file on Unix machines. ''' # Save the initial standard output initial_stdout = sys.stdout # Call start_log(), which redirects standard output to a log gs.start_log() print "Test message" sys.stdout.close() # Restore the initial standard output sys.stdout = initial_stdout # Ensure that start_log() actually redicted standard output # to a log at the expected path. with open('sconstruct.log', 'rU') as f: log_contents = f.read() self.assertEqual("Test message", log_contents.strip())
def test_start_log_stdout_on_unix(self): ''' Test that start_log() leads stdout to be captured in a log file on Unix machines. ''' # Save the initial standard output initial_stdout = sys.stdout test = "Test message" # Call start_log(), which redirects standard output to a log gs.start_log(mode='develop', vers='') print test sys.stdout.close() # Restore the initial standard output sys.stdout = initial_stdout # Ensure that start_log() actually redirected standard output # to a log at the expected path. with open('sconstruct.log', 'rU') as f: log_contents = f.read() message_match = '^\*\*\* New build: \{[0-9\s\-:]+\} \*\*\*\n%s$' % test self.assertTrue(re.search(message_match, log_contents.strip()))
def test_invalid_mode(self): '''Check behaviour when mode argument is invalid''' with self.assertRaises(Exception): gs.start_log(mode='release', vers='') with self.assertRaises(Exception): gs.start_log(mode=[1, 2, 3], vers='') with self.assertRaises(Exception): gs.start_log(mode=None, vers='')
def test_start_log_nonstring_input(self): ''' Test start_log()'s behaviour when its log argument is not a string. ''' initial_stdout = sys.stdout with self.assertRaises(TypeError): gs.start_log(mode='develop', vers='', log=1) with self.assertRaises(TypeError): gs.start_log(mode='develop', vers='', log=True) with self.assertRaises(TypeError): gs.start_log(mode='develop', vers='', log=[1, 2, 3])
def test_start_log_nonstring_input(self, mock_lfs): ''' Test start_log()'s behaviour when its log argument is not a string. ''' initial_stdout = sys.stdout gs.start_log(1) self.check_log_creation('1', initial_stdout) gs.start_log(True) self.check_log_creation('True', initial_stdout) gs.start_log([1, 2, 3]) self.check_log_creation(['[1,', '2,', '3]'], initial_stdout)
def test_start_log_no_lfs(self, mock_check_lfs): # Check that start_log() fails if check_lfs() finds that # git-lfs is not installed mock_check_lfs.side_effect = self.check_lfs_effect with self.assertRaises(ex_classes.LFSError), nostderrout(): gs.start_log()