check-implementation 0.1.1

Workspace check contract implementation
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
#![cfg(unix)]

use boxology_contract::{
    BoxId, CallContext, CallError, Caller, CancelToken, CapabilityId, ErasedCallError,
    ErasedCallTarget, ExposureLevel, SlotValue, TraceContext,
};
use boxology_generated_contract::{
    CheckError, CheckFailureKind, CheckFinding, CheckHandle, CheckRequest, CheckStatus,
    CheckStepStatus,
};
use boxology_import_classifier::{
    ClassifyFailure, ClassifyFailureStage, ClassifyFinding, ClassifyOutcome, ClassifyReport,
    CompatibilityClass, test_support::ClassifierFake,
};
use boxology_runtime::{
    Composition, CompositionBuilder, ImportTarget, RemoteImportTarget, TransportExposure,
    test_support::StubTransport,
};
use boxology_workspace::WorkspaceInputs;
use check_implementation::{CheckService, generated};
use std::{
    env,
    ffi::OsString,
    fs,
    future::Future,
    os::unix::fs::PermissionsExt,
    path::{Path, PathBuf},
    pin::{Pin, pin},
    sync::{
        Arc, Mutex, OnceLock,
        atomic::{AtomicU64, AtomicUsize, Ordering},
    },
    task::{Context, Poll, Waker},
};

const ROOT_MANIFEST: &str = "schema = 1\nid = \"platform\"\nkind = \"platform\"\nowned = [\"Cargo.toml\", \"boxology.toml\"]\n\n[[derived]]\nid = \"lockfile\"\ngenerator = \"cargo\"\ninputs = [\"Cargo.toml\"]\noutputs = [\"Cargo.lock\"]\n";
const METADATA: &str = r#"{"workspace_root":"/w","workspace_members":["path+file:///w/ping/generated/contract#0.0.0","path+file:///w/ping/implementation#0.0.0"],"packages":[{"id":"path+file:///w/ping/generated/contract#0.0.0","name":"ping-contract","manifest_path":"/w/ping/generated/contract/Cargo.toml","dependencies":[]},{"id":"path+file:///w/ping/implementation#0.0.0","name":"ping-implementation","manifest_path":"/w/ping/implementation/Cargo.toml","dependencies":[]}]}"#;
const CLEAN_JSON: &[u8] = b"{\n  \"schema\": \"boxology.check-report@1\",\n  \"steps\": [\n    {\n      \"id\": \"discovery\",\n      \"status\": \"passed\",\n      \"findings\": []\n    },\n    {\n      \"id\": \"regeneration\",\n      \"status\": \"passed\",\n      \"findings\": []\n    },\n    {\n      \"id\": \"contract-classification\",\n      \"status\": \"skipped\",\n      \"reason\": \"contract classification skipped: no repository is available\",\n      \"findings\": []\n    },\n    {\n      \"id\": \"diff-ownership\",\n      \"status\": \"skipped\",\n      \"reason\": \"not run: no repository is available\",\n      \"findings\": []\n    },\n    {\n      \"id\": \"cargo-graph\",\n      \"status\": \"passed\",\n      \"findings\": [],\n      \"output\": null\n    },\n    {\n      \"id\": \"fmt\",\n      \"status\": \"passed\",\n      \"findings\": [],\n      \"output\": null\n    },\n    {\n      \"id\": \"clippy\",\n      \"status\": \"passed\",\n      \"findings\": [],\n      \"output\": null\n    },\n    {\n      \"id\": \"tests\",\n      \"status\": \"passed\",\n      \"findings\": [],\n      \"output\": null\n    },\n    {\n      \"id\": \"quality\",\n      \"status\": \"passed\",\n      \"findings\": [],\n      \"output\": null\n    }\n  ],\n  \"result\": \"passed\"\n}\n";
const CLEAN_HUMAN: &[u8] = b"check discovery passed\ncheck regeneration passed\ncheck contract-classification skipped\n  contract classification skipped: no repository is available\ncheck diff-ownership skipped\n  not run: no repository is available\ncheck cargo-graph passed\ncheck fmt passed\ncheck clippy passed\ncheck tests passed\ncheck quality passed\ncheck result passed\n";
const OID: &str = "0000000000000000000000000000000000000000";
static NEXT: AtomicU64 = AtomicU64::new(0);

fn copy_tree(source: &Path, destination: &Path) {
    fs::create_dir_all(destination).unwrap();
    for entry in fs::read_dir(source).unwrap() {
        let entry = entry.unwrap();
        let target = destination.join(entry.file_name());
        if entry.file_type().unwrap().is_dir() {
            copy_tree(&entry.path(), &target);
        } else {
            fs::copy(entry.path(), target).unwrap();
        }
    }
}

struct Fixture {
    home: PathBuf,
    root: PathBuf,
    bin: PathBuf,
    metadata: PathBuf,
    base: PathBuf,
}

impl Fixture {
    fn new() -> Self {
        let home = env::temp_dir().join(format!(
            "boxology-check-handle-{}-{}",
            std::process::id(),
            NEXT.fetch_add(1, Ordering::Relaxed)
        ));
        let root = home.join("workspace");
        let bin = home.join("bin");
        fs::create_dir(&home).unwrap();
        fs::create_dir(&bin).unwrap();
        let source = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../fixtures/ping");
        fs::create_dir_all(root.join("ping")).unwrap();
        fs::copy(
            source.join("boxology.toml"),
            root.join("ping/boxology.toml"),
        )
        .unwrap();
        copy_tree(
            &source.join("implementation"),
            &root.join("ping/implementation"),
        );
        copy_tree(&source.join("generated"), &root.join("ping/generated"));
        fs::write(
            root.join("Cargo.toml"),
            "[workspace]\nmembers = [\"ping/implementation\", \"ping/generated/contract\"]\nresolver = \"3\"\n",
        )
        .unwrap();
        fs::write(root.join("Cargo.lock"), b"").unwrap();
        fs::write(root.join("boxology.toml"), ROOT_MANIFEST).unwrap();
        let metadata = home.join("metadata.json");
        let base = home.join("base.json");
        fs::write(&metadata, METADATA).unwrap();
        fs::write(&base, b"base-schema-exact").unwrap();
        let cargo = bin.join("cargo");
        fs::write(
            &cargo,
            "#!/bin/sh\nif [ \"$1\" = metadata ]; then\n  if [ \"${BOXOLOGY_TEST_METADATA_FAIL:-}\" = 1 ]; then printf '%s\\n' 'synthetic metadata stderr' >&2; exit 17; fi\n  /bin/cat \"$BOXOLOGY_TEST_METADATA\"; exit 0\nfi\nexit 0\n",
        )
        .unwrap();
        fs::set_permissions(&cargo, fs::Permissions::from_mode(0o755)).unwrap();
        let fixture = Self {
            home,
            root,
            bin,
            metadata,
            base,
        };
        fixture.regenerate();
        fixture
    }

    fn regenerate(&self) {
        let walked = boxology_cli_core::walk(&self.root).unwrap();
        let inputs = WorkspaceInputs::new(
            walked.files().to_vec(),
            walked.manifests().to_vec(),
            METADATA,
        )
        .unwrap();
        let workspace = inputs.check().unwrap();
        for plan in boxology_cli_core::plan(&workspace, None).unwrap() {
            boxology_cli_core::execute(&self.root, &plan).unwrap();
        }
    }

    fn install_git(&self) {
        let git = self.bin.join("git");
        fs::write(
            &git,
            "#!/bin/sh\ncase \"$1 $2\" in\n 'rev-parse --verify') printf '%s\\n' \"$BOXOLOGY_TEST_OID\";;\n 'ls-tree --name-only') printf '%s\\0' \"$6\";;\n 'cat-file -e') exit 0;;\n 'cat-file blob') /bin/cat \"$BOXOLOGY_TEST_BASE\";;\n 'ls-tree -r'|'diff --name-only') exit 0;;\n *) exit 19;;\nesac\n",
        )
        .unwrap();
        fs::set_permissions(&git, fs::Permissions::from_mode(0o755)).unwrap();
    }

