rumdl 0.2.78

A fast Markdown linter and formatter written in Rust
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
#[cfg(test)]
mod init_command_tests {
    use assert_cmd::cargo::cargo_bin_cmd;
    use predicates::prelude::*;

    use rumdl_lib::config;
    use std::fs;
    use tempfile::tempdir;

    #[test]
    fn test_init_command_creates_config_file() {
        // Create a temporary directory for the test
        let temp_dir = tempdir().expect("Failed to create temporary directory");
        let temp_path = temp_dir.path();

        // `init` writes into the working directory, so the directory under test is the
        // subprocess's own. Changing this process's working directory would leak into
        // every test sharing the binary.
        let config_path = temp_path.join(".rumdl.toml");

        // Run the init command
        let mut cmd = cargo_bin_cmd!("rumdl");
        let assert = cmd.current_dir(temp_path).arg("init").assert();

        // Check that the command succeeded
        assert
            .success()
            .stdout(predicates::str::contains("Created default configuration file"));

        // Check that the config file was created
        assert!(config_path.exists());

        // Check that the config file contains expected content
        let config_content = fs::read_to_string(config_path).expect("Failed to read config file");
        assert!(config_content.contains("[global]"));
        assert!(config_content.contains("exclude ="));
    }

    #[test]
    fn test_create_default_config_fails_if_file_exists() {
        // Create a temporary directory for the test
        let temp_dir = tempdir().expect("Failed to create temporary directory");
        let temp_path = temp_dir.path();

        // Create a config file
        let config_path = temp_path.join(".rumdl.toml");
        fs::write(&config_path, "# Existing config").expect("Failed to create config file");

        // Verify the file exists
        assert!(
            config_path.exists(),
            "Config file was not created properly for the test"
        );

        // Try to create the config file again
        let result = config::create_default_config(config_path.to_str().unwrap());

        // Check that the function returned an error
        assert!(result.is_err());

        // Check that the error message contains the expected text
        match result {
            Err(err) => {
                let err_string = err.to_string();
                assert!(err_string.contains("Configuration file already exists"));
            }
            Ok(_) => panic!("Expected an error but got Ok"),
        }

        // Check that the config file was not modified
        let config_content = fs::read_to_string(config_path).expect("Failed to read config file");
        assert_eq!(config_content, "# Existing config");
    }

    #[test]
    fn test_init_output_is_valid_configuration() {
        // Create a temporary directory for the test
        let temp_dir = tempdir().expect("Failed to create temporary directory");
        let temp_path = temp_dir.path();
        let config_path = temp_path.join(".rumdl.toml");

        // Run the init command in the temp directory
        let mut cmd = cargo_bin_cmd!("rumdl");
        cmd.current_dir(temp_path).arg("init").assert().success();

        // Read the generated config file
        let config_content = fs::read_to_string(&config_path).expect("Failed to read config file");

        // Parse the config to verify it's valid TOML
        let toml_value: toml::Value = toml::from_str(&config_content).expect("Generated config is not valid TOML");

        // Verify it can be deserialized into our Config struct
        let _config: config::Config =
            toml::from_str(&config_content).expect("Generated config cannot be deserialized into Config struct");

        // Verify some expected structure
        assert!(
            toml_value.get("global").is_some(),
            "Config should have a [global] section"
        );
    }

    #[test]
    fn test_init_output_can_be_used_by_linter() {
        // Create a temporary directory for the test
        let temp_dir = tempdir().expect("Failed to create temporary directory");
        let temp_path = temp_dir.path();

        // Run the init command
        let mut cmd = cargo_bin_cmd!("rumdl");
        cmd.current_dir(temp_path).arg("init").assert().success();

        // Create a simple test markdown file
        let test_md = temp_path.join("test.md");
        fs::write(&test_md, "# Hello\n\nThis is a test.\n").expect("Failed to write test file");

        // Run rumdl check with the generated config
        let mut cmd = cargo_bin_cmd!("rumdl");
        cmd.current_dir(temp_path)
            .arg("check")
            .arg("test.md")
            .assert()
            .success();
    }

    #[test]
    fn test_init_with_preset_google() {
        let temp_dir = tempdir().expect("Failed to create temporary directory");
        let temp_path = temp_dir.path();

        let mut cmd = cargo_bin_cmd!("rumdl");
        let assert = cmd.current_dir(temp_path).args(["init", "--preset", "google"]).assert();

        assert
            .success()
            .stdout(predicates::str::contains("Created google configuration file"));

        let config_path = temp_path.join(".rumdl.toml");
        assert!(config_path.exists());

        let content = fs::read_to_string(config_path).expect("Failed to read config");
        assert!(content.contains("Google"));
        assert!(content.contains("[MD003]"));
    }

