File: test-cli.hs

package info (click to toggle)
haskell-hjsmin 0.2.1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 116 kB
  • sloc: haskell: 152; sh: 30; makefile: 3
file content (57 lines) | stat: -rw-r--r-- 2,086 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
{-# LANGUAGE MultiWayIf #-}

import           Control.Monad (foldM, forM)
import           Control.Monad.Extra (concatMapM)

import           System.Directory (canonicalizePath, getCurrentDirectory, listDirectory)
import           System.Environment (setEnv)
import           System.FilePath (takeFileName, (</>))
import           System.Exit (ExitCode (..), exitFailure, exitSuccess)
import           System.Posix.Files (getFileStatus, isDirectory, isRegularFile)
import           System.Process (rawSystem)
import           System.IO (BufferMode (..))
import qualified System.IO as IO

main :: IO ()
main = do
  IO.hSetBuffering IO.stdout LineBuffering
  IO.hSetBuffering IO.stderr LineBuffering

  cwd <- getCurrentDirectory
  topdir <- canonicalizePath $ cwd </> "dist-ghc"

  -- Set an environment variable for all the exectuables we want to test.
  setExecutableEnvVar "HJSMIN" topdir "hjsmin"

  tests <- filter (`notElem` ["core", "data"]) <$> listDirectory "test/cli/"
  res <- forM tests $ \ t -> rawSystem "/bin/bash" ["test/cli/" ++ t ++ "/run"]
  if all (== ExitSuccess) res
    then exitSuccess
    else exitFailure


setExecutableEnvVar :: String -> FilePath -> FilePath -> IO ()
setExecutableEnvVar envName startDir target = do
  xs <- listDirectoryRecursive startDir
  case filter match xs of
    [] -> error "Unable to find hjsmin binary"
    [x] -> setEnv envName x
    _ -> error $ "Multiple binaries: " ++ show xs
 where
  match :: FilePath -> Bool
  match fp = takeFileName fp == target

listDirectoryRecursive :: FilePath -> IO [FilePath]
listDirectoryRecursive fpath = do
  xs <- fmap (fpath </>) <$> listDirectory fpath
  (files, dirs) <- foldM partitioner ([], []) xs
  rest <- concatMapM listDirectoryRecursive (dirs :: [FilePath])
  pure $ files ++ rest
 where
  partitioner :: ([FilePath], [FilePath]) -> FilePath -> IO ([FilePath], [FilePath])
  partitioner (files, dirs) fp = do
    st <- getFileStatus fp
    if
      | isRegularFile st -> pure (fp : files, dirs)
      | isDirectory st -> pure (files, fp : dirs)
      | otherwise -> pure (files, dirs)