elm-review/tests/NoUnusedDependenciesTest.elm

219 lines
6.0 KiB
Elm

module NoUnusedDependenciesTest exposing (all)
import Elm.Project
import Elm.Version
import Json.Decode as Decode
import NoUnusedDependencies exposing (rule)
import Review.Project as Project exposing (Project)
import Review.Test
import Test exposing (Test, describe, test)
createProject : String -> Project
createProject rawElmJson =
Project.new
|> Project.withElmJson (createElmJson rawElmJson)
|> Project.withDependency packageWithFoo
|> Project.withDependency packageWithBar
createElmJson : String -> { path : String, raw : String, project : Elm.Project.Project }
createElmJson rawElmJson =
case Decode.decodeString Elm.Project.decoder rawElmJson of
Ok elmJson ->
{ path = "elm.json"
, raw = rawElmJson
, project = elmJson
}
Err _ ->
createElmJson rawElmJson
applicationElmJson : String
applicationElmJson =
"""
{
"type": "application",
"source-directories": [
"src"
],
"elm-version": "0.19.1",
"dependencies": {
"direct": {
"elm/core": "1.0.0",
"author/package-with-foo": "1.0.0",
"author/package-with-bar": "1.0.0"
},
"indirect": {}
},
"test-dependencies": {
"direct": {},
"indirect": {}
}
}"""
packageElmJson : String
packageElmJson =
"""
{
"type": "package",
"name": "author/package",
"summary": "Summary",
"license": "BSD-3-Clause",
"version": "1.0.0",
"exposed-modules": [
"Exposed"
],
"elm-version": "0.19.0 <= v < 0.20.0",
"dependencies": {
"elm/core": "1.0.0 <= v < 2.0.0",
"author/package-with-foo": "1.0.0 <= v < 2.0.0",
"author/package-with-bar": "1.0.0 <= v < 2.0.0"
},
"test-dependencies": {}
}"""
packageWithFoo : Project.Dependency
packageWithFoo =
{ name = "author/package-with-foo"
, version = Elm.Version.one
, modules =
[ { name = "Foo"
, comment = ""
, unions = []
, aliases = []
, values = []
, binops = []
}
]
, elmJson = .project <| createElmJson """
{
"type": "package",
"name": "author/package-with-foo",
"summary": "Summary",
"license": "BSD-3-Clause",
"version": "1.0.0",
"exposed-modules": [
"Foo"
],
"elm-version": "0.19.0 <= v < 0.20.0",
"dependencies": {
"elm/core": "1.0.0 <= v < 2.0.0"
},
"test-dependencies": {}
}"""
}
packageWithBar : Project.Dependency
packageWithBar =
{ name = "author/package-with-bar"
, version = Elm.Version.one
, modules =
[ { name = "Bar"
, comment = ""
, unions = []
, aliases = []
, values = []
, binops = []
}
]
, elmJson = .project <| createElmJson """
{
"type": "package",
"name": "author/package-with-bar",
"summary": "Summary",
"license": "BSD-3-Clause",
"version": "1.0.0",
"exposed-modules": [
"Bar"
],
"elm-version": "0.19.0 <= v < 0.20.0",
"dependencies": {
"elm/core": "1.0.0 <= v < 2.0.0"
},
"test-dependencies": {}
}"""
}
details : List String
details =
[ "You can simplify your project a tiny bit by removing this dependency."
]
all : Test
all =
describe "NoUnused.Dependencies"
[ test "should not report anything if there is no `elm.json` file" <|
\() ->
"""
module A exposing (a)
a = 1
"""
|> Review.Test.run rule
|> Review.Test.expectNoErrors
, test "should report unused dependencies for an application when none of their modules are imported" <|
\() ->
"""
module A exposing (a)
a = 1
"""
|> Review.Test.runWithProjectData (createProject applicationElmJson) rule
|> Review.Test.expectErrorsForElmJson
[ Review.Test.error
{ message = "Unused dependency `author/package-with-bar`"
, details = details
, under = "author/package-with-bar"
}
, Review.Test.error
{ message = "Unused dependency `author/package-with-foo`"
, details = details
, under = "author/package-with-foo"
}
]
, test "should not report dependencies for an application whose modules are imported" <|
\() ->
"""
module A exposing (a)
import Foo
import Bar
a = 1
"""
|> Review.Test.runWithProjectData (createProject applicationElmJson) rule
|> Review.Test.expectNoErrors
, test "should report unused dependencies for a package when none of their modules are imported" <|
\() ->
"""
module A exposing (a)
a = 1
"""
|> Review.Test.runWithProjectData (createProject packageElmJson) rule
|> Review.Test.expectErrorsForElmJson
[ Review.Test.error
{ message = "Unused dependency `author/package-with-bar`"
, details = details
, under = "author/package-with-bar"
}
, Review.Test.error
{ message = "Unused dependency `author/package-with-foo`"
, details = details
, under = "author/package-with-foo"
}
]
, test "should not report dependencies for a package whose modules are imported" <|
\() ->
"""
module A exposing (a)
import Foo
import Bar
a = 1
"""
|> Review.Test.runWithProjectData (createProject packageElmJson) rule
|> Review.Test.expectNoErrors
]