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_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)
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))
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))
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))
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)
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')
def test_astah(self, app, status, warnings): # first build app.build() print(status.getvalue(), warnings.getvalue()) html = (app.outdir / 'index.html').read_text() image_files = (app.outdir / '_images').listdir() self.assertEqual(1, len(image_files)) image_filename = image_files[0] self.assertRegexpMatches( html, '<img alt="(_images/%s)" src="\\1" />' % image_filename) if not self.skip_image_check: expected = path("tests/examples/animal.png").read_bytes() actual = (app.outdir / '_images' / image_filename).read_bytes() self.assertEqual(expected, actual) # second build (no updates) status.truncate(0) warnings.truncate(0) app.build() self.assertIn('0 added, 0 changed, 0 removed', status.getvalue()) # thrid build (.vsdx file has changed) status.truncate(0) warnings.truncate(0) (app.srcdir / 'animal.asta').utime((time(), time())) app.build() self.assertIn('0 added, 1 changed, 0 removed', status.getvalue())
def test_visio(self, app, status, warnings): # first build app.build() print status.getvalue(), warnings.getvalue() html = (app.outdir / 'index.html').read_text() image_files = (app.outdir / '_images').listdir() self.assertEqual(1, len(image_files)) image_filename = image_files[0] self.assertRegexpMatches(html, '<img alt="(_images/%s)" src="\\1" />' % image_filename) expected = path("tests/examples/singlepage.png").read_bytes() actual = (app.outdir / '_images' / image_filename).read_bytes() self.assertEqual(expected, actual) # second build (no updates) status.truncate(0) warnings.truncate(0) app.build() self.assertIn('0 added, 0 changed, 0 removed', status.getvalue()) # thrid build (.vsdx file has changed) status.truncate(0) warnings.truncate(0) (app.srcdir / 'singlepage.vsdx').utime((time(), time())) app.build() self.assertIn('0 added, 1 changed, 0 removed', status.getvalue())
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_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 mkdtemp(suffix='', prefix='tmp', dir=None): import tempfile if isinstance(dir, path): tmpdir = tempfile.mkdtemp(suffix, prefix, str(dir)) else: tmpdir = tempfile.mkdtemp(suffix, prefix, dir) return path(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)
def test_name_option(self, app, status, warnings): # first build app.build() html = (app.outdir / 'index.html').read_text() image_files = (app.outdir / '_images').listdir() self.assertEqual(1, len(image_files)) image_filename = image_files[0] self.assertRegexpMatches(html, '<img alt="(_images/%s)" src="\\1" />' % image_filename) expected = path("tests/examples/multipages-2.png").read_bytes() actual = (app.outdir / '_images' / image_filename).read_bytes() self.assertEqual(expected, actual)
def test_with_app_bad_args(self, _mkdtemp): tmpdir = _mkdtemp.return_value = mkdtemp() srcdir = path(__file__).dirname() / 'examples' @with_app(srcdir=srcdir, copy_srcdir_to_tmpdir=True) def execute(oops): pass with self.assertRaises(TypeError): # TypeError: execute() takes 1 positional argument but 3 were given execute() self.assertFalse(tmpdir.exists())
def test_TestApp(self): try: srcdir = path(__file__).dirname() / 'examples' app = TestApp(srcdir=srcdir) self.assertIsInstance(app._status, StringIO) self.assertIsInstance(app._warning, StringIO) if sphinx.__version__ < '1.0.0': app.build(True, None) else: app.build() self.assertIn('index.html', os.listdir(app.outdir)) finally: app.cleanup()
def test_name_option(self, app, status, warnings): # first build app.build() html = (app.outdir / 'index.html').read_text() image_files = (app.outdir / '_images').listdir() self.assertEqual(1, len(image_files)) image_filename = image_files[0] self.assertRegexpMatches( html, '<img alt="(_images/%s)" src="\\1" />' % image_filename) expected = path("tests/examples/multipages-2.png").read_bytes() actual = (app.outdir / '_images' / image_filename).read_bytes() self.assertEqual(expected, actual)
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())
def test_image(self, app, status, warnings): app.build() print(status.getvalue(), warnings.getvalue()) html = (app.outdir / 'index.html').read_text() image_files = (app.outdir / '_images').listdir() self.assertEqual(1, len(image_files)) image_filename = image_files[0] self.assertRegexpMatches( html, '<img alt="(_images/%s)" src="\\1" />' % image_filename) if not self.skip_image_check: expected = path("tests/examples/animal.png").read_bytes() actual = (app.outdir / '_images' / image_filename).read_bytes() self.assertEqual(expected, actual)
def test_TestApp_when_srcdir_specified(self): try: srcdir = path(__file__).dirname() / 'examples' app = TestApp(srcdir=srcdir) self.assertEqual(srcdir, app.srcdir) self.assertNotEqual(app.srcdir, app.builddir.dirname()) self.assertTrue(app.builddir.isdir()) self.assertCountEqual(['conf.py', 'index.rst'], os.listdir(app.srcdir)) self.assertEqual((srcdir / 'conf.py').read_text(), (app.srcdir / 'conf.py').read_text()) self.assertEqual((srcdir / 'index.rst').read_text(), (app.srcdir / 'index.rst').read_text()) finally: app.cleanup() self.assertFalse(app.builddir.exists())
def test_figure(self, app, status, warnings): app.build() print(status.getvalue(), warnings.getvalue()) html = (app.outdir / 'index.html').read_text() image_files = (app.outdir / '_images').listdir() self.assertEqual(1, len(image_files)) image_filename = image_files[0] self.assertRegexpMatches(html, ( '<div class="figure".*?>\s*' '<img alt="(_images/%s)" src="\\1" />\s*' '<p class="caption">(<span class="caption-text">)?caption of figure(</span>)?</p>\s*' '</div>') % image_filename) if not self.skip_image_check: expected = path("tests/examples/animal.png").read_bytes() actual = (app.outdir / '_images' / image_filename).read_bytes() self.assertEqual(expected, actual)
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
def test_with_app(self): srcdir = path(__file__).dirname() / 'examples' builddir = [] @with_app(srcdir=srcdir, copy_srcdir_to_tmpdir=True) def execute(app, status, warning): (app.srcdir / 'unknown.rst').write_text('') builddir.append(app.builddir) # store to check outside of func if sphinx.__version__ < '1.0.0': app.build(True, None) else: app.build() self.assertIsInstance(status, StringIO) self.assertIsInstance(warning, StringIO) self.assertIn('index.html', os.listdir(app.outdir)) self.assertIn('Running Sphinx', status.getvalue()) self.assertIn("WARNING: document isn't included in any toctree", warning.getvalue()) execute() self.assertFalse(builddir[0].exists())
def convert(self, node, filename, to): path(to).write_text('') return True
def test_basename(self): p = path('/path/to/file') self.assertEqual("file", p.basename()) self.assertEqual("file", p.name)
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())
import os from unittest import TestCase from unittest.mock import ( ANY, patch, ) from munch import munchify from sphinx_testing import with_app from sphinx_testing.path import path from tut.sphinx.manager import TutManager import tut.sphinx.checkpoint test_root = path(__file__).parent.joinpath('root').abspath() @patch('tut.model.git', return_value='original_branch') class SphinxExtensionLifecycleTests(TestCase): # the order of these decorators is *important*: cleanup needs to # be replaced before the Sphinx Test Application is instantiated @patch('tut.sphinx.cleanup') @with_app(srcdir=test_root) def test_cleanup_called(self, cleanup_mock, git_mock, sphinx_app, status, warning): sphinx_app.build(force_all=True) self.assertEqual(cleanup_mock.call_count, 1)
def test_instantiate(self): p = path('/path/to/file') self.assertIsInstance(p, path) self.assertEqual('/path/to/file', p)
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())
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)
#!/usr/bin/env python # -*- coding: utf-8 -*- # ----------------------------------------------------------------------------- # Copyright (c) 2017 - Anil Lakhman - MIT License # ----------------------------------------------------------------------------- import logging import os import sys import docutils from sphinx_testing.path import path from sphinx_testing import with_app as with_sphinx_testing test_apps = path(__file__).parent.joinpath('testapps').abspath() rootdir = path(os.path.dirname(__file__) or '.').abspath() tempdir = path('./.cache').abspath() # log = logging.getLogger("bootstrap.logger") # log.debug("Debug : %s", app.builddir) logging.basicConfig(stream=sys.stderr) logging.getLogger("bootstrap.logger").setLevel(logging.DEBUG) # Utils ----------------------------------------------------------------------- def with_app(*args, **kwargs): # pragma: no cover """Decorator for passing a test Sphinx app to a function. Extends sphinx_testing's version by defaulting to a base test directory if none is specified. The test directory will be copied to a temporary directory before calling the function.
def test_suffix(self): p = path('/path/to/file.ext') self.assertEqual(".ext", p.suffix) self.assertEqual("/path/to/file", p.stem)