forked from shakacode/re-formality
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCase.re
129 lines (107 loc) · 2.82 KB
/
Case.re
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
type result = {
actual: (string, string),
expected: (string, string),
};
let testable = Alcotest.(pair(string, string));
let nothing = "";
module Path = {
let join = xs => xs |> String.concat(Filename.dir_sep);
let test_cases_dir =
Filename.([current_dir_name, "ppx", "test", "cases"] |> join);
let source = x => Filename.concat(test_cases_dir, x ++ ".re");
let snapshot = x => Filename.concat(test_cases_dir, x ++ ".snapshot");
let ppx =
Filename.(
concat(
[current_dir_name, "_build", "default", "ppx", "bin"] |> join,
"bin.exe",
)
);
let bsc =
Filename.(
concat([current_dir_name, "node_modules", ".bin"] |> join, "bsc")
);
let rescript_react =
Filename.(
[current_dir_name, "node_modules", "@rescript", "react", "lib", "ocaml"]
|> join
);
let re_formality =
Filename.(
[current_dir_name, "node_modules", "re-formality", "lib", "ocaml"]
|> join
);
};
module Bsc = {
let errors = "+A";
let cmd = case =>
Path.(
bsc
++ " -ppx "
++ ppx
++ " -I "
++ re_formality
++ " -I "
++ rescript_react
++ " -w "
++ errors
++ " -warn-error "
++ errors
++ " -color never"
++ " -bs-cmi-only "
++ (case |> source)
);
};
let env = {
let path = () => "PATH=" ++ Sys.getenv("PATH");
let systemroot = () => "SYSTEMROOT=" ++ Sys.getenv("SYSTEMROOT");
switch (Sys.os_type) {
| "Win32" => [|path(), systemroot()|]
| _ => [|path()|]
};
};
let read_from_channel = channel => {
let buffer = Buffer.create(1024);
let newline = "\n";
try(
while (true) {
channel |> input_line |> Buffer.add_string(buffer);
newline |> Buffer.add_string(buffer);
}
) {
| End_of_file => ()
};
buffer |> Buffer.contents;
};
let run_bsc = case => {
let (stdout, stdin, stderr) = Unix.open_process_full(case |> Bsc.cmd, env);
let res = (stdout |> read_from_channel, stderr |> read_from_channel);
Unix.close_process_full((stdout, stdin, stderr)) |> ignore;
res;
};
let diff_error_snapshot = case => {
let actual = case |> Bsc.cmd;
let snapshot = case |> Path.snapshot;
let cmd =
actual
++ " 2>&1"
++ (
// FIXME: It doesn't work on CI
switch (Sys.os_type) {
| "Win32" => {| | sed "s|\\test\\cases\\|/test/cases/|g"|}
| _ => ""
}
)
++ {| | diff --ignore-blank-lines --ignore-space-change |}
++ snapshot
++ {| -|};
let (stdout, stdin, stderr) = Unix.open_process_full(cmd, env);
let res = (stdout |> read_from_channel, stderr |> read_from_channel);
Unix.close_process_full((stdout, stdin, stderr)) |> ignore;
res;
};
let ok = case => {actual: case |> run_bsc, expected: (nothing, nothing)};
let error = case => {
actual: case |> diff_error_snapshot,
expected: (nothing, nothing),
};