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

Commit e44779a

Browse files
Added support for properly parsing Value and Original for dockerfiles containing heredocs syntax, along with tests.
1 parent 323b8d2 commit e44779a

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

parse.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,16 @@ 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)
77+
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"
86+
}
7987
}
8088

8189
ret = append(ret, cmd)

parse_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,25 @@ func TestParseFile(t *testing.T) {
116116
}
117117
assert.Equal(t, expected, cmds)
118118
}
119+
120+
func TestParseReaderHeredocs(t *testing.T) {
121+
dockerfile := `RUN <<EOF
122+
source $HOME/.bashrc && echo $HOME
123+
echo "Hello" >> /hello
124+
echo "World!" >> /hello
125+
EOF
126+
`
127+
cmds, err := ParseReader(bytes.NewBufferString(dockerfile))
128+
assert.Nil(t, err)
129+
expected := []Command{
130+
Command{
131+
Cmd: "RUN",
132+
Original: dockerfile,
133+
StartLine: 1,
134+
EndLine: 5,
135+
Flags: []string{},
136+
Value: []string{"source $HOME/.bashrc && echo $HOME\necho \"Hello\" >> /hello\necho \"World!\" >> /hello\n"},
137+
},
138+
}
139+
assert.Equal(t, expected, cmds)
140+
}

tests/dockerfile_test.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,25 @@ def test_parse_file_success():
101101
start_line=2, end_line=2, original='CMD ["echo", "hi"]',
102102
),
103103
)
104+
105+
106+
def test_heredoc_string_success():
107+
test_string = (
108+
'RUN <<EOF\n'
109+
'source $HOME/.bashrc && echo $HOME\n'
110+
'echo "Hello" >> /hello\n'
111+
'echo "World!" >> /hello\n'
112+
'EOF\n'
113+
)
114+
ret = dockerfile.parse_string(test_string)
115+
assert ret == (
116+
dockerfile.Command(
117+
cmd='RUN', sub_cmd=None, json=False, flags=(),
118+
value=(
119+
'source $HOME/.bashrc && echo $HOME\n'
120+
'echo "Hello" >> /hello\n'
121+
'echo "World!" >> /hello\n',
122+
),
123+
start_line=1, end_line=5, original=test_string,
124+
),
125+
)

0 commit comments

Comments
 (0)