cabinpkg 0.17.0

A package manager and build system for C/C++
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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
//! `cabin test` dev-dependency activation: `[dev-dependencies]` of
//! the selected packages become real graph edges (path, port, and
//! versioned sources), so `test` targets may link dev-only
//! packages.  Ordinary commands keep dev deps declaration-only,
//! and the activation never propagates to transitive deps.

use super::*;

/// Write the shared two-package fixture: `app` declares `harness`
/// under `[dev-dependencies]` via a path source, and only the
/// `app_test` target references it.  Returns the app manifest path.
fn write_app_with_dev_path_harness(root: &Path) -> PathBuf {
    assert_fs::fixture::ChildPath::new(root.join("harness/cabin.toml"))
        .write_str(
            r#"[package]
name = "harness"
version = "0.1.0"
cxx-standard = "c++17"

[target.harness]
type = "library"
sources = ["src/harness.cc"]
include-dirs = ["include"]
"#,
        )
        .unwrap();
    assert_fs::fixture::ChildPath::new(root.join("harness/include/harness.h"))
        .write_str("#pragma once\nint harness_answer();\n")
        .unwrap();
    assert_fs::fixture::ChildPath::new(root.join("harness/src/harness.cc"))
        .write_str("#include \"harness.h\"\nint harness_answer() { return 42; }\n")
        .unwrap();

    assert_fs::fixture::ChildPath::new(root.join("app/cabin.toml"))
        .write_str(
            r#"[package]
name = "app"
version = "0.1.0"
cxx-standard = "c++17"

[dev-dependencies]
harness = { path = "../harness" }

[target.applib]
type = "library"
sources = ["src/applib.cc"]
include-dirs = ["include"]

[target.app_test]
type = "test"
sources = ["tests/app_test.cc"]
deps = ["applib", "harness"]
"#,
        )
        .unwrap();
    assert_fs::fixture::ChildPath::new(root.join("app/include/applib.h"))
        .write_str("#pragma once\nint applib_answer();\n")
        .unwrap();
    assert_fs::fixture::ChildPath::new(root.join("app/src/applib.cc"))
        .write_str("#include \"applib.h\"\nint applib_answer() { return 42; }\n")
        .unwrap();
    assert_fs::fixture::ChildPath::new(root.join("app/tests/app_test.cc"))
        .write_str(
            "#include \"applib.h\"\n#include \"harness.h\"\nint main() { return applib_answer() == harness_answer() ? 0 : 1; }\n",
        )
        .unwrap();
    root.join("app/cabin.toml")
}

#[test]
fn test_links_dev_path_dependency_into_test_target() {
    require_cxx_build_tools();
    let dir = TempDir::new().unwrap();
    let manifest = write_app_with_dev_path_harness(dir.path());
    let assertion = cabin()
        .args(["test", "--manifest-path"])
        .arg(&manifest)
        .arg("--build-dir")
        .arg(dir.path().join("build"))
        .assert()
        .success();
    let stdout = String::from_utf8_lossy(&assertion.get_output().stdout);
    assert!(
        stdout.contains("test app:app_test ... ok"),
        "test executable linking the dev path dep should run: {stdout}"
    );
}

#[test]
fn build_keeps_dev_path_dependency_declaration_only() {
    require_cxx_build_tools();
    let dir = TempDir::new().unwrap();
    let manifest = write_app_with_dev_path_harness(dir.path());
    let build_dir = dir.path().join("build");
    cabin()
        .args(["build", "--manifest-path"])
        .arg(&manifest)
        .arg("--build-dir")
        .arg(&build_dir)
        .assert()
        .success();
    // The dev dep never enters the ordinary build: no compile,
    // archive, or link action mentions it.
    let ninja = fs::read_to_string(build_dir.join("dev/build.ninja")).unwrap();
    assert!(
        !ninja.contains("harness"),
        "dev path dep must stay out of the ordinary build graph: {ninja}"
    );
}

#[test]
fn build_tolerates_missing_dev_path_dependency_directory() {
    require_cxx_build_tools();
    let dir = TempDir::new().unwrap();
    let manifest = write_app_with_dev_path_harness(dir.path());
    fs::remove_dir_all(dir.path().join("harness")).unwrap();
    // Ordinary commands never materialize the dev edge, so the
    // missing directory cannot break them.
    cabin()
        .args(["build", "--manifest-path"])
        .arg(&manifest)
        .arg("--build-dir")
        .arg(dir.path().join("build"))
        .assert()
        .success();
}

