def test_success(self): with mock.patch('os.makedirs') as os_makedirs: path.makedirs('foo') path.makedirs('bar', 0o666) self.assertEqual(os_makedirs.mock_calls, [ mock.call('foo', 0o777), mock.call('bar', 0o666), ])
def test_exists(self): def mock_makedirs(path, mode): raise OSError(errno.EEXIST, 'msg') with mock.patch('os.makedirs', mock_makedirs), \ mock.patch('os.path.isdir', lambda x: x == 'dir'): # noqa self.assertRaises(OSError, path.makedirs, 'file') self.assertRaises(OSError, path.makedirs, 'file', exist_ok=True) self.assertRaises(OSError, path.makedirs, 'dir') path.makedirs('dir', exist_ok=True)
def cleandir(path, recreate=True): if os.path.exists(path): # Windows seems to keep an executable file open a little bit after the # process returns from wait(), so try a few times, sleeping a bit in # between. for t in [0.1, 0.25, 0.5, 1.0, 2.0, None]: try: shutil.rmtree(path) break except OSError: if t is None: raise RuntimeError('unable to remove {}'.format(path)) time.sleep(t) if recreate: makedirs(path)