Skip to content
This repository has been archived by the owner on Feb 8, 2023. It is now read-only.

Commit

Permalink
Add tests. The layout file is currently wrong and fails to build.
Browse files Browse the repository at this point in the history
  • Loading branch information
jamadden committed Jun 16, 2017
1 parent 23c830b commit e519646
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/z3c/recipe/sphinxdoc/__init__.py
Expand Up @@ -11,7 +11,7 @@
# FOR A PARTICULAR PURPOSE.
#
##############################################################################

from __future__ import print_function
import sys
import os
from os.path import join, dirname, isdir, normpath
Expand Down Expand Up @@ -56,10 +56,9 @@ def __init__(self, buildout, name, options):

def openfile(self, fn):
try:
f = open(fn)
return open(fn)
except IOError:
f = urlopen(fn)
return f
return urlopen(fn)

def install(self):
installed = []
Expand All @@ -78,6 +77,8 @@ def install(self):
srcDirs = eval(self.options.get('src-dirs','{}'))

projectsData = {}
# Preserve projectsData for testing
self._projectsData = projectsData
#for each egg listed as a buildout option, create a configuration space.
for doc in docs:
partDir = join(installDir, doc.project_name)
Expand Down Expand Up @@ -168,8 +169,11 @@ def install(self):
update = install


def main(projects):
def main(projects, argv=None, exit_on_error=False):
import sphinx
argv = argv or sys.argv
for project, args in projects.items():
print("building docs for", project, "---> sphinx-build", " ".join(args))
sphinx.main(argv=sys.argv+args)
code = sphinx.build_main(argv=argv+args)
if exit_on_error and code:
sys.exit(code)
Empty file.
127 changes: 127 additions & 0 deletions src/z3c/recipe/sphinxdoc/tests/test_sphinxdoc.py
@@ -0,0 +1,127 @@
##############################################################################
#
# Copyright (c) 2017 Zope Foundation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################

import shutil
import subprocess
import tempfile
import os
import unittest

from zc.buildout.testing import Buildout

from z3c.recipe import sphinxdoc

EMPTY_FILE = os.path.abspath(
os.path.join(
os.path.dirname(__file__),
'__init__.py',
)
)

class TestZopeOrgSetup(unittest.TestCase):

def setUp(self):
self.here = os.getcwd()
self.tmpdir = tempfile.mkdtemp('.sphinxdoctests')
os.chdir(self.tmpdir)
os.mkdir('bin')

def tearDown(self):
os.chdir(self.here)
shutil.rmtree(self.tmpdir)

def _makeOne(self, name='docs', options=None):
opts = {
'doc-eggs': 'z3c.recipe.sphinxdoc',
'eggs': 'z3c.recipe.sphinxdoc'
}
if options:
opts.update(options)
buildout = Buildout()

docs = sphinxdoc.ZopeOrgSetup(buildout, name, opts)
return docs, buildout

@property
def part_path(self):
return os.path.join('parts', 'docs', 'z3c.recipe.sphinxdoc')

@property
def templates_path(self):
return os.path.join(self.part_path, '.templates')

@property
def static_path(self):
return os.path.join(self.part_path, '.static')

def test_construct_sets_script(self):
docs, buildout = self._makeOne()
self.assertEqual(docs.options['script'],
os.path.join(buildout['buildout']['bin-directory'], 'docs'))

def test_openfile_falls_to_url(self):
docs, _ = self._makeOne()
self.assertRaises(IOError,
docs.openfile, 'file:///')

def test_basic_install(self):
docs, _ = self._makeOne()
docs.install()
conf_path = os.path.join(self.part_path, 'conf.py')
with open(conf_path, 'r') as f:
conf = f.read()

self.assertIn(os.path.abspath(self.templates_path), conf)
self.assertIn(os.path.abspath(self.static_path), conf)

script_path = os.path.join('bin', docs.name)
with open(script_path, 'r') as f:
script = f.read()
self.assertIn("sys.exit(z3c.recipe.sphinxdoc.main({'z3c.recipe.sphinxdoc':",
script)

# Go ahead and generate it, or at least try to, it shouldn't fail, even though there's no
# index.rst
subprocess.check_call('bin/docs')

# likewise, we can directly try to do it and it doesn't raise an exception
sphinxdoc.main(docs._projectsData, argv=['docs', '-W'])

# We can add another entry to the projectsData and we still don't raise anything
projects = dict(docs._projectsData)
projects['another'] = projects['z3c.recipe.sphinxdoc']
projects['another'][0] = '-W'

sphinxdoc.main(docs._projectsData, argv=['docs'], exit_on_error=True)

def test_override_css(self):
docs, _ = self._makeOne(options={'default.css': EMPTY_FILE})
docs.install()

css_path = os.path.join(self.static_path, 'default.css')
with open(css_path, 'r') as f:
css = f.read()
self.assertEqual(css, '')



def test_override_layout(self):
docs, _ = self._makeOne(options={'layout.html': EMPTY_FILE})
docs.install()


html_path = os.path.join(self.templates_path, 'layout.html')
with open(html_path, 'r') as f:
html = f.read()
self.assertEqual(html, '')

0 comments on commit e519646

Please sign in to comment.