File: parse_heredoc_spec.rb

package info (click to toggle)
puppet-agent 7.23.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 19,092 kB
  • sloc: ruby: 245,074; sh: 456; makefile: 38; xml: 33
file content (276 lines) | stat: -rw-r--r-- 6,194 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
require 'spec_helper'
require 'puppet/pops'

# relative to this spec file (./) does not work as this file is loaded by rspec
require File.join(File.dirname(__FILE__), '/parser_rspec_helper')

describe "egrammar parsing heredoc" do
  include ParserRspecHelper

  it "parses plain heredoc" do
    expect(dump(parse("@(END)\nThis is\nheredoc text\nEND\n"))).to eq([
      "(@()",
      "  'This is\nheredoc text\n'",
      ")"
    ].join("\n"))
  end

  it "parses heredoc with margin" do
    src = [
    "@(END)",
    "   This is",
    "   heredoc text",
    "   | END",
    ""
    ].join("\n")
    expect(dump(parse(src))).to eq([
      "(@()",
      "  'This is\nheredoc text\n'",
      ")"
    ].join("\n"))
  end

  it "parses heredoc with margin and right newline trim" do
    src = [
    "@(END)",
    "   This is",
    "   heredoc text",
    "   |- END",
    ""
    ].join("\n")
    expect(dump(parse(src))).to eq([
      "(@()",
      "  'This is\nheredoc text'",
      ")"
    ].join("\n"))
  end

  it "parses syntax and escape specification" do
    src = <<-CODE
    @(END:syntax/t)
    Tex\\tt\\n
    |- END
    CODE
    expect(dump(parse(src))).to eq([
      "(@(syntax)",
      "  'Tex\tt\\n'",
      ")"
    ].join("\n"))
  end

  it "parses interpolated heredoc expression" do
    src = <<-CODE
    @("END")
    Hello $name
    |- END
    CODE
    expect(dump(parse(src))).to eq([
      "(@()",
      "  (cat 'Hello ' (str $name))",
      ")"
    ].join("\n"))
  end

  it "parses interpolated heredoc expression containing escapes" do
    src = <<-CODE
    @("END")
    Hello \\$name
    |- END
    CODE
    expect(dump(parse(src))).to eq([
      "(@()",
      "  (cat 'Hello \\' (str $name))",
      ")"
    ].join("\n"))
  end

  it "parses interpolated heredoc expression containing escapes when escaping other things than $" do
    src = <<-CODE
    @("END"/t)
    Hello \\$name
    |- END
    CODE
    expect(dump(parse(src))).to eq([
      "(@()",
      "  (cat 'Hello \\' (str $name))",
      ")"
    ].join("\n"))
  end

  it "parses with escaped newlines without preceding whitespace" do
    src = <<-CODE
    @(END/L)
    First Line\\
     Second Line
    |- END
    CODE
    parse(src)
    expect(dump(parse(src))).to eq([
      "(@()",
      "  'First Line Second Line'",
      ")"
    ].join("\n"))
  end

  it "parses with escaped newlines with proper margin" do
    src = <<-CODE
    @(END/L)
     First Line\\
      Second Line
    |- END
    CODE
    parse(src)
    expect(dump(parse(src))).to eq([
      "(@()",
      "  ' First Line  Second Line'",
      ")"
    ].join("\n"))
  end

  it "parses interpolated heredoc expression with false start on $" do
    src = <<-CODE
    @("END")
    Hello $name$%a
    |- END
    CODE
    expect(dump(parse(src))).to eq([
      "(@()",
      "  (cat 'Hello ' (str $name) '$%a')",
      ")"
    ].join("\n"))
  end

  it "parses interpolated [] expression by looking at the correct preceding char for space when there is no heredoc margin" do
    # NOTE: Important not to use the left margin feature here
    src = <<-CODE
$xxxxxxx = @("END")
${facts['os']['family']}
XXXXXXX XXX
END
CODE
    expect(dump(parse(src))).to eq([
      "(= $xxxxxxx (@()",
      "  (cat (str (slice (slice $facts 'os') 'family')) '",
      "XXXXXXX XXX",
      "')",
      "))"].join("\n"))
  end

  it "parses interpolated [] expression by looking at the correct preceding char for space when there is a heredoc margin" do
    # NOTE: Important not to use the left margin feature here - the problem in PUP 9303 is triggered by lines and text before
    # an interpolation containing []. 
    src = <<-CODE
# comment
# comment
$xxxxxxx = @("END")
    1
    2
    3
    4
    5
    YYYYY${facts['fqdn']}
    XXXXXXX XXX
    | END
CODE
    expect(dump(parse(src))).to eq([
      "(= $xxxxxxx (@()",
      "  (cat '1", "2", "3", "4", "5",
      "YYYYY' (str (slice $facts 'fqdn')) '",
      "XXXXXXX XXX",
      "')",
      "))"].join("\n"))
  end

  it "correctly reports an error location in a nested heredoc with margin" do
    # NOTE: Important not to use the left margin feature here - the problem in PUP 9303 is triggered by lines and text before
    # an interpolation containing []. 
    src = <<-CODE
# comment
# comment
$xxxxxxx = @("END")
  1
  2
  3
  4
  5
  YYYYY${facts]}
  XXXXXXX XXX
  | END
CODE
    expect{parse(src)}.to raise_error(/Syntax error at '\]' \(line: 9, column: 15\)/)
  end

  it "correctly reports an error location in a heredoc with line endings escaped" do
    # DO NOT CHANGE INDENTATION OF THIS HEREDOC
    src = <<-CODE
    # line one
    # line two
    @("END"/L)
    First Line\\
    Second Line ${facts]}
    |- END
    CODE
    expect{parse(src)}.to raise_error(/Syntax error at '\]' \(line: 5, column: 24\)/)
  end

  it "correctly reports an error location in a heredoc with line endings escaped when there is text in the margin" do
    # DO NOT CHANGE INDENTATION OR SPACING OF THIS HEREDOC
    src = <<-CODE
    # line one
    # line two
    @("END"/L)
    First Line\\
    Second Line
  x Third Line ${facts]}
    |- END
    # line 8
    # line 9
    CODE
    expect{parse(src)}.to raise_error(/Syntax error at '\]' \(line: 6, column: 23\)/)
  end

  it "correctly reports an error location in a heredoc with line endings escaped when there is text in the margin" do
    # DO NOT CHANGE INDENTATION OR SPACING OF THIS HEREDOC
    src = <<-CODE
    @(END)
AAA
 BBB
  CCC
   DDD
    EEE
     FFF
    |- END
    CODE
    expect(dump(parse(src))).to eq([
      "(@()",
      "  'AAA", # no left space trimmed
      " BBB",
      "  CCC",
      "   DDD",
      "EEE", # left space trimmed
      " FFF'", # indented one because it is one in from margin marker
      ")"].join("\n"))
  end

  it 'parses multiple heredocs on the same line' do
    src = <<-CODE
    notice({ @(foo) => @(bar) })
    hello
    -foo
    world
    -bar
    notice '!'
    CODE
    expect(dump(parse(src))).to eq([
      '(block',
      '  (invoke notice ({} ((@()',
      '    \'    hello\'',
      '  ) (@()',
      '    \'    world\'',
      '  ))))',
      '  (invoke notice \'!\')',
      ')'
    ].join("\n"))
  end

end