Example #1
0
def test_pth_as_key_produce_unique_path():
    pth1 = pth_as_key("./toto/titi/")
    pth2 = pth_as_key("toto/titi")
    pth3 = pth_as_key("./toto/../toto/titi")

    assert pth1 == pth2
    assert pth1 == pth3
def test_pth_as_key_produce_unique_path():
    pth1 = pth_as_key("./toto/titi/")
    pth2 = pth_as_key("toto/titi")
    pth3 = pth_as_key("./toto/../toto/titi")

    assert pth1 == pth2
    assert pth1 == pth3
def test_fin_templates_handles_src_directory_with_namespace(tmp_dir):
    pth = tmp_dir / "src/src/{{ base.pkgname }}/test.txt"
    ensure_path(pth)
    pth.write_text("lorem ipsum")

    pkg_cfg = dict(DEFAULT_CFG)
    pkg_cfg['base'] = dict(pkgname='toto',
                           namespace='myns',
                           namespace_method='pkg_utils')
    cfg = Config(pkg_cfg)
    rg_tree = {}
    find_templates(tmp_dir / 'src', tmp_dir / 'tgt', cfg, rg_tree)

    tgt_dir = tmp_dir / "tgt"
    assert pth_as_key(tgt_dir / "src/myns/__init__.py") in rg_tree
    assert pth_as_key(tgt_dir / "src/myns/toto/test.txt") in rg_tree
def test_find_templates_ignores_specific_names(tmp_dir):
    ensure_created(tmp_dir / "src/{{ custom_name }}")
    fnames = ('{{ custom_name }}.txt', 'titi.txt',
              '{{ custom_name }}/{{ custom_name }}.txt')
    for fname in fnames:
        pth = tmp_dir / "src" / fname
        pth.write_text("lorem ipsum")

    cfg = Config(DEFAULT_CFG)
    cfg._env.globals['custom_name'] = '_'
    rg_tree = {}
    find_templates(tmp_dir / 'src', tmp_dir / 'tgt', cfg, rg_tree)

    assert pth_as_key(tmp_dir / 'tgt/titi.txt') in rg_tree
    fnames = ('_.txt', '_/_.txt')
    for fname in fnames:
        assert pth_as_key(tmp_dir / 'tgt' / fname) not in rg_tree
Example #5
0
def test_modified_file_detect_modifications_in_preserved_sections(tmp_pth):
    txt = "lorem ipsum\n" * 10
    hv = compute_hash(txt)

    with open(tmp_pth, 'w') as f:
        f.write("{# pkglts, toto\n")
        f.write(txt)
        f.write("#}\n")

    assert not modified_file_hash(tmp_pth,
                                  {pth_as_key(tmp_pth): dict(toto=hv)})

    with open(tmp_pth, 'w') as f:
        f.write("{# pkglts, toto\n")
        f.write(txt * 2)
        f.write("#}\n")

    assert modified_file_hash(tmp_pth, {pth_as_key(tmp_pth): dict(toto=hv)})
Example #6
0
def test_modified_file_if_not_same_preserved_sections(tmp_pth):
    txt = "lorem ipsum\n" * 10

    with open(tmp_pth, 'w') as f:
        f.write("{# pkglts, toto\n")
        f.write(txt)
        f.write("#}\n")

    assert modified_file_hash(tmp_pth,
                              {pth_as_key(tmp_pth): dict(titi="azerty")})
def test_find_templates_handles_src_directory_no_namespace(tmp_dir):
    pth = tmp_dir / "src/src/{{ base.pkgname }}/test.txt"
    ensure_path(pth)
    pth.write_text("lorem ipsum")

    pkg_cfg = dict(DEFAULT_CFG)
    pkg_cfg['base'] = dict(pkgname='toto', namespace=None)
    cfg = Config(pkg_cfg)
    rg_tree = {}
    find_templates(tmp_dir / 'src', tmp_dir / 'tgt', cfg, rg_tree)

    tgt = tmp_dir / "tgt/src/toto/test.txt"
    assert pth_as_key(tgt) in rg_tree
