|
1 | 1 | require 'spec_helper'
|
2 | 2 |
|
3 | 3 | describe Fastlane::Actions::AnLocalizeLibsAction do
|
4 |
| - # This test is more of a way of ensuring `run_described_fastlane_action` handles array of hashes properly than a comprehensive test for the `an_localize_libs_action` action. |
5 |
| - # |
6 |
| - # Please consider expanding this test if you'll find yourself working on its action. |
7 |
| - it 'merges the strings from the given array into the given main strings file' do |
8 |
| - in_tmp_dir do |tmp_dir| |
9 |
| - app_strings_path = File.join(tmp_dir, 'app.xml') |
10 |
| - File.write(app_strings_path, android_xml_with_content('<string name="a_string">test from app</string>')) |
11 |
| - |
12 |
| - lib1_strings_path = File.join(tmp_dir, 'lib1.xml') |
13 |
| - File.write(lib1_strings_path, android_xml_with_content('<string name="a_lib1_string">test from lib 1</string>')) |
14 |
| - |
15 |
| - lib2_strings_path = File.join(tmp_dir, 'lib2.xml') |
16 |
| - File.write(lib2_strings_path, android_xml_with_content('<string name="a_lib2_string">test from lib 2</string>')) |
17 |
| - |
18 |
| - run_described_fastlane_action( |
19 |
| - app_strings_path: app_strings_path, |
20 |
| - libs_strings_path: [ |
21 |
| - { library: 'lib_1', strings_path: lib1_strings_path, exclusions: [] }, |
22 |
| - { library: 'lib_2', strings_path: lib2_strings_path, exclusions: [] }, |
23 |
| - ] |
24 |
| - ) |
25 |
| - |
26 |
| - expected = <<~XML |
27 |
| - <string name="a_string">test from app</string> |
28 |
| - <string name="a_lib1_string">test from lib 1</string> |
29 |
| - <string name="a_lib2_string">test from lib 2</string> |
30 |
| - XML |
31 |
| - expect(File.read(app_strings_path)).to eq(android_xml_with_content(expected)) |
32 |
| - end |
| 4 | + def android_xml_with_lines(lines) |
| 5 | + # I couldn't find a way to interpolate a multiline string preserving its indentation in the heredoc below, so I hacked the following transformation of the input that adds the desired indentation to all lines. |
| 6 | + # |
| 7 | + # The desired indentation is 4 spaces to stay aligned with the production code applies when merging the XMLs. |
| 8 | + indented_content = lines.map { |l| " #{l.chomp}" }.join("\n") |
| 9 | + |
| 10 | + return <<~XML |
| 11 | + <?xml version="1.0" encoding="UTF-8"?> |
| 12 | + <resources xmlns:tools="http://schemas.android.com/tools"> |
| 13 | + #{indented_content} |
| 14 | + </resources> |
| 15 | + XML |
33 | 16 | end
|
34 |
| -end |
35 | 17 |
|
36 |
| -def android_xml_with_content(content) |
37 |
| - # I couldn't find a way to interpolate a multiline string preserving its indentation in the heredoc below, so I hacked the following transformation of the input that adds the desired indentation to all lines. |
38 |
| - # |
39 |
| - # The desired indentation is 4 spaces to stay aligned with the production code applies when merging the XMLs. |
40 |
| - indented_content = content.chomp.lines.map { |l| " #{l}" }.join |
41 |
| - |
42 |
| - return <<~XML |
43 |
| - <?xml version="1.0" encoding="UTF-8"?> |
44 |
| - <resources xmlns:tools="http://schemas.android.com/tools"> |
45 |
| - #{indented_content} |
46 |
| - </resources> |
47 |
| - XML |
| 18 | + def write_android_xml(path, lines) |
| 19 | + File.write(path, android_xml_with_lines(lines)) |
| 20 | + end |
| 21 | + |
| 22 | + describe 'merges the strings from the given array into the given main strings file' do |
| 23 | + it 'handles simple XMLs with no duplicates nor attributes' do |
| 24 | + in_tmp_dir do |tmp_dir| |
| 25 | + app_strings_path = File.join(tmp_dir, 'app.xml') |
| 26 | + File.write(app_strings_path, android_xml_with_lines(['<string name="a_string">test from app</string>'])) |
| 27 | + |
| 28 | + lib1_strings_path = File.join(tmp_dir, 'lib1.xml') |
| 29 | + File.write(lib1_strings_path, android_xml_with_lines(['<string name="a_lib1_string">test from lib 1</string>'])) |
| 30 | + |
| 31 | + lib2_strings_path = File.join(tmp_dir, 'lib2.xml') |
| 32 | + File.write(lib2_strings_path, android_xml_with_lines(['<string name="a_lib2_string">test from lib 2</string>'])) |
| 33 | + |
| 34 | + run_described_fastlane_action( |
| 35 | + app_strings_path: app_strings_path, |
| 36 | + libs_strings_path: [ |
| 37 | + { library: 'lib_1', strings_path: lib1_strings_path, exclusions: [] }, |
| 38 | + { library: 'lib_2', strings_path: lib2_strings_path, exclusions: [] }, |
| 39 | + ] |
| 40 | + ) |
| 41 | + |
| 42 | + expected = [ |
| 43 | + '<string name="a_string">test from app</string>', |
| 44 | + '<string name="a_lib1_string">test from lib 1</string>', |
| 45 | + '<string name="a_lib2_string">test from lib 2</string>', |
| 46 | + ] |
| 47 | + expect(File.read(app_strings_path)).to eq(android_xml_with_lines(expected)) |
| 48 | + end |
| 49 | + end |
| 50 | + |
| 51 | + it 'keeps app value if content_override is used' do |
| 52 | + in_tmp_dir do |tmp_dir| |
| 53 | + app_strings_path = File.join(tmp_dir, 'app.xml') |
| 54 | + app_xml_lines = [ |
| 55 | + '<string name="override-true" content_override="true">from app override-true</string>', |
| 56 | + '<string name="override-false" content_override="false">from app override-false</string>', |
| 57 | + '<string name="override-missing">from app override-missing</string>', |
| 58 | + ] |
| 59 | + File.write(app_strings_path, android_xml_with_lines(app_xml_lines)) |
| 60 | + |
| 61 | + lib_strings_path = File.join(tmp_dir, 'lib.xml') |
| 62 | + lib_xml_lines = [ |
| 63 | + '<string name="override-true">from lib override-true</string>', |
| 64 | + '<string name="override-false">from lib override-false</string>', |
| 65 | + '<string name="override-missing">from lib override-missing</string>', |
| 66 | + ] |
| 67 | + File.write(lib_strings_path, android_xml_with_lines(lib_xml_lines)) |
| 68 | + |
| 69 | + run_described_fastlane_action( |
| 70 | + app_strings_path: app_strings_path, |
| 71 | + libs_strings_path: [ |
| 72 | + { library: 'lib', strings_path: lib_strings_path, exclusions: [] }, |
| 73 | + ] |
| 74 | + ) |
| 75 | + |
| 76 | + expected = [ |
| 77 | + '<string name="override-true" content_override="true">from app override-true</string>', |
| 78 | + '', '', # FIXME: Current implementation adds empty lines; we should get rid of those at some point |
| 79 | + '<string name="override-false">from lib override-false</string>', |
| 80 | + '<string name="override-missing">from lib override-missing</string>', |
| 81 | + ] |
| 82 | + expect(File.read(app_strings_path)).to eq(android_xml_with_lines(expected)) |
| 83 | + end |
| 84 | + end |
| 85 | + |
| 86 | + it 'adds a8c-lib-src attribute if provided' do |
| 87 | + in_tmp_dir do |tmp_dir| |
| 88 | + app_strings_path = File.join(tmp_dir, 'app.xml') |
| 89 | + app_xml_lines = [ |
| 90 | + '<string name="override-true" content_override="true">from app override-true</string>', |
| 91 | + '<string name="override-missing">from app override-missing</string>', |
| 92 | + ] |
| 93 | + File.write(app_strings_path, android_xml_with_lines(app_xml_lines)) |
| 94 | + |
| 95 | + lib1_strings_path = File.join(tmp_dir, 'lib1.xml') |
| 96 | + lib1_xml_lines = [ |
| 97 | + '<string name="override-true">from lib1 override-true</string>', |
| 98 | + '<string name="override-missing">from lib1 override-missing</string>', |
| 99 | + '<string name="lib1-key">Key only present in lib1</string>', |
| 100 | + ] |
| 101 | + File.write(lib1_strings_path, android_xml_with_lines(lib1_xml_lines)) |
| 102 | + |
| 103 | + lib2_strings_path = File.join(tmp_dir, 'lib2.xml') |
| 104 | + lib2_xml_lines = [ |
| 105 | + '<string name="override-true">from lib2 override-true</string>', |
| 106 | + '<string name="override-missing">from lib2 override-missing</string>', |
| 107 | + '<string name="lib2-key">Key only present in lib2</string>', |
| 108 | + ] |
| 109 | + File.write(lib2_strings_path, android_xml_with_lines(lib2_xml_lines)) |
| 110 | + |
| 111 | + run_described_fastlane_action( |
| 112 | + app_strings_path: app_strings_path, |
| 113 | + libs_strings_path: [ |
| 114 | + { library: 'lib1', strings_path: lib1_strings_path, source_id: 'lib1-id' }, |
| 115 | + { library: 'lib2', strings_path: lib2_strings_path, source_id: 'lib2-id' }, |
| 116 | + ] |
| 117 | + ) |
| 118 | + |
| 119 | + expected = [ |
| 120 | + '<string name="override-true" content_override="true">from app override-true</string>', |
| 121 | + '', # FIXME: Current implementation adds empty lines; we should get rid of those at some point |
| 122 | + '<string name="lib1-key" a8c-src-lib="lib1-id">Key only present in lib1</string>', |
| 123 | + '<string name="override-missing" a8c-src-lib="lib2-id">from lib2 override-missing</string>', |
| 124 | + '<string name="lib2-key" a8c-src-lib="lib2-id">Key only present in lib2</string>', |
| 125 | + '', # FIXME: Current implementation adds empty lines; we should get rid of those at some point |
| 126 | + ] |
| 127 | + expect(File.read(app_strings_path)).to eq(android_xml_with_lines(expected)) |
| 128 | + end |
| 129 | + end |
| 130 | + |
| 131 | + it 'adds tools:ignore attribute when requested' do |
| 132 | + in_tmp_dir do |tmp_dir| |
| 133 | + app_strings_path = File.join(tmp_dir, 'app.xml') |
| 134 | + app_xml_lines = [ |
| 135 | + '<string name="override-true" content_override="true">from app, override true</string>', |
| 136 | + '<string name="ignore-unused" tools:ignore="UnusedResources">from app, tools:ignore="UnusedResources"</string>', |
| 137 | + '<string name="ignore-x-unused-y" tools:ignore="X,UnusedResources,Y">from app, tools:ignore mix</string>', |
| 138 | + '<string name="ignore-x-y" tools:ignore="X,Y">from app, tools:ignore mix</string>', |
| 139 | + ] |
| 140 | + File.write(app_strings_path, android_xml_with_lines(app_xml_lines)) |
| 141 | + |
| 142 | + lib1_strings_path = File.join(tmp_dir, 'lib1.xml') |
| 143 | + lib1_xml_lines = [ |
| 144 | + '<string name="override-true">from lib1, override true</string>', |
| 145 | + '<string name="lib1-key">Key only present in lib1, no extra attribute</string>', |
| 146 | + '<string name="lib1-ignore-unused" tools:ignore="UnusedResources">Key only present in lib1, with tools:ignore attribute</string>', |
| 147 | + '<string name="lib1-ignore-x-y" tools:ignore="X,Y">Key only present in lib1, with tools:ignore attribute x,y</string>', |
| 148 | + '<string name="lib1-ignore-x-unused-y" tools:ignore="X,UnusedResources,Y">Key only present in lib1, with tools:ignore attribute x,y</string>', |
| 149 | + ] |
| 150 | + File.write(lib1_strings_path, android_xml_with_lines(lib1_xml_lines)) |
| 151 | + |
| 152 | + lib2_strings_path = File.join(tmp_dir, 'lib2.xml') |
| 153 | + lib2_xml_lines = [ |
| 154 | + '<string name="override-true">from lib2, override true</string>', |
| 155 | + '<string name="lib2-key">Key only present in lib2, no extra attribute</string>', |
| 156 | + '<string name="lib2-ignore-unused" tools:ignore="UnusedResources">Key only present in lib2, with tools:ignore attribute</string>', |
| 157 | + '<string name="lib2-ignore-x-y" tools:ignore="X,Y">Key only present in lib2, with tools:ignore attribute x,y</string>', |
| 158 | + '<string name="lib2-ignore-x-unused-y" tools:ignore="X,UnusedResources,Y">Key only present in lib2, with tools:ignore attribute x,y</string>', |
| 159 | + ] |
| 160 | + File.write(lib2_strings_path, android_xml_with_lines(lib2_xml_lines)) |
| 161 | + |
| 162 | + run_described_fastlane_action( |
| 163 | + app_strings_path: app_strings_path, |
| 164 | + libs_strings_path: [ |
| 165 | + { library: 'lib1', strings_path: lib1_strings_path, source_id: 'lib1', add_ignore_attr: true }, |
| 166 | + { library: 'lib2', strings_path: lib2_strings_path, source_id: 'lib2' }, |
| 167 | + ] |
| 168 | + ) |
| 169 | + |
| 170 | + expected = [ |
| 171 | + '<string name="override-true" content_override="true">from app, override true</string>', |
| 172 | + '<string name="ignore-unused" tools:ignore="UnusedResources">from app, tools:ignore="UnusedResources"</string>', |
| 173 | + '<string name="ignore-x-unused-y" tools:ignore="X,UnusedResources,Y">from app, tools:ignore mix</string>', |
| 174 | + '<string name="ignore-x-y" tools:ignore="X,Y">from app, tools:ignore mix</string>', |
| 175 | + '<string name="lib1-key" tools:ignore="UnusedResources" a8c-src-lib="lib1">Key only present in lib1, no extra attribute</string>', |
| 176 | + '<string name="lib1-ignore-unused" tools:ignore="UnusedResources" a8c-src-lib="lib1">Key only present in lib1, with tools:ignore attribute</string>', |
| 177 | + '<string name="lib1-ignore-x-y" tools:ignore="X,Y,UnusedResources" a8c-src-lib="lib1">Key only present in lib1, with tools:ignore attribute x,y</string>', |
| 178 | + '<string name="lib1-ignore-x-unused-y" tools:ignore="X,UnusedResources,Y" a8c-src-lib="lib1">Key only present in lib1, with tools:ignore attribute x,y</string>', |
| 179 | + '<string name="lib2-key" a8c-src-lib="lib2">Key only present in lib2, no extra attribute</string>', |
| 180 | + '<string name="lib2-ignore-unused" tools:ignore="UnusedResources" a8c-src-lib="lib2">Key only present in lib2, with tools:ignore attribute</string>', |
| 181 | + '<string name="lib2-ignore-x-y" tools:ignore="X,Y" a8c-src-lib="lib2">Key only present in lib2, with tools:ignore attribute x,y</string>', |
| 182 | + '<string name="lib2-ignore-x-unused-y" tools:ignore="X,UnusedResources,Y" a8c-src-lib="lib2">Key only present in lib2, with tools:ignore attribute x,y</string>', |
| 183 | + ] |
| 184 | + expect(File.read(app_strings_path)).to eq(android_xml_with_lines(expected)) |
| 185 | + end |
| 186 | + end |
| 187 | + |
| 188 | + it 'handles exclusions list per library' do |
| 189 | + in_tmp_dir do |tmp_dir| |
| 190 | + app_strings_path = File.join(tmp_dir, 'app.xml') |
| 191 | + app_xml_lines = [ |
| 192 | + '<string name="override-true" content_override="true">from app override-true</string>', |
| 193 | + '<string name="override-false" content_override="false">from app override-false</string>', |
| 194 | + '<string name="override-missing">from app override-missing</string>', |
| 195 | + ] |
| 196 | + File.write(app_strings_path, android_xml_with_lines(app_xml_lines)) |
| 197 | + |
| 198 | + lib_strings_path = File.join(tmp_dir, 'lib.xml') |
| 199 | + lib_xml_lines = [ |
| 200 | + '<string name="override-true">from lib override-true</string>', |
| 201 | + '<string name="override-false">from lib override-false</string>', |
| 202 | + '<string name="override-missing">from lib override-missing</string>', |
| 203 | + ] |
| 204 | + File.write(lib_strings_path, android_xml_with_lines(lib_xml_lines)) |
| 205 | + |
| 206 | + run_described_fastlane_action( |
| 207 | + app_strings_path: app_strings_path, |
| 208 | + libs_strings_path: [ |
| 209 | + { library: 'lib', strings_path: lib_strings_path, exclusions: ['override-missing'] }, |
| 210 | + ] |
| 211 | + ) |
| 212 | + |
| 213 | + expected = [ |
| 214 | + '<string name="override-true" content_override="true">from app override-true</string>', |
| 215 | + '', # FIXME: Current implementation adds empty lines; we should get rid of those at some point |
| 216 | + '<string name="override-missing">from app override-missing</string>', |
| 217 | + '<string name="override-false">from lib override-false</string>', |
| 218 | + ] |
| 219 | + expect(File.read(app_strings_path)).to eq(android_xml_with_lines(expected)) |
| 220 | + end |
| 221 | + end |
| 222 | + end |
48 | 223 | end
|
0 commit comments