-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathtest_model.py
1375 lines (1154 loc) · 51.4 KB
/
test_model.py
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
#!/usr/bin/env python
# -*- coding: utf8 -*-
# ============================================================================
# Copyright (c) nexB Inc. http://www.nexb.com/ - All rights reserved.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.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.
# ============================================================================
import io
import json
import os
import posixpath
import shutil
import unittest
from unittest import mock
import saneyaml
from attributecode import CRITICAL
from attributecode import ERROR
from attributecode import INFO
from attributecode import WARNING
from attributecode import Error
from attributecode import model
from attributecode.util import add_unc, norm, on_windows
from attributecode.util import load_csv
from attributecode.util import to_posix
from attributecode.util import replace_tab_with_spaces
from testing_utils import extract_test_loc
from testing_utils import get_temp_dir
from testing_utils import get_temp_file
from testing_utils import get_test_loc
def check_csv(expected, result, regen=False, fix_cell_linesep=False):
"""
Assert that the contents of two CSV files locations `expected` and
`result` are equal.
"""
if regen:
shutil.copyfile(result, expected)
expected = sorted([sorted(d.items()) for d in load_csv(expected)])
result = [d.items() for d in load_csv(result)]
if fix_cell_linesep:
result = [list(fix_crlf(items)) for items in result]
result = sorted(sorted(items) for items in result)
assert expected == result
def fix_crlf(items):
"""
Hackish... somehow the CVS returned on Windows is sometimes using a backward
linesep convention:
instead of LF inside cells and CRLF at EOL,
they use CRLF everywhere.
This is fixing this until we find can why
"""
for key, value in items:
if isinstance(value, str) and '\r\n' in value:
value = value.replace('\r\n', '\n')
yield key, value
def check_json(expected, result):
"""
Assert that the contents of two JSON files are equal.
"""
with open(expected) as e:
expected = json.load(e, object_pairs_hook=dict)
with open(result) as r:
result = json.load(r, object_pairs_hook=dict)
assert expected == result
def get_test_content(test_location):
"""
Read file at test_location and return a unicode string.
"""
return get_unicode_content(get_test_loc(test_location))
def get_unicode_content(location):
"""
Read file at location and return a unicode string.
"""
with open(location, encoding='utf-8', errors='replace') as doc:
return doc.read()
class FieldTest(unittest.TestCase):
def test_Field_init(self):
model.Field()
model.StringField()
model.ListField()
model.UrlField()
model.BooleanField()
model.PathField()
model.FileTextField()
model.PackageUrlField()
def test_empty_Field_has_no_content(self):
field = model.Field()
assert not field.has_content
def test_empty_Field_has_default_value(self):
field = model.Field()
assert '' == field.value
def test_PathField_check_location(self):
test_file = 'license.LICENSE'
field = model.PathField(name='f', value=test_file, present=True)
base_dir = get_test_loc('test_model/base_dir')
errors = field.validate(base_dir=base_dir)
expected_errrors = []
assert expected_errrors == errors
result = field.value[test_file]
expected = add_unc(posixpath.join(to_posix(base_dir), test_file))
assert expected == result
def test_PathField_check_missing_location(self):
test_file = 'does.not.exist'
field = model.PathField(name='f', value=test_file, present=True)
base_dir = get_test_loc('test_model/base_dir')
errors = field.validate(base_dir=base_dir)
file_path = posixpath.join(base_dir, test_file)
err_msg = 'Field f: Path %s not found' % file_path
expected_errors = [
Error(CRITICAL, err_msg)]
assert expected_errors == errors
result = field.value[test_file]
assert None == result
def test_TextField_loads_file(self):
field = model.FileTextField(
name='f', value='license.LICENSE', present=True)
base_dir = get_test_loc('test_model/base_dir')
errors = field.validate(base_dir=base_dir)
assert [] == errors
expected = {'license.LICENSE': 'some license text'}
assert expected == field.value
def test_PackageUrlField_is_valid_url(self):
assert model.PackageUrlField.is_valid_purl('pkg:pypi/[email protected]')
def test_PackageUrlField_is_valid_url_no_version(self):
assert model.PackageUrlField.is_valid_purl('pkg:pypi/saneyaml')
def test_UrlField_is_valid_url(self):
assert model.UrlField.is_valid_url('http://www.google.com')
def test_UrlField_is_valid_url_not_starting_with_www(self):
assert model.UrlField.is_valid_url('https://nexb.com')
assert model.UrlField.is_valid_url(
'http://archive.apache.org/dist/httpcomponents/commons-httpclient/2.0/source/commons-httpclient-2.0-alpha2-src.tar.gz')
assert model.UrlField.is_valid_url(
'http://de.wikipedia.org/wiki/Elf (Begriffsklärung)')
assert model.UrlField.is_valid_url('http://nothing_here.com')
def test_UrlField_is_valid_url_no_schemes(self):
assert not model.UrlField.is_valid_url('google.com')
assert not model.UrlField.is_valid_url('www.google.com')
assert not model.UrlField.is_valid_url('')
def test_UrlField_is_valid_url_not_ends_with_com(self):
assert model.UrlField.is_valid_url('http://www.google')
def test_UrlField_is_valid_url_ends_with_slash(self):
assert model.UrlField.is_valid_url('http://www.google.co.uk/')
def test_UrlField_is_valid_url_empty_URL(self):
assert not model.UrlField.is_valid_url('http:')
def check_validate(self, field_class, value, expected, expected_errors):
"""
Check field values after validation
"""
field = field_class(name='s', value=value, present=True)
# check that validate can be applied multiple times without side effects
for _ in range(2):
errors = field.validate()
assert expected_errors == errors
assert expected == field.value
def test_StringField_validate_trailing_spaces_are_removed(self):
field_class = model.StringField
value = 'trailin spaces '
expected = 'trailin spaces'
self.check_validate(field_class, value, expected, expected_errors=[])
def test_ListField_contains_list_after_validate(self):
value = 'string'
field_class = model.ListField
expected = [value]
self.check_validate(field_class, value, expected, expected_errors=[])
def test_ListField_contains_stripped_strings_after_validate(self):
value = '''first line
second line '''
field_class = model.ListField
expected = ['first line', 'second line']
self.check_validate(field_class, value, expected, expected_errors=[])
def test_PathField_contains_stripped_strings_after_validate(self):
value = '''first line
second line '''
field_class = model.ListField
expected = ['first line', 'second line']
self.check_validate(field_class, value, expected, expected_errors=[])
def test_PathField_contains_dict_after_validate(self):
value = 'string'
field_class = model.PathField
expected = dict([('string', None)])
expected_errors = [
Error(
ERROR, 'Field s: Unable to verify path: string: No base directory provided')
]
self.check_validate(field_class, value, expected, expected_errors)
def test_SingleLineField_has_errors_if_multiline(self):
value = '''line1
line2'''
field_class = model.SingleLineField
expected = value
expected_errors = [
Error(ERROR, 'Field s: Cannot span multiple lines: line1\n line2')]
self.check_validate(field_class, value, expected, expected_errors)
class YamlParseTest(unittest.TestCase):
maxDiff = None
def test_saneyaml_load_can_parse_simple_fields(self):
test = get_test_content('test_model/parse/basic.about')
result = saneyaml.load(test)
expected = [
('single_line', 'optional'),
('other_field', 'value'),
]
assert expected == list(result.items())
def test_saneyaml_load_does_not_convert_to_crlf(self):
test = get_test_content('test_model/crlf/about.ABOUT')
result = saneyaml.load(test)
expected = [
(u'about_resource', u'.'),
(u'name', u'pytest'),
(u'description', u'first line\nsecond line\nthird line\n'),
(u'copyright', u'copyright')
]
assert expected == list(result.items())
def test_saneyaml_load_can_parse_continuations(self):
test = get_test_content('test_model/parse/continuation.about')
result = saneyaml.load(test)
expected = [
('single_line', 'optional'),
('other_field', 'value'),
(u'multi_line', u'some value and more and yet more')
]
assert expected == list(result.items())
def test_saneyaml_load_can_handle_multiline_texts_and_strips_text_fields(self):
test = get_test_content('test_model/parse/complex.about')
result = saneyaml.load(test)
expected = [
('single_line', 'optional'),
('other_field', 'value'),
('multi_line', 'some value and more and yet more'),
('yetanother', 'sdasd')]
assert expected == list(result.items())
def test_saneyaml_load_can_parse_verbatim_text_unstripped(self):
test = get_test_content('test_model/parse/continuation_verbatim.about')
result = saneyaml.load(test)
expected = [
(u'single_line', u'optional'),
(u'other_field', u'value'),
(u'multi_line', u'some value \n and more \n and yet more \n \n')
]
assert expected == list(result.items())
def test_saneyaml_load_can_parse_verbatim_tab_text_unstripped(self):
test = get_test_content(
'test_model/parse/continuation_verbatim_with_tab.about')
data = replace_tab_with_spaces(test)
result = saneyaml.load(data)
expected = [
(u'single_line', u'optional'),
(u'other_field', u'value'),
(u'multi_line', u'This is a long description\nwith tab.\n')
]
assert expected == list(result.items())
def test_saneyaml_load_report_error_for_invalid_field_name(self):
test = get_test_content('test_model/parse/invalid_names.about')
try:
saneyaml.load(test)
self.fail('Exception not raised')
except Exception:
pass
def test_saneyaml_dangling_text_is_not_an_invalid_continuation(self):
test = get_test_content('test_model/parse/invalid_continuation.about')
result = saneyaml.load(test)
expected = [
(u'single_line', u'optional'),
(u'other_field', u'value'),
(u'multi_line', u'some value and more\ninvalid continuation2')
]
assert expected == list(result.items())
def test_saneyaml_load_accepts_unicode_keys_and_values(self):
test = get_test_content(
'test_model/parse/non_ascii_field_name_value.about')
result = saneyaml.load(test)
expected = [
('name', 'name'),
('about_resource', '.'),
('owner', 'Matías Aguirre'),
(u'Matías', u'unicode field name')
]
assert expected == list(result.items())
def test_saneyaml_load_accepts_blank_lines_and_spaces_in_field_names(self):
test = '''
name: test space
version: 0.7.0
about_resource: about.py
field with spaces: This is a test case for field with spaces
'''
result = saneyaml.load(test)
expected = [
('name', 'test space'),
('version', '0.7.0'),
('about_resource', 'about.py'),
(u'field with spaces', u'This is a test case for field with spaces'),
]
assert expected == list(result.items())
def test_saneyaml_loads_blank_lines_and_lines_without_no_colon(self):
test = '''
name: no colon test
test
version: 0.7.0
about_resource: about.py
test with no colon
'''
try:
saneyaml.load(test)
self.fail('Exception not raised')
except Exception:
pass
class AboutTest(unittest.TestCase):
def test_About_load_ignores_original_field_order_and_uses_standard_predefined_order(self):
# fields in this file are not in the standard order
test_file = get_test_loc('test_model/parse/ordered_fields.ABOUT')
a = model.About(test_file)
assert [] == a.errors
expected = ['about_resource', 'name', 'version', 'download_url']
result = [f.name for f in a.all_fields() if f.present]
assert expected == result
def test_About_duplicate_field_names_are_detected_with_different_case(self):
# This test is failing because the YAML does not keep the order when
# loads the test files. For instance, it treat the 'About_Resource' as the
# first element and therefore the dup key is 'about_resource'.
test_file = get_test_loc('test_model/parse/dupe_field_name.ABOUT')
a = model.About(test_file)
expected = [
Error(
WARNING, 'Field About_Resource is a duplicate. Original value: "." replaced with: "new value"'),
Error(
WARNING, 'Field Name is a duplicate. Original value: "old" replaced with: "new"')
]
result = a.errors
assert sorted(expected) == sorted(result)
def test_About_duplicate_field_names_are_not_reported_if_same_value(self):
# This test is failing because the YAML does not keep the order when
# loads the test files. For instance, it treat the 'About_Resource' as the
# first element and therefore the dup key is 'about_resource'.
test_file = get_test_loc(
'test_model/parse/dupe_field_name_no_new_value.ABOUT')
a = model.About(test_file)
expected = [
]
result = a.errors
assert sorted(expected) == sorted(result)
def check_About_hydrate(self, about, fields):
expected = set([
'name',
'homepage_url',
'download_url',
'version',
'copyright',
'date',
'license_spdx',
'license_text_file',
'notice_file',
'about_resource'])
expected_errors = [
Error(INFO, 'Custom Field: date'),
Error(INFO, 'Custom Field: license_spdx'),
Error(INFO, 'Custom Field: license_text_file')]
errors = about.hydrate(fields)
assert expected_errors == errors
result = set([f.name for f in about.all_fields() if f.present])
assert expected == result
def test_About_hydrate_normalize_field_names_to_lowercase(self):
test_content = get_test_content(
'test_gen/parser_tests/upper_field_names.ABOUT')
fields = saneyaml.load(test_content).items()
a = model.About()
for _ in range(3):
self.check_About_hydrate(a, fields)
def test_About_with_existing_about_resource_has_no_error(self):
test_file = get_test_loc(
'test_gen/parser_tests/about_resource_field.ABOUT')
a = model.About(test_file)
assert [] == a.errors
result = a.about_resource.value['about_resource.c']
# this means we have a location
self.assertNotEqual([], result)
def test_About_loads_ignored_resources_field(self):
# fields in this file are not in the standard order
test_file = get_test_loc(
'test_model/parse/with_ignored_resources.ABOUT')
a = model.About(test_file)
# assert [] == a.errors
expected = ['about_resource', 'ignored_resources', 'name']
result = [f.name for f in a.all_fields() if f.present]
assert expected == result
def test_About_loads_deployed_resource_field(self):
# fields in this file are not in the standard order
test_file = get_test_loc(
'test_model/parse/with_deployed_resource.ABOUT')
a = model.About(test_file)
# assert [] == a.errors
expected = ['about_resource', 'deployed_resource', 'name', 'is_curated']
result = [f.name for f in a.all_fields() if f.present]
assert expected == result
def test_About_has_errors_when_about_resource_is_missing(self):
test_file = get_test_loc('test_gen/parser_tests/.ABOUT')
a = model.About(test_file)
expected = [Error(CRITICAL, 'Field about_resource is required')]
result = a.errors
assert expected == result
def test_About_has_errors_when_about_resource_does_not_exist(self):
test_file = get_test_loc(
'test_gen/parser_tests/missing_about_ref.ABOUT')
file_path = posixpath.join(posixpath.dirname(
test_file), 'about_file_missing.c')
a = model.About(test_file)
err_msg = 'Field about_resource: Path %s not found' % file_path
expected = [Error(INFO, err_msg)]
result = a.errors
assert expected == result
def test_About_has_errors_when_missing_required_fields_are_missing(self):
test_file = get_test_loc('test_model/parse/missing_required.ABOUT')
a = model.About(test_file)
expected = [
Error(CRITICAL, 'Field about_resource is required'),
Error(CRITICAL, 'Field name is required'),
]
result = a.errors
assert expected == result
def test_About_has_errors_when_required_fields_are_empty(self):
test_file = get_test_loc('test_model/parse/empty_required.ABOUT')
a = model.About(test_file)
expected = [
Error(CRITICAL, 'Field about_resource is required and empty'),
Error(CRITICAL, 'Field name is required and empty'),
]
result = a.errors
assert expected == result
def test_About_has_errors_with_empty_notice_file_field(self):
test_file = get_test_loc('test_model/parse/empty_notice_field.about')
a = model.About(test_file)
expected = [
Error(INFO, 'Field notice_file is present but empty.')]
result = a.errors
assert expected == result
def test_About_custom_fields_are_never_ignored(self):
test_file = get_test_loc(
'test_model/custom_fields/custom_fields.about')
a = model.About(test_file)
result = [(n, f.value) for n, f in a.custom_fields.items()]
expected = [
(u'single_line', u'README STUFF'),
(u'multi_line', u'line1\nline2'),
(u'other', u'sasasas'),
(u'empty', u'')
]
assert expected == result
def test_About_custom_fields_are_not_ignored_and_order_is_preserved(self):
test_file = get_test_loc(
'test_model/custom_fields/custom_fields.about')
a = model.About(test_file)
result = [(n, f.value) for n, f in a.custom_fields.items()]
expected = [
(u'single_line', u'README STUFF'),
(u'multi_line', u'line1\nline2'),
(u'other', u'sasasas'),
(u'empty', u'')
]
assert sorted(expected) == sorted(result)
def test_About_has_errors_for_illegal_custom_field_name(self):
test_file = get_test_loc('test_model/parse/illegal_custom_field.about')
a = model.About(test_file)
expected_errors = [
Error(INFO, 'Custom Field: hydrate'),
Error(
CRITICAL, "Internal error with custom field: 'hydrate': 'illegal name'.")
]
assert expected_errors == a.errors
assert not hasattr(getattr(a, 'hydrate'), 'value')
field = list(a.custom_fields.values())[0]
assert 'hydrate' == field.name
assert 'illegal name' == field.value
def test_About_file_fields_are_empty_if_present_and_path_missing(self):
test_file = get_test_loc(
'test_model/parse/missing_notice_license_files.ABOUT')
a = model.About(test_file)
file_path1 = posixpath.join(
posixpath.dirname(test_file), 'test.LICENSE')
file_path2 = posixpath.join(
posixpath.dirname(test_file), 'test.NOTICE')
err_msg1 = Error(
CRITICAL, 'Field license_file: Path %s not found' % file_path1)
err_msg2 = Error(
CRITICAL, 'Field notice_file: Path %s not found' % file_path2)
expected_errors = [err_msg1, err_msg2]
assert expected_errors == a.errors
assert {'test.LICENSE': None} == a.license_file.value
assert {'test.NOTICE': None} == a.notice_file.value
def test_About_notice_and_license_text_are_loaded_from_file(self):
test_file = get_test_loc(
'test_model/parse/license_file_notice_file.ABOUT')
a = model.About(test_file)
expected = '''Tester holds the copyright for test component. Tester relinquishes copyright of
this software and releases the component to Public Domain.
* Email [email protected] for any questions'''
result = a.license_file.value['license_text.LICENSE']
assert expected == result
expected = '''Test component is released to Public Domain.'''
result = a.notice_file.value['notice_text.NOTICE']
assert expected == result
def test_About_license_and_notice_text_are_empty_if_field_missing(self):
test_file = get_test_loc('test_model/parse/no_file_fields.ABOUT')
a = model.About(test_file)
assert [] == a.errors
assert {} == a.license_file.value
assert {} == a.notice_file.value
def test_About_rejects_non_ascii_names_and_accepts_unicode_values(self):
test_file = get_test_loc(
'test_model/parse/non_ascii_field_name_value.about')
a = model.About(test_file)
expected = [
Error(
WARNING, "Field name: ['mat\xedas'] contains illegal name characters (or empty spaces) and is ignored.")
]
assert expected == a.errors
def test_About_invalid_boolean_value(self):
test_file = get_test_loc('test_model/parse/invalid_boolean.about')
a = model.About(test_file)
expected_msg = "Field modified: Invalid flag value: 'blah'"
assert expected_msg in a.errors[0].message
def test_About_boolean_value(self):
test_file = get_test_loc('test_model/parse/boolean_data.about')
a = model.About(test_file)
expected_msg = "Field track_changes is present but empty."
assert expected_msg in a.errors[0].message
# Context of the test file
"""
about_resource: .
name: boolean_data
attribute: False
modified: true
internal_use_only: no
redistribute: yes
track_changes:
"""
assert a.attribute.value is False
assert a.modified.value is True
assert a.internal_use_only.value is False
assert a.redistribute.value is True
assert a.track_changes.value is None
def test_About_contains_about_file_path(self):
test_file = get_test_loc('test_model/serialize/about.ABOUT')
# TODO: I am not sure this override of the about_file_path makes sense
a = model.About(test_file, about_file_path='complete/about.ABOUT')
assert [] == a.errors
expected = 'complete/about.ABOUT'
result = a.about_file_path
assert expected == result
def test_About_equals(self):
test_file = get_test_loc('test_model/equal/complete/about.ABOUT')
a = model.About(test_file, about_file_path='complete/about.ABOUT')
b = model.About(test_file, about_file_path='complete/about.ABOUT')
assert a == b
def test_About_are_not_equal_with_small_text_differences(self):
test_file = get_test_loc('test_model/equal/complete2/about.ABOUT')
a = model.About(test_file, about_file_path='complete2/about.ABOUT')
test_file2 = get_test_loc('test_model/equal/complete/about.ABOUT')
b = model.About(test_file2, about_file_path='complete/about.ABOUT')
assert a.dumps() != b.dumps()
assert a != b
def test_get_field_names_only_returns_non_empties(self):
a = model.About()
a.custom_fields['f'] = model.StringField(
name='f', value='1', present=True)
b = model.About()
b.custom_fields['g'] = model.StringField(
name='g', value='1', present=True)
abouts = [a, b]
# ensure all fields (including custom fields) and
# about_resource are collected in the correct order
expected = [
model.About.ABOUT_RESOURCE_ATTR, 'name', 'f', 'g'
]
result = model.get_field_names(abouts)
assert expected == result
def test_get_field_names_does_not_return_duplicates_custom_fields(self):
a = model.About()
a.custom_fields['f'] = model.StringField(name='f', value='1',
present=True)
a.custom_fields['cf'] = model.StringField(name='cf', value='1',
present=True)
b = model.About()
b.custom_fields['g'] = model.StringField(name='g', value='1',
present=True)
b.custom_fields['cf'] = model.StringField(name='cf', value='2',
present=True)
abouts = [a, b]
# ensure all fields (including custom fields) and
# about_resource are collected in the correct order
expected = [
'about_resource',
'name',
'cf',
'f',
'g',
]
result = model.get_field_names(abouts)
assert expected == result
def test_comma_in_license(self):
test_file = get_test_loc('test_model/special_char/about.ABOUT')
a = model.About(test_file)
expected = Error(
ERROR, "The following character(s) cannot be in the license_key: [',']")
assert a.errors[0] == expected
def test_load_dict_issue_433(self):
package_data = {
'about_resource': 'package1.zip',
'name': 'package',
'version': '1.0',
'copyright': 'copyright on package',
'license_expression': 'license1 AND license2',
'notice_file': 'package1.zip.NOTICE',
'licenses': [
{'key': 'license1', 'name': 'License1', 'file': 'license1.LICENSE',
'url': 'some_url', 'spdx_license_key': 'key'},
{'key': 'license2', 'name': 'License2', 'file': 'license2.LICENSE',
'url': 'some_url', 'spdx_license_key': 'key'},
],
}
about = model.About()
about.load_dict(package_data, base_dir='')
as_dict = about.as_dict()
expected = '''about_resource: package1.zip
name: package
version: '1.0'
license_expression: license1 AND license2
copyright: copyright on package
notice_file: package1.zip.NOTICE
licenses:
- key: license1
name: License1
file: license1.LICENSE
url: some_url
spdx_license_key: key
- key: license2
name: License2
file: license2.LICENSE
url: some_url
spdx_license_key: key
'''
lic_dict = {u'license1': [u'License1', u'license1.LICENSE', u'', u'some_url', 'key'], u'license2': [
u'License2', u'license2.LICENSE', u'', u'some_url', 'key']}
assert about.dumps(lic_dict) == expected
class SerializationTest(unittest.TestCase):
def test_About_dumps(self):
test_file = get_test_loc('test_model/dumps/about.ABOUT')
a = model.About(test_file)
assert [] == a.errors
expected = '''about_resource: .
name: AboutCode
version: 0.11.0
description: |
AboutCode is a tool
to process ABOUT files.
An ABOUT file is a file.
homepage_url: http://dejacode.org
license_expression: apache-2.0
copyright: Copyright (c) 2013-2014 nexB Inc.
notice_file: NOTICE
owner: nexB Inc.
author: Jillian Daguil, Chin Yeung Li, Philippe Ombredanne, Thomas Druez
vcs_tool: git
vcs_repository: https://github.com/dejacode/about-code-tool.git
licenses:
- key: apache-2.0
name: Apache 2.0
file: apache-2.0.LICENSE
'''
result = a.dumps()
assert expected == result
def test_About_dumps_does_all_non_empty_present_fields(self):
test_file = get_test_loc('test_model/parse/complete2/about.ABOUT')
a = model.About(test_file)
expected_error = [
Error(INFO, 'Custom Field: custom1'),
Error(INFO, 'Custom Field: custom2'),
Error(INFO, 'Field custom2 is present but empty.')
]
assert sorted(expected_error) == sorted(a.errors)
expected = '''about_resource: .
name: AboutCode
version: 0.11.0
custom1: |
multi
line
'''
result = a.dumps()
assert expected == result
def test_About_dumps_with_different_boolean_value(self):
test_file = get_test_loc('test_model/parse/complete2/about2.ABOUT')
a = model.About(test_file)
expected_error_msg = "Field track_changes: Invalid flag value: 'blah' is not one of"
assert len(a.errors) == 1
assert expected_error_msg in a.errors[0].message
expected = '''about_resource: .
name: AboutCode
version: 0.11.0
redistribute: no
attribute: yes
modified: yes
'''
result = a.dumps()
assert set(expected) == set(result)
def test_About_dumps_all_non_empty_fields(self):
test_file = get_test_loc('test_model/parse/complete2/about.ABOUT')
a = model.About(test_file)
expected_error = [
Error(INFO, 'Custom Field: custom1'),
Error(INFO, 'Custom Field: custom2'),
Error(INFO, 'Field custom2 is present but empty.')
]
assert sorted(expected_error) == sorted(a.errors)
expected = '''about_resource: .
name: AboutCode
version: 0.11.0
custom1: |
multi
line
'''
result = a.dumps()
assert expected == result
def test_About_as_dict_contains_special_paths(self):
test_file = get_test_loc('test_model/special/about.ABOUT')
a = model.About(test_file, about_file_path='complete/about.ABOUT')
expected_errors = []
assert expected_errors == a.errors
as_dict = a.as_dict()
expected = 'complete/about.ABOUT'
result = as_dict[model.About.ABOUT_FILE_PATH_ATTR]
assert expected == result
def test_load_dump_is_idempotent(self):
test_file = get_test_loc('test_model/this.ABOUT')
a = model.About()
a.load(test_file)
dumped_file = get_temp_file('that.ABOUT')
a.dump(dumped_file)
expected = get_unicode_content(test_file).splitlines()
result = get_unicode_content(dumped_file).splitlines()
# Ignore comment and empty line
filtered_result = []
for line in result:
if not line.startswith('#') and not line == '':
filtered_result.append(line)
assert expected == filtered_result
def test_load_can_load_unicode(self):
test_file = get_test_loc('test_model/unicode/nose-selecttests.ABOUT')
a = model.About()
a.load(test_file)
file_path = posixpath.join(posixpath.dirname(
test_file), 'nose-selecttests-0.3.zip')
err_msg = 'Field about_resource: Path %s not found' % file_path
errors = [
Error(INFO, 'Custom Field: dje_license'),
Error(INFO, 'Custom Field: license_text_file'),
Error(INFO, 'Custom Field: scm_tool'),
Error(INFO, 'Custom Field: scm_repository'),
Error(INFO, 'Custom Field: test'),
Error(INFO, err_msg)]
assert errors == a.errors
assert 'Copyright (c) 2012, Domen Kožar' == a.copyright.value
def test_load_non_unicode(self):
test_file = get_test_loc('test_model/unicode/not-unicode.ABOUT')
a = model.About()
a.load(test_file)
err = a.errors[0]
assert CRITICAL == err.severity
assert 'Cannot load invalid ABOUT file' in err.message
def test_as_dict_load_dict_ignores_empties(self):
test = {
'about_resource': '.',
'author': '',
'copyright': 'Copyright (c) 2013-2014 nexB Inc.',
'custom1': 'some custom',
'custom_empty': '',
'description': 'AboutCode is a tool\nfor files.',
'license_expression': 'apache-2.0',
'name': 'AboutCode',
'owner': 'nexB Inc.'}
expected = {
'about_file_path': None,
'about_resource': dict([('.', None)]),
'copyright': 'Copyright (c) 2013-2014 nexB Inc.',
'custom1': 'some custom',
'description': 'AboutCode is a tool\nfor files.',
'license_expression': 'apache-2.0',
'name': 'AboutCode',
'owner': 'nexB Inc.'}
a = model.About()
base_dir = 'some_dir'
a.load_dict(test, base_dir)
as_dict = a.as_dict()
# FIXME: why converting back to dict?
assert expected == dict(as_dict)
def test_load_dict_as_dict_is_idempotent_ignoring_special(self):
test = {
'about_resource': ['.'],
'attribute': 'yes',
'author': 'Jillian Daguil, Chin Yeung Li, Philippe Ombredanne, Thomas Druez',
'copyright': 'Copyright (c) 2013-2014 nexB Inc.',
'description': 'AboutCode is a tool to process ABOUT files. An ABOUT file is a file.',
'homepage_url': 'http://dejacode.org',
'license_expression': 'apache-2.0',
'name': 'AboutCode',
'owner': 'nexB Inc.',
'vcs_repository': 'https://github.com/dejacode/about-code-tool.git',
'vcs_tool': 'git',
'version': '0.11.0'}
a = model.About()
base_dir = 'some_dir'
a.load_dict(test, base_dir)
as_dict = a.as_dict()
expected = {
'about_file_path': None,
'about_resource': dict([('.', None)]),
'attribute': 'yes',
'author': 'Jillian Daguil, Chin Yeung Li, Philippe Ombredanne, Thomas Druez',
'copyright': 'Copyright (c) 2013-2014 nexB Inc.',
'description': 'AboutCode is a tool to process ABOUT files. An ABOUT file is a file.',
'homepage_url': 'http://dejacode.org',
'license_expression': 'apache-2.0',
'name': 'AboutCode',
'owner': 'nexB Inc.',
'vcs_repository': 'https://github.com/dejacode/about-code-tool.git',
'vcs_tool': 'git',
'version': '0.11.0'}
assert expected == dict(as_dict)
def test_about_model_class_from_dict_constructor(self):
about_data = {
'about_resource': ['.'],
'attribute': 'yes',
'author': 'Jillian Daguil, Chin Yeung Li, Philippe Ombredanne, Thomas Druez',
'copyright': 'Copyright (c) 2013-2014 nexB Inc.',
'description': 'AboutCode is a tool to process ABOUT files. An ABOUT file is a file.',
'homepage_url': 'http://dejacode.org',
'license_expression': 'apache-2.0',
'name': 'AboutCode',
'owner': 'nexB Inc.',
'vcs_repository': 'https://github.com/dejacode/about-code-tool.git',
'vcs_tool': 'git',
'version': '0.11.0',
}
about = model.About.from_dict(about_data)
assert isinstance(about, model.About)
about_data.update({
'about_file_path': None,