-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.py
876 lines (710 loc) · 32.9 KB
/
action.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
#!/usr/bin/env python3
"""Tests for the validity of the channel and repository files.
You can run this script directly or with `python -m unittest` from this or the
root directory. For some reason `nosetests` does not pick up the generated tests
even though they are generated at load time.
Arguments:
--channel=channel.json
Channel filename to test
--repository=repository.json
Repository filename to test
--test-repositories
Also generates tests for all repositories in `channel.json` (the http
ones).
"""
import argparse
import os
import re
import json
import sys
import unittest
from functools import wraps
from urllib.request import urlopen
from urllib.error import HTTPError
from urllib.parse import urljoin
generator_method_type = 'method'
parser = argparse.ArgumentParser()
parser.add_argument('--channel', default='channel.json')
parser.add_argument('--repository', default='repository.json')
parser.add_argument('--test-repositories', action='store_true')
userargs, unittesting_args = parser.parse_known_args()
sys.argv = sys.argv[:1] + unittesting_args
################################################################################
# Utilities
def _open(filepath, *args, **kwargs):
"""Wrapper function to search one dir above if a file does not exist."""
if not os.path.exists(filepath):
filepath = os.path.join("..", filepath)
return open(filepath, 'rb', *args, **kwargs)
def generate_test_methods(cls, stream):
"""Class decorator for classes that use test generating methods.
A class that is decorated with this function will be searched for methods
starting with "generate_" (similar to "test_") and then run like a nosetest
generator.
Note: The generator function must be a classmethod!
If a "pre_generate" classmethod exists, it will be run before the generator
functions.
Generate tests using the following statement:
yield method, (arg1, arg2, arg3) # ...
"""
attributes = list(cls.__dict__.keys())
if 'pre_generate' in attributes:
func = getattr(cls, 'pre_generate')
if not func.__class__.__name__ == generator_method_type:
raise TypeError("Pre-Generator method must be classmethod")
func()
for name in list(cls.__dict__.keys()):
generator = getattr(cls, name)
if not name.startswith("generate_") or not callable(generator):
continue
if not generator.__class__.__name__ == generator_method_type:
raise TypeError("Generator methods must be classmethods")
# Create new methods for each `yield`
for sub_call in generator(stream):
method, params = sub_call
@wraps(method)
def wrapper(self, method=method, params=params):
return method(self, *params)
# Do not attempt to print lists/dicts with printed lenght of 1000 or
# more, they are not interesting for us (probably the whole file)
args = []
for v in params:
string = repr(v)
if len(string) > 1000:
args.append('...')
else:
args.append(string)
mname = method.__name__
if mname.startswith("_test"):
mname = mname[1:]
elif not mname.startswith("test_"):
mname = "test_" + mname
# Include parameters in attribute name
name = "%s(%s)" % (mname, ", ".join(args))
setattr(cls, name, wrapper)
# Remove the generator afterwards, it did its work
delattr(cls, name)
return cls
# Very limited subclassing of dict class, which just suits our needs
class CaseInsensitiveDict(dict):
@classmethod
def _k(cls, key):
return key.lower() if isinstance(key, str) else key
def __getitem__(self, key):
return super(CaseInsensitiveDict, self).__getitem__(self._k(key))
def __setitem__(self, key, value):
super(CaseInsensitiveDict, self).__setitem__(self._k(key), value)
def __contains__(self, key):
return super(CaseInsensitiveDict, self).__contains__(self._k(key))
def get_package_name(data):
"""Get "name" from a package with a workaround when it's not defined.
Use the last part of details url for the package's name otherwise since
packages must define one of these two keys anyway.
"""
return data.get('name') or data.get('details').strip("/").rsplit("/", 1)[-1]
################################################################################
# Tests
class TestContainer(object):
"""Contains tests that the generators can easily access (when subclassing).
Does not contain tests itself, must be used as mixin with unittest.TestCase.
"""
@classmethod
def setUpClass(cls):
cls.package_names = CaseInsensitiveDict()
cls.dependency_names = CaseInsensitiveDict()
# tuple of (prev_name, include, name); prev_name for case sensitivity
cls.previous_package_names = CaseInsensitiveDict()
# Default packages for ST2 and ST3 are largely the same,
# except for Pascal and Rust
# which only ship in ST3
default_packages = (
'ActionScript', 'AppleScript', 'ASP', 'Batch File', 'Binary', 'C#',
'C++', 'Clojure', 'Color Scheme - Default', 'CSS', 'D', 'Default',
'Diff', 'Erlang', 'Git Formats', 'Go', 'Graphviz', 'Groovy',
'Haskell', 'HTML', 'Java', 'JavaScript', 'Language - English',
'LaTeX', 'Lisp', 'Lua', 'Makefile', 'Markdown', 'Matlab',
'Objective-C', 'OCaml', 'Pascal', 'Perl', 'PHP', 'Python', 'R',
'Rails', 'Regular Expressions', 'RestructuredText', 'Ruby', 'Rust',
'Scala', 'ShellScript', 'SQL', 'TCL', 'Text', 'Textile',
'Theme - Default', 'Vintage', 'XML', 'YAML'
)
rel_b_reg = r'''^ ( https:// bitbucket\.org / [^/#?]+ / [^/#?]+
| https:// github\.com / [^/#?]+ / [^/#?]+
| https:// gitlab\.com / [^/#?]+ / [^/#?]+
) $'''
# Strip multilines for better debug info on failures
rel_b_reg = ' '.join(map(str.strip, rel_b_reg.split()))
release_base_regex = re.compile(rel_b_reg, re.X)
pac_d_reg = r'''^ ( https:// bitbucket\.org/ [^/#?]+/ [^/#?]+
( /src/ [^#?]*[^/#?] | \#tags | / )?
| https:// github\.com/ [^/#?]+/ [^/#?]+
(?<!\.git) ( /tree/ [^#?]*[^/#?] | / )?
| https:// gitlab\.com/ [^/#?]+/ [^/#?]+
(?<!\.git) ( /-/tree/ [^#?]*[^/#?] | / )?
) $'''
pac_d_reg = ' '.join(map(str.strip, pac_d_reg.split()))
package_details_regex = re.compile(pac_d_reg, re.X)
def _test_repository_keys(self, filename, data):
keys = ("$schema", 'schema_version', 'packages', 'dependencies', 'includes')
self.assertTrue(2 <= len(data) <= len(keys), "Unexpected number of keys")
self.assertIn('schema_version', data)
self.assertEqual(data['schema_version'], '3.0.0')
listkeys = [k for k in ('packages', 'dependencies', 'includes')
if k in data]
self.assertGreater(len(listkeys), 0, "Must contain something")
for k in listkeys:
self.assertIsInstance(data[k], list)
for k in data:
self.assertIn(k, keys, "Unexpected key")
includes = data.get('includes', [])
self.assertIsInstance(includes, list)
for include in includes:
self.assertIsInstance(include, str)
def _test_dependency_names(self, include, data):
m = re.search(r"(?:^|/)(0-9|[a-z]|dependencies)\.json$", include)
if not m:
self.fail("Include filename does not match")
repo_dependency_names = []
for pdata in data['dependencies']:
name = get_package_name(pdata)
if name in self.dependency_names:
self.fail("Dependency names must be unique: " + name)
else:
self.dependency_names[name] = include
repo_dependency_names.append(name)
if name in self.package_names:
self.fail("Dependency and package names must be unique: %s, "
"previously occured in %s"
% (name, self.package_names[name]))
# Check package order
self.assertEqual(repo_dependency_names,
sorted(repo_dependency_names, key=str.lower),
"Dependencies must be sorted alphabetically")
def _test_repository_package_names(self, include, data):
m = re.search(r"(?:^|/)(0-9|[a-z]|dependencies)\.json$", include)
if not m:
self.fail("Include filename does not match")
letter = m.group(1)
repo_package_names = []
# Collect package names and check if they are unique,
# including occurences in previous_names.
for pdata in data['packages']:
pname = get_package_name(pdata)
if pname in self.package_names:
self.fail("Package names must be unique: %s, previously "
"occured in %s"
% (pname, self.package_names[pname]))
elif (
pname in self.previous_package_names
# check casing
and pname == self.previous_package_names[pname][0]
):
print(pname, self.previous_package_names[pname][0])
self.fail("Package names can not occur as a name and as a "
"previous_name: %s, previously occured as "
"previous_name in %s: %s"
% (pname, self.previous_package_names[pname][1],
self.previous_package_names[pname][2]))
elif pname in self.dependency_names:
self.fail("Dependency and package names must be unique: %s, "
"previously occured in %s"
% (pname, self.dependency_names[pname]))
else:
self.package_names[pname] = include
repo_package_names.append(pname)
# Check if in the correct file
for package_name in repo_package_names:
if letter == '0-9':
self.assertTrue(package_name[0].isdigit(),
"Package inserted in wrong file")
else:
self.assertEqual(package_name[0].lower(), letter,
"Package inserted in wrong file")
# Check package order
self.assertEqual(repo_package_names,
sorted(repo_package_names, key=str.lower),
"Packages must be sorted alphabetically (by name)")
def _test_indentation(self, filename, contents):
for i, line in enumerate(contents.splitlines()):
self.assertRegex(line, r"^\t*\S",
"Indent must be tabs in line %d" % (i + 1))
package_key_types_map = {
'name': str,
'details': str,
'description': str,
'releases': list,
'homepage': str,
'author': (str, list),
'readme': str,
'issues': str,
'donate': (str, type(None)),
'buy': str,
'previous_names': list,
'labels': list
}
def _test_package(self, include, data):
name = get_package_name(data)
for k, v in data.items():
self.enforce_key_types_map(k, v, self.package_key_types_map)
if k == 'details':
self.assertRegex(v, self.package_details_regex,
'The details url is badly formatted or '
'invalid')
elif k == 'donate' and v is None:
# Allow "removing" the donate url that is added by "details"
continue
elif k == 'labels':
for label in v:
self.assertNotIn(",", label,
"Multiple labels should not be in the "
"same string")
# self.assertEqual(label, label.lower(),
# "Label name must be lowercase")
self.assertCountEqual(v, list(set(v)),
"Specifying the same label multiple times is redundant")
elif k == 'previous_names':
# Test if name is unique, against names and previous_names.
for prev_name in v:
if prev_name in self.previous_package_names:
self.fail("Previous package names must be unique: %s, "
"previously occured in %s"
% (prev_name,
self.previous_package_names[prev_name]))
elif prev_name in self.package_names:
self.fail("Package names can not occur as a name and "
"as a previous_name: %s, previously occured "
"as name in %s"
% (prev_name, self.package_names[prev_name]))
else:
self.previous_package_names[prev_name] = (
(prev_name, include, name)
)
elif k in ('homepage', 'readme', 'issues', 'donate', 'buy'):
self.assertRegex(v, '^https?://')
# Test for invalid characters (on file systems)
# Invalid on Windows (and sometimes problematic on UNIX)
self.assertNotRegex(name, r'[/?<>\\:*|"\x00-\x19]',
'Package names must be valid folder names on all '
'operating systems')
# Invalid on OS X (or more precisely: hidden)
self.assertFalse(name.startswith('.'), 'Package names may not start '
'with a dot')
self.assertNotIn(name, self.default_packages)
if 'details' not in data:
for key in ('name', 'homepage', 'author', 'releases'):
self.assertIn(key, data, '%r is required if no "details" URL '
'provided' % key)
dependency_key_types_map = {
'name': str,
'description': str,
'releases': list,
'issues': str,
'load_order': str,
'author': str
}
def _test_dependency(self, include, data):
for k, v in data.items():
self.enforce_key_types_map(k, v, self.dependency_key_types_map)
if k == 'issues':
self.assertRegex(v, '^https?://')
# Test for invalid characters (on file systems)
elif k == 'name':
# Invalid on Windows (and sometimes problematic on UNIX)
self.assertNotRegex(v, r'[/?<>\\:*|"\x00-\x19]')
self.assertFalse(v.startswith('.'))
elif k == 'load_order':
self.assertRegex(v, r'^\d\d$', '"load_order" must be a two '
'digit string')
for key in ('author', 'releases', 'issues', 'description', 'load_order'):
self.assertIn(key, data, '%r is required for dependencies' % key)
pck_release_key_types_map = {
'base': str,
'tags': (bool, str),
'branch': str,
'sublime_text': str,
'platforms': (list, str),
'dependencies': (list, str),
'version': str,
'date': str,
'url': str
}
dep_release_key_types_map = {
'base': str,
'tags': (bool, str),
'branch': str,
'sublime_text': str,
'platforms': (list, str),
'version': str,
'sha256': str,
'url': str
}
def _test_release(self, package_name, data, dependency, main_repo=True):
# Test for required keys (and fail early)
if main_repo:
if dependency:
condition = (
'base' in data
and ('tags' in data or 'branch' in data)
or ('sha256' in data
and ('url' not in data
or data['url'].startswith('http://')))
)
self.assertTrue(condition,
'A release must have a "base" and a "tags" or "branch" key '
'if it is in the main repository. For custom '
'releases, a custom repository.json file must be '
'hosted elsewhere. The only exception to this rule '
'is for packages that can not be served over HTTPS '
'since they help bootstrap proper secure HTTP '
'support for Sublime Text.')
else:
self.assertTrue(('tags' in data or 'branch' in data),
'A release must have a "tags" key or "branch" key '
'if it is in the main repository. For custom '
'releases, a custom repository.json file must be '
'hosted elsewhere.')
for key in ('url', 'version', 'date'):
self.assertNotIn(key, data,
'The version, date and url keys should not be '
'used in the main repository since a pull '
'request would be necessary for every release')
elif 'tags' not in data and 'branch' not in data:
if dependency:
for key in ('url', 'version'):
self.assertIn(key, data,
'A release must provide "url" and "version" '
'keys if it does not specify "tags" or "branch"')
else:
for key in ('url', 'version', 'date'):
self.assertIn(key, data,
'A release must provide "url", "version" and '
'"date" keys if it does not specify "tags" or'
'"branch"')
else:
for key in ('url', 'version', 'date'):
self.assertNotIn(key, data,
'The key "%s" is redundant when "tags" or '
'"branch" is specified' % key)
self.assertIn('sublime_text', data,
'A sublime text version selector is required')
if dependency:
self.assertIn('platforms', data,
'A platforms selector is required for dependencies')
self.assertFalse(('tags' in data and 'branch' in data),
'A release must have only one of the "tags" or '
'"branch" keys.')
# Test keys values
self.check_release_key_values(data, dependency)
def check_release_key_values(self, data, dependency):
"""Check the key-value pairs of a release for validity."""
release_key_types_map = (self.dep_release_key_types_map if dependency
else self.pck_release_key_types_map)
for k, v in data.items():
self.enforce_key_types_map(k, v, release_key_types_map)
if k == 'url':
if dependency:
if 'sha256' not in data:
self.assertRegex(v, r'^https://')
else:
self.assertRegex(v, r'^http://')
else:
self.assertRegex(v, r'^https?://')
elif k == 'base':
self.assertRegex(v, self.release_base_regex,
'The base url is badly formatted or '
'invalid')
elif k == 'sublime_text':
self.assertRegex(v, r'^(\*|<=?\d{4}|>=?\d{4}|\d{4} - \d{4})$',
'sublime_text must be `*`, of the form '
'`<relation><version>` '
'where <relation> is one of {<, <=, >, >=} '
'and <version> is a 4 digit number, '
'or of the form `<version> - <version>`')
elif k == 'platforms':
if isinstance(v, str):
v = [v]
for plat in v:
self.assertRegex(plat,
r"^(\*|(osx|linux|windows)(-(x(32|64)|arm64))?)$")
self.assertCountEqual(v, list(set(v)),
"Specifying the same platform multiple times is redundant")
if (("osx-x32" in v and "osx-x64" in v and "osx-arm64" in v) or
("windows-x32" in v and "windows-x64" in v and "windows-arm64" in v) or
("linux-x32" in v and "linux-x64" in v and "linux-arm64" in v)):
self.fail("Specifying all of x32, x64 and arm64 architectures is redundant")
self.assertFalse(set(["osx", "windows", "linux"]) == set(v),
'"osx, windows, linux" are similar to (and should be replaced by) "*"')
elif k == 'date':
self.assertRegex(v, r"^\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d$")
elif k == 'tags':
self.assertTrue(bool(v),
'"tags" must be `true` or a string of length>0')
if isinstance(v, str):
self.assertFalse(v == "true",
'It is unlikely to specify the prefix '
'"true" use not the boolean `true`')
elif k == 'branch':
self.assertNotEqual(v, "",
'"branch" must be non-empty')
elif k == 'sha256':
self.assertRegex(v, r'(?i)^[0-9a-f]{64}$')
def enforce_key_types_map(self, k, v, key_types_map):
self.assertIn(k, key_types_map, 'Unknown key "%s"' % k)
self.assertIsInstance(v, key_types_map[k], k)
if (
isinstance(v, list) and key_types_map[k] != list
and len(key_types_map[k]) == 2
):
# Test if all of the lists elements are of the other allowed types
other_types = tuple(filter(lambda t: t != list, key_types_map[k]))
for sub_value in v:
self.assertIsInstance(sub_value, other_types, k)
def _test_error(self, msg, e=None):
"""
A generic error-returning function used by the meta-programming features
of this class.
:param msg:
The error message to return
:param e:
An optional exception to include with the error message
"""
if e:
if isinstance(e, HTTPError):
self.fail("%s: %s" % (msg, e))
else:
self.fail("%s: %r" % (msg, e))
else:
self.fail(msg)
@classmethod
def _include_tests(cls, path, stream):
"""
Yields tuples of (method, args) to add to a unittest TestCase class.
A meta-programming function to expand the definition of class at run
time, based on the contents of a file or URL.
:param cls:
The class to add the methods to
:param path:
The URL or file path to fetch the repository info from
:param stream:
A file-like object used for diagnostic output that provides .write()
and .flush()
"""
# TODO multi-threading
stream.write("%s ... " % path)
stream.flush()
success = False
try:
if re.match(r'https?://', path, re.I) is not None:
# Download the repository
try:
with urlopen(path) as f:
source = f.read()
except Exception as e:
yield cls._fail("Downloading %s failed" % path, e)
return
source = source.decode("utf-8", 'strict')
else:
try:
with _open(path) as f:
source = f.read().decode('utf-8', 'strict')
except Exception as e:
yield cls._fail("Opening %s failed" % path, e)
return
if not source:
yield cls._fail("%s is empty" % path)
return
# Parse the repository
try:
data = json.loads(source)
except Exception as e:
yield cls._fail("Could not parse %s" % path, e)
return
# Check for the schema version first (and generator failures it's
# badly formatted)
if 'schema_version' not in data:
yield cls._fail("No schema_version found in %s" % path)
return
schema = data['schema_version']
if schema != '3.0.0' and float(schema) not in (1.0, 1.1, 1.2, 2.0):
yield cls._fail("Unrecognized schema version %s in %s"
% (schema, path))
return
success = True
# Do not generate 1000 failing tests for not yet updated repos
if schema != '3.0.0':
stream.write("skipping (schema version %s)"
% data['schema_version'])
cls.skipped_repositories[schema] += 1
return
else:
stream.write("done")
finally:
if not success:
stream.write("failed")
stream.write("\n")
# `path` is for output during tests only
yield cls._test_repository_keys, (path, data)
if 'packages' in data:
for package in data['packages']:
yield cls._test_package, (path, package)
package_name = get_package_name(package)
if 'releases' in package:
for release in package['releases']:
(yield cls._test_release,
("%s (%s)" % (package_name, path),
release, False, False))
if 'includes' in data:
for include in data['includes']:
i_url = urljoin(path, include)
for test in cls._include_tests(i_url, stream):
yield test
@classmethod
def _fail(cls, *args):
"""
Generates a (method, args) tuple that returns an error when called.
Allows for defering an error until the tests are actually run.
"""
return cls._test_error, args
@classmethod
def _write(cls, stream, string):
"""
Writes dianostic output to a file-like object.
:param stream:
Must have the methods .write() and .flush()
:param string:
The string to write - a newline will NOT be appended
"""
stream.write(string)
stream.flush()
@unittest.skipIf(
not userargs.channel or not os.path.isfile(userargs.channel),
"No {} found".format(userargs.channel)
)
class ChannelTests(TestContainer, unittest.TestCase):
maxDiff = None
@classmethod
def setUpClass(cls):
super(ChannelTests, cls).setUpClass()
cls.pre_generate()
# We need cls.j this for generating tests
@classmethod
def pre_generate(cls):
if not hasattr(cls, 'j') and os.path.isfile(userargs.channel):
with _open(userargs.channel) as f:
cls.source = f.read().decode('utf-8', 'strict')
cls.j = json.loads(cls.source)
from collections import defaultdict
cls.skipped_repositories = defaultdict(int)
@classmethod
def tearDownClass(cls):
if cls.skipped_repositories:
# TODO somehow pass stream here
print("Repositories skipped: %s" % dict(cls.skipped_repositories))
def test_channel_keys(self):
allowed_keys = ("$schema", 'repositories', 'schema_version')
keys = sorted(self.j.keys())
self.assertTrue(2 <= len(keys) <= len(allowed_keys), "Unexpected number of keys")
for k in keys:
self.assertIn(k, allowed_keys, "Unexpected key")
self.assertIn('schema_version', keys)
self.assertEqual(self.j['schema_version'], '3.0.0')
self.assertIn('repositories', keys)
self.assertIsInstance(self.j['repositories'], list)
for repo in self.j['repositories']:
self.assertIsInstance(repo, str)
def test_indentation(self):
return self._test_indentation(userargs.channel, self.source)
def test_channel_repositories(self):
repos = self.j['repositories']
for repo in repos:
self.assertRegex(repo, r"^(\.|https://)",
"Repositories must be relative urls or use the "
"HTTPS protocol")
self.assertEqual(repos, sorted(repos, key=str.lower),
"Repositories must be sorted alphabetically")
@classmethod
def generate_repository_tests(cls, stream):
if not userargs.test_repositories:
# Only generate tests for all repositories (those hosted online)
# when run with "--test-repositories" parameter.
return
stream.write("Fetching remote repositories:\n")
for repository in cls.j['repositories']:
if repository.startswith('.'):
continue
if not repository.startswith("http"):
cls._fail("Unexpected repository url: %s" % repository)
for test in cls._include_tests(repository, stream):
yield test
stream.write('\n')
stream.flush()
@unittest.skipIf(
not userargs.repository or not os.path.isfile(userargs.repository),
"No {} found".format(userargs.repository)
)
class RepositoryTests(TestContainer, unittest.TestCase):
maxDiff = None
@classmethod
def setUpClass(cls):
super(RepositoryTests, cls).setUpClass()
cls.pre_generate()
# We need cls.j this for generating tests
@classmethod
def pre_generate(cls):
if not hasattr(cls, 'j'):
with _open(userargs.repository) as f:
cls.source = f.read().decode('utf-8', 'strict')
cls.j = json.loads(cls.source)
def test_repository_keys(self):
self._test_repository_keys(userargs.repository, self.j)
def test_indentation(self):
return self._test_indentation(userargs.repository, self.source)
@classmethod
def generate_include_tests(cls, stream):
for include in cls.j.get('includes', []):
try:
with _open(include) as f:
contents = f.read().decode('utf-8', 'strict')
data = json.loads(contents)
except Exception as e:
yield cls._fail("strict while reading %r" % include, e)
continue
# `include` is for output during tests only
yield cls._test_indentation, (include, contents)
yield cls._test_repository_keys, (include, data)
yield cls._test_repository_package_names, (include, data)
for package in data['packages']:
yield cls._test_package, (include, package)
package_name = get_package_name(package)
if 'releases' in package:
for release in package['releases']:
(yield cls._test_release,
("%s (%s)" % (package_name, include),
release,
False))
if 'dependencies' in data:
yield cls._test_dependency_names, (include, data)
for dependency in data['dependencies']:
yield cls._test_dependency, (include, dependency)
dependency_name = get_package_name(dependency)
if 'releases' in dependency:
for release in dependency['releases']:
(yield cls._test_release,
("%s (%s)" % (dependency_name, include),
release,
True))
yield cls._fail("This won't be executed for some reason")
def generate_default_test_methods(stream=None):
if not stream:
stream = sys.stdout
generate_test_methods(RepositoryTests, stream)
generate_test_methods(ChannelTests, stream)
################################################################################
# Main
# When included to a Sublime package, sys.argv will not be set. We need to
# generate the test methods differently in that context, so we only generate
# them if sys.argv exists.
if hasattr(sys, 'argv'):
generate_default_test_methods()
if __name__ == '__main__':
unittest.main()