test/integration/format.rad 7.7 KiB raw
1
//! The source formatter: checking that it round-trips every source and
2
//! handles bad input, and formatting sources in place.
3
4
use std::fs;
5
use std::io;
6
use std::mem;
7
use std::process;
8
use std::sys;
9
use super::harness;
10
use super::harness::Harness;
11
use super::harness::Expect;
12
use std::process::Outcome;
13
use std::process::Output;
14
use std::process::Command;
15
16
/// Directory of sources to format. Each has a file of the same name in
17
/// `FIXTURE_OUTPUT`.
18
constant FIXTURE_INPUT: *[u8] = "test/format/input";
19
/// Directory of the expected formatter output for each source in `FIXTURE_INPUT`.
20
constant FIXTURE_OUTPUT: *[u8] = "test/format/output";
21
/// Wrap width, in columns, at which the fixtures are formatted.
22
constant FIXTURE_WIDTH: *[u8] = "80";
23
24
/// A formatter run on each of a list of inputs.
25
union Tool: Copy {
26
    /// Format the input.
27
    Format,
28
    /// Dump the AST of the input.
29
    Dump,
30
}
31
32
/// Run the tool on every input, in a batch. The output of input `i` is
33
/// `<i><tag>.rad` for formatting and `<i><tag>.ast` for the dump, and its log is
34
/// that name plus `.log`. Returns the outcomes.
35
fn batch(
36
    h: &mut Harness, inputs: *[*[u8]], tool: Tool, tag: *[u8]
37
) -> *[Outcome] {
38
    let mut commands: *mut [Command] = &mut [];
39
    for input, i in inputs {
40
        let base = h.path(&[h.num(i), tag]);
41
        let mut out = h.join(&[base, ".ast"]);
42
        let mut args: *[*[u8]] = h.copy(&["-pkg", "fmt", "-mod", input, "-dump", "ast"]);
43
        if tool == Tool::Format {
44
            set out = h.join(&[base, ".rad"]);
45
            set args = h.copy(&["-fmt", input]);
46
        }
47
        let mut command = h.cc(args, Output::File(out));
48
        set command.stderr = Output::File(h.join(&[out, ".log"]));
49
        try! commands.append(command, h.arena);
50
    }
51
    return h.runAll(&commands[..]);
52
}
53
54
/// Check that the formatter round-trips every one of the `sources`: each
55
/// source formats, its output formats to itself, and the AST of the output
56
/// equals the AST of the source. Then check the formatter's error handling and
57
/// wrap width.
58
export fn check(h: &mut Harness, sources: *[*[u8]]) throws (sys::Error) {
59
    try h.begin("fmt");
60
    let mut formatted: *mut [*[u8]] = &mut [];
61
    for i in 0 .. sources.len {
62
        try! formatted.append(h.path(&[h.num(i), "a.rad"]), h.arena);
63
    }
64
    // The output of each source is formatted again and dumped.
65
    let again = &formatted[..];
66
    let format = batch(h, sources, Tool::Format, "a");
67
    let dump = batch(h, sources, Tool::Dump, "a");
68
    let formatAgain = batch(h, again, Tool::Format, "b");
69
    let dumpAgain = batch(h, again, Tool::Dump, "b");
70
    let mut roundTrips: u32 = 0;
71
    for source, i in sources {
72
        let a = h.path(&[h.num(i), "a"]);
73
        let b = h.path(&[h.num(i), "b"]);
74
        let ok = h.outcome(source, format[i], harness::OK, h.join(&[a, ".rad.log"]))
75
            and h.outcome(source, formatAgain[i], harness::OK, h.join(&[b, ".rad.log"]))
76
            and h.expect(source, h.join(&[a, ".rad"]), Expect::Same(h.join(&[b, ".rad"])))
77
            and h.outcome(source, dump[i], harness::OK, h.join(&[a, ".ast.log"]))
78
            and h.outcome(source, dumpAgain[i], harness::OK, h.join(&[b, ".ast.log"]))
79
            and h.expect(source, h.join(&[a, ".ast"]), Expect::Same(h.join(&[b, ".ast"])));
80
        if ok {
81
            set roundTrips += 1;
82
        }
83
    }
84
    try fixtures(h);
85
    try errors(h);
86
    let failed = h.failed;
87
    io::print(h.join(
88
        &["fmt: ", h.num(roundTrips), " source files round-trip, ", h.num(failed), " failed\n"]
89
    ));
90
}
91
92
/// Run the formatter on `args` with standard output in `out` and standard
93
/// error in `err`, and check that it exits with `status`.
94
fn format(
95
    h: &mut Harness,
96
    what: *[u8],
97
    args: &[*[u8]],
98
    status: i32,
99
    out: *[u8],
100
    err: *[u8]
101
) -> bool {
102
    let mut command = h.cc(args, Output::File(out));
103
    set command.stderr = Output::File(err);
104
    return h.run(what, command, Outcome::Exited(status), &[]);
105
}
106
107
/// Format each source in `FIXTURE_INPUT` at `FIXTURE_WIDTH` columns and check
108
/// that the output equals the file of the same name in `FIXTURE_OUTPUT`.
109
fn fixtures(h: &mut Harness) throws (sys::Error) {
110
    let out = h.path(&["fixture.out"]);
111
    let err = h.path(&["fixture.err"]);
112
    let prefix = h.join(&[FIXTURE_INPUT, "/"]);
113
    for input in try h.listFiles(FIXTURE_INPUT, ".rad") {
114
        if not mem::startsWith(input, prefix) {
115
            panic "fixtures: unexpected path";
116
        }
117
        let name = &input[prefix.len ..];
118
        let what = h.join(&["fmt fixture: ", name]);
119
        format(h, what, &["-fmt", "-fmt.wrap", FIXTURE_WIDTH, input], 0, out, err);
120
        h.expect(what, out, Expect::Same(h.join(&[FIXTURE_OUTPUT, "/", name])));
121
    }
122
}
123
124
/// Check how the formatter reports a syntax error, a wrong command line, and
125
/// how `-fmt.check` reports unformatted source.
126
fn errors(h: &mut Harness) throws (sys::Error) {
127
    let bad = h.path(&["bad.rad"]);
128
    let out = h.path(&["errors.out"]);
129
    let err = h.path(&["errors.err"]);
130
    let unformatted = h.path(&["unformatted.rad"]);
131
    try fs::writeFile(unformatted, "fn f() { let x = a+1; }\n");
132
    try fs::writeFile(bad, "let x = ;\n");
133
    // `-fmt.check` prints nothing and fails when the source is not formatted.
134
    format(h, "fmt check: unformatted", &["-fmt.check", unformatted], 1, out, err);
135
    h.expect("fmt check: unformatted: standard output", out, Expect::Exact(""));
136
    h.expect("fmt check: unformatted: diagnostic", err, Expect::Contains("not formatted"));
137
    let formatted = h.path(&["formatted.rad"]);
138
    format(h, "fmt check: format", &["-fmt", unformatted], 0, formatted, err);
139
    format(h, "fmt check: formatted", &["-fmt.check", formatted], 0, out, err);
140
    h.expect("fmt check: formatted: standard output", out, Expect::Exact(""));
141
    h.expect("fmt check: formatted: diagnostic", err, Expect::Exact(""));
142
    // The wrap width applies in check mode too.
143
    format(h, "fmt check: wrap", &["-fmt.check", "-fmt.wrap", "80", formatted], 0, out, err);
144
    format(
145
        h,
146
        "fmt check: wrap unformatted",
147
        &["-fmt.check", "-fmt.wrap", "80", unformatted],
148
        1,
149
        out,
150
        err
151
    );
152
    // A syntax error is reported on standard error only, so that standard
153
    // output holds nothing but formatted text.
154
    format(h, "fmt syntax error", &["-fmt", bad], 1, out, err);
155
    h.expect("fmt syntax error: standard output", out, Expect::Exact(""));
156
    h.expect("fmt syntax error: diagnostic", err, Expect::Contains("error: "));
157
    // A wrong command line exits with status 1 and prints the usage.
158
    let usage: [*[*[u8]]; 8] = [
159
        h.copy(&["-fmt"]),
160
        h.copy(&["-fmt", bad, bad]),
161
        h.copy(&["-fmt", "-fmt.wrap", "0", bad]),
162
        h.copy(&["-fmt", "-fmt.wrap", "x", bad]),
163
        h.copy(&["-fmt", "-fmt.wrap"]),
164
        h.copy(&["-fmt.check"]),
165
        h.copy(&["-fmt", "-fmt.check", bad]),
166
        h.copy(&["-fmt.check", "-fmt", bad]),
167
    ];
168
    for args in &usage[..] {
169
        format(h, "fmt usage", args, 1, h.path(&["usage.out"]), err);
170
        h.expect("fmt usage", err, Expect::Prefix("usage:"));
171
    }
172
}
173
174
/// Format each source in place with the formatter. A source is rewritten only
175
/// when its text changes. Every source that cannot be formatted is reported and
176
/// fails the command.
177
export fn rewrite(h: &mut Harness, sources: *[*[u8]]) throws (sys::Error) {
178
    try h.begin("format");
179
    let outcomes = batch(h, sources, Tool::Format, "");
180
    for source, i in sources {
181
        let formatted = h.path(&[h.num(i), ".rad"]);
182
        if h.outcome(source, outcomes[i], harness::OK, h.join(&[formatted, ".log"]))
183
            and try h.compare(source, formatted) <> nil
184
        {
185
            try fs::writeFile(source, try h.load(formatted));
186
            io::print(h.join(&["fmt ", source, "\n"]));
187
        }
188
    }
189
}