Skip to content

Commit bbb58e6

Browse files
committed
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.
1 parent 4214de8 commit bbb58e6

File tree

3 files changed

+51
-4
lines changed

3 files changed

+51
-4
lines changed

test/test_alt.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,49 @@ def test_alt_template_with_condition(runner, paths, tst_arch):
195195
assert len(created) == 0
196196

197197

198+
@pytest.mark.usefixtures("ds1_copy")
199+
@pytest.mark.parametrize("kind", ["default", None, "envtpl", "j2", "esh"])
200+
@pytest.mark.parametrize(
201+
"label",
202+
[
203+
"s",
204+
"seed",
205+
],
206+
)
207+
def test_alt_seed(runner, paths, kind, label):
208+
"""Test template seed"""
209+
yadm_dir, yadm_data = setup_standard_yadm_dir(paths)
210+
211+
suffix = f"##{label}.{kind}"
212+
if kind is None:
213+
suffix = f"##{label}"
214+
215+
utils.create_alt_files(paths, suffix, content="first")
216+
run = runner([paths.pgm, "-Y", yadm_dir, "--yadm-data", yadm_data, "alt"])
217+
assert run.success
218+
assert run.err == ""
219+
220+
utils.create_alt_files(paths, suffix, preserve=True, content="second")
221+
run2 = runner([paths.pgm, "-Y", yadm_dir, "--yadm-data", yadm_data, "alt"])
222+
assert run2.success
223+
assert run2.err == ""
224+
225+
created = utils.parse_alt_output(run.out, linked=False)
226+
created2 = utils.parse_alt_output(run2.out, linked=False)
227+
assert len(created2) == 0
228+
229+
for created_path in TEST_PATHS:
230+
source_file_content = created_path + suffix
231+
source_file = paths.work.join(source_file_content)
232+
created_file = paths.work.join(created_path)
233+
if created_path == utils.ALT_DIR:
234+
source_file = source_file.join(utils.CONTAINED)
235+
created_file = created_file.join(utils.CONTAINED)
236+
assert created_file.isfile()
237+
assert created_file.read().strip() == source_file_content + "\nfirst"
238+
assert str(source_file) in created
239+
240+
198241
@pytest.mark.usefixtures("ds1_copy")
199242
@pytest.mark.parametrize("autoalt", [None, "true", "false"])
200243
def test_auto_alt(runner, yadm_cmd, paths, autoalt):

yadm

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -222,17 +222,17 @@ function score_file() {
222222
# extension isn't a condition and doesn't affect the score
223223
continue
224224
;;
225-
t | template | yadm)
225+
s | seed | t | template | yadm)
226226
if ((negate)); then
227227
INVALID_ALT+=("$source")
228-
else
228+
elif [ "${label:0:1}" != "s" ] || [ ! -e "$target" ]; then
229229
template_processor=$(choose_template_processor "$value")
230230
if [ -n "$template_processor" ]; then
231231
delta=0
232232
elif [ -n "$loud" ]; then
233-
echo "No supported template processor for template $source"
233+
echo "No supported template processor for $source"
234234
else
235-
debug "No supported template processor for template $source"
235+
debug "No supported template processor for $source"
236236
fi
237237
fi
238238
;;

yadm.1

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,10 @@ These are the supported attributes, in the order of the weighted precedence:
492492
Valid when the value matches a supported template processor.
493493
See the TEMPLATES section for more details.
494494
.TP
495+
.BR seed ,\ s
496+
Same as template, but only valid if the target file doesn't exist. I.e. the
497+
file will be "seeded" from the template on the first run.
498+
.TP
495499
.BR user ,\ u
496500
Valid if the value matches the current user.
497501
Current user is calculated by running

0 commit comments

Comments
 (0)