aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/world/file/watch.lux
blob: af69350e0fc3206d0d71996461502de71108a043 (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
(.using
 [library
  [lux (.except)
   ["_" test (.only Test)]
   [abstract
    [predicate (.only Predicate)]
    [monad (.only do)]]
   [control
    ["[0]" try (.only Try)]
    ["[0]" exception]
    [concurrency
     ["[0]" async (.only Async)]]]
   [data
    ["[0]" binary (.only Binary) ("[1]#[0]" equivalence)]
    ["[0]" text ("[1]#[0]" equivalence)
     ["%" format (.only format)]]
    [collection
     ["[0]" list]]]
   [math
    ["[0]" random (.only Random) ("[1]#[0]" monad)]]]]
 [\\library
  ["[0]" / (.only)
   ["/[1]" //]]]
 [////
  [data
   ["$[0]" binary]]])

(def: concern
  (Random [/.Concern (Predicate /.Concern)])
  (all random.either
       (random#in [/.creation /.creation?])
       (random#in [/.modification /.modification?])
       (random#in [/.deletion /.deletion?])
       ))

(def: concern##test
  Test
  (all _.and
       (_.coverage [/.creation /.creation?]
         (and (/.creation? /.creation)
              (not (/.creation? /.modification))
              (not (/.creation? /.deletion))))
       (_.coverage [/.modification /.modification?]
         (and (not (/.modification? /.creation))
              (/.modification? /.modification)
              (not (/.modification? /.deletion))))
       (_.coverage [/.deletion /.deletion?]
         (and (not (/.deletion? /.creation))
              (not (/.deletion? /.modification))
              (/.deletion? /.deletion)))
       (do random.monad
         [left ..concern
          right (random.only (|>> (same? left) not)
                             ..concern)
          .let [[left left?] left
                [right right?] right]]
         (_.coverage [/.also]
           (let [composition (/.also left right)]
             (and (left? composition)
                  (right? composition)))))
       (_.coverage [/.all]
         (and (/.creation? /.all)
              (/.modification? /.all)
              (/.deletion? /.all)))
       ))

(def: exception
  Test
  (do [! random.monad]
    [directory (random.alphabetic 5)
     .let [[fs watcher] (/.mock "/")]]
    (all _.and
         (in (do async.monad
               [?concern (# watcher concern directory)
                ?stop (# watcher stop directory)]
               (_.coverage' [/.not_being_watched]
                 (and (case ?concern
                        {try.#Failure error}
                        (exception.match? /.not_being_watched error)

                        {try.#Success _}
                        false)
                      (case ?stop
                        {try.#Failure error}
                        (exception.match? /.not_being_watched error)

                        {try.#Success _}
                        false)))))
         )))

(def: (no_events_prior_to_creation! fs watcher directory)
  (-> (//.System Async) (/.Watcher Async) //.Path (Async (Try Bit)))
  (do [! (try.with async.monad)]
    [_ (# fs make_directory directory)
     _ (# watcher start /.all directory)]
    (|> (# watcher poll [])
        (# ! each list.empty?))))

(def: (after_creation! fs watcher expected_path)
  (-> (//.System Async) (/.Watcher Async) //.Path (Async (Try Bit)))
  (do (try.with async.monad)
    [_ (is (Async (Try Any))
           (//.make_file async.monad fs (binary.empty 0) expected_path))
     poll/pre (# watcher poll [])
     poll/post (# watcher poll [])]
    (in (and (case poll/pre
               (pattern (list [concern actual_path]))
               (and (text#= expected_path actual_path)
                    (and (/.creation? concern)
                         (not (/.modification? concern))
                         (not (/.deletion? concern))))

               _
               false)
             (list.empty? poll/post)))))

(def: (after_modification! fs watcher data expected_path)
  (-> (//.System Async) (/.Watcher Async) Binary //.Path (Async (Try Bit)))
  (do (try.with async.monad)
    [_ (async.after 1 {try.#Success "Delay to make sure the over_write time-stamp always changes."})
     _ (# fs write expected_path data)
     poll/2 (# watcher poll [])
     poll/2' (# watcher poll [])]
    (in (and (case poll/2
               (pattern (list [concern actual_path]))
               (and (text#= expected_path actual_path)
                    (and (not (/.creation? concern))
                         (/.modification? concern)
                         (not (/.deletion? concern))))

               _
               false)
             (list.empty? poll/2')))))

(def: (after_deletion! fs watcher expected_path)
  (-> (//.System Async) (/.Watcher Async) //.Path (Async (Try Bit)))
  (do (try.with async.monad)
    [_ (# fs delete expected_path)
     poll/3 (# watcher poll [])
     poll/3' (# watcher poll [])]
    (in (and (case poll/3
               (pattern (list [concern actual_path]))
               (and (not (/.creation? concern))
                    (not (/.modification? concern))
                    (/.deletion? concern))

               _
               false)
             (list.empty? poll/3')))))

(def: .public test
  Test
  (<| (_.covering /._)
      (_.for [/.Watcher])
      (all _.and
           (_.for [/.Concern]
                  ..concern##test)
           ..exception

           (do [! random.monad]
             [directory (random.alphabetic 5)
              .let [/ "/"
                    [fs watcher] (/.mock /)]
              expected_path (# ! each (|>> (format directory /))
                               (random.alphabetic 5))
              data ($binary.random 10)]
             (in (do [! async.monad]
                   [verdict (do (try.with !)
                              [no_events_prior_to_creation!
                               (..no_events_prior_to_creation! fs watcher directory)

                               after_creation!
                               (..after_creation! fs watcher expected_path)

                               after_modification!
                               (..after_modification! fs watcher data expected_path)

                               after_deletion!
                               (..after_deletion! fs watcher expected_path)]
                              (in (and no_events_prior_to_creation!
                                       after_creation!
                                       after_modification!
                                       after_deletion!)))]
                   (_.coverage' [/.mock /.polling]
                     (try.else false verdict)))))
           (do random.monad
             [directory (random.alphabetic 5)
              .let [/ "/"
                    [fs watcher] (/.mock /)]]
             (in (do async.monad
                   [started? (# watcher start /.all directory)]
                   (_.coverage' [/.cannot_poll_a_non_existent_directory]
                     (case started?
                       {try.#Success _}
                       false
                       
                       {try.#Failure error}
                       (exception.match? /.cannot_poll_a_non_existent_directory error))))))
           )))