mdtablefix 0.6.1

`mdtablefix` unb0rks and reflows Markdown tables so that each column has a uniform width. When the `--wrap` option is used, it also wraps paragraphs and list items to 80 columns.
Documentation
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
//! Integration tests for CLI interface behaviour of the `mdtablefix` tool.
//!
//! This module validates the command-line interface functionality, including:
//! - File handling with the `--in-place` flag
//! - Ellipsis replacement with the `--ellipsis` option
//! - Error handling for invalid argument combinations
//! - Processing of Markdown files through the CLI interface

use assert_cmd::Command;
use camino::{Utf8Path, Utf8PathBuf};
use cap_std::{ambient_authority, fs_utf8::Dir};
use rstest::rstest;
use tempfile::{TempDir, tempdir};

#[macro_use]
#[path = "common/mod.rs"]
mod common;
#[path = "cli/ellipsis.rs"]
mod ellipsis;
#[path = "support/fixtures.rs"]
mod fixtures;
use fixtures::broken_table;

/// Verifies that the CLI fails when the `--in-place` flag is used without specifying a file.
///
/// This test ensures that running `mdtablefix --in-place` without a file argument results in a
/// command failure.
#[test]
fn test_cli_in_place_requires_file() {
    Command::cargo_bin("mdtablefix")
        .expect("Failed to create cargo command for mdtablefix")
        .arg("--in-place")
        .assert()
        .failure();
}

/// Verifies that the `--version` flag prints the crate version and exits.
#[test]
fn test_cli_version_flag() {
    Command::cargo_bin("mdtablefix")
        .expect("Failed to create cargo command for mdtablefix")
        .arg("--version")
        .assert()
        .success()
        .stdout(format!("mdtablefix {}\n", env!("CARGO_PKG_VERSION")));
}

/// Tests that the CLI processes a file containing a broken Markdown table and outputs the corrected
/// table to stdout.
///
/// This test creates a temporary file with a malformed table, runs the `mdtablefix` binary on it,
/// and asserts that the output is the expected fixed table.
#[rstest]
fn test_cli_process_file(broken_table: Vec<String>) {
    let dir = tempdir().expect("failed to create temporary directory");
    let (directory, parent_path) = capability_directory(&dir);
    let file_name = Utf8Path::new("sample.md");
    directory
        .write(file_name, format!("{}\n", broken_table.join("\n")))
        .expect("failed to write temporary file");
    Command::cargo_bin("mdtablefix")
        .expect("Failed to create cargo command for mdtablefix")
        .arg(parent_path.join(file_name).as_std_path())
        .assert()
        .success()
        .stdout("| A | B |\n| 1 | 2 |\n| 3 | 4 |\n");
}

/// Snapshots equivalent stdout and in-place output for a representative file.
#[test]
fn cli_output_modes_snapshot_table_prose() {
    let input = include_str!("data/cli-output-parity.dat");
    let expected = include_str!("data/cli-output-parity.expected.md");
    let dir = tempdir().expect("failed to create temporary directory");
    let (directory, parent_path) = capability_directory(&dir);
    let stdout_path = parent_path.join("stdout.md");
    let in_place_path = parent_path.join("in-place.md");
    directory
        .write("stdout.md", input)
        .expect("failed to write stdout fixture");
    directory
        .write("in-place.md", input)
        .expect("failed to write in-place fixture");

    let stdout = Command::cargo_bin("mdtablefix")
        .expect("failed to create cargo command for mdtablefix")
        .arg(stdout_path.as_std_path())
        .output()
        .expect("failed to run stdout command");
    assert!(
        stdout.status.success(),
        "stdout command failed: {}",
        String::from_utf8_lossy(&stdout.stderr)
    );
    let stdout_text = String::from_utf8_lossy(&stdout.stdout);
    assert_eq!(stdout_text, expected, "stdout output must match the oracle");
    assert_eq!(
        directory
            .read_to_string("stdout.md")
            .expect("failed to read stdout fixture"),
        input,
        "stdout formatting must not modify the input file"
    );
    insta::assert_snapshot!("format_to_string_table_prose", stdout_text);

    let in_place = Command::cargo_bin("mdtablefix")
        .expect("failed to create cargo command for mdtablefix")
        .args(["--in-place", in_place_path.as_str()])
        .output()
        .expect("failed to run in-place command");
    assert!(
        in_place.status.success(),
        "in-place command failed: {}",
        String::from_utf8_lossy(&in_place.stderr)
    );
    assert!(
        in_place.stdout.is_empty(),
        "in-place command wrote unexpected stdout: {}",
        String::from_utf8_lossy(&in_place.stdout)
    );
    let rewritten = directory
        .read_to_string("in-place.md")
        .expect("failed to read rewritten fixture");
    assert_eq!(rewritten, expected, "in-place output must match the oracle");
    insta::assert_snapshot!("rewrite_in_place_table_prose", rewritten);
}

