fallow-cli 2.73.0

CLI for fallow, Rust-native codebase intelligence for TypeScript and JavaScript
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
#[path = "common/mod.rs"]
mod common;

use common::{fixture_path, parse_json, redact_all, run_fallow, run_fallow_in_root};
use tempfile::tempdir;

fn init_git_index(root: &std::path::Path) {
    let status = std::process::Command::new("git")
        .args(["init", "-q"])
        .current_dir(root)
        .status()
        .expect("git init should run");
    assert!(status.success(), "git init should succeed");
    let status = std::process::Command::new("git")
        .args(["add", "."])
        .current_dir(root)
        .status()
        .expect("git add should run");
    assert!(status.success(), "git add should succeed");
}

// ---------------------------------------------------------------------------
// JSON output structure
// ---------------------------------------------------------------------------

/// `fallow dupes --performance` was previously a no-op: the global flag was
/// parsed but never wired through to `DupesOptions`, so users got nothing.
/// This pins the behaviour: human format renders a stderr "Duplication
/// Performance" panel; structured formats (JSON / SARIF / CodeClimate) stay
/// silent so the machine envelope is uncorrupted.
#[test]
fn dupes_performance_panel_renders_for_human_format() {
    let output = run_fallow("dupes", "duplicate-code", &["--performance"]);
    assert!(
        output.stderr.contains("Duplication Performance"),
        "human dupes --performance should print panel header. stderr: {}",
        output.stderr
    );
    assert!(
        output.stderr.contains("clone groups:"),
        "panel should include clone group count. stderr: {}",
        output.stderr
    );
}

#[test]
fn dupes_performance_panel_suppressed_for_json_format() {
    let output = run_fallow(
        "dupes",
        "duplicate-code",
        &["--performance", "--format", "json", "--quiet"],
    );
    assert!(
        !output.stderr.contains("Duplication Performance"),
        "json dupes --performance must not corrupt machine output with the panel. stderr: {}",
        output.stderr
    );
}

#[test]
fn dupes_json_output_has_clone_groups() {
    let output = run_fallow("dupes", "duplicate-code", &["--format", "json", "--quiet"]);
    let json = parse_json(&output);
    assert!(
        json.get("clone_groups").is_some(),
        "dupes JSON should have clone_groups key"
    );
    let groups = json["clone_groups"].as_array().unwrap();
    assert!(
        !groups.is_empty(),
        "duplicate-code fixture should have clone groups"
    );
}

#[test]
fn dupes_json_has_stats() {
    let output = run_fallow("dupes", "duplicate-code", &["--format", "json", "--quiet"]);
    let json = parse_json(&output);
    assert!(
        json.get("stats").is_some(),
        "dupes JSON should have stats key"
    );
}

// ---------------------------------------------------------------------------
// Mode flags
// ---------------------------------------------------------------------------

#[test]
fn dupes_strict_mode_accepted() {
    let output = run_fallow(
        "dupes",
        "duplicate-code",
        &["--mode", "strict", "--format", "json", "--quiet"],
    );
    assert!(
        output.code == 0 || output.code == 1,
        "dupes --mode strict should not crash, got exit code {}",
        output.code
    );
}

#[test]
fn dupes_mild_mode_accepted() {
    let output = run_fallow(
        "dupes",
        "duplicate-code",
        &["--mode", "mild", "--format", "json", "--quiet"],
    );
    assert!(
        output.code == 0 || output.code == 1,
        "dupes --mode mild should not crash"
    );
}

// ---------------------------------------------------------------------------
// Filtering
// ---------------------------------------------------------------------------

#[test]
fn dupes_min_tokens_filter() {
    let output = run_fallow(
        "dupes",
        "duplicate-code",
        &["--min-tokens", "1000", "--format", "json", "--quiet"],
    );
    let json = parse_json(&output);
    let groups = json["clone_groups"].as_array().unwrap();
    assert!(
        groups.is_empty(),
        "high min-tokens should filter out all clones"
    );
}

#[test]
fn dupes_top_flag() {
    let output = run_fallow(
        "dupes",
        "duplicate-code",
        &["--top", "1", "--format", "json", "--quiet"],
    );
    let json = parse_json(&output);
    let groups = json["clone_groups"].as_array().unwrap();
    assert!(
        groups.len() <= 1,
        "--top 1 should return at most 1 clone group"
    );
}