#[test]
fn test_links_dev_path_dependency_into_c_test_target() {
    require_c_and_cxx_build_tools();
    let dir = TempDir::new().unwrap();
    assert_fs::fixture::ChildPath::new(dir.path().join("charness/cabin.toml"))
        .write_str(
            r#"[package]
name = "charness"
version = "0.1.0"
c-standard = "c11"

[target.charness]
type = "library"
sources = ["src/charness.c"]
include-dirs = ["include"]
"#,
        )
        .unwrap();
    assert_fs::fixture::ChildPath::new(dir.path().join("charness/include/charness.h"))
        .write_str("#pragma once\nint charness_answer(void);\n")
        .unwrap();
    assert_fs::fixture::ChildPath::new(dir.path().join("charness/src/charness.c"))
        .write_str("#include \"charness.h\"\nint charness_answer(void) { return 7; }\n")
        .unwrap();
    assert_fs::fixture::ChildPath::new(dir.path().join("capp/cabin.toml"))
        .write_str(
            r#"[package]
name = "capp"
version = "0.1.0"
c-standard = "c11"

[dev-dependencies]
charness = { path = "../charness" }

[target.capp_test]
type = "test"
sources = ["tests/capp_test.c"]
deps = ["charness"]
"#,
        )
        .unwrap();
    assert_fs::fixture::ChildPath::new(dir.path().join("capp/tests/capp_test.c"))
        .write_str(
            "#include \"charness.h\"\nint main(void) { return charness_answer() == 7 ? 0 : 1; }\n",
        )
        .unwrap();
    let assertion = cabin()
        .args(["test", "--manifest-path"])
        .arg(dir.path().join("capp/cabin.toml"))
        .arg("--build-dir")
        .arg(dir.path().join("build"))
        .assert()
        .success();
    let stdout = String::from_utf8_lossy(&assertion.get_output().stdout);
    assert!(
        stdout.contains("test capp:capp_test ... ok"),
        "C test executable linking the dev path dep should run: {stdout}"
    );
}

#[test]
fn dev_activation_does_not_propagate_to_transitive_path_deps() {
    require_cxx_build_tools();
    let dir = TempDir::new().unwrap();
    // `lib` declares a dev path dep whose directory does not
    // exist.  Because activation is scoped to the *selected*
    // packages, testing `app` must not try to materialize it.
    assert_fs::fixture::ChildPath::new(dir.path().join("lib/cabin.toml"))
        .write_str(
            r#"[package]
name = "lib"
version = "0.1.0"
cxx-standard = "c++17"

[dev-dependencies]
ghost = { path = "../ghost-does-not-exist" }

[target.lib]
type = "library"
sources = ["src/lib.cc"]
include-dirs = ["include"]
"#,
        )
        .unwrap();
    assert_fs::fixture::ChildPath::new(dir.path().join("lib/include/lib.h"))
        .write_str("#pragma once\nint lib_answer();\n")
        .unwrap();
    assert_fs::fixture::ChildPath::new(dir.path().join("lib/src/lib.cc"))
        .write_str("#include \"lib.h\"\nint lib_answer() { return 3; }\n")
        .unwrap();
    assert_fs::fixture::ChildPath::new(dir.path().join("app/cabin.toml"))
        .write_str(
            r#"[package]
name = "app"
version = "0.1.0"
cxx-standard = "c++17"

[dependencies]
lib = { path = "../lib" }

[target.app_test]
type = "test"
sources = ["tests/app_test.cc"]
deps = ["lib"]
"#,
        )
        .unwrap();
    assert_fs::fixture::ChildPath::new(dir.path().join("app/tests/app_test.cc"))
        .write_str("#include \"lib.h\"\nint main() { return lib_answer() == 3 ? 0 : 1; }\n")
        .unwrap();
    cabin()
        .args(["test", "--manifest-path"])
        .arg(dir.path().join("app/cabin.toml"))
        .arg("--build-dir")
        .arg(dir.path().join("build"))
        .assert()
        .success();
}

