Skip to content

Commit

Permalink
update package-with-hooks test
Browse files Browse the repository at this point in the history
  • Loading branch information
sheaf committed Apr 17, 2024
1 parent a9756fe commit 17278dc
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,45 +31,44 @@ main = do
-- Get all the constants defined in the data file for the build tool.
customDataFile <- getDataFileName "CustomBuildToolData.txt"
customDataFileExists <- doesFileExist customDataFile
unless customDataFileExists $ do
cwd <- getCurrentDirectory
error $
unlines
[ "Custom preprocessor could not access its data file."
, "Tried to look in: " ++ customDataFile
, "cwd: " ++ show cwd ]
customDataLines <- lines <$> readFile customDataFile
--unless customDataFileExists $ do
-- cwd <- getCurrentDirectory
-- error $
-- unlines
-- [ "Custom preprocessor could not access its data file."
-- , "Tried to look in: " ++ customDataFile
-- , "cwd: " ++ show cwd ]
--customDataLines <- lines <$> readFile customDataFile
let customConstants :: Map String Int
customConstants = Map.fromList $ map read customDataLines
customConstants = Map.fromList $ [("MyConstant", 1717)] -- map read customDataLines

-- Obtain input/output file paths from arguments to the preprocessor.
args <- getArgs
(inputFile, outputFile) <-
case args of
[inputFile, outputFile] -> do
inputFileExists <- doesFileExist inputFile
unless inputFileExists $
error $
unlines
[ "Custom preprocess could not read input file."
, "Input file: " ++ inputFile ]
return (inputFile, outputFile)
_ ->
case args of
[inputFile, outputFile] -> do
inputFileExists <- doesFileExist inputFile
unless inputFileExists $
error $
unlines
[ "Custom preprocessor was given incorrect arguments."
, "Expected exactly two arguments (input and output file paths), but got " ++ what ++ "." ]
where
what = case args of
[] -> "none"
[_] -> "a single argument"
_ -> show (length args) ++ " arguments"

-- Read the input file, substitute constants for their values,
-- and write the result to the output file path.
inputLines <- lines <$> readFile inputFile
let outputLines = map ( preprocessLine customConstants ) ( zip [1..] inputLines )
writeFile outputFile ( unlines outputLines )
[ "Custom preprocessor could not read input file."
, "Input file: " ++ inputFile ]
-- Read the input file, substitute constants for their values,
-- and write the result to the output file path.
inputLines <- lines <$> readFile inputFile
let outputLines = map ( preprocessLine customConstants ) ( zip [1..] inputLines )

Check warning on line 58 in cabal-testsuite/PackageTests/HooksPreprocessor/custom-build-tool/exe/Main.hs

View workflow job for this annotation

GitHub Actions / hlint

Suggestion in main in module Main: Use zipWith ▫︎ Found: "map (preprocessLine customConstants) (zip [1 .. ] inputLines)" ▫︎ Perhaps: "zipWith (curry (preprocessLine customConstants)) [1 .. ] inputLines"
writeFile outputFile ( unlines outputLines )
[] ->
putStrLn "Custom preprocessor: no arguments."
_ ->
error $
unlines
[ "Custom preprocessor was given incorrect arguments."
, "Expected input and output file paths, but got " ++ what ++ "." ]
where
what = case args of
[] -> "none"
[_] -> "a single argument"
_ -> show (length args) ++ " arguments"

-- | Substitute any occurrence of {# ConstantName #} with the value of ConstantName,
-- looked up in the data file for the preprocessor.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ preBuildRules
mbWorkDir = mbWorkDirLBI lbi

-- 1. Look up the custom-build-tool preprocessor.
let customPpProg = (simpleProgram customPpName)
let customPpProg = simpleProgram customPpName
mbCustomPp = lookupProgram customPpProg progDb
customPp = case mbCustomPp of
Just pp -> pp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ test-suite package-with-hooks-test
type: exitcode-stdio-1.0
hs-source-dirs: test
main-is: Main.hs
other-modules: CallCustomPp
build-depends:
base >= 4.18 && < 5,
package-with-hooks
base
>= 4.18 && < 5,
template-haskell
>= 2.20 && < 3,
process,
package-with-hooks
build-tool-depends:
custom-build-tool:custom-build-tool
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
{-# LANGUAGE TemplateHaskell #-}

module Main ( main ) where

import MyLib ( someFunc )
-- template-haskell
import Language.Haskell.TH
( runIO )

-- package-with-hooks
import CallCustomPp
( callCustomPp )

--------------------------------------------------------------------------------

-- Check that we can invoke the custom preprocessor, and that it finds its
-- data directory, both at compile-time and at run-time.

$( do
runIO callCustomPp
return []
)

main :: IO ()
main = if someFunc 11 == 1728
then return ()
else error "Failure"
main = callCustomPp

0 comments on commit 17278dc

Please sign in to comment.