Skip to content

Commit

Permalink
fix: Quote labels and env also when having a space
Browse files Browse the repository at this point in the history
Fixes #988
  • Loading branch information
rhuss committed Apr 10, 2018
1 parent 1bf973b commit f599d29
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -244,23 +244,25 @@ private String createKeyValue(String key, String value) {
if (value == null || value.isEmpty()) {
return sb.append("\"\"").toString();
}
StringBuffer valBuf = new StringBuffer();
boolean escaped = false;
StringBuilder valBuf = new StringBuilder();
boolean toBeQuoted = false;
for (int i = 0; i < value.length(); ++i) {
char c = value.charAt(i);
switch (c) {
case '"':
case '\n':
case '\\':
escaped = true;
// escape the character
valBuf.append('\\');
// fall into writing the character
case ' ':
// space needs quotes, too
toBeQuoted = true;
default:
// always append
valBuf.append(c);
}
}
if (escaped) {
if (toBeQuoted) {
// need to keep quotes
sb.append('"').append(valBuf.toString()).append('"');
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,21 @@ public void testBuildDockerFileMultilineLabel() throws Exception {
.cmd(a)
.labels(ImmutableMap.of("key", "unquoted",
"flag", "",
"with_space", "1.fc nuremberg",
"some-json", "{\n \"key\": \"value\"\n}\n"))
.content();
String expected = loadFile("docker/Dockerfile.multiline_label.test");
assertEquals(expected, stripCR(dockerfileContent));
}

@Test
public void testBuildLabelWithSpace() throws Exception {
String dockerfileContent = new DockerFileBuilder()
.labels(ImmutableMap.of("key", "label with space"))
.content();
assertTrue(stripCR(dockerfileContent).contains("LABEL key=\"label with space\""));
}

@Test
public void testBuildDockerFileUDPPort() throws Exception {
Arguments a = Arguments.Builder.get().withParam("c1").withParam("c2").build();
Expand Down Expand Up @@ -85,7 +94,7 @@ public void testBuildDockerFileExplicitTCPPort() throws Exception {
String expected = loadFile("docker/Dockerfile_tcp.test");
assertEquals(expected, stripCR(dockerfileContent));
}

@Test(expected=IllegalArgumentException.class)
public void testBuildDockerFileBadPort() throws Exception {
Arguments a = Arguments.Builder.get().withParam("c1").withParam("c2").build();
Expand All @@ -101,7 +110,7 @@ public void testBuildDockerFileBadPort() throws Exception {
.volumes(Collections.singletonList("/vol1"))
.run(Arrays.asList("echo something", "echo second"))
.content();
}
}

@Test(expected=IllegalArgumentException.class)
public void testBuildDockerFileBadProtocol() throws Exception {
Expand All @@ -118,7 +127,7 @@ public void testBuildDockerFileBadProtocol() throws Exception {
.volumes(Collections.singletonList("/vol1"))
.run(Arrays.asList("echo something", "echo second"))
.content();
}
}

@Test
public void testDockerFileOptimisation() throws Exception {
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/docker/Dockerfile.multiline_label.test
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
FROM image
LABEL key=unquoted flag="" some-json="{\
LABEL key=unquoted flag="" with_space="1.fc nuremberg" some-json="{\
\"key\": \"value\"\
}\
"
Expand Down

0 comments on commit f599d29

Please sign in to comment.