packages feed

himari-1.1.6.4: test/Spec/HlintReexportSpec.hs

module Spec.HlintReexportSpec (tests) where

import Data.Set qualified as Set
import Data.Text qualified as T
import Himari
import HlintRunner
import Test.Sandwich

tests :: TopSpec
tests = do
  describe "hlint rules for symbols re-exported through himari" $ do
    describe "rules in .hlint.yaml" . withRestrictedFunctionNames $ do
      it "rules for Himari should be exactly the tested functions" $ do
        names <- getContext restrictedFunctionNames
        rulesThrough "Himari" names `shouldBe` reexportedFunctionNames

      it "rules for Himari.Prelude should be exactly the tested functions" $ do
        names <- getContext restrictedFunctionNames
        rulesThrough "Himari.Prelude" names `shouldBe` reexportedFunctionNames

      it "rules for Himari.SafePrelude should leave out the functions it lacks" $ do
        names <- getContext restrictedFunctionNames
        rulesThrough "Himari.SafePrelude" names
          `shouldBe` (reexportedFunctionNames `Set.difference` preludeOnlyFunctionNames)

    describe "import Himari" . withSample "ReexportHimari.hs" $ do
      describe "every restricted function re-exported by Himari should warn" $ do
        forM_ reexportedRestrictedFunctions $ \(declName, function, fragment) ->
          it (convert function <> " should warn with its own message") $
            shouldWarnFunction declName function fragment

      describe "functions restricted by a rule without a module name should warn" $ do
        it "throwString should warn" $
          shouldWarnFunction "useThrowString" "throwString" "typed exceptions with throwM"

      describe "functions of both Data.List and Data.Foldable should have a single message" $ do
        it "every function should be among the tested functions" $
          fmap snd listAndFoldableFunctions `shouldBe` listAndFoldableFunctionNames

        forM_ listAndFoldableFunctions $ \(declName, function) ->
          it (convert function <> " should not have concatenated messages") $
            shouldWarnFunctionOnce declName function "Partial:"

      describe "total alternatives should NOT warn" $ do
        it "headMay should NOT warn" $
          shouldNotWarnFunctionIn "useHeadMay"

        it "fromMaybe should NOT warn" $
          shouldNotWarnFunctionIn "useFromMaybe"

      describe "UnliftIO versions exported by Himari should NOT warn" $ do
        forM_ unliftioDeclarations $ \declName ->
          it (convert declName <> " should NOT warn") $
            shouldNotWarnFunctionIn declName

    describe "import Himari.Prelude" . withSample "ReexportHimariPrelude.hs" $ do
      it "head should warn" $
        shouldWarnFunction "useHead" "head" "headMay"

      it "foldl1 should warn with a single message" $
        shouldWarnFunctionOnce "useFoldl1" "foldl1" "Partial:"

      it "bifoldl1 should warn" $
        shouldWarnFunction "useBifoldl1" "bifoldl1" "bifoldl with a default value"

      it "fromJust should warn" $
        shouldWarnFunction "useFromJust" "fromJust" "fromMaybe"

      it "readFile should warn" $
        shouldWarnFunction "useReadFile" "readFile" "Lazy IO"

      it "singular should warn" $
        shouldWarnFunction "useSingular" "singular" "(^?)"

      it "stringException should warn" $
        shouldWarnFunction
          "useStringException"
          "stringException"
          "Define a proper exception type"

      it "headMay should NOT warn" $
        shouldNotWarnFunctionIn "useHeadMay"

      it "catch of UnliftIO should NOT warn" $
        shouldNotWarnFunctionIn "useCatch"

    describe "import Himari.SafePrelude" . withSample "ReexportSafePrelude.hs" $ do
      it "head should warn" $
        shouldWarnFunction "useHead" "head" "headMay"

      it "foldl1 should warn with a single message" $
        shouldWarnFunctionOnce "useFoldl1" "foldl1" "Partial:"

      it "bifoldl1 should warn" $
        shouldWarnFunction "useBifoldl1" "bifoldl1" "bifoldl with a default value"

      it "fromJust should warn" $
        shouldWarnFunction "useFromJust" "fromJust" "fromMaybe"

      it "readFile should warn" $
        shouldWarnFunction "useReadFile" "readFile" "Lazy IO"

      it "singular should warn" $
        shouldWarnFunction "useSingular" "singular" "(^?)"

      it "headMay should NOT warn" $
        shouldNotWarnFunctionIn "useHeadMay"

    describe "import forms of Himari" . withSample "ReexportImportForms.hs" $ do
      it "head qualified with the alias of Himari should warn" $
        shouldWarnFunction "useQualifiedHead" "H.head" "headMay"

      it "readFile qualified with the alias of Himari should warn" $
        shouldWarnFunction "useQualifiedReadFile" "H.readFile" "Lazy IO"

      it "tail should warn even though other names are hidden" $
        shouldWarnFunction "useTail" "tail" "tailMay"

      it "head of Data.List.NonEmpty should NOT warn when hidden from Himari" $
        shouldNotWarnFunctionIn "useNonEmptyHead"

      it "head qualified with Data.List.NonEmpty should NOT warn" $
        shouldNotWarnFunctionIn "useNonEmptyHeadQualified"

      it "definition of a user function named last should NOT warn" $
        shouldNotWarnFunctionIn "last"

      it "use of a user function named last should NOT warn" $
        shouldNotWarnFunctionIn "useOwnLast"

    describe "explicit import list of Himari" . withSample "ReexportExplicitImport.hs" $ do
      it "head should warn" $
        shouldWarnFunction "useHead" "head" "headMay"

      it "readFile should warn" $
        shouldWarnFunction "useReadFile" "readFile" "Lazy IO"

      it "headMay should NOT warn" $
        shouldNotWarnFunctionIn "useHeadMay"

-- | Label of the names shared by 'withRestrictedFunctionNames'.
restrictedFunctionNames :: Label "restrictedFunctionNames" [Text]
restrictedFunctionNames = Label

-- | Read the names of the functions restricted by @.hlint.yaml@ once
-- and share them among the tests.
withRestrictedFunctionNames
  :: SpecFree (LabelValue "restrictedFunctionNames" [Text] :> context) IO ()
  -> SpecFree context IO ()
withRestrictedFunctionNames =
  introduce
    "restricted function names of .hlint.yaml"
    restrictedFunctionNames
    loadRestrictedFunctionNames
    (const (pure ()))

-- | Module qualified names of the rules for the functions re-exported through the module.
-- Rules for the modules below it, such as @Himari.Prelude.head@ for @Himari@, are left out.
-- This does not support operators that contain a dot.
rulesThrough :: Text -> [Text] -> Set.Set Text
rulesThrough reexporter ruleNames =
  Set.fromList
    [ function
    | ruleName <- ruleNames
    , Just function <- [T.stripPrefix (reexporter <> ".") ruleName]
    , not ("." `T.isInfixOf` function)
    ]

-- | Names of the functions in 'reexportedRestrictedFunctions'.
reexportedFunctionNames :: Set.Set Text
reexportedFunctionNames =
  Set.fromList [function | (_, function, _) <- reexportedRestrictedFunctions]

-- | Functions re-exported by "Himari.Prelude" but not by "Himari.SafePrelude".
preludeOnlyFunctionNames :: Set.Set Text
preludeOnlyFunctionNames = Set.fromList ["stringException"]

