provenant-cli 1.0.0

Fast Rust scanner for licenses, copyrights, package metadata, SBOMs, and provenance data.
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
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
// SPDX-FileCopyrightText: nexB Inc. and others
// ScanCode is a trademark of nexB Inc.
// SPDX-FileCopyrightText: Provenant contributors
// SPDX-License-Identifier: Apache-2.0
// Derived from ScanCode Toolkit (Apache-2.0); modified. See NOTICE.

use std::collections::{BTreeSet, HashMap, HashSet};
use std::io::{self, Write};
use std::path::{Path, PathBuf};

use sha1::{Digest, Sha1};

use crate::output_schema::{
    Output, OutputFileInfo as FileInfo, OutputFileType, OutputMatch as Match, OutputPackage,
};
use crate::utils::time::{convert_header_timestamp_to_iso_utc, fallback_iso_utc_timestamp};

use super::shared::{sorted_files, xml_escape};
use super::{OutputWriteConfig, SPDX_DOCUMENT_NOTICE};

const EMPTY_SHA1_HEX: &str = "da39a3ee5e6b4b0d3255bfef95601890afd80709";

struct ExtractedLicenseInfo {
    license_id: String,
    name: String,
    extracted_text: String,
    comment: String,
}

/// One SPDX Package to emit, with the files it owns.
struct SpdxPackagePlan<'a> {
    spdx_id: String,
    name: String,
    files: Vec<&'a FileInfo>,
    /// The assembled package this plan was built from, when there is one.
    /// `None` for the no-package synthetic plan and the unassigned-files
    /// fallback bucket, which have no package-level license data to draw on.
    package: Option<&'a OutputPackage>,
}

pub(crate) fn write_spdx_tag_value(
    output: &Output,
    writer: &mut dyn Write,
    config: &OutputWriteConfig,
) -> io::Result<()> {
    let files = spdx_files(output);
    let fallback_name = primary_package_name(output, config);
    if files.is_empty() {
        writeln!(writer, "# No results for package '{}'.", fallback_name)?;
        return Ok(());
    }

    let plans = plan_spdx_packages(output, &files, config);
    let document_namespace = document_namespace_for(output, config);
    let extracted_license_infos = spdx_extracted_license_infos(output, &files);
    let created = spdx_created_timestamp(output);
    let creator = spdx_creator();

    // Global file index → SPDXRef so CONTAINS relationships can point at files.
    let mut file_spdx_ids: HashMap<&str, String> = HashMap::new();
    for (file_index, file) in (1usize..).zip(files.iter()) {
        file_spdx_ids.insert(file.path.as_str(), format!("SPDXRef-{file_index}"));
    }

    writeln!(writer, "## Document Information")?;
    writeln!(writer, "SPDXVersion: SPDX-2.2")?;
    writeln!(writer, "DataLicense: CC0-1.0")?;
    writeln!(writer, "SPDXID: SPDXRef-DOCUMENT")?;
    writeln!(writer, "DocumentName: SPDX Document created by Provenant")?;
    writeln!(writer, "DocumentNamespace: {}", document_namespace)?;
    writeln!(
        writer,
        "DocumentComment: <text>{}</text>",
        sanitize_spdx_text_content(SPDX_DOCUMENT_NOTICE)
    )?;
    writeln!(writer, "## Creation Information")?;
    writeln!(writer, "Creator: {}", creator)?;
    writeln!(writer, "Created: {}", created)?;

    for plan in &plans {
        let package_verification_code = spdx_package_verification_code(&plan.files);
        let package_license_info_from_files = spdx_package_license_info_from_files(&plan.files);
        let package_copyright_text = spdx_package_copyright_text(&plan.files);
        let package_license_concluded = spdx_package_license_concluded(plan.package);
        let package_license_declared = spdx_package_license_declared(plan.package);

        writeln!(writer, "## Package Information")?;
        writeln!(writer, "PackageName: {}", plan.name)?;
        writeln!(writer, "SPDXID: {}", plan.spdx_id)?;
        writeln!(writer, "PackageDownloadLocation: NOASSERTION")?;
        writeln!(writer, "FilesAnalyzed: true")?;
        writeln!(
            writer,
            "PackageVerificationCode: {}",
            package_verification_code
        )?;
        writeln!(
            writer,
            "PackageLicenseConcluded: {}",
            spdx_tv_license_value(package_license_concluded.as_deref())
        )?;
        for license_id in &package_license_info_from_files {
            writeln!(writer, "PackageLicenseInfoFromFiles: {}", license_id)?;
        }
        if package_license_info_from_files.is_empty() {
            writeln!(writer, "PackageLicenseInfoFromFiles: NONE")?;
        }
        writeln!(
            writer,
            "PackageLicenseDeclared: {}",
            spdx_tv_license_value(package_license_declared.as_deref())
        )?;
        writeln!(
            writer,
            "PackageCopyrightText: {}",
            format_spdx_text_field(&package_copyright_text)
        )?;
    }

    writeln!(writer, "## File Information")?;
    for (file_index, file) in (1usize..).zip(files.iter()) {
        let sha1 = file.sha1.as_deref().unwrap_or(EMPTY_SHA1_HEX);
        let file_license_info = spdx_file_license_info(file);
        let file_license_concluded = spdx_file_license_concluded(file);
        writeln!(
            writer,
            "FileName: {}",
            spdx_relative_file_name(&file.path, config.scanned_path.as_deref())
        )?;
        writeln!(writer, "SPDXID: SPDXRef-{}", file_index)?;
        writeln!(writer, "FileChecksum: SHA1: {}", sha1)?;
        writeln!(
            writer,
            "LicenseConcluded: {}",
            spdx_tv_license_value(file_license_concluded.as_deref())
        )?;
        if file_license_info.is_empty() {
            writeln!(writer, "LicenseInfoInFile: NONE")?;
        } else {
            for license_id in file_license_info {
                writeln!(writer, "LicenseInfoInFile: {}", license_id)?;
            }
        }

        if file.copyrights.is_empty() {
            writeln!(writer, "FileCopyrightText: NONE")?;
        } else {
            let text = file
                .copyrights
                .iter()
                .map(|c| c.copyright.clone())
                .collect::<Vec<_>>()
                .join("\\n");
            writeln!(
                writer,
                "FileCopyrightText: {}",
                format_spdx_text_field(&text)
            )?;
        }

        writeln!(writer)?;
    }

    writeln!(writer, "## Relationships")?;
    for plan in &plans {
        writeln!(
            writer,
            "Relationship: SPDXRef-DOCUMENT DESCRIBES {}",
            plan.spdx_id
        )?;
        for file in &plan.files {
            if let Some(file_id) = file_spdx_ids.get(file.path.as_str()) {
                writeln!(
                    writer,
                    "Relationship: {} CONTAINS {}",
                    plan.spdx_id, file_id
                )?;
            }
        }
    }

    if !extracted_license_infos.is_empty() {
        writeln!(writer, "## License Information")?;
        for info in extracted_license_infos {
            writeln!(writer, "LicenseID: {}", info.license_id)?;
            writeln!(
                writer,
                "ExtractedText: <text>{}",
                sanitize_spdx_text_content(&info.extracted_text)
            )?;
            writeln!(writer, "</text>")?;
            writeln!(writer, "LicenseName: {}", info.name)?;
            writeln!(
                writer,
                "LicenseComment: <text>{}",
                sanitize_spdx_text_content(&info.comment)
            )?;
            writeln!(writer, "</text>")?;
        }
    }

    Ok(())
}