/// Tests that the `--fences` option normalizes backtick fences.
#[test]
fn test_cli_fences_option() {
    Command::cargo_bin("mdtablefix")
        .expect("Failed to create cargo command for mdtablefix")
        .arg("--fences")
        .write_stdin("````rust\nfn main() {}\n````\n")
        .assert()
        .success()
        .stdout("```rust\nfn main() {}\n```\n");
}

/// Tests that the `--headings` option converts Setext headings to ATX headings.
#[test]
fn test_cli_headings_option() {
    Command::cargo_bin("mdtablefix")
        .expect("Failed to create cargo command for mdtablefix")
        .arg("--headings")
        .write_stdin("Title\n=====\n")
        .assert()
        .success()
        .stdout("# Title\n");
}

/// Verifies that Setext headings are left untouched unless `--headings` is provided.
#[test]
fn test_cli_headings_disabled_by_default() {
    Command::cargo_bin("mdtablefix")
        .expect("Failed to create cargo command for mdtablefix")
        .write_stdin("Heading\n-----\n")
        .assert()
        .success()
        .stdout("Heading\n-----\n");
}

/// Ensures the `--headings` option ignores short underline markers to avoid false positives.
#[test]
fn test_cli_headings_requires_long_marker() {
    Command::cargo_bin("mdtablefix")
        .expect("Failed to create cargo command for mdtablefix")
        .arg("--headings")
        .write_stdin("Maybe not\n==\n")
        .assert()
        .success()
        .stdout("Maybe not\n==\n");
}

/// Ensures blockquote paragraphs are not turned into headings when underline markers lack the
/// corresponding quote prefix.
#[test]
fn test_cli_headings_preserves_blockquote_paragraphs() {
    Command::cargo_bin("mdtablefix")
        .expect("Failed to create cargo command for mdtablefix")
        .arg("--headings")
        .write_stdin("> Quote\n-----\n")
        .assert()
        .success()
        .stdout("> Quote\n-----\n");
}

/// Ensures the `--headings` option rewrites blockquote headings while keeping
/// the quote prefix.
#[test]
fn test_cli_headings_blockquote_conversion() {
    Command::cargo_bin("mdtablefix")
        .expect("Failed to create cargo command for mdtablefix")
        .arg("--headings")
        .write_stdin("> Quote\n> ----\n")
        .assert()
        .success()
        .stdout("> ## Quote\n");
}

/// Ensures a candidate that is itself a block start does not become a heading.
///
/// `--headings` consumes the following `---`/`===` line as an underline, so a
/// candidate that is already a block would swallow the line below it: `## aa`
/// above `---` produced the single line `## ## aa`, and the break was lost.
/// Every case must therefore survive unchanged.
#[rstest]
#[case("## aa\n---\n")]
#[case("# aa\n===\n")]
#[case("###### aa ######\n---\n")]
#[case("> ## aa\n> ---\n")]
#[case("---\n---\n")]
#[case("***\n---\n")]
#[case("- item\n---\n")]
#[case("1. item\n---\n")]
#[case("  - item\n  ---\n")]
#[case("[^1]: note\n---\n")]
#[case("[label]: https://example.com\n---\n")]
#[case("<!-- markdownlint-disable MD013 -->\n---\n")]
fn test_cli_headings_preserves_block_starts(#[case] input: &'static str) {
    Command::cargo_bin("mdtablefix")
        .expect("Failed to create cargo command for mdtablefix")
        .arg("--headings")
        .write_stdin(input)
        .assert()
        .success()
        .stdout(input);
}

