Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions src/Strategy/Go/Gomod.hs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import Text.Megaparsec (
MonadParsec (eof, takeWhile1P, try),
Parsec,
between,
choice,
chunk,
count,
many,
Expand Down Expand Up @@ -217,12 +218,19 @@ parsePackageVersion lexify = parseSemOrPseudo <|> parseNonCanonical

gomodParser :: Parser Gomod
gomodParser = do
let emptyGoMod = do
eof
pure ("", [])
let nonEmptyGoMod = do
_ <- lexeme (chunk "module")
name <- modulePath
_ <- scn
statements <- many (statement <* scn)
eof
pure (name, statements)

_ <- scn
_ <- lexeme (chunk "module")
name <- modulePath
_ <- scn
statements <- many (statement <* scn)
eof
(name, statements) <- choice [nonEmptyGoMod, emptyGoMod]

let statements' = concat statements

Expand Down
20 changes: 18 additions & 2 deletions test/Go/GomodSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import Data.Function ((&))
import Data.Map.Strict qualified as Map
import Data.SemVer (version)
import Data.SemVer.Internal (Identifier (..))
import Data.Text (Text)
import Data.Text (Text, empty)
import Data.Text.IO qualified as TIO
import DepTypes (DepType (GoType), Dependency (..), VerConstraint (CEq))
import Effect.Grapher (direct, evalGrapher)
import Graphing (Graphing (..))
import Strategy.Go.Gomod (Gomod (..), PackageVersion (..), Require (..), buildGraph, gomodParser)
import Strategy.Go.Types (graphingGolang)
import Test.Hspec (Spec, describe, it, runIO, shouldBe)
import Test.Hspec.Megaparsec (shouldParse)
import Test.Hspec.Megaparsec (shouldFailOn, shouldParse)
import Text.Megaparsec (runParser)
import Text.RawString.QQ (r)

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

emptyGomod :: Gomod
emptyGomod =
Gomod
{ modName = ""
, modRequires = []
, modReplaces = Map.empty
, modLocalReplaces = Map.empty
, modExcludes = []
}

localReplaceGomod :: Gomod
localReplaceGomod =
Gomod
Expand Down Expand Up @@ -193,6 +203,9 @@ spec_parse = do
it "parses a trivial example" $ do
runParser gomodParser "" trivialInput `shouldParse` trivialGomod

it "parses empty go.mod" $ do
runParser gomodParser "" Data.Text.empty `shouldParse` emptyGomod

it "parses each edge case" $ do
runParser gomodParser "" edgecaseInput `shouldParse` edgeCaseGomod

Expand All @@ -211,6 +224,9 @@ spec_parse = do
runParser gomodParser "" goModWithRetractComment3 `shouldParse` gomodWithRetract
runParser gomodParser "" goModWithRetractComment4 `shouldParse` gomodWithRetract

it "fails to parse invalid go.mod" $ do
runParser gomodParser "" `shouldFailOn` "invalid input"

gomodWithRetract :: Gomod
gomodWithRetract =
Gomod
Expand Down
Loading