Skip to content

Commit

Permalink
Update Dockerfile handling to keep parser directives at the top
Browse files Browse the repository at this point in the history
Signed-off-by: CrazyMax <google@crazymax.dev>
  • Loading branch information
crazy-max authored and willnorris committed Aug 9, 2021
1 parent 5be3bee commit 99ebc9c
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 0 deletions.
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,8 @@ var head = []string{
"# encoding:", // Ruby encoding
"# frozen_string_literal:", // Ruby interpreter instruction
"<?php", // PHP opening tag
"# escape", // Dockerfile directive https://docs.docker.com/engine/reference/builder/#parser-directives
"# syntax", // Dockerfile directive https://docs.docker.com/engine/reference/builder/#parser-directives
}

func hashBang(b []byte) []byte {
Expand Down
2 changes: 2 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,8 @@ func TestAddLicense(t *testing.T) {
{"# encoding: UTF-8\ncontent", "# encoding: UTF-8\n// HYS\n\ncontent", true},
{"# frozen_string_literal: true\ncontent", "# frozen_string_literal: true\n// HYS\n\ncontent", true},
{"<?php\ncontent", "<?php\n// HYS\n\ncontent", true},
{"# escape: `\ncontent", "# escape: `\n// HYS\n\ncontent", true},
{"# syntax: docker/dockerfile:1.3\ncontent", "# syntax: docker/dockerfile:1.3\n// HYS\n\ncontent", true},

// ensure files with existing license or generated files are
// skipped. No need to test all permutations of these, since
Expand Down
File renamed without changes.
17 changes: 17 additions & 0 deletions testdata/expected/file2.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# syntax=docker/dockerfile:1.3
# Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

FROM scratch
CMD ["echo", "hello world"]
18 changes: 18 additions & 0 deletions testdata/expected/file3.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# escape=`
# Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

FROM microsoft/nanoserver
COPY testfile.txt c:\
RUN dir c:\
File renamed without changes.
3 changes: 3 additions & 0 deletions testdata/initial/file2.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# syntax=docker/dockerfile:1.3
FROM scratch
CMD ["echo", "hello world"]
4 changes: 4 additions & 0 deletions testdata/initial/file3.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# escape=`
FROM microsoft/nanoserver
COPY testfile.txt c:\
RUN dir c:\

0 comments on commit 99ebc9c

Please sign in to comment.