Skip to content

Commit

Permalink
Merge pull request #36 from hadolint/fix-new-line-escape
Browse files Browse the repository at this point in the history
Fixed the parser when there is a newline escape followed by space
  • Loading branch information
lorenzo authored Jun 7, 2018
2 parents 9a0e80f + 7333b73 commit ad14685
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 10 deletions.
4 changes: 3 additions & 1 deletion integration-tests/parseFile.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ main :: IO ()
main = do
args <- getArgs
output <- parseFile $ head args
print output
case output of
Left err -> error (parseErrorPretty err)
Right ast -> print (prettyPrintDockerfile ast)
11 changes: 7 additions & 4 deletions integration-tests/parse_files.sh
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,13 @@ function parse_dockerfiles() {
stack ghc parseFile.hs --package language-docker
dockerfiles=$(find . -name 'Dockerfile')
for dockerfile in $dockerfiles; do
if [[ "$BLACKLIST" == *$dockerfile* ]]; then
continue;
fi
if ./parseFile "$dockerfile" | grep -a1 unexpected; then
if [[ "$BLACKLIST" == *$dockerfile* ]]; then
continue;
fi

if ./parseFile "$dockerfile" > /dev/null; then
echo "$dockerfile"
else
result="false"
fi
done
Expand Down
2 changes: 1 addition & 1 deletion src/Language/Docker.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Language.Docker
, Text.Megaparsec.parseErrorPretty
-- * Pretty-printing Dockerfiles (@Language.Docker.PrettyPrint@)
, prettyPrint
, prettyPrintInstructionPos
, prettyPrintDockerfile
-- * Writting Dockerfiles (@Language.Docker.EDSL@)
, Language.Docker.EDSL.toDockerfileText
, Language.Docker.EDSL.toDockerfile
Expand Down
12 changes: 8 additions & 4 deletions src/Language/Docker/Normalize.hs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ normalize allLines =
-- and we return 'Nothing' as an indication that this line does not form part of the
-- final result.
transform :: NormalizedLine -> Text -> (NormalizedLine, Maybe Text)
transform (Joined prev times) line
transform (Joined prev times) rawLine
-- If we are buffering lines and the next one is empty or it starts with a comment
-- we simply ignore the comment and remember to add a newline
| Text.null line || isComment line = (Joined prev (times + 1), Nothing)
Expand All @@ -61,12 +61,16 @@ normalize allLines =
-- the concatanation of the buffer and the current line as result, after padding with
-- newlines
| otherwise = (Continue, Just (toText (prev <> Builder.fromText line <> padNewlines times)))
where
line = Text.stripEnd rawLine
-- When not buffering lines, then we just check if we need to start doing it by checking
-- whether or not the current line ends with \. If it does not, then we just yield the
-- current line as part of the result
transform Continue l
| endsWithEscape l = (Joined (normalizeLast l) 1, Nothing)
| otherwise = (Continue, Just l)
transform Continue rawLine
| endsWithEscape line = (Joined (normalizeLast line) 1, Nothing)
| otherwise = (Continue, Just line)
where
line = Text.stripEnd rawLine
--
endsWithEscape t
| Text.null t = False
Expand Down
3 changes: 3 additions & 0 deletions src/Language/Docker/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
module Language.Docker.Parser
( parseText
, parseFile
, Parser
, Error
, DockerfileError(..)
) where

import Control.Monad (void)
Expand Down
12 changes: 12 additions & 0 deletions test/Language/Docker/ParserSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,12 @@ spec = do
, "ENV NODE_VERSION=v5.7.1 DEBIAN_FRONTEND=noninteractive\n"
]
in normalizeEscapedLines dockerfile `shouldBe` normalizedDockerfile

it "join escaped lines" $
let dockerfile = Text.unlines ["ENV foo=bar \\", "baz=foz"]
normalizedDockerfile = Text.unlines ["ENV foo=bar baz=foz", ""]
in normalizeEscapedLines dockerfile `shouldBe` normalizedDockerfile

it "join long CMD" $
let longEscapedCmd =
Text.unlines
Expand All @@ -260,6 +262,16 @@ spec = do
, "\n"
]
in normalizeEscapedLines longEscapedCmd `shouldBe` longEscapedCmdExpected

it "tolerates spaces after a newline escape" $
let dockerfile = Text.unlines [ "FROM busy\\ "
, "box"
, "RUN echo\\ "
, " hello"
]
in assertAst dockerfile [ From (UntaggedImage "busybox" Nothing)
, Run "echo hello"
]
describe "expose" $ do
it "should handle number ports" $
let content = "EXPOSE 8080"
Expand Down

0 comments on commit ad14685

Please sign in to comment.