Browse code

Remove leading/trailing spaces in builder/parser

Per Erikh's suggestion at:
https://github.com/docker/docker/pull/9989#issuecomment-69832009
this PR will trim spaces in the parser to ensure that the user gets the same
results irrespetive of leading/trailing spaces.

Per @tiborvass's suggestion I added a test to make sure spaces in quotes
are not touched.

Signed-off-by: Doug Davis <dug@us.ibm.com>

Doug Davis authored on 2015/01/15 23:49:48
Showing 2 changed files
... ...
@@ -50,7 +50,8 @@ func fullDispatch(cmd, args string) (*Node, map[string]bool, error) {
50 50
 // splitCommand takes a single line of text and parses out the cmd and args,
51 51
 // which are used for dispatching to more exact parsing functions.
52 52
 func splitCommand(line string) (string, string, error) {
53
-	cmdline := TOKEN_WHITESPACE.Split(line, 2)
53
+	// Make sure we get the same results irrespective of leading/trailing spaces
54
+	cmdline := TOKEN_WHITESPACE.Split(strings.TrimSpace(line), 2)
54 55
 
55 56
 	if len(cmdline) != 2 {
56 57
 		return "", "", fmt.Errorf("We do not understand this file. Please ensure it is a valid Dockerfile. Parser error at %q", line)
... ...
@@ -4694,3 +4694,93 @@ func TestBuildDockerfileOutsideContext(t *testing.T) {
4694 4694
 
4695 4695
 	logDone("build - Dockerfile outside context")
4696 4696
 }
4697
+
4698
+func TestBuildSpaces(t *testing.T) {
4699
+	// Test to make sure that leading/trailing spaces on a command
4700
+	// doesn't change the error msg we get
4701
+	var (
4702
+		err1 error
4703
+		err2 error
4704
+	)
4705
+
4706
+	name := "testspaces"
4707
+	defer deleteImages(name)
4708
+	ctx, err := fakeContext("FROM busybox\nRUN\n",
4709
+		map[string]string{
4710
+			"Dockerfile": "FROM busybox\nRUN\n",
4711
+		})
4712
+	if err != nil {
4713
+		t.Fatal(err)
4714
+	}
4715
+	defer ctx.Close()
4716
+
4717
+	if _, err1 = buildImageFromContext(name, ctx, false); err1 == nil {
4718
+		t.Fatal("Build 1 was supposed to fail, but didn't")
4719
+	}
4720
+
4721
+	ctx.Add("Dockerfile", "FROM busybox\nRUN    ")
4722
+	if _, err2 = buildImageFromContext(name, ctx, false); err2 == nil {
4723
+		t.Fatal("Build 2 was supposed to fail, but didn't")
4724
+	}
4725
+
4726
+	// Skip over the times
4727
+	e1 := err1.Error()[strings.Index(err1.Error(), `level="`):]
4728
+	e2 := err2.Error()[strings.Index(err1.Error(), `level="`):]
4729
+
4730
+	// Ignore whitespace since that's what were verifying doesn't change stuff
4731
+	if strings.Replace(e1, " ", "", -1) != strings.Replace(e2, " ", "", -1) {
4732
+		t.Fatalf("Build 2's error wasn't the same as build 1's\n1:%s\n2:%s", err1, err2)
4733
+	}
4734
+
4735
+	ctx.Add("Dockerfile", "FROM busybox\n   RUN")
4736
+	if _, err2 = buildImageFromContext(name, ctx, false); err2 == nil {
4737
+		t.Fatal("Build 3 was supposed to fail, but didn't")
4738
+	}
4739
+
4740
+	// Skip over the times
4741
+	e1 = err1.Error()[strings.Index(err1.Error(), `level="`):]
4742
+	e2 = err2.Error()[strings.Index(err1.Error(), `level="`):]
4743
+
4744
+	// Ignore whitespace since that's what were verifying doesn't change stuff
4745
+	if strings.Replace(e1, " ", "", -1) != strings.Replace(e2, " ", "", -1) {
4746
+		t.Fatalf("Build 3's error wasn't the same as build 1's\n1:%s\n3:%s", err1, err2)
4747
+	}
4748
+
4749
+	ctx.Add("Dockerfile", "FROM busybox\n   RUN    ")
4750
+	if _, err2 = buildImageFromContext(name, ctx, false); err2 == nil {
4751
+		t.Fatal("Build 4 was supposed to fail, but didn't")
4752
+	}
4753
+
4754
+	// Skip over the times
4755
+	e1 = err1.Error()[strings.Index(err1.Error(), `level="`):]
4756
+	e2 = err2.Error()[strings.Index(err1.Error(), `level="`):]
4757
+
4758
+	// Ignore whitespace since that's what were verifying doesn't change stuff
4759
+	if strings.Replace(e1, " ", "", -1) != strings.Replace(e2, " ", "", -1) {
4760
+		t.Fatalf("Build 4's error wasn't the same as build 1's\n1:%s\n4:%s", err1, err2)
4761
+	}
4762
+
4763
+	logDone("build - test spaces")
4764
+}
4765
+
4766
+func TestBuildSpacesWithQuotes(t *testing.T) {
4767
+	// Test to make sure that spaces in quotes aren't lost
4768
+	name := "testspacesquotes"
4769
+	defer deleteImages(name)
4770
+
4771
+	dockerfile := `FROM busybox
4772
+RUN echo "  \
4773
+  foo  "`
4774
+
4775
+	_, out, err := buildImageWithOut(name, dockerfile, false)
4776
+	if err != nil {
4777
+		t.Fatal("Build failed:", err)
4778
+	}
4779
+
4780
+	expecting := "\n    foo  \n"
4781
+	if !strings.Contains(out, expecting) {
4782
+		t.Fatalf("Bad output: %q expecting to contian %q", out, expecting)
4783
+	}
4784
+
4785
+	logDone("build - test spaces with quotes")
4786
+}