    #[test]
    fn test_init_with_output_path() {
        let temp_dir = tempdir().expect("Failed to create temporary directory");
        let temp_path = temp_dir.path();
        let custom_path = temp_path.join("custom.toml");

        let mut cmd = cargo_bin_cmd!("rumdl");
        let assert = cmd
            .current_dir(temp_path)
            .args(["init", "--output", custom_path.to_str().unwrap()])
            .assert();

        assert.success();
        assert!(custom_path.exists());
    }

    #[test]
    fn test_init_with_preset_and_output() {
        let temp_dir = tempdir().expect("Failed to create temporary directory");
        let temp_path = temp_dir.path();
        let custom_path = temp_path.join("google.toml");

        let mut cmd = cargo_bin_cmd!("rumdl");
        let assert = cmd
            .current_dir(temp_path)
            .args(["init", "--preset", "google", "--output", custom_path.to_str().unwrap()])
            .assert();

        assert.success();
        assert!(custom_path.exists());

        let content = fs::read_to_string(custom_path).expect("Failed to read config");
        assert!(content.contains("[MD003]"));
        assert!(content.contains("style = \"atx\""));
    }

    #[test]
    fn test_init_preset_default_matches_init() {
        let temp_dir = tempdir().expect("Failed to create temporary directory");
        let temp_path = temp_dir.path();

        // Create with --preset default
        let preset_path = temp_path.join("preset.toml");
        let mut cmd = cargo_bin_cmd!("rumdl");
        cmd.current_dir(temp_path)
            .args(["init", "--preset", "default", "--output", preset_path.to_str().unwrap()])
            .assert()
            .success();

        // Create with plain init
        let default_path = temp_path.join("default.toml");
        let mut cmd = cargo_bin_cmd!("rumdl");
        cmd.current_dir(temp_path)
            .args(["init", "--output", default_path.to_str().unwrap()])
            .assert()
            .success();

        let preset_content = fs::read_to_string(preset_path).unwrap();
        let default_content = fs::read_to_string(default_path).unwrap();
        assert_eq!(preset_content, default_content);
    }

    #[test]
    fn test_init_pyproject_with_preset_google() {
        let temp_dir = tempdir().expect("Failed to create temporary directory");
        let temp_path = temp_dir.path();

        let mut cmd = cargo_bin_cmd!("rumdl");
        cmd.current_dir(temp_path)
            .args(["init", "--pyproject", "--preset", "google"])
            .assert()
            .success();

        let config_path = temp_path.join("pyproject.toml");
        assert!(config_path.exists());

        let content = fs::read_to_string(config_path).expect("Failed to read config");
        assert!(content.contains("[tool.rumdl]"));
        assert!(content.contains("[tool.rumdl.MD003]"));
        assert!(content.contains("style = \"atx\""));
        assert!(content.contains("[tool.rumdl.MD013]"));
        assert!(content.contains("line-length = 80"));
    }

    #[test]
    fn test_init_google_preset_can_be_used_by_linter() {
        let temp_dir = tempdir().expect("Failed to create temporary directory");
        let temp_path = temp_dir.path();

        // Create Google preset config
        let mut cmd = cargo_bin_cmd!("rumdl");
        cmd.current_dir(temp_path)
            .args(["init", "--preset", "google"])
            .assert()
            .success();

        // Create a simple test markdown file that complies with Google style
        let test_md = temp_path.join("test.md");
        fs::write(&test_md, "# Hello\n\nThis is a test.\n").expect("Failed to write test file");

        // Run rumdl check with the generated config
        let mut cmd = cargo_bin_cmd!("rumdl");
        cmd.current_dir(temp_path)
            .arg("check")
            .arg("test.md")
            .assert()
            .success();
    }

    #[test]
    fn test_init_with_preset_relaxed() {
        let temp_dir = tempdir().expect("Failed to create temporary directory");
        let temp_path = temp_dir.path();

        let mut cmd = cargo_bin_cmd!("rumdl");
        let assert = cmd
            .current_dir(temp_path)
            .args(["init", "--preset", "relaxed"])
            .assert();

        assert
            .success()
            .stdout(predicates::str::contains("Created relaxed configuration file"));

        let config_path = temp_path.join(".rumdl.toml");
        assert!(config_path.exists());

        let content = fs::read_to_string(config_path).expect("Failed to read config");
        assert!(content.contains("Relaxed"));
        assert!(content.contains("disable ="));
    }

    #[test]
    fn test_init_relaxed_preset_can_be_used_by_linter() {
        let temp_dir = tempdir().expect("Failed to create temporary directory");
        let temp_path = temp_dir.path();

        // Create relaxed preset config
        let mut cmd = cargo_bin_cmd!("rumdl");
        cmd.current_dir(temp_path)
            .args(["init", "--preset", "relaxed"])
            .assert()
            .success();

        // Create a test markdown file - relaxed should not complain about missing first heading
        // or inline HTML since those rules are disabled
        let test_md = temp_path.join("test.md");
        fs::write(&test_md, "Some text without a heading.\n\n<div>HTML content</div>\n")
            .expect("Failed to write test file");

        // Run rumdl check with the generated config - should pass since MD041 and MD033 are disabled
        let mut cmd = cargo_bin_cmd!("rumdl");
        cmd.current_dir(temp_path)
            .arg("check")
            .arg("test.md")
            .assert()
            .success();
    }

