comparison tests/run-tests.py @ 50210:6515d9a6592d stable

run-tests: make it possible to nest conditionals This is not that hard to implement and makes our life easier on a regular basis.
author Pierre-Yves David <pierre-yves.david@octobus.net>
date Tue, 31 Jan 2023 13:16:39 +0100
parents a2356e15200a
children a6b8b1ab9116
comparison
equal deleted inserted replaced
50180:be019ac8c1e4 50210:6515d9a6592d
1832 # Expected shell script output. 1832 # Expected shell script output.
1833 expected = {} 1833 expected = {}
1834 1834
1835 pos = prepos = -1 1835 pos = prepos = -1
1836 1836
1837 # True or False when in a true or false conditional section 1837 # The current stack of conditionnal section.
1838 skipping = None 1838 # Each relevant conditionnal section can have the following value:
1839 # - True: we should run this block
1840 # - False: we should skip this block
1841 # - None: The parent block is skipped,
1842 # (no branch of this one will ever run)
1843 condition_stack = []
1844
1845 def run_line():
1846 """return True if the current line should be run"""
1847 if not condition_stack:
1848 return True
1849 return bool(condition_stack[-1])
1850
1851 def push_conditional_block(should_run):
1852 """Push a new conditional context, with its initial state
1853
1854 i.e. entry a #if block"""
1855 if not run_line():
1856 condition_stack.append(None)
1857 else:
1858 condition_stack.append(should_run)
1859
1860 def flip_conditional():
1861 """reverse the current condition state
1862
1863 i.e. enter a #else
1864 """
1865 assert condition_stack
1866 if condition_stack[-1] is not None:
1867 condition_stack[-1] = not condition_stack[-1]
1868
1869 def pop_conditional():
1870 """exit the current skipping context
1871
1872 i.e. reach the #endif"""
1873 assert condition_stack
1874 condition_stack.pop()
1839 1875
1840 # We keep track of whether or not we're in a Python block so we 1876 # We keep track of whether or not we're in a Python block so we
1841 # can generate the surrounding doctest magic. 1877 # can generate the surrounding doctest magic.
1842 inpython = False 1878 inpython = False
1843 1879
1889 lsplit = l.split() 1925 lsplit = l.split()
1890 if len(lsplit) < 2 or lsplit[0] != b'#require': 1926 if len(lsplit) < 2 or lsplit[0] != b'#require':
1891 after.setdefault(pos, []).append( 1927 after.setdefault(pos, []).append(
1892 b' !!! invalid #require\n' 1928 b' !!! invalid #require\n'
1893 ) 1929 )
1894 if not skipping: 1930 if run_line():
1895 haveresult, message = self._hghave(lsplit[1:]) 1931 haveresult, message = self._hghave(lsplit[1:])
1896 if not haveresult: 1932 if not haveresult:
1897 script = [b'echo "%s"\nexit 80\n' % message] 1933 script = [b'echo "%s"\nexit 80\n' % message]
1898 break 1934 break
1899 after.setdefault(pos, []).append(l) 1935 after.setdefault(pos, []).append(l)
1900 elif l.startswith(b'#if'): 1936 elif l.startswith(b'#if'):
1901 lsplit = l.split() 1937 lsplit = l.split()
1902 if len(lsplit) < 2 or lsplit[0] != b'#if': 1938 if len(lsplit) < 2 or lsplit[0] != b'#if':
1903 after.setdefault(pos, []).append(b' !!! invalid #if\n') 1939 after.setdefault(pos, []).append(b' !!! invalid #if\n')
1904 if skipping is not None: 1940 push_conditional_block(self._iftest(lsplit[1:]))
1905 after.setdefault(pos, []).append(b' !!! nested #if\n')
1906 skipping = not self._iftest(lsplit[1:])
1907 after.setdefault(pos, []).append(l) 1941 after.setdefault(pos, []).append(l)
1908 elif l.startswith(b'#else'): 1942 elif l.startswith(b'#else'):
1909 if skipping is None: 1943 if not condition_stack:
1910 after.setdefault(pos, []).append(b' !!! missing #if\n') 1944 after.setdefault(pos, []).append(b' !!! missing #if\n')
1911 skipping = not skipping 1945 flip_conditional()
1912 after.setdefault(pos, []).append(l) 1946 after.setdefault(pos, []).append(l)
1913 elif l.startswith(b'#endif'): 1947 elif l.startswith(b'#endif'):
1914 if skipping is None: 1948 if not condition_stack:
1915 after.setdefault(pos, []).append(b' !!! missing #if\n') 1949 after.setdefault(pos, []).append(b' !!! missing #if\n')
1916 skipping = None 1950 pop_conditional()
1917 after.setdefault(pos, []).append(l) 1951 after.setdefault(pos, []).append(l)
1918 elif skipping: 1952 elif not run_line():
1919 after.setdefault(pos, []).append(l) 1953 after.setdefault(pos, []).append(l)
1920 elif l.startswith(b' >>> '): # python inlines 1954 elif l.startswith(b' >>> '): # python inlines
1921 after.setdefault(pos, []).append(l) 1955 after.setdefault(pos, []).append(l)
1922 prepos = pos 1956 prepos = pos
1923 pos = n 1957 pos = n
1958 # Non-command/result. Queue up for merged output. 1992 # Non-command/result. Queue up for merged output.
1959 after.setdefault(pos, []).append(l) 1993 after.setdefault(pos, []).append(l)
1960 1994
1961 if inpython: 1995 if inpython:
1962 script.append(b'EOF\n') 1996 script.append(b'EOF\n')
1963 if skipping is not None: 1997 if condition_stack:
1964 after.setdefault(pos, []).append(b' !!! missing #endif\n') 1998 after.setdefault(pos, []).append(b' !!! missing #endif\n')
1965 addsalt(n + 1, False) 1999 addsalt(n + 1, False)
1966 # Need to end any current per-command trace 2000 # Need to end any current per-command trace
1967 if activetrace: 2001 if activetrace:
1968 toggletrace() 2002 toggletrace()