compiler: Add a format check mode
c7176c837fa9b0e71beb157e37d438d9c3875a467dd6da6d71e5bb933abd27c0
There was no way to check that a source is formatted without comparing the output of `-fmt` by hand, and nothing checked commit messages against CONTRIBUTING, so unformatted sources and malformed messages could be committed. `radiance -fmt.check [-fmt.wrap <columns>] <input.rad>` formats the module, prints nothing, and fails with "not formatted" when the result differs from the source. `scripts/check-fmt` runs it over given files, over the staged versions of the sources that `make fmt` and the formatter tests keep formatted, or over all of those sources. Test fixtures are left out, since some are deliberately unformatted. `scripts/check-commit-message` checks a message against the format in CONTRIBUTING. The pre-commit and commit-msg hooks call them. The emulator limits of hosted tools move to `seed/emulator-args`, so the Makefile and the scripts share them. The formatter test formats each source in `test/format/input` at 80 columns and compares the result with the file of the same name in `test/format/output`. It replaces the generated wrap test. Assisted-by: Claude:opus-5.5
1 parent
64ff370d
.githooks/commit-msg
added
+12 -0
| 1 | + | #!/bin/sh |
|
| 2 | + | # Rejects a commit whose message breaks the format described in CONTRIBUTING. |
|
| 3 | + | # The rules live in scripts/check-commit-message. |
|
| 4 | + | ||
| 5 | + | top=$(git rev-parse --show-toplevel) || exit 1 |
|
| 6 | + | ||
| 7 | + | if ! "$top/scripts/check-commit-message" "$1"; then |
|
| 8 | + | echo "commit-msg: see WRITING COMMITS in CONTRIBUTING" >&2 |
|
| 9 | + | echo "commit-msg: the message is kept in $1; retry with" \ |
|
| 10 | + | "'git commit -e -F $1'" >&2 |
|
| 11 | + | exit 1 |
|
| 12 | + | fi |
.githooks/pre-commit
+8 -1
| 1 | 1 | #!/bin/sh |
|
| 2 | - | exec git diff --cached --check |
|
| 2 | + | # Rejects a commit that has whitespace errors, or that stages a Radiance |
|
| 3 | + | # source outside the test fixtures that is not in the canonical style (see |
|
| 4 | + | # scripts/check-fmt). The staged content is checked, not the working tree. |
|
| 5 | + | ||
| 6 | + | top=$(git rev-parse --show-toplevel) || exit 1 |
|
| 7 | + | ||
| 8 | + | git diff --cached --check || exit 1 |
|
| 9 | + | exec "$top/scripts/check-fmt" --staged |
Makefile
+3 -3
| 13 | 13 | # Emulator that runs every image. The integration driver starts its children |
|
| 14 | 14 | # on the same emulator. seed/update reads it from the environment. |
|
| 15 | 15 | RAD_EMULATOR ?= emulator |
|
| 16 | 16 | export RAD_EMULATOR |
|
| 17 | 17 | ||
| 18 | - | # Command that runs hosted build tools with shared memory and stack limits. |
|
| 19 | - | HOSTED := $(RAD_EMULATOR) -memory-size=385024 -data-size=348416 \ |
|
| 20 | - | -stack-size=1024 -count-instructions -run |
|
| 18 | + | # Command that runs hosted build tools with the memory and stack limits in |
|
| 19 | + | # `seed/emulator-args`. |
|
| 20 | + | HOSTED := $(RAD_EMULATOR) $(shell cat seed/emulator-args) -count-instructions -run |
|
| 21 | 21 | RADIANCE := $(HOSTED) $(RAD_BIN) |
|
| 22 | 22 | ||
| 23 | 23 | default: $(RAD_BIN) |
|
| 24 | 24 | ||
| 25 | 25 | # Compiler build |
compiler/radiance.rad
+45 -19
| 94 | 94 | /// Symbol name exported for startup code to call the semantic entry function. |
|
| 95 | 95 | export constant DEFAULT_ENTRY_SYMBOL: *[u8] = "::default"; |
|
| 96 | 96 | ||
| 97 | 97 | /// Usage string. |
|
| 98 | 98 | constant USAGE: *[u8] = |
|
| 99 | - | "usage: radiance -pkg <name> [-start <input.ras>] -mod <input>.. [-pkg <name> -mod <input>..] -entry <pkg> [-o <output> | -ril <directory>]\n-mod accepts a source path or logical/path=source.rad relative to the package root.\n radiance -fmt [-fmt.wrap <columns>] <input.rad>\n-fmt prints the source of a module in the canonical style, wrapped at 100 columns unless -fmt.wrap sets another width.\n"; |
|
| 99 | + | "usage: radiance -pkg <name> [-start <input.ras>] -mod <input>.. [-pkg <name> -mod <input>..] -entry <pkg> [-o <output> | -ril <directory>]\n-mod accepts a source path or logical/path=source.rad relative to the package root.\n radiance -fmt [-fmt.wrap <columns>] <input.rad>\n radiance -fmt.check [-fmt.wrap <columns>] <input.rad>\n-fmt prints the source of a module in the canonical style, wrapped at 100 columns unless -fmt.wrap sets another width.\n-fmt.check prints nothing and fails if the source of a module is not in the canonical style.\n"; |
|
| 100 | 100 | ||
| 101 | 101 | /// Compiler error. |
|
| 102 | 102 | union Error: Copy { |
|
| 103 | 103 | Other, |
|
| 104 | 104 | } |
|
| 105 | 105 | ||
| 106 | + | /// What `-fmt` does with the formatted text. |
|
| 107 | + | union FormatMode: Copy { |
|
| 108 | + | /// Print the formatted source. |
|
| 109 | + | Print, |
|
| 110 | + | /// Print nothing, and fail if the source differs from its formatted text. |
|
| 111 | + | Check, |
|
| 112 | + | } |
|
| 113 | + | ||
| 106 | 114 | /// What to dump during compilation. |
|
| 107 | 115 | union Dump: Copy { |
|
| 108 | 116 | /// Don't dump anything. |
|
| 109 | 117 | None, |
|
| 110 | 118 | /// Dump the parsed AST before semantic analysis. |
| 1493 | 1501 | } |
|
| 1494 | 1502 | ||
| 1495 | 1503 | /// Print the module at `path` in the canonical source style, wrapped at `width` |
|
| 1496 | 1504 | /// columns. |
|
| 1497 | 1505 | /// |
|
| 1498 | - | /// The text is built in the IL text buffer, which no other pass uses here, and |
|
| 1499 | - | /// printed at once. |
|
| 1500 | - | fn formatModule(storage: *mut Storage, path: *[u8], width: u32) throws (Error) { |
|
| 1506 | + | /// The text is built in the IL text buffer, which no other pass uses here. In |
|
| 1507 | + | /// `Print` mode it is printed at once. In `Check` mode nothing is printed, and |
|
| 1508 | + | /// the function throws if the text differs from the source. |
|
| 1509 | + | fn formatModule( |
|
| 1510 | + | storage: *mut Storage, path: *[u8], width: u32, mode: FormatMode |
|
| 1511 | + | ) throws (Error) { |
|
| 1501 | 1512 | let mut sourceArena = alloc::new(&mut storage.moduleSources[..]); |
|
| 1502 | 1513 | let nodes = ast::nodeArena(&mut storage.ast[..]); |
|
| 1503 | 1514 | let pool = strings::freshPool(&mut storage.poolTable[..]); |
|
| 1504 | 1515 | let mut frontend = parser::newParser(nodes, pool); |
|
| 1505 | 1516 | set frontend.triviaMode = parser::TriviaMode::Keep; |
| 1523 | 1534 | layout::text::render(tree, options, sink); |
|
| 1524 | 1535 | } |
|
| 1525 | 1536 | if text.pos >= text.buf.len { |
|
| 1526 | 1537 | throw error(&["fatal:", "formatted text exceeds the output buffer"]); |
|
| 1527 | 1538 | } |
|
| 1528 | - | io::print(&text.buf[.. text.pos]); |
|
| 1539 | + | match mode { |
|
| 1540 | + | case FormatMode::Print => io::print(&text.buf[.. text.pos]), |
|
| 1541 | + | case FormatMode::Check => { |
|
| 1542 | + | if not mem::eq(&text.buf[.. text.pos], source) { |
|
| 1543 | + | throw error(&[path, "not formatted"]); |
|
| 1544 | + | } |
|
| 1545 | + | } |
|
| 1546 | + | } |
|
| 1529 | 1547 | } |
|
| 1530 | 1548 | ||
| 1531 | 1549 | /// Compile the packages named on the command line. |
|
| 1532 | 1550 | /// |
|
| 1533 | 1551 | /// `storage` is the memory of the process. `main` splits it by field, and each |
|
| 1534 | 1552 | /// buffer has one owner: the arenas and tables move to the parser, the module |
|
| 1535 | 1553 | /// graph, the resolver, and the lowering and code generation passes, and the |
|
| 1536 | 1554 | /// output buffers are lent to the one pass that runs. |
|
| 1537 | 1555 | @default fn main(env: *sys::Env, storage: *mut Storage) -> i32 { |
|
| 1538 | - | if env.args.len > 0 and mem::eq(env.args[0], "-fmt") { |
|
| 1556 | + | if env.args.len > 0 and (mem::eq(env.args[0], "-fmt") or mem::eq(env.args[0], "-fmt.check")) { |
|
| 1539 | 1557 | let mut width = FORMAT_WIDTH; |
|
| 1540 | - | let mut first: u32 = 1; |
|
| 1541 | - | if env.args.len > 1 and mem::eq(env.args[1], "-fmt.wrap") { |
|
| 1542 | - | if env.args.len < 3 { |
|
| 1543 | - | io::printError(USAGE); |
|
| 1544 | - | return 1; |
|
| 1558 | + | let mut mode = FormatMode::Print; |
|
| 1559 | + | if mem::eq(env.args[0], "-fmt.check") { |
|
| 1560 | + | set mode = FormatMode::Check; |
|
| 1561 | + | } |
|
| 1562 | + | let mut i: u32 = 1; |
|
| 1563 | + | while i < env.args.len { |
|
| 1564 | + | if mem::eq(env.args[i], "-fmt.wrap") { |
|
| 1565 | + | if i + 1 >= env.args.len { |
|
| 1566 | + | io::printError(USAGE); |
|
| 1567 | + | return 1; |
|
| 1568 | + | } |
|
| 1569 | + | let columns = parseWidth(env.args[i + 1]) else { |
|
| 1570 | + | io::printError(USAGE); |
|
| 1571 | + | return 1; |
|
| 1572 | + | }; |
|
| 1573 | + | set width = columns; |
|
| 1574 | + | set i += 2; |
|
| 1575 | + | } else { |
|
| 1576 | + | break; |
|
| 1545 | 1577 | } |
|
| 1546 | - | let columns = parseWidth(env.args[2]) else { |
|
| 1547 | - | io::printError(USAGE); |
|
| 1548 | - | return 1; |
|
| 1549 | - | }; |
|
| 1550 | - | set width = columns; |
|
| 1551 | - | set first = 3; |
|
| 1552 | 1578 | } |
|
| 1553 | - | if env.args.len <> first + 1 { |
|
| 1579 | + | if env.args.len <> i + 1 { |
|
| 1554 | 1580 | io::printError(USAGE); |
|
| 1555 | 1581 | return 1; |
|
| 1556 | 1582 | } |
|
| 1557 | - | try formatModule(storage, env.args[first], width) catch { |
|
| 1583 | + | try formatModule(storage, env.args[i], width, mode) catch { |
|
| 1558 | 1584 | return 1; |
|
| 1559 | 1585 | }; |
|
| 1560 | 1586 | return 0; |
|
| 1561 | 1587 | } |
|
| 1562 | 1588 | let mut driverArena = alloc::new(&mut storage.driver[..]); |
scripts/check-commit-message
added
+73 -0
| 1 | + | #!/bin/sh |
|
| 2 | + | # Checks a commit message against the format described in CONTRIBUTING: |
|
| 3 | + | # summary style and length, blank line after it, trailer syntax and body line |
|
| 4 | + | # width. Git-generated subjects (fixup!, squash!, amend!, Merge) are |
|
| 5 | + | # not checked. |
|
| 6 | + | # |
|
| 7 | + | # Usage: check-commit-message FILE |
|
| 8 | + | # |
|
| 9 | + | # FILE `-` reads standard input. Comment lines and everything after the |
|
| 10 | + | # scissors line are ignored, as git does when cleaning up a message. Problems |
|
| 11 | + | # are printed on standard error and make the exit status 1. |
|
| 12 | + | ||
| 13 | + | if [ $# -ne 1 ]; then |
|
| 14 | + | echo "usage: check-commit-message FILE" >&2 |
|
| 15 | + | exit 2 |
|
| 16 | + | fi |
|
| 17 | + | msg=$1 |
|
| 18 | + | if [ "$msg" != - ] && [ ! -f "$msg" ]; then |
|
| 19 | + | echo "check-commit-message: $msg: no such file" >&2 |
|
| 20 | + | exit 2 |
|
| 21 | + | fi |
|
| 22 | + | ||
| 23 | + | cc=$(git config core.commentChar 2>/dev/null) |
|
| 24 | + | case $cc in |
|
| 25 | + | '' | auto) cc='#' ;; |
|
| 26 | + | esac |
|
| 27 | + | ||
| 28 | + | [ "$msg" = - ] && msg=/dev/stdin |
|
| 29 | + | ||
| 30 | + | problems=$(COMMENT_CHAR=$cc awk ' |
|
| 31 | + | BEGIN { cc = ENVIRON["COMMENT_CHAR"]; n = 0 } |
|
| 32 | + | ||
| 33 | + | # Mimic git cleanup: drop comment lines, stop at the scissors line. |
|
| 34 | + | index($0, cc " ------------------------ >8 ------------------------") == 1 { exit } |
|
| 35 | + | index($0, cc) == 1 { next } |
|
| 36 | + | { lines[++n] = $0 } |
|
| 37 | + | ||
| 38 | + | END { |
|
| 39 | + | first = 1 |
|
| 40 | + | while (first <= n && lines[first] ~ /^[ \t]*$/) first++ |
|
| 41 | + | last = n |
|
| 42 | + | while (last >= first && lines[last] ~ /^[ \t]*$/) last-- |
|
| 43 | + | ||
| 44 | + | subject = lines[first] |
|
| 45 | + | if (subject ~ /^(fixup|squash|amend)! / || subject ~ /^Merge /) exit |
|
| 46 | + | ||
| 47 | + | text = subject |
|
| 48 | + | if (match(text, /^[a-z0-9_.\/-]+: /)) text = substr(text, RLENGTH + 1) |
|
| 49 | + | if (subject == "") print "empty summary" |
|
| 50 | + | else if (text !~ /^[A-Z`]/) print "summary is not capitalized" |
|
| 51 | + | if (subject ~ /\.$/) print "summary ends with a period" |
|
| 52 | + | if (subject ~ /[ \t]$/) print "summary ends with whitespace" |
|
| 53 | + | if (length(subject) > 50) print "summary is longer than 50 characters" |
|
| 54 | + | ||
| 55 | + | for (i = first + 1; i <= last; i++) { |
|
| 56 | + | line = lines[i] |
|
| 57 | + | ln = i - first + 1 |
|
| 58 | + | if (ln == 2 && line != "") print "no blank line after the summary" |
|
| 59 | + | if (line ~ /^Assisted-by:/) { |
|
| 60 | + | if (line !~ /^Assisted-by: [^: ]+:[^ ]+$/) print "malformed trailer: " line |
|
| 61 | + | continue |
|
| 62 | + | } |
|
| 63 | + | if (line ~ /^[A-Z][A-Za-z]*(-[A-Za-z]+)+: /) continue |
|
| 64 | + | if (line ~ /[ \t]$/) print "line " ln " ends with whitespace" |
|
| 65 | + | if (length(line) > 72 && line !~ /^ / && line !~ /https?:\/\//) |
|
| 66 | + | print "line " ln " is longer than 72 characters" |
|
| 67 | + | } |
|
| 68 | + | } |
|
| 69 | + | ' "$msg") |
|
| 70 | + | ||
| 71 | + | [ -n "$problems" ] || exit 0 |
|
| 72 | + | printf '%s\n' "$problems" | sed 's/^/check-commit-message: /' >&2 |
|
| 73 | + | exit 1 |
scripts/check-fmt
added
+72 -0
| 1 | + | #!/bin/sh |
|
| 2 | + | # Checks that Radiance sources are in the canonical style, without changing |
|
| 3 | + | # them. Run from the repository top level. |
|
| 4 | + | # |
|
| 5 | + | # Usage: check-fmt [--staged | FILE...] |
|
| 6 | + | # |
|
| 7 | + | # FILE... check the given files in the working tree |
|
| 8 | + | # --staged check the staged versions of staged sources in the formatted scope |
|
| 9 | + | # (none) check every tracked source in the formatted scope |
|
| 10 | + | # |
|
| 11 | + | # The compiler is rebuilt first when it is stale. Every unformatted file is |
|
| 12 | + | # reported; the exit status is 1 if there is any. |
|
| 13 | + | ||
| 14 | + | # The scope globs below are git pathspecs, not shell patterns. |
|
| 15 | + | set -f |
|
| 16 | + | ||
| 17 | + | # Sources that are kept in the canonical style. |
|
| 18 | + | scope="lib/*.rad compiler/*.rad kernel/*.rad test/integration/*.rad |
|
| 19 | + | test/host/*.rad test/integration.rad" |
|
| 20 | + | ||
| 21 | + | # Memory and stack limits of hosted build tools. |
|
| 22 | + | limits=$(cat seed/emulator-args) || exit 2 |
|
| 23 | + | ||
| 24 | + | rad=bin/radiance.rv64.dev |
|
| 25 | + | emulator=${RAD_EMULATOR:-emulator} |
|
| 26 | + | ||
| 27 | + | if ! make -s "$rad" >/dev/null; then |
|
| 28 | + | echo "check-fmt: cannot build $rad" >&2 |
|
| 29 | + | exit 2 |
|
| 30 | + | fi |
|
| 31 | + | ||
| 32 | + | tmp= |
|
| 33 | + | trap '[ -z "$tmp" ] || rm -rf "$tmp"' EXIT |
|
| 34 | + | prefix= |
|
| 35 | + | ||
| 36 | + | case ${1-} in |
|
| 37 | + | --staged) |
|
| 38 | + | [ $# -eq 1 ] || { echo "usage: check-fmt [--staged | FILE...]" >&2; exit 2; } |
|
| 39 | + | # shellcheck disable=SC2086 |
|
| 40 | + | files=$(git diff --cached --name-only --diff-filter=ACMR -- $scope) |
|
| 41 | + | [ -n "$files" ] || exit 0 |
|
| 42 | + | tmp=$(mktemp -d) || exit 2 |
|
| 43 | + | prefix=$tmp/ |
|
| 44 | + | # shellcheck disable=SC2086 |
|
| 45 | + | git checkout-index --prefix="$prefix" -- $files || exit 2 |
|
| 46 | + | ;; |
|
| 47 | + | '') |
|
| 48 | + | # shellcheck disable=SC2086 |
|
| 49 | + | files=$(git ls-files -- $scope) |
|
| 50 | + | ;; |
|
| 51 | + | *) |
|
| 52 | + | files=$(printf '%s\n' "$@") |
|
| 53 | + | ;; |
|
| 54 | + | esac |
|
| 55 | + | ||
| 56 | + | status=0 |
|
| 57 | + | for f in $files; do |
|
| 58 | + | # shellcheck disable=SC2086 |
|
| 59 | + | out=$("$emulator" $limits -run "$rad" -fmt.check "$prefix$f" 2>&1) || { |
|
| 60 | + | if [ -n "$prefix" ]; then |
|
| 61 | + | printf '%s\n' "$out" | sed "s|$prefix||g" >&2 |
|
| 62 | + | else |
|
| 63 | + | printf '%s\n' "$out" >&2 |
|
| 64 | + | fi |
|
| 65 | + | status=1 |
|
| 66 | + | } |
|
| 67 | + | done |
|
| 68 | + | ||
| 69 | + | if [ "$status" -ne 0 ]; then |
|
| 70 | + | echo "check-fmt: run 'make fmt' or 'radiance -fmt' on the files above" >&2 |
|
| 71 | + | fi |
|
| 72 | + | exit $status |
seed/emulator-args
added
+1 -0
| 1 | + | -memory-size=385024 -data-size=348416 -stack-size=1024 |
test/format/input/basic.rad
added
+3 -0
| 1 | + | fn f(a:u32,b:u32)->u32{let total=compute(first,second,third,fourth,fifth,sixth, |
|
| 2 | + | seventh,eighth,ninth);if total>limit and total<maximum or overridden{return |
|
| 3 | + | total+offset*scale;}return 0;} |
test/format/output/basic.rad
added
+9 -0
| 1 | + | fn f(a: u32, b: u32) -> u32 { |
|
| 2 | + | let total = compute( |
|
| 3 | + | first, second, third, fourth, fifth, sixth, seventh, eighth, ninth |
|
| 4 | + | ); |
|
| 5 | + | if (total > limit and total < maximum) or overridden { |
|
| 6 | + | return total + offset * scale; |
|
| 7 | + | } |
|
| 8 | + | return 0; |
|
| 9 | + | } |
test/integration/format.rad
+47 -38
| 11 | 11 | use super::harness::Expect; |
|
| 12 | 12 | use std::process::Outcome; |
|
| 13 | 13 | use std::process::Output; |
|
| 14 | 14 | use std::process::Command; |
|
| 15 | 15 | ||
| 16 | - | /// Number of lines of the wide statement formatted at the default width. |
|
| 17 | - | constant WIDE_LINES: u32 = 3; |
|
| 18 | - | /// Number of times the argument is repeated in the wide statement. The |
|
| 19 | - | /// statement is 95 columns wide, so it wraps at 80 columns only. |
|
| 20 | - | constant WIDE_ARGUMENTS: u32 = 8; |
|
| 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"; |
|
| 21 | 23 | ||
| 22 | 24 | /// A formatter run on each of a list of inputs. |
|
| 23 | 25 | union Tool: Copy { |
|
| 24 | 26 | /// Format the input. |
|
| 25 | 27 | Format, |
| 77 | 79 | and h.expect(source, h.join(&[a, ".ast"]), Expect::Same(h.join(&[b, ".ast"]))); |
|
| 78 | 80 | if ok { |
|
| 79 | 81 | set roundTrips += 1; |
|
| 80 | 82 | } |
|
| 81 | 83 | } |
|
| 84 | + | try fixtures(h); |
|
| 82 | 85 | try errors(h); |
|
| 83 | 86 | let failed = h.failed; |
|
| 84 | 87 | io::print(h.join( |
|
| 85 | 88 | &["fmt: ", h.num(roundTrips), " source files round-trip, ", h.num(failed), " failed\n"] |
|
| 86 | 89 | )); |
| 99 | 102 | let mut command = h.cc(args, Output::File(out)); |
|
| 100 | 103 | set command.stderr = Output::File(err); |
|
| 101 | 104 | return h.run(what, command, Outcome::Exited(status), &[]); |
|
| 102 | 105 | } |
|
| 103 | 106 | ||
| 104 | - | /// Count the lines of the text. |
|
| 105 | - | fn lineCount(text: *[u8]) -> u32 { |
|
| 106 | - | let mut count: u32 = 0; |
|
| 107 | - | let mut lines = mem::split(text, '\n'); |
|
| 108 | - | while let line = lines.next() { |
|
| 109 | - | set count += 1; |
|
| 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 | + | let name = mem::stripPrefix(prefix, input) else { |
|
| 115 | + | panic "fixtures: unexpected path"; |
|
| 116 | + | }; |
|
| 117 | + | let what = h.join(&["fmt fixture: ", name]); |
|
| 118 | + | format(h, what, &["-fmt", "-fmt.wrap", FIXTURE_WIDTH, input], 0, out, err); |
|
| 119 | + | h.expect(what, out, Expect::Same(h.join(&[FIXTURE_OUTPUT, "/", name]))); |
|
| 110 | 120 | } |
|
| 111 | - | return count; |
|
| 112 | 121 | } |
|
| 113 | 122 | ||
| 114 | 123 | /// Check how the formatter reports a syntax error, a wrong command line, and |
|
| 115 | - | /// how it applies the wrap width. |
|
| 124 | + | /// how `-fmt.check` reports unformatted source. |
|
| 116 | 125 | fn errors(h: &mut Harness) throws (sys::Error) { |
|
| 117 | 126 | let bad = h.path(&["bad.rad"]); |
|
| 118 | 127 | let out = h.path(&["errors.out"]); |
|
| 119 | 128 | let err = h.path(&["errors.err"]); |
|
| 120 | - | let range = h.path(&["range.rad"]); |
|
| 121 | - | try fs::writeFile(range, "fn f() { let x = a+1..b+2; let y = a..; let z = ..b; }\n"); |
|
| 122 | - | format(h, "fmt ranges", &["-fmt", range], 0, out, err); |
|
| 123 | - | h.expect("fmt ranges", out, Expect::Exact( |
|
| 124 | - | "fn f() {\n let x = a + 1 .. b + 2;\n let y = a ..;\n let z = .. b;\n}\n" |
|
| 125 | - | )); |
|
| 129 | + | let unformatted = h.path(&["unformatted.rad"]); |
|
| 130 | + | try fs::writeFile(unformatted, "fn f() { let x = a+1; }\n"); |
|
| 126 | 131 | try fs::writeFile(bad, "let x = ;\n"); |
|
| 132 | + | // `-fmt.check` prints nothing and fails when the source is not formatted. |
|
| 133 | + | format(h, "fmt check: unformatted", &["-fmt.check", unformatted], 1, out, err); |
|
| 134 | + | h.expect("fmt check: unformatted: standard output", out, Expect::Exact("")); |
|
| 135 | + | h.expect("fmt check: unformatted: diagnostic", err, Expect::Contains("not formatted")); |
|
| 136 | + | let formatted = h.path(&["formatted.rad"]); |
|
| 137 | + | format(h, "fmt check: format", &["-fmt", unformatted], 0, formatted, err); |
|
| 138 | + | format(h, "fmt check: formatted", &["-fmt.check", formatted], 0, out, err); |
|
| 139 | + | h.expect("fmt check: formatted: standard output", out, Expect::Exact("")); |
|
| 140 | + | h.expect("fmt check: formatted: diagnostic", err, Expect::Exact("")); |
|
| 141 | + | // The wrap width applies in check mode too. |
|
| 142 | + | format(h, "fmt check: wrap", &["-fmt.check", "-fmt.wrap", "80", formatted], 0, out, err); |
|
| 143 | + | format( |
|
| 144 | + | h, |
|
| 145 | + | "fmt check: wrap unformatted", |
|
| 146 | + | &["-fmt.check", "-fmt.wrap", "80", unformatted], |
|
| 147 | + | 1, |
|
| 148 | + | out, |
|
| 149 | + | err |
|
| 150 | + | ); |
|
| 127 | 151 | // A syntax error is reported on standard error only, so that standard |
|
| 128 | 152 | // output holds nothing but formatted text. |
|
| 129 | 153 | format(h, "fmt syntax error", &["-fmt", bad], 1, out, err); |
|
| 130 | 154 | h.expect("fmt syntax error: standard output", out, Expect::Exact("")); |
|
| 131 | 155 | h.expect("fmt syntax error: diagnostic", err, Expect::Contains("error: ")); |
|
| 132 | 156 | // A wrong command line exits with status 1 and prints the usage. |
|
| 133 | - | let usage: [*[*[u8]]; 5] = [ |
|
| 157 | + | let usage: [*[*[u8]]; 8] = [ |
|
| 134 | 158 | h.copy(&["-fmt"]), |
|
| 135 | 159 | h.copy(&["-fmt", bad, bad]), |
|
| 136 | 160 | h.copy(&["-fmt", "-fmt.wrap", "0", bad]), |
|
| 137 | 161 | h.copy(&["-fmt", "-fmt.wrap", "x", bad]), |
|
| 138 | 162 | h.copy(&["-fmt", "-fmt.wrap"]), |
|
| 163 | + | h.copy(&["-fmt.check"]), |
|
| 164 | + | h.copy(&["-fmt", "-fmt.check", bad]), |
|
| 165 | + | h.copy(&["-fmt.check", "-fmt", bad]), |
|
| 139 | 166 | ]; |
|
| 140 | 167 | for args in &usage[..] { |
|
| 141 | 168 | format(h, "fmt usage", args, 1, h.path(&["usage.out"]), err); |
|
| 142 | 169 | h.expect("fmt usage", err, Expect::Prefix("usage:")); |
|
| 143 | 170 | } |
|
| 144 | - | // The width defaults to 100 columns and `-fmt.wrap` sets it. |
|
| 145 | - | let wide = h.path(&["wide.rad"]); |
|
| 146 | - | let mut statement: *mut [u8] = &mut []; |
|
| 147 | - | for i in 0 .. WIDE_ARGUMENTS { |
|
| 148 | - | try! statement.extend("argument, ", &mut h.arena); |
|
| 149 | - | } |
|
| 150 | - | try fs::writeFileParts(wide, &["fn f() {\n call(", &statement[..], "last);\n}\n"]); |
|
| 151 | - | format(h, "fmt wrap: default", &["-fmt", wide], 0, out, err); |
|
| 152 | - | let normal = lineCount(try h.load(out)); |
|
| 153 | - | format(h, "fmt wrap: 80 columns", &["-fmt", "-fmt.wrap", "80", wide], 0, out, err); |
|
| 154 | - | let narrow = lineCount(try h.load(out)); |
|
| 155 | - | if normal == WIDE_LINES and narrow > WIDE_LINES { |
|
| 156 | - | h.pass(); |
|
| 157 | - | } else { |
|
| 158 | - | h.fail("fmt wrap", h.join( |
|
| 159 | - | &["default ", h.num(normal), " lines, narrow ", h.num(narrow), " lines"] |
|
| 160 | - | )); |
|
| 161 | - | } |
|
| 162 | 171 | } |
|
| 163 | 172 | ||
| 164 | 173 | /// Format each source in place with the formatter. A source is rewritten only |
|
| 165 | 174 | /// when its text changes. Every source that cannot be formatted is reported and |
|
| 166 | 175 | /// fails the command. |