aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/aedifex/command/auto.lux
blob: b014162eae9b2094517574184ebf4f1a31a03aa1 (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
(.using
 [library
  [lux "*"
   ["_" test (.only Test)]
   [abstract
    [monad (.only do)]]
   [control
    ["[0]" pipe]
    ["[0]" try]
    [parser
     ["[0]" environment]]
    [concurrency
     ["[0]" atom (.only Atom)]
     ["[0]" async (.only Async)]]]
   [data
    ["[0]" binary]
    ["[0]" text
     ["%" format (.only format)]
     [encoding
      ["[0]" utf8]]]
    [collection
     ["[0]" set]]]
   [math
    ["[0]" random]
    [number
     ["n" nat]]]
   [time
    ["[0]" instant]]
   [world
    [console (.only Console)]
    ["[0]" shell (.only Exit Shell)]
    ["[0]" program (.only Program)]
    ["[0]" file
     ["[0]" watch]]]]]
 ["[0]" // "_"
  ["$[0]" version]
  ["$[0]" build]]
 [\\program
  ["[0]" /
   ["//[1]" /// "_"
    [command (.only Command)]
    ["[1]" profile (.only Profile)]
    ["[1][0]" action]
    [dependency
     [resolution (.only Resolution)]]]]])

(def: (command expected_runs end_signal fs dummy_file)
  (-> Nat Text (file.System Async) file.Path
      [(Atom Nat)
       (-> (Console Async) (Program Async) (file.System Async) (Shell Async) Resolution (Command [Exit Any]))])
  (let [@runs (is (Atom Nat)
                  (atom.atom 0))]
    [@runs
     (function (_ console program fs shell resolution profile)
       (do [! async.monad]
         [[_ actual_runs] (async.future (atom.update! ++ @runs))]
         (if (n.= expected_runs actual_runs)
           (in {try.#Failure end_signal})
           (do (try.with !)
             [_ (# fs write dummy_file (# utf8.codec encoded (%.nat actual_runs)))
              _ (# fs modify dummy_file (|> actual_runs .int instant.of_millis))]
             (in [shell.normal []])))))]))

(def: .public test
  Test
  (<| (_.covering /._)
      (do [! random.monad]
        [end_signal (random.alphabetic 5)
         .let [/ (# file.default separator)
               [fs watcher] (watch.mock /)]
         
         program (random.alphabetic 5)
         target (random.alphabetic 5)
         source (random.alphabetic 5)
         .let [empty_profile (is Profile
                                 (# ///.monoid identity))
               with_target (is (-> Profile Profile)
                               (has ///.#target target))
               with_program (is (-> Profile Profile)
                                (has ///.#program {.#Some program}))
               
               profile (|> empty_profile
                           with_program
                           with_target
                           (has ///.#sources (set.of_list text.hash (list source))))]

         home (random.alphabetic 5)
         working_directory (random.alphabetic 5)
         
         expected_runs (# ! each (|>> (n.% 10) (n.max 2)) random.nat)
         dummy_path (# ! each (|>> (format source /)) (random.alphabetic 5))
         [compiler resolution] $build.resolution]
        (all _.and
             (_.coverage [/.delay]
               (n.> 0 /.delay))
             (in (do async.monad
                   [verdict (do ///action.monad
                              [_ (# fs make_directory source)
                               _ (# fs write dummy_path (binary.empty 0))
                               .let [[@runs command] (..command expected_runs end_signal fs dummy_path)]
                               _ (# watcher poll [])]
                              (do [! async.monad]
                                [no_dangling_process! (|> profile
                                                          (has ///.#lux compiler)
                                                          ((/.do! 1 watcher command)
                                                           ($version.echo "")
                                                           (program.async (program.mock environment.empty home working_directory))
                                                           fs
                                                           (shell.async ($build.good_shell []))
                                                           resolution)
                                                          (# ! each (|>> (pipe.case
                                                                           {try.#Failure error}
                                                                           (same? end_signal error)

                                                                           {try.#Success _}
                                                                           false))))
                                 correct_number_of_runs! (|> @runs
                                                             atom.read!
                                                             async.future
                                                             (# ! each (n.= expected_runs)))]
                                (in {try.#Success (and correct_number_of_runs!
                                                       no_dangling_process!)})))]
                   (_.coverage' [/.do!]
                     (try.else false verdict))))
             ))))