#[test]
fn test_cli_fences_option_tilde() {
    Command::cargo_bin("mdtablefix")
        .expect("Failed to create cargo command for mdtablefix")
        .arg("--fences")
        .write_stdin("~~~~rust\nfn main() {}\n~~~~\n")
        .assert()
        .success()
        .stdout("```rust\nfn main() {}\n```\n");
}

/// Ensures fence normalization runs before other processing.
#[test]
fn test_cli_fences_before_ellipsis() {
    Command::cargo_bin("mdtablefix")
        .expect("Failed to create cargo command for mdtablefix")
        .args(["--fences", "--ellipsis"])
        .write_stdin("````\nlet x = ...;\n````\n")
        .assert()
        .success()
        .stdout("```\nlet x = ...;\n```\n");
}

/// Ensures orphan specifiers are attached when `--fences` is used.
#[test]
fn test_cli_fences_orphan_specifier() {
    Command::cargo_bin("mdtablefix")
        .expect("Failed to create cargo command for mdtablefix")
        .arg("--fences")
        .write_stdin("Rust\n```\nfn main() {}\n```\n")
        .assert()
        .success()
        .stdout("```rust\nfn main() {}\n```\n");
}

/// Combines fence normalization with renumbering to verify processing order.
#[test]
fn test_cli_fences_with_renumber() {
    let input = concat!(
        "Rust\n",
        "\n",
        "~~~~~~\n",
        "fn main() {}\n",
        "~~~~~~\n",
        "\n",
        "1. first\n",
        "3. second\n",
    );
    Command::cargo_bin("mdtablefix")
        .expect("Failed to create cargo command for mdtablefix")
        .args(["--fences", "--renumber"])
        .write_stdin(input)
        .assert()
        .success()
        .stdout("```rust\nfn main() {}\n```\n\n1. first\n2. second\n");
}

#[test]
fn test_cli_fences_preserve_existing_language() {
    Command::cargo_bin("mdtablefix")
        .expect("Failed to create cargo command for mdtablefix")
        .arg("--fences")
        .write_stdin("ruby\n```rust\nfn main() {}\n```\n")
        .assert()
        .success()
        .stdout("ruby\n```rust\nfn main() {}\n```\n");
}

#[test]
fn test_cli_fences_orphan_specifier_symbols() {
    Command::cargo_bin("mdtablefix")
        .expect("Failed to create cargo command for mdtablefix")
        .arg("--fences")
        .write_stdin("C++\n```\nfn main() {}\n```\n")
        .assert()
        .success()
        .stdout("```c++\nfn main() {}\n```\n");
}

#[test]
fn test_cli_no_attach_without_preceding_blank_line() {
    let input = concat!("text\n", "Rust\n", "```\n", "fn main() {}\n", "```\n");
    Command::cargo_bin("mdtablefix")
        .expect("Failed to create cargo command for mdtablefix")
        .arg("--fences")
        .write_stdin(input)
        .assert()
        .success()
        .stdout("text\nRust\n```\nfn main() {}\n```\n");
}

/// Tests the CLI `--footnotes` option to convert bare footnote links.
#[test]
fn test_cli_footnotes_option() {
    let input = include_str!("data/footnotes_input.txt");
    let expected = include_str!("data/footnotes_expected.txt");
    Command::cargo_bin("mdtablefix")
        .expect("Failed to create cargo command for mdtablefix")
        .arg("--footnotes")
        .write_stdin(input)
        .assert()
        .success()
        .stdout(format!("{}\n", expected.trim_end()));
}

/// Executes an in-place rewrite with the provided flags and asserts idempotence.
fn run_in_place(flags: &[&str], input: &str, expected: &str) {
    let dir = tempdir().expect("failed to create temporary directory");
    let (directory, parent_path) = capability_directory(&dir);
    let file_name = Utf8Path::new("sample.md");
    let file_path = parent_path.join(file_name);
    directory
        .write(file_name, input)
        .expect("failed to write test file");

    Command::cargo_bin("mdtablefix")
        .expect("Failed to create cargo command for mdtablefix")
        .args(["--in-place"])
        .args(flags)
        .arg(file_path.as_std_path())
        .assert()
        .success()
        .stdout("")
        .stderr("");

    let out = directory
        .read_to_string(file_name)
        .expect("failed to read output file");
    assert_eq!(out.trim_end(), expected.trim_end());
    assert!(
        out.ends_with('\n'),
        "output file must end with a trailing newline"
    );

    // idempotence
    Command::cargo_bin("mdtablefix")
        .expect("Failed to create cargo command for mdtablefix")
        .args(["--in-place"])
        .args(flags)
        .arg(file_path.as_std_path())
        .assert()
        .success()
        .stdout("")
        .stderr("");

    let out2 = directory
        .read_to_string(file_name)
        .expect("failed to read output file");
    assert!(
        out2.ends_with('\n'),
        "output file must end with a trailing newline"
    );
    assert_eq!(out2, out);
}

