packages feed

tilia-0.2.0.0: tests/Tilia/CorpusSpec.hs

{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedLabels #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PatternSynonyms #-}

-- | Formatting other people's Haskell.
module Tilia.CorpusSpec (spec) where

import Control.Exception (SomeException, evaluate, try)
import Control.Monad (foldM, join, unless, (<=<))
import Data.ByteString qualified as BS
import Data.Choice (isTrue, pattern Do, pattern Don't)
import Data.Foldable (for_)
import Data.Functor ((<&>))
import Data.IORef (IORef, atomicModifyIORef', newIORef, readIORef)
import Data.Map.Strict qualified as Map
import Data.Maybe (isJust)
import Data.Set qualified as Set
import Data.Text (Text)
import Data.Text qualified as T
import Data.Text.Encoding (decodeUtf8')
import System.Directory
  ( copyFile,
    createDirectoryIfMissing,
    doesFileExist,
    removePathForcibly,
  )
import System.Environment (lookupEnv)
import System.FilePath (replaceExtension, takeDirectory, (</>))
import System.IO (stdout)
import System.IO.Temp (withSystemTempDirectory)
import Test.Hspec hiding (Example, after, before, example)

import Tilia.Corpus
import Tilia.Corpus.Manifest
import Tilia.Cpp (countLeaves, describeCppError, usesCpp)
import Tilia.Diff qualified as Diff
import Tilia.Format
  ( FormatError (..),
    describeFormatError,
    formatErrorExitCode,
    refused,
  )
import Tilia.Palette (Color (Meta), Palette (Plain), paint, paletteFor)
import Tilia.Parser (ParseError (..))
import Tilia.Pragma (Defaults)
import Tilia.Settings (Settings, alternativeSettings, defaultSettings)
import Tilia.Span (spanStartColumn, spanStartLine)
import Tilia.Span.Ghc (spanOfSrcSpan)
import Tilia.TestConfig
  ( configurationsToCheck,
    effectiveExtensionsFor,
    formatExample,
  )
import Tilia.TestUtils
  ( settingsToTest,
    switchedOn,
    withEnvironment,
  )
import Tilia.Utils (tshow, writeUtf8File)

spec :: Spec
spec = do
  testedSettings <-
    runIO (filter ((/= defaultSettings) . snd) <$> settingsToTest)
  describe "obtaining Hackage releases" $
    it "takes in only the releases listed, whatever else is in the cache" $
      withSystemTempDirectory "tilia-corpus" $ \cache -> do
        for_ ["listed-1.0", "unlisted-1.0"] $ \release -> do
          createDirectoryIfMissing True (cache </> "hackage" </> release)
          writeFile
            (cache </> "hackage" </> release </> "M.hs")
            "module M where\n"
        obtained <-
          withEnvironment [("TILIA_CORPUS_DIR", cache)] . obtain $
            hackagePackages{corpusSource = HackageReleases ["listed-1.0"]}
        fmap (fmap exampleName) obtained `shouldBe` Right ["listed-1.0/M.hs"]
  corpusSpec testedSettings vendoredExamples
  corpusSpec testedSettings ghcTestSuite
  corpusSpec testedSettings hackagePackages

-- | Every example of one corpus.
corpusSpec ::
  -- | The settings other than the defaults to check it with, each with its
  -- name.
  [(String, Settings)] ->
  -- | The corpus.
  Corpus ->
  Spec
corpusSpec testedSettings corpus =
  describe (corpusName corpus) $
    runIO (obtain corpus) >>= \case
      Left problem ->
        it "is available" . expectationFailure $
          "corpus not on this machine and could not be fetched: "
            <> T.unpack problem
      Right examples -> do
        palette <- runIO (paletteFor stdout)
        dumpExample <- runIO (dumpExampleForCorpus (corpusName corpus))
        accepting <- runIO (switchedOn "TILIA_VENDORED_ACCEPT")
        let answered = case corpusReference corpus of
              ReferenceMarked mark | accepting -> answersWritten mark
              _ -> pure
        case corpusExpectations corpus of
          Listed lists ->
            againstLists
              lists
              (check palette dumpExample defaults testedSettings <=< answered)
              examples
          -- Plain, so that what a record says does not depend on whether the
          -- run that wrote it printed to a terminal.
          Recorded path -> do
            let report = replaceExtension path ".report"
            describe (fst defaults) $
              againstRecord
                (path, report)
                (check Plain dumpExample defaults unrecorded)
                examples
            describe (fst alternative) $
              againstRecord
                (path <> "'", report <> "'")
                (check Plain dumpExample alternative [])
                examples
  where
    defaults = ("the default settings", defaultSettings)
    alternative = ("the alternative settings", alternativeSettings)
    unrecorded = filter ((/= alternativeSettings) . snd) testedSettings

-- | A corpus small enough to name its exceptions in "Tilia.Corpus".
againstLists :: Lists -> (Example -> IO Result) -> [Example] -> Spec
againstLists listed run examples = do
  parallel $ for_ examples $ \example ->
    it (exampleName example) $ do
      result <- run example
      let name = exampleName example
      case verdict
        (Set.member name unreadable)
        (Map.lookup name declines)
        result of
        Passes -> pure ()
        Fails why -> expectationFailure (T.unpack why)
        Reserved why -> pendingWith (T.unpack why)
  it "lists nothing it does not have" $ do
    let gone = filter (`Set.notMember` had) listedNames
    unless (null gone) . expectationFailure $
      "the corpus lists examples it does not have: " <> unwords gone
  where
    unreadable = Set.fromList (expectUnreadable listed)
    declines = Map.fromList (expectDeclined listed)
    listedNames = expectUnreadable listed <> Map.keys declines
    had = Set.fromList (fmap exampleName examples)

-- | A corpus checked against a generated record of what it does.
againstRecord ::
  -- | The manifest of the record and its report.
  (FilePath, FilePath) ->
  -- | How to check an example.
  (Example -> IO Result) ->
  -- | The examples.
  [Example] ->
  Spec
againstRecord (path, reportPath) run examples = do
  accept <- runIO (switchedOn "TILIA_HACKAGE_ACCEPT")
  if accept then accepted else checked
  where
    accepted = do
      seen <- runIO (newIORef [])
      afterAll_ (record seen) $
        parallel $
          for_ examples $ \example ->
            it (exampleName example) $ do
              Result outcome why digest _ <- run example
              note
                seen
                ( exampleName example,
                  Entry outcome digest,
                  T.take reasonLength why
                )

    record seen = do
      noted <- readIORef seen
      if length noted /= length examples
        then
          putStrLn $
            "not writing "
              <> path
              <> ": "
              <> show (length noted)
              <> " of "
              <> show (length examples)
              <> " examples ran, so this run does not know what the rest do."
              <> " Regenerate without --match."
        else do
          writeManifest path (Map.fromList [(n, e) | (n, e, _) <- noted])
          writeReport reportPath [(n, entryOutcome e, w) | (n, e, w) <- noted]

    note :: IORef [a] -> a -> IO ()
    note seen entry = atomicModifyIORef' seen (\es -> (entry : es, ()))

    checked = do
      manifest <- runIO (readManifest path)
      report <- runIO (readReport reportPath)
      parallel $ for_ examples $ \example ->
        it (exampleName example) $ do
          Result outcome why digest _ <- run example
          case Map.lookup (exampleName example) manifest of
            Nothing -> expectationFailure (T.unpack (unrecorded outcome))
            Just expected
              | entryOutcome expected /= outcome ->
                  expectationFailure
                    (T.unpack (moved (entryOutcome expected) outcome why))
              | entryDigest expected /= digest ->
                  expectationFailure
                    (T.unpack (rewritten (entryDigest expected) digest))
              | Just problem <-
                  misreported
                    (Map.lookup (exampleName example) report)
                    outcome
                    (reasonLines (T.take reasonLength why)) ->
                  expectationFailure (T.unpack problem)
              | otherwise -> case outcome of
                  Formatted -> pure ()
                  Declined -> pure ()
                  DoesNotParse -> pure ()
                  NotUtf8 -> pure ()
                  PartlyChecked -> pendingWith (T.unpack why)
                  Broken -> pendingWith (T.unpack (T.take reasonLength why))
      it "records nothing it does not have" $ do
        let gone = [n | n <- Map.keys manifest, not (Set.member n had)]
        unless (null gone) . expectationFailure $
          show (length gone)
            <> " entries name examples this corpus does not have, starting with "
            <> unwords (take 5 gone)
            <> regenerate
      it "reports nothing it does not have" $ do
        let gone = [n | n <- Map.keys report, not (Set.member n had)]
        unless (null gone) . expectationFailure $
          reportPath
            <> " gives reasons for "
            <> show (length gone)
            <> " examples this corpus does not have, starting with "
            <> unwords (take 5 gone)
            <> regenerate

    had = Set.fromList (fmap exampleName examples)

    misreported filed outcome reason = case (outcome, filed) of
      (Formatted, Nothing) -> Nothing
      (Formatted, Just _) ->
        Just
          ( T.pack reportPath
              <> " gives a reason why this is not formatted, and it is"
              <> T.pack regenerate
          )
      (_, Just (filedUnder, given))
        | filedUnder == outcome && given == reason -> Nothing
      _ ->
        Just $
          T.pack reportPath
            <> " says otherwise why this "
            <> outcomeName outcome
            <> ", which is now:\n"
            <> T.unlines reason
            <> T.pack regenerate

    unrecorded outcome =
      "this is not in "
        <> T.pack path
        <> ", and it "
        <> outcomeName outcome
        <> T.pack regenerate

    rewritten was now =
      T.pack path
        <> " says this comes out as "
        <> was
        <> ", and it comes out as "
        <> now
        <> T.pack regenerate

    moved expected outcome why =
      T.pack path
        <> " says this "
        <> outcomeName expected
        <> ", and it "
        <> outcomeName outcome
        <> (if T.null why then "" else ": " <> why)
        <> T.pack regenerate

    regenerate =
      "\n\nIf that is the intended change, regenerate the record:"
        <> "\n    TILIA_HACKAGE_ACCEPT=1 cabal test"

-- | How much of an example's reason a record keeps.
reasonLength :: Int
reasonLength = 2000

----------------------------------------------------------------------------
-- Checking one example

-- | What running the formatter over one example established, and why.
data Result = Result
  { -- | The outcome.
    resultOutcome :: Outcome,
    -- | Explanation in text.
    resultWhy :: Text,
    -- | A digest of what the formatter wrote, or
    -- 'Tilia.Corpus.Manifest.noDigest' where it wrote nothing.
    resultDigest :: Text,
    -- | The exit code formatting gave up with, where it gave up.
    resultExitCode :: Maybe Int
  }

-- | What the runner should do about what one example produced.
data Verdict
  = -- | Nothing to report.
    Passes
  | -- | Something is wrong, and this is what.
    Fails Text
  | -- | Neither: everything that was asked of it held, and something worth
    -- asking went unasked. Reported rather than passed over, so that the
    -- number of examples whose properties were only partly established is
    -- visible in the summary instead of implied by its absence.
    Reserved Text

-- | What is to be said about what one example produced.
verdict ::
  -- | Does the corpus say this one cannot be read?
  Bool ->
  -- | The exit code the corpus says this one is declined with, if it says.
  Maybe Int ->
  -- | What the example produced.
  Result ->
  Verdict
verdict True _ (Result outcome _ _ _)
  | outcome `elem` [DoesNotParse, NotUtf8] = Passes
  | otherwise = Fails "the corpus says this cannot be read, and it was read"
verdict False declines result@(Result outcome why _ _) = case outcome of
  Broken -> Fails why
  NotUtf8 -> Fails (unlisted "is not UTF-8")
  DoesNotParse -> Fails (unlisted ("does not parse, at " <> why))
  Declined
    | Just expected <- declines,
      resultExitCode result /= Just expected ->
        Fails
          ( "the corpus says this is declined with exit code "
              <> tshow expected
              <> ", and it is declined with "
              <> maybe "none" tshow (resultExitCode result)
              <> ": "
              <> why
          )
    | Just _ <- declines -> Passes
    | otherwise ->
        Fails
          "the formatter declined this, and the corpus does not say it should"
  PartlyChecked
    | Just _ <- declines -> Fails wasNotDeclined
    | otherwise -> Reserved why
  Formatted
    | Just _ <- declines -> Fails wasNotDeclined
    | otherwise -> Passes
  where
    wasNotDeclined = "the corpus says this should be declined, and it was not"
    unlisted what =
      "this "
        <> what
        <> ", and the corpus does not list it under expectUnreadable"

-- | What formatting one example, with every check, establishes, with some
-- settings and then, where it formats with them, with each of the others.
check ::
  -- | How to color the diffs it reports.
  Palette ->
  -- | What to do with the output, given the name of the settings.
  Dump ->
  -- | The settings to check it with first, with their name.
  (String, Settings) ->
  -- | The other settings to check it with, each with its name.
  [(String, Settings)] ->
  -- | The example.
  Example ->
  IO Result
check palette dumpExample (primaryName, primary) others example = do
  source <- readUtf8 (exampleInput example)
  expected <- join <$> traverse readAnswer (exampleReference example)
  expectedAlternatively <-
    join <$> traverse readAnswer (exampleAlternativeReference example)
  let referenceFor settings
        | settings == defaultSettings = expected
        | settings == alternativeSettings = expectedAlternatively
        | otherwise = Nothing
  answering (exampleReference example) (isJust expected) <$> case source of
    Nothing -> pure (Result NotUtf8 "" noDigest Nothing)
    Just text -> guarded $ do
      let package = exampleExtensions example
          format checking settings =
            formatExample
              checking
              settings
              package
              (exampleOwnModules example)
              (exampleName example)
          unchecked = do
            printed <- format (Don't #check) primary text
            (printed,)
              <$> either
                (pure . Left)
                (format (Don't #check) primary)
                printed
          alternatively result (name, settings)
            | resultOutcome result `elem` [Formatted, PartlyChecked] =
                format (Do #check) settings text >>= dumpExample name example <&> \case
                  Right formatted
                    | Just reference <- referenceFor settings,
                      reference /= formatted ->
                        broken
                          ( "does not match the corpus's expected output\n"
                              <> diff
                                palette
                                ("expected", "ours")
                                reference
                                formatted
                          )
                    | otherwise -> result
                  Left e -> broken (describeFormatError Plain e)
            | otherwise = pure result
            where
              broken why =
                result
                  { resultOutcome = Broken,
                    resultWhy = "with " <> T.pack name <> ": " <> why
                  }
      checked <-
        format (Do #check) primary text >>= dumpExample primaryName example
      passes <- case checked of
        Left NotEquivalent{} -> Just <$> unchecked
        Left NotIdempotent{} -> Just <$> unchecked
        _ -> pure Nothing
      foldM
        alternatively
        (checkPure palette package text (referenceFor primary) checked passes)
        others

-- | A result that also fails an example whose answer is missing, or
-- whose answer is left from when it formatted.
answering ::
  -- | Where the corpus keeps the example's answer, if it keeps one.
  Maybe FilePath ->
  -- | Is the answer there?
  Bool ->
  -- | The result.
  Result ->
  Result
answering kept there result = case kept of
  Just path
    | not there,
      resultOutcome result `elem` [Formatted, PartlyChecked] ->
        broken
          ( T.pack path
              <> " is missing; TILIA_VENDORED_ACCEPT=1 cabal test writes it"
          )
    | there,
      resultOutcome result `elem` [Declined, DoesNotParse, NotUtf8] ->
        broken
          ( T.pack path
              <> " says what this formats to, and it does not format: "
              <> resultWhy result
          )
  _ -> result
  where
    broken why = result{resultOutcome = Broken, resultWhy = why}

-- | What to do with the output, given the name of the settings.
type Dump =
  String -> Example -> Either FormatError Text -> IO (Either FormatError Text)

-- | Write what each example of a corpus comes out as, or why it does not,
-- into the directory @TILIA_CORPUS_DUMP@ names, by the settings, the
-- corpus and the example, and the example itself under @input@.
dumpExampleForCorpus :: String -> IO Dump
dumpExampleForCorpus corpus =
  lookupEnv "TILIA_CORPUS_DUMP" <&> \case
    Just dir@(_ : _) -> \settings example result -> do
      let path = dir </> settings </> corpus </> exampleName example
          input = dir </> "input" </> corpus </> exampleName example
      for_ [path, input] (createDirectoryIfMissing True . takeDirectory)
      copyFile (exampleInput example) input
      case result of
        Right formatted -> writeUtf8File path formatted
        Left e -> writeUtf8File (path <> ".err") (describeFormatError Plain e)
      pure result
    _ -> \_ _ -> pure

-- | Write what an example comes out as with the default and the
-- alternative settings where the corpus keeps its answers, where it comes
-- out with both and passes every check, and point it at them.
answersWritten ::
  -- | What the corpus marks its answers with.
  String ->
  -- | The example.
  Example ->
  IO Example
answersWritten mark example =
  readUtf8 (exampleInput example) >>= \case
    Nothing -> pure example
    Just source -> do
      let format settings =
            formatExample
              (Do #check)
              settings
              (exampleExtensions example)
              (exampleOwnModules example)
              (exampleName example)
              source
      outputs <- (,) <$> format defaultSettings <*> format alternativeSettings
      case outputs of
        (Right formatted, Right alternatively) -> do
          writeUtf8File plain formatted
          primed' <-
            if alternatively == formatted
              then plain <$ removePathForcibly primed
              else primed <$ writeUtf8File primed alternatively
          pure
            example
              { exampleReference = Just plain,
                exampleAlternativeReference = Just primed'
              }
        _ -> pure example
  where
    plain = answerFile mark (exampleInput example)
    primed = answerFile (mark <> "'") (exampleInput example)

-- | Read a file that is supposed to be a Haskell module.
readUtf8 :: FilePath -> IO (Maybe Text)
readUtf8 path = either (const Nothing) Just . decodeUtf8' <$> BS.readFile path

-- | Read what an example is answered with, where it has been written.
readAnswer :: FilePath -> IO (Maybe Text)
readAnswer path =
  doesFileExist path >>= \case
    True -> readUtf8 path
    False -> pure Nothing

-- | A unified diff, cut short after 60 lines, past which it stops being
-- read.
diff :: Palette -> (Text, Text) -> Text -> Text -> Text
diff palette names before after
  | length body > room =
      T.intercalate "\n" (heading <> take room body <> [omitted])
  | otherwise = whole
  where
    whole = Diff.diff palette names before after
    (heading, body) = splitAt 2 (T.lines whole)
    room = 60
    omitted =
      paint palette Meta $
        "… and " <> T.pack (show (length body - room)) <> " more lines"

-- | Run a check, turning a crash into a failure rather than into a dead test
-- run.
guarded :: IO Result -> IO Result
guarded run =
  try (run >>= evaluate . forced) >>= \case
    Left (e :: SomeException) ->
      pure
        ( Result
            Broken
            ("the formatter raised an error: " <> firstLine (T.pack (show e)))
            noDigest
            Nothing
        )
    Right settled -> pure settled
  where
    forced r =
      resultOutcome r
        `seq` T.length (resultWhy r)
        `seq` T.length (resultDigest r)
        `seq` r
    firstLine = T.strip . T.takeWhile (/= '\n')

-- | What a refusal or a failure to format an example comes to.
gaveUp :: FormatError -> Result
gaveUp e = case e of
  NotParsed problem -> given DoesNotParse (parseProblem problem)
  CppUnsupported _ why -> given Declined (describeCppError why)
  _
    | refused e -> given Declined (describeFormatError Plain e)
    | otherwise -> given Broken (describeFormatError Plain e)
  where
    given outcome why =
      Result outcome why noDigest (Just (formatErrorExitCode e))

-- | Everything that can be established about one example from formatting
-- it with every check.
checkPure ::
  -- | How to color the diffs it reports.
  Palette ->
  -- | What the package around it puts in force, if it is a module of one.
  Maybe Defaults ->
  -- | The example.
  Text ->
  -- | The expected output, if the corpus has one.
  Maybe Text ->
  -- | Formatting the example, checked.
  Either FormatError Text ->
  -- | Where a check failed, formatting the example and then what that
  -- printed, unchecked.
  Maybe (Either FormatError Text, Either FormatError Text) ->
  Result
checkPure palette package source expected checked unchecked =
  case (checked, unchecked) of
    (Right formatted, _)
      | Just reference <- expected,
        reference /= formatted ->
          told
            formatted
            Broken
            ( "does not match the corpus's expected output\n"
                <> diff palette ("expected", "ours") reference formatted
            )
      | Just went <- beyond ->
          told
            formatted
            PartlyChecked
            ( count went
                <> " configurations is more than the "
                <> count (toInteger configurationsToCheck)
                <> " this checks, so only the ones varying a single"
                <> " conditional were compared"
            )
      | otherwise -> told formatted Formatted ""
    (Left e@NotEquivalent{}, Just (Right printed, _)) ->
      told
        printed
        Broken
        ( describeFormatError Plain e
            <> "\n"
            <> diff palette ("input", "output") source printed
        )
    (Left e@NotIdempotent{}, Just (Right printed, Right settled)) ->
      told
        printed
        Broken
        ( describeFormatError Plain e
            <> "\n"
            <> diff palette ("first pass", "second pass") printed settled
        )
    (Left e, _) -> gaveUp e
  where
    told formatted outcome why =
      Result outcome why (digestOf formatted) Nothing
    count :: Integer -> Text
    count = T.pack . show
    beyond = case countLeaves source of
      Right went
        | isTrue (usesCpp (effectiveExtensionsFor package source) source),
          went > toInteger configurationsToCheck ->
            Just went
      _ -> Nothing

-- | Why a parse failed, and where in the file, but not which file.
--
-- The example being reported already names it, and 'describeParseError'
-- opens with the path in full.
parseProblem :: ParseError -> Text
parseProblem problem = at <> peProblem problem
  where
    at = case spanOfSrcSpan (peSpan problem) of
      Nothing -> T.empty
      Just s ->
        T.pack (show (spanStartLine s))
          <> ":"
          <> T.pack (show (spanStartColumn s))
          <> ": "