|
| 1 | +/* |
| 2 | + * © 2025 Snyk Limited |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package dockerfile |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/rs/zerolog" |
| 23 | + "github.com/stretchr/testify/assert" |
| 24 | + "github.com/stretchr/testify/require" |
| 25 | +) |
| 26 | + |
| 27 | +func TestParser_Parse_SingleFrom(t *testing.T) { |
| 28 | + logger := zerolog.Nop() |
| 29 | + parser := New(&logger) |
| 30 | + |
| 31 | + content := `FROM ubuntu:20.04 |
| 32 | +RUN apt-get update |
| 33 | +COPY . /app |
| 34 | +` |
| 35 | + tree := parser.Parse([]byte(content), "/test/Dockerfile") |
| 36 | + |
| 37 | + require.NotNil(t, tree) |
| 38 | + assert.Equal(t, "/test/Dockerfile", tree.Document) |
| 39 | + assert.Equal(t, "/test/Dockerfile", tree.Root.Name) |
| 40 | + |
| 41 | + // Should have one FROM node |
| 42 | + require.Len(t, tree.Root.Children, 1) |
| 43 | + fromNode := tree.Root.Children[0] |
| 44 | + assert.Equal(t, "FROM", fromNode.Name) |
| 45 | + assert.Equal(t, "ubuntu:20.04", fromNode.Value) |
| 46 | + assert.Equal(t, 0, fromNode.Line) |
| 47 | +} |
| 48 | + |
| 49 | +func TestParser_Parse_MultipleFrom(t *testing.T) { |
| 50 | + logger := zerolog.Nop() |
| 51 | + parser := New(&logger) |
| 52 | + |
| 53 | + content := `FROM node:14 as builder |
| 54 | +WORKDIR /app |
| 55 | +COPY package*.json ./ |
| 56 | +RUN npm install |
| 57 | +
|
| 58 | +FROM nginx:alpine |
| 59 | +COPY --from=builder /app/dist /usr/share/nginx/html |
| 60 | +` |
| 61 | + tree := parser.Parse([]byte(content), "/test/Dockerfile") |
| 62 | + |
| 63 | + require.NotNil(t, tree) |
| 64 | + |
| 65 | + // Should have two FROM nodes |
| 66 | + require.Len(t, tree.Root.Children, 2) |
| 67 | + |
| 68 | + firstFrom := tree.Root.Children[0] |
| 69 | + assert.Equal(t, "FROM", firstFrom.Name) |
| 70 | + assert.Equal(t, "node:14", firstFrom.Value) |
| 71 | + assert.Equal(t, 0, firstFrom.Line) |
| 72 | + |
| 73 | + secondFrom := tree.Root.Children[1] |
| 74 | + assert.Equal(t, "FROM", secondFrom.Name) |
| 75 | + assert.Equal(t, "nginx:alpine", secondFrom.Value) |
| 76 | + assert.Equal(t, 5, secondFrom.Line) |
| 77 | +} |
| 78 | + |
| 79 | +func TestParser_Parse_SkipScratch(t *testing.T) { |
| 80 | + logger := zerolog.Nop() |
| 81 | + parser := New(&logger) |
| 82 | + |
| 83 | + content := `FROM scratch |
| 84 | +COPY --from=builder /app /app |
| 85 | +` |
| 86 | + tree := parser.Parse([]byte(content), "/test/Dockerfile") |
| 87 | + |
| 88 | + require.NotNil(t, tree) |
| 89 | + |
| 90 | + // Should have no FROM nodes (scratch is skipped) |
| 91 | + assert.Len(t, tree.Root.Children, 0) |
| 92 | +} |
| 93 | + |
| 94 | +func TestParser_Parse_CaseInsensitive(t *testing.T) { |
| 95 | + logger := zerolog.Nop() |
| 96 | + parser := New(&logger) |
| 97 | + |
| 98 | + content := `from ubuntu:20.04 |
| 99 | +FROM alpine:3.14 |
| 100 | +FrOm golang:1.19 |
| 101 | +` |
| 102 | + tree := parser.Parse([]byte(content), "/test/Dockerfile") |
| 103 | + |
| 104 | + require.NotNil(t, tree) |
| 105 | + |
| 106 | + // Should have three FROM nodes |
| 107 | + require.Len(t, tree.Root.Children, 3) |
| 108 | + assert.Equal(t, "ubuntu:20.04", tree.Root.Children[0].Value) |
| 109 | + assert.Equal(t, "alpine:3.14", tree.Root.Children[1].Value) |
| 110 | + assert.Equal(t, "golang:1.19", tree.Root.Children[2].Value) |
| 111 | +} |
| 112 | + |
| 113 | +func TestParser_Parse_WithWindowsLineEndings(t *testing.T) { |
| 114 | + logger := zerolog.Nop() |
| 115 | + parser := New(&logger) |
| 116 | + |
| 117 | + content := "FROM ubuntu:20.04\r\nRUN apt-get update\r\n" |
| 118 | + tree := parser.Parse([]byte(content), "/test/Dockerfile") |
| 119 | + |
| 120 | + require.NotNil(t, tree) |
| 121 | + |
| 122 | + // Should have one FROM node |
| 123 | + require.Len(t, tree.Root.Children, 1) |
| 124 | + assert.Equal(t, "ubuntu:20.04", tree.Root.Children[0].Value) |
| 125 | +} |
0 commit comments