cargo-mend 0.7.0

Opinionated visibility auditing for Rust crates and workspaces
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
#![allow(
    clippy::expect_used,
    reason = "tests should panic on unexpected values"
)]
#![allow(
    unused_imports,
    reason = "shared test support re-exports more helpers than this file uses"
)]
#![allow(
    dead_code,
    reason = "shared test support defines helpers reused by other integration tests"
)]
#![allow(
    clippy::unwrap_used,
    reason = "tests should panic on unexpected values"
)]
#![allow(clippy::panic, reason = "tests should panic on unexpected values")]

mod common;

use std::path::Path;
use std::process::Output;

use common::*;

fn run_mend_json_in(dir: &Path, args: &[&str]) -> Report {
    let output = mend_command()
        .current_dir(dir)
        .args(args)
        .arg("--json")
        .output()
        .expect("run cargo-mend --json");
    assert!(
        matches!(output.status.code(), Some(0..=2)),
        "cargo-mend returned unexpected status {:?}: {}",
        output.status.code(),
        String::from_utf8_lossy(&output.stderr)
    );
    parse_mend_json_output(&output.stdout)
}

fn run_mend_in(dir: &Path, args: &[&str]) -> Output {
    mend_command()
        .current_dir(dir)
        .args(args)
        .output()
        .expect("run cargo-mend")
}

fn create_simple_lib_fixture(name: &str) -> tempfile::TempDir {
    let temp = tempdir().expect("create temp fixture dir");
    fs::write(
        temp.path().join("Cargo.toml"),
        format!("[package]\nname = \"{name}\"\nversion = \"0.1.0\"\nedition = \"2024\"\n"),
    )
    .expect("write manifest");
    fs::create_dir_all(temp.path().join("src")).expect("create src");
    fs::write(temp.path().join("src/lib.rs"), "mod helpers;\n").expect("write lib");
    fs::write(
        temp.path().join("src/helpers.rs"),
        "pub fn internal_fn() {}\n",
    )
    .expect("write helpers");
    temp
}

fn create_workspace_with_example_fixture() -> tempfile::TempDir {
    let temp = tempdir().expect("create temp workspace dir");
    fs::create_dir_all(temp.path().join("member/src")).expect("create member src");
    fs::create_dir_all(temp.path().join("member/examples/demo"))
        .expect("create member example dir");

    fs::write(
        temp.path().join("Cargo.toml"),
        "[workspace]\nmembers = [\"member\"]\nresolver = \"3\"\n",
    )
    .expect("write workspace manifest");
    fs::write(
        temp.path().join("member/Cargo.toml"),
        "[package]\nname = \"workspace_member\"\nversion = \"0.1.0\"\nedition = \"2024\"\n",
    )
    .expect("write member manifest");
    fs::write(temp.path().join("member/src/lib.rs"), "mod helpers;\n").expect("write member lib");
    fs::write(
        temp.path().join("member/src/helpers.rs"),
        "pub fn internal_fn() {}\n",
    )
    .expect("write member helpers");
    fs::write(
        temp.path().join("member/examples/demo/main.rs"),
        "mod helper;\nfn main() {}\n",
    )
    .expect("write example root");
    fs::write(
        temp.path().join("member/examples/demo/helper.rs"),
        "pub fn example_only() {}\n",
    )
    .expect("write example helper");

    temp
}

fn create_simple_workspace_fixture() -> tempfile::TempDir {
    let temp = tempdir().expect("create temp workspace dir");
    fs::create_dir_all(temp.path().join("member/src")).expect("create member src");

    fs::write(
        temp.path().join("Cargo.toml"),
        "[workspace]\nmembers = [\"member\"]\nresolver = \"3\"\n",
    )
    .expect("write workspace manifest");
    fs::write(
        temp.path().join("member/Cargo.toml"),
        "[package]\nname = \"workspace_member\"\nversion = \"0.1.0\"\nedition = \"2024\"\n",
    )
    .expect("write member manifest");
    fs::write(temp.path().join("member/src/lib.rs"), "mod helpers;\n").expect("write member lib");
    fs::write(
        temp.path().join("member/src/helpers.rs"),
        "pub fn internal_fn() {}\n",
    )
    .expect("write member helpers");

    temp
}

fn create_lib_and_example_fixture(name: &str) -> tempfile::TempDir {
    let temp = tempdir().expect("create temp fixture dir");
    fs::write(
        temp.path().join("Cargo.toml"),
        format!("[package]\nname = \"{name}\"\nversion = \"0.1.0\"\nedition = \"2024\"\n"),
    )
    .expect("write manifest");
    fs::create_dir_all(temp.path().join("src")).expect("create src");
    fs::create_dir_all(temp.path().join("examples/demo")).expect("create example dir");
    fs::write(temp.path().join("src/lib.rs"), "mod helpers;\n").expect("write lib");
    fs::write(
        temp.path().join("src/helpers.rs"),
        "pub fn library_only() {}\n",
    )
    .expect("write lib helpers");
    fs::write(
        temp.path().join("examples/demo/main.rs"),
        "mod helper;\nfn main() {}\n",
    )
    .expect("write example root");
    fs::write(
        temp.path().join("examples/demo/helper.rs"),
        "pub fn example_only() {}\n",
    )
    .expect("write example helper");
    temp
}

