dart-decimate 0.0.5 → 0.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Cargo.lock +1 -1
- package/Cargo.toml +2 -4
- package/README.md +48 -15
- package/package.json +1 -1
- package/src/baseline.rs +17 -12
- package/src/changed_scope.rs +98 -5
- package/src/cli/audit_run.rs +2 -1
- package/src/cli/common_args.rs +13 -2
- package/src/cli/decision_surface_run.rs +149 -11
- package/src/cli/default_command.rs +304 -6
- package/src/cli/error.rs +4 -1
- package/src/cli/error_output.rs +10 -2
- package/src/cli/fix_run.rs +6 -11
- package/src/cli/html_open.rs +240 -0
- package/src/cli/inspect_run.rs +3 -0
- package/src/cli/output_format.rs +3 -0
- package/src/cli/scope_args.rs +14 -1
- package/src/cli/security_args.rs +4 -1
- package/src/cli/tests.rs +90 -0
- package/src/cli/trace_run.rs +12 -0
- package/src/cli.rs +88 -9
- package/src/config/rule_aliases.rs +8 -0
- package/src/config/rules.rs +9 -4
- package/src/config.rs +1 -0
- package/src/explain.rs +1 -0
- package/src/manifest/mcp.rs +9 -0
- package/src/manifest.rs +31 -10
- package/src/mcp/cli_args.rs +4 -1
- package/src/mcp/tools.rs +9 -0
- package/src/output/html.rs +691 -0
- package/src/output/human.rs +503 -10
- package/src/output/human_details.rs +449 -0
- package/src/output/next_steps.rs +11 -0
- package/src/output/security_findings.rs +5 -1
- package/src/output/types.rs +7 -0
- package/src/output.rs +3 -0
- package/src/report_schema/inventory.rs +1 -0
- package/src/security/detect.rs +724 -11
- package/src/security/tests.rs +390 -1
- package/src/security.rs +6 -0
- package/src/security_gate.rs +7 -2
package/Cargo.lock
CHANGED
package/Cargo.toml
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[package]
|
|
2
2
|
name = "dart-decimate"
|
|
3
|
-
version = "0.0.
|
|
3
|
+
version = "0.0.7"
|
|
4
4
|
edition = "2024"
|
|
5
5
|
rust-version = "1.85"
|
|
6
6
|
description = "Rust-native codebase intelligence for Dart and Flutter module graphs."
|
|
@@ -21,14 +21,12 @@ rayon = "1"
|
|
|
21
21
|
serde = { version = "1", features = ["derive"] }
|
|
22
22
|
serde_json = "1"
|
|
23
23
|
serde_yaml_ng = "0.10"
|
|
24
|
+
tempfile = "3"
|
|
24
25
|
thiserror = "2"
|
|
25
26
|
toml = "1.1"
|
|
26
27
|
tree-sitter = "0.26"
|
|
27
28
|
tree-sitter-dart = "0.2"
|
|
28
29
|
|
|
29
|
-
[dev-dependencies]
|
|
30
|
-
tempfile = "3"
|
|
31
|
-
|
|
32
30
|
[lints.rust]
|
|
33
31
|
unsafe_code = "forbid"
|
|
34
32
|
|
package/README.md
CHANGED
|
@@ -36,7 +36,7 @@ It answers practical questions:
|
|
|
36
36
|
Inside any Dart or Flutter project, run this:
|
|
37
37
|
|
|
38
38
|
```bash
|
|
39
|
-
npx --yes dart-decimate
|
|
39
|
+
npx --yes dart-decimate human .
|
|
40
40
|
```
|
|
41
41
|
|
|
42
42
|
That is the easiest command. It checks the whole repo for dead code, circular
|
|
@@ -44,14 +44,14 @@ dependencies, duplicated code, complex functions, dependency hygiene,
|
|
|
44
44
|
architecture drift, Flutter graph issues, security candidates, and PR-risk
|
|
45
45
|
signals.
|
|
46
46
|
|
|
47
|
-
For
|
|
47
|
+
For JSON output that agents and CI can parse:
|
|
48
48
|
|
|
49
49
|
```bash
|
|
50
|
-
npx --yes dart-decimate
|
|
50
|
+
npx --yes dart-decimate json .
|
|
51
51
|
```
|
|
52
52
|
|
|
53
|
-
If the report says `"verdict": "fail"`, Dart Decimate
|
|
54
|
-
error-level issues. It does not mean the tool crashed.
|
|
53
|
+
If the human report says `FAIL`, or JSON says `"verdict": "fail"`, Dart Decimate
|
|
54
|
+
worked. It means it found error-level issues. It does not mean the tool crashed.
|
|
55
55
|
|
|
56
56
|
Exit codes:
|
|
57
57
|
|
|
@@ -65,7 +65,7 @@ Exit codes:
|
|
|
65
65
|
You do not need to install anything permanently. Use `npx`:
|
|
66
66
|
|
|
67
67
|
```bash
|
|
68
|
-
npx --yes dart-decimate
|
|
68
|
+
npx --yes dart-decimate human .
|
|
69
69
|
```
|
|
70
70
|
|
|
71
71
|
Add this to `package.json` if you want a short project command:
|
|
@@ -73,10 +73,10 @@ Add this to `package.json` if you want a short project command:
|
|
|
73
73
|
```json
|
|
74
74
|
{
|
|
75
75
|
"scripts": {
|
|
76
|
-
"dart-decimate": "dart-decimate
|
|
76
|
+
"dart-decimate": "dart-decimate json ."
|
|
77
77
|
},
|
|
78
78
|
"devDependencies": {
|
|
79
|
-
"dart-decimate": "^0.0.
|
|
79
|
+
"dart-decimate": "^0.0.7"
|
|
80
80
|
}
|
|
81
81
|
}
|
|
82
82
|
```
|
|
@@ -130,15 +130,38 @@ install.
|
|
|
130
130
|
Check everything:
|
|
131
131
|
|
|
132
132
|
```bash
|
|
133
|
-
npx --yes dart-decimate
|
|
133
|
+
npx --yes dart-decimate human .
|
|
134
134
|
```
|
|
135
135
|
|
|
136
|
-
|
|
136
|
+
Machine-readable JSON:
|
|
137
137
|
|
|
138
138
|
```bash
|
|
139
|
-
npx --yes dart-decimate
|
|
139
|
+
npx --yes dart-decimate json .
|
|
140
140
|
```
|
|
141
141
|
|
|
142
|
+
Open the HTML report in your browser:
|
|
143
|
+
|
|
144
|
+
```bash
|
|
145
|
+
npx --yes dart-decimate html .
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
The `html` shortcut opens the report by default. On report commands, use
|
|
149
|
+
`--format html` to print static HTML or `--open` to write a private temporary
|
|
150
|
+
file and open it in the default browser.
|
|
151
|
+
|
|
152
|
+
Human terminal reports strip control characters from user-derived paths and
|
|
153
|
+
messages; HTML reports escape user-derived content.
|
|
154
|
+
|
|
155
|
+
Print the HTML report instead:
|
|
156
|
+
|
|
157
|
+
```bash
|
|
158
|
+
npx --yes dart-decimate html . --stdout > dart-decimate-report.html
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
Changed files only: `npx --yes dart-decimate html . --compare origin/main`.
|
|
162
|
+
`--compare REF` aliases `--changed-since REF` and suggests similar branches when
|
|
163
|
+
the ref is not found.
|
|
164
|
+
|
|
142
165
|
To run the GitHub version directly:
|
|
143
166
|
|
|
144
167
|
```bash
|
|
@@ -302,6 +325,7 @@ Dart Decimate finds candidates for:
|
|
|
302
325
|
- risky WebView settings
|
|
303
326
|
- process execution
|
|
304
327
|
- raw SQL
|
|
328
|
+
- Firebase client API keys in `FirebaseOptions`
|
|
305
329
|
- plain local storage of secret-like material
|
|
306
330
|
|
|
307
331
|
Useful commands:
|
|
@@ -310,8 +334,13 @@ Useful commands:
|
|
|
310
334
|
dart-decimate security . --surface --format json
|
|
311
335
|
dart-decimate security . --ci --sarif-file dart-decimate-security.sarif
|
|
312
336
|
git diff --cached --unified=0 | dart-decimate security . --gate new --diff-stdin --format json
|
|
337
|
+
dart-decimate security . --gate newly-reachable --compare origin/main --format json
|
|
313
338
|
```
|
|
314
339
|
|
|
340
|
+
`--gate new` keeps candidates on added lines. `--gate newly-reachable` keeps
|
|
341
|
+
reachable candidates affected by changed files. Both accept `--compare REF`,
|
|
342
|
+
`--changed-since REF`, `--diff-file PATCH`, or `--diff-stdin`.
|
|
343
|
+
|
|
315
344
|
### 9. PR Risk
|
|
316
345
|
|
|
317
346
|
Use this before merging changed code.
|
|
@@ -412,6 +441,10 @@ Example shape:
|
|
|
412
441
|
}
|
|
413
442
|
```
|
|
414
443
|
|
|
444
|
+
When grouped security findings hide additional occurrences, `next_steps` can
|
|
445
|
+
include `review-security-surface`, which reruns
|
|
446
|
+
`dart-decimate security . --format json --surface`.
|
|
447
|
+
|
|
415
448
|
If a JSON command fails before a report can be built, stdout still stays
|
|
416
449
|
machine-readable:
|
|
417
450
|
|
|
@@ -507,7 +540,7 @@ rules = ["lib/domain:lib/ui"]
|
|
|
507
540
|
|
|
508
541
|
[security]
|
|
509
542
|
surface = true
|
|
510
|
-
categories = ["hardcoded-secret", "insecure-transport", "tls-bypass"]
|
|
543
|
+
categories = ["hardcoded-secret", "firebase-api-key", "insecure-transport", "tls-bypass"]
|
|
511
544
|
|
|
512
545
|
[rules]
|
|
513
546
|
unused-files = "error"
|
|
@@ -575,7 +608,7 @@ Add Dart Decimate to CI so every PR gets the same repo health check:
|
|
|
575
608
|
|
|
576
609
|
```yaml
|
|
577
610
|
- name: Dart Decimate
|
|
578
|
-
run: npx --yes dart-decimate
|
|
611
|
+
run: npx --yes dart-decimate json .
|
|
579
612
|
```
|
|
580
613
|
|
|
581
614
|
That is the easiest CI command. It checks everything Dart Decimate knows how to
|
|
@@ -593,7 +626,7 @@ You can also put the full check in a git hook:
|
|
|
593
626
|
mkdir -p .git/hooks
|
|
594
627
|
cat > .git/hooks/pre-commit <<'SH'
|
|
595
628
|
#!/usr/bin/env sh
|
|
596
|
-
npx --yes dart-decimate
|
|
629
|
+
npx --yes dart-decimate json .
|
|
597
630
|
SH
|
|
598
631
|
chmod +x .git/hooks/pre-commit
|
|
599
632
|
```
|
|
@@ -656,7 +689,7 @@ This repository forbids `unsafe_code`.
|
|
|
656
689
|
|
|
657
690
|
## Release Flow
|
|
658
691
|
|
|
659
|
-
Current version: `0.0.
|
|
692
|
+
Current version: `0.0.7`.
|
|
660
693
|
|
|
661
694
|
After the first public release, changes should go through pull requests.
|
|
662
695
|
|
package/package.json
CHANGED
package/src/baseline.rs
CHANGED
|
@@ -378,6 +378,10 @@ pub fn apply_baseline_to_report(report: &mut JsonReport, baseline: &Baseline) {
|
|
|
378
378
|
.retain(|finding| !known.contains(finding_identity(finding).as_str()));
|
|
379
379
|
filter_detail_sections(report);
|
|
380
380
|
recompute_summary(report);
|
|
381
|
+
let has_hidden_security_surface = report.has_hidden_security_candidate_occurrences();
|
|
382
|
+
report
|
|
383
|
+
.next_steps
|
|
384
|
+
.retain(|step| next_step_live(&step.id, &report.findings, has_hidden_security_surface));
|
|
381
385
|
report.verdict = if report
|
|
382
386
|
.findings
|
|
383
387
|
.iter()
|
|
@@ -435,9 +439,6 @@ fn filter_detail_sections(report: &mut JsonReport) {
|
|
|
435
439
|
report
|
|
436
440
|
.attack_surface
|
|
437
441
|
.retain(|entry| attack_surface_live(entry, &report.security_candidates));
|
|
438
|
-
report
|
|
439
|
-
.next_steps
|
|
440
|
-
.retain(|step| next_step_live(&step.id, &remaining));
|
|
441
442
|
}
|
|
442
443
|
|
|
443
444
|
fn clone_group_live(
|
|
@@ -500,21 +501,25 @@ fn attack_surface_live(
|
|
|
500
501
|
.any(|candidate| candidate.category == entry.category)
|
|
501
502
|
}
|
|
502
503
|
|
|
503
|
-
fn next_step_live(id: &str,
|
|
504
|
+
fn next_step_live(id: &str, findings: &[Finding], has_hidden_security_surface: bool) -> bool {
|
|
504
505
|
match id {
|
|
505
|
-
"trace-unused-export" =>
|
|
506
|
-
"trace-unused-type" =>
|
|
507
|
-
"trace-unused-dependency" =>
|
|
506
|
+
"trace-unused-export" => has_finding_kind(findings, FindingKind::UnusedExport),
|
|
507
|
+
"trace-unused-type" => has_finding_kind(findings, FindingKind::UnusedType),
|
|
508
|
+
"trace-unused-dependency" => findings
|
|
508
509
|
.iter()
|
|
509
|
-
.any(|
|
|
510
|
-
"trace-code-duplication" =>
|
|
511
|
-
"complexity-breakdown" =>
|
|
510
|
+
.any(|finding| is_dependency_hygiene_kind(finding.kind)),
|
|
511
|
+
"trace-code-duplication" => has_finding_kind(findings, FindingKind::CodeDuplication),
|
|
512
|
+
"complexity-breakdown" => findings.iter().any(|finding| complexity_kind(finding.kind)),
|
|
513
|
+
"review-security-surface" => {
|
|
514
|
+
has_hidden_security_surface
|
|
515
|
+
&& has_finding_kind(findings, FindingKind::SecurityCandidate)
|
|
516
|
+
}
|
|
512
517
|
_ => true,
|
|
513
518
|
}
|
|
514
519
|
}
|
|
515
520
|
|
|
516
|
-
fn
|
|
517
|
-
|
|
521
|
+
fn has_finding_kind(findings: &[Finding], expected: FindingKind) -> bool {
|
|
522
|
+
findings.iter().any(|finding| finding.kind == expected)
|
|
518
523
|
}
|
|
519
524
|
|
|
520
525
|
fn complexity_kind(kind: FindingKind) -> bool {
|
package/src/changed_scope.rs
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
use std::collections::BTreeSet;
|
|
2
|
+
use std::fmt;
|
|
2
3
|
use std::path::{Path, PathBuf};
|
|
3
4
|
use std::process::Command;
|
|
4
5
|
|
|
@@ -17,15 +18,37 @@ pub enum ChangedScopeError {
|
|
|
17
18
|
source: std::io::Error,
|
|
18
19
|
},
|
|
19
20
|
/// Git returned a non-zero status.
|
|
20
|
-
#[error("git diff failed for base {base:?}: {stderr}")]
|
|
21
|
+
#[error("git diff failed for base {base:?}: {stderr}{suggestions}")]
|
|
21
22
|
GitDiff {
|
|
22
23
|
/// Base revision passed by the caller.
|
|
23
24
|
base: String,
|
|
24
25
|
/// Stderr from git.
|
|
25
26
|
stderr: String,
|
|
27
|
+
/// Similar refs found in the local repository.
|
|
28
|
+
suggestions: RefSuggestions,
|
|
26
29
|
},
|
|
27
30
|
}
|
|
28
31
|
|
|
32
|
+
/// Similar Git refs for an invalid changed-scope base.
|
|
33
|
+
#[derive(Debug, Clone, Default)]
|
|
34
|
+
pub struct RefSuggestions(Vec<String>);
|
|
35
|
+
|
|
36
|
+
impl fmt::Display for RefSuggestions {
|
|
37
|
+
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
38
|
+
match self.0.as_slice() {
|
|
39
|
+
[] => Ok(()),
|
|
40
|
+
[one] => write!(formatter, "\nDid you mean {one}?"),
|
|
41
|
+
many => write!(formatter, "\nDid you mean one of: {}?", many.join(", ")),
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
impl RefSuggestions {
|
|
47
|
+
pub(crate) fn for_base(root: &Path, base: &str) -> Self {
|
|
48
|
+
Self(similar_refs(root, base))
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
29
52
|
/// Return root-normalized files changed since `base`.
|
|
30
53
|
///
|
|
31
54
|
/// # Errors
|
|
@@ -40,10 +63,7 @@ pub fn changed_files(root: &Path, base: &str) -> Result<Vec<PathBuf>, ChangedSco
|
|
|
40
63
|
.map_err(|source| ChangedScopeError::Git { source })?;
|
|
41
64
|
|
|
42
65
|
if !output.status.success() {
|
|
43
|
-
return Err(
|
|
44
|
-
base: base.to_owned(),
|
|
45
|
-
stderr: String::from_utf8_lossy(&output.stderr).trim().to_owned(),
|
|
46
|
-
});
|
|
66
|
+
return Err(git_diff_error(root, base, &output.stderr));
|
|
47
67
|
}
|
|
48
68
|
|
|
49
69
|
let untracked = Command::new("git")
|
|
@@ -56,6 +76,7 @@ pub fn changed_files(root: &Path, base: &str) -> Result<Vec<PathBuf>, ChangedSco
|
|
|
56
76
|
return Err(ChangedScopeError::GitDiff {
|
|
57
77
|
base: base.to_owned(),
|
|
58
78
|
stderr: String::from_utf8_lossy(&untracked.stderr).trim().to_owned(),
|
|
79
|
+
suggestions: RefSuggestions::default(),
|
|
59
80
|
});
|
|
60
81
|
}
|
|
61
82
|
|
|
@@ -115,3 +136,75 @@ fn normalize_changed_path(root: &Path, line: &str) -> Option<PathBuf> {
|
|
|
115
136
|
let path = normalize_against(root, Path::new(line));
|
|
116
137
|
path.starts_with(root).then_some(path)
|
|
117
138
|
}
|
|
139
|
+
|
|
140
|
+
fn git_diff_error(root: &Path, base: &str, stderr: &[u8]) -> ChangedScopeError {
|
|
141
|
+
ChangedScopeError::GitDiff {
|
|
142
|
+
base: base.to_owned(),
|
|
143
|
+
stderr: String::from_utf8_lossy(stderr).trim().to_owned(),
|
|
144
|
+
suggestions: RefSuggestions::for_base(root, base),
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
fn similar_refs(root: &Path, base: &str) -> Vec<String> {
|
|
149
|
+
let Ok(output) = Command::new("git")
|
|
150
|
+
.args([
|
|
151
|
+
"for-each-ref",
|
|
152
|
+
"--format=%(refname:short)",
|
|
153
|
+
"refs/heads",
|
|
154
|
+
"refs/remotes",
|
|
155
|
+
])
|
|
156
|
+
.current_dir(root)
|
|
157
|
+
.output()
|
|
158
|
+
else {
|
|
159
|
+
return Vec::new();
|
|
160
|
+
};
|
|
161
|
+
if !output.status.success() {
|
|
162
|
+
return Vec::new();
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
let mut scored = String::from_utf8_lossy(&output.stdout)
|
|
166
|
+
.lines()
|
|
167
|
+
.map(str::trim)
|
|
168
|
+
.filter(|ref_name| !ref_name.is_empty())
|
|
169
|
+
.filter(|ref_name| !ref_name.ends_with("/HEAD"))
|
|
170
|
+
.filter(|ref_name| *ref_name != base)
|
|
171
|
+
.filter_map(|ref_name| {
|
|
172
|
+
let short = ref_name.rsplit('/').next().unwrap_or(ref_name);
|
|
173
|
+
let score = levenshtein(base, ref_name).min(levenshtein(base, short));
|
|
174
|
+
let contains_bonus = if ref_name.contains(base) || short.contains(base) {
|
|
175
|
+
0
|
|
176
|
+
} else {
|
|
177
|
+
score
|
|
178
|
+
};
|
|
179
|
+
let score = score.min(contains_bonus);
|
|
180
|
+
(score <= suggestion_threshold(base)).then_some((score, ref_name.to_owned()))
|
|
181
|
+
})
|
|
182
|
+
.collect::<Vec<_>>();
|
|
183
|
+
scored.sort();
|
|
184
|
+
scored
|
|
185
|
+
.into_iter()
|
|
186
|
+
.map(|(_, ref_name)| ref_name)
|
|
187
|
+
.take(3)
|
|
188
|
+
.collect()
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
fn suggestion_threshold(value: &str) -> usize {
|
|
192
|
+
2.max(value.len() / 3)
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
fn levenshtein(left: &str, right: &str) -> usize {
|
|
196
|
+
let right_chars = right.chars().collect::<Vec<_>>();
|
|
197
|
+
let mut previous = (0..=right_chars.len()).collect::<Vec<_>>();
|
|
198
|
+
for (left_index, left_char) in left.chars().enumerate() {
|
|
199
|
+
let mut current = Vec::with_capacity(right_chars.len() + 1);
|
|
200
|
+
current.push(left_index + 1);
|
|
201
|
+
for (right_index, right_char) in right_chars.iter().enumerate() {
|
|
202
|
+
let insertion = current[right_index] + 1;
|
|
203
|
+
let deletion = previous[right_index + 1] + 1;
|
|
204
|
+
let substitution = previous[right_index] + usize::from(left_char != *right_char);
|
|
205
|
+
current.push(insertion.min(deletion).min(substitution));
|
|
206
|
+
}
|
|
207
|
+
previous = current;
|
|
208
|
+
}
|
|
209
|
+
previous[right_chars.len()]
|
|
210
|
+
}
|
package/src/cli/audit_run.rs
CHANGED
|
@@ -7,7 +7,7 @@ use std::time::{SystemTime, UNIX_EPOCH};
|
|
|
7
7
|
use clap::{Arg, ArgMatches};
|
|
8
8
|
|
|
9
9
|
use crate::baseline::baseline_from_report;
|
|
10
|
-
use crate::changed_scope::{changed_file_scope_from_changed, changed_files};
|
|
10
|
+
use crate::changed_scope::{RefSuggestions, changed_file_scope_from_changed, changed_files};
|
|
11
11
|
use crate::config::apply_rules_to_report;
|
|
12
12
|
use crate::graph::normalize_path;
|
|
13
13
|
use crate::output::{
|
|
@@ -284,6 +284,7 @@ where
|
|
|
284
284
|
Err(crate::changed_scope::ChangedScopeError::GitDiff {
|
|
285
285
|
base: base.to_owned(),
|
|
286
286
|
stderr: String::from_utf8_lossy(&output.stderr).trim().to_owned(),
|
|
287
|
+
suggestions: RefSuggestions::default(),
|
|
287
288
|
}
|
|
288
289
|
.into())
|
|
289
290
|
}
|
package/src/cli/common_args.rs
CHANGED
|
@@ -5,6 +5,9 @@ use clap::{Arg, ArgAction, ArgMatches, Command, value_parser};
|
|
|
5
5
|
|
|
6
6
|
use super::regression_args::regression_command;
|
|
7
7
|
|
|
8
|
+
pub(super) const FORMAT_VALUES: [&str; 2] = ["human", "json"];
|
|
9
|
+
pub(super) const REPORT_FORMAT_VALUES: [&str; 4] = ["human", "html", "json", "sarif"];
|
|
10
|
+
|
|
8
11
|
pub(super) fn scan_command(command: Command) -> Command {
|
|
9
12
|
scan_command_with_format(command, format_arg())
|
|
10
13
|
}
|
|
@@ -13,6 +16,7 @@ pub(super) fn report_command(command: Command) -> Command {
|
|
|
13
16
|
super::scope_args::report_command(super::summary_args::summary_command(
|
|
14
17
|
scan_command_with_format(command, report_format_arg()),
|
|
15
18
|
))
|
|
19
|
+
.arg(open_arg())
|
|
16
20
|
}
|
|
17
21
|
|
|
18
22
|
fn scan_command_with_format(command: Command, format: Arg) -> Command {
|
|
@@ -70,11 +74,18 @@ pub(super) fn format_arg() -> Arg {
|
|
|
70
74
|
.value_name("FORMAT")
|
|
71
75
|
.help("Output format")
|
|
72
76
|
.default_value("human")
|
|
73
|
-
.value_parser(
|
|
77
|
+
.value_parser(FORMAT_VALUES)
|
|
74
78
|
}
|
|
75
79
|
|
|
76
80
|
fn report_format_arg() -> Arg {
|
|
77
|
-
format_arg().value_parser(
|
|
81
|
+
format_arg().value_parser(REPORT_FORMAT_VALUES)
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
fn open_arg() -> Arg {
|
|
85
|
+
Arg::new("open")
|
|
86
|
+
.long("open")
|
|
87
|
+
.help("Write an HTML report to a temporary file and open its file:// URL in the default browser")
|
|
88
|
+
.action(ArgAction::SetTrue)
|
|
78
89
|
}
|
|
79
90
|
|
|
80
91
|
pub(super) fn config_arg() -> Arg {
|
|
@@ -1,15 +1,16 @@
|
|
|
1
1
|
use std::io::Write;
|
|
2
2
|
|
|
3
|
-
use clap::{Arg, ArgMatches, Command, value_parser};
|
|
3
|
+
use clap::{Arg, ArgMatches, Command, parser::ValueSource, value_parser};
|
|
4
4
|
|
|
5
5
|
use crate::changed_scope::changed_files;
|
|
6
6
|
use crate::decision_surface::{
|
|
7
7
|
decision_surface_report_for_command, render_decision_surface_report,
|
|
8
8
|
};
|
|
9
|
+
use crate::output::render_decision_surface_html_report;
|
|
9
10
|
use crate::scan::{ScanOptions, scan_project_with_options};
|
|
10
11
|
|
|
11
12
|
use super::common_args::{config_arg, format_arg, root_arg, root_flag_arg, root_path};
|
|
12
|
-
use super::{CliError,
|
|
13
|
+
use super::{CliError, ReportOutputFormat, html_open};
|
|
13
14
|
|
|
14
15
|
pub(super) fn decision_surface_command() -> Command {
|
|
15
16
|
decision_surface_command_named(
|
|
@@ -54,8 +55,21 @@ fn decision_surface_command_named(name: &'static str, about: &'static str) -> Co
|
|
|
54
55
|
pub(super) fn run_decision_surface<W: Write>(
|
|
55
56
|
subcommand: &ArgMatches,
|
|
56
57
|
mut writer: W,
|
|
57
|
-
command: &str,
|
|
58
|
+
command: &'static str,
|
|
58
59
|
) -> Result<i32, CliError> {
|
|
60
|
+
run_decision_surface_with_opener(subcommand, &mut writer, command, html_open::open_url)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
fn run_decision_surface_with_opener<W, F>(
|
|
64
|
+
subcommand: &ArgMatches,
|
|
65
|
+
writer: &mut W,
|
|
66
|
+
command: &'static str,
|
|
67
|
+
opener: F,
|
|
68
|
+
) -> Result<i32, CliError>
|
|
69
|
+
where
|
|
70
|
+
W: Write,
|
|
71
|
+
F: FnOnce(&str) -> std::io::Result<()>,
|
|
72
|
+
{
|
|
59
73
|
let root = root_path(subcommand);
|
|
60
74
|
let config = super::load_config(&root, subcommand)?;
|
|
61
75
|
let project = scan_project_with_options(
|
|
@@ -76,24 +90,148 @@ pub(super) fn run_decision_surface<W: Write>(
|
|
|
76
90
|
let report =
|
|
77
91
|
decision_surface_report_for_command(&project, base, &changed, max_decisions, command);
|
|
78
92
|
|
|
79
|
-
match
|
|
80
|
-
|
|
93
|
+
match decision_surface_output_format(subcommand, command)? {
|
|
94
|
+
ReportOutputFormat::Human => {
|
|
81
95
|
writer.write_all(render_decision_surface_report(&report).as_bytes())?;
|
|
82
96
|
}
|
|
83
|
-
|
|
84
|
-
|
|
97
|
+
ReportOutputFormat::HtmlOpen => {
|
|
98
|
+
html_open::write_and_open_html_document_with(
|
|
99
|
+
&report.command,
|
|
100
|
+
&render_decision_surface_html_report(&report),
|
|
101
|
+
writer,
|
|
102
|
+
opener,
|
|
103
|
+
)?;
|
|
104
|
+
}
|
|
105
|
+
ReportOutputFormat::Html => {
|
|
106
|
+
writer.write_all(render_decision_surface_html_report(&report).as_bytes())?;
|
|
107
|
+
}
|
|
108
|
+
ReportOutputFormat::Json => {
|
|
109
|
+
serde_json::to_writer_pretty(&mut *writer, &report)?;
|
|
85
110
|
writeln!(writer)?;
|
|
86
111
|
}
|
|
112
|
+
ReportOutputFormat::Sarif => return Err(CliError::UnsupportedSarifFormat { command }),
|
|
87
113
|
}
|
|
88
114
|
Ok(0)
|
|
89
115
|
}
|
|
90
116
|
|
|
91
|
-
fn
|
|
92
|
-
|
|
117
|
+
fn decision_surface_output_format(
|
|
118
|
+
subcommand: &ArgMatches,
|
|
119
|
+
command: &'static str,
|
|
120
|
+
) -> Result<ReportOutputFormat, CliError> {
|
|
121
|
+
let format = match subcommand
|
|
93
122
|
.get_one::<String>("format")
|
|
94
123
|
.map_or("human", String::as_str)
|
|
95
124
|
{
|
|
96
|
-
"
|
|
97
|
-
|
|
125
|
+
"html" => ReportOutputFormat::Html,
|
|
126
|
+
"json" => ReportOutputFormat::Json,
|
|
127
|
+
"sarif" => ReportOutputFormat::Sarif,
|
|
128
|
+
_ => ReportOutputFormat::Human,
|
|
129
|
+
};
|
|
130
|
+
let open_html = subcommand
|
|
131
|
+
.try_get_one::<bool>("open")
|
|
132
|
+
.ok()
|
|
133
|
+
.flatten()
|
|
134
|
+
.copied()
|
|
135
|
+
.unwrap_or_default();
|
|
136
|
+
if !open_html {
|
|
137
|
+
return Ok(format);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
let explicit_format = subcommand.value_source("format") == Some(ValueSource::CommandLine);
|
|
141
|
+
if explicit_format && format != ReportOutputFormat::Html {
|
|
142
|
+
return Err(CliError::HtmlOpenRequiresHtml);
|
|
143
|
+
}
|
|
144
|
+
if format == ReportOutputFormat::Sarif {
|
|
145
|
+
return Err(CliError::UnsupportedSarifFormat { command });
|
|
146
|
+
}
|
|
147
|
+
Ok(ReportOutputFormat::HtmlOpen)
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
#[cfg(test)]
|
|
151
|
+
mod tests {
|
|
152
|
+
use std::cell::RefCell;
|
|
153
|
+
use std::fs;
|
|
154
|
+
use std::process::Command;
|
|
155
|
+
|
|
156
|
+
use tempfile::TempDir;
|
|
157
|
+
|
|
158
|
+
use super::*;
|
|
159
|
+
|
|
160
|
+
#[test]
|
|
161
|
+
fn audit_brief_open_writes_html_and_opens_file_url() -> Result<(), Box<dyn std::error::Error>> {
|
|
162
|
+
let fixture = changed_dependency_fixture()?;
|
|
163
|
+
let matches = super::super::command().try_get_matches_from([
|
|
164
|
+
"dart-decimate",
|
|
165
|
+
"audit",
|
|
166
|
+
fixture.path().to_str().unwrap_or("."),
|
|
167
|
+
"--base",
|
|
168
|
+
"HEAD",
|
|
169
|
+
"--brief",
|
|
170
|
+
"--open",
|
|
171
|
+
])?;
|
|
172
|
+
let Some(("audit", subcommand)) = matches.subcommand() else {
|
|
173
|
+
panic!("audit subcommand");
|
|
174
|
+
};
|
|
175
|
+
let opened = RefCell::new(String::new());
|
|
176
|
+
let mut output = Vec::new();
|
|
177
|
+
|
|
178
|
+
let code =
|
|
179
|
+
run_decision_surface_with_opener(subcommand, &mut output, "audit --brief", |url| {
|
|
180
|
+
opened.replace(url.to_owned());
|
|
181
|
+
Ok(())
|
|
182
|
+
})?;
|
|
183
|
+
|
|
184
|
+
let opened = opened.into_inner();
|
|
185
|
+
let path = opened.strip_prefix("file://").unwrap_or(&opened);
|
|
186
|
+
let html = fs::read_to_string(path)?;
|
|
187
|
+
let message = String::from_utf8(output)?;
|
|
188
|
+
assert_eq!(code, 0);
|
|
189
|
+
assert!(opened.starts_with("file:///"));
|
|
190
|
+
assert!(message.contains("Opened HTML report: file:///"));
|
|
191
|
+
assert!(html.contains("<h1>audit --brief decision surface</h1>"));
|
|
192
|
+
assert!(html.contains("pubspec.yaml"));
|
|
193
|
+
Ok(())
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
fn changed_dependency_fixture() -> Result<TempDir, Box<dyn std::error::Error>> {
|
|
197
|
+
let fixture = tempfile::tempdir()?;
|
|
198
|
+
git(&fixture, ["init", "-q"])?;
|
|
199
|
+
git(
|
|
200
|
+
&fixture,
|
|
201
|
+
["config", "user.email", "dart-decimate@example.com"],
|
|
202
|
+
)?;
|
|
203
|
+
git(&fixture, ["config", "user.name", "Dart Decimate Tests"])?;
|
|
204
|
+
write(&fixture, "pubspec.yaml", "name: app\n")?;
|
|
205
|
+
write(&fixture, "lib/main.dart", "void main() {}\n")?;
|
|
206
|
+
git(&fixture, ["add", "."])?;
|
|
207
|
+
git(&fixture, ["commit", "-m", "baseline", "-q"])?;
|
|
208
|
+
write(
|
|
209
|
+
&fixture,
|
|
210
|
+
"pubspec.yaml",
|
|
211
|
+
"name: app\ndependencies:\n http: ^1.0.0\n",
|
|
212
|
+
)?;
|
|
213
|
+
Ok(fixture)
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
fn git<const N: usize>(
|
|
217
|
+
fixture: &TempDir,
|
|
218
|
+
args: [&str; N],
|
|
219
|
+
) -> Result<(), Box<dyn std::error::Error>> {
|
|
220
|
+
let output = Command::new("git")
|
|
221
|
+
.args(args)
|
|
222
|
+
.current_dir(fixture.path())
|
|
223
|
+
.output()?;
|
|
224
|
+
if output.status.success() {
|
|
225
|
+
return Ok(());
|
|
226
|
+
}
|
|
227
|
+
Err(String::from_utf8_lossy(&output.stderr).to_string().into())
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
fn write(fixture: &TempDir, path: &str, source: &str) -> Result<(), std::io::Error> {
|
|
231
|
+
let path = fixture.path().join(path);
|
|
232
|
+
if let Some(parent) = path.parent() {
|
|
233
|
+
fs::create_dir_all(parent)?;
|
|
234
|
+
}
|
|
235
|
+
fs::write(path, source)
|
|
98
236
|
}
|
|
99
237
|
}
|