    fn environment(&self, metadata_failure: bool) -> Environment {
        Environment::set([
            (
                "PATH",
                env::join_paths([self.bin.as_path(), Path::new("/usr/bin"), Path::new("/bin")])
                    .unwrap(),
            ),
            (
                "GIT_CEILING_DIRECTORIES",
                self.root.clone().into_os_string(),
            ),
            (
                "BOXOLOGY_TEST_METADATA",
                self.metadata.clone().into_os_string(),
            ),
            ("BOXOLOGY_TEST_BASE", self.base.clone().into_os_string()),
            ("BOXOLOGY_TEST_OID", OsString::from(OID)),
            (
                "BOXOLOGY_TEST_METADATA_FAIL",
                OsString::from(if metadata_failure { "1" } else { "0" }),
            ),
        ])
    }
}

impl Drop for Fixture {
    fn drop(&mut self) {
        fs::remove_dir_all(&self.home).unwrap();
    }
}

struct Environment {
    previous: Vec<(&'static str, Option<OsString>)>,
    _guard: std::sync::MutexGuard<'static, ()>,
}

impl Environment {
    fn set<const N: usize>(values: [(&'static str, OsString); N]) -> Self {
        static LOCK: OnceLock<Mutex<()>> = OnceLock::new();
        let guard = LOCK
            .get_or_init(|| Mutex::new(()))
            .lock()
            .unwrap_or_else(|e| e.into_inner());
        let previous = values
            .iter()
            .map(|(key, _)| (*key, env::var_os(key)))
            .collect();
        for (key, value) in values {
            unsafe { env::set_var(key, value) };
        }
        Self {
            previous,
            _guard: guard,
        }
    }
}

impl Drop for Environment {
    fn drop(&mut self) {
        for (key, value) in self.previous.drain(..) {
            unsafe {
                match value {
                    Some(value) => env::set_var(key, value),
                    None => env::remove_var(key),
                }
            }
        }
    }
}

struct Remote<T> {
    target: T,
    capabilities: Vec<CapabilityId>,
}

impl<T: ErasedCallTarget + Send + Sync> ErasedCallTarget for Remote<T> {
    fn call<'a>(
        &'a self,
        capability: &'a CapabilityId,
        context: CallContext,
        input: SlotValue,
    ) -> Pin<Box<dyn Future<Output = Result<SlotValue, ErasedCallError>> + Send + 'a>> {
        self.target.call(capability, context, input)
    }
}

impl<T: ErasedCallTarget + Send + Sync> RemoteImportTarget for Remote<T> {
    fn supports_capability(&self, capability: &CapabilityId) -> bool {
        self.capabilities.contains(capability)
    }
}

struct Exposed(Vec<TransportExposure>);

impl ErasedCallTarget for Exposed {
    fn call<'a>(
        &'a self,
        capability: &'a CapabilityId,
        context: CallContext,
        input: SlotValue,
    ) -> Pin<Box<dyn Future<Output = Result<SlotValue, ErasedCallError>> + Send + 'a>> {
        self.0
            .iter()
            .find(|exposure| exposure.descriptor().id() == capability)
            .expect("check capability is exposed")
            .dispatch(context, input)
    }
}

