Skip to content

Commit

Permalink
Merge pull request #91 from Pinata-Consulting/latexpdf-tests
Browse files Browse the repository at this point in the history
tests: run html and latexpdf builders for all tests
  • Loading branch information
mithro authored Sep 22, 2023
2 parents 71177fa + 5f72d39 commit 2ef1795
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 27 deletions.
1 change: 1 addition & 0 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ dependencies:
- python=3.7
- sphinx
- yosys
- r-rsvg
- pip: # Packages installed from PyPI
- -r requirements.txt
- -r docs/requirements.txt
Expand Down
30 changes: 24 additions & 6 deletions sphinxcontrib_hdl_diagrams/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,17 @@
logger = logging.getLogger(__name__)


import subprocess

def run_quietly(cmd):
print("Running: {}".format(subprocess.list2cmdline(cmd)))
try:
result = subprocess.run(cmd, check=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
except subprocess.CalledProcessError as e:
print(f"Command {subprocess.list2cmdline(cmd)} failed with error {e.returncode}:")
print(e.stdout, file=sys.stderr)
raise

class HDLDiagramError(SphinxError):
category = 'HDLDiagram error'

Expand Down Expand Up @@ -297,9 +308,13 @@ def diagram_yosys(ipath, opath, module='top', flatten=False,
# somehow yowasp_yosys fails to execute `dot` to convert the dot file to svg,
# which works on native yosys, perhaps a limitation with wasm
svgdata = subprocess.check_output(["dot", "-Tsvg", "{}.dot".format(oprefix)])
with open("{}.svg".format(oprefix), "wb") as img:
tmpfile = "{}.svg".format(oprefix)
with open(tmpfile, "wb") as img:
img.write(svgdata)

if tmpfile != opath:
convert(tmpfile, opath)

assert path.exists(opath), 'Output file {} was not created!'.format(opath)
print('Output file created: {}'.format(opath))

Expand Down Expand Up @@ -375,6 +390,11 @@ def nmigen_to_rtlil(fname, oname):
cmd = "{python} {script} > {output}".format(python=sys.executable, script=fname, output=oname)
subprocess.run(cmd, shell=True, check=True)

def convert(source, destination):
# convertcmd = ['inkscape', source, f"--export-filename={destination}"]
# convertcmd = ['convert', source, destination]
convertcmd = ['rsvg-convert', '-f', 'pdf', source, '-o', destination]
run_quietly(convertcmd)

def render_diagram(self, code, options, format):
# type: (nodes.NodeVisitor, unicode, Dict, unicode, unicode) -> Tuple[unicode, unicode]
Expand Down Expand Up @@ -433,18 +453,16 @@ def render_diagram(self, code, options, format):
yosys_script=yosys_script,
yosys=yosys)
elif diagram_type == 'netlistsvg':
tmpfile = outfn + '.pdf' if format == 'pdf' else outfn
tmpfile = outfn if format == 'svg' else outfn + '.svg'
diagram_netlistsvg(
source_path,
tmpfile,
module=options['module'],
flatten=options['flatten'],
skin=skin,
yosys=yosys)
if format == 'pdf':
inkscapecmd = ['inkscape', tmpfile, '--export-type=pdf', '-o', outfn]
print("Running: {}".format(subprocess.list2cmdline(inkscapecmd)))
subprocess.check_output(inkscapecmd)
if format != 'svg':
convert(tmpfile, outfn)
else:
raise Exception('Invalid diagram type "%s"' % diagram_type)
# raise self.severe(\n' %
Expand Down
34 changes: 13 additions & 21 deletions tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ def prepare_test(self, test_name, test_build_dir, test_files, **test_jinja_dict)
shutil.copyfile(src, dst)


def run_builds(sphinx_dirs):
for builder in ("html", "latex"):
with docutils_namespace():
app = Sphinx(buildername=builder, warningiserror=True, **sphinx_dirs)
app.build(force_all=True)

## Test cases

class TestSkins(TestBase):
Expand All @@ -101,9 +107,7 @@ def test_netlistsvg_diagram(self):

# Run the Sphinx
sphinx_dirs = get_sphinx_dirs(TEST_BUILD_DIR)
with docutils_namespace():
app = Sphinx(buildername="html", warningiserror=True, **sphinx_dirs)
app.build(force_all=True)
run_builds(sphinx_dirs)


class TestYosysScript(TestBase):
Expand All @@ -130,9 +134,7 @@ def test_yosys_script(self):

# Run the Sphinx
sphinx_dirs = get_sphinx_dirs(TEST_BUILD_DIR)
with docutils_namespace():
app = Sphinx(buildername="html", warningiserror=True, **sphinx_dirs)
app.build(force_all=True)
run_builds(sphinx_dirs)


class TestYosysType(TestBase):
Expand Down Expand Up @@ -204,9 +206,7 @@ def test_yosys_path(self):

# Run the Sphinx
sphinx_dirs = get_sphinx_dirs(TEST_BUILD_DIR)
with docutils_namespace():
app = Sphinx(buildername="html", warningiserror=True, **sphinx_dirs)
app.build(force_all=True)
run_builds(sphinx_dirs)


class TestNMigen(TestBase):
Expand All @@ -231,9 +231,7 @@ def test_yosys_script(self):

# Run the Sphinx
sphinx_dirs = get_sphinx_dirs(TEST_BUILD_DIR)
with docutils_namespace():
app = Sphinx(buildername="html", warningiserror=True, **sphinx_dirs)
app.build(force_all=True)
run_builds(sphinx_dirs)


class TestRTLIL(TestBase):
Expand All @@ -258,9 +256,7 @@ def test_yosys_script(self):

# Run the Sphinx
sphinx_dirs = get_sphinx_dirs(TEST_BUILD_DIR)
with docutils_namespace():
app = Sphinx(buildername="html", warningiserror=True, **sphinx_dirs)
app.build(force_all=True)
run_builds(sphinx_dirs)


class TestCompat(TestBase):
Expand Down Expand Up @@ -288,9 +284,7 @@ def test_yosys_script(self):

# Run the Sphinx
sphinx_dirs = get_sphinx_dirs(TEST_BUILD_DIR)
with docutils_namespace():
app = Sphinx(buildername="html", warningiserror=True, **sphinx_dirs)
app.build(force_all=True)
run_builds(sphinx_dirs)


class TestFlatten(TestBase):
Expand All @@ -316,9 +310,7 @@ def test_yosys_script(self):

# Run the Sphinx
sphinx_dirs = get_sphinx_dirs(TEST_BUILD_DIR)
with docutils_namespace():
app = Sphinx(buildername="html", warningiserror=True, **sphinx_dirs)
app.build(force_all=True)
run_builds(sphinx_dirs)


if __name__ == '__main__':
Expand Down

0 comments on commit 2ef1795

Please sign in to comment.