# Path: shipwright/_lib/tar.py
# RE_DOCKER_TAG = re.compile(
#     r'^(?P<from>\s*from\s+)'
#     r'(?P<registry>[\w.-]+(?P<port>:\d+)?(?P<path>([\w.-]+/)+|/))?'
#     r'(?P<name>[\w.-]+)'
#     r'(?P<whitespace>(\s*))$',
#     flags=re.MULTILINE | re.IGNORECASE,
# )
# def bundle_docker_dir(tag, docker_path):
# def tag_parent(tag, docker_content):
# def mkcontext(tag, docker_path):


import tarfile
from shipwright._lib import tar
from __future__ import absolute_import




def test_mkcontext(tmpdir):
    tmp = tmpdir.mkdir('image')
    docker_path = tmp.join('Dockerfile')
    docker_path.write('FROM example.com/r/image')
    tmp.join('bogus').write('hi mom')

    with tar.mkcontext('xyz', str(docker_path)) as f:
        with tarfile.open(fileobj=f, mode='a') as t:
            names = t.getnames()

    assert names == ['', 'bogus', 'Dockerfile']


def test_mkcontext_dockerignore(tmpdir):
    tmp = tmpdir.mkdir('image')
    docker_path = tmp.join('Dockerfile')
    docker_path.write('FROM example.com/r/image')
    tmp.join('.dockerignore').write('bogus2')
    tmp.join('bogus').write('hi mom')
    tmp.join('bogus2').write('This is ignored')


# Path: shipwright/_lib/tar.py
# RE_DOCKER_TAG = re.compile(
#     r'^(?P<from>\s*from\s+)'
#     r'(?P<registry>[\w.-]+(?P<port>:\d+)?(?P<path>([\w.-]+/)+|/))?'
#     r'(?P<name>[\w.-]+)'
#     r'(?P<whitespace>(\s*))$',
#     flags=re.MULTILINE | re.IGNORECASE,
# )
# def bundle_docker_dir(tag, docker_path):
# def tag_parent(tag, docker_content):
# def mkcontext(tag, docker_path):


# Path: tests/test_tar.py
import tarfile
from shipwright._lib import tar

from __future__ import absolute_import




def test_mkcontext(tmpdir):
    tmp = tmpdir.mkdir('image')
    docker_path = tmp.join('Dockerfile')
    docker_path.write('FROM example.com/r/image')
    tmp.join('bogus').write('hi mom')

    with tar.mkcontext('xyz', str(docker_path)) as f:
        with tarfile.open(fileobj=f, mode='a') as t:
            names = t.getnames()

    assert names == ['', 'bogus', 'Dockerfile']


def test_mkcontext_dockerignore(tmpdir):
    tmp = tmpdir.mkdir('image')
    docker_path = tmp.join('Dockerfile')
    docker_path.write('FROM example.com/r/image')
    tmp.join('.dockerignore').write('bogus2')
    tmp.join('bogus').write('hi mom')
    tmp.join('bogus2').write('This is ignored')


    with tar.mkcontext('xyz', str(docker_path)) as f: