@@ -2,7 +2,12 @@ module Test.Core.UndesirableFunctions where
22
33import Test.Prelude
44
5+ import Codec.JSON.DecodeError as DecodeError
6+ import Control.Monad.Error.Class (throwError )
7+ import Data.Codec.JSON as CJ
58import Data.Map as Map
9+ import Effect.Exception (error )
10+ import JSON as JSON
611import PureScript.CST.Types (ModuleName (..))
712import Test.Spec (Spec , describe , it )
813import Test.Spec.Assertions (shouldEqual )
@@ -14,9 +19,9 @@ spec = describe "UndesirableFunctions" do
1419
1520 it " Reports bad functions" $
1621 hasViolations
17- [ " 4:5-4:8" /\ " Do not use fn1"
18- , " 5:4-5:7" /\ " Do not use fn2 from Bad.Module"
19- , " 6:4-6:7" /\ " Do not use fn3 from Bad.Module"
22+ [ " 4:5-4:8 Do not use fn1"
23+ , " 5:4-5:7 Do not use fn2 from Bad.Module"
24+ , " 6:4-6:7 Do not use fn3 from Bad.Module"
2025 ]
2126 """
2227 import Some (fn1)
@@ -29,13 +34,13 @@ spec = describe "UndesirableFunctions" do
2934 """
3035
3136 it " Falls back to unqualified message when there is no matching import" do
32- hasViolations [" 1:6-1:9" /\ " Do not use fn3" ] """
37+ hasViolations [" 1:6-1:9 Do not use fn3" ] """
3338 import Some (fn3)
3439 x = y fn3
3540 """
3641
3742 it " Assumes open-imported module if there is only one" do
38- hasViolations [" 2:6-2:9" /\ " Do not use fn3 from Bad.Module" ] """
43+ hasViolations [" 2:6-2:9 Do not use fn3 from Bad.Module" ] """
3944 import Bad.Module
4045 import Another.Module (fn2)
4146 x = y fn3
@@ -49,7 +54,7 @@ spec = describe "UndesirableFunctions" do
4954 """
5055
5156 it " Falls back to unqualified message when the import is open" do
52- hasViolations [" 1:6-1:9" /\ " Do not use fn3" ] """
57+ hasViolations [" 1:6-1:9 Do not use fn3" ] """
5358 import Some
5459 x = y fn3
5560 """
@@ -62,8 +67,8 @@ spec = describe "UndesirableFunctions" do
6267
6368 it " Reports when imported with a qualifier" do
6469 hasViolations
65- [ " 2:6-2:13" /\ " Do not use fn2 from Bad.Module"
66- , " 3:6-3:13" /\ " Do not use fn3 from Bad.Module"
70+ [ " 2:6-2:13 Do not use fn2 from Bad.Module"
71+ , " 3:6-3:13 Do not use fn3 from Bad.Module"
6772 ]
6873 """
6974 import Bad.Module as FN2
@@ -72,6 +77,18 @@ spec = describe "UndesirableFunctions" do
7277 y = z FN3.fn3
7378 """
7479
80+ it " Picks correct message according to the import" do
81+ hasViolations
82+ [ " 2:6-2:9 Do not use fn2 from Bad.Module"
83+ , " 3:6-3:12 Do not use fn2 from Another.Module"
84+ ]
85+ """
86+ import Bad.Module (fn2)
87+ import Another.Module as AM
88+ x = y fn2
89+ y = z AM.fn2
90+ """
91+
7592 it " Allows imports from other modules" do
7693 hasViolations [] """
7794 import Innocent.Module (fn2)
@@ -83,7 +100,43 @@ spec = describe "UndesirableFunctions" do
83100 x = y IM.fn2
84101 """
85102
103+ describe " Config parsing" do
104+ it " parses correctly" do
105+ json <- unsafeRight $ JSON .parse """
106+ {
107+ "functions": {
108+ "fn1": "Do not use fn1",
109+ "Bad.fn1": "Do not use fn1 from Bad",
110+ "Bad.Module.fn1": "Do not use fn1 from Bad.Module",
111+ "Bad.fn2": "Do not use fn2 from Bad",
112+ "Bad.Module.fn2": "Do not use fn2 from Bad.Module"
113+ }
114+ }
115+ """
116+
117+ parsed <- unsafeRight $ lmap DecodeError .print $
118+ CJ .decode UndesirableFunctions .codec json
119+ <#> Map .toUnfoldable
120+ <#> map \({ function } /\ m) ->
121+ function /\ (Map .toUnfoldable m <#> \(mn /\ msg) -> (unwrap <$> mn) /\ msg)
122+
123+ parsed `shouldEqual`
124+ [ " fn1" /\
125+ [ Nothing /\ " Do not use fn1"
126+ , Just " Bad" /\ " Do not use fn1 from Bad"
127+ , Just " Bad.Module" /\ " Do not use fn1 from Bad.Module"
128+ ]
129+ , " fn2" /\
130+ [ Just " Bad" /\ " Do not use fn2 from Bad"
131+ , Just " Bad.Module" /\ " Do not use fn2 from Bad.Module"
132+ ]
133+ ]
86134 where
135+ unsafeRight :: ∀ a . Either String a -> _ a
136+ unsafeRight = case _ of
137+ Left e -> throwError $ error $ " Failed to parse config: " <> e
138+ Right a -> pure a
139+
87140 config = Map .fromFoldable
88141 [ { function: " fn1" } /\ Map .singleton
89142 Nothing " Do not use fn1"
@@ -102,5 +155,5 @@ spec = describe "UndesirableFunctions" do
102155 { rule: UndesirableFunctions .rule config
103156 , module: mod
104157 }
105- <#> map (\v -> fromMaybe " " (formatRange <$> v.source) /\ v.message)
158+ <#> map (\v -> fromMaybe " " (formatRange <$> v.source) <> " " <> v.message)
106159 >>= shouldEqual vs
0 commit comments