jscycles 0.2.1

Find circular imports between files and workspace packages in JavaScript and TypeScript projects
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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
//! Process-level tests of scan completion, configuration, and output failures.
#![allow(
    clippy::expect_used,
    reason = "Process-test setup failures should fail the test"
)]

use std::fs;
use std::io::Write as _;
use std::path::Path;
use std::process::{Command, Output, Stdio};

use tempfile::tempdir;

fn scan(root: &Path, arguments: &[&str]) -> Output {
    Command::new(env!("CARGO_BIN_EXE_jscycles"))
        .current_dir(root)
        .args(arguments)
        .output()
        .expect("CLI should start")
}

fn package(root: &Path) {
    fs::write(root.join("package.json"), r#"{"name":"example"}"#)
        .expect("manifest should be written");
    fs::write(root.join("a.ts"), "export const a = 1;").expect("source should be written");
}

#[test]
fn argument_errors_escape_controls_and_bidi_without_destroying_usage_layout() {
    let temp = tempdir().expect("temporary directory");
    for character in [
        '\n', '\t', '\u{1b}', '\u{9b}', '\u{061c}', '\u{200e}', '\u{200f}', '\u{202a}', '\u{202b}',
        '\u{202c}', '\u{202d}', '\u{202e}', '\u{2066}', '\u{2067}', '\u{2068}', '\u{2069}',
        '\u{2028}', '\u{2029}',
    ] {
        let unknown = format!("--bad{character}flag");
        let extension = format!("ts{character}");
        for arguments in [
            vec![unknown.as_str()],
            vec!["--extensions", extension.as_str()],
        ] {
            let output = scan(temp.path(), &arguments);
            assert_eq!(output.status.code(), Some(2), "invalid arguments fail");
            assert!(output.stdout.is_empty(), "no report for parser errors");
            let diagnostic = String::from_utf8(output.stderr).expect("UTF-8 diagnostic");
            assert!(
                diagnostic.contains(&character.escape_unicode().to_string()),
                "the argument is visible as an escape: {diagnostic:?}"
            );
            if !matches!(character, '\n' | '\t') {
                assert!(!diagnostic.contains(character), "no raw terminal control");
            }
            if arguments.len() == 1 {
                assert!(
                    diagnostic.contains("\nUsage:"),
                    "preserve generated usage layout"
                );
            }
        }
    }
}

#[test]
fn valid_paths_keep_their_original_unicode_and_control_characters() {
    let temp = tempdir().expect("temporary directory");
    for name in ["ordinary-λ", "direction-\u{202e}"] {
        let root = temp.path().join(name);
        fs::create_dir(&root).expect("package directory");
        package(&root);
        fs::write(root.join("a.ts"), "import './b';").expect("source a");
        fs::write(root.join("b.ts"), "import './a';").expect("source b");
        let output = Command::new(env!("CARGO_BIN_EXE_jscycles"))
            .arg(&root)
            .args(["--inner", "--json", "--no-tsconfig"])
            .output()
            .expect("CLI starts");
        assert_eq!(output.status.code(), Some(1), "preserve the selected path");
        let report: serde_json::Value = serde_json::from_slice(&output.stdout).expect("JSON");
        assert!(
            report["file_cycles"].to_string().contains(name),
            "preserve path identity"
        );
    }
}

fn workspace(root: &Path, imports_cycle: bool, manifests_cycle: bool) {
    fs::write(
        root.join("package.json"),
        r#"{"name":"root","workspaces":["packages/*"]}"#,
    )
    .expect("workspace manifest");
    for (name, other) in [("a", "b"), ("b", "a"), ("c", "c")] {
        let path = root.join("packages").join(name);
        fs::create_dir_all(&path).expect("package directory");
        let dependency = if manifests_cycle { other } else { "c" };
        fs::write(
            path.join("package.json"),
            format!(r#"{{"name":"{name}","dependencies":{{"{dependency}":"workspace:*"}}}}"#),
        )
        .expect("package manifest");
        let source = if imports_cycle && name != "c" {
            format!("import {{ value }} from '{other}'; export const local = value;")
        } else {
            "export const value = 1;".to_owned()
        };
        fs::write(path.join("index.ts"), source).expect("package source");
    }
}

#[test]
fn adherence_outer_is_a_filter_of_the_same_import_graph() {
    for (imports_cycle, manifests_cycle) in [(true, false), (false, true)] {
        let temp = tempdir().expect("temporary directory");
        workspace(temp.path(), imports_cycle, manifests_cycle);
        for explicit in [false, true] {
            let mut arguments = vec!["--json"];
            if explicit {
                arguments.push(".");
            }
            let full = scan(temp.path(), &arguments);
            arguments.push("--outer");
            let outer = scan(temp.path(), &arguments);
            let expected = i32::from(imports_cycle);
            assert_eq!(
                full.status.code(),
                Some(expected),
                "source imports determine the full result"
            );
            assert_eq!(
                outer.status.code(),
                Some(expected),
                "filtering must preserve package-cycle status"
            );
            let full: serde_json::Value = serde_json::from_slice(&full.stdout).expect("full JSON");
            let outer: serde_json::Value =
                serde_json::from_slice(&outer.stdout).expect("outer JSON");
            assert_eq!(
                full["package_cycles"], outer["package_cycles"],
                "same package graph in both modes"
            );
        }
    }
}

#[test]
fn adherence_automatic_scans_use_configured_include_patterns() {
    let temp = tempdir().expect("temporary directory");
    workspace(temp.path(), true, false);
    fs::write(
        temp.path().join("jscycles.yaml"),
        "scan:\n  include: ['packages/a']\n",
    )
    .expect("scan selection");
    for filter in ["--inner", "--outer"] {
        let output = scan(temp.path(), &["--json", filter]);
        assert_eq!(
            output.status.code(),
            Some(0),
            "only package a is selected: {output:?}"
        );
    }
    let output = scan(temp.path(), &["--json", "--inner"]);
    let json: serde_json::Value = serde_json::from_slice(&output.stdout).expect("scan JSON");
    let packages: Vec<_> = json["file_cycles"]
        .as_array()
        .expect("package reports")
        .iter()
        .map(|package| package["name"].as_str().expect("package name"))
        .collect();
    assert_eq!(
        packages,
        vec!["a"],
        "scan.include selects automatic package roots"
    );
    assert_eq!(
        scan(temp.path(), &["--json", ".", "--outer"]).status.code(),
        Some(1),
        "explicit directory overrides automatic includes and scans members"
    );
}

#[test]
fn adherence_alias_cycles_use_consistent_source_paths() {
    let temp = tempdir().expect("temporary directory");
    package(temp.path());
    fs::create_dir(temp.path().join("src")).expect("source directory");
    fs::write(
        temp.path().join("tsconfig.json"),
        r#"{"compilerOptions":{"baseUrl":".","paths":{"@/*":["src/*"]}}}"#,
    )
    .expect("aliases");
    fs::write(
        temp.path().join("src/a.ts"),
        "import {b} from '@/b'; export const a = b;",
    )
    .expect("source a");
    fs::write(
        temp.path().join("src/b.ts"),
        "import {a} from '@/a'; export const b = a;",
    )
    .expect("source b");
    let absolute = temp.path().join("tsconfig.json");
    for config in [None, Some("tsconfig.json"), absolute.to_str()] {
        let mut arguments = vec!["--json", "--inner"];
        if let Some(path) = config {
            arguments.extend(["--tsconfig", path]);
        }
        let output = scan(temp.path(), &arguments);
        assert_eq!(
            output.status.code(),
            Some(1),
            "aliases must retain cycles for {config:?}: {output:?}"
        );
        let json: serde_json::Value = serde_json::from_slice(&output.stdout).expect("scan JSON");
        let path = json["file_cycles"][0]["cycles"][0][0]
            .as_str()
            .expect("cycle path");
        assert!(
            !Path::new(path).is_absolute(),
            "CLI cycle paths stay relative to the reported package"
        );
    }
}

fn assert_error(output: &Output) {
    assert_eq!(
        output.status.code(),
        Some(2),
        "failure must exit 2: {output:?}"
    );
    assert!(
        output.stdout.is_empty(),
        "failed scan must not emit a successful report"
    );
    assert!(!output.stderr.is_empty(), "failure should be diagnosed");
}

#[test]
fn invalid_configuration_is_not_a_clean_scan() {
    let temp = tempdir().expect("temporary directory");
    package(temp.path());
    for yaml in [
        "scan: [",
        "typo: true",
        "scan:\n  incldue: [src]",
        "defaults:\n  extensions: 5",
        "scan: {}\nscan: {}",
        "packages:\n  '[': {}",
        "packages:\n  '*':\n    ignore: ['[']",
    ] {
        fs::write(temp.path().join("jscycles.yaml"), yaml).expect("config should be written");
        assert_error(&scan(temp.path(), &["--json", "--inner"]));
    }
}

#[test]
fn explicit_extensions_override_yaml_defaults_and_package_settings() {
    let temp = tempdir().expect("temporary directory");
    package(temp.path());
    fs::write(temp.path().join("a.ts"), "import './a';").expect("cycle source");
    fs::write(
        temp.path().join("jscycles.yaml"),
        "defaults:\n  extensions: [js]\npackages:\n  example:\n    extensions: [js]\n",
    )
    .expect("configuration");
    assert_eq!(
        scan(temp.path(), &["--inner"]).status.code(),
        Some(0),
        "YAML excludes TypeScript"
    );
    assert_eq!(
        scan(temp.path(), &["--inner", "--extensions", "ts"])
            .status
            .code(),
        Some(1),
        "explicit CLI extensions must override YAML"
    );
}

#[test]
fn optional_absent_config_and_explicit_missing_config_differ() {
    let temp = tempdir().expect("temporary directory");
    package(temp.path());
    let clean = scan(temp.path(), &["--json", "--inner"]);
    assert_eq!(
        clean.status.code(),
        Some(0),
        "absent default config is optional"
    );
    let json: serde_json::Value = serde_json::from_slice(&clean.stdout).expect("valid JSON");
    assert_eq!(
        json["has_cycles"], false,
        "clean JSON must agree with exit status"
    );
    assert_error(&scan(temp.path(), &["--config", "missing.yaml", "--inner"]));
    assert_error(&scan(
        temp.path(),
        &["--tsconfig", "missing.json", "--inner"],
    ));
}

#[test]
fn explicit_tsconfig_errors_are_reported_in_outer_mode() {
    let temp = tempdir().expect("temporary directory");
    workspace(temp.path(), false, false);
    let output = scan(temp.path(), &["--outer", "--tsconfig", "missing.json"]);
    assert_error(&output);
    assert!(
        String::from_utf8_lossy(&output.stderr).contains("missing.json"),
        "diagnostic identifies the selected input"
    );
    assert_error(&scan(
        temp.path(),
        &["--no-tsconfig", "--tsconfig", "missing.json"],
    ));
}

#[test]
fn missing_scan_root_is_an_error() {
    let temp = tempdir().expect("temporary directory");
    assert_error(&scan(temp.path(), &["does-not-exist", "--inner"]));
}

#[test]
fn unsupported_file_operand_is_not_a_clean_scan() {
    let temp = tempdir().expect("temporary directory");
    package(temp.path());
    assert_error(&scan(temp.path(), &["a.ts", "--inner"]));
}

#[test]
fn cycles_keep_the_existing_exit_and_json_contract() {
    let temp = tempdir().expect("temporary directory");
    package(temp.path());
    fs::write(temp.path().join("a.ts"), "import './a';").expect("cycle source");
    let output = scan(temp.path(), &["--json", "--inner"]);
    assert_eq!(output.status.code(), Some(1), "cycles must exit 1");
    let json: serde_json::Value = serde_json::from_slice(&output.stdout).expect("valid JSON");
    assert_eq!(
        json["has_cycles"], true,
        "cycle report must agree with exit status"
    );
}

#[test]
fn invalid_utf8_stdin_is_not_silently_truncated() {
    let temp = tempdir().expect("temporary directory");
    package(temp.path());
    let mut child = Command::new(env!("CARGO_BIN_EXE_jscycles"))
        .current_dir(temp.path())
        .args(["--stdin", "--inner", "--json"])
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
        .expect("CLI should start");
    child
        .stdin
        .take()
        .expect("stdin pipe")
        .write_all(b".\n\xff\n")
        .expect("write input");
    assert_error(&child.wait_with_output().expect("CLI should finish"));
}

#[cfg(target_os = "linux")]
#[test]
fn real_output_failure_is_nonzero() {
    let temp = tempdir().expect("temporary directory");
    package(temp.path());
    let output = Command::new(env!("CARGO_BIN_EXE_jscycles"))
        .current_dir(temp.path())
        .args(["--json", "--inner"])
        .stdout(
            fs::OpenOptions::new()
                .write(true)
                .open("/dev/full")
                .expect("open /dev/full"),
        )
        .output()
        .expect("CLI should finish");
    assert_eq!(
        output.status.code(),
        Some(2),
        "write/flush failure must exit 2"
    );
}

#[cfg(unix)]
#[test]
fn closed_stdout_is_an_output_error() {
    use std::os::fd::OwnedFd;
    use std::os::unix::net::UnixStream;

    let temp = tempdir().expect("temporary directory");
    package(temp.path());
    for arguments in [vec!["--json", "--inner"], vec!["--help"], vec!["--version"]] {
        let (reader, writer) = UnixStream::pair().expect("stdout socket");
        // Force an OS write error without depending on peer-close propagation or
        // concurrent child-descriptor inheritance timing on Unix socket pairs.
        writer
            .shutdown(std::net::Shutdown::Write)
            .expect("disable stdout writes");
        drop(reader);
        let output = Command::new(env!("CARGO_BIN_EXE_jscycles"))
            .current_dir(temp.path())
            .args(arguments)
            .stdout(Stdio::from(OwnedFd::from(writer)))
            .output()
            .expect("CLI should finish");
        assert_eq!(output.status.code(), Some(2), "output failure must exit 2");
        assert!(
            String::from_utf8_lossy(&output.stderr).contains("Error"),
            "output failure retains an operational diagnostic"
        );
    }
}

/// The README's usage block is published as the website's command-line
/// reference, so it must stay identical to the real `--help` text.
#[test]
fn readme_usage_block_matches_help_output() {
    let output = Command::new(env!("CARGO_BIN_EXE_jscycles"))
        .arg("--help")
        .output()
        .expect("CLI should start");
    assert!(output.status.success(), "--help should succeed");
    let help = String::from_utf8(output.stdout).expect("help is UTF-8");
    let readme = include_str!("../README.md").replace("\r\n", "\n");
    let (_, after) = readme
        .split_once("```text\nUsage: jscycles")
        .expect("README.md has a usage block");
    let (rest, _) = after.split_once("```").expect("usage block is closed");
    let usage = format!("Usage: jscycles{rest}");
    assert!(
        help.ends_with(&usage),
        "README.md's usage block differs from `jscycles --help`; replace it with:\n{help}"
    );
}

/// A scan that selects no package has checked nothing, so it must not pass as
/// clean: a CI job started in the wrong directory would otherwise succeed.
#[test]
fn an_empty_selection_is_an_error_not_a_clean_scan() {
    let temp = tempdir().expect("temporary directory");
    for arguments in [&[][..], &["--json", "--quiet"][..], &["--outer"][..]] {
        let output = scan(temp.path(), arguments);
        assert_eq!(output.status.code(), Some(2), "{arguments:?}");
        assert!(output.stdout.is_empty(), "no report for {arguments:?}");
        let stderr = String::from_utf8_lossy(&output.stderr);
        assert!(stderr.contains("no packages to check"), "{stderr}");
        assert!(!stderr.contains("--only"), "no filter was given: {stderr}");
    }

    package(temp.path());
    let output = scan(temp.path(), &["--only", "@nothing/*"]);
    assert_eq!(
        output.status.code(),
        Some(2),
        "a filter that matches nothing"
    );
    assert!(
        String::from_utf8_lossy(&output.stderr)
            .contains("no package name matches --only/--exclude"),
        "the message names the filters"
    );
    assert_eq!(
        scan(temp.path(), &[]).status.code(),
        Some(0),
        "the package itself is clean"
    );
}

/// Release build records hash `Cargo.lock`, so every runner must check it out
/// byte for byte the same. `.gitattributes` keeps LF endings on Windows,
/// where a CRLF checkout once made the release reject the Windows binary.
#[test]
fn lockfile_has_lf_line_endings_on_every_platform() {
    let lock = include_bytes!("../Cargo.lock");
    assert!(
        !lock.contains(&b'\r'),
        "Cargo.lock was checked out with CRLF; check .gitattributes"
    );
}