aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/world/shell.lux
blob: b3ea3ca464eae6a10aa868768b2429ba6f6db880 (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
(.require
 [library
  [lux (.except)
   [abstract
    [monad (.only do)]]
   [control
    ["[0]" try (.only Try) (.use "[1]#[0]" functor)]
    ["[0]" exception]
    ["[0]" io (.only IO)]
    [concurrency
     ["[0]" async (.only Async) (.use "[1]#[0]" monad)]]]
   [data
    ["[0]" product]
    ["[0]" text (.use "[1]#[0]" equivalence)
     ["%" \\format (.only format)]]
    [collection
     ["[0]" list]]]
   [math
    ["[0]" random]
    [number
     ["n" nat]
     ["i" int]]]
   [test
    ["_" property (.only Test)]
    ["[0]" unit]]]]
 [\\library
  ["[0]" / (.only)
   [//
    [file (.only Path)]
    ["[0]" environment
     ["[1]" \\parser (.only Environment)]]]]])

(with_template [<name> <command> <type> <prep>]
  [(def <name>
     (-> <type> [Environment Path /.Command (List /.Argument)])
     (|>> <prep> list [environment.empty "~" <command>]))]

  [echo! "echo" Text (|>)]
  [sleep! "sleep" Nat %.nat]
  )

(def (can_wait! process)
  (-> (/.Process Async) unit.Test)
  (|> (of process await [])
      (async#each (|>> (try#each (i.= /.normal))
                       (try.else false)
                       (unit.coverage [/.Exit /.normal])))
      async#conjoint))

(def (can_read! expected process)
  (-> Text (/.Process Async) (Async Bit))
  (|> (of process read [])
      (async#each (|>> (try#each (text#= expected))
                       (try.else false)))))

(def (can_destroy! process)
  (-> (/.Process Async) (Async Bit))
  (do async.monad
    [?destroy (of process destroy [])
     ?await (of process await [])]
    (in (and (when ?destroy
               {try.#Success _}
               true
               
               {try.#Failure error}
               false)
             (when ?await
               {try.#Success _}
               false
               
               {try.#Failure error}
               true)))))

(with_expansions [<shell_coverage> (these [/.Command /.Argument])]
  (def .public (spec shell)
    (-> (/.Shell Async)
        Test)
    (<| (_.for [/.Shell
                /.execute
                
                /.Process
                /.read /.fail /.write /.destroy /.await])
        (do [! random.monad]
          [message (random.alphabetic 10)
           seconds (of ! each (|>> (n.% 5) (n.+ 5)) random.nat)]
          (in (do [! async.monad]
                [?echo (of shell execute (..echo! message))
                 ?sleep (of shell execute (..sleep! seconds))]
                (when [?echo ?sleep]
                  [{try.#Success echo} {try.#Success sleep}]
                  (do !
                    [can_read! (..can_read! message echo)
                     can_destroy! (..can_destroy! sleep)]
                    (all unit.and
                         (unit.coverage <shell_coverage>
                           (and can_read!
                                can_destroy!))
                         (..can_wait! echo)
                         ))
                  
                  _
                  (unit.coverage <shell_coverage>
                    false))))))))

(exception.def dead)

(def (mock [environment working_directory command arguments])
  (-> [Environment Path /.Command (List /.Argument)]
      (/.Mock Bit))
  (implementation
   (def (on_read dead?)
     (if dead?
       (exception.except ..dead [])
       (do try.monad
         [echo (try.of_maybe (list.head arguments))]
         (in [dead? echo]))))
   
   (def (on_fail dead?)
     (if dead?
       (exception.except ..dead [])
       {try.#Success [dead? ""]}))
   
   (def (on_write message dead?)
     (if dead?
       (exception.except ..dead [])
       {try.#Success dead?}))

   (def (on_destroy dead?)
     (if dead?
       (exception.except ..dead [])
       {try.#Success true}))

   (def (on_await dead?)
     (if dead?
       (exception.except ..dead [])
       {try.#Success [true /.normal]}))))

(def (io_shell command oops input destruction exit)
  (-> /.Command Text Text Text /.Exit (/.Shell IO))
  (implementation
   (def (execute [environment working_directory command arguments])
     (<| io.io
         {try.#Success}
         (is (/.Process IO))
         (implementation
          (def (read _)
            (io.io {try.#Success command}))
          (def (fail _)
            (io.io {try.#Success oops}))
          (def (write message)
            (io.io {try.#Failure message}))
          (def (destroy _)
            (io.io {try.#Failure destruction}))
          (def (await _)
            (io.io {try.#Success exit})))))))

(def .public test
  Test
  (<| (_.covering /._)
      (all _.and
           (_.for [/.Mock /.mock
                   /.on_read /.on_fail /.on_write /.on_destroy /.on_await

                   /.async]
                  (..spec (/.async (/.mock (|>> ..mock {try.#Success})
                                           false))))
           (_.coverage [/.error]
             (not (i.= /.normal /.error)))
           (do random.monad
             [command (random.alphabetic 5)
              oops (random.alphabetic 5)
              input (random.alphabetic 5)
              destruction (random.alphabetic 5)
              exit random.int
              .let [shell (/.async (..io_shell command oops input destruction exit))]]
             (in (do [! async.monad]
                   [verdict (do (try.with !)
                              [process (of shell execute [environment.empty "~" command (list)])
                               read (of process read [])
                               failure (of process fail [])
                               wrote! (do !
                                        [write (of process write input)]
                                        (in {try.#Success (when write
                                                            {try.#Success _}
                                                            false
                                                            
                                                            {try.#Failure write}
                                                            (text#= input write))}))
                               destroyed! (do !
                                            [destroy (of process destroy [])]
                                            (in {try.#Success (when destroy
                                                                {try.#Success _}
                                                                false
                                                                
                                                                {try.#Failure destroy}
                                                                (text#= destruction destroy))}))
                               await (of process await [])]
                              (in (and (text#= command read)
                                       (text#= oops failure)
                                       wrote!
                                       destroyed!
                                       (i.= exit await))))]
                   (unit.coverage [/.Shell]
                     (try.else false verdict)))))
           )))