Skip to content
Snippets Groups Projects
conftest.py 1.63 KiB
Newer Older
James Vasile's avatar
James Vasile committed
import os
import pytest 
import psutil
import requests
import subprocess
from xprocess import ProcessStarter

James Vasile's avatar
James Vasile committed
from website import pelicanconf

James Vasile's avatar
James Vasile committed
OUTPUT_PATH = os.path.join("../website", pelicanconf.OUTPUT_PATH)
def fetch(url, just_contents=True):
    res = requests.get(f'http://localhost:{PORT}/{url}')
    if just_contents:
        return res.content.decode('utf-8')
    return res

@pytest.fixture
def pelican_server(xprocess):
    class Starter(ProcessStarter):
        # startup pattern
        pattern = f"^Done: Processed "

        # command to start process
        args = ['serve']

    # ensure process is running and return its logfile
    logfile = xprocess.ensure("pelican_server", Starter)

    conn = PORT
    yield conn

    # clean up whole process tree afterwards
    xprocess.getinfo("pelican_server").terminate()


site_rebuilt = False
@pytest.fixture
def rebuild_site():
    """We only want to rebuid the site once and only if bin/serve isn't already
    automaritcally rebuilding it."""

    # Use a global to keep state between runs because I couldn't figure out how
    # to decorate a closure as a pytest.fixture.
    global site_rebuilt

    if not site_rebuilt:

        # Check if automatic rebuilder already running
        for pid in psutil.pids():
            p = psutil.Process(pid)
            if p.name() == "pelican" and p.cmdline()[-1] == "PELICAN_BINSERVE=true":
                site_rebuilt = True
                return True

        # Do the rebuild by running our build script
        out = subprocess.check_output("build").decode("utf-8")
        print(out)
        site_rebuilt = True
    return site_rebuilt