aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/world/file/watch.lux
blob: 9b9937a258fd66d31418e86ec59d47f08a7e9f7b (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
(.module:
  [lux #*
   ["_" test (#+ Test)]
   [abstract
    [predicate (#+ Predicate)]
    [monad (#+ do)]]
   [control
    ["." try (#+ Try)]
    ["." exception]
    [concurrency
     ["." promise (#+ Promise)]]]
   [data
    ["." binary (#+ Binary) ("#\." equivalence)]
    ["." text ("#\." equivalence)
     ["%" format (#+ format)]]
    [collection
     ["." list]]]
   [math
    ["." random (#+ Random) ("#\." monad)]]]
  [\\
   ["." /
    ["/#" //]]]
  [////
   [data
    ["$." binary]]])

(def: concern
  (Random [/.Concern (Predicate /.Concern)])
  ($_ random.either
      (random\wrap [/.creation /.creation?])
      (random\wrap [/.modification /.modification?])
      (random\wrap [/.deletion /.deletion?])
      ))

(def: concern\\test
  Test
  (<| (_.for [/.Concern])
      ($_ _.and
          (_.cover [/.creation /.creation?]
                   (and (/.creation? /.creation)
                        (not (/.creation? /.modification))
                        (not (/.creation? /.deletion))))
          (_.cover [/.modification /.modification?]
                   (and (not (/.modification? /.creation))
                        (/.modification? /.modification)
                        (not (/.modification? /.deletion))))
          (_.cover [/.deletion /.deletion?]
                   (and (not (/.deletion? /.creation))
                        (not (/.deletion? /.modification))
                        (/.deletion? /.deletion)))
          (do random.monad
            [left ..concern
             right (random.filter (|>> (is? left) not)
                                  ..concern)
             #let [[left left?] left
                   [right right?] right]]
            (_.cover [/.also]
                     (let [composition (/.also left right)]
                       (and (left? composition)
                            (right? composition)))))
          (_.cover [/.all]
                   (and (/.creation? /.all)
                        (/.modification? /.all)
                        (/.deletion? /.all)))
          )))

(def: exception
  Test
  (do {! random.monad}
    [directory (random.ascii/alpha 5)
     #let [[fs watcher] (/.mock "/")]]
    ($_ _.and
        (wrap (do promise.monad
                [?concern (\ watcher concern directory)
                 ?stop (\ watcher stop directory)]
                (_.cover' [/.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 Promise) (/.Watcher Promise) //.Path (Promise (Try Bit)))
  (do {! (try.with promise.monad)}
    [_ (\ fs make_directory directory)
     _ (\ watcher start /.all directory)]
    (|> (\ watcher poll [])
        (\ ! map list.empty?))))

(def: (after_creation! fs watcher expected_path)
  (-> (//.System Promise) (/.Watcher Promise) //.Path (Promise (Try Bit)))
  (do (try.with promise.monad)
    [_ (: (Promise (Try Any))
          (//.make_file promise.monad fs (binary.create 0) expected_path))
     poll/pre (\ watcher poll [])
     poll/post (\ watcher poll [])]
    (wrap (and (case poll/pre
                 (^ (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 Promise) (/.Watcher Promise) Binary //.Path (Promise (Try Bit)))
  (do (try.with promise.monad)
    [_ (promise.delay 1 (#try.Success "Delay to make sure the over_write time-stamp always changes."))
     _ (\ fs write data expected_path)
     poll/2 (\ watcher poll [])
     poll/2' (\ watcher poll [])]
    (wrap (and (case poll/2
                 (^ (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 Promise) (/.Watcher Promise) //.Path (Promise (Try Bit)))
  (do (try.with promise.monad)
    [_ (\ fs delete expected_path)
     poll/3 (\ watcher poll [])
     poll/3' (\ watcher poll [])]
    (wrap (and (case poll/3
                 (^ (list [concern actual_path]))
                 (and (not (/.creation? concern))
                      (not (/.modification? concern))
                      (/.deletion? concern))

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

(def: #export test
  Test
  (<| (_.covering /._)
      (_.for [/.Watcher])
      ($_ _.and
          ..concern\\test
          ..exception

          (do {! random.monad}
            [directory (random.ascii/alpha 5)
             #let [/ "/"
                   [fs watcher] (/.mock /)]
             expected_path (\ ! map (|>> (format directory /))
                              (random.ascii/alpha 5))
             data ($binary.random 10)]
            (wrap (do {! promise.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)]
                               (wrap (and no_events_prior_to_creation!
                                          after_creation!
                                          after_modification!
                                          after_deletion!)))]
                    (_.cover' [/.mock /.polling]
                              (try.default false verdict)))))
          )))