fn assembled(fake: ClassifierFake) -> (Composition, CheckHandle) {
    let transport = Arc::new(StubTransport::new());
    let descriptor = generated::implementation_descriptor();
    let capability = descriptor.contract().capabilities()[0].id().clone();
    let classifier_capabilities = boxology_import_classifier::contract_descriptor()
        .capabilities()
        .iter()
        .map(|item| item.id().clone())
        .collect();
    let mut builder = CompositionBuilder::new();
    builder.add_box(descriptor, |imports| {
        let dependencies = generated::typed_imports(&imports);
        generated::factory(CheckService::new(dependencies.classifier), imports)
    });
    builder.resolve_import(
        BoxId::new("check").unwrap(),
        BoxId::new("classifier").unwrap(),
        ImportTarget::remote(Arc::new(Remote {
            target: fake,
            capabilities: classifier_capabilities,
        })),
    );
    builder.expose(
        BoxId::new("check").unwrap(),
        capability,
        transport.clone(),
        ExposureLevel::CodeOnly,
    );
    let composition = builder.start().unwrap();
    let handle = CheckHandle::from_erased(Arc::new(Exposed(
        transport.runtime().unwrap().exposures().to_vec(),
    )));
    (composition, handle)
}

fn context() -> CallContext {
    CallContext::new(
        Caller::Anonymous,
        None,
        CancelToken::new(),
        TraceContext::empty(),
        None,
    )
}

fn ready<F: Future>(future: F) -> F::Output {
    let mut future = pin!(future);
    match future
        .as_mut()
        .poll(&mut Context::from_waker(Waker::noop()))
    {
        Poll::Ready(value) => value,
        Poll::Pending => panic!("check unexpectedly pending"),
    }
}

fn call(
    handle: &CheckHandle,
    fixture: &Fixture,
    base: Option<&str>,
) -> Result<boxology_generated_contract::CheckOutcome, CallError<CheckError>> {
    ready(handle.check(
        context(),
        CheckRequest {
            workspace: fixture.root.to_string_lossy().into_owned(),
            base: base.map(str::to_owned),
        },
    ))
}

#[test]
fn clean_no_repository_crosses_real_handle_with_canonical_bytes() {
    let fixture = Fixture::new();
    let calls = Arc::new(AtomicUsize::new(0));
    let fake = ClassifierFake::new().with_classify({
        let calls = calls.clone();
        move |_, _| {
            calls.fetch_add(1, Ordering::SeqCst);
            async { unreachable!() }
        }
    });
    let (_composition, handle) = assembled(fake);
    let _environment = fixture.environment(false);
    let outcome = call(&handle, &fixture, None).unwrap();
    let report = outcome.report.unwrap();
    assert!(outcome.failure.is_none());
    assert_eq!(calls.load(Ordering::SeqCst), 0);
    assert_eq!(
        report.status,
        CheckStatus::Passed,
        "{}",
        String::from_utf8_lossy(&report.human)
    );
    assert_eq!(report.steps.len(), 9);
    assert_eq!(report.human, CLEAN_HUMAN);
    assert_eq!(report.json, CLEAN_JSON);
}

#[test]
fn typed_finding_preserves_exact_request_and_is_report_only() {
    let fixture = Fixture::new();
    fixture.install_git();
    let base = fs::read(&fixture.base).unwrap();
    let submitted = fs::read(fixture.root.join("ping/generated/schema.json")).unwrap();
    let fake = ClassifierFake::new().with_classify(move |_, request| {
        assert_eq!(request.base.as_deref(), Some(base.as_slice()));
        assert_eq!(request.submitted, submitted);
        async {
            Ok(ClassifyOutcome {
                report: Some(ClassifyReport {
                    verdict: CompatibilityClass::Incompatible,
                    findings: vec![ClassifyFinding {
                        code: "BXC9000".into(),
                        path: "ping.value".into(),
                        kind: "changed".into(),
                        class: CompatibilityClass::Incompatible,
                        base_excerpt: Some("old".into()),
                        submitted_excerpt: Some("new".into()),
                        condition: Some("migrate".into()),
                    }],
                    rendered_text: "ignored rendering".into(),
                }),
                failure: None,
            })
        }
    });
    let (_composition, handle) = assembled(fake);
    let _environment = fixture.environment(false);
    let outcome = call(&handle, &fixture, Some("main")).unwrap();
    let report = outcome.report.unwrap();
    assert!(outcome.failure.is_none());
    assert_eq!(
        report.status,
        CheckStatus::Passed,
        "{}",
        String::from_utf8_lossy(&report.human)
    );
    let step = report
        .steps
        .iter()
        .find(|step| step.id == "contract-classification")
        .unwrap();
    assert_eq!(step.status, CheckStepStatus::Passed);
    assert!(String::from_utf8_lossy(&report.human).contains(
        "check contract-classification passed\n  BXC9000 ping ping.value incompatible condition=\"migrate\"\n"
    ));
    assert_eq!(
        step.findings,
        vec![CheckFinding {
            kind: "classifier".into(),
            code: "BXC9000".into(),
            path: "ping.value".into(),
            package: Some("ping".into()),
            payload: None,
            rule: None,
            rule_source: None,
            span_start_line: None,
            span_start_column: None,
            span_end_line: None,
            span_end_column: None,
            offending: None,
            class: Some("incompatible".into()),
            condition: Some("migrate".into()),
        }]
    );
}

