aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/aedifex/command/clean.lux
blob: 07beacb4dc6f7d8ccc16e17771cb7caa0c4b23d6 (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
... This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
... If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.

(.require
 [library
  [lux (.except)
   [abstract
    ["[0]" monad (.only do)]]
   [control
    ["[0]" try (.only Try)]
    [concurrency
     ["[0]" async (.only Async)]]]
   [data
    [binary (.only Binary)]
    ["[0]" product]
    ["[0]" text (.use "[1]#[0]" equivalence)
     ["%" \\format (.only format)]]
    [collection
     ["[0]" list (.use "[1]#[0]" functor)]
     ["[0]" set]]]
   [math
    ["[0]" random (.only Random)]
    [number
     ["n" nat]]]
   [world
    ["[0]" file (.only Path)]]
   [test
    ["[0]" unit]
    ["_" property (.only Test)]]]]
 [//
  ["@[0]" version]
  [//
   ["@[0]" profile]
   [//
    [lux
     [data
      ["$[0]" binary]]]]]]
 [\\program
  ["[0]" / (.only)
   ["//[1]" ///
    ["[1]" profile]
    ["[1][0]" action (.only Action)]]]])

(def node_name
  (Random Text)
  (random.alphabetic 10))

(def (files prefix)
  (-> Path (Random (List [Path Binary])))
  (do [! random.monad]
    [count (of ! each (n.% 10) random.nat)
     names (random.set text.hash count ..node_name)
     contents (random.list count ($binary.random 100))]
    (in (list.zipped_2 (list#each (|>> (format prefix)) (set.list names))
                       contents))))

(def (create_file! fs [path content])
  (-> (file.System Async) [Path Binary] (Async (Try Any)))
  (of fs write path content))

(def (create_directory! fs path files)
  (-> (file.System Async) Path (List [Path Binary]) (Async (Try Any)))
  (do [! (try.with async.monad)]
    [_ (is (Async (Try Any))
           (file.make_directories async.monad fs path))
     _ (monad.each ! (..create_file! fs) files)]
    (in [])))

(def (directory_exists? fs)
  (-> (file.System Async) Path (Async (Try Bit)))
  (|>> (of fs directory?) (try.lifted async.monad)))

(def (file_exists? fs)
  (-> (file.System Async) Path (Async (Try Bit)))
  (|>> (of fs file?) (try.lifted async.monad)))

(def (assets_exist? fs directory_path files)
  (-> (file.System Async) Path (List [Path Binary]) (Async (Try Bit)))
  (do [! (try.with async.monad)]
    [directory_exists? (..directory_exists? fs directory_path)
     files_exist? (is (Action (List Bit))
                      (|> files
                          (list#each product.left)
                          (monad.each ///action.monad (..file_exists? fs))))]
    (in (and directory_exists?
             (list.every? (|>>) files_exist?)))))

(def .public test
  Test
  (<| (_.covering /._)
      (do [! random.monad]
        [context ..node_name
         target ..node_name
         sub ..node_name
         .let [fs (file.mock (of file.default separator))
               / (of fs separator)
               target_path (format context / target)
               sub_path (format target_path / sub)]
         direct_files (..files (format target_path /))
         sub_files (..files (format sub_path /))

         dummy @profile.random]
        (in (do async.monad
              [.let [console (@version.echo "")]
               verdict (do [! (try.with async.monad)]
                         [_ (..create_directory! fs target_path direct_files)
                          _ (..create_directory! fs sub_path sub_files)
                          context_exists!/pre (..directory_exists? fs context)
                          target_exists!/pre (..assets_exist? fs target_path direct_files)
                          sub_exists!/pre (..assets_exist? fs sub_path sub_files)
                          _ (/.do! console fs (has ///.#target target_path dummy))
                          context_exists!/post (..directory_exists? fs context)
                          target_exists!/post (..assets_exist? fs target_path direct_files)
                          sub_exists!/post (..assets_exist? fs sub_path sub_files)
                          logging (of console read_line [])]
                         (in (and (and context_exists!/pre
                                       context_exists!/post)
                                  (and target_exists!/pre
                                       (not target_exists!/post))
                                  (and sub_exists!/pre
                                       (not sub_exists!/post))
                                  (text#= (/.success target_path) logging))))]
              (unit.coverage [/.do! /.success]
                (try.else false verdict)))))))