mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Merge pull request #32580 from dnephin/refactor-builder-parser-directive
Refactor Dockerfile.parser and directive
This commit is contained in:
commit
700b4807c3
13 changed files with 211 additions and 235 deletions
|
@ -62,7 +62,7 @@ type Builder struct {
|
|||
disableCommit bool
|
||||
cacheBusted bool
|
||||
buildArgs *buildArgs
|
||||
directive parser.Directive
|
||||
escapeToken rune
|
||||
|
||||
imageCache builder.ImageCache
|
||||
from builder.Image
|
||||
|
@ -122,14 +122,9 @@ func NewBuilder(clientCtx context.Context, config *types.ImageBuildOptions, back
|
|||
runConfig: new(container.Config),
|
||||
tmpContainers: map[string]struct{}{},
|
||||
buildArgs: newBuildArgs(config.BuildArgs),
|
||||
directive: parser.Directive{
|
||||
EscapeSeen: false,
|
||||
LookingForDirectives: true,
|
||||
},
|
||||
escapeToken: parser.DefaultEscapeToken,
|
||||
}
|
||||
b.imageContexts = &imageContexts{b: b}
|
||||
|
||||
parser.SetEscapeToken(parser.DefaultEscapeToken, &b.directive) // Assume the default token for escape
|
||||
return b, nil
|
||||
}
|
||||
|
||||
|
@ -195,9 +190,9 @@ func (b *Builder) build(stdout io.Writer, stderr io.Writer, out io.Writer) (stri
|
|||
return "", err
|
||||
}
|
||||
|
||||
addNodesForLabelOption(dockerfile, b.options.Labels)
|
||||
addNodesForLabelOption(dockerfile.AST, b.options.Labels)
|
||||
|
||||
if err := checkDispatchDockerfile(dockerfile); err != nil {
|
||||
if err := checkDispatchDockerfile(dockerfile.AST); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
|
@ -225,10 +220,13 @@ func (b *Builder) build(stdout io.Writer, stderr io.Writer, out io.Writer) (stri
|
|||
return b.image, nil
|
||||
}
|
||||
|
||||
func (b *Builder) dispatchDockerfileWithCancellation(dockerfile *parser.Node) (string, error) {
|
||||
total := len(dockerfile.Children)
|
||||
func (b *Builder) dispatchDockerfileWithCancellation(dockerfile *parser.Result) (string, error) {
|
||||
// TODO: pass this to dispatchRequest instead
|
||||
b.escapeToken = dockerfile.EscapeToken
|
||||
|
||||
total := len(dockerfile.AST.Children)
|
||||
var shortImgID string
|
||||
for i, n := range dockerfile.Children {
|
||||
for i, n := range dockerfile.AST.Children {
|
||||
select {
|
||||
case <-b.clientCtx.Done():
|
||||
logrus.Debug("Builder: build cancelled!")
|
||||
|
@ -325,13 +323,13 @@ func BuildFromConfig(config *container.Config, changes []string) (*container.Con
|
|||
return nil, err
|
||||
}
|
||||
|
||||
ast, err := parser.Parse(bytes.NewBufferString(strings.Join(changes, "\n")), &b.directive)
|
||||
result, err := parser.Parse(bytes.NewBufferString(strings.Join(changes, "\n")))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// ensure that the commands are valid
|
||||
for _, n := range ast.Children {
|
||||
for _, n := range result.AST.Children {
|
||||
if !validCommitCommands[n.Value] {
|
||||
return nil, fmt.Errorf("%s is not a valid change command", n.Value)
|
||||
}
|
||||
|
@ -342,11 +340,11 @@ func BuildFromConfig(config *container.Config, changes []string) (*container.Con
|
|||
b.Stderr = ioutil.Discard
|
||||
b.disableCommit = true
|
||||
|
||||
if err := checkDispatchDockerfile(ast); err != nil {
|
||||
if err := checkDispatchDockerfile(result.AST); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := dispatchFromDockerfile(b, ast); err != nil {
|
||||
if err := dispatchFromDockerfile(b, result); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return b.runConfig, nil
|
||||
|
@ -361,8 +359,12 @@ func checkDispatchDockerfile(dockerfile *parser.Node) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func dispatchFromDockerfile(b *Builder, ast *parser.Node) error {
|
||||
func dispatchFromDockerfile(b *Builder, result *parser.Result) error {
|
||||
// TODO: pass this to dispatchRequest instead
|
||||
b.escapeToken = result.EscapeToken
|
||||
ast := result.AST
|
||||
total := len(ast.Children)
|
||||
|
||||
for i, n := range ast.Children {
|
||||
if err := b.dispatch(i, total, n); err != nil {
|
||||
return err
|
||||
|
|
|
@ -10,9 +10,7 @@ import (
|
|||
|
||||
func TestAddNodesForLabelOption(t *testing.T) {
|
||||
dockerfile := "FROM scratch"
|
||||
d := parser.Directive{}
|
||||
parser.SetEscapeToken(parser.DefaultEscapeToken, &d)
|
||||
nodes, err := parser.Parse(strings.NewReader(dockerfile), &d)
|
||||
result, err := parser.Parse(strings.NewReader(dockerfile))
|
||||
assert.NilError(t, err)
|
||||
|
||||
labels := map[string]string{
|
||||
|
@ -22,6 +20,7 @@ func TestAddNodesForLabelOption(t *testing.T) {
|
|||
"org.b": "cli-b",
|
||||
"org.a": "cli-a",
|
||||
}
|
||||
nodes := result.AST
|
||||
addNodesForLabelOption(nodes, labels)
|
||||
|
||||
expected := []string{
|
||||
|
|
|
@ -200,7 +200,7 @@ func from(b *Builder, args []string, attributes map[string]bool, original string
|
|||
substituionArgs = append(substituionArgs, key+"="+value)
|
||||
}
|
||||
|
||||
name, err := ProcessWord(args[0], substituionArgs, b.directive.EscapeToken)
|
||||
name, err := ProcessWord(args[0], substituionArgs, b.escapeToken)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -180,7 +180,7 @@ func (b *Builder) evaluateEnv(cmd string, str string, envs []string) ([]string,
|
|||
return []string{word}, err
|
||||
}
|
||||
}
|
||||
return processFunc(str, envs, b.directive.EscapeToken)
|
||||
return processFunc(str, envs, b.escapeToken)
|
||||
}
|
||||
|
||||
// buildArgsWithoutConfigEnv returns a list of key=value pairs for all the build
|
||||
|
|
|
@ -171,9 +171,7 @@ func executeTestCase(t *testing.T, testCase dispatchTestCase) {
|
|||
}()
|
||||
|
||||
r := strings.NewReader(testCase.dockerfile)
|
||||
d := parser.Directive{}
|
||||
parser.SetEscapeToken(parser.DefaultEscapeToken, &d)
|
||||
n, err := parser.Parse(r, &d)
|
||||
result, err := parser.Parse(r)
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("Error when parsing Dockerfile: %s", err)
|
||||
|
@ -192,6 +190,7 @@ func executeTestCase(t *testing.T, testCase dispatchTestCase) {
|
|||
buildArgs: newBuildArgs(options.BuildArgs),
|
||||
}
|
||||
|
||||
n := result.AST
|
||||
err = b.dispatch(0, len(n.Children), n.Children[0])
|
||||
|
||||
if err == nil {
|
||||
|
|
|
@ -462,12 +462,12 @@ func (b *Builder) processImageFrom(img builder.Image) error {
|
|||
|
||||
// parse the ONBUILD triggers by invoking the parser
|
||||
for _, step := range onBuildTriggers {
|
||||
ast, err := parser.Parse(strings.NewReader(step), &b.directive)
|
||||
result, err := parser.Parse(strings.NewReader(step))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, n := range ast.Children {
|
||||
for _, n := range result.AST.Children {
|
||||
if err := checkDispatch(n); err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -481,7 +481,7 @@ func (b *Builder) processImageFrom(img builder.Image) error {
|
|||
}
|
||||
}
|
||||
|
||||
if err := dispatchFromDockerfile(b, ast); err != nil {
|
||||
if err := dispatchFromDockerfile(b, result); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
@ -650,7 +650,7 @@ func (b *Builder) clearTmp() {
|
|||
}
|
||||
|
||||
// readAndParseDockerfile reads a Dockerfile from the current context.
|
||||
func (b *Builder) readAndParseDockerfile() (*parser.Node, error) {
|
||||
func (b *Builder) readAndParseDockerfile() (*parser.Result, error) {
|
||||
// If no -f was specified then look for 'Dockerfile'. If we can't find
|
||||
// that then look for 'dockerfile'. If neither are found then default
|
||||
// back to 'Dockerfile' and use that in the error message.
|
||||
|
@ -664,9 +664,9 @@ func (b *Builder) readAndParseDockerfile() (*parser.Node, error) {
|
|||
}
|
||||
}
|
||||
|
||||
nodes, err := b.parseDockerfile()
|
||||
result, err := b.parseDockerfile()
|
||||
if err != nil {
|
||||
return nodes, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// After the Dockerfile has been parsed, we need to check the .dockerignore
|
||||
|
@ -680,10 +680,10 @@ func (b *Builder) readAndParseDockerfile() (*parser.Node, error) {
|
|||
if dockerIgnore, ok := b.context.(builder.DockerIgnoreContext); ok {
|
||||
dockerIgnore.Process([]string{b.options.Dockerfile})
|
||||
}
|
||||
return nodes, nil
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (b *Builder) parseDockerfile() (*parser.Node, error) {
|
||||
func (b *Builder) parseDockerfile() (*parser.Result, error) {
|
||||
f, err := b.context.Open(b.options.Dockerfile)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
|
@ -702,5 +702,5 @@ func (b *Builder) parseDockerfile() (*parser.Node, error) {
|
|||
return nil, fmt.Errorf("The Dockerfile (%s) cannot be empty", b.options.Dockerfile)
|
||||
}
|
||||
}
|
||||
return parser.Parse(f, &b.directive)
|
||||
return parser.Parse(f)
|
||||
}
|
||||
|
|
|
@ -23,14 +23,10 @@ func main() {
|
|||
}
|
||||
defer f.Close()
|
||||
|
||||
d := parser.Directive{LookingForDirectives: true}
|
||||
parser.SetEscapeToken(parser.DefaultEscapeToken, &d)
|
||||
|
||||
ast, err := parser.Parse(f, &d)
|
||||
result, err := parser.Parse(f)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
} else {
|
||||
fmt.Println(ast.Dump())
|
||||
}
|
||||
fmt.Println(result.AST.Dump())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -28,10 +28,9 @@ var validJSONArraysOfStrings = map[string][]string{
|
|||
|
||||
func TestJSONArraysOfStrings(t *testing.T) {
|
||||
for json, expected := range validJSONArraysOfStrings {
|
||||
d := Directive{}
|
||||
SetEscapeToken(DefaultEscapeToken, &d)
|
||||
d := NewDefaultDirective()
|
||||
|
||||
if node, _, err := parseJSON(json, &d); err != nil {
|
||||
if node, _, err := parseJSON(json, d); err != nil {
|
||||
t.Fatalf("%q should be a valid JSON array of strings, but wasn't! (err: %q)", json, err)
|
||||
} else {
|
||||
i := 0
|
||||
|
@ -51,10 +50,9 @@ func TestJSONArraysOfStrings(t *testing.T) {
|
|||
}
|
||||
}
|
||||
for _, json := range invalidJSONArraysOfStrings {
|
||||
d := Directive{}
|
||||
SetEscapeToken(DefaultEscapeToken, &d)
|
||||
d := NewDefaultDirective()
|
||||
|
||||
if _, _, err := parseJSON(json, &d); err != errDockerfileNotStringArray {
|
||||
if _, _, err := parseJSON(json, d); err != errDockerfileNotStringArray {
|
||||
t.Fatalf("%q should be an invalid JSON array of strings, but wasn't!", json)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -42,7 +42,7 @@ func parseSubCommand(rest string, d *Directive) (*Node, map[string]bool, error)
|
|||
return nil, nil, nil
|
||||
}
|
||||
|
||||
_, child, err := ParseLine(rest, d, false)
|
||||
child, err := newNodeFromLine(rest, d)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
@ -103,7 +103,7 @@ func parseWords(rest string, d *Directive) []string {
|
|||
blankOK = true
|
||||
phase = inQuote
|
||||
}
|
||||
if ch == d.EscapeToken {
|
||||
if ch == d.escapeToken {
|
||||
if pos+chWidth == len(rest) {
|
||||
continue // just skip an escape token at end of line
|
||||
}
|
||||
|
@ -122,7 +122,7 @@ func parseWords(rest string, d *Directive) []string {
|
|||
phase = inWord
|
||||
}
|
||||
// The escape token is special except for ' quotes - can't escape anything for '
|
||||
if ch == d.EscapeToken && quote != '\'' {
|
||||
if ch == d.escapeToken && quote != '\'' {
|
||||
if pos+chWidth == len(rest) {
|
||||
phase = inWord
|
||||
continue // just skip the escape token at end
|
||||
|
|
|
@ -12,6 +12,7 @@ import (
|
|||
"unicode"
|
||||
|
||||
"github.com/docker/docker/builder/dockerfile/command"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// Node is a structure used to represent a parse tree.
|
||||
|
@ -34,7 +35,7 @@ type Node struct {
|
|||
Original string // original line used before parsing
|
||||
Flags []string // only top Node should have this set
|
||||
StartLine int // the line in the original dockerfile where the node begins
|
||||
EndLine int // the line in the original dockerfile where the node ends
|
||||
endLine int // the line in the original dockerfile where the node ends
|
||||
}
|
||||
|
||||
// Dump dumps the AST defined by `node` as a list of sexps.
|
||||
|
@ -62,13 +63,19 @@ func (node *Node) Dump() string {
|
|||
return strings.TrimSpace(str)
|
||||
}
|
||||
|
||||
// Directive is the structure used during a build run to hold the state of
|
||||
// parsing directives.
|
||||
type Directive struct {
|
||||
EscapeToken rune // Current escape token
|
||||
LineContinuationRegex *regexp.Regexp // Current line continuation regex
|
||||
LookingForDirectives bool // Whether we are currently looking for directives
|
||||
EscapeSeen bool // Whether the escape directive has been seen
|
||||
func (node *Node) lines(start, end int) {
|
||||
node.StartLine = start
|
||||
node.endLine = end
|
||||
}
|
||||
|
||||
// AddChild adds a new child node, and updates line information
|
||||
func (node *Node) AddChild(child *Node, startLine, endLine int) {
|
||||
child.lines(startLine, endLine)
|
||||
if node.StartLine < 0 {
|
||||
node.StartLine = startLine
|
||||
}
|
||||
node.endLine = endLine
|
||||
node.Children = append(node.Children, child)
|
||||
}
|
||||
|
||||
var (
|
||||
|
@ -79,18 +86,60 @@ var (
|
|||
)
|
||||
|
||||
// DefaultEscapeToken is the default escape token
|
||||
const DefaultEscapeToken = "\\"
|
||||
const DefaultEscapeToken = '\\'
|
||||
|
||||
// SetEscapeToken sets the default token for escaping characters in a Dockerfile.
|
||||
func SetEscapeToken(s string, d *Directive) error {
|
||||
// Directive is the structure used during a build run to hold the state of
|
||||
// parsing directives.
|
||||
type Directive struct {
|
||||
escapeToken rune // Current escape token
|
||||
lineContinuationRegex *regexp.Regexp // Current line continuation regex
|
||||
processingComplete bool // Whether we are done looking for directives
|
||||
escapeSeen bool // Whether the escape directive has been seen
|
||||
}
|
||||
|
||||
// setEscapeToken sets the default token for escaping characters in a Dockerfile.
|
||||
func (d *Directive) setEscapeToken(s string) error {
|
||||
if s != "`" && s != "\\" {
|
||||
return fmt.Errorf("invalid ESCAPE '%s'. Must be ` or \\", s)
|
||||
}
|
||||
d.EscapeToken = rune(s[0])
|
||||
d.LineContinuationRegex = regexp.MustCompile(`\` + s + `[ \t]*$`)
|
||||
d.escapeToken = rune(s[0])
|
||||
d.lineContinuationRegex = regexp.MustCompile(`\` + s + `[ \t]*$`)
|
||||
return nil
|
||||
}
|
||||
|
||||
// processLine looks for a parser directive '# escapeToken=<char>. Parser
|
||||
// directives must precede any builder instruction or other comments, and cannot
|
||||
// be repeated.
|
||||
func (d *Directive) processLine(line string) error {
|
||||
if d.processingComplete {
|
||||
return nil
|
||||
}
|
||||
// Processing is finished after the first call
|
||||
defer func() { d.processingComplete = true }()
|
||||
|
||||
tecMatch := tokenEscapeCommand.FindStringSubmatch(strings.ToLower(line))
|
||||
if len(tecMatch) == 0 {
|
||||
return nil
|
||||
}
|
||||
if d.escapeSeen == true {
|
||||
return errors.New("only one escape parser directive can be used")
|
||||
}
|
||||
for i, n := range tokenEscapeCommand.SubexpNames() {
|
||||
if n == "escapechar" {
|
||||
d.escapeSeen = true
|
||||
return d.setEscapeToken(tecMatch[i])
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// NewDefaultDirective returns a new Directive with the default escapeToken token
|
||||
func NewDefaultDirective() *Directive {
|
||||
directive := Directive{}
|
||||
directive.setEscapeToken(string(DefaultEscapeToken))
|
||||
return &directive
|
||||
}
|
||||
|
||||
func init() {
|
||||
// Dispatch Table. see line_parsers.go for the parse functions.
|
||||
// The command is parsed and mapped to the line parser. The line parser
|
||||
|
@ -120,28 +169,6 @@ func init() {
|
|||
}
|
||||
}
|
||||
|
||||
// ParseLine parses a line and returns the remainder.
|
||||
func ParseLine(line string, d *Directive, ignoreCont bool) (string, *Node, error) {
|
||||
if escapeFound, err := handleParserDirective(line, d); err != nil || escapeFound {
|
||||
d.EscapeSeen = escapeFound
|
||||
return "", nil, err
|
||||
}
|
||||
|
||||
d.LookingForDirectives = false
|
||||
|
||||
if line = stripComments(line); line == "" {
|
||||
return "", nil, nil
|
||||
}
|
||||
|
||||
if !ignoreCont && d.LineContinuationRegex.MatchString(line) {
|
||||
line = d.LineContinuationRegex.ReplaceAllString(line, "")
|
||||
return line, nil, nil
|
||||
}
|
||||
|
||||
node, err := newNodeFromLine(line, d)
|
||||
return "", node, err
|
||||
}
|
||||
|
||||
// newNodeFromLine splits the line into parts, and dispatches to a function
|
||||
// based on the command and command arguments. A Node is created from the
|
||||
// result of the dispatch.
|
||||
|
@ -170,109 +197,98 @@ func newNodeFromLine(line string, directive *Directive) (*Node, error) {
|
|||
}, nil
|
||||
}
|
||||
|
||||
// Handle the parser directive '# escape=<char>. Parser directives must precede
|
||||
// any builder instruction or other comments, and cannot be repeated.
|
||||
func handleParserDirective(line string, d *Directive) (bool, error) {
|
||||
if !d.LookingForDirectives {
|
||||
return false, nil
|
||||
}
|
||||
tecMatch := tokenEscapeCommand.FindStringSubmatch(strings.ToLower(line))
|
||||
if len(tecMatch) == 0 {
|
||||
return false, nil
|
||||
}
|
||||
if d.EscapeSeen == true {
|
||||
return false, fmt.Errorf("only one escape parser directive can be used")
|
||||
}
|
||||
for i, n := range tokenEscapeCommand.SubexpNames() {
|
||||
if n == "escapechar" {
|
||||
if err := SetEscapeToken(tecMatch[i], d); err != nil {
|
||||
return false, err
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
return false, nil
|
||||
// Result is the result of parsing a Dockerfile
|
||||
type Result struct {
|
||||
AST *Node
|
||||
EscapeToken rune
|
||||
}
|
||||
|
||||
// Parse is the main parse routine.
|
||||
// It handles an io.ReadWriteCloser and returns the root of the AST.
|
||||
func Parse(rwc io.Reader, d *Directive) (*Node, error) {
|
||||
// Parse reads lines from a Reader, parses the lines into an AST and returns
|
||||
// the AST and escape token
|
||||
func Parse(rwc io.Reader) (*Result, error) {
|
||||
d := NewDefaultDirective()
|
||||
currentLine := 0
|
||||
root := &Node{}
|
||||
root.StartLine = -1
|
||||
root := &Node{StartLine: -1}
|
||||
scanner := bufio.NewScanner(rwc)
|
||||
|
||||
utf8bom := []byte{0xEF, 0xBB, 0xBF}
|
||||
var err error
|
||||
for scanner.Scan() {
|
||||
scannedBytes := scanner.Bytes()
|
||||
// We trim UTF8 BOM
|
||||
if currentLine == 0 {
|
||||
scannedBytes = bytes.TrimPrefix(scannedBytes, utf8bom)
|
||||
bytes := scanner.Bytes()
|
||||
switch currentLine {
|
||||
case 0:
|
||||
bytes, err = processFirstLine(d, bytes)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
default:
|
||||
bytes = processLine(bytes, true)
|
||||
}
|
||||
scannedLine := strings.TrimLeftFunc(string(scannedBytes), unicode.IsSpace)
|
||||
currentLine++
|
||||
line, child, err := ParseLine(scannedLine, d, false)
|
||||
|
||||
startLine := currentLine
|
||||
line, isEndOfLine := trimContinuationCharacter(string(bytes), d)
|
||||
if isEndOfLine && line == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
for !isEndOfLine && scanner.Scan() {
|
||||
bytes := processLine(scanner.Bytes(), false)
|
||||
currentLine++
|
||||
|
||||
// TODO: warn this is being deprecated/removed
|
||||
if isEmptyContinuationLine(bytes) {
|
||||
continue
|
||||
}
|
||||
|
||||
continuationLine := string(bytes)
|
||||
continuationLine, isEndOfLine = trimContinuationCharacter(continuationLine, d)
|
||||
line += continuationLine
|
||||
}
|
||||
|
||||
child, err := newNodeFromLine(line, d)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
startLine := currentLine
|
||||
|
||||
if line != "" && child == nil {
|
||||
for scanner.Scan() {
|
||||
newline := scanner.Text()
|
||||
currentLine++
|
||||
|
||||
if stripComments(strings.TrimSpace(newline)) == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
line, child, err = ParseLine(line+newline, d, false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if child != nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
if child == nil && line != "" {
|
||||
// When we call ParseLine we'll pass in 'true' for
|
||||
// the ignoreCont param if we're at the EOF. This will
|
||||
// prevent the func from returning immediately w/o
|
||||
// parsing the line thinking that there's more input
|
||||
// to come.
|
||||
|
||||
_, child, err = ParseLine(line, d, scanner.Err() == nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if child != nil {
|
||||
// Update the line information for the current child.
|
||||
child.StartLine = startLine
|
||||
child.EndLine = currentLine
|
||||
// Update the line information for the root. The starting line of the root is always the
|
||||
// starting line of the first child and the ending line is the ending line of the last child.
|
||||
if root.StartLine < 0 {
|
||||
root.StartLine = currentLine
|
||||
}
|
||||
root.EndLine = currentLine
|
||||
root.Children = append(root.Children, child)
|
||||
}
|
||||
root.AddChild(child, startLine, currentLine)
|
||||
}
|
||||
|
||||
return root, nil
|
||||
return &Result{AST: root, EscapeToken: d.escapeToken}, nil
|
||||
}
|
||||
|
||||
// covers comments and empty lines. Lines should be trimmed before passing to
|
||||
// this function.
|
||||
func stripComments(line string) string {
|
||||
// string is already trimmed at this point
|
||||
if tokenComment.MatchString(line) {
|
||||
return tokenComment.ReplaceAllString(line, "")
|
||||
func trimComments(src []byte) []byte {
|
||||
return tokenComment.ReplaceAll(src, []byte{})
|
||||
}
|
||||
|
||||
func trimWhitespace(src []byte) []byte {
|
||||
return bytes.TrimLeftFunc(src, unicode.IsSpace)
|
||||
}
|
||||
|
||||
func isEmptyContinuationLine(line []byte) bool {
|
||||
return len(trimComments(trimWhitespace(line))) == 0
|
||||
}
|
||||
|
||||
var utf8bom = []byte{0xEF, 0xBB, 0xBF}
|
||||
|
||||
func trimContinuationCharacter(line string, d *Directive) (string, bool) {
|
||||
if d.lineContinuationRegex.MatchString(line) {
|
||||
line = d.lineContinuationRegex.ReplaceAllString(line, "")
|
||||
return line, false
|
||||
}
|
||||
|
||||
return line
|
||||
return line, true
|
||||
}
|
||||
|
||||
// TODO: remove stripLeftWhitespace after deprecation period. It seems silly
|
||||
// to preserve whitespace on continuation lines. Why is that done?
|
||||
func processLine(token []byte, stripLeftWhitespace bool) []byte {
|
||||
if stripLeftWhitespace {
|
||||
token = trimWhitespace(token)
|
||||
}
|
||||
return trimComments(token)
|
||||
}
|
||||
|
||||
func processFirstLine(d *Directive, token []byte) ([]byte, error) {
|
||||
token = bytes.TrimPrefix(token, utf8bom)
|
||||
token = trimWhitespace(token)
|
||||
err := d.processLine(string(token))
|
||||
return trimComments(token), err
|
||||
}
|
||||
|
|
|
@ -8,6 +8,8 @@ import (
|
|||
"path/filepath"
|
||||
"runtime"
|
||||
"testing"
|
||||
|
||||
"github.com/docker/docker/pkg/testutil/assert"
|
||||
)
|
||||
|
||||
const testDir = "testfiles"
|
||||
|
@ -16,17 +18,11 @@ const testFileLineInfo = "testfile-line/Dockerfile"
|
|||
|
||||
func getDirs(t *testing.T, dir string) []string {
|
||||
f, err := os.Open(dir)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
assert.NilError(t, err)
|
||||
defer f.Close()
|
||||
|
||||
dirs, err := f.Readdirnames(0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
assert.NilError(t, err)
|
||||
return dirs
|
||||
}
|
||||
|
||||
|
@ -35,17 +31,11 @@ func TestTestNegative(t *testing.T) {
|
|||
dockerfile := filepath.Join(negativeTestDir, dir, "Dockerfile")
|
||||
|
||||
df, err := os.Open(dockerfile)
|
||||
if err != nil {
|
||||
t.Fatalf("Dockerfile missing for %s: %v", dir, err)
|
||||
}
|
||||
assert.NilError(t, err)
|
||||
defer df.Close()
|
||||
|
||||
d := Directive{LookingForDirectives: true}
|
||||
SetEscapeToken(DefaultEscapeToken, &d)
|
||||
_, err = Parse(df, &d)
|
||||
if err == nil {
|
||||
t.Fatalf("No error parsing broken dockerfile for %s", dir)
|
||||
}
|
||||
_, err = Parse(df)
|
||||
assert.Error(t, err, "")
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -55,33 +45,21 @@ func TestTestData(t *testing.T) {
|
|||
resultfile := filepath.Join(testDir, dir, "result")
|
||||
|
||||
df, err := os.Open(dockerfile)
|
||||
if err != nil {
|
||||
t.Fatalf("Dockerfile missing for %s: %v", dir, err)
|
||||
}
|
||||
assert.NilError(t, err)
|
||||
defer df.Close()
|
||||
|
||||
d := Directive{LookingForDirectives: true}
|
||||
SetEscapeToken(DefaultEscapeToken, &d)
|
||||
ast, err := Parse(df, &d)
|
||||
if err != nil {
|
||||
t.Fatalf("Error parsing %s's dockerfile: %v", dir, err)
|
||||
}
|
||||
result, err := Parse(df)
|
||||
assert.NilError(t, err)
|
||||
|
||||
content, err := ioutil.ReadFile(resultfile)
|
||||
if err != nil {
|
||||
t.Fatalf("Error reading %s's result file: %v", dir, err)
|
||||
}
|
||||
assert.NilError(t, err)
|
||||
|
||||
if runtime.GOOS == "windows" {
|
||||
// CRLF --> CR to match Unix behavior
|
||||
content = bytes.Replace(content, []byte{'\x0d', '\x0a'}, []byte{'\x0a'}, -1)
|
||||
}
|
||||
|
||||
if ast.Dump()+"\n" != string(content) {
|
||||
fmt.Fprintln(os.Stderr, "Result:\n"+ast.Dump())
|
||||
fmt.Fprintln(os.Stderr, "Expected:\n"+string(content))
|
||||
t.Fatalf("%s: AST dump of dockerfile does not match result", dir)
|
||||
}
|
||||
assert.Equal(t, result.AST.Dump()+"\n", string(content), "In "+dockerfile)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -122,51 +100,34 @@ func TestParseWords(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, test := range tests {
|
||||
d := Directive{LookingForDirectives: true}
|
||||
SetEscapeToken(DefaultEscapeToken, &d)
|
||||
words := parseWords(test["input"][0], &d)
|
||||
if len(words) != len(test["expect"]) {
|
||||
t.Fatalf("length check failed. input: %v, expect: %q, output: %q", test["input"][0], test["expect"], words)
|
||||
}
|
||||
for i, word := range words {
|
||||
if word != test["expect"][i] {
|
||||
t.Fatalf("word check failed for word: %q. input: %q, expect: %q, output: %q", word, test["input"][0], test["expect"], words)
|
||||
}
|
||||
}
|
||||
words := parseWords(test["input"][0], NewDefaultDirective())
|
||||
assert.DeepEqual(t, words, test["expect"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestLineInformation(t *testing.T) {
|
||||
df, err := os.Open(testFileLineInfo)
|
||||
if err != nil {
|
||||
t.Fatalf("Dockerfile missing for %s: %v", testFileLineInfo, err)
|
||||
}
|
||||
assert.NilError(t, err)
|
||||
defer df.Close()
|
||||
|
||||
d := Directive{LookingForDirectives: true}
|
||||
SetEscapeToken(DefaultEscapeToken, &d)
|
||||
ast, err := Parse(df, &d)
|
||||
if err != nil {
|
||||
t.Fatalf("Error parsing dockerfile %s: %v", testFileLineInfo, err)
|
||||
}
|
||||
result, err := Parse(df)
|
||||
assert.NilError(t, err)
|
||||
|
||||
if ast.StartLine != 5 || ast.EndLine != 31 {
|
||||
fmt.Fprintf(os.Stderr, "Wrong root line information: expected(%d-%d), actual(%d-%d)\n", 5, 31, ast.StartLine, ast.EndLine)
|
||||
ast := result.AST
|
||||
if ast.StartLine != 5 || ast.endLine != 31 {
|
||||
fmt.Fprintf(os.Stderr, "Wrong root line information: expected(%d-%d), actual(%d-%d)\n", 5, 31, ast.StartLine, ast.endLine)
|
||||
t.Fatal("Root line information doesn't match result.")
|
||||
}
|
||||
if len(ast.Children) != 3 {
|
||||
fmt.Fprintf(os.Stderr, "Wrong number of child: expected(%d), actual(%d)\n", 3, len(ast.Children))
|
||||
t.Fatalf("Root line information doesn't match result for %s", testFileLineInfo)
|
||||
}
|
||||
assert.Equal(t, len(ast.Children), 3)
|
||||
expected := [][]int{
|
||||
{5, 5},
|
||||
{11, 12},
|
||||
{17, 31},
|
||||
}
|
||||
for i, child := range ast.Children {
|
||||
if child.StartLine != expected[i][0] || child.EndLine != expected[i][1] {
|
||||
if child.StartLine != expected[i][0] || child.endLine != expected[i][1] {
|
||||
t.Logf("Wrong line information for child %d: expected(%d-%d), actual(%d-%d)\n",
|
||||
i, expected[i][0], expected[i][1], child.StartLine, child.EndLine)
|
||||
i, expected[i][0], expected[i][1], child.StartLine, child.endLine)
|
||||
t.Fatal("Root line information doesn't match result.")
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
FROM alpine:3.5
|
||||
|
||||
RUN something \
|
|
@ -0,0 +1,2 @@
|
|||
(from "alpine:3.5")
|
||||
(run "something")
|
Loading…
Add table
Reference in a new issue