1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2026-02-04 07:51:50 +00:00
community.general/tests/integration/targets/file_remove/tasks/test_regex.yml
Shahar Golshani af99cc7deb
Add New Module file_remove (#11032)
* Add New Module file_remove

* Add fixes from code review

* Change file_type documentation

* Remove python to_native from the module

* Remove redundant block/always cleanup

* Update plugins/modules/file_remove.py

Co-authored-by: Felix Fontein <felix@fontein.de>

* Update plugins/modules/file_remove.py

Co-authored-by: Felix Fontein <felix@fontein.de>

* Update plugins/modules/file_remove.py

Co-authored-by: Felix Fontein <felix@fontein.de>

* Update plugins/modules/file_remove.py

Co-authored-by: Felix Fontein <felix@fontein.de>

* Update plugins/modules/file_remove.py

Co-authored-by: Felix Fontein <felix@fontein.de>

* Update plugins/modules/file_remove.py

Co-authored-by: Felix Fontein <felix@fontein.de>

* Update plugins/modules/file_remove.py

Co-authored-by: Felix Fontein <felix@fontein.de>

* Update plugins/modules/file_remove.py

Co-authored-by: Felix Fontein <felix@fontein.de>

* Add more nox fixes to latest review

* Update plugins/modules/file_remove.py

LGTM

Co-authored-by: Felix Fontein <felix@fontein.de>

* Update tests/integration/targets/file_remove/tasks/main.yml

Right, that's better.

Co-authored-by: Felix Fontein <felix@fontein.de>

* Fix EXAMPLES regex pattern

* Add warning when listed file was removed by other process during
playbook execution

* remove raise exception from find_matching_files;

* Update plugins/modules/file_remove.py

Co-authored-by: Felix Fontein <felix@fontein.de>

* Update plugins/modules/file_remove.py

Co-authored-by: Felix Fontein <felix@fontein.de>

---------

Co-authored-by: Felix Fontein <felix@fontein.de>
2025-11-21 18:26:30 +01:00

97 lines
2.9 KiB
YAML

---
# Copyright (c) Ansible Project
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later
# Test regex pattern matching
- name: Create test files for regex testing
ansible.builtin.file:
path: "{{ file_remove_testdir }}/{{ item }}"
state: touch
mode: '0644'
loop:
- backup_20241101.tar.gz
- backup_20241102.tar.gz
- backup_20241103.tar.gz
- backup_old.tar.gz
- file123.txt
- file456.txt
- fileabc.txt
- test_file.log
- name: Remove files matching regex backup_[0-9]{8}\.tar\.gz
community.general.file_remove:
path: "{{ file_remove_testdir }}"
pattern: "backup_[0-9]{8}\\.tar\\.gz"
use_regex: true
register: regex_result_1
- name: Verify regex matched files were removed
ansible.builtin.assert:
that:
- regex_result_1 is changed
- regex_result_1.files_count == 3
- "file_remove_testdir ~ '/backup_20241101.tar.gz' in regex_result_1.removed_files"
- "file_remove_testdir ~ '/backup_20241102.tar.gz' in regex_result_1.removed_files"
- "file_remove_testdir ~ '/backup_20241103.tar.gz' in regex_result_1.removed_files"
- name: Verify backup_old.tar.gz still exists
ansible.builtin.stat:
path: "{{ file_remove_testdir }}/backup_old.tar.gz"
register: backup_old_stat
- name: Assert backup_old.tar.gz was not removed
ansible.builtin.assert:
that:
- backup_old_stat.stat.exists
- name: Remove files matching regex file[0-9]+\.txt (digits only)
community.general.file_remove:
path: "{{ file_remove_testdir }}"
pattern: "file[0-9]+\\.txt"
use_regex: true
register: regex_result_2
- name: Verify files with digits were removed
ansible.builtin.assert:
that:
- regex_result_2 is changed
- regex_result_2.files_count == 2
- "file_remove_testdir ~ '/file123.txt' in regex_result_2.removed_files"
- "file_remove_testdir ~ '/file456.txt' in regex_result_2.removed_files"
- name: Verify fileabc.txt still exists
ansible.builtin.stat:
path: "{{ file_remove_testdir }}/fileabc.txt"
register: fileabc_stat
- name: Assert fileabc.txt was not removed
ansible.builtin.assert:
that:
- fileabc_stat.stat.exists
- name: Remove files with regex using anchors ^test.*
community.general.file_remove:
path: "{{ file_remove_testdir }}"
pattern: "^test.*"
use_regex: true
register: regex_result_3
- name: Verify files starting with 'test' were removed
ansible.builtin.assert:
that:
- regex_result_3 is changed
- regex_result_3.files_count == 1
- "file_remove_testdir ~ '/test_file.log' in regex_result_3.removed_files"
- name: Clean up for next test
ansible.builtin.file:
path: "{{ file_remove_testdir }}"
state: absent
- name: Recreate test directory
ansible.builtin.file:
path: "{{ file_remove_testdir }}"
state: directory
mode: '0755'