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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
|
# -*- coding: utf-8 -*-
import os
import sys
import shutil
from tempfile import mkdtemp
from sphinx_testing import with_tmpdir
from sphinx_testing.path import path
if sys.version_info < (2, 7):
import unittest2 as unittest
else:
import unittest
if sys.version_info < (3,):
unittest.TestCase.assertCountEqual = unittest.TestCase.assertItemsEqual
class TestPath(unittest.TestCase):
def test_instantiate(self):
p = path('/path/to/file')
self.assertIsInstance(p, path)
self.assertEqual('/path/to/file', p)
def test_suffix(self):
p = path('/path/to/file.ext')
self.assertEqual(".ext", p.suffix)
self.assertEqual("/path/to/file", p.stem)
def test_basename(self):
p = path('/path/to/file')
self.assertEqual("file", p.basename())
self.assertEqual("file", p.name)
def test_dirname(self):
p = path('/path/to/file')
self.assertIsInstance(p.dirname(), path)
self.assertEqual("/path/to", p.dirname())
self.assertEqual("/path/to", p.parent)
self.assertEqual("/path", p.dirname().dirname())
self.assertEqual("/", p.dirname().dirname().dirname())
def test_abspath(self):
p = path('.')
self.assertIsInstance(p.abspath(), path)
self.assertEqual(os.getcwd(), p.abspath())
def test_isabs(self):
p = path('path/to/file')
self.assertFalse(p.isabs())
p = path('/path/to/file')
self.assertTrue(p.isabs())
def test_isdir(self):
p = path(__file__)
self.assertFalse(p.isdir()) # file
self.assertTrue(p.dirname().isdir()) # directory
p = path('/path/to/file') # not exists
self.assertFalse(p.isdir())
def test_isfile(self):
p = path(__file__)
self.assertTrue(p.isfile()) # file
self.assertFalse(p.dirname().isfile()) # directory
p = path('/path/to/file') # not exists
self.assertFalse(p.isfile())
def test_islink(self):
try:
tmpdir = mkdtemp()
symlink = "%s/test.symlink" % tmpdir
os.symlink(__file__, symlink)
p = path(symlink)
self.assertTrue(p.islink()) # symlink
p = path(__file__)
self.assertFalse(p.islink()) # file
self.assertFalse(p.dirname().islink()) # directory
p = path('/path/to/file') # not exists
self.assertFalse(p.islink())
finally:
shutil.rmtree(tmpdir)
def test_ismount(self):
pass # FIXME: to be test
@with_tmpdir
def test_rmtree(self, tmpdir):
subdir = mkdtemp(dir=tmpdir)
filename = "%s/entry.txt" % subdir
open(filename, 'w').close() # create empty file
path(subdir).rmtree()
self.assertFalse(os.path.exists(filename))
self.assertFalse(os.path.exists(subdir))
with self.assertRaises(OSError):
path('/path/to/file').rmtree()
path('/path/to/file').rmtree(ignore_errors=True) # no exceptions
# error handler
result = []
def onerror(func, path, exc_info):
result.append((func, path, exc_info))
path('/path/to/file').rmtree(onerror=onerror)
self.assertNotEqual([], result) # errors are stacked
@with_tmpdir
def test_copytree(self, tmpdir):
subdir = mkdtemp(dir=tmpdir)
subsubdir = "%s/subdir" % subdir
filename = "%s/test.file" % subdir
symlink = "%s/test.symlink" % subdir
os.makedirs(subsubdir)
open(filename, 'w').close() # create empty file
os.symlink(__file__, symlink)
dstdir = os.path.join(tmpdir, "path/to/dstdir")
path(subdir).copytree(dstdir)
self.assertTrue(os.path.exists(subdir))
self.assertTrue(os.path.exists(dstdir))
self.assertTrue(os.path.isdir("%s/subdir" % dstdir))
self.assertTrue(os.path.isfile("%s/test.file" % dstdir))
self.assertTrue(os.path.isfile("%s/test.symlink" % dstdir))
self.assertFalse(os.path.islink("%s/test.symlink" % dstdir))
dstdir = os.path.join(tmpdir, "path/to/dstdir2")
path(subdir).copytree(dstdir, symlinks=True)
self.assertTrue(os.path.exists(subdir))
self.assertTrue(os.path.exists(dstdir))
self.assertTrue(os.path.isdir("%s/subdir" % dstdir))
self.assertTrue(os.path.isfile("%s/test.file" % dstdir))
self.assertTrue(os.path.isfile("%s/test.symlink" % dstdir))
self.assertTrue(os.path.islink("%s/test.symlink" % dstdir))
@with_tmpdir
def test_move(self, tmpdir):
subdir = mkdtemp(dir=tmpdir)
subsubdir = "%s/subdir" % subdir
filename = "%s/test.file" % subdir
symlink = "%s/test.symlink" % subdir
os.makedirs(subsubdir)
open(filename, 'w').close() # create empty file
os.symlink(__file__, symlink)
# rename
dstdir = os.path.join(tmpdir, "dstdir")
path(subdir).move(dstdir)
self.assertFalse(os.path.exists(subdir))
self.assertTrue(os.path.exists(dstdir))
self.assertTrue(os.path.isdir("%s/subdir" % dstdir))
self.assertTrue(os.path.isfile("%s/test.file" % dstdir))
self.assertTrue(os.path.islink("%s/test.symlink" % dstdir))
# move into the directory
dstdir2 = mkdtemp(dir=tmpdir)
dstsubdir = "%s/%s" % (dstdir2, os.path.basename(dstdir))
path(dstdir).move(dstdir2)
self.assertFalse(os.path.exists(dstdir))
self.assertTrue(os.path.exists(dstdir2))
self.assertTrue(os.path.exists(dstsubdir))
@with_tmpdir
def test_unlink(self, tmpdir):
filename = "%s/test.file" % tmpdir
symlink = "%s/test.symlink" % tmpdir
open(filename, 'w').close() # create empty file
os.symlink(__file__, symlink)
path(filename).unlink()
self.assertFalse(os.path.exists(filename))
path(symlink).unlink()
self.assertFalse(os.path.exists(symlink))
@with_tmpdir
def test_utime(self, tmpdir):
filename = "%s/test.file" % tmpdir
open(filename, 'w').close() # create empty file
path(filename).utime((123, 456))
self.assertEqual(123, os.stat(filename).st_atime)
self.assertEqual(456, os.stat(filename).st_mtime)
@with_tmpdir
def test_listdir(self, tmpdir):
subdir = "%s/subdir" % tmpdir
filename = "%s/test.file" % tmpdir
symlink = "%s/test.symlink" % tmpdir
os.makedirs(subdir)
open(filename, 'w').close() # create empty file
os.symlink(__file__, symlink)
files = path(tmpdir).listdir()
self.assertCountEqual(['subdir', 'test.file', 'test.symlink'], files)
@with_tmpdir
def test_write_text(self, tmpdir):
filename = "%s/test.file" % tmpdir
path(filename).write_text('hello world')
text = open(filename).read()
self.assertEqual('hello world', text)
@with_tmpdir
def test_read_text(self, tmpdir):
filename = "%s/test.file" % tmpdir
with open(filename, 'w') as fd:
fd.write('hello world')
self.assertEqual('hello world', path(filename).read_text())
@with_tmpdir
def test_write_bytes(self, tmpdir):
filename = "%s/test.file" % tmpdir
path(filename).write_bytes(b'hello world')
text = open(filename, 'rb').read()
self.assertEqual(b'hello world', text)
@with_tmpdir
def test_read_bytes(self, tmpdir):
filename = "%s/test.file" % tmpdir
with open(filename, 'wb') as fd:
fd.write(b'hello world')
self.assertEqual(b'hello world', path(filename).read_bytes())
@with_tmpdir
def test_exists(self, tmpdir):
subdir = "%s/subdir" % tmpdir
filename = "%s/test.file" % tmpdir
symlink1 = "%s/test.symlink" % tmpdir
symlink2 = "%s/test.symlink2" % tmpdir
os.makedirs(subdir)
open(filename, 'w').close() # create empty file
os.symlink(__file__, symlink1)
os.symlink('/path/to/file', symlink2)
# path#exists()
self.assertTrue(path(subdir).exists())
self.assertTrue(path(filename).exists())
self.assertTrue(path(symlink1).exists())
self.assertFalse(path(symlink2).exists())
self.assertFalse(path('/path/to/file').exists())
# path#lexists()
self.assertTrue(path(subdir).lexists())
self.assertTrue(path(filename).lexists())
self.assertTrue(path(symlink1).lexists())
self.assertTrue(path(symlink2).lexists())
self.assertFalse(path('/path/to/file').lexists())
@with_tmpdir
def test_makedirs(self, tmpdir):
try:
umask = os.umask(0o000) # reset umask at first
subdir = "%s/path/to/subdir" % tmpdir
path(subdir).makedirs()
self.assertTrue(os.path.isdir(subdir))
self.assertEqual(0o777, os.stat(subdir).st_mode & 0o777)
subdir = "%s/another/path/to/subdir" % tmpdir
path(subdir).makedirs(0o700)
self.assertTrue(os.path.isdir(subdir))
self.assertEqual(0o700, os.stat(subdir).st_mode & 0o777)
finally:
os.umask(umask)
@with_tmpdir
def test_jonpath(self, tmpdir):
p = path('.')
self.assertEqual('./path/to/file', p.joinpath('path/to/file'))
self.assertEqual('/path/to/file', p.joinpath('/path/to/file'))
self.assertEqual('./path/to/file', p / 'path/to/file')
self.assertEqual('/path/to/file', p / '/path/to/file')
|