diffr-cli 0.1.3

Structural diffs with a streaming API and interactive terminal frontend.
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
//! The plugin host and the bundled plugins, run the way the stream runs
//! them: a file projected with the bundled queries, then each plugin's
//! native code through a [`Pipeline`].
mod context;
mod deleted_bodies;
mod group;
mod hide_files;
mod removed_runs;
mod summarize;
mod test_bodies;

use super::*;
use crate::config::{Config, Params};
use crate::options::DiffOptions;
use crate::protocol::{project, Diff, FileRef};
use diffr_plugin_sdk::tree::{docstring_of, has_tag, is_fold, walk};
use diffr_plugin_sdk::{FileEntry, Move, Plugin};
use serde_json::json;

/// Project a two-source comparison with the bundled queries, the way the
/// stream does before the plugins run.
pub(crate) fn project(
    path: &str,
    before: &str,
    after: &str,
) -> (FileChange, Pairing<protocol::Source>) {
    project_with(path, before, after, DiffOptions::default())
}

pub(crate) fn project_with(
    path: &str,
    before: &str,
    after: &str,
    options: DiffOptions,
) -> (FileChange, Pairing<protocol::Source>) {
    let params = Config::from_toml("").unwrap().compile().unwrap();
    project_compiled(path, before, after, &params, options)
}

/// Project with the queries `params` was compiled with.
pub(crate) fn project_compiled(
    path: &str,
    before: &str,
    after: &str,
    params: &Params,
    options: DiffOptions,
) -> (FileChange, Pairing<protocol::Source>) {
    let result = crate::summary::DiffResult::from_sources_with_options(
        path, before, after, params, &options,
    )
    .unwrap();
    let file_ref = FileRef {
        path: path.to_owned(),
        oid: String::new(),
        mode: String::new(),
    };
    let file = FileChange {
        file: Pairing::Both {
            lhs: file_ref.clone(),
            rhs: file_ref,
        },
        status: FileStatus::Modified,
        tags: Vec::new(),
    };
    let diff = project::diff(
        &result,
        project::Inputs {
            file: &file.file,
            sizes: (before.len() as u64, after.len() as u64),
            syntax: (Vec::new(), Vec::new()),
        },
    );
    let Diff::Text { sides, .. } = diff else {
        panic!("text diff expected");
    };
    (file, sides)
}

/// A manifest entry for `path` on the sides `sides` names.
pub(crate) fn manifest<T>(path: &str, sides: &tree::Pairing<T>, status: FileStatus) -> FileChange {
    let file_ref = || FileRef {
        path: path.to_owned(),
        oid: String::new(),
        mode: String::new(),
    };
    FileChange {
        file: match sides {
            tree::Pairing::Both { .. } => Pairing::Both {
                lhs: file_ref(),
                rhs: file_ref(),
            },
            tree::Pairing::LeftOnly { .. } => Pairing::LeftOnly { lhs: file_ref() },
            tree::Pairing::RightOnly { .. } => Pairing::RightOnly { rhs: file_ref() },
        },
        status,
        tags: Vec::new(),
    }
}

/// The wire's sides as the trees plugins read.
pub(crate) fn trees(sides: &Pairing<protocol::Source>) -> tree::Pairing<tree::Source> {
    match sides {
        Pairing::Both { lhs, rhs } => tree::Pairing::Both {
            lhs: to_tree(lhs),
            rhs: to_tree(rhs),
        },
        Pairing::LeftOnly { lhs } => tree::Pairing::LeftOnly { lhs: to_tree(lhs) },
        Pairing::RightOnly { rhs } => tree::Pairing::RightOnly { rhs: to_tree(rhs) },
    }
}

/// Trees built by hand, as the wire's sides.
pub(crate) fn wire(sides: tree::Pairing<tree::Source>) -> Pairing<protocol::Source> {
    let source = |side: tree::Source| protocol::Source {
        text: side.text,
        syntax: Vec::new(),
        regions: from_tree(side.regions),
    };
    match sides {
        tree::Pairing::Both { lhs, rhs } => Pairing::Both {
            lhs: source(lhs),
            rhs: source(rhs),
        },
        tree::Pairing::LeftOnly { lhs } => Pairing::LeftOnly { lhs: source(lhs) },
        tree::Pairing::RightOnly { rhs } => Pairing::RightOnly { rhs: source(rhs) },
    }
}

