Report an error if the project is an application

This commit is contained in:
Jeroen Engels 2023-06-02 21:44:46 +02:00
parent 04fdc1fba1
commit a2bd65eeb6
2 changed files with 87 additions and 20 deletions

View File

@ -77,27 +77,32 @@ withPathToChangelog changelogPath _ =
type alias ProjectContext =
{ elmJsonVersion : String
{ elmJsonVersion : Maybe String
}
initialProjectContext : ProjectContext
initialProjectContext =
{ elmJsonVersion = "1.0.0"
{ elmJsonVersion = Nothing
}
elmJsonVisitor : Maybe { a | project : Project } -> ProjectContext -> ( List nothing, ProjectContext )
elmJsonVisitor : Maybe { a | project : Project } -> ProjectContext -> ( List (Rule.Error scope), ProjectContext )
elmJsonVisitor maybeElmJsonData context =
case maybeElmJsonData of
Just { project } ->
case project of
Elm.Project.Package { version } ->
( [], { context | elmJsonVersion = Elm.Version.toString version } )
( [], { context | elmJsonVersion = Just (Elm.Version.toString version) } )
Elm.Project.Application _ ->
-- TODO Report an error
( [], context )
( [ Rule.globalError
{ message = "The Elm project is unexpectedly an application"
, details = [ "This rule only supports Elm packages, but doesn't support Elm applications as they don't have a version number. I recommend that you remove this rule from your review configuration." ]
}
]
, context
)
Nothing ->
( [], context )
@ -107,17 +112,22 @@ extraFilesVisitor : Maybe String -> List { path : String, content : String } ->
extraFilesVisitor changelogPath files context =
case List.head files of
Just { content } ->
if String.contains context.elmJsonVersion content then
( [], context )
case context.elmJsonVersion of
Nothing ->
( [], context )
else
( [ Rule.globalError
{ message = "Missing entry in CHANGELOG.md for version " ++ context.elmJsonVersion
, details = [ "It seems you have or are ready to release a new version of your package, but forgot to include releases notes for it in your CHANGELOG.md file." ]
}
]
, context
)
Just elmJsonVersion ->
if String.contains elmJsonVersion content then
( [], context )
else
( [ Rule.globalError
{ message = "Missing entry in CHANGELOG.md for version " ++ elmJsonVersion
, details = [ "It seems you have or are ready to release a new version of your package, but forgot to include releases notes for it in your CHANGELOG.md file." ]
}
]
, context
)
Nothing ->
case changelogPath of

View File

@ -93,17 +93,38 @@ a = 1
]
}
]
, test "should report an error when the project is an application" <|
\() ->
let
project : Project
project =
Project.addExtraFiles
[ { path = "CHANGELOG.md"
, content = "# something"
}
]
application
in
"""module A exposing (..)
a = 1
"""
|> Review.Test.runWithProjectData project (rule defaults)
|> Review.Test.expectGlobalErrors
[ { message = "The Elm project is unexpectedly an application"
, details = [ "This rule only supports Elm packages, but doesn't support Elm applications as they don't have a version number. I recommend that you remove this rule from your review configuration." ]
}
]
]
package : Project
package =
case Decode.decodeString Elm.Project.decoder elmJson of
case Decode.decodeString Elm.Project.decoder packageElmJson of
Ok project ->
Project.new
|> Project.addElmJson
{ path = "elm.json"
, raw = elmJson
, raw = packageElmJson
, project = project
}
@ -111,8 +132,8 @@ package =
Debug.todo ("Invalid elm.json supplied to test: " ++ Debug.toString err)
elmJson : String
elmJson =
packageElmJson : String
packageElmJson =
"""{
"type": "package",
"name": "author/package",
@ -128,3 +149,39 @@ elmJson =
},
"test-dependencies": {}
}"""
application : Project
application =
case Decode.decodeString Elm.Project.decoder applicationElmJson of
Ok project ->
Project.new
|> Project.addElmJson
{ path = "elm.json"
, raw = applicationElmJson
, project = project
}
Err err ->
Debug.todo ("Invalid elm.json supplied to test: " ++ Debug.toString err)
applicationElmJson : String
applicationElmJson =
"""{
"type": "application",
"source-directories": [
"src"
],
"elm-version": "0.19.1",
"dependencies": {
"direct": {
"elm/core": "1.0.0"
},
"indirect": {}
},
"test-dependencies": {
"direct": {},
"indirect": {}
}
}"""