Browse code

fmt dockerfile error message

Signed-off-by: sakeven <jc5930@sina.cn>

sakeven authored on 2016/08/26 18:06:07
Showing 3 changed files
... ...
@@ -68,7 +68,7 @@ func env(b *Builder, args []string, attributes map[string]bool, original string)
68 68
 		// value ==> args[j+1]
69 69
 
70 70
 		if len(args[j]) == 0 {
71
-			return fmt.Errorf("ENV names can not be blank")
71
+			return errBlankCommandNames("ENV")
72 72
 		}
73 73
 
74 74
 		newVar := args[j] + "=" + args[j+1] + ""
... ...
@@ -136,7 +136,7 @@ func label(b *Builder, args []string, attributes map[string]bool, original strin
136 136
 		// value ==> args[j+1]
137 137
 
138 138
 		if len(args[j]) == 0 {
139
-			return fmt.Errorf("LABEL names can not be blank")
139
+			return errBlankCommandNames("LABEL")
140 140
 		}
141 141
 
142 142
 		newVar := args[j] + "=" + args[j+1] + ""
... ...
@@ -455,7 +455,7 @@ func parseOptInterval(f *Flag) (time.Duration, error) {
455 455
 //
456 456
 func healthcheck(b *Builder, args []string, attributes map[string]bool, original string) error {
457 457
 	if len(args) == 0 {
458
-		return fmt.Errorf("HEALTHCHECK requires an argument")
458
+		return errAtLeastOneArgument("HEALTHCHECK")
459 459
 	}
460 460
 	typ := strings.ToUpper(args[0])
461 461
 	args = args[1:]
... ...
@@ -529,11 +529,7 @@ func healthcheck(b *Builder, args []string, attributes map[string]bool, original
529 529
 		b.runConfig.Healthcheck = &healthcheck
530 530
 	}
531 531
 
532
-	if err := b.commit("", b.runConfig.Cmd, fmt.Sprintf("HEALTHCHECK %q", b.runConfig.Healthcheck)); err != nil {
533
-		return err
534
-	}
535
-
536
-	return nil
532
+	return b.commit("", b.runConfig.Cmd, fmt.Sprintf("HEALTHCHECK %q", b.runConfig.Healthcheck))
537 533
 }
538 534
 
539 535
 // ENTRYPOINT /usr/sbin/nginx
... ...
@@ -654,7 +650,7 @@ func volume(b *Builder, args []string, attributes map[string]bool, original stri
654 654
 	for _, v := range args {
655 655
 		v = strings.TrimSpace(v)
656 656
 		if v == "" {
657
-			return fmt.Errorf("Volume specified can not be an empty string")
657
+			return fmt.Errorf("VOLUME specified can not be an empty string")
658 658
 		}
659 659
 		b.runConfig.Volumes[v] = struct{}{}
660 660
 	}
... ...
@@ -669,7 +665,7 @@ func volume(b *Builder, args []string, attributes map[string]bool, original stri
669 669
 // Set the signal that will be used to kill the container.
670 670
 func stopSignal(b *Builder, args []string, attributes map[string]bool, original string) error {
671 671
 	if len(args) != 1 {
672
-		return fmt.Errorf("STOPSIGNAL requires exactly one argument")
672
+		return errExactlyOneArgument("STOPSIGNAL")
673 673
 	}
674 674
 
675 675
 	sig := args[0]
... ...
@@ -689,7 +685,7 @@ func stopSignal(b *Builder, args []string, attributes map[string]bool, original
689 689
 // Dockerfile author may optionally set a default value of this variable.
690 690
 func arg(b *Builder, args []string, attributes map[string]bool, original string) error {
691 691
 	if len(args) != 1 {
692
-		return fmt.Errorf("ARG requires exactly one argument definition")
692
+		return errExactlyOneArgument("ARG")
693 693
 	}
694 694
 
695 695
 	var (
... ...
@@ -707,7 +703,7 @@ func arg(b *Builder, args []string, attributes map[string]bool, original string)
707 707
 	if strings.Contains(arg, "=") {
708 708
 		parts := strings.SplitN(arg, "=", 2)
709 709
 		if len(parts[0]) == 0 {
710
-			return fmt.Errorf("ARG names can not be blank")
710
+			return errBlankCommandNames("ARG")
711 711
 		}
712 712
 
713 713
 		name = parts[0]
... ...
@@ -764,6 +760,10 @@ func errAtLeastTwoArguments(command string) error {
764 764
 	return fmt.Errorf("%s requires at least two arguments", command)
765 765
 }
766 766
 
767
+func errBlankCommandNames(command string) error {
768
+	return fmt.Errorf("%s names can not be blank", command)
769
+}
770
+
767 771
 func errTooManyArguments(command string) error {
768 772
 	return fmt.Errorf("Bad input to %s, too many arguments", command)
769 773
 }
... ...
@@ -22,7 +22,8 @@ func TestCommandsExactlyOneArgument(t *testing.T) {
22 22
 		{"MAINTAINER", func(args []string) error { return maintainer(nil, args, nil, "") }},
23 23
 		{"FROM", func(args []string) error { return from(nil, args, nil, "") }},
24 24
 		{"WORKDIR", func(args []string) error { return workdir(nil, args, nil, "") }},
25
-		{"USER", func(args []string) error { return user(nil, args, nil, "") }}}
25
+		{"USER", func(args []string) error { return user(nil, args, nil, "") }},
26
+		{"STOPSIGNAL", func(args []string) error { return stopSignal(nil, args, nil, "") }}}
26 27
 
27 28
 	for _, command := range commands {
28 29
 		err := command.function([]string{})
... ...
@@ -31,9 +32,9 @@ func TestCommandsExactlyOneArgument(t *testing.T) {
31 31
 			t.Fatalf("Error should be present for %s command", command.name)
32 32
 		}
33 33
 
34
-		expectedError := fmt.Sprintf("%s requires exactly one argument", command.name)
34
+		expectedError := errExactlyOneArgument(command.name)
35 35
 
36
-		if err.Error() != expectedError {
36
+		if err.Error() != expectedError.Error() {
37 37
 			t.Fatalf("Wrong error message for %s. Got: %s. Should be: %s", command.name, err.Error(), expectedError)
38 38
 		}
39 39
 	}
... ...
@@ -44,6 +45,7 @@ func TestCommandsAtLeastOneArgument(t *testing.T) {
44 44
 		{"ENV", func(args []string) error { return env(nil, args, nil, "") }},
45 45
 		{"LABEL", func(args []string) error { return label(nil, args, nil, "") }},
46 46
 		{"ONBUILD", func(args []string) error { return onbuild(nil, args, nil, "") }},
47
+		{"HEALTHCHECK", func(args []string) error { return healthcheck(nil, args, nil, "") }},
47 48
 		{"EXPOSE", func(args []string) error { return expose(nil, args, nil, "") }},
48 49
 		{"VOLUME", func(args []string) error { return volume(nil, args, nil, "") }}}
49 50
 
... ...
@@ -54,9 +56,9 @@ func TestCommandsAtLeastOneArgument(t *testing.T) {
54 54
 			t.Fatalf("Error should be present for %s command", command.name)
55 55
 		}
56 56
 
57
-		expectedError := fmt.Sprintf("%s requires at least one argument", command.name)
57
+		expectedError := errAtLeastOneArgument(command.name)
58 58
 
59
-		if err.Error() != expectedError {
59
+		if err.Error() != expectedError.Error() {
60 60
 			t.Fatalf("Wrong error message for %s. Got: %s. Should be: %s", command.name, err.Error(), expectedError)
61 61
 		}
62 62
 	}
... ...
@@ -74,9 +76,9 @@ func TestCommandsAtLeastTwoArguments(t *testing.T) {
74 74
 			t.Fatalf("Error should be present for %s command", command.name)
75 75
 		}
76 76
 
77
-		expectedError := fmt.Sprintf("%s requires at least two arguments", command.name)
77
+		expectedError := errAtLeastTwoArguments(command.name)
78 78
 
79
-		if err.Error() != expectedError {
79
+		if err.Error() != expectedError.Error() {
80 80
 			t.Fatalf("Wrong error message for %s. Got: %s. Should be: %s", command.name, err.Error(), expectedError)
81 81
 		}
82 82
 	}
... ...
@@ -94,9 +96,35 @@ func TestCommandsTooManyArguments(t *testing.T) {
94 94
 			t.Fatalf("Error should be present for %s command", command.name)
95 95
 		}
96 96
 
97
-		expectedError := fmt.Sprintf("Bad input to %s, too many arguments", command.name)
97
+		expectedError := errTooManyArguments(command.name)
98 98
 
99
-		if err.Error() != expectedError {
99
+		if err.Error() != expectedError.Error() {
100
+			t.Fatalf("Wrong error message for %s. Got: %s. Should be: %s", command.name, err.Error(), expectedError)
101
+		}
102
+	}
103
+}
104
+
105
+func TestCommandseBlankNames(t *testing.T) {
106
+	bflags := &BFlags{}
107
+	config := &container.Config{}
108
+
109
+	b := &Builder{flags: bflags, runConfig: config, disableCommit: true}
110
+
111
+	commands := []commandWithFunction{
112
+		{"ENV", func(args []string) error { return env(b, args, nil, "") }},
113
+		{"LABEL", func(args []string) error { return label(b, args, nil, "") }},
114
+	}
115
+
116
+	for _, command := range commands {
117
+		err := command.function([]string{"", ""})
118
+
119
+		if err == nil {
120
+			t.Fatalf("Error should be present for %s command", command.name)
121
+		}
122
+
123
+		expectedError := errBlankCommandNames(command.name)
124
+
125
+		if err.Error() != expectedError.Error() {
100 126
 			t.Fatalf("Wrong error message for %s. Got: %s. Should be: %s", command.name, err.Error(), expectedError)
101 127
 		}
102 128
 	}
... ...
@@ -45,7 +45,7 @@ func initDispatchTestCases() []dispatchTestCase {
45 45
 		{
46 46
 			name:          "ARG two arguments",
47 47
 			dockerfile:    "ARG foo bar",
48
-			expectedError: "ARG requires exactly one argument definition",
48
+			expectedError: "ARG requires exactly one argument",
49 49
 			files:         nil,
50 50
 		},
51 51
 		{