#[test]
fn build_diagnoses_ordinary_target_referencing_dev_dependency() {
    require_cxx_build_tools();
    let dir = TempDir::new().unwrap();
    let manifest = write_app_with_dev_path_harness(dir.path());
    // Rewire the *library* target to reference the dev dep: the
    // planner must name the dev-dependency policy instead of a
    // generic unknown-target error.
    let body = fs::read_to_string(&manifest).unwrap();
    assert_fs::fixture::ChildPath::new(&manifest)
        .write_str(&body.replace(
            "sources = [\"src/applib.cc\"]",
            "sources = [\"src/applib.cc\"]\ndeps = [\"harness\"]",
        ))
        .unwrap();
    let assertion = cabin()
        .args(["build", "--manifest-path"])
        .arg(&manifest)
        .arg("--build-dir")
        .arg(dir.path().join("build"))
        .assert()
        .failure();
    let stderr = String::from_utf8_lossy(&assertion.get_output().stderr);
    assert!(
        stderr.contains("[dev-dependencies]") && stderr.contains("harness"),
        "expected the dev-dependency diagnostic: {stderr}"
    );
}

#[test]
fn test_resolves_versioned_deps_of_dev_path_dependency() {
    require_cxx_build_tools();
    let dir = TempDir::new().unwrap();
    // `app` dev-depends on path package `harness`, whose *normal*
    // `[dependencies]` include registry package `depcheck`.  The
    // dev edge makes `harness` reachable, so its versioned dep
    // must resolve, fetch, link, and lock like any other - the
    // resolver input walks the dev-aware closure, not the
    // dev-blind initial graph.
    let archive = dir.path().join("artifacts/depcheck-1.0.0.tar.gz");
    let hex = make_archive(
        &archive,
        &[
            (
                "cabin.toml",
                r#"[package]
name = "depcheck"
version = "1.0.0"
cxx-standard = "c++17"

[target.depcheck]
type = "library"
sources = ["src/depcheck.cc"]
include-dirs = ["include"]
"#,
            ),
            (
                "include/depcheck.h",
                "#pragma once\nint depcheck_answer();\n",
            ),
            (
                "src/depcheck.cc",
                "#include \"depcheck.h\"\nint depcheck_answer() { return 11; }\n",
            ),
        ],
    );
    write_index_entry(
        &dir.path().join("index"),
        "depcheck",
        "1.0.0",
        "{}",
        &hex,
        "../artifacts/depcheck-1.0.0.tar.gz",
    );
    assert_fs::fixture::ChildPath::new(dir.path().join("harness/cabin.toml"))
        .write_str(
            r#"[package]
name = "harness"
version = "0.1.0"
cxx-standard = "c++17"

[dependencies]
depcheck = ">=1"

[target.harness]
type = "library"
sources = ["src/harness.cc"]
include-dirs = ["include"]
deps = ["depcheck"]
"#,
        )
        .unwrap();
    assert_fs::fixture::ChildPath::new(dir.path().join("harness/include/harness.h"))
        .write_str("#pragma once\nint harness_answer();\n")
        .unwrap();
    assert_fs::fixture::ChildPath::new(dir.path().join("harness/src/harness.cc"))
        .write_str(
            "#include \"harness.h\"\n#include \"depcheck.h\"\nint harness_answer() { return depcheck_answer(); }\n",
        )
        .unwrap();
    assert_fs::fixture::ChildPath::new(dir.path().join("app/cabin.toml"))
        .write_str(
            r#"[package]
name = "app"
version = "0.1.0"
cxx-standard = "c++17"

[dev-dependencies]
harness = { path = "../harness" }

[target.applib]
type = "library"
sources = ["src/applib.cc"]

[target.app_test]
type = "test"
sources = ["tests/app_test.cc"]
deps = ["harness"]
"#,
        )
        .unwrap();
    assert_fs::fixture::ChildPath::new(dir.path().join("app/src/applib.cc"))
        .write_str("int applib_answer() { return 11; }\n")
        .unwrap();
    assert_fs::fixture::ChildPath::new(dir.path().join("app/tests/app_test.cc"))
        .write_str(
            "#include \"harness.h\"\nint main() { return harness_answer() == 11 ? 0 : 1; }\n",
        )
        .unwrap();

    let assertion = cabin()
        .args(["test", "--manifest-path"])
        .arg(dir.path().join("app/cabin.toml"))
        .arg("--index-path")
        .arg(dir.path().join("index"))
        .arg("--cache-dir")
        .arg(dir.path().join("cache"))
        .arg("--build-dir")
        .arg(dir.path().join("build"))
        .assert()
        .success();
    let stdout = String::from_utf8_lossy(&assertion.get_output().stdout);
    assert!(
        stdout.contains("test app:app_test ... ok"),
        "test linking through the dev path dep's registry dep should run: {stdout}"
    );
    let lock = fs::read_to_string(dir.path().join("app/cabin.lock")).unwrap();
    assert!(
        lock.contains(r#"name = "depcheck""#),
        "the dev path dep's registry dep should be locked: {lock}"
    );
}

#[test]
fn test_resolves_feature_gated_optional_dep_of_dev_path_dependency() {
    require_cxx_build_tools();
    let dir = TempDir::new().unwrap();
    // `app` dev-depends on path package `harness`, whose default
    // feature enables the optional registry dep `optdep`.
    // Feature resolution must traverse the activated dev edge so
    // the optional dep reaches the resolver, fetches, links, and
    // locks.
    let archive = dir.path().join("artifacts/optdep-1.0.0.tar.gz");
    let hex = make_archive(
        &archive,
        &[
            (
                "cabin.toml",
                r#"[package]
name = "optdep"
version = "1.0.0"
cxx-standard = "c++17"

[target.optdep]
type = "library"
sources = ["src/optdep.cc"]
include-dirs = ["include"]
"#,
            ),
            ("include/optdep.h", "#pragma once\nint optdep_answer();\n"),
            (
                "src/optdep.cc",
                "#include \"optdep.h\"\nint optdep_answer() { return 21; }\n",
            ),
        ],
    );
    write_index_entry(
        &dir.path().join("index"),
        "optdep",
        "1.0.0",
        "{}",
        &hex,
        "../artifacts/optdep-1.0.0.tar.gz",
    );
    assert_fs::fixture::ChildPath::new(dir.path().join("harness/cabin.toml"))
        .write_str(
            r#"[package]
name = "harness"
version = "0.1.0"
cxx-standard = "c++17"

[dependencies]
optdep = { version = ">=1", optional = true }

[features]
withopt = ["dep:optdep"]
default = ["withopt"]

[target.harness]
type = "library"
sources = ["src/harness.cc"]
include-dirs = ["include"]
deps = ["optdep"]
"#,
        )
        .unwrap();
    assert_fs::fixture::ChildPath::new(dir.path().join("harness/include/harness.h"))
        .write_str("#pragma once\nint harness_answer();\n")
        .unwrap();
    assert_fs::fixture::ChildPath::new(dir.path().join("harness/src/harness.cc"))
        .write_str(
            "#include \"harness.h\"\n#include \"optdep.h\"\nint harness_answer() { return optdep_answer(); }\n",
        )
        .unwrap();
    assert_fs::fixture::ChildPath::new(dir.path().join("app/cabin.toml"))
        .write_str(
            r#"[package]
name = "app"
version = "0.1.0"
cxx-standard = "c++17"

[dev-dependencies]
harness = { path = "../harness" }

[target.applib]
type = "library"
sources = ["src/applib.cc"]

[target.app_test]
type = "test"
sources = ["tests/app_test.cc"]
deps = ["harness"]
"#,
        )
        .unwrap();
    assert_fs::fixture::ChildPath::new(dir.path().join("app/src/applib.cc"))
        .write_str("int applib_answer() { return 21; }\n")
        .unwrap();
    assert_fs::fixture::ChildPath::new(dir.path().join("app/tests/app_test.cc"))
        .write_str(
            "#include \"harness.h\"\nint main() { return harness_answer() == 21 ? 0 : 1; }\n",
        )
        .unwrap();

    let assertion = cabin()
        .args(["test", "--manifest-path"])
        .arg(dir.path().join("app/cabin.toml"))
        .arg("--index-path")
        .arg(dir.path().join("index"))
        .arg("--cache-dir")
        .arg(dir.path().join("cache"))
        .arg("--build-dir")
        .arg(dir.path().join("build"))
        .assert()
        .success();
    let stdout = String::from_utf8_lossy(&assertion.get_output().stdout);
    assert!(
        stdout.contains("test app:app_test ... ok"),
        "test linking the dev path dep's feature-gated optional dep should run: {stdout}"
    );
    let lock = fs::read_to_string(dir.path().join("app/cabin.lock")).unwrap();
    assert!(
        lock.contains(r#"name = "optdep""#),
        "the feature-gated optional dep should be locked: {lock}"
    );
}

#[test]
fn allow_no_tests_skips_dev_dep_resolution_without_index() {
    // No test targets are selected, so `--allow-no-tests` must
    // succeed before the versioned-dep check - even though the
    // dev path dep's own registry dependency would otherwise
    // require an index.  No build tools needed: the command
    // exits before toolchain detection.
    let dir = TempDir::new().unwrap();
    assert_fs::fixture::ChildPath::new(dir.path().join("harness/cabin.toml"))
        .write_str(
            r#"[package]
name = "harness"
version = "0.1.0"
cxx-standard = "c++17"

[dependencies]
depcheck = ">=1"

[target.harness]
type = "library"
sources = ["src/harness.cc"]
"#,
        )
        .unwrap();
    assert_fs::fixture::ChildPath::new(dir.path().join("harness/src/harness.cc"))
        .write_str("int h() { return 1; }\n")
        .unwrap();
    assert_fs::fixture::ChildPath::new(dir.path().join("app/cabin.toml"))
        .write_str(
            r#"[package]
name = "app"
version = "0.1.0"
cxx-standard = "c++17"

[dev-dependencies]
harness = { path = "../harness" }

[target.applib]
type = "library"
sources = ["src/applib.cc"]
"#,
        )
        .unwrap();
    assert_fs::fixture::ChildPath::new(dir.path().join("app/src/applib.cc"))
        .write_str("int a() { return 1; }\n")
        .unwrap();
    cabin()
        .args(["test", "--allow-no-tests", "--manifest-path"])
        .arg(dir.path().join("app/cabin.toml"))
        .arg("--build-dir")
        .arg(dir.path().join("build"))
        .assert()
        .success()
        .stdout(predicate::str::contains("no test targets found"));

    // A *missing* dev path dep directory must not fail the run
    // either: the no-tests shortcut fires before port discovery
    // or any dev-aware load touches the dev edge.
    fs::remove_dir_all(dir.path().join("harness")).unwrap();
    cabin()
        .args(["test", "--allow-no-tests", "--manifest-path"])
        .arg(dir.path().join("app/cabin.toml"))
        .arg("--build-dir")
        .arg(dir.path().join("build"))
        .assert()
        .success()
        .stdout(predicate::str::contains("no test targets found"));
}

#[test]
fn test_resolves_feature_entry_referencing_versioned_dev_dep() {
    require_cxx_build_tools();
    let dir = TempDir::new().unwrap();
    // A `[features]` entry of the selected package references its
    // *versioned* dev dep (`devkit/simd`).  Feature resolution
    // must treat the activated dev dep as declared even before
    // its registry edge is fetched - the pre-resolution probe
    // would otherwise fail with `UnknownDependency`.
    let archive = dir.path().join("artifacts/devkit-1.0.0.tar.gz");
    let hex = make_archive(
        &archive,
        &[
            (
                "cabin.toml",
                r#"[package]
name = "devkit"
version = "1.0.0"
cxx-standard = "c++17"

[features]
simd = []

[target.devkit]
type = "library"
sources = ["src/devkit.cc"]
include-dirs = ["include"]
"#,
            ),
            ("include/devkit.h", "#pragma once\nint devkit_answer();\n"),
            (
                "src/devkit.cc",
                "#include \"devkit.h\"\nint devkit_answer() { return 8; }\n",
            ),
        ],
    );
    write_index_entry(
        &dir.path().join("index"),
        "devkit",
        "1.0.0",
        "{}",
        &hex,
        "../artifacts/devkit-1.0.0.tar.gz",
    );
    assert_fs::fixture::ChildPath::new(dir.path().join("app/cabin.toml"))
        .write_str(
            r#"[package]
name = "app"
version = "0.1.0"
cxx-standard = "c++17"

[dev-dependencies]
devkit = ">=1"

[features]
simd-tests = ["devkit/simd"]
default = ["simd-tests"]

[target.applib]
type = "library"
sources = ["src/applib.cc"]

[target.app_test]
type = "test"
sources = ["tests/app_test.cc"]
deps = ["devkit"]
"#,
        )
        .unwrap();
    assert_fs::fixture::ChildPath::new(dir.path().join("app/src/applib.cc"))
        .write_str("int applib_answer() { return 8; }\n")
        .unwrap();
    assert_fs::fixture::ChildPath::new(dir.path().join("app/tests/app_test.cc"))
        .write_str("#include \"devkit.h\"\nint main() { return devkit_answer() == 8 ? 0 : 1; }\n")
        .unwrap();

    let assertion = cabin()
        .args(["test", "--manifest-path"])
        .arg(dir.path().join("app/cabin.toml"))
        .arg("--index-path")
        .arg(dir.path().join("index"))
        .arg("--cache-dir")
        .arg(dir.path().join("cache"))
        .arg("--build-dir")
        .arg(dir.path().join("build"))
        .assert()
        .success();
    let stdout = String::from_utf8_lossy(&assertion.get_output().stdout);
    assert!(
        stdout.contains("test app:app_test ... ok"),
        "feature entry referencing the versioned dev dep should not break cabin test: {stdout}"
    );

    // Under `cabin build` the dev dep is not activated, so the
    // enabled default feature's `devkit/simd` entry still
    // surfaces the unknown-dependency error - the ordinary
    // command behavior is unchanged.
    cabin()
        .args(["build", "--manifest-path"])
        .arg(dir.path().join("app/cabin.toml"))
        .arg("--build-dir")
        .arg(dir.path().join("build-ordinary"))
        .assert()
        .failure();
}

#[test]
fn test_resolves_versioned_dev_dependency_and_locks_it() {
    require_cxx_build_tools();
    let dir = TempDir::new().unwrap();
    // Registry package `devcheck` is declared only under
    // `[dev-dependencies]`; `cabin test` must resolve, fetch,
    // build, and link it - and record it in the lockfile.
    let archive = dir.path().join("artifacts/devcheck-1.2.0.tar.gz");
    let hex = make_archive(
        &archive,
        &[
            (
                "cabin.toml",
                r#"[package]
name = "devcheck"
version = "1.2.0"
cxx-standard = "c++17"

[target.devcheck]
type = "library"
sources = ["src/devcheck.cc"]
include-dirs = ["include"]
"#,
            ),
            (
                "include/devcheck.h",
                "#pragma once\nint devcheck_answer();\n",
            ),
            (
                "src/devcheck.cc",
                "#include \"devcheck.h\"\nint devcheck_answer() { return 5; }\n",
            ),
        ],
    );
    write_index_entry(
        &dir.path().join("index"),
        "devcheck",
        "1.2.0",
        "{}",
        &hex,
        "../artifacts/devcheck-1.2.0.tar.gz",
    );
    assert_fs::fixture::ChildPath::new(dir.path().join("app/cabin.toml"))
        .write_str(
            r#"[package]
name = "app"
version = "0.1.0"
cxx-standard = "c++17"

[dev-dependencies]
devcheck = ">=1 <2"

[target.applib]
type = "library"
sources = ["src/applib.cc"]

[target.app_test]
type = "test"
sources = ["tests/app_test.cc"]
deps = ["devcheck"]
"#,
        )
        .unwrap();
    assert_fs::fixture::ChildPath::new(dir.path().join("app/src/applib.cc"))
        .write_str("int applib_answer() { return 5; }\n")
        .unwrap();
    assert_fs::fixture::ChildPath::new(dir.path().join("app/tests/app_test.cc"))
        .write_str(
            "#include \"devcheck.h\"\nint main() { return devcheck_answer() == 5 ? 0 : 1; }\n",
        )
        .unwrap();

    let assertion = cabin()
        .args(["test", "--manifest-path"])
        .arg(dir.path().join("app/cabin.toml"))
        .arg("--index-path")
        .arg(dir.path().join("index"))
        .arg("--cache-dir")
        .arg(dir.path().join("cache"))
        .arg("--build-dir")
        .arg(dir.path().join("build"))
        .assert()
        .success();
    let stdout = String::from_utf8_lossy(&assertion.get_output().stdout);
    assert!(
        stdout.contains("test app:app_test ... ok"),
        "test executable linking the versioned dev dep should run: {stdout}"
    );

    // The activated dev dep is a real resolution input, so the
    // lockfile records it.
    let lock = fs::read_to_string(dir.path().join("app/cabin.lock")).unwrap();
    assert!(
        lock.contains(r#"name = "devcheck""#),
        "activated dev dep should be locked: {lock}"
    );

    // The same tree under `cabin build` ignores the dev dep
    // entirely - no index needed, nothing of `devcheck` in the
    // ordinary build graph.
    let build_dir = dir.path().join("build-ordinary");
    cabin()
        .args(["build", "--manifest-path"])
        .arg(dir.path().join("app/cabin.toml"))
        .arg("--build-dir")
        .arg(&build_dir)
        .assert()
        .success();
    let ninja = fs::read_to_string(build_dir.join("dev/build.ninja")).unwrap();
    assert!(
        !ninja.contains("devcheck"),
        "versioned dev dep must stay out of the ordinary build graph: {ninja}"
    );
}

#[test]
fn test_links_dev_port_path_dependency_into_test_target() {
    require_cxx_build_tools();
    let tmp = TempDir::new().unwrap();
    let repo = FakePortRepo::new(tmp.path());
    let checkkit = repo
        .port("checkkit", "1.0.0")
        .archive_prefix("checkkit-1.0.0")
        .file(
            "include/checkkit.h",
            "#pragma once\nint checkkit_answer();\n",
        )
        .file(
            "src/checkkit.cc",
            "#include \"checkkit.h\"\nint checkkit_answer() { return 9; }\n",
        )
        .overlay_manifest(
            r#"[package]
name = "checkkit"
version = "1.0.0"
cxx-standard = "c++17"

[target.checkkit]
type = "library"
sources = ["src/checkkit.cc"]
include-dirs = ["include"]
"#,
        )
        .build();
    let server = FakeArchiveServer::new().serve(&checkkit.archive).start();

    assert_fs::fixture::ChildPath::new(tmp.path().join("consumer/cabin.toml"))
        .write_str(
            r#"[package]
name = "consumer"
version = "0.1.0"
cxx-standard = "c++17"

[dev-dependencies]
checkkit = { port-path = "../ports/checkkit/1.0.0" }

[target.consumerlib]
type = "library"
sources = ["src/lib.cc"]

[target.consumer_test]
type = "test"
sources = ["tests/consumer_test.cc"]
deps = ["checkkit"]
"#,
        )
        .unwrap();
    assert_fs::fixture::ChildPath::new(tmp.path().join("consumer/src/lib.cc"))
        .write_str("int consumer_lib() { return 1; }\n")
        .unwrap();
    assert_fs::fixture::ChildPath::new(tmp.path().join("consumer/tests/consumer_test.cc"))
        .write_str(
            "#include \"checkkit.h\"\nint main() { return checkkit_answer() == 9 ? 0 : 1; }\n",
        )
        .unwrap();

    // Ordinary build first: the dev port is declaration-only, so
    // nothing is downloaded.
    cabin()
        .args(["build", "--manifest-path"])
        .arg(tmp.path().join("consumer/cabin.toml"))
        .arg("--build-dir")
        .arg(tmp.path().join("build-ordinary"))
        .arg("--cache-dir")
        .arg(tmp.path().join("cache"))
        .assert()
        .success();
    assert_eq!(
        server.total_requests(),
        0,
        "cabin build must not fetch dev port archives"
    );

    // `cabin test` activates the dev port: download, build, link,
    // run.
    let assertion = cabin()
        .args(["test", "--manifest-path"])
        .arg(tmp.path().join("consumer/cabin.toml"))
        .arg("--build-dir")
        .arg(tmp.path().join("build"))
        .arg("--cache-dir")
        .arg(tmp.path().join("cache"))
        .assert()
        .success();
    let stdout = String::from_utf8_lossy(&assertion.get_output().stdout);
    assert!(
        stdout.contains("test consumer:consumer_test ... ok"),
        "test executable linking the dev port should run: {stdout}"
    );
    assert_eq!(server.requests_for(checkkit.archive.name()), 1);
}