1
0
Fork 0
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:
Alexei Znamensky 2021-05-23 08:20:37 +12:00 committed by GitHub
parent d7e55db99b
commit cc293f90a2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 1 deletions

View 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).

View file

@ -104,6 +104,7 @@ EXAMPLES = r'''
backup: yes backup: yes
''' '''
import io
import os import os
import re import re
import tempfile import tempfile
@ -141,7 +142,7 @@ def do_ini(module, filename, section=None, option=None, value=None,
os.makedirs(destpath) os.makedirs(destpath)
ini_lines = [] ini_lines = []
else: 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() ini_lines = ini_file.readlines()
if module._diff: if module._diff:

View file

@ -480,3 +480,37 @@
assert: assert:
that: that:
- content15 == expected15 - 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