aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/specification/lux/world/file.lux
blob: 8a618c3ae2c0432e27e52a2b549bbf395eec33a5 (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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
(.module:
  [library
   [lux #*
    ["_" test (#+ Test)]
    [abstract
     [monad (#+ do)]
     ["." predicate]]
    [control
     [pipe (#+ case>)]
     [io (#+ IO)]
     ["." maybe ("#\." functor)]
     ["." try ("#\." functor)]
     ["." exception]
     [concurrency
      ["." async (#+ Async)]]]
    [data
     ["." text ("#\." equivalence)
      ["%" format (#+ format)]
      [encoding
       ["." utf8 ("#\." codec)]]]
     ["." binary (#+ Binary) ("#\." equivalence monoid)
      ["$#" \\test]]
     [collection
      ["." list]]]
    [math
     ["." random]
     [number
      ["n" nat]]]
    [time
     ["." instant (#+ Instant) ("#\." equivalence)]]]]
  [\\library
   ["." /]])

(def: (for_path fs)
  (-> (IO (/.System Async)) Test)
  (<| (_.for [/.Path])
      (do {! random.monad}
        [parent (random.ascii/numeric 2)
         child (random.ascii/numeric 2)])
      in
      (do async.monad
        [fs (async.future fs)]
        ($_ _.and'
            (_.cover' [/.rooted]
                      (let [path (/.rooted fs parent child)]
                        (and (text.starts_with? parent path)
                             (text.ends_with? child path))))
            (_.cover' [/.parent]
                      (|> (/.rooted fs parent child)
                          (/.parent fs)
                          (maybe\each (text\= parent))
                          (maybe.else false)))
            (_.cover' [/.name]
                      (|> (/.rooted fs parent child)
                          (/.name fs)
                          (text\= child)))
            ))))

(def: (directory?&make_directory fs parent)
  (-> (/.System Async) /.Path (Async Bit))
  (do async.monad
    [directory_pre! (\ fs directory? parent)
     made? (\ fs make_directory parent)
     directory_post! (\ fs directory? parent)]
    (in (and (not directory_pre!)
             (case made?
               (#try.Success _) true
               (#try.Failure _) false)
             directory_post!))))

(def: (file?&write fs content path)
  (-> (/.System Async) Binary /.Path (Async Bit))
  (do async.monad
    [file_pre! (\ fs file? path)
     made? (\ fs write content path)
     file_post! (\ fs file? path)]
    (in (and (not file_pre!)
             (case made?
               (#try.Success _) true
               (#try.Failure _) false)
             file_post!))))

(def: (file_size&read&append fs expected_file_size content appendix path)
  (-> (/.System Async) Nat Binary Binary /.Path (Async Bit))
  (do async.monad
    [pre_file_size (\ fs file_size path)
     pre_content (\ fs read path)
     appended? (\ fs append appendix path)
     post_file_size (\ fs file_size path)
     post_content (\ fs read path)]
    (in (<| (try.else false)
            (do {! try.monad}
              [pre_file_size!
               (\ ! each (n.= expected_file_size) pre_file_size)
               
               pre_content!
               (\ ! each (binary\= content) pre_content)
               
               _ appended?

               post_file_size!
               (\ ! each (n.= (n.* 2 expected_file_size)) post_file_size)
               
               post_content!
               (\ ! each (binary\= (binary\composite content appendix)) post_content)]
              (in (and pre_file_size!
                       pre_content!
                       post_file_size!
                       post_content!)))))))

(def: (modified?&last_modified fs expected_time path)
  (-> (/.System Async) Instant /.Path (Async Bit))
  (do async.monad
    [modified? (\ fs modify expected_time path)
     last_modified (\ fs last_modified path)]
    (in (<| (try.else false)
            (do {! try.monad}
              [_ modified?]
              (\ ! each (instant\= expected_time) last_modified))))))

(def: (directory_files&sub_directories fs parent sub_dir child)
  (-> (/.System Async) /.Path /.Path /.Path (Async Bit))
  (let [sub_dir (/.rooted fs parent sub_dir)
        child (/.rooted fs parent child)]
    (do async.monad
      [made_sub? (\ fs make_directory sub_dir)
       directory_files (\ fs directory_files parent)
       sub_directories (\ fs sub_directories parent)
       .let [(^open "list\.") (list.equivalence text.equivalence)]]
      (in (<| (try.else false)
              (do try.monad
                [_ made_sub?]
                (in (and (|> directory_files
                             (try\each (list\= (list child)))
                             (try.else false))
                         (|> sub_directories
                             (try\each (list\= (list sub_dir)))
                             (try.else false))))))))))

(def: (move&delete fs parent child alternate_child)
  (-> (/.System Async) /.Path Text Text (Async Bit))
  (let [origin (/.rooted fs parent child)
        destination (/.rooted fs parent alternate_child)]
    (do {! async.monad}
      [moved? (\ fs move destination origin)
       lost? (|> origin
                 (\ fs file?)
                 (\ ! each not))
       found? (\ fs file? destination)
       deleted? (\ fs delete destination)]
      (in (<| (try.else false)
              (do try.monad
                [_ moved?
                 _ deleted?]
                (in (and lost?
                         found?))))))))

(def: (for_system fs)
  (-> (IO (/.System Async)) Test)
  (<| (do {! random.monad}
        [parent (random.ascii/numeric 2)
         child (random.ascii/numeric 2)
         sub_dir (random.only (|>> (text\= child) not)
                              (random.ascii/numeric 2))
         alternate_child (random.only (predicate.and
                                       (|>> (text\= child) not)
                                       (|>> (text\= sub_dir) not))
                                      (random.ascii/numeric 2))
         expected_file_size (\ ! each (|>> (n.% 10) ++) random.nat)
         content ($binary.random expected_file_size)
         appendix ($binary.random expected_file_size)
         expected_time random.instant])
      in
      (do {! async.monad}
        [fs (async.future fs)
         .let [path (/.rooted fs parent child)]
         
         directory?&make_directory
         (..directory?&make_directory fs parent)

         file?&write
         (..file?&write fs content path)

         file_size&read&append
         (..file_size&read&append fs expected_file_size content appendix path)

         modified?&last_modified
         (..modified?&last_modified fs expected_time path)

         can_execute?
         (|> path
             (\ fs can_execute?)
             (\ ! each (|>> (try.else true) not)))

         directory_files&sub_directories
         (..directory_files&sub_directories fs parent sub_dir child)

         move&delete
         (..move&delete fs parent child alternate_child)])
      (_.cover' [/.System]
                (and directory?&make_directory
                     file?&write
                     file_size&read&append
                     modified?&last_modified
                     can_execute?
                     directory_files&sub_directories
                     move&delete))))

(def: (make_directories&cannot_make_directory fs)
  (-> (IO (/.System Async)) Test)
  (<| (do {! random.monad}
        [dir/0 (random.ascii/numeric 2)
         dir/1 (random.ascii/numeric 2)
         dir/2 (random.ascii/numeric 2)])
      in
      (do {! async.monad}
        [fs (async.future fs)
         .let [dir/1 (/.rooted fs dir/0 dir/1)
               dir/2 (/.rooted fs dir/1 dir/2)]
         pre_dir/0 (\ fs directory? dir/0)
         pre_dir/1 (\ fs directory? dir/1)
         pre_dir/2 (\ fs directory? dir/2)
         made? (/.make_directories ! fs dir/2)
         post_dir/0 (\ fs directory? dir/0)
         post_dir/1 (\ fs directory? dir/1)
         post_dir/2 (\ fs directory? dir/2)

         cannot_make_directory!/0 (/.make_directories ! fs "")
         cannot_make_directory!/1 (/.make_directories ! fs (\ fs separator))])
      ($_ _.and'
          (_.cover' [/.make_directories]
                    (and (not pre_dir/0)
                         (not pre_dir/1)
                         (not pre_dir/2)
                         (case made?
                           (#try.Success _) true
                           (#try.Failure _) false)
                         post_dir/0
                         post_dir/1
                         post_dir/2))
          (_.cover' [/.cannot_make_directory]
                    (and (case cannot_make_directory!/0
                           (#try.Success _)
                           false
                           
                           (#try.Failure error)
                           (exception.match? /.cannot_make_directory error))
                         (case cannot_make_directory!/1
                           (#try.Success _)
                           false
                           
                           (#try.Failure error)
                           (exception.match? /.cannot_make_directory error))))
          )))

(def: (make_file&cannot_make_file fs)
  (-> (IO (/.System Async)) Test)
  (<| (do {! random.monad}
        [file/0 (random.ascii/numeric 3)])
      in
      (do {! async.monad}
        [fs (async.future fs)
         make_file!/0 (/.make_file ! fs (utf8\encoded file/0) file/0)
         make_file!/1 (/.make_file ! fs (utf8\encoded file/0) file/0)])
      ($_ _.and'
          (_.cover' [/.make_file]
                    (case make_file!/0
                      (#try.Success _) true
                      (#try.Failure error) false))
          (_.cover' [/.cannot_make_file]
                    (case make_file!/1
                      (#try.Success _)
                      false
                      
                      (#try.Failure error)
                      (exception.match? /.cannot_make_file error)))
          )))

(def: (for_utilities fs)
  (-> (IO (/.System Async)) Test)
  ($_ _.and
      (..make_directories&cannot_make_directory fs)
      (..make_file&cannot_make_file fs)
      ))

(def: (exists? fs)
  (-> (IO (/.System Async)) Test)
  (<| (do {! random.monad}
        [file (random.ascii/numeric 2)
         dir (random.only (|>> (text\= file) not)
                          (random.ascii/numeric 2))])
      in
      (do {! async.monad}
        [fs (async.future fs)
         
         pre_file/0 (\ fs file? file)
         pre_file/1 (/.exists? ! fs file)
         pre_dir/0 (\ fs directory? dir)
         pre_dir/1 (/.exists? ! fs dir)

         made_file? (/.make_file ! fs (utf8\encoded file) file)
         made_dir? (\ fs make_directory dir)

         post_file/0 (\ fs file? file)
         post_file/1 (/.exists? ! fs file)
         post_dir/0 (\ fs directory? dir)
         post_dir/1 (/.exists? ! fs dir)])
      (_.cover' [/.exists?]
                (and (not pre_file/0)
                     (not pre_file/1)
                     (not pre_dir/0)
                     (not pre_dir/1)

                     (case made_file?
                       (#try.Success _) true
                       (#try.Failure _) false)
                     (case made_dir?
                       (#try.Success _) true
                       (#try.Failure _) false)

                     post_file/0
                     post_file/1
                     post_dir/0
                     post_dir/1))))

(def: .public (spec fs)
  (-> (IO (/.System Async)) Test)
  ($_ _.and
      (..for_path fs)
      (..for_utilities fs)
      (..for_system fs)
      (..exists? fs)
      ))