|
| 1 | +from __future__ import absolute_import |
| 2 | +from __future__ import unicode_literals |
| 3 | + |
| 4 | +import mock |
| 5 | +import os.path |
| 6 | +import pytest |
| 7 | +from plumbum import local |
| 8 | + |
| 9 | +from pre_commit import make_archives |
| 10 | +from pre_commit.util import tarfile_open |
| 11 | +from testing.fixtures import git_dir |
| 12 | +from testing.util import get_head_sha |
| 13 | +from testing.util import skipif_slowtests_false |
| 14 | + |
| 15 | + |
| 16 | +def test_make_archive(tmpdir_factory): |
| 17 | + output_dir = tmpdir_factory.get() |
| 18 | + git_path = git_dir(tmpdir_factory) |
| 19 | + # Add a files to the git directory |
| 20 | + with local.cwd(git_path): |
| 21 | + local['touch']('foo') |
| 22 | + local['git']('add', '.') |
| 23 | + local['git']('commit', '-m', 'foo') |
| 24 | + # We'll use this sha |
| 25 | + head_sha = get_head_sha('.') |
| 26 | + # And check that this file doesn't exist |
| 27 | + local['touch']('bar') |
| 28 | + local['git']('add', '.') |
| 29 | + local['git']('commit', '-m', 'bar') |
| 30 | + |
| 31 | + # Do the thing |
| 32 | + archive_path = make_archives.make_archive( |
| 33 | + 'foo', git_path, head_sha, output_dir, |
| 34 | + ) |
| 35 | + |
| 36 | + assert archive_path == os.path.join(output_dir, 'foo.tar.gz') |
| 37 | + assert os.path.exists(archive_path) |
| 38 | + |
| 39 | + extract_dir = tmpdir_factory.get() |
| 40 | + |
| 41 | + # Extract the tar |
| 42 | + with tarfile_open(archive_path) as tf: |
| 43 | + tf.extractall(extract_dir) |
| 44 | + |
| 45 | + # Verify the contents of the tar |
| 46 | + assert os.path.exists(os.path.join(extract_dir, 'foo')) |
| 47 | + assert os.path.exists(os.path.join(extract_dir, 'foo', 'foo')) |
| 48 | + assert not os.path.exists(os.path.join(extract_dir, 'foo', '.git')) |
| 49 | + assert not os.path.exists(os.path.join(extract_dir, 'foo', 'bar')) |
| 50 | + |
| 51 | + |
| 52 | +@skipif_slowtests_false |
| 53 | +@pytest.mark.integration |
| 54 | +def test_main(tmpdir_factory): |
| 55 | + path = tmpdir_factory.get() |
| 56 | + |
| 57 | + # Don't actually want to make these in the current repo |
| 58 | + with mock.patch.object(make_archives, 'RESOURCES_DIR', path): |
| 59 | + make_archives.main() |
| 60 | + |
| 61 | + for archive, _, _ in make_archives.REPOS: |
| 62 | + assert os.path.exists(os.path.join(path, archive + '.tar.gz')) |
0 commit comments