1import os
2import zipfile
3import contextlib
4
5import pytest
6
7from setuptools.command.upload_docs import upload_docs
8from setuptools.dist import Distribution
9
10from .textwrap import DALS
11from . import contexts
12
13SETUP_PY = DALS(
14    """
15    from setuptools import setup
16
17    setup(name='foo')
18    """)
19
20
21@pytest.fixture
22def sample_project(tmpdir_cwd):
23    # setup.py
24    with open('setup.py', 'wt') as f:
25        f.write(SETUP_PY)
26
27    os.mkdir('build')
28
29    # A test document.
30    with open('build/index.html', 'w') as f:
31        f.write("Hello world.")
32
33    # An empty folder.
34    os.mkdir('build/empty')
35
36
37@pytest.mark.usefixtures('sample_project')
38@pytest.mark.usefixtures('user_override')
39class TestUploadDocsTest:
40    def test_create_zipfile(self):
41        """
42        Ensure zipfile creation handles common cases, including a folder
43        containing an empty folder.
44        """
45
46        dist = Distribution()
47
48        cmd = upload_docs(dist)
49        cmd.target_dir = cmd.upload_dir = 'build'
50        with contexts.tempdir() as tmp_dir:
51            tmp_file = os.path.join(tmp_dir, 'foo.zip')
52            zip_file = cmd.create_zipfile(tmp_file)
53
54            assert zipfile.is_zipfile(tmp_file)
55
56            with contextlib.closing(zipfile.ZipFile(tmp_file)) as zip_file:
57                assert zip_file.namelist() == ['index.html']
58
59    def test_build_multipart(self):
60        data = dict(
61            a="foo",
62            b="bar",
63            file=('file.txt', b'content'),
64        )
65        body, content_type = upload_docs._build_multipart(data)
66        assert 'form-data' in content_type
67        assert "b'" not in content_type
68        assert 'b"' not in content_type
69        assert isinstance(body, bytes)
70        assert b'foo' in body
71        assert b'content' in body
72