加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

haskell – 如何使用bash测试cabal

发布时间:2020-12-15 17:02:43 所属栏目:安全 来源:网络整理
导读:对于我的项目,我已经将一些单元测试写成了bash脚本.确实没有合理的方法在 Haskell中编写测试. 我想在输入cabal测试时运行这些脚本.我该如何做到这一点? 此模块允许您在特定子目录中运行所有.sh脚本作为测试.此外,这使用了测试框架包,因此如果需要,您可以运
对于我的项目,我已经将一些单元测试写成了bash脚本.确实没有合理的方法在 Haskell中编写测试.

我想在输入cabal测试时运行这些脚本.我该如何做到这一点?

此模块允许您在特定子目录中运行所有.sh脚本作为测试.此外,这使用了测试框架包,因此如果需要,您可以运行测试:
cabal test '--test-option=--jxml=dist/test/$test-suite.xml'

然后,您可以从测试中获取junit样式的XML.目前已在my project for testing cabal things中签入.测试代码:

import Data.List (isSuffixOf)
import Control.Applicative
import Test.Framework (defaultMain,testGroup,Test)
import Test.Framework.Providers.HUnit
import Test.HUnit (assertFailure)
import System.Directory
import System.Exit (ExitCode(..))
import System.Process


main :: IO ()
main = makeTests "test" >>= defaultMain

-- Make a test out of those things which end in ".sh" and are executable
-- Make a testgroup out of directories
makeTests :: FilePath -> IO [Test]
makeTests dir = do
  origDir <- getCurrentDirectory
  contents <- getDirectoryContents dir
  setCurrentDirectory dir
  retval <- mapM fileFunc contents
  setCurrentDirectory origDir
  return $concat retval
  where
    fileFunc "." = return []
    fileFunc ".." = return []
    fileFunc f | ".sh" `isSuffixOf` f = do
      fullName <- canonicalizePath f
      isExecutable <- executable <$> getPermissions fullName
      let hunitTest = mkTest fullName
      return [testCase f hunitTest | isExecutable]
    fileFunc d = do
      fullName <- canonicalizePath d
      isSearchable <- searchable <$> getPermissions fullName
      if isSearchable
        then do subTests <- makeTests d
                return [testGroup d subTests]
        else return []
    mkTest fullName = do
      execResult <- system fullName
      case execResult of
        ExitSuccess -> return ()
        ExitFailure code -> assertFailure ("Failed with code " ++ show code)

我在.cabal文件中使用此子句:

test-suite BackflipShellTests
  type:                exitcode-stdio-1.0
  main-is:             BackflipShellTests.hs
  hs-source-dirs:      test
  build-depends:       backflip,base,test-framework-hunit,test-framework,directory,process,HUnit
  default-language:    Haskell2010

请注意,虽然我将.sh测试和测试模块放在同一目录(称为测试)中,但没有固有的理由这样做.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读