#[test]
fn dupes_filters_atomic_function_call_clones() {
    let dir = tempdir().unwrap();
    std::fs::create_dir_all(dir.path().join("src/a")).unwrap();
    std::fs::create_dir_all(dir.path().join("src/b")).unwrap();
    std::fs::write(
        dir.path().join("package.json"),
        r#"{"name":"dupes-call-filter","type":"module","main":"src/a/call.ts"}"#,
    )
    .unwrap();
    let call = r#"export function alpha() {
  return createComplexWidget(
    currentProject.id,
    currentUser.id,
    activeWorkspace.slug,
    selectedEnvironment.name,
    featureFlags.enableAuditTrail,
    permissions.canPublish,
    billingAccount.plan,
    retryPolicy.maxAttempts,
    retryPolicy.backoffMs,
    notifier.email,
    logger.child({ scope: "workflow" }),
    {
      source: "settings",
      reason: "manual-run",
      requestedBy: currentUser.email,
      correlationId: request.id,
      priority: selectedWorkflow.priority,
      tags: selectedWorkflow.tags,
      metadata: selectedWorkflow.metadata,
      createdAt: clock.now(),
    },
  );
}
"#;
    std::fs::write(dir.path().join("src/a/call.ts"), call).unwrap();
    std::fs::write(
        dir.path().join("src/b/call.ts"),
        call.replace("alpha", "beta"),
    )
    .unwrap();
    init_git_index(dir.path());

    let output = run_fallow_in_root(
        "dupes",
        dir.path(),
        &["--format", "json", "--quiet", "--no-cache"],
    );
    let json = parse_json(&output);
    let groups = json["clone_groups"].as_array().unwrap();
    assert!(
        groups.is_empty(),
        "atomic call clones should be filtered. stdout: {} stderr: {}",
        output.stdout,
        output.stderr
    );
    assert_eq!(json["stats"]["clone_groups"], serde_json::json!(0));
}

#[test]
fn dupes_still_reports_repeated_control_flow() {
    let dir = tempdir().unwrap();
    std::fs::create_dir_all(dir.path().join("src/a")).unwrap();
    std::fs::create_dir_all(dir.path().join("src/b")).unwrap();
    std::fs::write(
        dir.path().join("package.json"),
        r#"{"name":"dupes-control-flow","type":"module","main":"src/a/flow.ts"}"#,
    )
    .unwrap();
    let flow = r#"export function alpha(value) {
  const normalized = normalizeValue(value);
  const score = calculateScore(normalized);
  if (score > 90) {
    auditTrail.record("high", normalized.id);
    notifications.send("high-score", normalized.owner);
    metrics.increment("score.high");
    return buildResult(normalized, "high", score);
  }
  if (score > 50) {
    auditTrail.record("medium", normalized.id);
    notifications.send("medium-score", normalized.owner);
    metrics.increment("score.medium");
    return buildResult(normalized, "medium", score);
  }
  auditTrail.record("low", normalized.id);
  notifications.send("low-score", normalized.owner);
  metrics.increment("score.low");
  return buildResult(normalized, "low", score);
}
"#;
    std::fs::write(dir.path().join("src/a/flow.ts"), flow).unwrap();
    std::fs::write(
        dir.path().join("src/b/flow.ts"),
        flow.replace("alpha", "beta"),
    )
    .unwrap();
    init_git_index(dir.path());

    let output = run_fallow_in_root(
        "dupes",
        dir.path(),
        &["--format", "json", "--quiet", "--no-cache"],
    );
    let json = parse_json(&output);
    let groups = json["clone_groups"].as_array().unwrap();
    assert!(
        !groups.is_empty(),
        "non-atomic repeated control flow should still be reported. stdout: {} stderr: {}",
        output.stdout,
        output.stderr
    );
}

#[test]
fn dupes_still_reports_repeated_callback_bodies_inside_calls() {
    let dir = tempdir().unwrap();
    std::fs::create_dir_all(dir.path().join("src/a")).unwrap();
    std::fs::create_dir_all(dir.path().join("src/b")).unwrap();
    std::fs::write(
        dir.path().join("package.json"),
        r#"{"name":"dupes-callback-body","type":"module","main":"src/a/routes.ts"}"#,
    )
    .unwrap();
    let route = r#"router.get("/alpha", async (ctx) => {
  const normalized = normalizeValue(ctx.input);
  const score = calculateScore(normalized);
  if (score > 90) {
    auditTrail.record("high", normalized.id);
    notifications.send("high-score", normalized.owner);
    metrics.increment("score.high");
    return buildResult(normalized, "high", score);
  }
  if (score > 50) {
    auditTrail.record("medium", normalized.id);
    notifications.send("medium-score", normalized.owner);
    metrics.increment("score.medium");
    return buildResult(normalized, "medium", score);
  }
  auditTrail.record("low", normalized.id);
  notifications.send("low-score", normalized.owner);
  metrics.increment("score.low");
  return buildResult(normalized, "low", score);
});
"#;
    std::fs::write(dir.path().join("src/a/routes.ts"), route).unwrap();
    std::fs::write(
        dir.path().join("src/b/routes.ts"),
        route.replace("/alpha", "/beta"),
    )
    .unwrap();
    init_git_index(dir.path());

    let output = run_fallow_in_root(
        "dupes",
        dir.path(),
        &["--format", "json", "--quiet", "--no-cache"],
    );
    let json = parse_json(&output);
    let groups = json["clone_groups"].as_array().unwrap();
    assert!(
        !groups.is_empty(),
        "callback bodies inside calls should still be reported. stdout: {} stderr: {}",
        output.stdout,
        output.stderr
    );
}

