aboutsummaryrefslogtreecommitdiffstats
path: root/test/Main.hs
blob: ad694695333c01675e3ca27e19cf9048e7f948ff (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
{-# LANGUAGE BlockArguments #-}
{-# LANGUAGE QuasiQuotes #-}

module Main (main) where

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 -n stdout|]
    it "capture stderr" do
      (`shouldBe` "stderr") . snd @String =<< [sh|>&2 echo -n stderr|]
    it "capture stdout and stderr interleaved" do
      (`shouldBe` ("stdout", "stderr"))
        =<< [sh|
        echo -n stdout
        >&2 echo -n stderr
        |]
    it "capture interleaved" do
      (`shouldBe` "stdout\nstderr\n")
        =<< [sh|
        echo stdout
        >&2 echo stderr
        |]
  describe "arguments" do
    it "passes `Int`" do
      (`shouldBe` "1") =<< [sh|echo -n %|] (1 :: Int)
    it "passes `Text`" do
      (`shouldBe` "foobar") =<< [sh|echo -n %|] (T.pack "foobar")
      (`shouldBe` "foobar") =<< [sh|echo -n %|] (LT.pack "foobar")
    it "passes `ByteString`" do
      (`shouldBe` "foobar") =<< [sh|echo -n %|] (B.pack "foobar")
      (`shouldBe` "foobar") =<< [sh|echo -n %|] (LB.pack "foobar")
  describe "quoting" do
    it "preserves arguments" do
      (`shouldBe` "foo\\ bar")
        =<< [sh|printf %%q %|] "foo bar"
    it "preserves special characters" do
      (`shouldBe` "foo\\ bar")
        =<< [sh|foo=foo; bar=bar; ( printf %%q % )|] "$foo $bar"
    it "escapes special characters" do
      (`shouldBe` "\\$foo\\ \\$bar")
        =<< [sh|printf %%q '%'|] "$foo $bar"
    it "preserves empty arguments" do
      (`shouldBe` "''") =<< [sh|printf %%q %|] ""
      (`shouldBe` "''") =<< [sh|printf %%q '%'|] ""
  describe "parsing" do
    it "parses garbled arguments" do
      (`shouldBe` "%  foo") =<< [sh|echo -n '% ' %|] "foo"
      (`shouldBe` "  foo") =<< [sh|echo -n ' ' %|] "foo"