pub(crate) fn lhs(sides: &tree::Pairing<tree::Source>) -> &tree::Source {
    sides.lhs().expect("a before side")
}

pub(crate) fn rhs(sides: &tree::Pairing<tree::Source>) -> &tree::Source {
    sides.rhs().expect("an after side")
}

/// A pipeline of the bundled plugin `name` alone, made with its defaults and
/// `overrides`.
pub(crate) fn bundled(name: &str, overrides: serde_json::Value) -> Pipeline {
    let mut options = builtin::manifest(name)
        .expect("a bundled plugin")
        .defaults();
    let serde_json::Value::Object(overrides) = overrides else {
        panic!("overrides are an object");
    };
    options.extend(overrides);
    let mut pipeline = Pipeline::default();
    pipeline
        .push(
            name,
            serde_json::Value::Object(options),
            &|host, options| {
                if let Some(bytes) = builtin::component(name) {
                    let engine = super::wasm::engine()?;
                    super::wasm::WasmPlugin::load(
                        &engine,
                        &super::config::ComponentSource::Bundled(bytes),
                    )?
                    .create(host, options)
                } else {
                    native::registered(native::lookup(name)?.expect("native code"), host, options)
                }
            },
        )
        .unwrap();
    pipeline
}

/// Run the bundled plugin `name` with `overrides` and carry out its moves.
pub(crate) fn run(
    name: &str,
    overrides: serde_json::Value,
    file: &FileChange,
    sides: &mut Pairing<protocol::Source>,
) {
    bundled(name, overrides).run(file, sides).unwrap();
}

/// Run the bundled plugin `name` with `overrides` on trees built by hand,
/// and carry out its moves.
pub(crate) fn run_trees(
    name: &str,
    overrides: serde_json::Value,
    file: &FileChange,
    sides: &mut tree::Pairing<tree::Source>,
) {
    let mut wired = wire(sides.clone());
    run(name, overrides, file, &mut wired);
    *sides = trees(&wired);
}

/// The moves the only plugin of `pipeline` asks for, not carried out.
pub(crate) fn moves(
    pipeline: &Pipeline,
    file: &FileChange,
    sides: &Pairing<protocol::Source>,
) -> anyhow::Result<Vec<Move>> {
    let [plugin] = &pipeline.plugins[..] else {
        panic!("one plugin");
    };
    let records = super::source_sides(&trees(sides));
    plugin
        .runner
        .mutate(pipeline.host(&plugin.name), &file_entry(file), &records)
}

/// For each `deleted-bodies:function` body on the after side, the first line
/// of the body and the lines of its docstring, as `docstring_of` finds it.
fn documented(path: &str, after: &str) -> Vec<(u32, Option<(u32, u32)>)> {
    let (_, sides) = project(path, "", after);
    let sides = trees(&sides);
    let source = rhs(&sides);
    let mut bodies = Vec::new();
    walk(&source.regions, &mut |region| {
        if is_fold(region) && has_tag(region, "deleted-bodies:function") {
            let docstring = docstring_of(source, region, "deleted-bodies").map(|id| {
                let mut lines = None;
                walk(&source.regions, &mut |docstring| {
                    if docstring.id == id {
                        let range = docstring.range.lines();
                        lines = Some((range.start, range.end));
                    }
                });
                lines.expect("the docstring is on this side")
            });
            bodies.push((region.range.start.line, docstring));
        }
    });
    bodies
}

#[test]
fn rust_doc_and_line_comments_above_a_function_are_its_docstring() {
    let after = "fn keep() -> u32 {\n    let x = 1;\n    x\n}\n\n/// Adds one.\n/// Twice, really.\nfn add(\n    a: u32,\n) -> u32 {\n    let b = a;\n    b + 2\n}\n\n// Plain comment.\n// Two lines.\n#[inline]\nfn sub(a: u32) -> u32 {\n    let b = a;\n    b - 1\n}\n\n// One line.\nfn one(a: u32) -> u32 {\n    let b = a;\n    b - 1\n}\n\n/**\n * Block.\n */\nfn block() {\n    x();\n    y();\n}\n";
    assert_eq!(
        documented("a.rs", after),
        [
            (1, None),
            (10, Some((5, 7))),
            (18, Some((14, 16))),
            (24, None),
            (32, Some((28, 31)))
        ],
        "a one-line docstring is not a region"
    );
}