    #[test]
    fn test_init_pyproject_with_preset_relaxed() {
        let temp_dir = tempdir().expect("Failed to create temporary directory");
        let temp_path = temp_dir.path();

        let mut cmd = cargo_bin_cmd!("rumdl");
        cmd.current_dir(temp_path)
            .args(["init", "--pyproject", "--preset", "relaxed"])
            .assert()
            .success();

        let config_path = temp_path.join("pyproject.toml");
        assert!(config_path.exists());

        let content = fs::read_to_string(config_path).expect("Failed to read config");
        assert!(content.contains("[tool.rumdl]"));
        assert!(content.contains("MD013"));
        assert!(content.contains("MD033"));
        assert!(content.contains("style = \"consistent\""));
    }

    #[test]
    fn test_init_pyproject_conflicts_with_output() {
        let temp_dir = tempdir().expect("Failed to create temporary directory");
        let temp_path = temp_dir.path();

        let mut cmd = cargo_bin_cmd!("rumdl");
        cmd.current_dir(temp_path)
            .args(["init", "--pyproject", "--output", "custom.toml"])
            .assert()
            .failure()
            .stderr(predicates::str::contains("cannot be used with"));
    }

    /// A stand-in `code` on PATH that records every invocation, so the tests
    /// observe what `init` would do to a real editor without touching one.
    #[cfg(unix)]
    fn fake_editor(dir: &std::path::Path) -> (std::path::PathBuf, std::path::PathBuf) {
        use std::os::unix::fs::PermissionsExt;
        let bin = dir.join("bin");
        fs::create_dir_all(&bin).unwrap();
        let log = dir.join("code.log");
        let script = bin.join("code");
        fs::write(
            &script,
            format!(
                "#!/bin/sh\necho \"$*\" >> '{}'\n[ \"$1\" = \"--list-extensions\" ] && exit 0\necho 1.99.0\n",
                log.display()
            ),
        )
        .unwrap();
        fs::set_permissions(&script, fs::Permissions::from_mode(0o755)).unwrap();
        let path = format!("{}:/usr/bin:/bin", bin.display());
        (std::path::PathBuf::from(path), log)
    }

    #[cfg(unix)]
    #[test]
    fn init_without_a_terminal_never_offers_or_installs_the_editor_extension() {
        let temp_dir = tempdir().unwrap();
        let project = temp_dir.path().join("project");
        fs::create_dir(&project).unwrap();
        let (path, log) = fake_editor(temp_dir.path());

        // stdin is closed, as in CI or an agent's shell: end of input must not
        // read as the default "yes" of the VS Code prompt.
        cargo_bin_cmd!("rumdl")
            .current_dir(&project)
            .env("PATH", &path)
            .env("TERM_PROGRAM", "vscode")
            .arg("init")
            .assert()
            .success()
            .stdout(predicates::str::contains("Created default configuration file"))
            .stdout(predicates::str::contains("Would you like").not());

        let calls = fs::read_to_string(&log).unwrap_or_default();
        assert!(
            !calls.contains("--install-extension"),
            "init installed the extension without consent: {calls}"
        );
        assert!(project.join(".rumdl.toml").exists());
    }

    #[test]
    fn init_leaves_an_existing_pyproject_alone_without_a_terminal_or_yes() {
        let temp_dir = tempdir().unwrap();
        let pyproject = temp_dir.path().join("pyproject.toml");
        let original = "[project]\nname = \"demo\"\n";
        fs::write(&pyproject, original).unwrap();

        cargo_bin_cmd!("rumdl")
            .current_dir(temp_dir.path())
            .args(["init", "--pyproject"])
            .assert()
            .code(2)
            .stderr(predicates::str::contains("--yes"));

        assert_eq!(fs::read_to_string(&pyproject).unwrap(), original);
    }

    #[test]
    fn init_yes_appends_to_an_existing_pyproject() {
        let temp_dir = tempdir().unwrap();
        let pyproject = temp_dir.path().join("pyproject.toml");
        fs::write(&pyproject, "[project]\nname = \"demo\"\n").unwrap();

        cargo_bin_cmd!("rumdl")
            .current_dir(temp_dir.path())
            .args(["init", "--pyproject", "--yes"])
            .assert()
            .success()
            .stdout(predicates::str::contains("Added rumdl configuration to pyproject.toml"));

        let content = fs::read_to_string(&pyproject).unwrap();
        assert!(content.starts_with("[project]\nname = \"demo\"\n"), "{content}");
        assert!(content.contains("[tool.rumdl]"), "{content}");
    }
}