pub(crate) fn write_spdx_rdf_xml(
    output: &Output,
    writer: &mut dyn Write,
    config: &OutputWriteConfig,
) -> io::Result<()> {
    let fallback_name = primary_package_name(output, config);
    let files = spdx_files(output);
    if files.is_empty() {
        writeln!(
            writer,
            "<!-- No results for package '{}'. -->",
            fallback_name
        )?;
        return Ok(());
    }

    let plans = plan_spdx_packages(output, &files, config);
    let document_namespace = document_namespace_for(output, config);
    let document_namespace_xml = xml_escape(&document_namespace);
    let extracted_license_infos = spdx_extracted_license_infos(output, &files);
    let created = xml_escape(&spdx_created_timestamp(output));
    let creator = xml_escape(&spdx_creator());

    let mut file_spdx_ids: HashMap<&str, String> = HashMap::new();
    for (file_index, file) in (1usize..).zip(files.iter()) {
        file_spdx_ids.insert(file.path.as_str(), format!("SPDXRef-{file_index}"));
    }

    let mut xml = String::new();
    xml.push_str("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
    xml.push_str("<rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" xmlns:rdfs=\"http://www.w3.org/2000/01/rdf-schema#\" xmlns:spdx=\"http://spdx.org/rdf/terms#\">\n");

    for plan in &plans {
        let package_verification_code = spdx_package_verification_code(&plan.files);
        let package_license_info_from_files = spdx_package_license_info_from_files(&plan.files);
        let package_copyright_text = xml_escape(&spdx_package_copyright_text(&plan.files));
        let package_name = xml_escape(&plan.name);
        let package_license_concluded = spdx_package_license_concluded(plan.package);
        let package_license_declared = spdx_package_license_declared(plan.package);

        xml.push_str("  <spdx:Package rdf:about=\"");
        xml.push_str(&document_namespace_xml);
        xml.push('#');
        xml.push_str(&xml_escape(&plan.spdx_id));
        xml.push_str("\">\n");
        xml.push_str("    <spdx:filesAnalyzed rdf:datatype=\"http://www.w3.org/2001/XMLSchema#boolean\">true</spdx:filesAnalyzed>\n");
        xml.push_str(
            "    <spdx:downloadLocation rdf:resource=\"http://spdx.org/rdf/terms#noassertion\"/>\n",
        );
        xml.push_str("    <spdx:licenseConcluded rdf:resource=\"");
        xml.push_str(&xml_escape(&spdx_rdf_license_resource(
            package_license_concluded.as_deref(),
            &document_namespace,
        )));
        xml.push_str("\"/>\n");
        xml.push_str("    <spdx:licenseDeclared rdf:resource=\"");
        xml.push_str(&xml_escape(&spdx_rdf_license_resource(
            package_license_declared.as_deref(),
            &document_namespace,
        )));
        xml.push_str("\"/>\n");
        if package_license_info_from_files.is_empty() {
            xml.push_str(
                "    <spdx:licenseInfoFromFiles rdf:resource=\"http://spdx.org/rdf/terms#none\"/>\n",
            );
        } else {
            for license_id in &package_license_info_from_files {
                xml.push_str("    <spdx:licenseInfoFromFiles rdf:resource=\"");
                xml.push_str(&xml_escape(&spdx_license_rdf_resource(
                    license_id,
                    &document_namespace,
                )));
                xml.push_str("\"/>\n");
            }
        }
        xml.push_str("    <spdx:packageVerificationCode><spdx:PackageVerificationCode><spdx:packageVerificationCodeValue>");
        xml.push_str(&package_verification_code);
        xml.push_str("</spdx:packageVerificationCodeValue></spdx:PackageVerificationCode></spdx:packageVerificationCode>\n");

        for file in &plan.files {
            let Some(file_id) = file_spdx_ids.get(file.path.as_str()) else {
                continue;
            };
            let file_license_info = spdx_file_license_info(file);
            let file_license_concluded = spdx_file_license_concluded(file);
            xml.push_str("    <spdx:relationship><spdx:Relationship>");
            xml.push_str("<spdx:relationshipType rdf:resource=\"http://spdx.org/rdf/terms#relationshipType_contains\"/>");
            xml.push_str("<spdx:relatedSpdxElement><spdx:File rdf:about=\"");
            xml.push_str(&document_namespace_xml);
            xml.push('#');
            xml.push_str(&xml_escape(file_id));
            xml.push_str("\">");
            xml.push_str("<spdx:licenseConcluded rdf:resource=\"");
            xml.push_str(&xml_escape(&spdx_rdf_license_resource(
                file_license_concluded.as_deref(),
                &document_namespace,
            )));
            xml.push_str("\"/>");
            if file_license_info.is_empty() {
                xml.push_str(
                    "<spdx:licenseInfoInFile rdf:resource=\"http://spdx.org/rdf/terms#none\"/>",
                );
            } else {
                for license_id in file_license_info {
                    xml.push_str("<spdx:licenseInfoInFile rdf:resource=\"");
                    xml.push_str(&xml_escape(&spdx_license_rdf_resource(
                        &license_id,
                        &document_namespace,
                    )));
                    xml.push_str("\"/>");
                }
            }
            xml.push_str("<spdx:checksum><spdx:Checksum><spdx:algorithm rdf:resource=\"http://spdx.org/rdf/terms#checksumAlgorithm_sha1\"/>");
            xml.push_str("<spdx:checksumValue>");
            xml.push_str(&xml_escape(file.sha1.as_deref().unwrap_or(EMPTY_SHA1_HEX)));
            xml.push_str("</spdx:checksumValue></spdx:Checksum></spdx:checksum>");
            xml.push_str("<spdx:fileName>");
            xml.push_str(&xml_escape(&spdx_relative_file_name(
                &file.path,
                config.scanned_path.as_deref(),
            )));
            xml.push_str("</spdx:fileName>");
            xml.push_str("<spdx:copyrightText>");
            if file.copyrights.is_empty() {
                xml.push_str("NONE");
            } else {
                xml.push_str(&xml_escape(
                    &file
                        .copyrights
                        .iter()
                        .map(|c| c.copyright.clone())
                        .collect::<Vec<_>>()
                        .join("\\n"),
                ));
            }
            xml.push_str("</spdx:copyrightText>");
            xml.push_str(
                "</spdx:File></spdx:relatedSpdxElement></spdx:Relationship></spdx:relationship>\n",
            );
        }

        xml.push_str("    <spdx:copyrightText>");
        xml.push_str(&package_copyright_text);
        xml.push_str("</spdx:copyrightText>\n");
        xml.push_str("    <spdx:name>");
        xml.push_str(&package_name);
        xml.push_str("</spdx:name>\n");
        xml.push_str("  </spdx:Package>\n");
    }

    // spdx-tools expects SpdxDocument rdf:about = documentNamespace + "#SPDXRef-DOCUMENT".
    xml.push_str("  <spdx:SpdxDocument rdf:about=\"");
    xml.push_str(&document_namespace_xml);
    xml.push_str("#SPDXRef-DOCUMENT\">\n");
    xml.push_str("    <spdx:dataLicense rdf:resource=\"http://spdx.org/licenses/CC0-1.0\"/>\n");
    xml.push_str("    <rdfs:comment>");
    xml.push_str(&xml_escape(SPDX_DOCUMENT_NOTICE));
    xml.push_str("</rdfs:comment>\n");
    for info in extracted_license_infos {
        xml.push_str(
            "    <spdx:hasExtractedLicensingInfo><spdx:ExtractedLicensingInfo rdf:about=\"",
        );
        xml.push_str(&document_namespace_xml);
        xml.push('#');
        xml.push_str(&xml_escape(&info.license_id));
        xml.push_str("\">");
        xml.push_str("<spdx:licenseId>");
        xml.push_str(&xml_escape(&info.license_id));
        xml.push_str("</spdx:licenseId>");
        xml.push_str("<spdx:name>");
        xml.push_str(&xml_escape(&info.name));
        xml.push_str("</spdx:name>");
        xml.push_str("<rdfs:comment>");
        xml.push_str(&xml_escape(&info.comment));
        xml.push_str("</rdfs:comment>");
        xml.push_str("<spdx:extractedText>");
        xml.push_str(&xml_escape(&info.extracted_text));
        xml.push_str("</spdx:extractedText>");
        xml.push_str("</spdx:ExtractedLicensingInfo></spdx:hasExtractedLicensingInfo>\n");
    }
    xml.push_str("    <spdx:name>SPDX Document created by Provenant</spdx:name>\n");
    xml.push_str("    <spdx:specVersion>SPDX-2.2</spdx:specVersion>\n");
    xml.push_str("    <spdx:creationInfo><spdx:CreationInfo>");
    xml.push_str("<spdx:creator>");
    xml.push_str(&creator);
    xml.push_str("</spdx:creator>");
    xml.push_str("<spdx:created>");
    xml.push_str(&created);
    xml.push_str("</spdx:created>");
    xml.push_str("</spdx:CreationInfo></spdx:creationInfo>\n");
    for plan in &plans {
        xml.push_str("    <spdx:relationship><spdx:Relationship>");
        xml.push_str(
            "<spdx:relationshipType rdf:resource=\"http://spdx.org/rdf/terms#relationshipType_describes\"/>",
        );
        xml.push_str("<spdx:relatedSpdxElement rdf:resource=\"");
        xml.push_str(&document_namespace_xml);
        xml.push('#');
        xml.push_str(&xml_escape(&plan.spdx_id));
        xml.push_str("\"/>");
        xml.push_str("</spdx:Relationship></spdx:relationship>\n");
    }
    xml.push_str("  </spdx:SpdxDocument>\n");

    xml.push_str("</rdf:RDF>\n");
    writer.write_all(xml.as_bytes())
}

fn primary_package_name(output: &Output, config: &OutputWriteConfig) -> String {
    if output.packages.len() == 1
        && let Some(name) = output.packages.first().and_then(|p| p.name.clone())
    {
        return sanitize_spdx_package_name(&name);
    }

    if let Some(scanned_path) = &config.scanned_path {
        let path = PathBuf::from(scanned_path);
        if let Some(name) = path.file_name().and_then(|n| n.to_str())
            && !name.is_empty()
        {
            return sanitize_spdx_package_name(name);
        }
    }

    output
        .packages
        .first()
        .and_then(|p| p.name.clone())
        .map(|name| sanitize_spdx_package_name(&name))
        .unwrap_or_else(|| "provenant-analyzed-package".to_string())
}

fn sanitize_spdx_package_name(name: &str) -> String {
    let mut out = String::with_capacity(name.len());
    for ch in name.chars() {
        if ch.is_ascii_alphanumeric() || ch == '_' || ch == '-' || ch == '.' {
            out.push(ch);
        } else {
            out.push('_');
        }
    }
    if out.is_empty() {
        "provenant-analyzed-package".to_string()
    } else {
        out
    }
}

fn spdx_files(output: &Output) -> Vec<&FileInfo> {
    sorted_files(&output.files)
        .into_iter()
        .filter(|f| f.file_type == OutputFileType::File)
        .collect()
}

fn spdx_package_verification_code(files: &[&FileInfo]) -> String {
    let mut file_sha1s = files
        .iter()
        .map(|f| f.sha1.as_deref().unwrap_or(EMPTY_SHA1_HEX).to_string())
        .collect::<Vec<_>>();
    file_sha1s.sort_unstable();

    let mut hasher = Sha1::new();
    for sha1_hex in file_sha1s {
        hasher.update(sha1_hex.as_bytes());
    }
    hex::encode(hasher.finalize())
}

fn spdx_file_license_info(file: &FileInfo) -> Vec<String> {
    let mut license_ids = BTreeSet::new();

    for detection in file.license_detections.iter().chain(
        file.package_data
            .iter()
            .flat_map(|package_data| package_data.license_detections.iter())
            .chain(
                file.package_data
                    .iter()
                    .flat_map(|package_data| package_data.other_license_detections.iter()),
            ),
    ) {
        if detection.matches.is_empty() {
            license_ids.extend(spdx_ids_from_expression(&detection.license_expression_spdx));
            continue;
        }

        for detection_match in &detection.matches {
            let expression = if detection_match.license_expression_spdx.is_empty() {
                &detection.license_expression_spdx
            } else {
                &detection_match.license_expression_spdx
            };
            license_ids.extend(spdx_ids_from_expression(expression));
        }
    }

    license_ids.into_iter().collect()
}

fn spdx_package_license_info_from_files(files: &[&FileInfo]) -> Vec<String> {
    let mut unique = BTreeSet::new();
    for file in files {
        for license_id in spdx_file_license_info(file) {
            unique.insert(license_id);
        }
    }
    unique.into_iter().collect()
}

/// The package's own declared SPDX expression, straight from parser/assembly
/// -normalized manifest data. `None` (rendered as NOASSERTION) when the
/// package declares nothing we could resolve to a valid SPDX expression; this
/// never falls back to detected evidence, since "declared" specifically means
/// what the package's producer stated.
fn spdx_package_license_declared(package: Option<&OutputPackage>) -> Option<String> {
    package
        .and_then(|pkg| pkg.declared_license_expression_spdx.as_deref())
        .and_then(spdx_validated_expression)
}

/// The best honest SPDX conclusion for the package: the declared expression
/// when present (the single most authoritative source we have), otherwise
/// `other_license_expression_spdx` — license text assembly detected in the
/// package's own files that was not folded into the declared expression (see
/// `reference_following.rs`). Falls back to `None` (NOASSERTION) rather than
/// inventing a conclusion from evidence the package itself doesn't carry.
fn spdx_package_license_concluded(package: Option<&OutputPackage>) -> Option<String> {
    package.and_then(|pkg| {
        pkg.declared_license_expression_spdx
            .as_deref()
            .and_then(spdx_validated_expression)
            .or_else(|| {
                pkg.other_license_expression_spdx
                    .as_deref()
                    .and_then(spdx_validated_expression)
            })
    })
}

/// Re-parse an expression through the same strict SPDX combiner the file
/// conclusion path uses, so malformed package fields (newlines, bad tokens)
/// become `None` / NOASSERTION instead of broken tag-value output.
fn spdx_validated_expression(expression: &str) -> Option<String> {
    if expression.is_empty() {
        return None;
    }
    crate::utils::spdx::combine_license_expressions_preserving_structure_strict([
        expression.to_string()
    ])
}

/// The best honest SPDX conclusion for a file: reuses the same three-tier
/// fallback the JSON `detected_license_expression_spdx` field already applies
/// (own detections, then owning package-data detections, then the carried
/// expression). `None` (rendered as NOASSERTION) when nothing was detected.
fn spdx_file_license_concluded(file: &FileInfo) -> Option<String> {
    file.detected_license_expression_spdx()
}

/// Renders an already-resolved SPDX expression for a tag-value field, or the
/// NOASSERTION placeholder when nothing is known.
fn spdx_tv_license_value(expression: Option<&str>) -> &str {
    expression.unwrap_or("NOASSERTION")
}

/// Renders an already-resolved SPDX expression as an RDF resource. Only a
/// single license id (no `AND`/`OR`/`WITH` operators) can be expressed as the
/// simple `rdf:resource` link this writer uses elsewhere (see
/// `spdx_license_rdf_resource`); a compound expression would need a nested
/// `ConjunctiveLicenseSet`/`DisjunctiveLicenseSet` structure this writer does
/// not build, so it honestly falls back to NOASSERTION in RDF only (the
/// tag-value form above keeps the full expression, since tag-value license
/// fields accept SPDX expression syntax directly).
fn spdx_rdf_license_resource(expression: Option<&str>, document_namespace: &str) -> String {
    match expression.map(spdx_ids_from_expression) {
        Some(ids) if ids.len() == 1 => spdx_license_rdf_resource(&ids[0], document_namespace),
        _ => "http://spdx.org/rdf/terms#noassertion".to_string(),
    }
}

fn spdx_package_copyright_text(files: &[&FileInfo]) -> String {
    let copyrights: BTreeSet<String> = files
        .iter()
        .flat_map(|file| file.copyrights.iter())
        .map(|copyright| copyright.copyright.clone())
        .collect();

    if copyrights.is_empty() {
        "NONE".to_string()
    } else {
        copyrights.into_iter().collect::<Vec<_>>().join("\n")
    }
}

fn spdx_extracted_license_infos(output: &Output, files: &[&FileInfo]) -> Vec<ExtractedLicenseInfo> {
    let license_reference_names: HashMap<&str, &str> = output
        .license_references
        .iter()
        .map(|reference| (reference.spdx_license_key.as_str(), reference.name.as_str()))
        .collect();
    let mut seen = HashSet::new();
    let mut infos = Vec::new();

    for file in files {
        for detection in file.license_detections.iter().chain(
            file.package_data
                .iter()
                .flat_map(|package_data| package_data.license_detections.iter())
                .chain(
                    file.package_data
                        .iter()
                        .flat_map(|package_data| package_data.other_license_detections.iter()),
                ),
        ) {
            for detection_match in &detection.matches {
                let expression = if detection_match.license_expression_spdx.is_empty() {
                    &detection.license_expression_spdx
                } else {
                    &detection_match.license_expression_spdx
                };

                for license_id in spdx_ids_from_expression(expression) {
                    if !license_id.starts_with("LicenseRef-") || !seen.insert(license_id.clone()) {
                        continue;
                    }

                    let comment = spdx_license_comment(detection_match);
                    let extracted_text = detection_match
                        .matched_text
                        .clone()
                        .filter(|text| !text.is_empty())
                        .unwrap_or_else(|| comment.clone());
                    let name = license_reference_names
                        .get(license_id.as_str())
                        .copied()
                        .unwrap_or(license_id.as_str())
                        .to_string();

                    infos.push(ExtractedLicenseInfo {
                        license_id,
                        name,
                        extracted_text,
                        comment,
                    });
                }
            }
        }
    }

    infos
}

fn spdx_license_comment(detection_match: &Match) -> String {
    if let Some(rule_url) = detection_match.rule_url.as_deref()
        && !rule_url.is_empty()
    {
        format!("See details at {}", rule_url)
    } else {
        detection_match
            .matched_text
            .clone()
            .unwrap_or_else(|| "NOASSERTION".to_string())
    }
}

/// Listed SPDX licenses use the canonical `spdx.org/licenses/` URI; document-
/// scoped `LicenseRef-*` ids point at the matching `ExtractedLicensingInfo`
/// node (`{documentNamespace}#{LicenseRef-…}`) instead of a non-existent
/// listed-license URL.
fn spdx_license_rdf_resource(license_id: &str, document_namespace: &str) -> String {
    if license_id.starts_with("LicenseRef-") {
        format!("{document_namespace}#{license_id}")
    } else {
        format!("http://spdx.org/licenses/{license_id}")
    }
}

fn spdx_ids_from_expression(expression: &str) -> Vec<String> {
    let mut ids = Vec::new();
    let mut token = String::new();

    let flush = |token: &mut String, ids: &mut Vec<String>| {
        if token.is_empty() {
            return;
        }
        if !matches!(token.as_str(), "AND" | "OR" | "WITH") {
            ids.push(token.clone());
        }
        token.clear();
    };

    for ch in expression.chars() {
        // Keep underscores so LicenseRef- and SPDX ids that use them stay intact.
        if ch.is_ascii_alphanumeric() || matches!(ch, '-' | '.' | '+' | '_') {
            token.push(ch);
        } else {
            flush(&mut token, &mut ids);
        }
    }
    flush(&mut token, &mut ids);

    ids
}

fn spdx_creator() -> String {
    format!("Tool: Provenant-{}", crate::version::BUILD_VERSION)
}

fn spdx_created_timestamp(output: &Output) -> String {
    output
        .headers
        .first()
        .and_then(|h| convert_header_timestamp_to_iso_utc(&h.start_timestamp))
        .unwrap_or_else(|| fallback_iso_utc_timestamp().to_string())
}

fn sanitize_spdx_text_content(value: &str) -> String {
    // SPDX tag-value `<text>` blocks end at the first `</text>`; neutralize
    // embedded closers so source-derived copyright/license text cannot break
    // the document. Fullwidth brackets keep the content readable.
    value
        .replace("</text>", "</text>")
        .replace("</TEXT>", "</TEXT>")
}

fn format_spdx_text_field(value: &str) -> String {
    let sanitized = sanitize_spdx_text_content(value);
    if sanitized.contains('\n') {
        format!("<text>{sanitized}</text>")
    } else {
        sanitized
    }
}

fn spdx_relative_file_name(path: &str, scanned_path: Option<&str>) -> String {
    // Normalize both sides the same way so a CLI root like `./proj` still
    // strips against collected paths that lose the leading `./`.
    let path = Path::new(trim_spdx_dot_slash(path));
    let relative = match scanned_path {
        Some(root) => match path.strip_prefix(Path::new(trim_spdx_dot_slash(root))) {
            Ok(stripped) => stripped.to_string_lossy().into_owned(),
            Err(_) => path.to_string_lossy().into_owned(),
        },
        None => path.to_string_lossy().into_owned(),
    };

    if relative.is_empty() {
        "./.".to_string()
    } else if let Some(stripped) = relative.strip_prefix('/') {
        // Absolute path that did not strip against scanned_path: keep one slash.
        format!("./{stripped}")
    } else {
        format!("./{relative}")
    }
}

fn trim_spdx_dot_slash(path: &str) -> &str {
    path.trim_start_matches("./")
}

/// Plan SPDX packages: one per assembled package (with owned files), plus a
/// scan-root fallback package for files that no assembled package claims.
/// When there are no assembled packages, keep a single synthetic package
/// (`SPDXRef-001`) owning every file — the historic no-package contract.
fn plan_spdx_packages<'a>(
    output: &'a Output,
    files: &[&'a FileInfo],
    config: &OutputWriteConfig,
) -> Vec<SpdxPackagePlan<'a>> {
    if output.packages.is_empty() {
        return vec![SpdxPackagePlan {
            spdx_id: "SPDXRef-001".to_string(),
            name: primary_package_name(output, config),
            files: files.to_vec(),
            package: None,
        }];
    }

    let mut assigned_paths: HashSet<&str> = HashSet::new();
    let mut plans = Vec::with_capacity(output.packages.len() + 1);

    for (idx, package) in output.packages.iter().enumerate() {
        let owned: Vec<&FileInfo> = files
            .iter()
            .copied()
            .filter(|file| {
                file.for_packages
                    .iter()
                    .any(|uid| uid == &package.package_uid)
            })
            .collect();
        for file in &owned {
            assigned_paths.insert(file.path.as_str());
        }
        plans.push(SpdxPackagePlan {
            spdx_id: format!("SPDXRef-Package-{}", idx + 1),
            name: spdx_assembled_package_name(package, idx),
            files: owned,
            package: Some(package),
        });
    }

    let unassigned: Vec<&FileInfo> = files
        .iter()
        .copied()
        .filter(|file| !assigned_paths.contains(file.path.as_str()))
        .collect();
    if !unassigned.is_empty() {
        plans.push(SpdxPackagePlan {
            spdx_id: "SPDXRef-Package-unassigned".to_string(),
            name: primary_package_name(output, config),
            files: unassigned,
            package: None,
        });
    }

    plans
}

