biov 0.1.9

A uv-style tool manager for bioinformatics: reproducible Docker-backed tools with digest-pinned lockfiles (installs as `bv`)
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
/// Integration tests for the `bv` CLI.
///
/// Tests marked `requires Docker daemon` run in CI via:
///   cargo test --test integration -- --include-ignored --skip alphafold
///
/// Tests marked `requires GPU` or `requires large download` are excluded from
/// CI and must be run manually on the appropriate hardware.
///
/// Run everything locally:
///   cargo test --test integration -- --include-ignored
///
/// On a remote server without a local registry checkout:
///   BV_REGISTRY=https://github.com/mlberkeley/bv-registry \
///     cargo test --test integration -- --include-ignored --skip alphafold
use std::env;
use std::path::{Path, PathBuf};
use std::process::Command;

/// Path to the compiled `bv` binary.
fn bv_bin() -> PathBuf {
    env::current_exe()
        .unwrap()
        .parent()
        .unwrap()
        .parent()
        .unwrap()
        .join("bv")
}

/// Registry URL: honours BV_REGISTRY env var so CI/remote boxes work without
/// a local checkout. Falls back to the sibling `bv-registry/` directory for
/// local dev.
fn registry_url() -> String {
    if let Ok(url) = std::env::var("BV_REGISTRY") {
        return url;
    }
    let manifest_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
    manifest_dir
        .parent()
        .unwrap()
        .parent()
        .unwrap()
        .join("bv-registry")
        .to_string_lossy()
        .to_string()
}

/// Build a `Command` for `bv` with isolated project and cache directories.
fn bv(args: &[&str], project_dir: &Path, cache_dir: &Path) -> Command {
    let mut cmd = Command::new(bv_bin());
    cmd.args(args)
        .current_dir(project_dir)
        .env("BV_CACHE_DIR", cache_dir)
        .env_remove("BV_BACKEND");
    cmd
}

#[allow(dead_code)]
fn bv_backend(args: &[&str], project_dir: &Path, cache_dir: &Path, backend: &str) -> Command {
    let mut cmd = bv(args, project_dir, cache_dir);
    cmd.env("BV_BACKEND", backend);
    cmd
}

#[test]
#[ignore = "requires Docker daemon and network access"]
fn add_blast_creates_toml_and_lock() {
    let dir = tempfile::tempdir().expect("project dir");
    let cache = tempfile::tempdir().expect("cache dir");
    let registry = registry_url();

    let status = bv(
        &[
            "add",
            "blast@2.14.0",
            "--ignore-hardware",
            "--registry",
            &registry,
        ],
        dir.path(),
        cache.path(),
    )
    .status()
    .expect("bv add failed to launch");
    assert!(status.success(), "bv add exited non-zero: {status}");

    let bv_toml = std::fs::read_to_string(dir.path().join("bv.toml")).expect("bv.toml missing");
    assert!(bv_toml.contains("blast"), "bv.toml doesn't mention blast");

    let bv_lock = std::fs::read_to_string(dir.path().join("bv.lock")).expect("bv.lock missing");
    assert!(bv_lock.contains("blast"), "bv.lock doesn't mention blast");
    assert!(bv_lock.contains("2.14.0"), "bv.lock missing version");
    assert!(bv_lock.contains("sha256:"), "bv.lock missing digest");
    assert!(
        bv_lock.contains("manifest_sha256"),
        "bv.lock missing manifest_sha256"
    );
}

#[test]
#[ignore = "requires Docker daemon and network access"]
fn add_is_idempotent() {
    let dir = tempfile::tempdir().expect("project dir");
    let cache = tempfile::tempdir().expect("cache dir");
    let registry = registry_url();

    let s1 = bv(
        &[
            "add",
            "blast@2.14.0",
            "--ignore-hardware",
            "--registry",
            &registry,
        ],
        dir.path(),
        cache.path(),
    )
    .status()
    .expect("first add failed to launch");
    assert!(s1.success());

    let s2 = bv(
        &[
            "add",
            "blast@2.14.0",
            "--ignore-hardware",
            "--registry",
            &registry,
        ],
        dir.path(),
        cache.path(),
    )
    .status()
    .expect("second add failed to launch");
    assert!(s2.success(), "idempotent re-add failed: {s2}");
}

#[test]
#[ignore = "requires Docker daemon and network access"]
fn run_blast_version() {
    let dir = tempfile::tempdir().expect("project dir");
    let cache = tempfile::tempdir().expect("cache dir");
    let registry = registry_url();

    let add_ok = bv(
        &[
            "add",
            "blast@2.14.0",
            "--ignore-hardware",
            "--registry",
            &registry,
        ],
        dir.path(),
        cache.path(),
    )
    .status()
    .expect("bv add failed to launch");
    assert!(add_ok.success(), "bv add failed: {add_ok}");

    let run_ok = bv(
        &["run", "blast", "--", "blastn", "-version"],
        dir.path(),
        cache.path(),
    )
    .status()
    .expect("bv run failed to launch");
    assert_eq!(run_ok.code(), Some(0), "bv run exited non-zero: {run_ok}");
}

