From bbb58e6625de5d934fd49b4b81c1981e137e3057 Mon Sep 17 00:00:00 2001 From: Erik Flodin Date: Sun, 23 Mar 2025 21:32:50 +0100 Subject: [PATCH] Add support for "seed" template (#435) The file will only be created from the template the first run. On subsequent runs it will not be updated, even if the template has changed. --- test/test_alt.py | 43 +++++++++++++++++++++++++++++++++++++++++++ yadm | 8 ++++---- yadm.1 | 4 ++++ 3 files changed, 51 insertions(+), 4 deletions(-) diff --git a/test/test_alt.py b/test/test_alt.py index 2b2058f..ed66bf4 100644 --- a/test/test_alt.py +++ b/test/test_alt.py @@ -195,6 +195,49 @@ def test_alt_template_with_condition(runner, paths, tst_arch): assert len(created) == 0 +@pytest.mark.usefixtures("ds1_copy") +@pytest.mark.parametrize("kind", ["default", None, "envtpl", "j2", "esh"]) +@pytest.mark.parametrize( + "label", + [ + "s", + "seed", + ], +) +def test_alt_seed(runner, paths, kind, label): + """Test template seed""" + yadm_dir, yadm_data = setup_standard_yadm_dir(paths) + + suffix = f"##{label}.{kind}" + if kind is None: + suffix = f"##{label}" + + utils.create_alt_files(paths, suffix, content="first") + run = runner([paths.pgm, "-Y", yadm_dir, "--yadm-data", yadm_data, "alt"]) + assert run.success + assert run.err == "" + + utils.create_alt_files(paths, suffix, preserve=True, content="second") + run2 = runner([paths.pgm, "-Y", yadm_dir, "--yadm-data", yadm_data, "alt"]) + assert run2.success + assert run2.err == "" + + created = utils.parse_alt_output(run.out, linked=False) + created2 = utils.parse_alt_output(run2.out, linked=False) + assert len(created2) == 0 + + for created_path in TEST_PATHS: + source_file_content = created_path + suffix + source_file = paths.work.join(source_file_content) + created_file = paths.work.join(created_path) + if created_path == utils.ALT_DIR: + source_file = source_file.join(utils.CONTAINED) + created_file = created_file.join(utils.CONTAINED) + assert created_file.isfile() + assert created_file.read().strip() == source_file_content + "\nfirst" + assert str(source_file) in created + + @pytest.mark.usefixtures("ds1_copy") @pytest.mark.parametrize("autoalt", [None, "true", "false"]) def test_auto_alt(runner, yadm_cmd, paths, autoalt): diff --git a/yadm b/yadm index 261b987..9757345 100755 --- a/yadm +++ b/yadm @@ -222,17 +222,17 @@ function score_file() { # extension isn't a condition and doesn't affect the score continue ;; - t | template | yadm) + s | seed | t | template | yadm) if ((negate)); then INVALID_ALT+=("$source") - else + elif [ "${label:0:1}" != "s" ] || [ ! -e "$target" ]; then template_processor=$(choose_template_processor "$value") if [ -n "$template_processor" ]; then delta=0 elif [ -n "$loud" ]; then - echo "No supported template processor for template $source" + echo "No supported template processor for $source" else - debug "No supported template processor for template $source" + debug "No supported template processor for $source" fi fi ;; diff --git a/yadm.1 b/yadm.1 index fa4df39..b62dbbd 100644 --- a/yadm.1 +++ b/yadm.1 @@ -492,6 +492,10 @@ These are the supported attributes, in the order of the weighted precedence: Valid when the value matches a supported template processor. See the TEMPLATES section for more details. .TP +.BR seed ,\ s +Same as template, but only valid if the target file doesn't exist. I.e. the +file will be "seeded" from the template on the first run. +.TP .BR user ,\ u Valid if the value matches the current user. Current user is calculated by running