def checkMkdirError(self, quiet): """ Utility function to test _makeInfoFiles() when os.mkdir() fails. Patch os.mkdir() to raise an exception, and check that _makeInfoFiles() handles mkdir errors correctly. @param quiet: the value of 'quiet' argument for _makeInfoFiles() """ self.patch(os.path, "exists", mock.Mock(return_value=False)) # patch os.mkdir() to raise an exception self.patch(os, "mkdir", mock.Mock(side_effect=OSError(0, "err-msg"))) # call _makeInfoFiles() and check that correct exception is raised with self.assertRaisesRegex( create_worker.CreateWorkerError, "error creating directory %s: err-msg" % _regexp_path("bdir", "info")): create_worker._makeInfoFiles("bdir", quiet) # check output to stdout if quiet: self.assertWasQuiet() else: self.assertStdoutEqual("mkdir {0}\n".format( os.path.join("bdir", "info")))
def testInfoDirExists(self): """ test calling _makeInfoFiles() on basedir with fully populated 'info' directory """ self.patch(os.path, "exists", mock.Mock(return_value=True)) create_worker._makeInfoFiles("bdir", False) # there should be no messages to stdout self.assertWasQuiet()
def checkCreatedSuccessfully(self, quiet): """ Utility function to test _makeInfoFiles() when called on base directory that does not have 'info' sub-directory. @param quiet: the value of 'quiet' argument for _makeInfoFiles() """ # patch os.path.exists() to report the no dirs/files exists self.patch(os.path, "exists", mock.Mock(return_value=False)) # patch os.mkdir() to do nothing mkdir_mock = mock.Mock() self.patch(os, "mkdir", mkdir_mock) # capture calls to open() and write() self.setUpOpen() # call _makeInfoFiles() create_worker._makeInfoFiles("bdir", quiet) # check calls to os.mkdir() info_path = os.path.join("bdir", "info") mkdir_mock.assert_called_once_with(info_path) # check open() calls self.open.assert_has_calls( [mock.call(os.path.join(info_path, "admin"), "wt"), mock.call(os.path.join(info_path, "host"), "wt")] ) # check write() calls self.fileobj.write.assert_has_calls( [ mock.call("Your Name Here <*****@*****.**>\n"), mock.call("Please put a description of this build host here\n"), ] ) # check output to stdout if quiet: self.assertWasQuiet() else: self.assertStdoutEqual( "mkdir %s\n" "Creating %s, you need to edit it appropriately.\n" "Creating %s, you need to edit it appropriately.\n" "Not creating %s - add it if you wish\n" "Please edit the files in %s appropriately.\n" % ( info_path, os.path.join("info", "admin"), os.path.join("info", "host"), os.path.join("info", "access_uri"), info_path, ) )
def checkCreatedSuccessfully(self, quiet): """ Utility function to test _makeInfoFiles() when called on base directory that does not have 'info' sub-directory. @param quiet: the value of 'quiet' argument for _makeInfoFiles() """ # patch os.path.exists() to report the no dirs/files exists self.patch(os.path, "exists", mock.Mock(return_value=False)) # patch os.mkdir() to do nothing mkdir_mock = mock.Mock() self.patch(os, "mkdir", mkdir_mock) # capture calls to open() and write() self.setUpOpen() # call _makeInfoFiles() create_worker._makeInfoFiles("bdir", quiet) # check calls to os.mkdir() info_path = os.path.join("bdir", "info") mkdir_mock.assert_called_once_with(info_path) # check open() calls self.open.assert_has_calls([ mock.call(os.path.join(info_path, "admin"), "wt"), mock.call(os.path.join(info_path, "host"), "wt") ]) # check write() calls self.fileobj.write.assert_has_calls([ mock.call("Your Name Here <*****@*****.**>\n"), mock.call("Please put a description of this build host here\n") ]) # check output to the log if quiet: self.assertWasQuiet() else: self.assertLogged( re.escape("mkdir %s" % info_path), re.escape("Creating %s, you need to edit it appropriately." % os.path.join("info", "admin")), re.escape("Creating %s, you need to edit it appropriately." % os.path.join("info", "host")), re.escape("Not creating %s - add it if you wish" % os.path.join("info", "access_ur")), re.escape("Please edit the files in %s appropriately." % info_path))
def checkIOError(self, error_type, quiet): """ Utility function to test _makeInfoFiles() when open() or write() fails. Patch file IO functions to raise an exception, and check that _makeInfoFiles() handles file IO errors correctly. @param error_type: type of error to emulate, 'open' - patch open() to fail 'write' - patch write() to fail @param quiet: the value of 'quiet' argument for _makeInfoFiles() """ # patch os.path.exists() to simulate that 'info' directory exists # but not 'admin' or 'host' files self.patch(os.path, "exists", lambda path: path.endswith("info")) # set-up requested IO error if error_type == "open": self.setUpOpenError(strerror="info-err-msg") elif error_type == "write": self.setUpWriteError(strerror="info-err-msg") else: self.fail("unexpected error_type '{0}'".format(error_type)) # call _makeInfoFiles() and check that correct exception is raised with self.assertRaisesRegex( create_worker.CreateWorkerError, "could not write {0}: info-err-msg".format( _regexp_path("bdir", "info", "admin"))): create_worker._makeInfoFiles("bdir", quiet) # check output to stdout if quiet: self.assertWasQuiet() else: self.assertStdoutEqual( "Creating %s, you need to edit it appropriately.\n" % os.path.join("info", "admin"))