#[test]
#[ignore = "requires Docker daemon and network access"]
fn unknown_tool_gives_clear_error() {
    let dir = tempfile::tempdir().expect("project dir");
    let cache = tempfile::tempdir().expect("cache dir");
    let registry = registry_url();

    let output = bv(
        &["add", "fakename", "--registry", &registry],
        dir.path(),
        cache.path(),
    )
    .output()
    .expect("bv add failed to launch");

    assert!(
        !output.status.success(),
        "expected non-zero exit for unknown tool"
    );
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("not found") || stderr.contains("fakename"),
        "expected informative error, got: {stderr}"
    );
}

#[test]
#[ignore = "requires Docker daemon and network access"]
fn run_without_add_gives_clear_error() {
    let dir = tempfile::tempdir().expect("project dir");
    let cache = tempfile::tempdir().expect("cache dir");

    let output = bv(
        &["run", "blast", "--", "blastn", "-version"],
        dir.path(),
        cache.path(),
    )
    .output()
    .expect("bv run failed to launch");

    assert!(!output.status.success());
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("bv add") || stderr.contains("not in this project"),
        "expected helpful error, got: {stderr}"
    );
}

#[test]
#[ignore = "requires Docker daemon and network access"]
fn sync_reproduces_environment() {
    let dir_a = tempfile::tempdir().expect("project dir A");
    let dir_b = tempfile::tempdir().expect("project dir B");
    let cache_a = tempfile::tempdir().expect("cache A");
    let cache_b = tempfile::tempdir().expect("cache B");
    let registry = registry_url();

    let add_ok = bv(
        &[
            "add",
            "blast@2.14.0",
            "hmmer",
            "--ignore-hardware",
            "--registry",
            &registry,
        ],
        dir_a.path(),
        cache_a.path(),
    )
    .status()
    .expect("bv add failed to launch");
    assert!(add_ok.success(), "bv add in dir_a failed");

    std::fs::copy(dir_a.path().join("bv.toml"), dir_b.path().join("bv.toml")).unwrap();
    std::fs::copy(dir_a.path().join("bv.lock"), dir_b.path().join("bv.lock")).unwrap();

    let sync_ok = bv(&["sync"], dir_b.path(), cache_b.path())
        .status()
        .expect("bv sync failed to launch");
    assert!(sync_ok.success(), "bv sync in dir_b failed");

    let check_ok = bv(
        &["lock", "--check", "--registry", &registry],
        dir_b.path(),
        cache_b.path(),
    )
    .status()
    .expect("bv lock --check failed to launch");
    assert!(
        check_ok.success(),
        "bv lock --check failed (lockfile drifted)"
    );
}

#[test]
#[ignore = "requires Docker daemon and network access"]
fn lock_check_detects_out_of_date() {
    let dir = tempfile::tempdir().expect("project dir");
    let cache = tempfile::tempdir().expect("cache dir");
    let registry = registry_url();

    std::fs::write(
        dir.path().join("bv.toml"),
        format!(
            "[project]\nname = \"test\"\n\n[registry]\nurl = \"{registry}\"\n\n[[tools]]\nid = \"blast\"\n"
        ),
    )
    .unwrap();

    let status = bv(
        &["lock", "--check", "--registry", &registry],
        dir.path(),
        cache.path(),
    )
    .status()
    .expect("bv lock --check failed to launch");
    assert!(
        !status.success(),
        "expected non-zero exit when bv.lock is missing"
    );
}

#[test]
#[ignore = "requires Docker daemon and network access"]
fn remove_updates_both_files() {
    let dir = tempfile::tempdir().expect("project dir");
    let cache = tempfile::tempdir().expect("cache dir");
    let registry = registry_url();

    let add_ok = bv(
        &[
            "add",
            "blast@2.14.0",
            "--ignore-hardware",
            "--registry",
            &registry,
        ],
        dir.path(),
        cache.path(),
    )
    .status()
    .expect("bv add failed to launch");
    assert!(add_ok.success());

    let rm_ok = bv(&["remove", "blast"], dir.path(), cache.path())
        .status()
        .expect("bv remove failed to launch");
    assert!(rm_ok.success(), "bv remove failed");

    let toml = std::fs::read_to_string(dir.path().join("bv.toml")).unwrap();
    let lock = std::fs::read_to_string(dir.path().join("bv.lock")).unwrap();
    assert!(
        !toml.contains("blast"),
        "bv.toml still mentions blast after remove"
    );
    assert!(
        !lock.contains("blast"),
        "bv.lock still mentions blast after remove"
    );
}