#[test]
fn dupes_group_by_package_validates_non_monorepo() {
    let dir = tempdir().unwrap();
    std::fs::write(
        dir.path().join("package.json"),
        r#"{"name":"single","version":"1.0.0","main":"src/index.ts"}"#,
    )
    .unwrap();
    std::fs::create_dir_all(dir.path().join("src")).unwrap();
    std::fs::write(dir.path().join("src/index.ts"), "export const value = 1;\n").unwrap();

    let output = run_fallow_in_root(
        "dupes",
        dir.path(),
        &["--group-by", "package", "--format", "json", "--quiet"],
    );

    assert_eq!(output.code, 2, "dupes should reject package grouping");
    let parsed: serde_json::Value =
        serde_json::from_str(&output.stdout).expect("stdout should be a single JSON error object");
    assert_eq!(parsed["error"], serde_json::json!(true));
    let msg = parsed["message"]
        .as_str()
        .expect("error message should be a string");
    assert!(
        msg.contains("monorepo"),
        "error message should mention 'monorepo': {msg}"
    );
}

#[test]
fn dupes_save_baseline_creates_parent_directory() {
    let dir = tempdir().unwrap();
    std::fs::write(
        dir.path().join("package.json"),
        r#"{"name":"dupes-save","version":"1.0.0"}"#,
    )
    .unwrap();
    std::fs::create_dir_all(dir.path().join("src")).unwrap();
    let clone = "export function shared(value) {\n  if (value > 1) {\n    return value * 2;\n  }\n  return value + 1;\n}\n";
    std::fs::write(dir.path().join("src/one.ts"), clone).unwrap();
    std::fs::write(dir.path().join("src/two.ts"), clone).unwrap();

    let baseline_path = dir.path().join("fallow-baselines/dupes.json");
    let output = run_fallow_in_root(
        "dupes",
        dir.path(),
        &[
            "--save-baseline",
            baseline_path.to_str().unwrap(),
            "--format",
            "json",
            "--quiet",
        ],
    );
    let rendered = redact_all(&format!("{}\n{}", output.stdout, output.stderr), dir.path());
    assert!(
        output.code == 0 || output.code == 1,
        "dupes save baseline should not crash: {rendered}"
    );
    assert!(
        baseline_path.exists(),
        "dupes save baseline should create nested file: {rendered}"
    );
}

// ---------------------------------------------------------------------------
// Path relativization (regression: #85)
// ---------------------------------------------------------------------------

#[test]
fn dupes_json_paths_are_relative() {
    let output = run_fallow("dupes", "duplicate-code", &["--format", "json", "--quiet"]);
    let json = parse_json(&output);
    let groups = json["clone_groups"].as_array().unwrap();
    assert!(!groups.is_empty(), "fixture should have clone groups");

    // All instance paths must be relative (no leading /)
    for group in groups {
        for instance in group["instances"].as_array().unwrap() {
            let path = instance["file"].as_str().unwrap();
            assert!(
                !path.starts_with('/'),
                "clone group instance path should be relative, got: {path}"
            );
        }
    }

    // Clone families should also have relative paths
    if let Some(families) = json.get("clone_families").and_then(|f| f.as_array()) {
        for family in families {
            if let Some(files) = family.get("files").and_then(|f| f.as_array()) {
                for file in files {
                    let path = file.as_str().unwrap();
                    assert!(
                        !path.starts_with('/'),
                        "clone family file path should be relative, got: {path}"
                    );
                }
            }
        }
    }
}

// ---------------------------------------------------------------------------
// Human output snapshot
// ---------------------------------------------------------------------------

#[test]
fn dupes_human_output_snapshot() {
    let output = run_fallow("dupes", "duplicate-code", &["--quiet"]);
    let root = fixture_path("duplicate-code");
    let redacted = redact_all(&output.stdout, &root);
    insta::assert_snapshot!("dupes_human_output", redacted);
}

// ---------------------------------------------------------------------------
// Plugin-scoped hidden directory traversal
// ---------------------------------------------------------------------------

/// Standalone `fallow dupes` must include React Router's `.client` / `.server`
/// folders in its file walk. The threshold is dropped to the minimum so the
/// small fixture files survive dupes' token / line filters and surface in
/// `stats.total_files`.
#[test]
fn dupes_includes_plugin_scoped_hidden_dirs_for_react_router() {
    let output = run_fallow(
        "dupes",
        "react-router-conventions",
        &[
            "--format",
            "json",
            "--quiet",
            "--min-tokens",
            "1",
            "--min-lines",
            "1",
        ],
    );
    assert_eq!(output.code, 0, "stderr was: {}", output.stderr);

    let json = parse_json(&output);
    let total_files = json["stats"]["total_files"]
        .as_u64()
        .expect("stats.total_files is a number");
    assert!(
        total_files >= 5,
        "expected stats.total_files >= 5 (root + routes + .client + .server), got {total_files}"
    );
}