fn spdx_assembled_package_name(package: &OutputPackage, idx: usize) -> String {
    package
        .name
        .as_deref()
        .filter(|name| !name.is_empty())
        .map(sanitize_spdx_package_name)
        .unwrap_or_else(|| format!("package-{}", idx + 1))
}

fn document_namespace_for(output: &Output, config: &OutputWriteConfig) -> String {
    let base = if output.packages.len() > 1 {
        config
            .scanned_path
            .as_deref()
            .and_then(|p| Path::new(p).file_name().and_then(|n| n.to_str()))
            .map(sanitize_spdx_package_name)
            .unwrap_or_else(|| "provenant-scan".to_string())
    } else {
        primary_package_name(output, config)
    };
    format!("http://spdx.org/spdxdocs/{base}")
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::license_detection::MatcherKind;
    use crate::models::{
        FileType, LicenseDetection, LineNumber, MatchScore, PackageData, PackageType,
    };

    #[test]
    fn spdx_relative_file_name_uses_path_prefix_not_string_prefix() {
        // Path::strip_prefix fails for /tmp/proj vs /tmp/project/..., so the
        // original path is preserved (with ./ prefix), not truncated to ect/...
        assert_eq!(
            spdx_relative_file_name("/tmp/project/src.rs", Some("/tmp/proj")),
            "./tmp/project/src.rs"
        );
        assert_eq!(
            spdx_relative_file_name("/tmp/proj/src.rs", Some("/tmp/proj")),
            "./src.rs"
        );
    }

    #[test]
    fn spdx_relative_file_name_strips_matching_dot_slash_roots() {
        assert_eq!(
            spdx_relative_file_name("./proj/src.rs", Some("./proj")),
            "./src.rs"
        );
        assert_eq!(
            spdx_relative_file_name("proj/src.rs", Some("./proj")),
            "./src.rs"
        );
    }

    #[test]
    fn format_spdx_text_field_neutralizes_embedded_text_closers() {
        let rendered = format_spdx_text_field("before</text>after\nline2");
        assert!(rendered.starts_with("<text>"));
        assert!(rendered.ends_with("</text>"));
        assert!(!rendered.contains("before</text>after"));
        assert!(rendered.contains("before</text>after"));
    }

    #[test]
    fn plan_spdx_packages_emits_one_package_per_assembled_package() {
        let mut file_a = crate::models::FileInfo::new(
            "a/mix.exs".to_string(),
            "mix.exs".to_string(),
            String::new(),
            "a/mix.exs".to_string(),
            FileType::File,
            None,
            None,
            1,
            None,
            None,
            None,
            None,
            None,
            vec![],
            None,
            vec![],
            vec![],
            vec![],
            vec![],
            vec![],
            vec![],
            vec![],
            vec![],
            vec![],
        );
        file_a.for_packages = vec![crate::models::PackageUid::from_raw("uid-a".to_string())];
        let mut file_b = crate::models::FileInfo::new(
            "b/mix.exs".to_string(),
            "mix.exs".to_string(),
            String::new(),
            "b/mix.exs".to_string(),
            FileType::File,
            None,
            None,
            1,
            None,
            None,
            None,
            None,
            None,
            vec![],
            None,
            vec![],
            vec![],
            vec![],
            vec![],
            vec![],
            vec![],
            vec![],
            vec![],
            vec![],
        );
        file_b.for_packages = vec![crate::models::PackageUid::from_raw("uid-b".to_string())];
        let file_orphan = crate::models::FileInfo::new(
            "README".to_string(),
            "README".to_string(),
            String::new(),
            "README".to_string(),
            FileType::File,
            None,
            None,
            1,
            None,
            None,
            None,
            None,
            None,
            vec![],
            None,
            vec![],
            vec![],
            vec![],
            vec![],
            vec![],
            vec![],
            vec![],
            vec![],
            vec![],
        );

        let pkg_a = {
            let mut pkg = crate::models::Package::from_package_data(
                &PackageData {
                    package_type: Some(PackageType::Hex),
                    name: Some("a".to_string()),
                    version: Some("0.1.0".to_string()),
                    purl: Some("pkg:hex/a@0.1.0".to_string()),
                    ..Default::default()
                },
                "a/mix.exs".to_string(),
            );
            pkg.package_uid = crate::models::PackageUid::from_raw("uid-a".to_string());
            pkg
        };
        let pkg_b = {
            let mut pkg = crate::models::Package::from_package_data(
                &PackageData {
                    package_type: Some(PackageType::Hex),
                    name: Some("b".to_string()),
                    version: Some("0.1.0".to_string()),
                    purl: Some("pkg:hex/b@0.1.0".to_string()),
                    ..Default::default()
                },
                "b/mix.exs".to_string(),
            );
            pkg.package_uid = crate::models::PackageUid::from_raw("uid-b".to_string());
            pkg
        };

        let output = crate::models::Output {
            summary: None,
            tallies: None,
            tallies_of_key_files: None,
            tallies_by_facet: None,
            headers: vec![],
            packages: vec![pkg_a, pkg_b],
            dependencies: vec![],
            license_detections: vec![],
            files: vec![file_a, file_b, file_orphan],
            license_references: vec![],
            license_rule_references: vec![],
        };
        let schema = Output::from(&output);
        let files = spdx_files(&schema);
        let plans = plan_spdx_packages(
            &schema,
            &files,
            &OutputWriteConfig {
                format: crate::output::OutputFormat::SpdxTv,
                custom_template: None,
                scanned_path: Some("umbrella".to_string()),
            },
        );
        assert_eq!(plans.len(), 3);
        assert_eq!(plans[0].spdx_id, "SPDXRef-Package-1");
        assert_eq!(plans[0].name, "a");
        assert_eq!(plans[0].files.len(), 1);
        assert_eq!(plans[1].spdx_id, "SPDXRef-Package-2");
        assert_eq!(plans[2].spdx_id, "SPDXRef-Package-unassigned");
        assert_eq!(plans[2].files.len(), 1);
    }

    #[test]
    fn spdx_file_license_info_includes_manifest_package_data_detections() {
        let mut file = crate::models::FileInfo::new(
            "Cargo.toml".to_string(),
            "Cargo".to_string(),
            ".toml".to_string(),
            "project/Cargo.toml".to_string(),
            FileType::File,
            None,
            None,
            1,
            None,
            None,
            None,
            None,
            None,
            Vec::new(),
            None,
            Vec::new(),
            Vec::new(),
            Vec::new(),
            Vec::new(),
            Vec::new(),
            Vec::new(),
            Vec::new(),
            Vec::new(),
            Vec::new(),
        );
        file.package_data = vec![PackageData {
            package_type: Some(PackageType::Cargo),
            license_detections: vec![LicenseDetection {
                license_expression: "mit".to_string(),
                license_expression_spdx: "MIT".to_string(),
                matches: vec![crate::models::Match {
                    license_expression: "mit".to_string(),
                    license_expression_spdx: "MIT".to_string(),
                    from_file: Some("project/Cargo.toml".to_string()),
                    start_line: LineNumber::ONE,
                    end_line: LineNumber::ONE,
                    matcher: MatcherKind::Declared,
                    score: MatchScore::MAX,
                    matched_length: Some(1),
                    match_coverage: Some(100.0),
                    rule_relevance: Some(100),
                    rule_identifier: String::new(),
                    rule_url: None,
                    matched_text: Some("MIT".to_string()),
                    referenced_filenames: Some(vec!["LICENSE".to_string()]),
                    matched_text_diagnostics: None,
                }],
                detection_log: vec!["unknown-reference-to-local-file".to_string()],
                identifier: String::new(),
            }],
            ..Default::default()
        }];

        let schema_file = crate::output_schema::OutputFileInfo::from(&file);
        assert_eq!(
            spdx_file_license_info(&schema_file),
            vec!["MIT".to_string()]
        );
    }

    fn output_package_with(package_data: PackageData) -> crate::output_schema::OutputPackage {
        let package =
            crate::models::Package::from_package_data(&package_data, "package.json".to_string());
        crate::output_schema::OutputPackage::from(&package)
    }

    #[test]
    fn spdx_package_license_declared_uses_only_the_declared_spdx_expression() {
        let declared = output_package_with(PackageData {
            package_type: Some(PackageType::Npm),
            declared_license_expression_spdx: Some("MIT".to_string()),
            other_license_expression_spdx: Some("Apache-2.0".to_string()),
            ..Default::default()
        });
        assert_eq!(
            spdx_package_license_declared(Some(&declared)),
            Some("MIT".to_string())
        );

        // Never falls back to detected-but-undeclared evidence, and no package
        // (the synthetic/unassigned SPDX plans) means nothing was declared.
        let undeclared = output_package_with(PackageData {
            package_type: Some(PackageType::Npm),
            other_license_expression_spdx: Some("Apache-2.0".to_string()),
            ..Default::default()
        });
        assert_eq!(spdx_package_license_declared(Some(&undeclared)), None);
        assert_eq!(spdx_package_license_declared(None), None);
    }

    #[test]
    fn spdx_package_license_helpers_reject_invalid_expressions() {
        let malformed = output_package_with(PackageData {
            package_type: Some(PackageType::Npm),
            declared_license_expression_spdx: Some("MIT\" or malformed".to_string()),
            other_license_expression_spdx: Some("not a@@license".to_string()),
            ..Default::default()
        });
        assert_eq!(spdx_package_license_declared(Some(&malformed)), None);
        assert_eq!(spdx_package_license_concluded(Some(&malformed)), None);

        // Invalid declared must not poison a valid other-license fallback.
        let declared_bad_other_ok = output_package_with(PackageData {
            package_type: Some(PackageType::Npm),
            declared_license_expression_spdx: Some("MIT\" or malformed".to_string()),
            other_license_expression_spdx: Some("Apache-2.0".to_string()),
            ..Default::default()
        });
        assert_eq!(
            spdx_package_license_concluded(Some(&declared_bad_other_ok)),
            Some("Apache-2.0".to_string())
        );
    }

    #[test]
    fn spdx_license_rdf_resource_uses_document_namespace_for_license_refs() {
        let ns = "http://spdx.org/spdxdocs/demo";
        assert_eq!(
            spdx_license_rdf_resource("MIT", ns),
            "http://spdx.org/licenses/MIT"
        );
        assert_eq!(
            spdx_license_rdf_resource("LicenseRef-Custom", ns),
            "http://spdx.org/spdxdocs/demo#LicenseRef-Custom"
        );
        assert_eq!(
            spdx_rdf_license_resource(Some("LicenseRef-Custom"), ns),
            "http://spdx.org/spdxdocs/demo#LicenseRef-Custom"
        );
    }

    #[test]
    fn spdx_package_license_concluded_prefers_declared_then_falls_back_to_other() {
        let declared = output_package_with(PackageData {
            package_type: Some(PackageType::Npm),
            declared_license_expression_spdx: Some("MIT".to_string()),
            other_license_expression_spdx: Some("Apache-2.0".to_string()),
            ..Default::default()
        });
        assert_eq!(
            spdx_package_license_concluded(Some(&declared)),
            Some("MIT".to_string())
        );

        let other_only = output_package_with(PackageData {
            package_type: Some(PackageType::Npm),
            other_license_expression_spdx: Some("Apache-2.0".to_string()),
            ..Default::default()
        });
        assert_eq!(
            spdx_package_license_concluded(Some(&other_only)),
            Some("Apache-2.0".to_string())
        );

        let unknown = output_package_with(PackageData {
            package_type: Some(PackageType::Npm),
            ..Default::default()
        });
        assert_eq!(spdx_package_license_concluded(Some(&unknown)), None);
        assert_eq!(spdx_package_license_concluded(None), None);
    }

    #[test]
    fn spdx_file_license_concluded_reuses_the_detected_spdx_expression() {
        let mut file = crate::models::FileInfo::new(
            "notice.c".to_string(),
            "notice".to_string(),
            ".c".to_string(),
            "project/notice.c".to_string(),
            FileType::File,
            None,
            None,
            1,
            None,
            None,
            None,
            None,
            None,
            Vec::new(),
            None,
            Vec::new(),
            Vec::new(),
            Vec::new(),
            Vec::new(),
            Vec::new(),
            Vec::new(),
            Vec::new(),
            Vec::new(),
            Vec::new(),
        );
        file.license_detections = vec![LicenseDetection {
            license_expression: "mit".to_string(),
            license_expression_spdx: "MIT".to_string(),
            matches: vec![],
            detection_log: vec![],
            identifier: String::new(),
        }];

        let schema_file = crate::output_schema::OutputFileInfo::from(&file);
        assert_eq!(
            spdx_file_license_concluded(&schema_file),
            Some("MIT".to_string())
        );

        let mut undetected = file;
        undetected.license_detections = vec![];
        let schema_undetected = crate::output_schema::OutputFileInfo::from(&undetected);
        assert_eq!(spdx_file_license_concluded(&schema_undetected), None);
    }

    #[test]
    fn spdx_rdf_license_resource_only_resolves_a_single_license_id() {
        let ns = "http://spdx.org/spdxdocs/demo";
        assert_eq!(
            spdx_rdf_license_resource(Some("MIT"), ns),
            "http://spdx.org/licenses/MIT"
        );
        // A compound expression would need a nested RDF license-set structure
        // this writer doesn't build, so it honestly reports NOASSERTION.
        assert_eq!(
            spdx_rdf_license_resource(Some("MIT AND Apache-2.0"), ns),
            "http://spdx.org/rdf/terms#noassertion"
        );
        assert_eq!(
            spdx_rdf_license_resource(None, ns),
            "http://spdx.org/rdf/terms#noassertion"
        );
    }
}