blob: 96612dcfd5b6d40e14d524e77aa4437d78424122 (
plain)
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
58
59
60
61
62
63
64
65
66
67
68
69
70
|
{-# LANGUAGE BlockArguments #-}
{-# LANGUAGE PartialTypeSignatures #-}
{-# LANGUAGE QuasiQuotes #-}
module Main (main) where
import Control.Monad.Reader
import Data.ByteString.Char8 qualified as B
import Data.ByteString.Lazy.Char8 qualified as LB
import Data.Text qualified as T
import Data.Text.Lazy qualified as LT
import Process.Shell
import Test.Hspec
main :: IO ()
main = hspec do
describe "output" do
it "capture stdout" do
(`shouldBe` "stdout") . fst @String @String =<< [sh|echo stdout|]
it "capture stderr" do
(`shouldBe` "stderr") . snd @String =<< [sh|>&2 echo stderr|]
it "capture stdout and stderr" do
(`shouldBe` ("stdout", "stderr"))
=<< [sh|
echo stdout
>&2 echo stderr
|]
it "capture stdout and stderr interleaved" do
(`shouldBe` "stdout\nstderr")
=<< [sh|
echo stdout
>&2 echo stderr
|]
it "preserve trailing newline" do
(`shouldBe` "stdout\n") . fst @String @String =<< [sh|echo stdout \|]
describe "arguments" do
it "passes `Int`" do
(`shouldBe` "1") =<< let x = 1 :: Int in [sh|echo '#{x}'|]
it "passes `Text`" do
(`shouldBe` "foobar") =<< let x = T.pack "foobar" in [sh|echo '#{x}'|]
(`shouldBe` "foobar") =<< let x = LT.pack "foobar" in [sh|echo '#{x}'|]
it "passes `ByteString`" do
(`shouldBe` "foobar") =<< let x = B.pack "foobar" in [sh|echo '#{x}'|]
(`shouldBe` "foobar") =<< let x = LB.pack "foobar" in [sh|echo '#{x}'|]
it "interpolates expressions" do
(`shouldBe` "1") =<< let x = 1 :: Int in [sh|echo '#{show x}'|]
it "interpolates monadic expressions" do
(`shouldBe` "1") =<< let x = 1 :: Int in [sh|echo '#{{pure @IO x}}'|]
it "interpolates monadic expressions" do
(`shouldBe` "1") =<< runReaderT [sh|echo '#{{asks fst}}'|] (1 :: Int, 2 :: Int)
it "preserves argument order" do
(`shouldBe` "1 2") =<< let x = "1"; y = "2" in [sh|echo '#{x}' '#{y}'|]
describe "quoting" do
it "preserves arguments" do
(`shouldBe` "foo\\ bar")
=<< let x = "foo bar" in [sh|printf %q '#{x}'|]
it "preserves special characters" do
(`shouldBe` "foo\\ bar")
=<< let x = "$foo $bar" in [sh|foo=foo; bar=bar; ( printf %q #{x} )|]
it "escapes special characters" do
(`shouldBe` "\\$foo\\ \\$bar")
=<< let x = "$foo $bar" in [sh|foo=foo; bar=bar; ( printf %q '#{x}' )|]
it "preserves empty arguments" do
(`shouldBe` "''") =<< let x = "" in [sh|printf %q #{x}|]
(`shouldBe` "''") =<< let x = "" in [sh|printf %q '#{x}'|]
describe "parsing" do
it "parses shell quotes" do
(`shouldBe` "foobar") =<< [sh|echo 'foobar'|]
it "parses double cross" do
(`shouldBe` "0") =<< [sh|echo $#|]
|