fluidattacks-blends 0.6.0

Blends imperative shell: parsing, AST-graph construction, serialization
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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
//! Top-level orchestration: a path to its set of graphs.

use std::path::Path;

use blends_domain::graph_set::GraphSet;

use crate::ast::get_ast_graph;
use crate::content::Content;
use crate::syntax::get_syntax_graph;

#[must_use]
pub fn get_graphs_from_path(
    path: &Path,
    with_cfg: Option<bool>,
    with_metadata: Option<bool>,
) -> GraphSet {
    let Some(content) = Content::from_path(path, None) else {
        return GraphSet::default();
    };

    let Some(ast) = get_ast_graph(&content) else {
        return GraphSet::default();
    };

    let Some(syntax) = get_syntax_graph(&ast, &content, with_cfg, with_metadata) else {
        return GraphSet {
            ast: Some(ast),
            syntax: None,
        };
    };

    GraphSet {
        ast: Some(ast),
        syntax: Some(syntax),
    }
}

#[cfg(test)]
mod tests {
    use super::get_graphs_from_path;
    use crate::attrs::{
        ast_edge_attrs, ast_node_attrs, sorted_object, syntax_edge_attrs, syntax_node_attrs,
    };
    use blends_domain::ast::AstGraph;
    use blends_domain::syntax::{SyntaxGraph, SyntaxNode};
    use blends_domain::NodeId;
    use serde_json::{Map, Value};
    use std::collections::{BTreeMap, BTreeSet};
    use std::fs;
    use std::path::{Path, PathBuf};
    use test_case::test_case;

    fn fixtures_dir() -> PathBuf {
        Path::new(env!("CARGO_MANIFEST_DIR")).join("../../test/data/test_files/syntax_graph")
    }

    fn results_dir() -> PathBuf {
        Path::new(env!("CARGO_MANIFEST_DIR")).join("../../test/data/results")
    }

    fn output_dir() -> PathBuf {
        Path::new(env!("CARGO_MANIFEST_DIR")).join("../../test/data/output")
    }

    fn export_ast_graph_as_json(graph: &AstGraph) -> Value {
        let mut nodes = Map::new();
        for (id, node) in &graph.nodes {
            nodes.insert(id.0.to_string(), sorted_object(ast_node_attrs(node)));
        }

        let mut edges = Map::new();
        for (from, targets) in &graph.edges {
            let mut inner = Map::new();
            for (to, edge) in targets {
                inner.insert(to.0.to_string(), sorted_object(ast_edge_attrs(*edge)));
            }
            edges.insert(from.0.to_string(), Value::Object(inner));
        }

        let mut root = BTreeMap::new();
        root.insert("edges".to_owned(), Value::Object(edges));
        root.insert("nodes".to_owned(), Value::Object(nodes));
        sorted_object(root)
    }

    fn export_syntax_graph_as_json(graph: &SyntaxGraph) -> Value {
        let mut nodes = Map::new();
        for (id, node) in &graph.nodes {
            let attrs = syntax_node_attrs(node).unwrap_or_else(|| {
                panic!("syntax export not implemented for {}", node.label_type())
            });
            nodes.insert(id.0.to_string(), sorted_object(attrs));
        }

        let mut edges = Map::new();
        for (from, targets) in &graph.edges {
            let mut inner = Map::new();
            for (to, edge) in targets {
                inner.insert(to.0.to_string(), sorted_object(syntax_edge_attrs(*edge)));
            }
            edges.insert(from.0.to_string(), Value::Object(inner));
        }

        let mut root = BTreeMap::new();
        root.insert("edges".to_owned(), Value::Object(edges));
        root.insert("nodes".to_owned(), Value::Object(nodes));
        sorted_object(root)
    }

    #[test]
    fn empty_set_for_unsupported_file() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("a.unknown");
        fs::write(&path, b"whatever").unwrap();

