Skip to content

Commit cbde285

Browse files
committed
fix: use trim_mode "-" with explicit -%> for ERB control structures
The ERB renderer uses trim_mode: "-" which requires explicit -%> syntax to suppress newlines from control structure tags. Updated all test templates to: - Add -%> to <% if ... %> tags to suppress trailing newlines - Add -%> to <% if_p(...) do |var| %> tags for conditional helpers - Add -%> to <% .each do |var| %> tags for iteration - Add -%> to <% .each_with_index do |var, idx| %> tags - Add -%> to <% .map {...}.each do |var| %> tags for chained operations - Add -%> to <% end %> tags to close control structures cleanly This approach maintains compatibility with the standard BOSH ERB trim_mode while ensuring test expectations match actual rendered output. Fixes: rkoster/rubionic-workspace#229
1 parent ed63376 commit cbde285

2 files changed

Lines changed: 39 additions & 39 deletions

File tree

templatescompiler/erbrenderer/erb_renderer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ def initialize(json_context_path)
193193
end
194194

195195
def render(src_path, dst_path)
196-
erb = ERB.new(File.read(src_path), trim_mode: ">")
196+
erb = ERB.new(File.read(src_path), trim_mode: "-")
197197
erb.filename = src_path
198198

199199
context_hash = JSON.load(File.read(@json_context_path))

templatescompiler/erbrenderer/erb_renderer_test.go

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -230,9 +230,9 @@ no_default: default_value`
230230
})
231231

232232
It("executes block when property exists", func() {
233-
erbTemplateContent = `<% if_p('feature_config.host') do |host| %>
233+
erbTemplateContent = `<% if_p('feature_config.host') do |host| -%>
234234
host_configured: <%= host %>
235-
<% end %>`
235+
<% end -%>`
236236
expectedTemplateContents = `
237237
host_configured: example.com
238238
`
@@ -249,9 +249,9 @@ host_configured: example.com
249249
})
250250

251251
It("supports multiple properties in if_p", func() {
252-
erbTemplateContent = `<% if_p('feature_config.host', 'feature_config.port') do |host, port| %>
252+
erbTemplateContent = `<% if_p('feature_config.host', 'feature_config.port') do |host, port| -%>
253253
config: <%= host %>:<%= port %>
254-
<% end %>`
254+
<% end -%>`
255255
expectedTemplateContents = `
256256
config: example.com:8080
257257
`
@@ -269,9 +269,9 @@ config: example.com:8080
269269

270270
It("skips block when property is missing", func() {
271271
erbTemplateContent = `before
272-
<% if_p('completely_missing_prop') do |host| %>
272+
<% if_p('completely_missing_prop') do |host| -%>
273273
should_not_appear: <%= host %>
274-
<% end %>
274+
<% end -%>
275275
after`
276276
expectedTemplateContents = `before
277277
@@ -315,9 +315,9 @@ after`
315315
})
316316

317317
It("iterates over array and accesses hash elements", func() {
318-
erbTemplateContent = `<% p('users').each do |user| %>
318+
erbTemplateContent = `<% p('users').each do |user| -%>
319319
user: <%= user['name'] %> pass: <%= user['password'] %>
320-
<% end %>`
320+
<% end -%>`
321321
expectedTemplateContents = `
322322
user: alice pass: secret1
323323
@@ -525,9 +525,9 @@ debug is disabled
525525
})
526526

527527
It("transforms array elements using map with block", func() {
528-
erbTemplateContent = `<% p('servers').map { |s| s['host'] }.each do |host| %>
528+
erbTemplateContent = `<% p('servers').map { |s| s['host'] }.each do |host| -%>
529529
host: <%= host %>
530-
<% end %>`
530+
<% end -%>`
531531
expectedTemplateContents = `
532532
host: 10.0.0.1
533533
@@ -693,9 +693,9 @@ host: 10.0.0.2
693693
})
694694

695695
It("iterates with index access", func() {
696-
erbTemplateContent = `<% p('routes').each_with_index do |route, index| %>
696+
erbTemplateContent = `<% p('routes').each_with_index do |route, index| -%>
697697
route_<%= index %>: <%= route['uri'] %>
698-
<% end %>`
698+
<% end -%>`
699699
expectedTemplateContents = `
700700
route_0: api.example.com
701701
@@ -754,9 +754,9 @@ route_1: www.example.com
754754
})
755755

