mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
ini_file - opening file as utf-8-sig (#2578)
* opening file as utf-8-sig * added changelog fragment * using io.open() * Update tests/integration/targets/ini_file/tasks/main.yml Co-authored-by: Felix Fontein <felix@fontein.de> Co-authored-by: Felix Fontein <felix@fontein.de>
This commit is contained in:
parent
d7e55db99b
commit
cc293f90a2
3 changed files with 38 additions and 1 deletions
2
changelogs/fragments/2578-ini-file-utf8-bom.yml
Normal file
2
changelogs/fragments/2578-ini-file-utf8-bom.yml
Normal file
|
@ -0,0 +1,2 @@
|
|||
minor_changes:
|
||||
- ini_file - opening file with encoding ``utf-8-sig`` (https://github.com/ansible-collections/community.general/issues/2189).
|
|
@ -104,6 +104,7 @@ EXAMPLES = r'''
|
|||
backup: yes
|
||||
'''
|
||||
|
||||
import io
|
||||
import os
|
||||
import re
|
||||
import tempfile
|
||||
|
@ -141,7 +142,7 @@ def do_ini(module, filename, section=None, option=None, value=None,
|
|||
os.makedirs(destpath)
|
||||
ini_lines = []
|
||||
else:
|
||||
with open(filename, 'r') as ini_file:
|
||||
with io.open(filename, 'r', encoding="utf-8-sig") as ini_file:
|
||||
ini_lines = ini_file.readlines()
|
||||
|
||||
if module._diff:
|
||||
|
|
|
@ -480,3 +480,37 @@
|
|||
assert:
|
||||
that:
|
||||
- content15 == expected15
|
||||
|
||||
- name: Create starting ini file
|
||||
copy:
|
||||
# The content below is the following text file with BOM:
|
||||
# [section1]
|
||||
# var1=aaa
|
||||
# var2=bbb
|
||||
# [section2]
|
||||
# var3=ccc
|
||||
content: !!binary |
|
||||
77u/W3NlY3Rpb24xXQp2YXIxPWFhYQp2YXIyPWJiYgpbc2VjdGlvbjJdCnZhcjM9Y2NjCg==
|
||||
dest: "{{ output_file }}"
|
||||
- name: Test ini breakage
|
||||
ini_file:
|
||||
path: "{{ output_file }}"
|
||||
section: section1
|
||||
option: var4
|
||||
value: 0
|
||||
|
||||
- name: read content from output file
|
||||
slurp:
|
||||
src: "{{ output_file }}"
|
||||
register: output_content
|
||||
|
||||
- name: set expected content and get current ini file content
|
||||
set_fact:
|
||||
expected16: "[section1]\nvar1=aaa\nvar2=bbb\nvar4 = 0\n[section2]\nvar3=ccc\n"
|
||||
content16: "{{ output_content.content | b64decode }}"
|
||||
- debug:
|
||||
var: content16
|
||||
- name: Verify content of ini file is as expected
|
||||
assert:
|
||||
that:
|
||||
- content16 == expected16
|
||||
|
|
Loading…
Reference in a new issue