Skip to content

Commit 3f83ada

Browse files
committed
Add test and fix handling of invalid go.mod
1 parent a5ad571 commit 3f83ada

File tree

2 files changed

+35
-13
lines changed

2 files changed

+35
-13
lines changed

src/Strategy/Go/Gomod.hs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ import Text.Megaparsec (
5353
MonadParsec (eof, takeWhile1P, try),
5454
Parsec,
5555
between,
56+
choice,
5657
chunk,
5758
count,
5859
many,
5960
oneOf,
60-
option,
6161
optional,
6262
parse,
6363
sepBy,
@@ -220,16 +220,22 @@ gomodParser :: Parser Gomod
220220
gomodParser = do
221221
_ <- scn
222222
(name, statements) <-
223-
option
224-
("empty module", [])
225-
( do
226-
_ <- lexeme (chunk "module")
227-
name <- modulePath
228-
_ <- scn
229-
statements <- many (statement <* scn)
230-
eof
231-
pure (name, statements)
232-
)
223+
choice
224+
[ -- Non-empty go.mod file
225+
( do
226+
_ <- lexeme (chunk "module")
227+
name <- modulePath
228+
_ <- scn
229+
statements <- many (statement <* scn)
230+
eof
231+
pure (name, statements)
232+
)
233+
, -- Empty go.mod file
234+
( do
235+
eof
236+
pure ("", [])
237+
)
238+
]
233239

234240
let statements' = concat statements
235241

test/Go/GomodSpec.hs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ import Data.Function ((&))
77
import Data.Map.Strict qualified as Map
88
import Data.SemVer (version)
99
import Data.SemVer.Internal (Identifier (..))
10-
import Data.Text (Text)
10+
import Data.Text (Text, empty)
1111
import Data.Text.IO qualified as TIO
1212
import DepTypes (DepType (GoType), Dependency (..), VerConstraint (CEq))
1313
import Effect.Grapher (direct, evalGrapher)
1414
import Graphing (Graphing (..))
1515
import Strategy.Go.Gomod (Gomod (..), PackageVersion (..), Require (..), buildGraph, gomodParser)
1616
import Strategy.Go.Types (graphingGolang)
1717
import Test.Hspec (Spec, describe, it, runIO, shouldBe)
18-
import Test.Hspec.Megaparsec (shouldParse)
18+
import Test.Hspec.Megaparsec (shouldFailOn, shouldParse)
1919
import Text.Megaparsec (runParser)
2020
import Text.RawString.QQ (r)
2121

@@ -57,6 +57,16 @@ trivialGraph = run . evalGrapher $ do
5757
direct $ dep "github.com/pkg/overridden" "overridden"
5858
direct $ dep "github.com/pkg/three/v3" "v3.0.0"
5959

60+
emptyGomod :: Gomod
61+
emptyGomod =
62+
Gomod
63+
{ modName = ""
64+
, modRequires = []
65+
, modReplaces = Map.empty
66+
, modLocalReplaces = Map.empty
67+
, modExcludes = []
68+
}
69+
6070
localReplaceGomod :: Gomod
6171
localReplaceGomod =
6272
Gomod
@@ -193,6 +203,9 @@ spec_parse = do
193203
it "parses a trivial example" $ do
194204
runParser gomodParser "" trivialInput `shouldParse` trivialGomod
195205

206+
it "parses empty go.mod" $ do
207+
runParser gomodParser "" Data.Text.empty `shouldParse` emptyGomod
208+
196209
it "parses each edge case" $ do
197210
runParser gomodParser "" edgecaseInput `shouldParse` edgeCaseGomod
198211

@@ -211,6 +224,9 @@ spec_parse = do
211224
runParser gomodParser "" goModWithRetractComment3 `shouldParse` gomodWithRetract
212225
runParser gomodParser "" goModWithRetractComment4 `shouldParse` gomodWithRetract
213226

227+
it "fails to parse invalid go.mod" $ do
228+
runParser gomodParser "" `shouldFailOn` "invalid input"
229+
214230
gomodWithRetract :: Gomod
215231
gomodWithRetract =
216232
Gomod

0 commit comments

Comments
 (0)