github.com/tilt-dev/tilt@v0.33.15-0.20240515162809-0a22ed45d8a0/internal/dockerfile/ast_test.go (about)

     1  package dockerfile
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  )
     8  
     9  func TestPrintBasicAST(t *testing.T) {
    10  	assertPrintSame(t, `
    11  
    12  FROM golang:10
    13  RUN echo hi
    14  
    15  
    16  ADD . .
    17  
    18  RUN echo bye
    19  `)
    20  }
    21  
    22  func TestPrintHeredoc(t *testing.T) {
    23  	assertPrintSame(t, `
    24  FROM golang:10
    25  
    26  COPY <<FILE1 <<FILE2 /dest
    27  content 1
    28  FILE1
    29  content 2
    30  content 2 line 2
    31  FILE2
    32  
    33  RUN <<EOF
    34  echo hello
    35  EOF
    36  `)
    37  }
    38  
    39  func TestPrintASTRemovesComments(t *testing.T) {
    40  	assertPrint(t, `
    41  # comment
    42  FROM golang:10
    43  RUN echo bye
    44  `, `
    45  
    46  FROM golang:10
    47  RUN echo bye
    48  `)
    49  }
    50  
    51  func TestPrintCmd(t *testing.T) {
    52  	assertPrintSame(t, `
    53  FROM golang:10
    54  CMD echo bye
    55  `)
    56  }
    57  
    58  func TestPrintCmdJSON(t *testing.T) {
    59  	assertPrintSame(t, `
    60  FROM golang:10
    61  CMD ["sh", "-c", "echo bye"]
    62  `)
    63  }
    64  
    65  func TestPrintLabel(t *testing.T) {
    66  	// Examples taken from
    67  	// https://docs.docker.com/engine/reference/builder/#label
    68  	assertPrint(t, `
    69  LABEL key1=val1 key2=val2
    70  LABEL "com.example.vendor"="ACME Incorporated"
    71  LABEL com.example.label-with-value="foo"
    72  LABEL version="1.0"
    73  LABEL description="This text illustrates \
    74  that label-values can span multiple lines."
    75  `, `
    76  LABEL key1=val1 key2=val2
    77  LABEL "com.example.vendor"="ACME Incorporated"
    78  LABEL com.example.label-with-value="foo"
    79  LABEL version="1.0"
    80  LABEL description="This text illustrates that label-values can span multiple lines."
    81  `)
    82  }
    83  
    84  func TestPrintCopyFlags(t *testing.T) {
    85  	assertPrintSame(t, `
    86  FROM golang:10
    87  COPY --from=gcr.io/windmill/image-a /src /src
    88  RUN echo bye
    89  `)
    90  }
    91  
    92  func TestPrintCopyFlagsLabel(t *testing.T) {
    93  	assertPrintSame(t, `
    94  FROM golang:10
    95  COPY --from=gcr.io/windmill/image-a:latest /src /src
    96  RUN echo bye
    97  `)
    98  }
    99  
   100  func TestPrintSyntaxDirective(t *testing.T) {
   101  	assertPrintSame(t, `# syntax = foobarbaz
   102  
   103  FROM golang:10
   104  RUN echo hi
   105  RUN echo bye
   106  `)
   107  }
   108  
   109  func TestMultipleDirectivesOrderDeterministic(t *testing.T) {
   110  	orig := `# syntax = dockerfile:1
   111  # escape = \
   112  # unknown = foo
   113  
   114  FROM golang:10
   115  `
   116  	// known directives should be preserved
   117  	// unknown directives should be dropped
   118  	expected := `# syntax = dockerfile:1
   119  # escape = \
   120  
   121  
   122  FROM golang:10
   123  `
   124  
   125  	assertPrint(t, orig, expected)
   126  }
   127  
   128  // Convert the dockerfile into an AST, print it, and then
   129  // assert that the result is the same as the original.
   130  func assertPrintSame(t *testing.T, original string) {
   131  	assertPrint(t, original, original)
   132  }
   133  
   134  // Convert the dockerfile into an AST, print it, and then
   135  // assert that the result is as expected.
   136  func assertPrint(t *testing.T, original, expected string) {
   137  	df := Dockerfile(original)
   138  	ast, err := ParseAST(df)
   139  	if err != nil {
   140  		t.Fatal(err)
   141  	}
   142  
   143  	actual, err := ast.Print()
   144  	if err != nil {
   145  		t.Fatal(err)
   146  	}
   147  
   148  	assert.Equal(t, expected, string(actual))
   149  }