def test_more_walk():
    """
    Ensure s'more basics in walking a directory, with an extention
    filter.
    """
    with tmpdir() as t:
        with cd(t):
            mkdir("foo")
            mkdir("bar")
            mkdir("baz")

            valid_targets = ["./foo/bar.target", "./bar/foo.target", "./kruft.target"]

            invalid_targets = ["./foo/bar", "./baz/foo", "./kruft"]
            invalid_cmp = list(invalid_targets)

            for target in valid_targets + invalid_targets:
                touch(target)

            for entry in dir_walk("./", xtn="target"):
                if entry in valid_targets:
                    valid_targets.remove(entry)
                if entry in invalid_targets:
                    invalid_targets.remove(entry)

        assert valid_targets == []
        assert invalid_targets == invalid_cmp
def test_jinja_template_advanced():
    """
    Ensure multi-Jinja sanity, as well as nested files.
    """
    with tmpdir() as tmp:
        jt1 = JinjaTemplate("tests/resources/templates/jinja1")
        jt2 = JinjaTemplate("tests/resources/templates/jinja2")

        context = {"foo": "foo1", "kruft": "kruft1", "plop": "plop1", "no": "no", "go": "go"}

        jt1.set_context(context)
        jt2.set_context(context)
        jt1.render(tmp)
        jt2.render(tmp)

        files = {
            "literal": "{{literal}}",
            "really/nested/directory/with/a/file/foo": context["foo"],
            "kruft": context["kruft"],
            "foo": context["foo"],
            "nogo": "%s %s" % (context["no"], context["go"]),
            "plop": context["plop"],
        }

        with cd(tmp):
            for f in files:
                assert_content(f, files[f])
def test_plain_template_basic():
    """
    Ensure basic sanity with the PlainTemplate system.
    """
    with tmpdir() as tmp:
        pt1 = PlainTemplate("tests/resources/templates/plain1")
        pt1.render(tmp)
        with cd(tmp):
            for x in ["foo", "bar", "baz"]:
                assert_content(x, x)
def test_insane_compression_fails():
    pkgname = "pkgfoo"
    version = "2.0"
    debdir = "%s-%s" % (pkgname, version)

    with tmpdir() as tmp:
        with cd(tmp):
            mkdir(debdir)
            try:
                make_orig_tarball(".", pkgname, version, compression="asfasf")
                assert True is False
            except ValueError:
                assert True is True
def test_localdir_generation():
    pkgname = "pkgfoo"
    version = "2.0"
    debdir = "%s-%s" % (pkgname, version)
    compression = "bzip2"
    tarball = "%s_%s.orig.tar.bz2" % (pkgname, version)

    with tmpdir() as tmp:
        with cd(tmp):
            mkdir(debdir)
            assert not os.path.exists(tarball)
            make_orig_tarball(".", pkgname, version, compression=compression)
            assert os.path.exists(tarball)
def test_upstream_shim():
    """
    Ensure we create a debian tarball thinger.
    """
    pkgname = "testpkg-name"
    version = "1.0"
    with tmpdir() as tmp:
        with cd(tmp):
            mkdir("%s-%s" % (pkgname, version))
        tmp += "/%s-%s" % (pkgname, version)
        uss = UpstreamShim(pkgname, version)
        uss.set_compression("gzip")
        uss.render(tmp)
        assert "%s/%s_%s.orig.tar.gz" % (tmp, pkgname, version)
        assert "%s/%s-%s" % (tmp, pkgname, version)
def test_plain_template_advanced():
    """
    Ensure sanity when two PlainTemplates are rendered together,
    along with a directory.
    """
    with tmpdir() as tmp:
        pt1 = PlainTemplate("tests/resources/templates/plain1")
        pt2 = PlainTemplate("tests/resources/templates/plain2")
        pt1.render(tmp)
        pt2.render(tmp)

        values = {"foo": "foo", "bar": "not bar", "baz": "baz", "kruft/flip": "flip"}

        with cd(tmp):
            for entry in values:
                content = values[entry]
                assert_content(entry, content)
def test_jinja_template_basic():
    """
    Ensure basic Jinja sanity.
    """
    with tmpdir() as tmp:
        jt1 = JinjaTemplate("tests/resources/templates/jinja1")

        context = {"foo": "foo1", "kruft": "kruft1", "plop": "plop1"}

        jt1.set_context(context)
        jt1.render(tmp)

        with cd(tmp):
            for x in context:
                if not os.path.exists(x):
                    assert os.path.exists(x)

            for entry in context:
                assert_content(entry, context[entry])
def test_basic_walk():
    """
    Test to make sure we can walk a directory.
    """
    with tmpdir() as t:
        with cd(t):
            mkdir("foo")
            mkdir("bar")
            mkdir("baz")

            targets = ["./foo/bar.target", "./foo/bar", "./bar/foo.target", "./baz/foo"]

            for target in targets:
                touch(target)

            for entry in dir_walk("./"):
                assert entry in targets
                targets.remove(entry)

        assert targets == []
def make_and_check_tarball(testname, rundir, upname, upversion, compression,
                           visitor, visit_open=True):
    """Create, check and clean up a tarball (test utility)

    Utility for setting up a dir, compile a tarball from a resource path,
    opening the tarball and passing it to vistor.

    testname is used to create a unique name for the test.  The
    compression is also used for this purpose, so multiple tests can
    share the same "testname" as long as the compression differs.

    rundir, upname, upversion and compression are passed (as is) to
    make_orig_tarball.

    The tarball passed to visitor may not be seekable and should be
    checked inorder.
    """

    testdir = "%s-%s" % (testname, compression)
    xtn = compression
    if xtn == "gzip":
        xtn = "gz"
    elif xtn == "bzip2":
        xtn = "bz2"

    rundir = abspath(rundir)

    with tmpdir() as tmp:
        with cd(tmp):
            mkdir(testdir)

            make_orig_tarball(rundir, upname, upversion,
                              compression=compression, outputdir=testdir)
            tarname = "%s_%s.orig.tar.%s" % (upname, upversion, xtn)
            path = os.path.join(testdir, tarname)
            if visit_open:
                with open_compressed_tarball(path, compression) as tar:
                    visitor(tar)
            else:
                visitor(path)