From 1cacf933b7d3b6a876bf73a99a1f33bf2140b761 Mon Sep 17 00:00:00 2001 From: David Shrewsbury Date: Tue, 6 Apr 2021 12:11:10 -0400 Subject: [PATCH] lineinfile - improve test coverage (#74122) --- .../targets/lineinfile/tasks/main.yml | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/test/integration/targets/lineinfile/tasks/main.yml b/test/integration/targets/lineinfile/tasks/main.yml index cad926b364d..4640d2c978e 100644 --- a/test/integration/targets/lineinfile/tasks/main.yml +++ b/test/integration/targets/lineinfile/tasks/main.yml @@ -29,6 +29,59 @@ - "result.checksum == '5feac65e442c91f557fc90069ce6efc4d346ab51'" - "result.state == 'file'" +- name: "create a file that does not yet exist with `create: yes` and produce diff" + lineinfile: + dest: "{{ output_dir }}/a/a.txt" + state: present + line: "First line" + create: yes + diff: yes + register: result1 + +- name: assert that a diff was returned + assert: + that: + - result1.diff | length > 0 + +- name: stat the new file + stat: + path: "{{ output_dir }}/a/a.txt" + register: result + +- name: assert that the file exists + assert: + that: + - result.stat.exists + +- block: + - name: "EXPECTED FAILURE - test source file does not exist w/o `create: yes`" + lineinfile: + path: "/some/where/that/doesnotexist.txt" + state: present + line: "Doesn't matter" + - fail: + msg: "Should not get here" + rescue: + - name: Validate failure + assert: + that: + - "'Destination /some/where/that/doesnotexist.txt does not exist !' in ansible_failed_result.msg" + +- block: + - name: EXPECTED FAILURE - test invalid `validate` value + lineinfile: + path: "{{ output_dir }}/test.txt" + state: present + line: "Doesn't matter" + validate: '/some/path' + - fail: + msg: "Should not get here" + rescue: + - name: Validate failure + assert: + that: + - "'validate must contain %s: /some/path' in ansible_failed_result.msg" + - name: insert a line at the beginning of the file, and back it up lineinfile: dest: "{{ output_dir }}/test.txt"