From 19dc267e4cae675831015b2c224d7d961398cc58 Mon Sep 17 00:00:00 2001 From: Julien Champseix Date: Wed, 25 Jul 2018 22:10:40 +0200 Subject: Allow specifying the output encoding in the template module (#42171) Allow specifying the source and destination files' encodings in the template module * Added output_encoding to the template module, default to utf-8 * Added documentation for the new variables * Leveraged the encoding argument on to_text() and to_bytes() to keep the implementation as simple as possible * Added integration tests with files in utf-8 and windows-1252 encodings, testing all combinations * fix bad smell test by excluding windows-1252 files from the utf8 checks * fix bad smell test by excluding valid files from the smart quote test --- .../template/files/encoding_1252_utf-8.expected | 1 + .../files/encoding_1252_windows-1252.expected | 1 + test/integration/targets/template/tasks/main.yml | 23 ++++++++++++++++++++++ .../targets/template/templates/encoding_1252.j2 | 1 + 4 files changed, 26 insertions(+) create mode 100644 test/integration/targets/template/files/encoding_1252_utf-8.expected create mode 100644 test/integration/targets/template/files/encoding_1252_windows-1252.expected create mode 100644 test/integration/targets/template/templates/encoding_1252.j2 (limited to 'test/integration/targets/template') diff --git a/test/integration/targets/template/files/encoding_1252_utf-8.expected b/test/integration/targets/template/files/encoding_1252_utf-8.expected new file mode 100644 index 0000000000..0d3cc35251 --- /dev/null +++ b/test/integration/targets/template/files/encoding_1252_utf-8.expected @@ -0,0 +1 @@ +windows-1252 Special Characters: €‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”•–—˜™š›œžŸ ¡¢£¤¥¦§¨©ª«¬­®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ diff --git a/test/integration/targets/template/files/encoding_1252_windows-1252.expected b/test/integration/targets/template/files/encoding_1252_windows-1252.expected new file mode 100644 index 0000000000..7fb94a7b06 --- /dev/null +++ b/test/integration/targets/template/files/encoding_1252_windows-1252.expected @@ -0,0 +1 @@ +windows-1252 Special Characters: diff --git a/test/integration/targets/template/tasks/main.yml b/test/integration/targets/template/tasks/main.yml index 7e34852afa..87e2e7d16f 100644 --- a/test/integration/targets/template/tasks/main.yml +++ b/test/integration/targets/template/tasks/main.yml @@ -619,5 +619,28 @@ - 'template_results.mode == "0547"' - 'stat_results.stat["mode"] == "0547"' +# Test output_encoding +- name: Prepare the list of encodings we want to check, including empty string for defaults + set_fact: + template_encoding_1252_encodings: ['', 'utf-8', 'windows-1252'] + +- name: Copy known good encoding_1252_*.expected into place + copy: + src: 'encoding_1252_{{ item | default("utf-8", true) }}.expected' + dest: '{{ output_dir }}/encoding_1252_{{ item }}.expected' + loop: '{{ template_encoding_1252_encodings }}' + +- name: Generate the encoding_1252_* files from templates using various encoding combinations + template: + src: 'encoding_1252.j2' + dest: '{{ output_dir }}/encoding_1252_{{ item }}.txt' + output_encoding: '{{ item }}' + loop: '{{ template_encoding_1252_encodings }}' + +- name: Compare the encoding_1252_* templated files to known good + command: diff -u {{ output_dir }}/encoding_1252_{{ item }}.expected {{ output_dir }}/encoding_1252_{{ item }}.txt + register: encoding_1252_diff_result + loop: '{{ template_encoding_1252_encodings }}' + # aliases file requires root for template tests so this should be safe - include: backup_test.yml diff --git a/test/integration/targets/template/templates/encoding_1252.j2 b/test/integration/targets/template/templates/encoding_1252.j2 new file mode 100644 index 0000000000..0d3cc35251 --- /dev/null +++ b/test/integration/targets/template/templates/encoding_1252.j2 @@ -0,0 +1 @@ +windows-1252 Special Characters: €‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”•–—˜™š›œžŸ ¡¢£¤¥¦§¨©ª«¬­®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ -- cgit v1.2.1