#[test]
fn typed_classifier_failure_stages_preserve_exact_validation_bytes() {
    for (stage, expected) in [
        (
            ClassifyFailureStage::Base,
            "BXW0080 ping base: the base-revision schema document must satisfy the strict format-1 reader: coded diagnostics\n",
        ),
        (
            ClassifyFailureStage::Submitted,
            "BXW0081 ping submitted: the checked-in schema document must satisfy the strict format-1 reader: coded diagnostics\n",
        ),
        (
            ClassifyFailureStage::Pairing,
            "BXW0082 ping pairing: the base-revision and checked-in schema documents must pair and satisfy classifier integrity: coded diagnostics\n",
        ),
    ] {
        let fixture = Fixture::new();
        fixture.install_git();
        let fake = ClassifierFake::new().with_classify(move |_, _| {
            let stage = stage.clone();
            async move {
                Ok(ClassifyOutcome {
                    report: None,
                    failure: Some(ClassifyFailure {
                        stage,
                        diagnostics: "coded diagnostics".into(),
                    }),
                })
            }
        });
        let (_composition, handle) = assembled(fake);
        let _environment = fixture.environment(false);
        let outcome = call(&handle, &fixture, Some("main")).unwrap();
        let failure = outcome.failure.unwrap();
        assert!(outcome.report.is_none());
        assert_eq!(failure.kind, CheckFailureKind::Validation);
        assert_eq!(failure.human, expected.as_bytes());
        assert_eq!(failure.json, expected.as_bytes());
    }
}

#[test]
fn malformed_classifier_outcome_becomes_domain_internal() {
    let fixture = Fixture::new();
    fixture.install_git();
    let fake = ClassifierFake::new().with_classify(|_, _| async {
        Ok(ClassifyOutcome {
            report: None,
            failure: None,
        })
    });
    let (_composition, handle) = assembled(fake);
    let _environment = fixture.environment(false);
    assert!(matches!(
        call(&handle, &fixture, Some("main")),
        Err(CallError::Domain(CheckError::Internal))
    ));
}

#[test]
fn metadata_invocation_failure_is_typed_without_classifier_call() {
    let fixture = Fixture::new();
    let calls = Arc::new(AtomicUsize::new(0));
    let fake = ClassifierFake::new().with_classify({
        let calls = calls.clone();
        move |_, _| {
            calls.fetch_add(1, Ordering::SeqCst);
            async { unreachable!() }
        }
    });
    let (_composition, handle) = assembled(fake);
    let _environment = fixture.environment(true);
    let outcome = call(&handle, &fixture, None).unwrap();
    let failure = outcome.failure.unwrap();
    assert!(outcome.report.is_none());
    assert_eq!(calls.load(Ordering::SeqCst), 0);
    assert_eq!(failure.kind, CheckFailureKind::Invocation);
    assert_eq!(failure.human, b"BXW0075 Cargo.toml: cargo metadata could not be executed or did not return valid workspace metadata\nsynthetic metadata stderr\n");
    assert_eq!(failure.json, failure.human);
}