def test_find_templates_walk_all_files_in_src_dir(tmp_dir):
    ensure_created(tmp_dir / "src/sub")
    fnames = ('toto.txt', 'titi.txt', 'sub/toto.txt')
    for fname in fnames:
        pth = tmp_dir / "src" / fname
        pth.write_text("lorem ipsum")

    cfg = Config(DEFAULT_CFG)
    rg_tree = {}
    find_templates(tmp_dir / 'src', tmp_dir / 'tgt', cfg, rg_tree)

    for fname in fnames:
        assert pth_as_key(tmp_dir / 'tgt' / fname) in rg_tree
def test_find_templates_renders_path_names(tmp_dir):
    ensure_created(tmp_dir / "src/{{ custom_name }}")
    fnames = ('{{ custom_name }}.txt', 'titi.txt',
              '{{ custom_name }}/{{ custom_name }}.txt')
    for fname in fnames:
        pth = tmp_dir / "src" / fname
        pth.write_text("lorem ipsum")

    cfg = Config(DEFAULT_CFG)
    cfg._env.globals['custom_name'] = 'toto'
    rg_tree = {}
    find_templates(tmp_dir / 'src', tmp_dir / 'tgt', cfg, rg_tree)

    fnames = ('toto.txt', 'titi.txt', 'toto/toto.txt')
    for fname in fnames:
        assert pth_as_key(tmp_dir / 'tgt' / fname) in rg_tree
def test_render_template_does_not_overwrite_protected_files(tmp_dir):
    src_pth = tmp_dir / "src/test.txt"
    tgt_pth = tmp_dir / "tgt/test.txt"
    src_pth.write_text("{# pkglts, b0\n{{ random() }}\n#}\n")

    cfg = Config(DEFAULT_CFG)
    cfg._env.globals['random'] = random
    render_template([src_pth], tgt_pth, cfg, {})

    cnt0 = tgt_pth.read_text()

    overwrite = {pth_as_key(tgt_pth): False}
    render_template([src_pth], tgt_pth, cfg, overwrite)

    cnt1 = tgt_pth.read_text()

    assert cnt0 == cnt1
def test_regenerate_do_not_overwrite_protected_files():
    pth = pj(tmp_dir, "src", "test.txt")
    with open(pth, 'w') as f:
        f.write("{# pkglts, b0\n{{ random() }}\n#}")

    env = create_env(default_cfg)
    env.globals['random'] = random
    regenerate_dir(pj(tmp_dir, 'src'), pj(tmp_dir, 'tgt'), env, {})

    pth = pj(tmp_dir, "tgt", "test.txt")
    with open(pth, 'r') as f:
        cnt0 = f.read()

    overwrite = {pth_as_key(pth): False}
    regenerate_dir(pj(tmp_dir, 'src'), pj(tmp_dir, 'tgt'), env, overwrite)

    pth = pj(tmp_dir, "tgt", "test.txt")
    with open(pth, 'r') as f:
        cnt1 = f.read()

    assert cnt0 == cnt1
Example #12
0
def test_regenerate_do_not_overwrite_protected_files(tmp_dir):
    pth = pj(tmp_dir, "src", "test.txt")
    with open(pth, 'w') as f:
        f.write("{# pkglts, b0\n{{ random() }}\n#}\n")

    cfg = Config(default_cfg)
    cfg._env.globals['random'] = random
    regenerate_dir(pj(tmp_dir, 'src'), pj(tmp_dir, 'tgt'), cfg, {})

    pth = pj(tmp_dir, "tgt", "test.txt")
    with open(pth, 'r') as f:
        cnt0 = f.read()

    overwrite = {pth_as_key(pth): False}
    regenerate_dir(pj(tmp_dir, 'src'), pj(tmp_dir, 'tgt'), cfg, overwrite)

    pth = pj(tmp_dir, "tgt", "test.txt")
    with open(pth, 'r') as f:
        cnt1 = f.read()

    assert cnt0 == cnt1
Example #13
0
def test_modified_file_detect_modifications_only_in_preserved_sections(
        tmp_pth):
    with open(tmp_pth, 'w') as f:
        f.write("lorem ipsum\n" * 10)

    assert not modified_file_hash(tmp_pth, {pth_as_key(tmp_pth): []})