#[test]
fn a_comment_separated_from_the_function_by_code_does_not_count() {
    let after =
        "// About the constant.\n// Really.\nconst X: u32 = 1;\nfn f() -> u32 {\n    let y = X;\n    y\n}\n";
    assert_eq!(documented("a.rs", after), [(4, None)]);
}

#[test]
fn a_docstring_is_not_found_past_a_one_line_function() {
    let after = "/// First.\n/// Documented.\nfn a() {}\nfn b() {\n    x();\n    y();\n}\n";
    assert_eq!(documented("a.rs", after), [(4, None)]);
    let after =
        "// First.\n// Documented.\nexport const a = 1;\nfunction b() {\n  x();\n  y();\n}\n";
    assert_eq!(documented("a.ts", after), [(4, None)]);
}

#[test]
fn a_python_string_first_in_the_body_is_its_docstring() {
    let after =
        "def f(a):\n    \"\"\"Double a.\n\n    Returns an int.\n    \"\"\"\n    return a * 2\n";
    assert_eq!(documented("a.py", after), [(1, Some((1, 5)))]);
}

#[test]
fn go_and_javascript_comment_runs_document_functions() {
    for (path, source, expected) in [
        (
            "a.go",
            "package a\n\n// Sum adds.\n// Twice.\nfunc Sum(a int) int {\n\tb := a\n\treturn a + b\n}\n",
            (5, Some((2, 4))),
        ),
        (
            "a.ts",
            "// Sum adds.\n// Twice.\nexport function sum(a: number) {\n  const b = a;\n  return a + b;\n}\n",
            (3, Some((0, 2))),
        ),
        (
            "a.js",
            "/**\n * Sum adds.\n */\nconst sum = (a) => {\n  const b = a;\n  return a + b;\n};\n",
            (4, Some((0, 3))),
        ),
    ] {
        assert_eq!(documented(path, source), [expected], "{path}");
    }
}

#[test]
fn the_default_pipeline_makes_every_plugin_that_is_on() {
    let pipeline = Pipeline::from_config(&PluginsConfig::default(), Path::new(".")).unwrap();
    let made: Vec<&str> = pipeline
        .plugins
        .iter()
        .map(|plugin| &*plugin.name)
        .collect();
    assert_eq!(
        made,
        [
            "context",
            "hide-files",
            "deleted-bodies",
            "test-bodies",
            "removed-runs",
            "group"
        ],
        "the summarizer is off until turned on"
    );
}

/// Test plugins without options.
#[derive(serde::Deserialize)]
#[serde(deny_unknown_fields)]
struct NoOptions {}

/// A plugin asking for a move that cannot be carried out.
struct Bad;

impl Plugin for Bad {
    type Options = NoOptions;

    fn new(_: NoOptions) -> anyhow::Result<Self> {
        Ok(Self)
    }

    fn classify(&self, _: &FileEntry) -> anyhow::Result<Vec<String>> {
        Ok(Vec::new())
    }

    fn mutate(&self, _: &FileEntry, _: &tree::Pairing<tree::Source>) -> anyhow::Result<Vec<Move>> {
        Ok(vec![Move::SetCollapsed((99_999, true))])
    }
}

/// Plugins adding tags: `a` and `z`; then `b`, checking it sees the tags
/// before it; then one that is not a tag.
struct TagsAZ;
struct TagsB;
struct NotATag;

impl Plugin for TagsAZ {
    type Options = NoOptions;

    fn new(_: NoOptions) -> anyhow::Result<Self> {
        Ok(Self)
    }

    fn classify(&self, _: &FileEntry) -> anyhow::Result<Vec<String>> {
        Ok(vec!["a".to_owned(), "z".to_owned()])
    }

    fn mutate(&self, _: &FileEntry, _: &tree::Pairing<tree::Source>) -> anyhow::Result<Vec<Move>> {
        Ok(Vec::new())
    }
}

impl Plugin for TagsB {
    type Options = NoOptions;

    fn new(_: NoOptions) -> anyhow::Result<Self> {
        Ok(Self)
    }

    fn classify(&self, file: &FileEntry) -> anyhow::Result<Vec<String>> {
        assert_eq!(file.tags, ["a", "z"], "a plugin sees the tags before it");
        Ok(vec!["b".to_owned()])
    }

    fn mutate(&self, _: &FileEntry, _: &tree::Pairing<tree::Source>) -> anyhow::Result<Vec<Move>> {
        Ok(Vec::new())
    }
}