-- | Declarations of the sample paired with the restricted function they use
-- and a fragment of the message of its rule.
-- These are all the functions that have a rule qualified with a module of himari in @.hlint.yaml@,
-- which is verified against @.hlint.yaml@ by the tests.
-- They are re-exported by "Himari" as the very same entity as the original one.
reexportedRestrictedFunctions :: [(Text, Text, Text)]
reexportedRestrictedFunctions =
  [ ("useHead", "head", "headMay")
  , ("useTail", "tail", "tailMay")
  , ("useInit", "init", "initMay")
  , ("useLast", "last", "lastMay")
  , ("useCycle", "cycle", "cycleMay")
  , ("useScanl1", "scanl1", "scanl1May")
  , ("useScanr1", "scanr1", "scanr1May")
  , ("useFoldl1", "foldl1", "foldl1May from Safe.Foldable")
  , ("useFoldr1", "foldr1", "foldr1May from Safe.Foldable")
  , ("useMaximum", "maximum", "maximumMay or maximumBound from Safe.Foldable")
  , ("useMinimum", "minimum", "minimumMay or minimumBound from Safe.Foldable")
  , ("useMaximumBy", "maximumBy", "maximumByMay or maximumBoundBy from Safe.Foldable")
  , ("useMinimumBy", "minimumBy", "minimumByMay or minimumBoundBy from Safe.Foldable")
  , ("useBifoldl1", "bifoldl1", "bifoldl with a default value")
  , ("useBifoldr1", "bifoldr1", "bifoldr with a default value")
  , ("useBimaximum", "bimaximum", "bifoldr with explicit initial value")
  , ("useBiminimum", "biminimum", "bifoldr with explicit initial value")
  , ("useBimaximumBy", "bimaximumBy", "bifoldr with explicit initial value")
  , ("useBiminimumBy", "biminimumBy", "bifoldr with explicit initial value")
  , ("useFromJust", "fromJust", "fromMaybe")
  , ("useGetChar", "getChar", "Encoding depends on LANG")
  , ("useGetLine", "getLine", "Encoding depends on LANG")
  , ("useGetContents", "getContents", "Use strict Text or ByteString")
  , ("useInteract", "interact", "Lazy IO + env-dependent encoding.")
  , ("useReadIO", "readIO", "partial (uses read)")
  , ("useReadLn", "readLn", "partial (uses read)")
  , ("usePutChar", "putChar", "Encoding depends on LANG")
  , ("usePutStr", "putStr", "Encoding depends on LANG")
  , ("usePutStrLn", "putStrLn", "Encoding depends on LANG")
  , ("usePrint", "print", "Encoding depends on LANG")
  , ("useReadFile", "readFile", "Data.Text.IO.readFile")
  , ("useWriteFile", "writeFile", "Data.Text.IO.writeFile")
  , ("useAppendFile", "appendFile", "Data.Text.IO.appendFile")
  , ("useSingular", "singular", "(^?)")
  , ("useUnsafeSingular", "unsafeSingular", "0 or >1 elements")
  , ("useUnsafePartsOf", "unsafePartsOf", "list length changes")
  , ("useIunsafePartsOf", "iunsafePartsOf", "list length changes")
  , ("useStringException", "stringException", "Define a proper exception type")
  ]

-- | Functions that both "Data.List" and "Data.Foldable" export as the very same entity.
-- Both modules have a rule for them, so their messages are prone to be concatenated.
-- The declarations that use them are looked up in 'reexportedRestrictedFunctions'.
listAndFoldableFunctions :: [(Text, Text)]
listAndFoldableFunctions =
  [ (declName, function)
  | (declName, function, _) <- reexportedRestrictedFunctions
  , function `elem` listAndFoldableFunctionNames
  ]

-- | Names of the functions in 'listAndFoldableFunctions'.
listAndFoldableFunctionNames :: [Text]
listAndFoldableFunctionNames =
  [ "foldl1"
  , "foldr1"
  , "maximum"
  , "minimum"
  , "maximumBy"
  , "minimumBy"
  ]

-- | Declarations of the sample that use the UnliftIO version of a name
-- whose non-UnliftIO version has a rule in @.hlint.yaml@.
unliftioDeclarations :: [Text]
unliftioDeclarations =
  [ "useCatch"
  , "useBracket"
  , "useFinally"
  , "useTry"
  , "useWithMVar"
  , "useModifyMVar"
  , "useTimeout"
  , "useMyThreadId"
  , "useDoesFileExist"
  ]