From 60b23e33e8bf57197d14d4cd4a1657bf751e5ed0 Mon Sep 17 00:00:00 2001 From: jonasc Date: Mon, 1 Jun 2020 19:37:43 +0200 Subject: [PATCH] Assert existence of config file's parent directory Fixes TheLocehiliosan/yadm#226 When a config file path is passed to yadm whose parent directory does not exist, git (and hence yadm) fails without writing the file. Yadm should, however, make sure that the directory exists s.t. git can just write to the file. --- test/test_config.py | 24 ++++++++++++++++++++++++ yadm | 2 ++ 2 files changed, 26 insertions(+) diff --git a/test/test_config.py b/test/test_config.py index 4e44b1c..17d0248 100644 --- a/test/test_config.py +++ b/test/test_config.py @@ -137,3 +137,27 @@ def test_config_local_write(runner, yadm_y, paths, supported_local_configs): assert run.success assert run.err == '' assert run.out.strip() == f'value_of_{config}' + + +def test_config_without_parent_directory(runner, yadm_y, paths): + """Write and read attribute to/from config file with non-existent parent directory + + Update configuration file + Display value + Exit with 0 + """ + + config_file = paths.root + '/folder/does/not/exist/config' + + run = runner(yadm_y('--yadm-config', config_file, 'config', TEST_KEY, TEST_VALUE)) + + assert run.success + assert run.err == '' + assert run.out == '' + + paths.config.write(TEST_FILE) + run = runner(yadm_y('--yadm-config', config_file, 'config', TEST_KEY)) + + assert run.success + assert run.err == '' + assert run.out.strip() == TEST_VALUE diff --git a/yadm b/yadm index f3a2343..28bfce2 100755 --- a/yadm +++ b/yadm @@ -849,6 +849,8 @@ EOF CHANGES_POSSIBLE=1 else + # make sure parent folder of config file exists + assert_parent "$YADM_CONFIG" # operate on the yadm configuration file "$GIT_PROGRAM" config --file="$(mixed_path "$YADM_CONFIG")" "$@"