impl Plugin for NotATag {
    type Options = NoOptions;

    fn new(_: NoOptions) -> anyhow::Result<Self> {
        Ok(Self)
    }

    fn classify(&self, _: &FileEntry) -> anyhow::Result<Vec<String>> {
        Ok(vec!["Not A Tag".to_owned()])
    }

    fn mutate(&self, _: &FileEntry, _: &tree::Pairing<tree::Source>) -> anyhow::Result<Vec<Move>> {
        Ok(Vec::new())
    }
}

/// A pipeline of test plugins, each made with no options.
fn pipeline(plugins: Vec<(&str, native::Constructor)>) -> Pipeline {
    let mut pipeline = Pipeline::default();
    for (name, create) in plugins {
        pipeline.push(name, json!({}), &create).unwrap();
    }
    pipeline
}

#[test]
fn classifying_plugins_add_tags_in_order_and_a_bad_tag_is_an_error() {
    let (mut file, _) = project("a.rs", "", "");
    file.tags = vec!["z".to_owned()];
    let tagging = pipeline(vec![
        ("first", native::native::<TagsAZ>),
        ("second", native::native::<TagsB>),
    ]);
    assert_eq!(tagging.classify(&file).unwrap(), ["a", "b", "z"]);
    let bad = pipeline(vec![("bad", native::native::<NotATag>)]);
    assert_eq!(
        format!("{:#}", bad.classify(&file).unwrap_err()),
        "plugin bad: classify a.rs: \"Not A Tag\" is not a tag; use lowercase letters, digits, '-' and '_'"
    );
}

#[test]
fn a_move_that_cannot_be_carried_out_fails_naming_the_plugin() {
    let (file, mut sides) = project("a.rs", "fn a() {}\n", "fn b() {}\n");
    let bad = pipeline(vec![("bad", native::native::<Bad>)]);
    let error = bad.run(&file, &mut sides).unwrap_err();
    assert!(error.downcast_ref::<MutationFailed>().is_some());
    assert_eq!(format!("{error:#}"), "mutation bad: no region 99999");
}

#[test]
fn a_plugin_that_cannot_be_made_is_a_setup_error() {
    let config =
        Config::from_toml("[plugins.bundled.summarize]\nenabled = true\napi_key = ''\n").unwrap();
    // Only meaningful when the environment carries no key.
    if std::env::var_os("GEMINI_API_KEY").is_some() || std::env::var_os("GOOGLE_API_KEY").is_some()
    {
        return;
    }
    let error = Pipeline::from_config(&config.plugins, Path::new("."))
        .err()
        .expect("a summarizer without a key cannot be made");
    assert_eq!(
        format!("{error:#}"),
        "plugins.bundled.summarize: no API key: set plugins.bundled.summarize.api_key, or GEMINI_API_KEY or GOOGLE_API_KEY in the environment, or turn the summarizer off with plugins.bundled.summarize.enabled = false"
    );
}

#[test]
fn options_that_do_not_deserialize_are_a_setup_error() {
    let mut pipeline = Pipeline::default();
    let error = pipeline
        .push("bad", json!({"extra": 1}), &native::native::<Bad>)
        .unwrap_err();
    assert_eq!(
        format!("{error:#}"),
        "plugins.bad: invalid options: unknown field `extra`, there are no fields at line 1 column 8"
    );
}

#[test]
fn external_plugins_never_fall_back_to_a_native_registration() {
    let dir = tempfile::tempdir().unwrap();
    std::fs::write(
        dir.path().join("plugin.toml"),
        "name = 'context'\ntitle = 'External context'\n",
    )
    .unwrap();
    let config = Config::from_toml_in(
        "[plugins]\norder = ['external.context']\n[plugins.external.context]\npath = '.'\n",
        dir.path(),
    )
    .unwrap();
    let error = Pipeline::from_config(&config.plugins, dir.path())
        .err()
        .unwrap();
    let error = format!("{error:#}");
    assert!(error.contains("plugin.wasm"), "{error}");
    assert!(error.contains("plugins.external.context"), "{error}");
}

#[test]
fn a_subset_of_bundled_plugins_can_use_shared_query_tags() {
    Config::from_toml("[plugins]\norder = ['bundled.deleted-bodies']\n")
        .unwrap()
        .compile()
        .unwrap();
}

mod deferred;