756756
It("checks for hash key membership", func() {
757-
erbTemplateContent = `<% if p('config').key?('optional_key') %>
757+
erbTemplateContent = `<% if p('config').key?('optional_key') -%>
758758
has_key: true
759-
<% end %>`
759+
<% end -%>`
760760
expectedTemplateContents = `
761761
has_key: true
762762
`
@@ -794,9 +794,9 @@ has_key: true
794794
})
795795

796796
It("checks string prefix with start_with?", func() {
797-
erbTemplateContent = `<% if p('api_url').start_with?('https') %>
797+
erbTemplateContent = `<% if p('api_url').start_with?('https') -%>
798798
secure: true
799-
<% end %>`
799+
<% end -%>`
800800
expectedTemplateContents = `
801801
secure: true
802802
`
@@ -813,9 +813,9 @@ secure: true
813813
})
814814

815815
It("checks for empty strings", func() {
816-
erbTemplateContent = `<% if p('cert') == "" %>
816+
erbTemplateContent = `<% if p('cert') == "" -%>
817817
no_cert: true
818-
<% end %>`
818+
<% end -%>`
819819
expectedTemplateContents = `
820820
no_cert: true
821821
`
@@ -955,12 +955,12 @@ provider_name: thales`
955955
})
956956

957957
It("checks if any element matches condition", func() {
958-
erbTemplateContent = `<% if p('providers').any? { |p| p['type'] == 'hsm' } %>
958+
erbTemplateContent = `<% if p('providers').any? { |p| p['type'] == 'hsm' } -%>
959959
using_hsm: true
960-
<% end %>
961-
<% if !p('empty_list').any? %>
960+
<% end -%>
961+
<% if !p('empty_list').any? -%>
962962
list_empty: true
963-
<% end %>`
963+
<% end -%>`
964964
expectedTemplateContents = `
965965
using_hsm: true
966966
list_empty: true
@@ -1001,18 +1001,18 @@ list_empty: true
10011001
})
10021002

10031003
It("checks for empty strings and arrays", func() {
1004-
erbTemplateContent = `<% if p('optional_cert').empty? %>
1004+
erbTemplateContent = `<% if p('optional_cert').empty? -%>
10051005
no_cert: true
1006-
<% end %>
1007-
<% if !p('required_key').empty? %>
1006+
<% end -%>
1007+
<% if !p('required_key').empty? -%>
10081008
has_key: true
1009-
<% end %>
1010-
<% if p('empty_array').empty? %>
1009+
<% end -%>
1010+
<% if p('empty_array').empty? -%>
10111011
array_empty: true
1012-
<% end %>
1013-
<% if !p('filled_array').empty? %>
1012+
<% end -%>
1013+
<% if !p('filled_array').empty? -%>
10141014
array_filled: true
1015-
<% end %>`
1015+
<% end -%>`
10161016
expectedTemplateContents = `
10171017
no_cert: true
10181018
has_key: true
@@ -1053,12 +1053,12 @@ array_filled: true
10531053
})
10541054

10551055
It("checks array membership", func() {
1056-
erbTemplateContent = `<% if p('valid_modes').include?(p('selected_mode')) %>
1056+
erbTemplateContent = `<% if p('valid_modes').include?(p('selected_mode')) -%>
10571057
valid_selection: true
1058-
<% end %>
1059-
<% if p('tls_modes').include?('enabled') %>
1058+
<% end -%>
1059+
<% if p('tls_modes').include?('enabled') -%>
10601060
supports_tls: true
1061-
<% end %>`
1061+
<% end -%>`
10621062
expectedTemplateContents = `
10631063
valid_selection: true
10641064
supports_tls: true
@@ -1212,14 +1212,14 @@ retries: 3`
12121212
})
12131213

12141214
It("finds substring positions with index", func() {
1215-
erbTemplateContent = `<% if p('cert_with_newlines').index("\n").nil? %>
1215+
erbTemplateContent = `<% if p('cert_with_newlines').index("\n").nil? -%>
12161216
no_real_newline: true
12171217
<% else %>
12181218
has_real_newline: true
1219-
<% end %>
1220-
<% if p('cert_without_newlines').index("\n").nil? %>
1219+
<% end -%>
1220+
<% if p('cert_without_newlines').index("\n").nil? -%>
12211221
no_escaped_newline: true
1222-
<% end %>`
1222+
<% end -%>`
12231223
expectedTemplateContents = `
12241224
has_real_newline: true
12251225

0 commit comments

Comments
 (0)