2020-04-03 16:50:37 +03:00
|
|
|
module NoUnused.DependenciesTest exposing (all)
|
2020-02-16 13:46:08 +03:00
|
|
|
|
2020-03-07 20:37:00 +03:00
|
|
|
import Elm.Docs
|
2020-02-16 13:46:08 +03:00
|
|
|
import Elm.Project
|
|
|
|
import Json.Decode as Decode
|
2020-04-03 16:50:37 +03:00
|
|
|
import NoUnused.Dependencies exposing (rule)
|
2020-02-16 13:46:08 +03:00
|
|
|
import Review.Project as Project exposing (Project)
|
2020-03-07 20:37:00 +03:00
|
|
|
import Review.Project.Dependency as Dependency exposing (Dependency)
|
2020-02-16 13:46:08 +03:00
|
|
|
import Review.Test
|
|
|
|
import Test exposing (Test, describe, test)
|
|
|
|
|
|
|
|
|
|
|
|
createProject : String -> Project
|
|
|
|
createProject rawElmJson =
|
|
|
|
Project.new
|
2020-03-20 01:50:41 +03:00
|
|
|
|> Project.addElmJson (createElmJson rawElmJson)
|
|
|
|
|> Project.addDependency packageWithFoo
|
|
|
|
|> Project.addDependency packageWithBar
|
2020-02-16 13:46:08 +03:00
|
|
|
|
|
|
|
|
|
|
|
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 _ ->
|
2020-03-07 20:37:00 +03:00
|
|
|
Debug.todo "Invalid elm.json supplied to test"
|
2020-02-16 13:46:08 +03:00
|
|
|
|
|
|
|
|
|
|
|
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": {}
|
|
|
|
}"""
|
|
|
|
|
|
|
|
|
2020-03-07 20:37:00 +03:00
|
|
|
packageWithFoo : Dependency
|
2020-02-16 13:46:08 +03:00
|
|
|
packageWithFoo =
|
2020-03-07 20:37:00 +03:00
|
|
|
let
|
|
|
|
modules : List Elm.Docs.Module
|
|
|
|
modules =
|
|
|
|
[ { name = "Foo"
|
|
|
|
, comment = ""
|
|
|
|
, unions = []
|
|
|
|
, aliases = []
|
|
|
|
, values = []
|
|
|
|
, binops = []
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
elmJson : Elm.Project.Project
|
|
|
|
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": {}
|
|
|
|
}"""
|
|
|
|
in
|
|
|
|
Dependency.create
|
|
|
|
"author/package-with-foo"
|
|
|
|
elmJson
|
|
|
|
modules
|
|
|
|
|
|
|
|
|
|
|
|
packageWithBar : Dependency
|
2020-02-16 13:46:08 +03:00
|
|
|
packageWithBar =
|
2020-03-07 20:37:00 +03:00
|
|
|
let
|
|
|
|
modules : List Elm.Docs.Module
|
|
|
|
modules =
|
|
|
|
[ { name = "Bar"
|
|
|
|
, comment = ""
|
|
|
|
, unions = []
|
|
|
|
, aliases = []
|
|
|
|
, values = []
|
|
|
|
, binops = []
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
elmJson : Elm.Project.Project
|
|
|
|
elmJson =
|
|
|
|
.project <| createElmJson """
|
2020-02-16 19:50:49 +03:00
|
|
|
{
|
2020-03-07 20:37:00 +03:00
|
|
|
"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": {}
|
2020-02-16 19:50:49 +03:00
|
|
|
}"""
|
2020-03-07 20:37:00 +03:00
|
|
|
in
|
|
|
|
Dependency.create
|
|
|
|
"author/package-with-bar"
|
|
|
|
elmJson
|
|
|
|
modules
|
2020-02-16 13:46:08 +03:00
|
|
|
|
|
|
|
|
|
|
|
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
|
2020-02-16 18:52:05 +03:00
|
|
|
|> Review.Test.expectErrorsForElmJson
|
|
|
|
[ Review.Test.error
|
|
|
|
{ message = "Unused dependency `author/package-with-bar`"
|
2020-04-03 16:50:37 +03:00
|
|
|
, details = [ "To remove it, I recommend installing `elm-json` and running `elm-json uninstall author/package-with-bar`" ]
|
2020-02-16 18:52:05 +03:00
|
|
|
, under = "author/package-with-bar"
|
|
|
|
}
|
|
|
|
, Review.Test.error
|
|
|
|
{ message = "Unused dependency `author/package-with-foo`"
|
2020-04-03 16:50:37 +03:00
|
|
|
, details = [ "To remove it, I recommend installing `elm-json` and running `elm-json uninstall author/package-with-foo`" ]
|
2020-02-16 18:52:05 +03:00
|
|
|
, under = "author/package-with-foo"
|
|
|
|
}
|
2020-02-16 13:46:08 +03:00
|
|
|
]
|
|
|
|
, 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
|
2020-02-16 18:52:05 +03:00
|
|
|
|> Review.Test.expectErrorsForElmJson
|
|
|
|
[ Review.Test.error
|
|
|
|
{ message = "Unused dependency `author/package-with-bar`"
|
2020-04-03 16:50:37 +03:00
|
|
|
, details = [ "To remove it, I recommend installing `elm-json` and running `elm-json uninstall author/package-with-bar`" ]
|
2020-02-16 18:52:05 +03:00
|
|
|
, under = "author/package-with-bar"
|
|
|
|
}
|
|
|
|
, Review.Test.error
|
|
|
|
{ message = "Unused dependency `author/package-with-foo`"
|
2020-04-03 16:50:37 +03:00
|
|
|
, details = [ "To remove it, I recommend installing `elm-json` and running `elm-json uninstall author/package-with-foo`" ]
|
2020-02-16 18:52:05 +03:00
|
|
|
, under = "author/package-with-foo"
|
|
|
|
}
|
2020-02-16 13:46:08 +03:00
|
|
|
]
|
|
|
|
, 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
|
|
|
|
]
|