-
-
Notifications
You must be signed in to change notification settings - Fork 203
Expand file tree
/
Copy pathtest_d2d.py
More file actions
2601 lines (2286 loc) · 102 KB
/
Copy pathtest_d2d.py
File metadata and controls
2601 lines (2286 loc) · 102 KB
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
# SPDX-License-Identifier: Apache-2.0
#
# http://nexb.com and https://github.com/nexB/scancode.io
# The ScanCode.io software is licensed under the Apache License version 2.0.
# Data generated with ScanCode.io is provided as-is without warranties.
# ScanCode is a trademark of nexB Inc.
#
# You may not use this software except in compliance with the License.
# You may obtain a copy of the License at: http://apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software distributed
# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
# CONDITIONS OF ANY KIND, either express or implied. See the License for the
# specific language governing permissions and limitations under the License.
#
# Data Generated with ScanCode.io is provided on an "AS IS" BASIS, WITHOUT WARRANTIES
# OR CONDITIONS OF ANY KIND, either express or implied. No content created from
# ScanCode.io should be considered or used as legal advice. Consult an Attorney
# for any legal advice.
#
# ScanCode.io is a free software code scanning tool from nexB Inc. and others.
# Visit https://github.com/nexB/scancode.io for support and download.
import io
import json
import sys
import tempfile
import uuid
from dataclasses import asdict
from pathlib import Path
from unittest import mock
from unittest import skipIf
from django.db.utils import DataError
from django.test import TestCase
from scanpipe import pipes
from scanpipe.models import CodebaseRelation
from scanpipe.models import CodebaseResource
from scanpipe.models import Project
from scanpipe.pipes import d2d
from scanpipe.pipes import d2d_config
from scanpipe.pipes import flag
from scanpipe.pipes import jvm
from scanpipe.pipes import scancode
from scanpipe.pipes import symbols
from scanpipe.pipes.input import copy_input
from scanpipe.pipes.input import copy_inputs
from scanpipe.tests import make_resource_directory
from scanpipe.tests import make_resource_file
from scanpipe.tests import package_data1
from scanpipe.tests import package_data2
from scanpipe.tests import resource_data1
class ScanPipeD2DPipesTest(TestCase):
data = Path(__file__).parent.parent / "data"
def setUp(self):
self.project1 = Project.objects.create(name="Analysis")
def test_scanpipe_pipes_d2d_get_inputs(self):
with self.assertRaises(FileNotFoundError) as error:
d2d.get_inputs(self.project1)
self.assertEqual("from* input files not found.", str(error.exception))
_, input_location = tempfile.mkstemp(prefix="from-")
self.project1.copy_input_from(input_location)
with self.assertRaises(FileNotFoundError) as error:
d2d.get_inputs(self.project1)
self.assertEqual("to* input files not found.", str(error.exception))
_, input_location = tempfile.mkstemp(prefix="to-")
self.project1.copy_input_from(input_location)
from_files, to_files = d2d.get_inputs(self.project1)
self.assertEqual(1, len(from_files))
self.assertEqual(1, len(to_files))
_, input_location = tempfile.mkstemp(prefix="from-")
self.project1.copy_input_from(input_location)
_, input_location = tempfile.mkstemp(prefix="to-")
self.project1.copy_input_from(input_location)
from_files, to_files = d2d.get_inputs(self.project1)
self.assertEqual(2, len(from_files))
self.assertEqual(2, len(to_files))
_, input_location = tempfile.mkstemp(prefix="")
self.project1.copy_input_from(input_location)
url_with_fragment = "https://download.url#from"
input_source1 = self.project1.add_input_source(
download_url=url_with_fragment, filename=Path(input_location).name
)
_, input_location = tempfile.mkstemp(prefix="")
self.project1.copy_input_from(input_location)
url_with_fragment = "https://download.url#to"
input_source2 = self.project1.add_input_source(
download_url=url_with_fragment, filename=Path(input_location).name
)
from_files, to_files = d2d.get_inputs(self.project1)
self.assertEqual(3, len(from_files))
self.assertEqual(3, len(to_files))
self.assertIn(input_source1.path, from_files)
self.assertIn(input_source2.path, to_files)
def test_scanpipe_pipes_d2d_get_extracted_path(self):
path = "not/an/extracted/path/"
r1 = make_resource_file(self.project1, path)
expected = "not/an/extracted/path/-extract/"
self.assertEqual(expected, d2d.get_extracted_path(r1))
path = "a.jar-extract/subpath/file.ext"
r2 = make_resource_file(self.project1, path)
expected = "a.jar-extract/subpath/file.ext-extract/"
self.assertEqual(expected, d2d.get_extracted_path(r2))
def test_scanpipe_pipes_d2d_get_extracted_subpath(self):
path = "not/an/extracted/path/"
self.assertEqual(path, d2d.get_extracted_subpath(path))
path = "a.jar-extract/subpath/file.ext"
self.assertEqual("subpath/file.ext", d2d.get_extracted_subpath(path))
path = "a.jar-extract/subpath/b.jar-extract/subpath/file.ext"
self.assertEqual("subpath/file.ext", d2d.get_extracted_subpath(path))
@mock.patch("scanpipe.pipes.purldb.match_resources")
def test_scanpipe_pipes_d2d_match_sha1s_to_purldb(self, mock_match_resource):
to_1 = make_resource_file(
self.project1,
"to/notice.NOTICE",
sha1="4bd631df28995c332bf69d9d4f0f74d7ee089598",
)
resources_by_sha1 = {to_1.sha1: [to_1]}
package_data = package_data1.copy()
package_data_by_purldb_urls = {"example.com/package-instance": package_data}
resource_data = resource_data1.copy()
resource_data["package"] = "example.com/package-instance"
mock_match_resource.return_value = [resource_data]
resources_by_sha1, matched_count, sha1_count = d2d.match_sha1s_to_purldb(
self.project1,
resources_by_sha1,
d2d.match_purldb_resource,
package_data_by_purldb_urls,
)
self.assertFalse(resources_by_sha1)
self.assertEqual(1, matched_count)
self.assertEqual(1, sha1_count)
# Ensure match_purldb_resource was run
package = self.project1.discoveredpackages.get()
self.assertEqual(package_data["purl"], package.purl)
to_1.refresh_from_db()
self.assertEqual(flag.MATCHED_TO_PURLDB_RESOURCE, to_1.status)
self.assertEqual(1, to_1.discovered_packages.count())
to_1_package = to_1.discovered_packages.get()
self.assertEqual(package, to_1_package)
@mock.patch("scanpipe.pipes.purldb.match_packages")
def test_scanpipe_pipes_d2d_match_purldb_resources(self, mock_match_package):
to_1 = make_resource_file(self.project1, "to/package.jar", sha1="abcdef")
to_1.is_archive = True
to_1.save()
# The initial status will be updated to flag.MATCHED_TO_PURLDB_PACKAGE
to_2 = make_resource_file(
self.project1, "to/package.jar-extract/a.class", status=flag.MAPPED
)
to_3 = make_resource_file(self.project1, "to/package.jar-extract/b.class")
package_data = package_data1.copy()
package_data["uuid"] = uuid.uuid4()
package_data["sha1"] = "abcdef"
mock_match_package.return_value = [package_data]
buffer = io.StringIO()
d2d.match_purldb_resources(
self.project1,
extensions=[".jar"],
matcher_func=d2d.match_purldb_package,
logger=buffer.write,
)
expected = (
"Matching 1 .jar resources in PurlDB, using SHA1"
"3 resources matched in PurlDB using 1 SHA1s"
)
self.assertEqual(expected, buffer.getvalue())
package = self.project1.discoveredpackages.get()
self.assertEqual(package_data["name"], package.name)
self.assertNotEqual(package_data["uuid"], package.uuid)
for resource in [to_1, to_2, to_3]:
resource.refresh_from_db()
self.assertEqual(flag.MATCHED_TO_PURLDB_PACKAGE, resource.status)
self.assertEqual(package, resource.discovered_packages.get())
@mock.patch("scanpipe.pipes.purldb.request_get")
def test_scanpipe_pipes_d2d_match_purldb_directories(self, mock_request_get):
to_1 = make_resource_directory(
self.project1,
"to/package.jar-extract",
extra_data={"directory_content": "abcdef"},
)
to_2 = make_resource_file(self.project1, "to/package.jar-extract/a.class")
to_3 = make_resource_file(self.project1, "to/package.jar-extract/b.class")
package_data = package_data1.copy()
package_data["uuid"] = uuid.uuid4()
mock_request_get.side_effect = [
[
{
"fingerprint": "abcdef",
"matched_fingerprint": "abcdef",
"package": "http://private.purldb.io/api/packages/package-id-123",
}
],
package_data,
[],
]
buffer = io.StringIO()
d2d.match_purldb_directories(
self.project1,
logger=buffer.write,
)
expected = (
"Matching 1 directory from to/ in PurlDB1 directory matched in PurlDB"
)
self.assertEqual(expected, buffer.getvalue())
package = self.project1.discoveredpackages.get()
self.assertEqual(package_data["name"], package.name)
self.assertNotEqual(package_data["uuid"], package.uuid)
for resource in [to_1, to_2, to_3]:
resource.refresh_from_db()
self.assertEqual("matched-to-purldb-directory", resource.status)
self.assertEqual(package, resource.discovered_packages.get())
def test_scanpipe_pipes_d2d_get_best_path_matches_same_name(self):
to_1 = CodebaseResource(name="package-1.0.ext", path="to/package-1.0.ext")
to_2 = CodebaseResource(name="package-2.0.ext", path="to/package-2.0.ext")
from_1 = CodebaseResource(name="package-1.0.ext", path="from/package-1.0.ext")
from_2 = CodebaseResource(name="package-2.0.ext", path="from/package-2.0.ext")
matches = [from_1, from_2]
self.assertEqual([from_1], d2d.get_best_path_matches(to_1, matches))
self.assertEqual([from_2], d2d.get_best_path_matches(to_2, matches))
def test_scanpipe_pipes_d2d_get_best_path_matches_extracted_subpath(self):
to_1 = CodebaseResource(path="to/jar-extract/a/package-1.0.ext")
to_2 = CodebaseResource(path="to/jar-extract/a/package-2.0.ext")
from_1 = CodebaseResource(path="from/src/a/package-1.0.ext")
from_2 = CodebaseResource(path="from/src/a/package-2.0.ext")
matches = [from_1, from_2]
self.assertEqual([from_1], d2d.get_best_path_matches(to_1, matches))
self.assertEqual([from_2], d2d.get_best_path_matches(to_2, matches))
def test_scanpipe_pipes_d2d_get_best_path_matches(self):
to_1 = make_resource_file(self.project1, path="to/a/b/c/file.txt")
from_1 = make_resource_file(self.project1, path="from/source/f/i/j/file.txt")
from_2 = make_resource_file(self.project1, path="from/source/a/b/c/file.txt")
from_3 = make_resource_file(self.project1, path="from/q/w/e/file.txt")
matches = [from_1, from_2, from_3]
self.assertEqual([from_2], d2d.get_best_path_matches(to_1, matches))
# Cannot determine the best as only the filename matches
to_2 = make_resource_file(self.project1, path="to/x/y/z/init.jsp.readme")
self.assertEqual(matches, d2d.get_best_path_matches(to_2, matches))
def test_scanpipe_pipes_d2d_map_checksum(self):
sha1 = "abcde"
to_1 = make_resource_file(self.project1, path="to/a/b/c/file.txt", sha1=sha1)
make_resource_file(self.project1, path="from/source/f/i/j/file.txt", sha1=sha1)
from_2 = make_resource_file(
self.project1, path="from/source/a/b/c/file.txt", sha1=sha1
)
# Matchable path but missing sha1 value
make_resource_file(self.project1, path="from/content/a/b/c/file.txt")
make_resource_file(self.project1, path="from/q/w/e/file.txt", sha1=sha1)
buffer = io.StringIO()
d2d.map_checksum(self.project1, "sha1", logger=buffer.write)
expected = "Mapping 1 to/ resources using sha1 against from/ codebase"
self.assertEqual(expected, buffer.getvalue())
self.assertEqual(1, to_1.related_from.count())
relation = to_1.related_from.get()
self.assertEqual("sha1", relation.map_type)
self.assertEqual(from_2, relation.from_resource)
def test_scanpipe_pipes_d2d_flag_processed_archives(self):
to_archive = make_resource_file(
self.project1, path="to/archive.lpkg", is_archive=True
)
make_resource_directory(
self.project1, path="to/archive.lpkg-extract", status=flag.IGNORED_DIRECTORY
)
to_archive_embedded = make_resource_file(
self.project1,
path="to/archive.lpkg-extract/embedded-archive.lpkg",
is_archive=True,
)
make_resource_directory(
self.project1,
path="to/archive.lpkg-extract/embedded-archive.lpkg-extract",
status=flag.IGNORED_DIRECTORY,
)
make_resource_file(
self.project1,
path="to/archive.lpkg-extract/file1.txt",
status=flag.MATCHED_TO_PURLDB_RESOURCE,
)
make_resource_file(
self.project1,
path="to/archive.lpkg-extract/file2.txt",
status=flag.MATCHED_TO_PURLDB_RESOURCE,
)
resource1 = make_resource_file(
self.project1,
path="to/archive.lpkg-extract/embedded-archive.lpkg-extract/file3.txt",
status=flag.MATCHED_TO_PURLDB_RESOURCE,
)
d2d.flag_processed_archives(self.project1)
to_archive_embedded.refresh_from_db()
self.assertEqual(flag.ARCHIVE_PROCESSED, to_archive_embedded.status)
to_archive.refresh_from_db()
self.assertEqual(flag.ARCHIVE_PROCESSED, to_archive.status)
to_archive_embedded.update(status="")
resource1.update(status="")
d2d.flag_processed_archives(self.project1)
to_archive_embedded.refresh_from_db()
self.assertEqual("", to_archive_embedded.status)
def test_scanpipe_pipes_d2d_map_java_to_class(self):
from1 = make_resource_file(
self.project1,
path="from/flume-ng-node-1.9.0-sources.jar-extract/org/apache/flume/node/"
"AbstractConfigurationProvider.java",
extra_data={"java_package": "org.apache.flume.node"},
)
from2 = make_resource_file(
self.project1,
path="from/flume-ng-node-1.9.0-sources.jar-extract/org/apache/flume/WRONG/"
"Application.java",
extra_data={"java_package": "org.apache.flume.WRONG"},
)
to1 = make_resource_file(
self.project1,
path="to/flume-ng-node-1.9.0.jar-extract/org/apache/flume/node/"
"AbstractConfigurationProvider$ChannelComponent.class",
)
to2 = make_resource_file(
self.project1,
path="to/flume-ng-node-1.9.0.jar-extract/org/apache/flume/node/"
"AbstractConfigurationProvider.class",
)
to3 = make_resource_file(
self.project1,
path="to/flume-ng-node-1.9.0.jar-extract/org/apache/flume/node/"
"Application.class",
)
buffer = io.StringIO()
d2d.map_jvm_to_class(
self.project1, logger=buffer.write, jvm_lang=jvm.JavaLanguage
)
expected = "Mapping 3 .class (or other deployed file) resources to 2 ('.java',)"
self.assertIn(expected, buffer.getvalue())
self.assertEqual(2, self.project1.codebaserelations.count())
r1 = self.project1.codebaserelations.get(to_resource=to1, from_resource=from1)
self.assertEqual("java_to_class", r1.map_type)
expected = {"from_source_root": "from/flume-ng-node-1.9.0-sources.jar-extract/"}
self.assertEqual(expected, r1.extra_data)
r2 = self.project1.codebaserelations.get(to_resource=to2, from_resource=from1)
self.assertEqual("java_to_class", r2.map_type)
expected = {"from_source_root": "from/flume-ng-node-1.9.0-sources.jar-extract/"}
self.assertEqual(expected, r2.extra_data)
no_relations = self.project1.codebaseresources.has_no_relation()
self.assertIn(from2, no_relations)
self.assertIn(to3, no_relations)
to3.refresh_from_db()
self.assertEqual("", to3.status)
def test_scanpipe_pipes_d2d_map_java_to_class_with_java_in_deploy(self):
input_dir = self.project1.input_path
# "from-Baz.zip" contains Baz.java
# "to-Baz.jar" contains Baz.java and Baz.class
input_resources = [
self.data / "d2d" / "find_java_packages" / "from-Baz.zip",
self.data / "d2d" / "find_java_packages" / "to-Baz.jar",
]
copy_inputs(input_resources, input_dir)
self.from_files, self.to_files = d2d.get_inputs(self.project1)
inputs_with_codebase_path_destination = [
(self.from_files, self.project1.codebase_path / d2d.FROM),
(self.to_files, self.project1.codebase_path / d2d.TO),
]
for input_files, codebase_path in inputs_with_codebase_path_destination:
for input_file_path in input_files:
scancode.extract_archive(input_file_path, codebase_path)
scancode.extract_archives(
self.project1.codebase_path,
recurse=True,
)
pipes.collect_and_create_codebase_resources(self.project1)
buffer = io.StringIO()
d2d.map_checksum(
project=self.project1, checksum_field="sha1", logger=buffer.write
)
d2d.find_jvm_packages(
self.project1, jvm_lang=jvm.JavaLanguage, logger=buffer.write
)
expected = "Finding java packages for 1 ('.java',) resources."
self.assertIn(expected, buffer.getvalue())
# Now run map_java_to_class
d2d.map_jvm_to_class(
self.project1, logger=buffer.write, jvm_lang=jvm.JavaLanguage
)
expected = "Mapping 1 .class (or other deployed file) resources to 1 ('.java',)"
self.assertIn(expected, buffer.getvalue())
def test_scanpipe_pipes_d2d_map_grammar_to_class(self):
from1 = make_resource_file(
self.project1,
path="from/antlr4-4.5.1-beta-1/tool/src/org/antlr/v4/parse/BlockSetTransformer.g",
extra_data={"grammar_package": "org.antlr.v4.parse"},
)
to1 = make_resource_file(
self.project1,
path="to/org/antlr/v4/parse/BlockSetTransformer.class",
)
buffer = io.StringIO()
d2d.map_jvm_to_class(
self.project1, logger=buffer.write, jvm_lang=jvm.GrammarLanguage
)
expected = (
"Mapping 1 .class (or other deployed file) resources to 1 ('.g', '.g4')"
)
self.assertIn(expected, buffer.getvalue())
self.assertEqual(1, self.project1.codebaserelations.count())
r1 = self.project1.codebaserelations.get(to_resource=to1, from_resource=from1)
self.assertEqual("grammar_to_class", r1.map_type)
expected = {"from_source_root": "from/antlr4-4.5.1-beta-1/tool/src/"}
self.assertEqual(expected, r1.extra_data)
def test_scanpipe_pipes_d2d_map_xtend_to_class(self):
from1 = make_resource_file(
self.project1,
path="from/org.openhab.binding.urtsi/src/main/java/org/openhab/"
+ "binding/urtsi/internal/UrtsiDevice.xtend",
extra_data={"xtend_package": "org.openhab.binding.urtsi.internal"},
)
to1 = make_resource_file(
self.project1,
path="to/org.openhab.binding.urtsi-1.6.2.jar-extract/org/"
+ "openhab/binding/urtsi/internal/UrtsiDevice.class",
)
buffer = io.StringIO()
d2d.map_jvm_to_class(
self.project1, logger=buffer.write, jvm_lang=jvm.XtendLanguage
)
expected = (
"Mapping 1 .class (or other deployed file) resources to 1 ('.xtend',)"
)
self.assertIn(expected, buffer.getvalue())
self.assertEqual(1, self.project1.codebaserelations.count())
r1 = self.project1.codebaserelations.get(to_resource=to1, from_resource=from1)
self.assertEqual("xtend_to_class", r1.map_type)
expected = {"from_source_root": "from/org.openhab.binding.urtsi/src/main/java/"}
self.assertEqual(expected, r1.extra_data)
def test_scanpipe_pipes_d2d_map_java_to_class_no_java(self):
make_resource_file(self.project1, path="to/Abstract.class")
buffer = io.StringIO()
d2d.map_jvm_to_class(
self.project1, logger=buffer.write, jvm_lang=jvm.JavaLanguage
)
expected = "No ('.java',) resources to map."
self.assertIn(expected, buffer.getvalue())
def test_scanpipe_pipes_d2d_java_ignore_pattern(self):
make_resource_file(self.project1, path="to/module-info.class")
make_resource_file(self.project1, path="to/META-INF/MANIFEST.MF")
make_resource_file(self.project1, path="to/test.class")
make_resource_file(self.project1, path="to/META-INF/others.txt")
make_resource_file(
self.project1, path="to/META-INF/spring-configuration-metadata.json"
)
make_resource_file(self.project1, path="to/OSGI-INF/test.xml")
make_resource_file(self.project1, path="to/OSGI-INF/test.json")
make_resource_file(self.project1, path="to/OSGI-INF/test.class")
buffer = io.StringIO()
java_config = d2d_config.get_ecosystem_config(ecosystem="Java")
d2d.ignore_unmapped_resources_from_config(
project=self.project1,
patterns_to_ignore=java_config.deployed_resource_path_exclusions,
logger=buffer.write,
)
expected = "Ignoring 6 to/ resources with ecosystem specific configurations."
self.assertIn(expected, buffer.getvalue())
def test_scanpipe_pipes_d2d_map_jar_to_java_source(self):
from1 = make_resource_file(
self.project1,
path="from/flume-ng-node-1.9.0-sources.jar-extract/org/apache/flume/node/"
"AbstractConfigurationProvider.java",
extra_data={"java_package": "org.apache.flume.node"},
)
from2 = make_resource_file(
self.project1,
path="from/flume-ng-node-1.9.0-sources.jar-extract",
)
to1 = make_resource_file(
self.project1,
path="to/flume-ng-node-1.9.0.jar-extract/org/apache/flume/node/"
"AbstractConfigurationProvider.class",
)
make_resource_file(
self.project1,
path="to/flume-ng-node-1.9.0.jar-extract/META-INF/MANIFEST.MF",
)
to_jar = make_resource_file(
self.project1,
path="to/flume-ng-node-1.9.0.jar",
)
buffer = io.StringIO()
d2d.map_jvm_to_class(
self.project1, logger=buffer.write, jvm_lang=jvm.JavaLanguage
)
relation = self.project1.codebaserelations.get()
self.assertEqual(from1, relation.from_resource)
self.assertEqual(to1, relation.to_resource)
self.assertEqual("java_to_class", relation.map_type)
expected = {"from_source_root": "from/flume-ng-node-1.9.0-sources.jar-extract/"}
self.assertEqual(expected, relation.extra_data)
buffer = io.StringIO()
with self.assertNumQueries(6):
d2d.map_jar_to_jvm_source(
self.project1, logger=buffer.write, jvm_lang=jvm.JavaLanguage
)
expected = "Mapping 1 .jar resources using map_jar_to_source"
self.assertIn(expected, buffer.getvalue())
self.assertEqual(2, self.project1.codebaserelations.count())
relation = self.project1.codebaserelations.get(map_type="jar_to_source")
self.assertEqual(from2, relation.from_resource)
self.assertEqual(to_jar, relation.to_resource)
def test_scanpipe_pipes_d2d_map_groovy_to_class(self):
from1 = make_resource_file(
self.project1,
path="from/project/test.groovy",
extra_data={"groovy_package": "project"},
)
to1 = make_resource_file(
self.project1,
path="to/project/test.class",
)
buffer = io.StringIO()
d2d.map_jvm_to_class(
self.project1, logger=buffer.write, jvm_lang=jvm.GroovyLanguage
)
expected = (
"Mapping 1 .class (or other deployed file) resources to 1 ('.groovy',)"
)
self.assertIn(expected, buffer.getvalue())
self.assertEqual(1, self.project1.codebaserelations.count())
r1 = self.project1.codebaserelations.get(to_resource=to1, from_resource=from1)
self.assertEqual("groovy_to_class", r1.map_type)
expected = {"from_source_root": "from/"}
self.assertEqual(expected, r1.extra_data)
def test_scanpipe_pipes_d2d_map_aspectj_to_class(self):
from1 = make_resource_file(
self.project1,
path="from/project/test.aj",
extra_data={"aspectj_package": "project"},
)
to1 = make_resource_file(
self.project1,
path="to/project/test.class",
)
buffer = io.StringIO()
d2d.map_jvm_to_class(
self.project1, logger=buffer.write, jvm_lang=jvm.AspectJLanguage
)
expected = "Mapping 1 .class (or other deployed file) resources to 1 ('.aj',)"
self.assertIn(expected, buffer.getvalue())
self.assertEqual(1, self.project1.codebaserelations.count())
r1 = self.project1.codebaserelations.get(to_resource=to1, from_resource=from1)
self.assertEqual("aspectj_to_class", r1.map_type)
expected = {"from_source_root": "from/"}
self.assertEqual(expected, r1.extra_data)
def test_scanpipe_pipes_d2d_map_clojure_to_class(self):
from1 = make_resource_file(
self.project1,
path="from/project/test.clj",
extra_data={"clojure_package": "project"},
)
to1 = make_resource_file(
self.project1,
path="to/project/test.class",
)
buffer = io.StringIO()
d2d.map_jvm_to_class(
self.project1, logger=buffer.write, jvm_lang=jvm.ClojureLanguage
)
expected = "Mapping 1 .class (or other deployed file) resources to 1 ('.clj',)"
self.assertIn(expected, buffer.getvalue())
self.assertEqual(1, self.project1.codebaserelations.count())
r1 = self.project1.codebaserelations.get(to_resource=to1, from_resource=from1)
self.assertEqual("clojure_to_class", r1.map_type)
expected = {"from_source_root": "from/"}
self.assertEqual(expected, r1.extra_data)
def test_scanpipe_pipes_d2d_map_scala_to_class(self):
from1 = make_resource_file(
self.project1,
path="from/tastyquery/Annotations.scala",
extra_data={"scala_package": "tastyquery"},
)
to1 = make_resource_file(
self.project1,
path="to/tastyquery/Annotations.tasty",
)
to2 = make_resource_file(
self.project1,
path="to/tastyquery/Annotations.class",
)
buffer = io.StringIO()
d2d.map_jvm_to_class(
self.project1, logger=buffer.write, jvm_lang=jvm.ScalaLanguage
)
expected = (
"Mapping 2 .class (or other deployed file) resources to 1 ('.scala',)"
)
self.assertIn(expected, buffer.getvalue())
self.assertEqual(2, self.project1.codebaserelations.count())
r1 = self.project1.codebaserelations.get(to_resource=to1, from_resource=from1)
self.assertEqual("scala_to_class", r1.map_type)
expected = {"from_source_root": "from/"}
self.assertEqual(expected, r1.extra_data)
r2 = self.project1.codebaserelations.get(to_resource=to2, from_resource=from1)
self.assertEqual("scala_to_class", r2.map_type)
expected = {"from_source_root": "from/"}
self.assertEqual(expected, r2.extra_data)
def test_scanpipe_pipes_d2d_map_jar_to_scala_source(self):
from1 = make_resource_file(
self.project1,
path="from/flume-ng-node-1.9.0-sources.jar-extract/org/apache/flume/node/"
"AbstractConfigurationProvider.scala",
extra_data={"scala_package": "org.apache.flume.node"},
)
from2 = make_resource_file(
self.project1,
path="from/flume-ng-node-1.9.0-sources.jar-extract",
)
to1 = make_resource_file(
self.project1,
path="to/flume-ng-node-1.9.0.jar-extract/org/apache/flume/node/"
"AbstractConfigurationProvider.class",
)
make_resource_file(
self.project1,
path="to/flume-ng-node-1.9.0.jar-extract/META-INF/MANIFEST.MF",
)
to_jar = make_resource_file(
self.project1,
path="to/flume-ng-node-1.9.0.jar",
)
buffer = io.StringIO()
d2d.map_jvm_to_class(
self.project1, logger=buffer.write, jvm_lang=jvm.ScalaLanguage
)
relation = self.project1.codebaserelations.get()
self.assertEqual(from1, relation.from_resource)
self.assertEqual(to1, relation.to_resource)
self.assertEqual("scala_to_class", relation.map_type)
expected = {"from_source_root": "from/flume-ng-node-1.9.0-sources.jar-extract/"}
self.assertEqual(expected, relation.extra_data)
buffer = io.StringIO()
with self.assertNumQueries(6):
d2d.map_jar_to_jvm_source(
self.project1, logger=buffer.write, jvm_lang=jvm.ScalaLanguage
)
expected = "Mapping 1 .jar resources using map_jar_to_source"
self.assertIn(expected, buffer.getvalue())
self.assertEqual(2, self.project1.codebaserelations.count())
relation = self.project1.codebaserelations.get(map_type="jar_to_source")
self.assertEqual(from2, relation.from_resource)
self.assertEqual(to_jar, relation.to_resource)
def test_scanpipe_pipes_d2d_scala_ignore_pattern(self):
make_resource_file(self.project1, path="to/META-INF/MANIFEST.MF")
make_resource_file(self.project1, path="to/test.class")
make_resource_file(self.project1, path="to/META-INF/others.txt")
buffer = io.StringIO()
scala_config = d2d_config.get_ecosystem_config(ecosystem="Scala")
d2d.ignore_unmapped_resources_from_config(
project=self.project1,
patterns_to_ignore=scala_config.deployed_resource_path_exclusions,
logger=buffer.write,
)
expected = "Ignoring 2 to/ resources with ecosystem specific configurations."
self.assertIn(expected, buffer.getvalue())
def test_scanpipe_pipes_d2d_map_jar_to_kotlin_source(self):
from1 = make_resource_file(
self.project1,
path="from/flume-ng-node-1.9.0-sources.jar-extract/org/apache/flume/node/"
"AbstractConfigurationProvider.kt",
extra_data={"kotlin_package": "org.apache.flume.node"},
)
from2 = make_resource_file(
self.project1,
path="from/flume-ng-node-1.9.0-sources.jar-extract",
)
to1 = make_resource_file(
self.project1,
path="to/flume-ng-node-1.9.0.jar-extract/org/apache/flume/node/"
"AbstractConfigurationProvider.class",
)
make_resource_file(
self.project1,
path="to/flume-ng-node-1.9.0.jar-extract/META-INF/MANIFEST.MF",
)
to_jar = make_resource_file(
self.project1,
path="to/flume-ng-node-1.9.0.jar",
)
buffer = io.StringIO()
d2d.map_jvm_to_class(
self.project1, logger=buffer.write, jvm_lang=jvm.KotlinLanguage
)
relation = self.project1.codebaserelations.get()
self.assertEqual(from1, relation.from_resource)
self.assertEqual(to1, relation.to_resource)
self.assertEqual("kotlin_to_class", relation.map_type)
expected = {"from_source_root": "from/flume-ng-node-1.9.0-sources.jar-extract/"}
self.assertEqual(expected, relation.extra_data)
buffer = io.StringIO()
with self.assertNumQueries(6):
d2d.map_jar_to_jvm_source(
self.project1, logger=buffer.write, jvm_lang=jvm.KotlinLanguage
)
expected = "Mapping 1 .jar resources using map_jar_to_source"
self.assertIn(expected, buffer.getvalue())
self.assertEqual(2, self.project1.codebaserelations.count())
relation = self.project1.codebaserelations.get(map_type="jar_to_source")
self.assertEqual(from2, relation.from_resource)
self.assertEqual(to_jar, relation.to_resource)
def test_scanpipe_pipes_d2d_kotlin_ignore_pattern(self):
make_resource_file(self.project1, path="to/META-INF/test.knm")
make_resource_file(self.project1, path="to/test.class")
make_resource_file(
self.project1, path="to/META-INF/kotlin-project-structure-metadata.json"
)
buffer = io.StringIO()
kotlin_config = d2d_config.get_ecosystem_config(ecosystem="Kotlin")
d2d.ignore_unmapped_resources_from_config(
project=self.project1,
patterns_to_ignore=kotlin_config.deployed_resource_path_exclusions,
logger=buffer.write,
)
expected = "Ignoring 2 to/ resources with ecosystem specific configurations."
self.assertIn(expected, buffer.getvalue())
def test_scanpipe_pipes_d2d_map_jar_to_source_works_for_jar(self):
from1 = make_resource_file(
self.project1,
path="from/org/apache/logging/log4j/core/util/SystemClock.java",
extra_data={"java_package": "org.apache.logging.log4j.core.util"},
)
to1 = make_resource_file(
self.project1,
path=(
"to/META-INF/versions/9/org/apache/logging/log4j/core/util/"
"SystemClock.class"
),
)
to2 = make_resource_file(
self.project1,
path="to/org/apache/logging/log4j/core/util/SystemClock.class",
)
d2d.map_jvm_to_class(self.project1, jvm_lang=jvm.JavaLanguage)
expected = [
(from1.path, to1.path, "java_to_class"),
(from1.path, to2.path, "java_to_class"),
]
results = list(
self.project1.codebaserelations.all().values_list(
"from_resource__path", "to_resource__path", "map_type"
)
)
self.assertEqual(expected, results)
def test_scanpipe_pipes_d2d_get_indexable_qualified_java_paths_from_values_yields_correct_paths( # NOQA: E501
self,
):
resource_values = [
(
1,
"SystemClock.java",
{"java_package": "org.apache.logging.log4j.core.util"},
),
(
2,
"SystemClock2.java",
{"java_package": "org.apache.logging.log4j.core.util"},
),
]
expected = [
(1, "org/apache/logging/log4j/core/util/SystemClock.java"),
(2, "org/apache/logging/log4j/core/util/SystemClock2.java"),
]
results = list(
jvm.JavaLanguage.get_indexable_qualified_paths_from_values(resource_values)
)
self.assertEqual(expected, results)
def test_scanpipe_pipes_d2d_map_path(self):
from1 = make_resource_file(
self.project1,
path="from/core/src/main/org/apache/bar/file.ext",
)
make_resource_file(
self.project1,
path="from/core/src/main/org/apache2/bar/file.ext",
)
make_resource_file(
self.project1,
path="from/core/src/main/org/apache/bar/file2.ext",
)
to1 = make_resource_file(
self.project1,
path="to/apache/bar/file.ext",
)
make_resource_file(
self.project1,
path="to/apache/foo/file.ext",
)
buffer = io.StringIO()
d2d.map_path(self.project1, logger=buffer.write)
expected = "Mapping 2 to/ resources using path map against from/ codebase"
self.assertIn(expected, buffer.getvalue())
file_name_too_many = self.project1.codebaseresources.get(
path="to/apache/foo/file.ext"
)
self.assertEqual(1, self.project1.codebaserelations.count())
relation = self.project1.codebaserelations.get()
self.assertEqual(from1, relation.from_resource)
self.assertEqual(to1, relation.to_resource)
self.assertEqual("path", relation.map_type)
self.assertEqual({"path_score": "3/3"}, relation.extra_data)
self.assertNotEqual("too-many-maps", file_name_too_many.status)
def test_scanpipe_pipes_d2d_find_java_packages(self):
input_locations = [
self.data / "d2d" / "find_java_packages" / "Foo.java",
self.data / "d2d" / "find_java_packages" / "Baz.java",
self.data / "d2d" / "find_java_packages" / "Baz.class",
]
from_dir = self.project1.codebase_path / "from"
from_dir.mkdir()
copy_inputs(input_locations, from_dir)
pipes.collect_and_create_codebase_resources(self.project1)
buffer = io.StringIO()
d2d.find_jvm_packages(
self.project1, jvm_lang=jvm.JavaLanguage, logger=buffer.write
)
expected = "Finding java packages for 2 ('.java',) resources."
self.assertEqual(expected, buffer.getvalue())
expected = [
{"extra_data": {}, "path": "from"},
{"extra_data": {}, "path": "from/Baz.class"},
{"extra_data": {"java_package": "org.apache.biz"}, "path": "from/Baz.java"},
{"extra_data": {"java_package": "org.apache.foo"}, "path": "from/Foo.java"},
]
results = list(self.project1.codebaseresources.values("path", "extra_data"))
self.assertEqual(expected, results)
def test_scanpipe_pipes_d2d_map_javascript_skips_dot_file(self):
make_resource_file(
self.project1,
path=(
"from/project.tar.zst/modules/apps/adaptive-media/"
"adaptive-media-web/src/main/resources/META-INF/resources/"
"adaptive_media/js/.main.js"
),
)
d2d.map_javascript(self.project1)
self.assertEqual(0, self.project1.codebaserelations.count())
def test_scanpipe_pipes_d2d_map_javascript(self):
to_dir = (
self.project1.codebase_path / "to/project.tar.zst-extract/osgi/marketplace/"
"intelligent robotics platform.lpkg-extract/"
"com.example.adaptive.media.web-0.0.5.jar-extract/META-INF/"
"resources/adaptive_media/js"
)
to_dir.mkdir(parents=True)
resource_files = [
self.data / "d2d-javascript" / "to" / "main.js.map",
self.data / "d2d-javascript" / "to" / "main.js",
]
copy_inputs(resource_files, to_dir)
from_input_location = self.data / "d2d-javascript" / "from" / "main.js"
from_dir = (
self.project1.codebase_path
/ "from/project.tar.zst/modules/apps/adaptive-media/"
"adaptive-media-web/src/main/resources/META-INF/resources/"
"adaptive_media/js"
)
from_dir.mkdir(parents=True)
copy_input(from_input_location, from_dir)
pipes.collect_and_create_codebase_resources(self.project1)
from_resource = self.project1.codebaseresources.get(
path=(
"from/project.tar.zst/modules/apps/adaptive-media/"
"adaptive-media-web/src/main/resources/META-INF/resources/"
"adaptive_media/js/main.js"
)
)
buffer = io.StringIO()
d2d.map_javascript(self.project1, logger=buffer.write)
expected = (
"Mapping 1 .map resources using javascript map against from/ codebase."
)
self.assertIn(expected, buffer.getvalue())