From f8c201992e10b14e9d1706c54e0f204cdd9504e5 Mon Sep 17 00:00:00 2001 From: Elmir Jagudin Date: Wed, 21 Aug 2013 09:01:30 +0200 Subject: [PATCH] unit test on buildbot.steps.transfer._FileWriter.__init__() Test creating an instance of _FileWrite class. Test the code path where destfile's parent directory is created. --- .../buildbot/test/unit/test_steps_transfer.py | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/master/buildbot/test/unit/test_steps_transfer.py b/master/buildbot/test/unit/test_steps_transfer.py index b06b57ef95f..e48a7b613fc 100644 --- a/master/buildbot/test/unit/test_steps_transfer.py +++ b/master/buildbot/test/unit/test_steps_transfer.py @@ -18,6 +18,7 @@ import tempfile, os import shutil import tarfile +import stat from twisted.trial import unittest from mock import Mock @@ -33,6 +34,46 @@ from buildbot.test.fake.remotecommand import Expect, ExpectRemoteRef +# Test buildbot.steps.transfer._FileWriter class. +class TestFileWriter(unittest.TestCase): + + # test _FileWrite.__init__() method. + def testInit(self): + # + # patch functions called in constructor + # + + # patch os.path.exists() to always return False + mockedExists = Mock(return_value=False) + self.patch(os.path, "exists", mockedExists) + + # capture calls to os.makedirs() + mockedMakedirs = Mock() + self.patch(os, 'makedirs', mockedMakedirs) + + # capture calls to tempfile.mkstemp() + mockedMkstemp = Mock(return_value=(7, "tmpname")) + self.patch(tempfile, "mkstemp", mockedMkstemp) + + # capture calls to os.fdopen() + mockedFdopen = Mock() + self.patch(os, "fdopen", mockedFdopen) + + # + # call _FileWriter constructor + # + destfile = os.path.join("dir", "file") + transfer._FileWriter(destfile, 64, stat.S_IRUSR) + + # + # validate captured calls + # + absdir = os.path.dirname(os.path.abspath(os.path.join("dir", "file"))) + mockedExists.assert_called_once_with(absdir) + mockedMakedirs.assert_called_once_with(absdir) + mockedMkstemp.assert_called_once_with(dir=absdir) + mockedFdopen.assert_called_once_with(7, 'wb') + # Test buildbot.steps.transfer._TransferBuildStep class. class TestTransferBuildStep(unittest.TestCase):