fn create_clean_lib_with_example_fixture(name: &str) -> tempfile::TempDir {
    let temp = tempdir().expect("create temp fixture dir");
    fs::write(
        temp.path().join("Cargo.toml"),
        format!("[package]\nname = \"{name}\"\nversion = \"0.1.0\"\nedition = \"2024\"\n"),
    )
    .expect("write manifest");
    fs::create_dir_all(temp.path().join("src")).expect("create src");
    fs::create_dir_all(temp.path().join("examples/demo")).expect("create example dir");
    fs::write(temp.path().join("src/lib.rs"), "pub fn exported() {}\n").expect("write clean lib");
    fs::write(
        temp.path().join("examples/demo/main.rs"),
        "mod helper;\nfn main() {}\n",
    )
    .expect("write example root");
    fs::write(
        temp.path().join("examples/demo/helper.rs"),
        "pub fn example_only() {}\n",
    )
    .expect("write example helper");
    temp
}

#[test]
fn version_flag_prints_version_and_exits_successfully() {
    let output = mend_command()
        .arg("--version")
        .output()
        .expect("run cargo-mend --version");

    assert!(output.status.success());
    assert!(String::from_utf8_lossy(&output.stderr).is_empty());

    let stdout = String::from_utf8(output.stdout).expect("decode stdout");
    assert_eq!(stdout, format!("mend {}\n", env!("CARGO_PKG_VERSION")));
}

#[test]
fn build_info_flag_prints_build_metadata_and_exits_successfully() {
    let output = mend_command()
        .arg("--build-info")
        .output()
        .expect("run cargo-mend --build-info");

    assert!(output.status.success());
    assert!(String::from_utf8_lossy(&output.stderr).is_empty());

    let stdout = String::from_utf8(output.stdout).expect("decode stdout");
    assert!(stdout.starts_with(&format!("cargo-mend {}\n", env!("CARGO_PKG_VERSION"))));
    assert!(stdout.contains("\ngit_hash: "));
    assert!(stdout.contains("\nbuild_id: "));
    assert!(stdout.contains("\nbuild_sysroot: "));
}

#[test]
fn default_invocation_from_package_root_reports_findings() {
    let temp = create_simple_lib_fixture("cli_default_fixture");

    let report = run_mend_json_in(temp.path(), &[]);
    let narrow_findings: Vec<_> = report
        .findings
        .iter()
        .filter(|finding| finding.code == DiagnosticCode::NarrowToPubCrate)
        .collect();

    assert_eq!(narrow_findings.len(), 1);
    assert_eq!(narrow_findings[0].path, "src/helpers.rs");
    assert_summary_matches_findings(&report);
}

#[test]
fn positional_manifest_path_reports_findings() {
    let temp = create_simple_lib_fixture("cli_positional_manifest_fixture");

    let report = run_mend_json_in(temp.path(), &[temp.path().to_str().expect("utf-8 path")]);
    let narrow_findings: Vec<_> = report
        .findings
        .iter()
        .filter(|finding| finding.code == DiagnosticCode::NarrowToPubCrate)
        .collect();

    assert_eq!(narrow_findings.len(), 1);
    assert_eq!(narrow_findings[0].path, "src/helpers.rs");
    assert_summary_matches_findings(&report);
}

