2020-06-01 10:01:53 +03:00
|
|
|
module NoUnapprovedLicenseTest exposing (all)
|
2020-02-16 21:03:49 +03:00
|
|
|
|
2020-03-07 20:37:00 +03:00
|
|
|
import Elm.Docs
|
2020-02-16 21:03:49 +03:00
|
|
|
import Elm.Project
|
|
|
|
import Json.Decode as Decode
|
2020-06-01 10:01:53 +03:00
|
|
|
import NoUnapprovedLicense exposing (rule)
|
2020-02-16 21:03:49 +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 21:03:49 +03:00
|
|
|
import Review.Test
|
|
|
|
import Test exposing (Test, describe, test)
|
|
|
|
|
|
|
|
|
|
|
|
createProject : String -> Project
|
|
|
|
createProject license =
|
|
|
|
Project.new
|
2020-03-20 01:50:41 +03:00
|
|
|
|> Project.addElmJson (createElmJson applicationElmJson)
|
|
|
|
|> Project.addDependency (dependency license)
|
2020-02-16 21:03:49 +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 21:03:49 +03:00
|
|
|
|
|
|
|
|
|
|
|
applicationElmJson : String
|
|
|
|
applicationElmJson =
|
|
|
|
"""
|
|
|
|
{
|
|
|
|
"type": "application",
|
|
|
|
"source-directories": [
|
|
|
|
"src"
|
|
|
|
],
|
|
|
|
"elm-version": "0.19.1",
|
|
|
|
"dependencies": {
|
|
|
|
"direct": {
|
|
|
|
"elm/core": "1.0.0",
|
|
|
|
"author/dependency": "1.0.0"
|
|
|
|
},
|
|
|
|
"indirect": {}
|
|
|
|
},
|
|
|
|
"test-dependencies": {
|
|
|
|
"direct": {},
|
|
|
|
"indirect": {}
|
|
|
|
}
|
|
|
|
}"""
|
|
|
|
|
|
|
|
|
2020-03-07 20:37:00 +03:00
|
|
|
dependency : String -> Dependency
|
2020-02-16 21:03:49 +03:00
|
|
|
dependency license =
|
2020-03-07 20:37:00 +03:00
|
|
|
let
|
|
|
|
modules : List Elm.Docs.Module
|
|
|
|
modules =
|
|
|
|
[ { name = "Foo"
|
|
|
|
, comment = ""
|
|
|
|
, unions = []
|
|
|
|
, aliases = []
|
|
|
|
, values = []
|
|
|
|
, binops = []
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
rawElmJson : String
|
|
|
|
rawElmJson =
|
|
|
|
"""
|
2020-02-16 21:03:49 +03:00
|
|
|
{
|
2020-03-07 20:37:00 +03:00
|
|
|
"type": "package",
|
|
|
|
"name": "author/dependency",
|
|
|
|
"summary": "Summary",
|
|
|
|
"license": \"""" ++ license ++ """",
|
|
|
|
"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": {}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
elmJson : Elm.Project.Project
|
|
|
|
elmJson =
|
|
|
|
createElmJson rawElmJson
|
|
|
|
|> .project
|
|
|
|
in
|
|
|
|
Dependency.create
|
|
|
|
"author/dependency"
|
|
|
|
elmJson
|
|
|
|
modules
|
2020-02-16 21:03:49 +03:00
|
|
|
|
|
|
|
|
|
|
|
sourceCode : String
|
|
|
|
sourceCode =
|
|
|
|
"""
|
|
|
|
module A exposing (a)
|
|
|
|
a = 1
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
all : Test
|
|
|
|
all =
|
2020-06-01 10:01:53 +03:00
|
|
|
describe "NoUnapprovedLicense"
|
2020-02-16 21:03:49 +03:00
|
|
|
[ test "should not report anything if there is no `elm.json` file" <|
|
|
|
|
\() ->
|
|
|
|
sourceCode
|
|
|
|
|> Review.Test.run (rule { allowed = [], forbidden = [] })
|
|
|
|
|> Review.Test.expectNoErrors
|
|
|
|
, test "should not report anything if all dependencies have a license that is allowed" <|
|
|
|
|
\() ->
|
|
|
|
sourceCode
|
|
|
|
|> Review.Test.runWithProjectData (createProject "MIT") (rule { allowed = [ "MIT" ], forbidden = [] })
|
|
|
|
|> Review.Test.expectNoErrors
|
|
|
|
, test "should report an error if a dependency has an unknown license" <|
|
|
|
|
\() ->
|
|
|
|
sourceCode
|
|
|
|
|> Review.Test.runWithProjectData (createProject "BSD-3-Clause") (rule { allowed = [ "MIT" ], forbidden = [] })
|
|
|
|
|> Review.Test.expectErrorsForElmJson
|
|
|
|
[ Review.Test.error
|
|
|
|
{ message = "Unknown license `BSD-3-Clause` for dependency `author/dependency`"
|
|
|
|
, details =
|
|
|
|
[ "Talk to your legal team and see if this license is allowed. If it is allowed, add it to the list of allowed licenses. Otherwise, add it to the list of forbidden licenses and remove this dependency."
|
|
|
|
, "More info about licenses at https://spdx.org/licenses."
|
|
|
|
]
|
|
|
|
, under = "author/dependency"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
, test "should report an error if a dependency has a forbidden license" <|
|
|
|
|
\() ->
|
|
|
|
sourceCode
|
|
|
|
|> Review.Test.runWithProjectData (createProject "BSD-3-Clause") (rule { allowed = [ "MIT" ], forbidden = [ "BSD-3-Clause" ] })
|
|
|
|
|> Review.Test.expectErrorsForElmJson
|
|
|
|
[ Review.Test.error
|
|
|
|
{ message = "Forbidden license `BSD-3-Clause` for dependency `author/dependency`"
|
|
|
|
, details = [ "This license has been marked as forbidden and you should therefore not use this package." ]
|
|
|
|
, under = "author/dependency"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
]
|