elm-review/tests/NoDebug/LogTest.elm

288 lines
9.7 KiB
Elm
Raw Normal View History

module NoDebug.LogTest exposing (all)
import NoDebug.Log exposing (rule)
import Review.Test exposing (ReviewResult)
2018-11-22 21:19:19 +03:00
import Test exposing (Test, describe, test)
2017-06-18 14:43:37 +03:00
testRule : String -> ReviewResult
2018-11-06 20:46:46 +03:00
testRule string =
"module A exposing (..)\n\n"
++ string
|> Review.Test.run rule
2019-06-16 15:53:07 +03:00
2019-06-30 12:48:19 +03:00
message : String
message =
2020-03-07 01:03:37 +03:00
"Remove the use of `Debug.log` before shipping to production"
2019-07-28 01:16:57 +03:00
details : List String
details =
2020-03-07 01:03:37 +03:00
[ "`Debug.log` is useful when developing, but is not meant to be shipped to production or published in a package. I suggest removing its use before committing and attempting to push to production."
2019-07-28 01:16:57 +03:00
]
tests : List Test
tests =
[ test "should not report normal function calls" <|
\() ->
2017-06-18 14:43:37 +03:00
testRule """
2018-11-06 20:46:46 +03:00
a = foo n
b = bar.foo n
c = debug
e = debug.log n
d = debug.log n
"""
|> Review.Test.expectNoErrors
2020-03-07 01:03:37 +03:00
, test "should not report Debug.todo or Debug.toString calls" <|
\() ->
testRule """
a = Debug.toString n
b = Debug.todo ""
"""
|> Review.Test.expectNoErrors
, test "should report Debug.log use" <|
\() ->
2017-06-18 14:43:37 +03:00
testRule "a = Debug.log"
|> Review.Test.expectErrors
[ Review.Test.error
2019-06-30 12:48:19 +03:00
{ message = message
2019-07-28 01:16:57 +03:00
, details = details
2019-06-30 12:48:19 +03:00
, under = "Debug.log"
}
]
, test "should report Debug.log calls" <|
\() ->
2017-06-18 14:43:37 +03:00
testRule "a = Debug.log z"
|> Review.Test.expectErrors
[ Review.Test.error
2019-06-30 12:48:19 +03:00
{ message = message
2019-07-28 01:16:57 +03:00
, details = details
2019-06-30 12:48:19 +03:00
, under = "Debug.log"
}
]
, test "should report multiple Debug.log calls" <|
\() ->
2017-06-18 14:43:37 +03:00
testRule """
2018-11-06 20:46:46 +03:00
a = Debug.log z
b = Debug.log z
"""
|> Review.Test.expectErrors
[ Review.Test.error
2019-06-30 12:48:19 +03:00
{ message = message
2019-07-28 01:16:57 +03:00
, details = details
2019-06-30 12:48:19 +03:00
, under = "Debug.log"
}
|> Review.Test.atExactly { start = { row = 4, column = 5 }, end = { row = 4, column = 14 } }
, Review.Test.error
2019-06-30 12:48:19 +03:00
{ message = message
2019-07-28 01:16:57 +03:00
, details = details
2019-06-30 12:48:19 +03:00
, under = "Debug.log"
}
|> Review.Test.atExactly { start = { row = 5, column = 5 }, end = { row = 5, column = 14 } }
]
2020-03-07 01:03:37 +03:00
, test "should report Debug.log in a binary expression" <|
\() ->
2019-06-30 12:48:19 +03:00
testRule "a = ( Debug.log z ) + 2"
|> Review.Test.expectErrors
[ Review.Test.error
2019-06-30 12:48:19 +03:00
{ message = message
2019-07-28 01:16:57 +03:00
, details = details
2019-06-30 12:48:19 +03:00
, under = "Debug.log"
}
]
2020-03-07 01:03:37 +03:00
, test "should report Debug.log in a << binary expression" <|
\() ->
2017-06-18 14:43:37 +03:00
testRule "a = fn << Debug.log"
|> Review.Test.expectErrors
[ Review.Test.error
2019-06-30 12:48:19 +03:00
{ message = message
2019-07-28 01:16:57 +03:00
, details = details
2019-06-30 12:48:19 +03:00
, under = "Debug.log"
}
]
2020-03-07 01:03:37 +03:00
, test "should report Debug.log in a pipe expression" <|
\() ->
2017-06-18 14:43:37 +03:00
testRule "a = fn |> Debug.log z"
|> Review.Test.expectErrors
[ Review.Test.error
2019-06-30 12:48:19 +03:00
{ message = message
2019-07-28 01:16:57 +03:00
, details = details
2019-06-30 12:48:19 +03:00
, under = "Debug.log"
}
]
2020-03-07 01:03:37 +03:00
, test "should report Debug.log in an list expression" <|
\() ->
2019-06-30 12:48:19 +03:00
testRule "a = [ Debug.log z y ]"
|> Review.Test.expectErrors
[ Review.Test.error
2019-06-30 12:48:19 +03:00
{ message = message
2019-07-28 01:16:57 +03:00
, details = details
2019-06-30 12:48:19 +03:00
, under = "Debug.log"
}
]
2020-03-07 01:03:37 +03:00
, test "should report Debug.log in an record expression" <|
\() ->
2017-06-18 14:43:37 +03:00
testRule "a = { foo = Debug.log z y }"
|> Review.Test.expectErrors
[ Review.Test.error
2019-06-30 12:48:19 +03:00
{ message = message
2019-07-28 01:16:57 +03:00
, details = details
2019-06-30 12:48:19 +03:00
, under = "Debug.log"
}
]
2020-03-07 01:03:37 +03:00
, test "should report Debug.log in an record update expression" <|
2017-06-18 15:06:36 +03:00
\() ->
testRule "a = { model | foo = Debug.log z y }"
|> Review.Test.expectErrors
[ Review.Test.error
2019-06-30 12:48:19 +03:00
{ message = message
2019-07-28 01:16:57 +03:00
, details = details
2019-06-30 12:48:19 +03:00
, under = "Debug.log"
}
]
2020-03-07 01:03:37 +03:00
, test "should report Debug.log in an lambda expression" <|
\() ->
2017-06-18 14:43:37 +03:00
testRule "a = (\\foo -> Debug.log z foo)"
|> Review.Test.expectErrors
[ Review.Test.error
2019-06-30 12:48:19 +03:00
{ message = message
2019-07-28 01:16:57 +03:00
, details = details
2019-06-30 12:48:19 +03:00
, under = "Debug.log"
}
]
2020-03-07 01:03:37 +03:00
, test "should report Debug.log in an if expression condition" <|
\() ->
2017-06-18 14:43:37 +03:00
testRule "a = if Debug.log a b then True else False"
|> Review.Test.expectErrors
[ Review.Test.error
2019-06-30 12:48:19 +03:00
{ message = message
2019-07-28 01:16:57 +03:00
, details = details
2019-06-30 12:48:19 +03:00
, under = "Debug.log"
}
]
2020-03-07 01:03:37 +03:00
, test "should report Debug.log in an if expression then branch" <|
\() ->
2017-06-18 14:43:37 +03:00
testRule "a = if True then Debug.log a b else False"
|> Review.Test.expectErrors
[ Review.Test.error
2019-06-30 12:48:19 +03:00
{ message = message
2019-07-28 01:16:57 +03:00
, details = details
2019-06-30 12:48:19 +03:00
, under = "Debug.log"
}
]
2020-03-07 01:03:37 +03:00
, test "should report Debug.log in an if expression else branch" <|
\() ->
2017-06-18 14:43:37 +03:00
testRule "a = if True then True else Debug.log a b"
|> Review.Test.expectErrors
[ Review.Test.error
2019-06-30 12:48:19 +03:00
{ message = message
2019-07-28 01:16:57 +03:00
, details = details
2019-06-30 12:48:19 +03:00
, under = "Debug.log"
}
]
2020-03-07 01:03:37 +03:00
, test "should report Debug.log in a case value" <|
\() ->
2017-06-18 14:43:37 +03:00
testRule """
2018-11-06 20:46:46 +03:00
a = case Debug.log a b of
_ -> []
"""
|> Review.Test.expectErrors
[ Review.Test.error
2019-06-30 12:48:19 +03:00
{ message = message
2019-07-28 01:16:57 +03:00
, details = details
2019-06-30 12:48:19 +03:00
, under = "Debug.log"
}
]
2020-03-07 01:03:37 +03:00
, test "should report Debug.log in a case body" <|
\() ->
2017-06-18 14:43:37 +03:00
testRule """
2018-11-06 20:46:46 +03:00
a = case a of
_ -> Debug.log a b
"""
|> Review.Test.expectErrors
[ Review.Test.error
2019-06-30 12:48:19 +03:00
{ message = message
2019-07-28 01:16:57 +03:00
, details = details
2019-06-30 12:48:19 +03:00
, under = "Debug.log"
}
]
2020-03-07 01:03:37 +03:00
, test "should report Debug.log in let declaration section" <|
\() ->
2017-06-18 14:43:37 +03:00
testRule """
2018-11-06 20:46:46 +03:00
a = let b = Debug.log a b
in b
"""
|> Review.Test.expectErrors
[ Review.Test.error
2019-06-30 12:48:19 +03:00
{ message = message
2019-07-28 01:16:57 +03:00
, details = details
2019-06-30 12:48:19 +03:00
, under = "Debug.log"
}
]
2020-03-07 01:03:37 +03:00
, test "should report Debug.log in let body" <|
\() ->
2017-06-18 14:43:37 +03:00
testRule """
2018-11-06 20:46:46 +03:00
a = let b = c
in Debug.log a b
"""
|> Review.Test.expectErrors
[ Review.Test.error
2019-06-30 12:48:19 +03:00
{ message = message
2019-07-28 01:16:57 +03:00
, details = details
2019-06-30 12:48:19 +03:00
, under = "Debug.log"
}
]
, test "should not report calls from a module containing Debug but that is not Debug" <|
\() ->
testRule """
a = Foo.Debug.log 1
b = Debug.Foo.log 1
"""
|> Review.Test.expectNoErrors
2020-03-07 01:03:37 +03:00
, test "should not report the import of the Debug module" <|
\() ->
testRule "import Debug"
2020-03-07 01:03:37 +03:00
|> Review.Test.expectNoErrors
, test "should report the use of `log` when it has been explicitly imported" <|
\() ->
testRule """
import Debug exposing (log)
a = log "" 1
"""
|> Review.Test.expectErrors
[ Review.Test.error
{ message = message
2019-07-28 01:16:57 +03:00
, details = details
2020-03-07 01:03:37 +03:00
, under = "log"
}
2020-03-07 01:03:37 +03:00
|> Review.Test.atExactly { start = { row = 5, column = 5 }, end = { row = 5, column = 8 } }
]
2020-03-07 01:03:37 +03:00
, test "should report the use of `log` when it has been implicitly imported" <|
\() ->
2020-03-07 01:03:37 +03:00
testRule """
import Debug exposing (..)
a = log "" 1
"""
|> Review.Test.expectErrors
[ Review.Test.error
{ message = message
2019-07-28 01:16:57 +03:00
, details = details
2020-03-07 01:03:37 +03:00
, under = "log"
}
]
2020-03-07 01:03:37 +03:00
, test "should not report the use of `log` when it has not been imported" <|
\() ->
testRule """
2020-03-07 01:03:37 +03:00
import Debug exposing (todo)
a = log "" 1
"""
|> Review.Test.expectNoErrors
]
all : Test
all =
2020-03-07 01:03:37 +03:00
describe "NoDebugLog" tests