File: conftest.py

package info (click to toggle)
jaraco.context 6.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 164 kB
  • sloc: python: 196; sh: 9; makefile: 4
file content (37 lines) | stat: -rw-r--r-- 1,015 bytes parent folder | download
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
import http.server
import io
import functools
import tarfile
import threading

import portend
import pytest


class QuietHTTPRequestHandler(http.server.SimpleHTTPRequestHandler):
    def log_message(self, format, *args):
        pass


@pytest.fixture
def tarfile_served(tmp_path_factory):
    """
    Start an HTTP server serving a tarfile.
    """
    tmp_path = tmp_path_factory.mktemp('www')
    fn = tmp_path / 'served.tgz'
    tf = tarfile.open(fn, mode='w:gz')
    info = tarfile.TarInfo('served/contents.txt')
    tf.addfile(info, io.BytesIO('hello, contents'.encode()))
    tf.close()
    httpd, url = start_server(tmp_path)
    with httpd:
        yield url + '/served.tgz'


def start_server(path):
    _host, port = addr = ('', portend.find_available_local_port())
    Handler = functools.partial(QuietHTTPRequestHandler, directory=path)
    httpd = http.server.HTTPServer(addr, Handler)
    threading.Thread(target=httpd.serve_forever, daemon=True).start()
    return httpd, f'http://localhost:{port}'