<commit_before>
module Main where

import qualified Test.Tasty as T
import qualified Test.Tasty.Golden as G
import System.IO (hPutStrLn, stderr)
import System.FilePath ((</>), (<.>))
import System.FilePath.Glob (glob)
import qualified System.Directory as Dir
import qualified System.Process as Proc
import CabalBounds.Args
import CabalBounds.Main (cabalBounds)
import CabalBounds.VersionComp (VersionComp(..))
import Control.Monad (when)
import Control.Exception (finally)

main :: IO ()
main = ensureSetupConfig >> T.defaultMain tests


ensureSetupConfig :: IO ()
ensureSetupConfig = do
   configExists <- Dir.doesFileExist $ "tests" </> "inputFiles" </> "setup-config"
   when (not configExists) $ do
      curDir <- Dir.getCurrentDirectory
      buildSetupConfig curDir `finally` cleanUp curDir
   where
      buildSetupConfig curDir = do
         let inputFiles = curDir </> "tests" </> "inputFiles"
         let buildDir   = inputFiles </> "setup-config-build-env"
         let libsDir    = buildDir </> "libs"
         Dir.setCurrentDirectory buildDir

         Proc.runCommand "cabal sandbox init" >>= Proc.waitForProcess
         Proc.runCommand ("cabal sandbox add-source " ++ (libsDir </> "A")) >>= Proc.waitForProcess
         Proc.runCommand ("cabal sandbox add-source " ++ (libsDir </> "B")) >>= Proc.waitForProcess
         Proc.runCommand ("cabal sandbox add-source " ++ (libsDir </> "C")) >>= Proc.waitForProcess
         Proc.runCommand ("cabal sandbox add-source " ++ (libsDir </> "D")) >>= Proc.waitForProcess
         Proc.runCommand "cabal install" >>= Proc.waitForProcess

         [distDir] <- glob $ "dist" </> "dist-sandbox-*"
         Dir.copyFile (distDir </> "setup-config") (inputFiles </> "setup-config")

      cleanUp prevDir = do
         let buildDir = prevDir </> "tests" </> "inputFiles" </> "setup-config-build-env"
         Dir.setCurrentDirectory buildDir
         Proc.runCommand "cabal sandbox delete" >>= Proc.waitForProcess

         Dir.setCurrentDirectory prevDir


tests :: T.TestTree
tests = T.testGroup "Tests" [dropTests, updateTests, dumpTests, libsTests]


dropTests :: T.TestTree
dropTests = T.testGroup "Drop Tests"
   [ test "DropBothOfAll" defaultDrop
   , test "DropUpperOfAll" $ defaultDrop { upper = True }
   , test "DropBothOfLib" $ defaultDrop { library = True }
   , test "DropUpperOfLib" $ defaultDrop { upper = True, library = True }
   , test "DropBothOfExe" $ defaultDrop { executable = ["cabal-bounds"] }
   , test "DropUpperOfExe" $ defaultDrop { upper = True, executable = ["cabal-bounds"] }
   , test "DropBothOfOtherExe" $ defaultDrop { executable = ["other-exe"] }
   , test "DropUpperOfOtherExe" $ defaultDrop { upper = True, executable = ["other-exe"] }
   , test "DropBothOfAllExes" $ defaultDrop { executable = ["cabal-bounds", "other-exe"] }
   , test "DropUpperOfAllExes" $ defaultDrop { upper = True, executable = ["cabal-bounds", "other-exe"] }<commit_msg>Rebuild setup-config file for each test run<commit_after>
module Main where

import qualified Test.Tasty as T
import qualified Test.Tasty.Golden as G
import System.IO (hPutStrLn, stderr)
import System.FilePath ((</>), (<.>))
import System.FilePath.Glob (glob)
import qualified System.Directory as Dir
import qualified System.Process as Proc
import CabalBounds.Args
import CabalBounds.Main (cabalBounds)
import CabalBounds.VersionComp (VersionComp(..))
import Control.Monad (when)
import Control.Exception (finally)

main :: IO ()
main = do
   ensureSetupConfig
   T.defaultMain tests
   deleteSetupConfig


ensureSetupConfig :: IO ()
ensureSetupConfig = do
   configExists <- Dir.doesFileExist $ "tests" </> "inputFiles" </> "setup-config"
   when (not configExists) $ do
      curDir <- Dir.getCurrentDirectory
      buildSetupConfig curDir `finally` cleanUp curDir
   where
      buildSetupConfig curDir = do
         let inputFiles = curDir </> "tests" </> "inputFiles"
         let buildDir   = inputFiles </> "setup-config-build-env"
         let libsDir    = buildDir </> "libs"
         Dir.setCurrentDirectory buildDir

         Proc.runCommand "cabal sandbox init" >>= Proc.waitForProcess
         Proc.runCommand ("cabal sandbox add-source " ++ (libsDir </> "A")) >>= Proc.waitForProcess
         Proc.runCommand ("cabal sandbox add-source " ++ (libsDir </> "B")) >>= Proc.waitForProcess
         Proc.runCommand ("cabal sandbox add-source " ++ (libsDir </> "C")) >>= Proc.waitForProcess
         Proc.runCommand ("cabal sandbox add-source " ++ (libsDir </> "D")) >>= Proc.waitForProcess
         Proc.runCommand "cabal install" >>= Proc.waitForProcess

         [distDir] <- glob $ "dist" </> "dist-sandbox-*"
         Dir.copyFile (distDir </> "setup-config") (inputFiles </> "setup-config")

      cleanUp prevDir = do
         let buildDir = prevDir </> "tests" </> "inputFiles" </> "setup-config-build-env"
         Dir.setCurrentDirectory buildDir
         Proc.runCommand "cabal sandbox delete" >>= Proc.waitForProcess

         Dir.removeDirectoryRecursive $ buildDir </> "dist"
         Dir.setCurrentDirectory prevDir


deleteSetupConfig :: IO ()
deleteSetupConfig = do
   let config = "tests" </> "inputFiles" </> "setup-config"
   configExists <- Dir.doesFileExist
   when configExists $ do
      Dir.removeFile config



tests :: T.TestTree
tests = T.testGroup "Tests" [dropTests, updateTests, dumpTests, libsTests]


dropTests :: T.TestTree
dropTests = T.testGroup "Drop Tests"
   [ test "DropBothOfAll" defaultDrop
   , test "DropUpperOfAll" $ defaultDrop { upper = True }
   , test "DropBothOfLib" $ defaultDrop { library = True }
   , test "DropUpperOfLib" $ defaultDrop { upper = True, library = True }
   , test "DropBothOfExe" $ defaultDrop { executable = ["cabal-bounds"] }
   , test "DropUpperOfExe" $ defaultDrop { upper = True, executable = ["cabal-bounds"] }
   , test "DropBothOfOtherExe" $ defaultDrop { executable = ["other-exe"] }
   , test "DropUpperOfOtherExe" $ defaultDrop { upper = True, executable = ["other-exe"] }
   , test "DropBothOfAllExes" $ defaultDrop { executable = ["cabal-bounds", "other-exe"] }
   , test "DropUpperOfAllExes" $ defaultDrop { upper = True, executable = ["cabal-bounds", "other-exe"] }
{'fraction_non_alphanumeric': 0.13240755691910222, 'fraction_numerical': 0.0, 'mean_word_length': 3.5277777777777777, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 72, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4066307', 'n_tokens_mistral': 1987, 'n_tokens_neox': 1891, 'n_words': 485}