Coverage for test_syntax.py : 76%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# -*- coding: utf-8 -*-
2#@+leo-ver=5-thin
3#@+node:ekr.20210901140718.1: * @file ../unittests/test_syntax.py
4#@@first
5"""Syntax tests, including a check that Leo will continue to load!"""
6# pylint: disable=no-member
7import glob
8from leo.core import leoGlobals as g
9from leo.core.leoTest2 import LeoUnitTest
10#@+others
11#@+node:ekr.20210901140855.1: ** class TestSyntax(LeoUnitTest)
12class TestSyntax(LeoUnitTest):
13 """Unit tests checking syntax of Leo files."""
14 #@+others
15 #@+node:ekr.20210901140645.1: *3* TestSyntax.tests...
16 #@+node:ekr.20210910102910.1: *4* TestSyntax.check_syntax
17 def check_syntax(self, fileName, s):
18 """Called by a unit test to check the syntax of a file."""
19 try:
20 s = s.replace('\r', '')
21 tree = compile(s + '\n', fileName, 'exec')
22 del tree # #1454: Suppress -Wd ResourceWarning.
23 return True
24 except SyntaxError:
25 raise SyntaxError(fileName) # pylint: disable=raise-missing-from
26 except Exception:
27 g.trace("unexpected error in:", fileName)
28 raise
29 #@+node:ekr.20210901140645.21: *4* TestSyntax.test_syntax_of_all_files
30 def test_syntax_of_all_files(self):
31 skip_tuples = (
32 ('extensions', 'asciidoc.py'),
33 ('test', 'scriptFile.py'),
34 )
35 join = g.os_path_finalize_join
36 skip_list = [join(g.app.loadDir, '..', a, b) for a, b in skip_tuples]
37 n = 0
38 for theDir in ('core', 'external', 'extensions', 'modes', 'plugins', 'scripts', 'test'):
39 path = g.os_path_finalize_join(g.app.loadDir, '..', theDir)
40 self.assertTrue(g.os_path_exists(path), msg=path)
41 aList = glob.glob(g.os_path_join(path, '*.py'))
42 if g.isWindows:
43 aList = [z.replace('\\', '/') for z in aList]
44 for z in aList:
45 if z not in skip_list:
46 n += 1
47 fn = g.shortFileName(z)
48 s, e = g.readFileIntoString(z)
49 self.assertTrue(self.check_syntax(fn, s), msg=fn)
50 #@+node:ekr.20210901140645.22: *4* TestSyntax.test_syntax_of_setup_py
51 def test_syntax_of_setup_py(self):
52 fn = g.os_path_finalize_join(g.app.loadDir, '..', '..', 'setup.py')
53 # Only run this test if setup.py exists: it may not in the actual distribution.
54 if not g.os_path_exists(fn):
55 self.skipTest('setup.py not found')
56 s, e = g.readFileIntoString(fn)
57 assert self.check_syntax(fn, s)
58 #@+node:ekr.20210906062410.1: *4* TestSyntax.test_that_leo_starts
59 def test_that_leo_starts(self):
60 return self.skipTest('forbidden')
61 # It's possible that Leo can be corrupted without this test failing.
62 # However, the risk seems small enough!
63 if 1:
64 # Verify (weakly) that Leo's startup logic doesn't crash.
65 # Similar (but not exactly so) the startup code in runLeo.py
66 import importlib
67 import leo.core.leoApp as leoApp
68 # It's not clear that the reloads improve the test.
69 importlib.reload(g)
70 importlib.reload(leoApp)
71 assert g
72 assert leoApp
73 app = leoApp.LeoApp()
74 lm = leoApp.LoadManager()
75 assert app and lm
76 else:
77 # Run Leo in a separate process.
78 # Alas, this can leave files open in g.app.db,
79 # and it's not easy here to do anything about it
80 import subprocess
81 command = 'leo --quit --gui=null --no-plugins'
82 proc = subprocess.Popen(
83 command,
84 stdout=subprocess.DEVNULL,
85 shell=True)
86 proc.communicate()
87 #@-others
88#@-others
89#@-leo