/// Opens a temporary directory as the capability-scoped I/O boundary for a test.
fn capability_directory(tempdir: &TempDir) -> (Dir, Utf8PathBuf) {
    let path = Utf8PathBuf::from_path_buf(tempdir.path().to_path_buf())
        .expect("temporary directory path is UTF-8");
    let directory = Dir::open_ambient_dir(&path, ambient_authority())
        .expect("failed to open temporary directory");
    (directory, path)
}

/// Ensures `--in-place` rewrites files correctly for multiple flag combinations.
#[rstest]
#[case(&["--fences"], "Rust\n```\nfn main() {}\n```\n", "```rust\nfn main() {}\n```\n")]
#[case(&["--footnotes"], include_str!("data/footnotes_input.txt"), include_str!("data/footnotes_expected.txt"))]
#[case(&["--fences", "--footnotes"], include_str!("data/fences_footnotes_input.txt"), include_str!("data/fences_footnotes_expected.txt"))]
#[case(
    // The four-backtick opener makes the three-backtick line below it literal
    // content, so the payload stays inside the unclosed fence and the ellipsis
    // pass must not reach it.
    &["--fences", "--ellipsis"],
    "````\n```\nliteral...\n",
    "````\n```\nliteral...\n"
)]
#[case(&["--wrap", "--footnotes"], include_str!("data/footnotes_input.txt"), include_str!("data/footnotes_wrap_expected.txt"))]
#[case(&["--wrap", "--ellipsis"], include_str!("data/ellipsis_wrap_input.txt"), include_str!("data/ellipsis_wrap_expected.txt"))]
#[case(&["--headings"], "Title\n=====\n", "# Title\n")]
#[case(
    &["--footnotes", "--code-emphasis", "--headings"],
    "aa\n-----\n---\n",
    "## aa\n---\n"
)]
fn test_cli_in_place_variants(#[case] flags: &[&str], #[case] input: &str, #[case] expected: &str) {
    run_in_place(flags, input, expected);
}

#[rstest]
#[case("```null\nfn main() {}\n```\n", "```\nfn main() {}\n```\n")]
#[case("```NULL\nfn main() {}\n```\n", "```\nfn main() {}\n```\n")]
#[case("```Null\nfn main() {}\n```\n", "```\nfn main() {}\n```\n")]
#[case("```null  \nfn main() {}\n```\n", "```\nfn main() {}\n```\n")]
#[case("```NULL  \nfn main() {}\n```\n", "```\nfn main() {}\n```\n")]
#[case("```Null  \nfn main() {}\n```\n", "```\nfn main() {}\n```\n")]
#[case("~~~~null\nfn main() {}\n~~~~\n", "```\nfn main() {}\n```\n")]
#[case("~~~~NULL\nfn main() {}\n~~~~\n", "```\nfn main() {}\n```\n")]
#[case("~~~~Null\nfn main() {}\n~~~~\n", "```\nfn main() {}\n```\n")]
#[case("~~~~null  \nfn main() {}\n~~~~\n", "```\nfn main() {}\n```\n")]
#[case("~~~~NULL  \nfn main() {}\n~~~~\n", "```\nfn main() {}\n```\n")]
#[case("~~~~Null  \nfn main() {}\n~~~~\n", "```\nfn main() {}\n```\n")]
#[case("  ```null\nfn main() {}\n```\n", "  ```\nfn main() {}\n```\n")]
fn test_cli_fences_null_language(#[case] input: &str, #[case] expected: &'static str) {
    Command::cargo_bin("mdtablefix")
        .expect("Failed to create cargo command for mdtablefix")
        .arg("--fences")
        .write_stdin(input)
        .assert()
        .success()
        .stdout(expected);
}