Skip to content
This repository was archived by the owner on Jan 20, 2025. It is now read-only.

Commit aa700eb

Browse files
Fixed to support multiple heredocs, and to include initial line and heredoc separators in values
1 parent 4710a47 commit aa700eb

File tree

3 files changed

+62
-10
lines changed

3 files changed

+62
-10
lines changed

parse.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,17 @@ func ParseReader(file io.Reader) ([]Command, error) {
7474
}
7575

7676
cmd.Json = child.Attributes["json"]
77+
for n := child.Next; n != nil; n = n.Next {
78+
cmd.Value = append(cmd.Value, n.Value)
79+
}
7780

78-
if len(child.Heredocs) == 0 {
79-
for n := child.Next; n != nil; n = n.Next {
80-
cmd.Value = append(cmd.Value, n.Value)
81-
}
82-
} else {
83-
for _, value := range child.Heredocs {
84-
cmd.Value = append(cmd.Value, value.Content)
85-
cmd.Original = cmd.Original + "\n" + value.Content + value.Name + "\n"
81+
if len(child.Heredocs) != 0 {
82+
// For heredocs, add heredocs extra lines to Original,
83+
// and each content to the Value array.
84+
cmd.Original = cmd.Original + "\n"
85+
for _, heredoc := range child.Heredocs {
86+
cmd.Original = cmd.Original + heredoc.Content + heredoc.Name + "\n"
87+
cmd.Value = append(cmd.Value, heredoc.Content+heredoc.Name+"\n")
8688
}
8789
}
8890

parse_test.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,29 @@ EOF
133133
StartLine: 1,
134134
EndLine: 5,
135135
Flags: []string{},
136-
Value: []string{"source $HOME/.bashrc && echo $HOME\necho \"Hello\" >> /hello\necho \"World!\" >> /hello\n"},
136+
Value: []string{"<<EOF", "source $HOME/.bashrc && echo $HOME\necho \"Hello\" >> /hello\necho \"World!\" >> /hello\nEOF\n"},
137+
},
138+
}
139+
assert.Equal(t, expected, cmds)
140+
}
141+
142+
func TestParseReaderHeredocsMultiple(t *testing.T) {
143+
dockerfile := `COPY <<FILE1 <<FILE2 /dest
144+
content 1
145+
FILE1
146+
content 2
147+
FILE2
148+
`
149+
cmds, err := ParseReader(bytes.NewBufferString(dockerfile))
150+
assert.Nil(t, err)
151+
expected := []Command{
152+
Command{
153+
Cmd: "COPY",
154+
Original: dockerfile,
155+
StartLine: 1,
156+
EndLine: 5,
157+
Flags: []string{},
158+
Value: []string{"<<FILE1", "<<FILE2", "/dest", "content 1\nFILE1\n", "content 2\nFILE2\n"},
137159
},
138160
}
139161
assert.Equal(t, expected, cmds)

tests/dockerfile_test.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,37 @@ def test_heredoc_string_success():
116116
dockerfile.Command(
117117
cmd='RUN', sub_cmd=None, json=False, flags=(),
118118
value=(
119+
'<<EOF',
119120
'source $HOME/.bashrc && echo $HOME\n'
120121
'echo "Hello" >> /hello\n'
121-
'echo "World!" >> /hello\n',
122+
'echo "World!" >> /hello\n'
123+
'EOF\n',
124+
),
125+
start_line=1, end_line=5, original=test_string,
126+
),
127+
)
128+
129+
130+
def test_heredoc_string_multiple_success():
131+
test_string = (
132+
'COPY <<FILE1 <<FILE2 /dest\n'
133+
'content 1\n'
134+
'FILE1\n'
135+
'content 2\n'
136+
'FILE2\n'
137+
)
138+
ret = dockerfile.parse_string(test_string)
139+
assert ret == (
140+
dockerfile.Command(
141+
cmd='COPY', sub_cmd=None, json=False, flags=(),
142+
value=(
143+
'<<FILE1',
144+
'<<FILE2',
145+
'/dest',
146+
'content 1\n'
147+
'FILE1\n',
148+
'content 2\n'
149+
'FILE2\n',
122150
),
123151
start_line=1, end_line=5, original=test_string,
124152
),

0 commit comments

Comments
 (0)