Skip to content

Commit

Permalink
test: add test for formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
stevana committed Nov 14, 2024
1 parent 461bbaf commit 33ba621
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 20 deletions.
4 changes: 2 additions & 2 deletions INSTALL.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ cabal install spex spex-demo-petstore

If you don't use Nix, then the easiest way to get the right Haskell
dependencies is to first install
[`ghcup`](https://www.haskell.org/ghcup/install/), the
[Haskell](https://www.haskell.org/) installer, and then issue:
[`ghcup`](https://www.haskell.org/ghcup/install/), the Haskell installer, and
then issue:

```bash
git clone https://github.com/spex-lang/spex.git
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ more details about how to use them.
## Installation
See [`INSTALL.md`](INSTALL.md).
See [`INSTALL.md`](INSTALL.md) for installation instructions.
## Usage
Expand Down
1 change: 0 additions & 1 deletion spex.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -123,5 +123,4 @@ test-suite test
, spex
, tasty
, tasty-golden
, tasty-hunit
, temporary
19 changes: 16 additions & 3 deletions src/Spex/CommandLine/Option.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ data Options = Options
, nonInteractive :: Bool
}

-- XXX: renmae veryVerbose to trace?
-- XXX: rename veryVerbose to trace?
data Logging = Quiet Bool | Verbose Bool | VeryVerbose Bool

data Command
Expand All @@ -48,7 +48,10 @@ data VerifyOptions = VerifyOptions
}

data FormatOptions = FormatOptions
{specFilePath :: FilePath}
{ specFilePath :: FilePath
, output :: Maybe FilePath
-- , inPlace :: Inplace ; data InPlace = NotInplace | Inplace | InplaceWithBackup Suffix
}

data CheckOptions = CheckOptions
{specFilePath :: FilePath}
Expand Down Expand Up @@ -212,7 +215,17 @@ parser =
)

format :: Parser FormatOptions
format = FormatOptions <$> specFile
format =
FormatOptions
<$> specFile
<*> optional
( strOption
( long "output"
<> short 'o'
<> metavar "FILE"
<> help "Output file path for the formatted specification"
)
)

check :: Parser CheckOptions
check = CheckOptions <$> specFile
Expand Down
6 changes: 4 additions & 2 deletions src/Spex/LibMain.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Data.ByteString qualified as BS
import Data.ByteString.Lazy qualified as LBS
import GHC.IO.Encoding (setLocaleEncoding)
import System.Exit
import System.IO (utf8)
import System.IO (IOMode (WriteMode), utf8, withFile)

import Spex.CommandLine.Option
import Spex.Lexer
Expand Down Expand Up @@ -83,7 +83,9 @@ formatApp :: FormatOptions -> App ()
formatApp opts = do
bs <- liftIO (try (BS.readFile opts.specFilePath)) <?> ReadSpecFileError
spec <- pure (runParser specP bs) <?> ParserError
liftIO (putSpec spec)
case opts.output of
Nothing -> liftIO (putSpec spec)
Just fp -> liftIO $ withFile fp WriteMode $ \h -> hPutSpec h spec

checkApp :: CheckOptions -> App ()
checkApp opts = do
Expand Down
8 changes: 6 additions & 2 deletions src/Spex/PrettyPrinter.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import Data.Map (Map)
import Data.Map qualified as Map
import Data.String
import Prettyprinter
import Prettyprinter.Util
import Prettyprinter.Render.Text
import System.IO

import Spex.Syntax

Expand Down Expand Up @@ -90,4 +91,7 @@ prettyBS :: (Coercible a ByteString) => a -> Doc x
prettyBS = fromString . BS8.unpack . coerce

putSpec :: Spec -> IO ()
putSpec = putDocW 72 . prettySpec
putSpec = hPutSpec stdout

hPutSpec :: Handle -> Spec -> IO ()
hPutSpec h = hPutDoc h . prettySpec
38 changes: 29 additions & 9 deletions test/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,23 @@ tests :: FilePath -> TestTree
tests tmpDir =
testGroup
"Golden"
[ withPetstore
0
( test
[ "verify"
, "example/petstore-basic.spex"
, "--seed"
, "2503963955766725184"
[ testGroup
"Verify"
( zipWith
withPetstore
[ test
[ "verify"
, "example/petstore-basic.spex"
, "--seed"
, "2503963955766725184"
]
]
[0 ..] -- Used to compute the ports used by the tests.
)
, testGroup
"Format"
[ testOutput ["format", "example/petstore-bad-formatting.spex"]
]
]
where
test :: [String] -> TestTree
Expand All @@ -41,8 +49,20 @@ tests tmpDir =
logFile
(testMain ("--non-interactive" : "--log-file" : logFile : args))

withPetstore :: Int -> TestTree -> TestTree
withPetstore i tt =
testOutput :: [String] -> TestTree
testOutput args =
let testName = intercalate " " args
outName = replace '/' '-' (intercalate "_" args)
outFile = tmpDir </> outName <.> "txt"
in goldenVsFileDiff
testName
(\ref new -> ["diff", "-u", ref, new])
("test" </> "golden" </> outName <.> "golden")
outFile
(testMain ("--non-interactive" : args ++ ["--output", outFile]))

withPetstore :: TestTree -> Int -> TestTree
withPetstore tt i =
withResource
(forkIO (Petstore.libMain (8080 + i)))
killThread
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
component PetStore where

addPet : POST /pet Pet
getPet : GET /pet/{petId : Int} -> Pet

0 comments on commit 33ba621

Please sign in to comment.