#[test]
fn json_success_emits_clean_stdout_and_no_stderr_noise() {
    let temp = tempdir().expect("create temp fixture dir");
    fs::write(
        temp.path().join("Cargo.toml"),
        "[package]\nname = \"cli_json_clean_fixture\"\nversion = \"0.1.0\"\nedition = \"2024\"\n",
    )
    .expect("write manifest");
    fs::create_dir_all(temp.path().join("src")).expect("create src");
    fs::write(
        temp.path().join("src/lib.rs"),
        "mod helpers;\n\npub fn exported() { helpers::internal_fn(); }\n",
    )
    .expect("write lib");
    fs::write(
        temp.path().join("src/helpers.rs"),
        "pub fn internal_fn() {}\n",
    )
    .expect("write helpers");

    let output = mend_command()
        .current_dir(temp.path())
        .arg("--json")
        .output()
        .expect("run cargo-mend --json");
    assert!(
        matches!(output.status.code(), Some(0..=2)),
        "cargo-mend returned unexpected status {:?}: {}",
        output.status.code(),
        String::from_utf8_lossy(&output.stderr)
    );

    let stdout = String::from_utf8(output.stdout).expect("decode stdout");
    let json_lines = stdout.lines().collect::<Vec<_>>();
    let first_message: serde_json::Value =
        serde_json::from_str(json_lines.first().expect("first JSON line")).expect("parse first");
    let last_message: serde_json::Value =
        serde_json::from_str(json_lines.last().expect("last JSON line")).expect("parse last");
    let report = parse_mend_json_output(stdout.as_bytes());
    let stderr = strip_ansi(&String::from_utf8(output.stderr).expect("decode stderr"));

    assert_eq!(
        first_message
            .get("reason")
            .and_then(serde_json::Value::as_str),
        Some("compiler-message")
    );
    assert!(
        first_message
            .get("package_id")
            .and_then(serde_json::Value::as_str)
            .is_some_and(|package_id| !package_id.is_empty())
    );
    assert!(
        first_message
            .get("manifest_path")
            .and_then(serde_json::Value::as_str)
            .is_some_and(|manifest_path| manifest_path.ends_with("Cargo.toml"))
    );
    assert_eq!(
        first_message.pointer("/message/$message_type"),
        Some(&serde_json::Value::String("diagnostic".to_string()))
    );
    assert_eq!(
        first_message
            .pointer("/message/spans/0/text/0/text")
            .and_then(serde_json::Value::as_str),
        Some("pub fn internal_fn() {}")
    );
    assert_eq!(
        last_message
            .get("reason")
            .and_then(serde_json::Value::as_str),
        Some("build-finished")
    );
    assert_eq!(
        last_message
            .get("success")
            .and_then(serde_json::Value::as_bool),
        Some(true)
    );
    assert_eq!(
        stderr, "",
        "expected successful `--json` run to keep stderr quiet"
    );
    assert_summary_matches_findings(&report);
}

#[test]
fn workspace_flag_from_workspace_root_reports_member_findings() {
    let temp = create_simple_workspace_fixture();

    let report = run_mend_json_in(temp.path(), &["--workspace"]);
    let narrow_findings: Vec<_> = report
        .findings
        .iter()
        .filter(|finding| finding.code == DiagnosticCode::NarrowToPubCrate)
        .collect();

    assert_eq!(narrow_findings.len(), 1);
    assert_eq!(narrow_findings[0].path, "member/src/helpers.rs");
    assert_summary_matches_findings(&report);
}

#[test]
fn workspace_all_targets_includes_example_target_findings() {
    let temp = create_workspace_with_example_fixture();

    let report = run_mend_json_in(temp.path(), &["--workspace", "--all-targets"]);
    let narrow_paths = report
        .findings
        .iter()
        .filter(|finding| finding.code == DiagnosticCode::NarrowToPubCrate)
        .map(|finding| finding.path.as_str())
        .collect::<BTreeSet<_>>();

    assert_eq!(
        narrow_paths,
        BTreeSet::from(["member/examples/demo/helper.rs", "member/src/helpers.rs"])
    );
    assert_summary_matches_findings(&report);
}

#[test]
fn lib_flag_limits_analysis_to_library_target() {
    let temp = create_lib_and_example_fixture("cli_lib_fixture");

    let report = run_mend_json_in(temp.path(), &["--lib"]);
    let narrow_paths = report
        .findings
        .iter()
        .filter(|finding| finding.code == DiagnosticCode::NarrowToPubCrate)
        .map(|finding| finding.path.as_str())
        .collect::<BTreeSet<_>>();

    assert_eq!(narrow_paths, BTreeSet::from(["src/helpers.rs"]));
    assert_summary_matches_findings(&report);
}

#[test]
fn named_example_limits_analysis_to_example_target() {
    let temp = create_clean_lib_with_example_fixture("cli_named_example_fixture");

    let report = run_mend_json_in(temp.path(), &["--example", "demo"]);
    let narrow_paths = report
        .findings
        .iter()
        .filter(|finding| finding.code == DiagnosticCode::NarrowToPubCrate)
        .map(|finding| finding.path.as_str())
        .collect::<BTreeSet<_>>();

    assert_eq!(narrow_paths, BTreeSet::from(["examples/demo/helper.rs"]));
    assert_summary_matches_findings(&report);
}

#[test]
fn fix_dry_run_smoke_reports_and_preserves_files() {
    let temp = create_simple_lib_fixture("cli_fix_dry_run_fixture");

    let output = run_mend_in(temp.path(), &["--fix", "--dry-run"]);
    assert!(
        output.status.success(),
        "cargo-mend --fix --dry-run failed: {}\n{}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );

    let stderr = String::from_utf8(output.stderr).expect("decode stderr");
    assert!(stderr.contains("would apply 1"));

    let helpers = fs::read_to_string(temp.path().join("src/helpers.rs")).expect("read helpers");
    assert!(helpers.contains("pub fn internal_fn()"));
}