        assert!(get_graphs_from_path(&path, None, None).ast.is_none());
    }

    #[test]
    fn empty_set_for_malformed_supported_file() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("a.java");
        fs::write(&path, b"class A {").unwrap();

        assert!(get_graphs_from_path(&path, None, None).ast.is_none());
    }

    fn rename_field_key(key: &str) -> String {
        key.strip_prefix("label_field_")
            .map_or_else(|| key.to_owned(), |field| format!("{field}_id"))
    }

    fn rename_node_attrs(attrs: &Value) -> Value {
        let Some(attrs) = attrs.as_object() else {
            return attrs.clone();
        };
        let mut renamed = Map::new();
        for (key, value) in attrs {
            renamed.insert(rename_field_key(key), value.clone());
        }
        Value::Object(renamed)
    }

    // The python golden keys node fields as `label_field_<field>`; the Rust engine
    // keys them as `<field>_id`. Rewrite the golden's keys in memory so the two
    // graphs compare on content. The `results/` file on disk is untouched.
    fn normalize_field_keys(graph: &Value) -> Value {
        let mut nodes = Map::new();
        if let Some(original) = graph.get("nodes").and_then(Value::as_object) {
            for (id, attrs) in original {
                nodes.insert(id.clone(), rename_node_attrs(attrs));
            }
        }

        let mut result = Map::new();
        if let Some(edges) = graph.get("edges") {
            result.insert("edges".to_owned(), edges.clone());
        }
        result.insert("nodes".to_owned(), Value::Object(nodes));
        Value::Object(result)
    }

    fn write_rust_output(suffix: &str, relative: &str, ast: &Value, syntax: Option<&Value>) {
        let mut entry = Map::new();
        entry.insert("graph".to_owned(), ast.clone());
        if let Some(syntax) = syntax {
            entry.insert("syntax_graph".to_owned(), syntax.clone());
        }
        let mut by_path = Map::new();
        by_path.insert(relative.to_owned(), Value::Object(entry));
        let mut root = Map::new();
        root.insert("graphs".to_owned(), Value::Object(by_path));

        let pretty = serde_json::to_string_pretty(&Value::Object(root)).expect("serialize output");
        let dir = output_dir();
        fs::create_dir_all(&dir).expect("create output dir");
        fs::write(dir.join(format!("root-graph_{suffix}.json")), pretty).expect("write output");
    }

    fn section(graph: &Value, key: &str) -> Map<String, Value> {
        graph
            .get(key)
            .and_then(Value::as_object)
            .cloned()
            .unwrap_or_default()
    }

    // The python golden dumps the ast after the syntax readers run, and some
    // readers overwrite `label_l` (c# class/method declaration take the line of
    // their identifier). Until those readers exist in rust, drop only `label_l`
    // on those node types from both sides, so just the mutated line is ignored
    // while every other attribute (line column, type, fields) is still compared.
    fn ignore_line_for(nodes: Map<String, Value>, skip_types: &[&str]) -> Map<String, Value> {
        nodes
            .into_iter()
            .map(|(id, mut attrs)| {
                let skip = attrs
                    .get("label_type")
                    .and_then(Value::as_str)
                    .is_some_and(|kind| skip_types.contains(&kind));
                if let Some(node) = attrs.as_object_mut().filter(|_| skip) {
                    node.remove("label_l");
                }
                (id, attrs)
            })
            .collect()
    }

    // Concise per-entry diff: only the ids whose content differs, rust vs python.
    fn diff_section(
        kind: &str,
        rust: &Map<String, Value>,
        python: &Map<String, Value>,
    ) -> Vec<String> {
        let mut diffs = Vec::new();
        for (id, rust_entry) in rust {
            match python.get(id) {
                None => diffs.push(format!(
                    "{kind} {id}: in rust output, missing in python golden"
                )),
                Some(python_entry) if python_entry != rust_entry => diffs.push(format!(
                    "{kind} {id} differs:\n  rust:   {rust_entry}\n  python: {python_entry}"
                )),
                Some(_) => {}
            }
        }
        for id in python.keys() {
            if !rust.contains_key(id) {
                diffs.push(format!(
                    "{kind} {id}: in python golden, missing in rust output"
                ));
            }
        }
        diffs
    }

    const MAX_REPORTED_DIFFS: usize = 30;

    // Fixture suffixes whose language has no rust syntax dispatcher yet.
    const SYNTAX_NOT_YET_MIGRATED: &[&str] = &[
        "elixir", "go", "hcl", "kotlin", "php", "ruby", "rust", "scala", "swift",
    ];

    // Fixture suffixes whose language dispatcher exists but still degrades
    // some node types to MissingNode: implemented nodes compare exactly,
    // diffs anchored at the missing placeholders are ignored.
    const SYNTAX_IN_PROGRESS: &[&str] = &[];

    fn ast_diffs(rust_ast: &Value, golden: &Value, suffix: &str) -> Vec<String> {
        let expected = golden
            .get("graph")
            .map(normalize_field_keys)
            .expect("locate graph block in python golden");

        // c# alone has syntax readers that overwrite `label_l` on class/method
        // declarations; ignore that attribute only for c#, never other languages.
        let line_skip: &[&str] = match suffix {
            "c_sharp" => &["class_declaration", "method_declaration"],
            _ => &[],
        };
        let mut diffs = diff_section(
            "node",
            &ignore_line_for(section(rust_ast, "nodes"), line_skip),
            &ignore_line_for(section(&expected, "nodes"), line_skip),
        );
        diffs.extend(diff_section(
            "edge",
            &section(rust_ast, "edges"),
            &section(&expected, "edges"),
        ));
        diffs
    }

    fn syntax_diffs(generated_syntax: &Value, golden: &Value) -> Vec<String> {
        let expected = golden
            .get("syntax_graph")
            .cloned()
            .expect("locate syntax_graph block in python golden");

        let mut diffs = diff_section(
            "syntax node",
            &section(generated_syntax, "nodes"),
            &section(&expected, "nodes"),
        );
        diffs.extend(diff_section(
            "syntax edge",
            &section(generated_syntax, "edges"),
            &section(&expected, "edges"),
        ));
        diffs
    }

    // The ids the rust engine degraded to MissingNode: subtrees whose reader
    // is pending, plus punctuation children the python readers consumed
    // without creating a syntax node.
    fn missing_ids(generated_syntax: &Value) -> BTreeSet<String> {
        section(generated_syntax, "nodes")
            .into_iter()
            .filter(|(_, attrs)| {
                attrs.get("label_type").and_then(Value::as_str) == Some("MissingNode")
            })
            .map(|(id, _)| id)
            .collect()
    }

    fn edge_target_ids(edges: &Map<String, Value>, from: &str) -> Vec<String> {
        edges
            .get(from)
            .and_then(Value::as_object)
            .map(|targets| targets.keys().cloned().collect())
            .unwrap_or_default()
    }

    fn pending_subtree_ids(generated_syntax: &Value) -> BTreeSet<String> {
        let edges = section(generated_syntax, "edges");
        let mut skip = missing_ids(generated_syntax);
        let mut stack: Vec<String> = skip.iter().cloned().collect();
        while let Some(from) = stack.pop() {
            let fresh: Vec<String> = edge_target_ids(&edges, &from)
                .into_iter()
                .filter(|to| skip.insert(to.clone()))
                .collect();
            stack.extend(fresh);
        }
        skip
    }

    fn drop_missing_nodes(
        nodes: Map<String, Value>,
        skip: &BTreeSet<String>,
    ) -> Map<String, Value> {
        nodes
            .into_iter()
            .filter(|(id, _)| !skip.contains(id))
            .collect()
    }

    fn drop_missing_targets(targets: &Value, skip: &BTreeSet<String>) -> Map<String, Value> {
        targets
            .as_object()
            .cloned()
            .unwrap_or_default()
            .into_iter()
            .filter(|(to, _)| !skip.contains(to))
            .collect()
    }

    fn drop_missing_edges(
        edges: Map<String, Value>,
        skip: &BTreeSet<String>,
    ) -> Map<String, Value> {
        edges
            .into_iter()
            .filter(|(from, _)| !skip.contains(from))
            .map(|(from, targets)| (from, drop_missing_targets(&targets, skip)))
            .filter(|(_, kept)| !kept.is_empty())
            .map(|(from, kept)| (from, Value::Object(kept)))
            .collect()
    }

    // In-progress compare: everything not anchored at a MissingNode id must
    // match the golden exactly, on both sides — a golden-only entry between
    // implemented nodes is still a real diff.
    fn syntax_diffs_partial(generated_syntax: &Value, golden: &Value) -> Vec<String> {
        let expected = golden
            .get("syntax_graph")
            .cloned()
            .expect("locate syntax_graph block in python golden");
        let skip = pending_subtree_ids(generated_syntax);

        let mut diffs = diff_section(
            "syntax node",
            &drop_missing_nodes(section(generated_syntax, "nodes"), &skip),
            &drop_missing_nodes(section(&expected, "nodes"), &skip),
        );
        diffs.extend(diff_section(
            "syntax edge",
            &drop_missing_edges(section(generated_syntax, "edges"), &skip),
            &drop_missing_edges(section(&expected, "edges"), &skip),
        ));
        diffs
    }

    #[test_case("c_sharp.cs", "c_sharp")]
    #[test_case("elixir.ex", "elixir")]
    #[test_case("go.go", "go")]
    #[test_case("terraform.tf", "hcl")]
    #[test_case("java.java", "java")]
    #[test_case("javascript.js", "javascript")]
    #[test_case("json.json", "json")]
    #[test_case("kotlin.kt", "kotlin")]
    #[test_case("python.py", "python")]
    #[test_case("php.php", "php")]
    #[test_case("ruby.rb", "ruby")]
    #[test_case("rust.rs", "rust")]
    #[test_case("scala.scala", "scala")]
    #[test_case("swift.swift", "swift")]
    #[test_case("syntax_cfg.ts", "typescript")]
    #[test_case("yaml.yaml", "yaml")]
    #[test_case("templates/helm_configmap.yaml", "helm_configmap_yaml")]
    #[test_case("templates/helm_configmap.json", "helm_configmap_json")]
    #[test_case("flow_mapping.yaml", "flow_mapping")]
    #[test_case("flow_sequence.yaml", "flow_sequence")]
    fn graph_generation(test_file: &str, suffix: &str) {
        let path = fixtures_dir().join(test_file);
        let graph_set = get_graphs_from_path(&path, None, None);

        assert!(
            !(SYNTAX_NOT_YET_MIGRATED.contains(&suffix) && SYNTAX_IN_PROGRESS.contains(&suffix)),
            "suffix {suffix} cannot be pending and in progress at the same time"
        );
        assert_eq!(
            graph_set.syntax.is_none(),
            SYNTAX_NOT_YET_MIGRATED.contains(&suffix),
            "\n[Syntax Parity Error] Inconsistency detected for language extension: .{suffix}\n\
             - Was syntax graph generated (None)? -> {}\n\
             - Is it marked as pending in SYNTAX_NOT_YET_MIGRATED? -> {}\n\
             👉 Hint: If it was generated but is marked as pending, move '.{suffix}' to \
             SYNTAX_IN_PROGRESS (partial compare) or drop it from both lists (strict compare).\n\
             👉 Hint: If it is not pending but returned None, a regression occurred in the language dispatcher.",
            graph_set.syntax.is_none(),
            SYNTAX_NOT_YET_MIGRATED.contains(&suffix)
        );

        let generated_ast = graph_set
            .ast
            .as_ref()
            .map(export_ast_graph_as_json)
            .expect("AST graph should be built for the fixture");

        let generated_syntax = graph_set.syntax.as_ref().map(export_syntax_graph_as_json);

        let relative = format!("test/data/test_files/{test_file}");
        write_rust_output(suffix, &relative, &generated_ast, generated_syntax.as_ref());

        let python_results: Value = serde_json::from_str(
            &fs::read_to_string(results_dir().join(format!("root-graph_{suffix}.json")))
                .expect("read python golden"),
        )
        .expect("parse python golden");
        let golden = python_results
            .get("graphs")
            .and_then(|graphs| graphs.get(&relative))
            .expect("locate the fixture entry in python golden");

        let mut diffs = ast_diffs(&generated_ast, golden, suffix);
        if let Some(generated_syntax) = &generated_syntax {
            if SYNTAX_IN_PROGRESS.contains(&suffix) {
                diffs.extend(syntax_diffs_partial(generated_syntax, golden));
            } else {
                diffs.extend(syntax_diffs(generated_syntax, golden));
            }
        }

        assert_graph_parity(suffix, &diffs);
    }

    #[test_case("java.java", "java")]
    fn graph_generation_with_metadata(test_file: &str, suffix: &str) {
        let path = fixtures_dir().join(test_file);
        let mut graph_set = get_graphs_from_path(&path, None, Some(true));

        let relative_fixture = format!("test/data/test_files/syntax_graph/{test_file}");
        if let Some(syntax) = graph_set.syntax.as_mut() {
            if let Some(SyntaxNode::Metadata {
                path: metadata_path,
                ..
            }) = syntax.nodes.get_mut(&NodeId(0))
            {
                *metadata_path = relative_fixture;
            }
        }

        let generated_ast = graph_set
            .ast
            .as_ref()
            .map(export_ast_graph_as_json)
            .expect("AST graph should be built for the fixture");
        let generated_syntax = graph_set
            .syntax
            .as_ref()
            .map(export_syntax_graph_as_json)
            .expect("syntax graph should be built with metadata");

        let relative = format!("test/data/test_files/{test_file}");
        write_rust_output(
            &format!("metadata_{suffix}"),
            &relative,
            &generated_ast,
            Some(&generated_syntax),
        );

        let python_results: Value = serde_json::from_str(
            &fs::read_to_string(results_dir().join(format!("root-graph-metadata_{suffix}.json")))
                .expect("read python golden"),
        )
        .expect("parse python golden");
        let golden = python_results
            .get("graphs")
            .and_then(|graphs| graphs.get(&relative))
            .expect("locate the fixture entry in python golden");

        let mut diffs = ast_diffs(&generated_ast, golden, suffix);
        diffs.extend(syntax_diffs(&generated_syntax, golden));
        assert_graph_parity(suffix, &diffs);
    }

    fn assert_graph_parity(suffix: &str, diffs: &[String]) {
        let shown = diffs
            .iter()
            .take(MAX_REPORTED_DIFFS)
            .cloned()
            .collect::<Vec<_>>()
            .join("\n");
        let extra = diffs.len().saturating_sub(MAX_REPORTED_DIFFS);
        let more = if extra > 0 {
            format!("\n… and {extra} more differing entries")
        } else {
            String::new()
        };

        assert!(
            diffs.is_empty(),
            "graph parity mismatch for {suffix} ({} differing entries):\n{shown}{more}",
            diffs.len()
        );
    }
}