From 9c3dd3e60e125a7f891e0632045c999d95e45b6d Mon Sep 17 00:00:00 2001 From: Caian Ertl Date: Fri, 7 Oct 2022 22:52:29 -0300 Subject: [PATCH 1/9] chore: remove warnings about the python interpreter when testing --- tests/ansible.cfg | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ansible.cfg b/tests/ansible.cfg index 3318037..a76b8bb 100644 --- a/tests/ansible.cfg +++ b/tests/ansible.cfg @@ -1,3 +1,3 @@ [defaults] -interpreter_python=auto +interpreter_python = auto_silent From 93dea09918e94ef5dc1031c52f9584f964d6111a Mon Sep 17 00:00:00 2001 From: Caian Ertl Date: Fri, 7 Oct 2022 22:53:28 -0300 Subject: [PATCH 2/9] test: run "state: latest" a couple of times to check the "changed" value --- tests/test.sh | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/test.sh b/tests/test.sh index 48ab35d..70b3272 100755 --- a/tests/test.sh +++ b/tests/test.sh @@ -34,6 +34,9 @@ test -h "$HOME/.config/foo" && exit 1 figlet "state: latest" run_playbook "present" run_playbook "latest" +run_playbook "latest" +run_playbook "latest" +run_playbook "latest" run_playbook "absent" # create a file that should conflict with the package From a9c5be4d051b61e25a8fc8f79b9a38b4395897ac Mon Sep 17 00:00:00 2001 From: Caian Ertl Date: Fri, 7 Oct 2022 22:56:59 -0300 Subject: [PATCH 3/9] build: bump dependencies --- poetry.lock | 62 ++++++++++++++++++++++++-------------------------- pyproject.toml | 2 +- 2 files changed, 31 insertions(+), 33 deletions(-) diff --git a/poetry.lock b/poetry.lock index 9f9a703..134d76d 100644 --- a/poetry.lock +++ b/poetry.lock @@ -62,11 +62,11 @@ yaml = ["PyYAML"] [[package]] name = "black" -version = "22.8.0" +version = "22.10.0" description = "The uncompromising code formatter." category = "dev" optional = false -python-versions = ">=3.6.2" +python-versions = ">=3.7" [package.dependencies] click = ">=8.0.0" @@ -157,7 +157,7 @@ smmap = ">=3.0.1,<6" [[package]] name = "GitPython" -version = "3.1.27" +version = "3.1.28" description = "GitPython is a python library used to interact with Git repositories" category = "dev" optional = false @@ -451,7 +451,7 @@ python-versions = ">=3.6" [[package]] name = "typing-extensions" -version = "4.3.0" +version = "4.4.0" description = "Backported and Experimental Type Hints for Python 3.7+" category = "dev" optional = false @@ -491,7 +491,7 @@ testing = ["func-timeout", "jaraco.itertools", "pytest (>=6)", "pytest-black (>= [metadata] lock-version = "1.1" python-versions = "^3.7.2" -content-hash = "35e0090e9032598d5083b5be464eddfce2edc6dae2055802384abf34b24ada66" +content-hash = "b43a56320877150ed08355783c3c26ef4155921bc2c7146841f66869ab4a2093" [metadata.files] ansible = [ @@ -509,29 +509,27 @@ bandit = [ {file = "bandit-1.7.4.tar.gz", hash = "sha256:2d63a8c573417bae338962d4b9b06fbc6080f74ecd955a092849e1e65c717bd2"}, ] black = [ - {file = "black-22.8.0-cp310-cp310-macosx_10_9_universal2.whl", hash = "sha256:ce957f1d6b78a8a231b18e0dd2d94a33d2ba738cd88a7fe64f53f659eea49fdd"}, - {file = "black-22.8.0-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:5107ea36b2b61917956d018bd25129baf9ad1125e39324a9b18248d362156a27"}, - {file = "black-22.8.0-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:e8166b7bfe5dcb56d325385bd1d1e0f635f24aae14b3ae437102dedc0c186747"}, - {file = "black-22.8.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:dd82842bb272297503cbec1a2600b6bfb338dae017186f8f215c8958f8acf869"}, - {file = "black-22.8.0-cp310-cp310-win_amd64.whl", hash = "sha256:d839150f61d09e7217f52917259831fe2b689f5c8e5e32611736351b89bb2a90"}, - {file = "black-22.8.0-cp36-cp36m-macosx_10_9_x86_64.whl", hash = "sha256:a05da0430bd5ced89176db098567973be52ce175a55677436a271102d7eaa3fe"}, - {file = "black-22.8.0-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:4a098a69a02596e1f2a58a2a1c8d5a05d5a74461af552b371e82f9fa4ada8342"}, - {file = "black-22.8.0-cp36-cp36m-win_amd64.whl", hash = "sha256:5594efbdc35426e35a7defa1ea1a1cb97c7dbd34c0e49af7fb593a36bd45edab"}, - {file = "black-22.8.0-cp37-cp37m-macosx_10_9_x86_64.whl", hash = "sha256:a983526af1bea1e4cf6768e649990f28ee4f4137266921c2c3cee8116ae42ec3"}, - {file = "black-22.8.0-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:3b2c25f8dea5e8444bdc6788a2f543e1fb01494e144480bc17f806178378005e"}, - {file = "black-22.8.0-cp37-cp37m-win_amd64.whl", hash = "sha256:78dd85caaab7c3153054756b9fe8c611efa63d9e7aecfa33e533060cb14b6d16"}, - {file = "black-22.8.0-cp38-cp38-macosx_10_9_universal2.whl", hash = "sha256:cea1b2542d4e2c02c332e83150e41e3ca80dc0fb8de20df3c5e98e242156222c"}, - {file = "black-22.8.0-cp38-cp38-macosx_10_9_x86_64.whl", hash = "sha256:5b879eb439094751185d1cfdca43023bc6786bd3c60372462b6f051efa6281a5"}, - {file = "black-22.8.0-cp38-cp38-macosx_11_0_arm64.whl", hash = "sha256:0a12e4e1353819af41df998b02c6742643cfef58282915f781d0e4dd7a200411"}, - {file = "black-22.8.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:c3a73f66b6d5ba7288cd5d6dad9b4c9b43f4e8a4b789a94bf5abfb878c663eb3"}, - {file = "black-22.8.0-cp38-cp38-win_amd64.whl", hash = "sha256:e981e20ec152dfb3e77418fb616077937378b322d7b26aa1ff87717fb18b4875"}, - {file = "black-22.8.0-cp39-cp39-macosx_10_9_universal2.whl", hash = "sha256:8ce13ffed7e66dda0da3e0b2eb1bdfc83f5812f66e09aca2b0978593ed636b6c"}, - {file = "black-22.8.0-cp39-cp39-macosx_10_9_x86_64.whl", hash = "sha256:32a4b17f644fc288c6ee2bafdf5e3b045f4eff84693ac069d87b1a347d861497"}, - {file = "black-22.8.0-cp39-cp39-macosx_11_0_arm64.whl", hash = "sha256:0ad827325a3a634bae88ae7747db1a395d5ee02cf05d9aa7a9bd77dfb10e940c"}, - {file = "black-22.8.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:53198e28a1fb865e9fe97f88220da2e44df6da82b18833b588b1883b16bb5d41"}, - {file = "black-22.8.0-cp39-cp39-win_amd64.whl", hash = "sha256:bc4d4123830a2d190e9cc42a2e43570f82ace35c3aeb26a512a2102bce5af7ec"}, - {file = "black-22.8.0-py3-none-any.whl", hash = "sha256:d2c21d439b2baf7aa80d6dd4e3659259be64c6f49dfd0f32091063db0e006db4"}, - {file = "black-22.8.0.tar.gz", hash = "sha256:792f7eb540ba9a17e8656538701d3eb1afcb134e3b45b71f20b25c77a8db7e6e"}, + {file = "black-22.10.0-1fixedarch-cp310-cp310-macosx_11_0_x86_64.whl", hash = "sha256:5cc42ca67989e9c3cf859e84c2bf014f6633db63d1cbdf8fdb666dcd9e77e3fa"}, + {file = "black-22.10.0-1fixedarch-cp311-cp311-macosx_11_0_x86_64.whl", hash = "sha256:5d8f74030e67087b219b032aa33a919fae8806d49c867846bfacde57f43972ef"}, + {file = "black-22.10.0-1fixedarch-cp37-cp37m-macosx_10_16_x86_64.whl", hash = "sha256:197df8509263b0b8614e1df1756b1dd41be6738eed2ba9e9769f3880c2b9d7b6"}, + {file = "black-22.10.0-1fixedarch-cp38-cp38-macosx_10_16_x86_64.whl", hash = "sha256:2644b5d63633702bc2c5f3754b1b475378fbbfb481f62319388235d0cd104c2d"}, + {file = "black-22.10.0-1fixedarch-cp39-cp39-macosx_11_0_x86_64.whl", hash = "sha256:e41a86c6c650bcecc6633ee3180d80a025db041a8e2398dcc059b3afa8382cd4"}, + {file = "black-22.10.0-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:2039230db3c6c639bd84efe3292ec7b06e9214a2992cd9beb293d639c6402edb"}, + {file = "black-22.10.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:14ff67aec0a47c424bc99b71005202045dc09270da44a27848d534600ac64fc7"}, + {file = "black-22.10.0-cp310-cp310-win_amd64.whl", hash = "sha256:819dc789f4498ecc91438a7de64427c73b45035e2e3680c92e18795a839ebb66"}, + {file = "black-22.10.0-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:5b9b29da4f564ba8787c119f37d174f2b69cdfdf9015b7d8c5c16121ddc054ae"}, + {file = "black-22.10.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:b8b49776299fece66bffaafe357d929ca9451450f5466e997a7285ab0fe28e3b"}, + {file = "black-22.10.0-cp311-cp311-win_amd64.whl", hash = "sha256:21199526696b8f09c3997e2b4db8d0b108d801a348414264d2eb8eb2532e540d"}, + {file = "black-22.10.0-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:1e464456d24e23d11fced2bc8c47ef66d471f845c7b7a42f3bd77bf3d1789650"}, + {file = "black-22.10.0-cp37-cp37m-win_amd64.whl", hash = "sha256:9311e99228ae10023300ecac05be5a296f60d2fd10fff31cf5c1fa4ca4b1988d"}, + {file = "black-22.10.0-cp38-cp38-macosx_11_0_arm64.whl", hash = "sha256:fba8a281e570adafb79f7755ac8721b6cf1bbf691186a287e990c7929c7692ff"}, + {file = "black-22.10.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:915ace4ff03fdfff953962fa672d44be269deb2eaf88499a0f8805221bc68c87"}, + {file = "black-22.10.0-cp38-cp38-win_amd64.whl", hash = "sha256:444ebfb4e441254e87bad00c661fe32df9969b2bf224373a448d8aca2132b395"}, + {file = "black-22.10.0-cp39-cp39-macosx_11_0_arm64.whl", hash = "sha256:974308c58d057a651d182208a484ce80a26dac0caef2895836a92dd6ebd725e0"}, + {file = "black-22.10.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:72ef3925f30e12a184889aac03d77d031056860ccae8a1e519f6cbb742736383"}, + {file = "black-22.10.0-cp39-cp39-win_amd64.whl", hash = "sha256:432247333090c8c5366e69627ccb363bc58514ae3e63f7fc75c54b1ea80fa7de"}, + {file = "black-22.10.0-py3-none-any.whl", hash = "sha256:c957b2b4ea88587b46cf49d1dc17681c1e672864fd7af32fc1e9664d572b3458"}, + {file = "black-22.10.0.tar.gz", hash = "sha256:f513588da599943e0cde4e32cc9879e825d58720d6557062d1098c5ad80080e1"}, ] cffi = [ {file = "cffi-1.15.1-cp27-cp27m-macosx_10_9_x86_64.whl", hash = "sha256:a66d3508133af6e8548451b25058d5812812ec3798c886bf38ed24a98216fab2"}, @@ -644,8 +642,8 @@ gitdb = [ {file = "gitdb-4.0.9.tar.gz", hash = "sha256:bac2fd45c0a1c9cf619e63a90d62bdc63892ef92387424b855792a6cabe789aa"}, ] GitPython = [ - {file = "GitPython-3.1.27-py3-none-any.whl", hash = "sha256:5b68b000463593e05ff2b261acff0ff0972df8ab1b70d3cdbd41b546c8b8fc3d"}, - {file = "GitPython-3.1.27.tar.gz", hash = "sha256:1c885ce809e8ba2d88a29befeb385fcea06338d3640712b59ca623c220bb5704"}, + {file = "GitPython-3.1.28-py3-none-any.whl", hash = "sha256:77bfbd299d8709f6af7e0c70840ef26e7aff7cf0c1ed53b42dd7fc3a310fcb02"}, + {file = "GitPython-3.1.28.tar.gz", hash = "sha256:6bd3451b8271132f099ceeaf581392eaf6c274af74bb06144307870479d0697c"}, ] importlib-metadata = [ {file = "importlib_metadata-5.0.0-py3-none-any.whl", hash = "sha256:ddb0e35065e8938f867ed4928d0ae5bf2a53b7773871bfe6bcc7e4fcdc7dea43"}, @@ -885,8 +883,8 @@ typed-ast = [ {file = "typed_ast-1.5.4.tar.gz", hash = "sha256:39e21ceb7388e4bb37f4c679d72707ed46c2fbf2a5609b8b8ebc4b067d977df2"}, ] typing-extensions = [ - {file = "typing_extensions-4.3.0-py3-none-any.whl", hash = "sha256:25642c956049920a5aa49edcdd6ab1e06d7e5d467fc00e0506c44ac86fbfca02"}, - {file = "typing_extensions-4.3.0.tar.gz", hash = "sha256:e6d2677a32f47fc7eb2795db1dd15c1f34eff616bcaf2cfb5e997f854fa1c4a6"}, + {file = "typing_extensions-4.4.0-py3-none-any.whl", hash = "sha256:16fa4864408f655d35ec496218b85f79b3437c829e93320c7c9215ccfd92489e"}, + {file = "typing_extensions-4.4.0.tar.gz", hash = "sha256:1511434bb92bf8dd198c12b1cc812e800d4181cfcb867674e0f8279cc93087aa"}, ] vulture = [ {file = "vulture-2.6-py2.py3-none-any.whl", hash = "sha256:e792e903ccc063ec4873a8979dcf11b51ea3d65a2d3b31c113d47be48f0cdcae"}, diff --git a/pyproject.toml b/pyproject.toml index c6c8abc..98425bb 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -25,7 +25,7 @@ ansible = "4.10" [tool.poetry.dev-dependencies] bandit = "1.7.4" -black = "22.8.0" +black = "22.10.0" jedi = "0.18.1" poethepoet = "0.16.2" pylint = "2.15.3" From e474f046da8f17aa70d7dbaf4e3c0d2182702fb3 Mon Sep 17 00:00:00 2001 From: Caian Ertl Date: Fri, 7 Oct 2022 22:58:06 -0300 Subject: [PATCH 4/9] ci: reduce dependabot update interval and upgrade actions --- .github/dependabot.yml | 4 ++-- .github/workflows/code-check.yml | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/dependabot.yml b/.github/dependabot.yml index 6c61ae5..51ace20 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -4,9 +4,9 @@ updates: - package-ecosystem: github-actions directory: / schedule: - interval: daily + interval: monthly - package-ecosystem: pip directory: / schedule: - interval: daily + interval: monthly diff --git a/.github/workflows/code-check.yml b/.github/workflows/code-check.yml index bcc89e8..48e8a03 100644 --- a/.github/workflows/code-check.yml +++ b/.github/workflows/code-check.yml @@ -25,14 +25,14 @@ jobs: python-version: 3.8 - name: Install poetry - uses: snok/install-poetry@v1.3.1 + uses: snok/install-poetry@v1.3.2 with: virtualenvs-create: true virtualenvs-in-project: true - name: Cache venv id: code-check-cache - uses: actions/cache@v3.0.9 + uses: actions/cache@v3.0.10 with: path: .venv key: code-check-python-${{ matrix.python-version }}-${{ hashFiles('**/poetry.lock') }} From bff7a641a42b087bc7822cb69fa5584b0e1d3c08 Mon Sep 17 00:00:00 2001 From: Caian Ertl Date: Fri, 7 Oct 2022 22:59:07 -0300 Subject: [PATCH 5/9] feat: improve "changed" flag by parsing the stow output --- stow | 53 ++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 46 insertions(+), 7 deletions(-) diff --git a/stow b/stow index 00927e2..babd135 100644 --- a/stow +++ b/stow @@ -27,9 +27,18 @@ For more information, please see ''' import os +import re + from ansible.module_utils.basic import AnsibleModule +unlink_re = re.compile('^UNLINK: (?P.+)$') + +link_re = re.compile( + '^LINK: (?P.+) => (?P.+)(?P \(reverts previous action\))?$' +) + + def purge_conflicts(conflicted_files): """Delete a file or unlink a symlink conflicting with a package. @@ -131,8 +140,7 @@ def stow_has_conflicts(module, package, cmd): conflicts = [] # Grab the conflicting files path. - stderr_lines = stderr.split('\n') - for sel in stderr_lines: + for sel in stderr.split('\n'): if '* existing target is' in sel: conflict = sel.split(':') conflict = conflict[-1].strip() @@ -146,6 +154,27 @@ def stow_has_conflicts(module, package, cmd): return dict(recoverable=True, message=msg, files=conflicts) +def has_stow_changed_links(stow_output): + if stow_output.strip() == '': + return 0 + + n_linked = set() + n_unlinked = set() + + for sel in stow_output.split('\n'): + + link_match = link_re.match(sel) + if link_match: + n_linked.add(link_match.group('file_path')) + continue + + unlink_match = unlink_re.match(sel) + if unlink_match: + n_unlinked.add(unlink_match.group('link_path')) + + return n_linked != n_unlinked + + def stow(module, package, state): """Perform stow on a package against the filesystem. @@ -193,10 +222,10 @@ def stow(module, package, state): # (supposing execution passed all errors checking). rc_, _, se_ = module.run_command(cmd) if rc_ != 0: - msg = 'execution of command "{cmd}" failed with error code {rc_}; output: "{se_}"' + msg = f'execution of command "{cmd}" failed with error code {rc_}; output: "{se_}"' return dict(error=True, message=msg) - return dict(error=False, changed=(se_ != '')) + return dict(error=False, changed=has_stow_changed_links(se_)) def main(): @@ -204,9 +233,19 @@ def main(): module = AnsibleModule( argument_spec={ - 'dir': {'required': True, 'type': 'str'}, - 'package': {'required': True, 'type': 'list'}, - 'target': {'required': False, 'type': 'str', 'default': os.environ.get('HOME')}, + 'dir': { + 'required': True, + 'type': 'str', + }, + 'package': { + 'required': True, + 'type': 'list', + }, + 'target': { + 'required': False, + 'type': 'str', + 'default': os.environ.get('HOME'), + }, 'state': { 'required': True, 'type': 'str', From 19001776f5c8f9c3371b76b128bc6e0096799916 Mon Sep 17 00:00:00 2001 From: Caian Ertl Date: Mon, 15 Jul 2024 01:37:32 -0300 Subject: [PATCH 6/9] chore: tabulate properties --- pyproject.toml | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 54bced1..c5b9704 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -2,10 +2,10 @@ name = "ansible-stow" version = "1.2.0" description = "Ansible module that interacts with GNU Stow packages" -authors = ["Caian Ertl "] -license = "CC0-1.0" -homepage = "https://github.com/caian-org/ansible-stow" -repository = "https://github.com/caian-org/ansible-stow" +authors = ["Caian Ertl "] +license = "CC0-1.0" +homepage = "https://github.com/caian-org/ansible-stow" +repository = "https://github.com/caian-org/ansible-stow" classifiers = [ # Trove classifiers # Full list: https://pypi.python.org/pypi?%3Aaction=list_classifiers @@ -22,23 +22,23 @@ classifiers = [ ] [tool.poetry.dependencies] -python = "^3.10" +python = "^3.10" ansible = "9.7.0" [tool.poetry.dev-dependencies] -bandit = "1.7.9" -black = "24.4.2" +bandit = "1.7.9" +black = "24.4.2" poethepoet = "0.27.0" -pylint = "3.2.5" -vulture = "2.11" +pylint = "3.2.5" +vulture = "2.11" [tool.poe.tasks] -"fix:style" = "black -l 120 -S stow" +"fix:style" = "black -l 120 -S stow" "check:deadcode" = "vulture stow" -"check:lint" = "pylint --fail-under 9 --rcfile .pylintrc --output-format colorized --verbose stow" -"check:style" = "black -l 120 -S --check stow" +"check:lint" = "pylint --fail-under 9 --rcfile .pylintrc --output-format colorized --verbose stow" +"check:style" = "black -l 120 -S --check stow" "check:security" = "bandit -r stow" [build-system] -requires = ["poetry-core>=1.0.0"] +requires = ["poetry-core>=1.0.0"] build-backend = "poetry.core.masonry.api" From 51eeeb9770f2310e4a75d80d945e73ae8a4c6558 Mon Sep 17 00:00:00 2001 From: Caian Ertl Date: Mon, 15 Jul 2024 01:39:26 -0300 Subject: [PATCH 7/9] test: improve output --- tests/ansible.cfg | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/ansible.cfg b/tests/ansible.cfg index a76b8bb..1e074ac 100644 --- a/tests/ansible.cfg +++ b/tests/ansible.cfg @@ -1,3 +1,4 @@ [defaults] interpreter_python = auto_silent +stdout_callback = debug From 7e79aceb2b94514a9b696f2d4a789f4f009998d5 Mon Sep 17 00:00:00 2001 From: Caian Ertl Date: Mon, 15 Jul 2024 01:40:54 -0300 Subject: [PATCH 8/9] feat: last touches --- stow | 41 +++++++++-------------------------------- 1 file changed, 9 insertions(+), 32 deletions(-) diff --git a/stow b/stow index a837e5f..c76dcd6 100644 --- a/stow +++ b/stow @@ -32,18 +32,12 @@ import re from ansible.module_utils.basic import AnsibleModule -unlink_re = re.compile('^UNLINK: (?P.+)$') - -link_re = re.compile( - '^LINK: (?P.+) => (?P.+)(?P \(reverts previous action\))?$' -) STOW_CONFLICTS_MESSAGE_REGEX_BY_VERSION = { - '2.3.1': r'^\* existing target is neither a link nor a directory: (?P.+)$', + '2.3.1': r'^\* existing target is neither a link nor a directory: (?P.+)$', # pylint: disable-next=line-too-long - '2.4.0': r'^\* cannot stow (?P.+) over existing target (?P.+) since neither a link nor a directory and --adopt not specified$', + '2.4.0': r'^\* cannot stow (?P.+) over existing target (?P.+) since neither a link nor a directory and --adopt not specified$', } - SUPPORTED_STOW_VERSIONS = list(STOW_CONFLICTS_MESSAGE_REGEX_BY_VERSION.keys()) @@ -176,7 +170,7 @@ def stow_has_conflicts(stow_version, module, package, cmd): conflict_match = conflict_re.match(sel.strip()) if conflict_match: - conflicts.append(os.path.join(params['target'], conflict_match.group('target_path'))) + conflicts.append(os.path.join(params['target'], conflict_match.group('link_path'))) conff = ', '.join(f'"{f}"' for f in conflicts) msg = f'unable to stow package "{package}" to "{params["target"]}"; conflicted files: {conff}' @@ -189,34 +183,17 @@ def has_stow_changed_links(stow_output): return 0 n_linked = set() - n_unlinked = set() + link_re = re.compile(r'^LINK: (?P.+) => (?P.+)(?P \(reverts previous action\))?$') - for sel in stow_output.split('\n'): - - link_match = link_re.match(sel) - if link_match: - n_linked.add(link_match.group('file_path')) - continue - - unlink_match = unlink_re.match(sel) - if unlink_match: - n_unlinked.add(unlink_match.group('link_path')) - - return n_linked != n_unlinked - - -def has_stow_changed_links(stow_output): - if stow_output.strip() == '': - return 0 - - n_linked = set() n_unlinked = set() + unlink_re = re.compile(r'^UNLINK: (?P.+)$') for sel in stow_output.split('\n'): + sel = sel.strip() link_match = link_re.match(sel) if link_match: - n_linked.add(link_match.group('file_path')) + n_linked.add(link_match.group('link_path')) continue unlink_match = unlink_re.match(sel) @@ -275,9 +252,9 @@ def stow(stow_version, module, package, state): rc_, _, se_ = module.run_command(cmd) if rc_ != 0: msg = f'execution of command "{cmd}" failed with error code {rc_}; output: "{se_}"' - return dict(error=True, message=msg) + return {'error': True, 'message': msg} - return dict(error=False, changed=has_stow_changed_links(se_)) + return {'error': False, 'changed': has_stow_changed_links(se_)} def main(): From 2ad68e90750bcceba9dc7480bae59d996ae7be2f Mon Sep 17 00:00:00 2001 From: Caian Ertl Date: Mon, 15 Jul 2024 01:43:27 -0300 Subject: [PATCH 9/9] test: add task that fails the test if "changed" is greater than 0 when nothing has changed --- tests/stow.yml | 8 +++++++- tests/test.sh | 11 +++++------ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/tests/stow.yml b/tests/stow.yml index 04ba932..9a4c372 100644 --- a/tests/stow.yml +++ b/tests/stow.yml @@ -7,4 +7,10 @@ stow: state: '{{ state }}' package: foo - dir: '$HOME/package' \ No newline at end of file + dir: '$HOME/package' + register: stowt + + - name: 'fail if "changed" is greater than 0 when nothing has changed' + fail: + msg: 'task should not have changed anything' + when: stowt is changed and fail_if_changed == 'yes' diff --git a/tests/test.sh b/tests/test.sh index 70b3272..49646c9 100755 --- a/tests/test.sh +++ b/tests/test.sh @@ -3,15 +3,15 @@ set -e run_playbook() { - ansible-playbook -i hosts stow.yml --extra-vars "state=$1" -v + ansible-playbook -i hosts stow.yml --extra-vars "state=$1 fail_if_changed=$2" -v } # create the library directory and copy the module -mkdir library +mkdir -p library cp ../stow library # move the package to the home directory -mv package ~ +cp -r package ~ # run the playbook and stow the package figlet "state: present" @@ -34,9 +34,8 @@ test -h "$HOME/.config/foo" && exit 1 figlet "state: latest" run_playbook "present" run_playbook "latest" -run_playbook "latest" -run_playbook "latest" -run_playbook "latest" +run_playbook "latest" "yes" +run_playbook "latest" "yes" run_playbook "absent" # create a file that should conflict with the package