#[test]
#[ignore = "requires Docker daemon and network access"]
fn sync_frozen_fails_on_mismatch() {
    let dir = tempfile::tempdir().expect("project dir");
    let cache = tempfile::tempdir().expect("cache dir");
    let registry = registry_url();

    let add_ok = bv(
        &[
            "add",
            "blast@2.14.0",
            "--ignore-hardware",
            "--registry",
            &registry,
        ],
        dir.path(),
        cache.path(),
    )
    .status()
    .expect("bv add failed to launch");
    assert!(add_ok.success());

    let toml_path = dir.path().join("bv.toml");
    let mut content = std::fs::read_to_string(&toml_path).unwrap();
    content.push_str("\n[[tools]]\nid = \"hmmer\"\n");
    std::fs::write(&toml_path, content).unwrap();

    let status = bv(&["sync", "--frozen"], dir.path(), cache.path())
        .status()
        .expect("bv sync failed to launch");
    assert!(
        !status.success(),
        "expected --frozen to fail when bv.toml has hmmer but bv.lock does not"
    );
}

/// Verify that `bv run` fails with a clear `bv data fetch` hint when required
/// reference data is absent. This test synthesizes bv.lock and a cached manifest
/// directly so it does not need Docker or network access.
#[test]
#[ignore = "requires Docker daemon (bv run invokes docker)"]
fn run_tool_with_missing_required_data_fails_with_hint() {
    let dir = tempfile::tempdir().expect("project dir");
    let cache = tempfile::tempdir().expect("cache dir");

    // Write bv.toml
    std::fs::write(
        dir.path().join("bv.toml"),
        "[project]\nname = \"test\"\n\n[[tools]]\nid = \"fake-ref-tool\"\n",
    )
    .unwrap();

    // Write bv.lock with a fake entry for fake-ref-tool
    std::fs::write(
        dir.path().join("bv.lock"),
        r#"version = 1

[metadata]
bv_version = "0.1.0"
generated_at = "2024-01-15T10:00:00Z"

[tools.fake-ref-tool]
tool_id = "fake-ref-tool"
version = "1.0.0"
image_reference = "hello-world:latest"
image_digest = "sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
manifest_sha256 = ""
resolved_at = "2024-01-15T10:00:00Z"
"#,
    )
    .unwrap();

    // Write a cached manifest that declares required reference data
    let manifest_dir = cache
        .path()
        .join("tools")
        .join("fake-ref-tool")
        .join("1.0.0");
    std::fs::create_dir_all(&manifest_dir).unwrap();
    std::fs::write(
        manifest_dir.join("manifest.toml"),
        r#"[tool]
id = "fake-ref-tool"
version = "1.0.0"

[tool.image]
backend = "docker"
reference = "hello-world:latest"

[tool.hardware]

[tool.hardware.gpu]
required = false

[tool.reference_data.testdb]
id = "testdb"
version = "1.0"
required = true
mount_path = "/data/testdb"

[tool.entrypoint]
command = "echo"
"#,
    )
    .unwrap();

    // bv run should fail before touching Docker because testdb is not in cache
    let output = bv(
        &["run", "fake-ref-tool", "--", "echo", "hi"],
        dir.path(),
        cache.path(),
    )
    .output()
    .expect("bv run failed to launch");

    assert!(
        !output.status.success(),
        "expected non-zero exit when reference data is missing"
    );
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("bv data fetch"),
        "expected fetch hint in error, got:\n{stderr}"
    );
}

/// Requires pulling the 8 GB alphafold image; only run on a GPU machine with
/// the image already cached locally to avoid timeouts.
#[test]
#[ignore = "requires GPU machine with alphafold image pre-pulled"]
fn alphafold_add_prints_reference_data_notice() {
    let dir = tempfile::tempdir().expect("project dir");
    let cache = tempfile::tempdir().expect("cache dir");
    let registry = registry_url();

    let output = bv(
        &[
            "add",
            "alphafold@2.3.2",
            "--ignore-hardware",
            "--registry",
            &registry,
        ],
        dir.path(),
        cache.path(),
    )
    .output()
    .expect("bv add failed to launch");

    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("requires the following reference datasets"),
        "expected reference data notice, got:\n{stderr}"
    );
    assert!(
        stderr.contains("bv data fetch"),
        "expected fetch hint in notice, got:\n{stderr}"
    );
}

#[test]
#[ignore = "requires network access to NCBI FTP (large file)"]
fn data_fetch_downloads_and_verifies() {
    let dir = tempfile::tempdir().expect("project dir");
    let cache = tempfile::tempdir().expect("cache dir");
    let registry = registry_url();

    std::fs::write(
        dir.path().join("bv.toml"),
        format!("[project]\nname = \"test\"\n\n[registry]\nurl = \"{registry}\"\n"),
    )
    .unwrap();

    let status = bv(
        &["data", "fetch", "pdbaa", "--yes", "--registry", &registry],
        dir.path(),
        cache.path(),
    )
    .status()
    .expect("bv data fetch failed to launch");

    // Replace with assert!(status.success()) once sha256 in the registry manifest is updated.
    let _ = status;
}