tests/run-tests.py
author Augie Fackler <augie@google.com>
Tue, 14 Apr 2015 15:56:44 -0400
changeset 25038 66da89457c47
parent 25037 570141fa38f9
child 25039 8505eb1bafb1
permissions -rwxr-xr-x
run-tests: fix _findprogram to reliably return bytes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2110
25a8d116ab6a Add a pure python version of run-tests.
Stephen Darnell <stephen@darnell.plus.com>
parents:
diff changeset
     1
#!/usr/bin/env python
25a8d116ab6a Add a pure python version of run-tests.
Stephen Darnell <stephen@darnell.plus.com>
parents:
diff changeset
     2
#
25a8d116ab6a Add a pure python version of run-tests.
Stephen Darnell <stephen@darnell.plus.com>
parents:
diff changeset
     3
# run-tests.py - Run a set of tests on Mercurial
25a8d116ab6a Add a pure python version of run-tests.
Stephen Darnell <stephen@darnell.plus.com>
parents:
diff changeset
     4
#
25a8d116ab6a Add a pure python version of run-tests.
Stephen Darnell <stephen@darnell.plus.com>
parents:
diff changeset
     5
# Copyright 2006 Matt Mackall <mpm@selenic.com>
25a8d116ab6a Add a pure python version of run-tests.
Stephen Darnell <stephen@darnell.plus.com>
parents:
diff changeset
     6
#
8225
46293a0c7e9f updated license to be explicit about GPL version 2
Martin Geisler <mg@lazybytes.net>
parents: 8161
diff changeset
     7
# This software may be used and distributed according to the terms of the
10263
25e572394f5c Update license to GPLv2+
Matt Mackall <mpm@selenic.com>
parents: 10154
diff changeset
     8
# GNU General Public License version 2 or any later version.
2110
25a8d116ab6a Add a pure python version of run-tests.
Stephen Darnell <stephen@darnell.plus.com>
parents:
diff changeset
     9
8674
0941ee76489e run-tests: redefine --with-hg so it takes the 'hg' script to run.
Greg Ward <greg-hg@gerg.ca>
parents: 8673
diff changeset
    10
# Modifying this script is tricky because it has many modes:
0941ee76489e run-tests: redefine --with-hg so it takes the 'hg' script to run.
Greg Ward <greg-hg@gerg.ca>
parents: 8673
diff changeset
    11
#   - serial (default) vs parallel (-jN, N > 1)
0941ee76489e run-tests: redefine --with-hg so it takes the 'hg' script to run.
Greg Ward <greg-hg@gerg.ca>
parents: 8673
diff changeset
    12
#   - no coverage (default) vs coverage (-c, -C, -s)
0941ee76489e run-tests: redefine --with-hg so it takes the 'hg' script to run.
Greg Ward <greg-hg@gerg.ca>
parents: 8673
diff changeset
    13
#   - temp install (default) vs specific hg script (--with-hg, --local)
0941ee76489e run-tests: redefine --with-hg so it takes the 'hg' script to run.
Greg Ward <greg-hg@gerg.ca>
parents: 8673
diff changeset
    14
#   - tests are a mix of shell scripts and Python scripts
0941ee76489e run-tests: redefine --with-hg so it takes the 'hg' script to run.
Greg Ward <greg-hg@gerg.ca>
parents: 8673
diff changeset
    15
#
0941ee76489e run-tests: redefine --with-hg so it takes the 'hg' script to run.
Greg Ward <greg-hg@gerg.ca>
parents: 8673
diff changeset
    16
# If you change this script, it is recommended that you ensure you
0941ee76489e run-tests: redefine --with-hg so it takes the 'hg' script to run.
Greg Ward <greg-hg@gerg.ca>
parents: 8673
diff changeset
    17
# haven't broken it by running it in various modes with a representative
0941ee76489e run-tests: redefine --with-hg so it takes the 'hg' script to run.
Greg Ward <greg-hg@gerg.ca>
parents: 8673
diff changeset
    18
# sample of test scripts.  For example:
8843
eb7b247a98ea kill trailing whitespace
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 8724
diff changeset
    19
#
8674
0941ee76489e run-tests: redefine --with-hg so it takes the 'hg' script to run.
Greg Ward <greg-hg@gerg.ca>
parents: 8673
diff changeset
    20
#  1) serial, no coverage, temp install:
0941ee76489e run-tests: redefine --with-hg so it takes the 'hg' script to run.
Greg Ward <greg-hg@gerg.ca>
parents: 8673
diff changeset
    21
#      ./run-tests.py test-s*
0941ee76489e run-tests: redefine --with-hg so it takes the 'hg' script to run.
Greg Ward <greg-hg@gerg.ca>
parents: 8673
diff changeset
    22
#  2) serial, no coverage, local hg:
0941ee76489e run-tests: redefine --with-hg so it takes the 'hg' script to run.
Greg Ward <greg-hg@gerg.ca>
parents: 8673
diff changeset
    23
#      ./run-tests.py --local test-s*
0941ee76489e run-tests: redefine --with-hg so it takes the 'hg' script to run.
Greg Ward <greg-hg@gerg.ca>
parents: 8673
diff changeset
    24
#  3) serial, coverage, temp install:
0941ee76489e run-tests: redefine --with-hg so it takes the 'hg' script to run.
Greg Ward <greg-hg@gerg.ca>
parents: 8673
diff changeset
    25
#      ./run-tests.py -c test-s*
0941ee76489e run-tests: redefine --with-hg so it takes the 'hg' script to run.
Greg Ward <greg-hg@gerg.ca>
parents: 8673
diff changeset
    26
#  4) serial, coverage, local hg:
0941ee76489e run-tests: redefine --with-hg so it takes the 'hg' script to run.
Greg Ward <greg-hg@gerg.ca>
parents: 8673
diff changeset
    27
#      ./run-tests.py -c --local test-s*      # unsupported
0941ee76489e run-tests: redefine --with-hg so it takes the 'hg' script to run.
Greg Ward <greg-hg@gerg.ca>
parents: 8673
diff changeset
    28
#  5) parallel, no coverage, temp install:
0941ee76489e run-tests: redefine --with-hg so it takes the 'hg' script to run.
Greg Ward <greg-hg@gerg.ca>
parents: 8673
diff changeset
    29
#      ./run-tests.py -j2 test-s*
0941ee76489e run-tests: redefine --with-hg so it takes the 'hg' script to run.
Greg Ward <greg-hg@gerg.ca>
parents: 8673
diff changeset
    30
#  6) parallel, no coverage, local hg:
0941ee76489e run-tests: redefine --with-hg so it takes the 'hg' script to run.
Greg Ward <greg-hg@gerg.ca>
parents: 8673
diff changeset
    31
#      ./run-tests.py -j2 --local test-s*
0941ee76489e run-tests: redefine --with-hg so it takes the 'hg' script to run.
Greg Ward <greg-hg@gerg.ca>
parents: 8673
diff changeset
    32
#  7) parallel, coverage, temp install:
0941ee76489e run-tests: redefine --with-hg so it takes the 'hg' script to run.
Greg Ward <greg-hg@gerg.ca>
parents: 8673
diff changeset
    33
#      ./run-tests.py -j2 -c test-s*          # currently broken
9899
be574a37a8ae run-tests: give each child its own tmp dir (issue1911)
Greg Ward <greg@gerg.ca>
parents: 9707
diff changeset
    34
#  8) parallel, coverage, local install:
8674
0941ee76489e run-tests: redefine --with-hg so it takes the 'hg' script to run.
Greg Ward <greg-hg@gerg.ca>
parents: 8673
diff changeset
    35
#      ./run-tests.py -j2 -c --local test-s*  # unsupported (and broken)
9899
be574a37a8ae run-tests: give each child its own tmp dir (issue1911)
Greg Ward <greg@gerg.ca>
parents: 9707
diff changeset
    36
#  9) parallel, custom tmp dir:
be574a37a8ae run-tests: give each child its own tmp dir (issue1911)
Greg Ward <greg@gerg.ca>
parents: 9707
diff changeset
    37
#      ./run-tests.py -j2 --tmpdir /tmp/myhgtests
8674
0941ee76489e run-tests: redefine --with-hg so it takes the 'hg' script to run.
Greg Ward <greg-hg@gerg.ca>
parents: 8673
diff changeset
    38
#
0941ee76489e run-tests: redefine --with-hg so it takes the 'hg' script to run.
Greg Ward <greg-hg@gerg.ca>
parents: 8673
diff changeset
    39
# (You could use any subset of the tests: test-s* happens to match
0941ee76489e run-tests: redefine --with-hg so it takes the 'hg' script to run.
Greg Ward <greg-hg@gerg.ca>
parents: 8673
diff changeset
    40
# enough that it's worth doing parallel runs, few enough that it
0941ee76489e run-tests: redefine --with-hg so it takes the 'hg' script to run.
Greg Ward <greg-hg@gerg.ca>
parents: 8673
diff changeset
    41
# completes fairly quickly, includes both shell and Python scripts, and
0941ee76489e run-tests: redefine --with-hg so it takes the 'hg' script to run.
Greg Ward <greg-hg@gerg.ca>
parents: 8673
diff changeset
    42
# includes some scripts that run daemon processes.)
0941ee76489e run-tests: redefine --with-hg so it takes the 'hg' script to run.
Greg Ward <greg-hg@gerg.ca>
parents: 8673
diff changeset
    43
25031
0adc22a0b6b3 python3: update killdaemons and run-tests print and exception syntax
Augie Fackler <augie@google.com>
parents: 24984
diff changeset
    44
from __future__ import print_function
0adc22a0b6b3 python3: update killdaemons and run-tests print and exception syntax
Augie Fackler <augie@google.com>
parents: 24984
diff changeset
    45
10648
58128004cca1 tests: use external coverage, mandate newer version
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents: 10413
diff changeset
    46
from distutils import version
2571
83cfd95eafb5 tests: add timeouts, make run-tests.py clean up dead daemon processes
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2570
diff changeset
    47
import difflib
83cfd95eafb5 tests: add timeouts, make run-tests.py clean up dead daemon processes
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2570
diff changeset
    48
import errno
83cfd95eafb5 tests: add timeouts, make run-tests.py clean up dead daemon processes
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2570
diff changeset
    49
import optparse
83cfd95eafb5 tests: add timeouts, make run-tests.py clean up dead daemon processes
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2570
diff changeset
    50
import os
10905
13a1b2fb7ef2 pylint, pyflakes: remove unused or duplicate imports
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 10903
diff changeset
    51
import shutil
8280
0b02d98d44d0 util: always use subprocess
Martin Geisler <mg@lazybytes.net>
parents: 8258
diff changeset
    52
import subprocess
2571
83cfd95eafb5 tests: add timeouts, make run-tests.py clean up dead daemon processes
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2570
diff changeset
    53
import signal
24967
00790cc2b753 run-test: ensure the test ports are available before launching test
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24965
diff changeset
    54
import socket
2571
83cfd95eafb5 tests: add timeouts, make run-tests.py clean up dead daemon processes
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2570
diff changeset
    55
import sys
2110
25a8d116ab6a Add a pure python version of run-tests.
Stephen Darnell <stephen@darnell.plus.com>
parents:
diff changeset
    56
import tempfile
2571
83cfd95eafb5 tests: add timeouts, make run-tests.py clean up dead daemon processes
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2570
diff changeset
    57
import time
18616
35b4affe6fdd test: display used python hash seed
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 18565
diff changeset
    58
import random
11741
431e2bf37ae7 tests: basic support for unified tests
Matt Mackall <mpm@selenic.com>
parents: 11740
diff changeset
    59
import re
14000
636a6f5aa2cd run-tests: add locking on results struct
Matt Mackall <mpm@selenic.com>
parents: 13999
diff changeset
    60
import threading
17464
eddfb9a550d0 run-tests: do not duplicate killdaemons() code
Patrick Mezard <patrick@mezard.eu>
parents: 16906
diff changeset
    61
import killdaemons as killmod
25031
0adc22a0b6b3 python3: update killdaemons and run-tests print and exception syntax
Augie Fackler <augie@google.com>
parents: 24984
diff changeset
    62
try:
0adc22a0b6b3 python3: update killdaemons and run-tests print and exception syntax
Augie Fackler <augie@google.com>
parents: 24984
diff changeset
    63
    import Queue as queue
0adc22a0b6b3 python3: update killdaemons and run-tests print and exception syntax
Augie Fackler <augie@google.com>
parents: 24984
diff changeset
    64
except ImportError:
0adc22a0b6b3 python3: update killdaemons and run-tests print and exception syntax
Augie Fackler <augie@google.com>
parents: 24984
diff changeset
    65
    import queue
22044
a06172e85fd4 run-tests: add support for xunit test reports
Augie Fackler <raf@durin42.com>
parents: 21997
diff changeset
    66
from xml.dom import minidom
21426
791bdd65acd3 run-tests: initial support for running tests with unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21388
diff changeset
    67
import unittest
2110
25a8d116ab6a Add a pure python version of run-tests.
Stephen Darnell <stephen@darnell.plus.com>
parents:
diff changeset
    68
25037
570141fa38f9 run-tests: introduce a name for os.environb
Augie Fackler <augie@google.com>
parents: 25036
diff changeset
    69
osenvironb = getattr(os, 'environb', os.environ)
570141fa38f9 run-tests: introduce a name for os.environb
Augie Fackler <augie@google.com>
parents: 25036
diff changeset
    70
22391
c42e69268f5b run-tests: added '--json' functionality to store test result in json file
anuraggoel <anurag.dsps@gmail.com>
parents: 22361
diff changeset
    71
try:
23263
dd51abf0aa17 run-tests: use a try/except ladder instead of looking for a specific version
Augie Fackler <raf@durin42.com>
parents: 23139
diff changeset
    72
    import json
dd51abf0aa17 run-tests: use a try/except ladder instead of looking for a specific version
Augie Fackler <raf@durin42.com>
parents: 23139
diff changeset
    73
except ImportError:
dd51abf0aa17 run-tests: use a try/except ladder instead of looking for a specific version
Augie Fackler <raf@durin42.com>
parents: 23139
diff changeset
    74
    try:
22391
c42e69268f5b run-tests: added '--json' functionality to store test result in json file
anuraggoel <anurag.dsps@gmail.com>
parents: 22361
diff changeset
    75
        import simplejson as json
23263
dd51abf0aa17 run-tests: use a try/except ladder instead of looking for a specific version
Augie Fackler <raf@durin42.com>
parents: 23139
diff changeset
    76
    except ImportError:
dd51abf0aa17 run-tests: use a try/except ladder instead of looking for a specific version
Augie Fackler <raf@durin42.com>
parents: 23139
diff changeset
    77
        json = None
22391
c42e69268f5b run-tests: added '--json' functionality to store test result in json file
anuraggoel <anurag.dsps@gmail.com>
parents: 22361
diff changeset
    78
14019
fbbd5f91d5e1 run-tests: do chdir for tests under a lock for thread safety
Matt Mackall <mpm@selenic.com>
parents: 14018
diff changeset
    79
processlock = threading.Lock()
19413
a4de0d3dc35a run-tests: lock popen wait/poll
Brendan Cully <brendan@kublai.com>
parents: 19407
diff changeset
    80
25033
2bdd9e442bcc run-tests: work around the rename of xrange to range
Augie Fackler <augie@google.com>
parents: 25031
diff changeset
    81
if sys.version_info > (3, 0, 0):
2bdd9e442bcc run-tests: work around the rename of xrange to range
Augie Fackler <augie@google.com>
parents: 25031
diff changeset
    82
    xrange = range # we use xrange in one place, and we'd rather not use range
2bdd9e442bcc run-tests: work around the rename of xrange to range
Augie Fackler <augie@google.com>
parents: 25031
diff changeset
    83
19415
a0699972e75a run-tests: replace popen locking with a noop _cleanup handler on py24
Brendan Cully <brendan@kublai.com>
parents: 19414
diff changeset
    84
# subprocess._cleanup can race with any Popen.wait or Popen.poll on py24
a0699972e75a run-tests: replace popen locking with a noop _cleanup handler on py24
Brendan Cully <brendan@kublai.com>
parents: 19414
diff changeset
    85
# http://bugs.python.org/issue1731717 for details. We shouldn't be producing
a0699972e75a run-tests: replace popen locking with a noop _cleanup handler on py24
Brendan Cully <brendan@kublai.com>
parents: 19414
diff changeset
    86
# zombies but it's pretty harmless even if we do.
20219
f694cd81b600 run-tests: better check for python version
Simon Heimberg <simohe@besonet.ch>
parents: 20046
diff changeset
    87
if sys.version_info < (2, 5):
19415
a0699972e75a run-tests: replace popen locking with a noop _cleanup handler on py24
Brendan Cully <brendan@kublai.com>
parents: 19414
diff changeset
    88
    subprocess._cleanup = lambda: None
14019
fbbd5f91d5e1 run-tests: do chdir for tests under a lock for thread safety
Matt Mackall <mpm@selenic.com>
parents: 14018
diff changeset
    89
24508
fbe2fb71a6e6 run-tests: move run into Test class
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24507
diff changeset
    90
wifexited = getattr(os, "WIFEXITED", lambda x: False)
fbe2fb71a6e6 run-tests: move run into Test class
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24507
diff changeset
    91
24967
00790cc2b753 run-test: ensure the test ports are available before launching test
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24965
diff changeset
    92
def checkportisavailable(port):
00790cc2b753 run-test: ensure the test ports are available before launching test
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24965
diff changeset
    93
    """return true if a port seems free to bind on localhost"""
00790cc2b753 run-test: ensure the test ports are available before launching test
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24965
diff changeset
    94
    try:
00790cc2b753 run-test: ensure the test ports are available before launching test
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24965
diff changeset
    95
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
00790cc2b753 run-test: ensure the test ports are available before launching test
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24965
diff changeset
    96
        s.bind(('localhost', port))
00790cc2b753 run-test: ensure the test ports are available before launching test
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24965
diff changeset
    97
        s.close()
00790cc2b753 run-test: ensure the test ports are available before launching test
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24965
diff changeset
    98
        return True
25031
0adc22a0b6b3 python3: update killdaemons and run-tests print and exception syntax
Augie Fackler <augie@google.com>
parents: 24984
diff changeset
    99
    except socket.error as exc:
24967
00790cc2b753 run-test: ensure the test ports are available before launching test
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24965
diff changeset
   100
        if not exc.errno == errno.EADDRINUSE:
00790cc2b753 run-test: ensure the test ports are available before launching test
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24965
diff changeset
   101
            raise
00790cc2b753 run-test: ensure the test ports are available before launching test
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24965
diff changeset
   102
        return False
00790cc2b753 run-test: ensure the test ports are available before launching test
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24965
diff changeset
   103
8280
0b02d98d44d0 util: always use subprocess
Martin Geisler <mg@lazybytes.net>
parents: 8258
diff changeset
   104
closefds = os.name == 'posix'
19262
7864e8f274fe run-tests: add env dict to isolate test environment
Matt Mackall <mpm@selenic.com>
parents: 19252
diff changeset
   105
def Popen4(cmd, wd, timeout, env=None):
14019
fbbd5f91d5e1 run-tests: do chdir for tests under a lock for thread safety
Matt Mackall <mpm@selenic.com>
parents: 14018
diff changeset
   106
    processlock.acquire()
19262
7864e8f274fe run-tests: add env dict to isolate test environment
Matt Mackall <mpm@selenic.com>
parents: 19252
diff changeset
   107
    p = subprocess.Popen(cmd, shell=True, bufsize=-1, cwd=wd, env=env,
8280
0b02d98d44d0 util: always use subprocess
Martin Geisler <mg@lazybytes.net>
parents: 8258
diff changeset
   108
                         close_fds=closefds,
0b02d98d44d0 util: always use subprocess
Martin Geisler <mg@lazybytes.net>
parents: 8258
diff changeset
   109
                         stdin=subprocess.PIPE, stdout=subprocess.PIPE,
0b02d98d44d0 util: always use subprocess
Martin Geisler <mg@lazybytes.net>
parents: 8258
diff changeset
   110
                         stderr=subprocess.STDOUT)
14019
fbbd5f91d5e1 run-tests: do chdir for tests under a lock for thread safety
Matt Mackall <mpm@selenic.com>
parents: 14018
diff changeset
   111
    processlock.release()
fbbd5f91d5e1 run-tests: do chdir for tests under a lock for thread safety
Matt Mackall <mpm@selenic.com>
parents: 14018
diff changeset
   112
8280
0b02d98d44d0 util: always use subprocess
Martin Geisler <mg@lazybytes.net>
parents: 8258
diff changeset
   113
    p.fromchild = p.stdout
0b02d98d44d0 util: always use subprocess
Martin Geisler <mg@lazybytes.net>
parents: 8258
diff changeset
   114
    p.tochild = p.stdin
0b02d98d44d0 util: always use subprocess
Martin Geisler <mg@lazybytes.net>
parents: 8258
diff changeset
   115
    p.childerr = p.stderr
14001
9c4da6ab4e5a run-tests: switch timeout handling from alarm to helper thread
Matt Mackall <mpm@selenic.com>
parents: 14000
diff changeset
   116
14337
439ed4721a6d run-tests: ignore timeout when Popen.terminate is unavailable
Patrick Mezard <pmezard@gmail.com>
parents: 14336
diff changeset
   117
    p.timeout = False
14001
9c4da6ab4e5a run-tests: switch timeout handling from alarm to helper thread
Matt Mackall <mpm@selenic.com>
parents: 14000
diff changeset
   118
    if timeout:
9c4da6ab4e5a run-tests: switch timeout handling from alarm to helper thread
Matt Mackall <mpm@selenic.com>
parents: 14000
diff changeset
   119
        def t():
9c4da6ab4e5a run-tests: switch timeout handling from alarm to helper thread
Matt Mackall <mpm@selenic.com>
parents: 14000
diff changeset
   120
            start = time.time()
9c4da6ab4e5a run-tests: switch timeout handling from alarm to helper thread
Matt Mackall <mpm@selenic.com>
parents: 14000
diff changeset
   121
            while time.time() - start < timeout and p.returncode is None:
16346
48692b416fbc tests: shorten post-test sleeps
Matt Mackall <mpm@selenic.com>
parents: 15942
diff changeset
   122
                time.sleep(.1)
14001
9c4da6ab4e5a run-tests: switch timeout handling from alarm to helper thread
Matt Mackall <mpm@selenic.com>
parents: 14000
diff changeset
   123
            p.timeout = True
9c4da6ab4e5a run-tests: switch timeout handling from alarm to helper thread
Matt Mackall <mpm@selenic.com>
parents: 14000
diff changeset
   124
            if p.returncode is None:
14821
2017495bd552 run-tests: fallback to SIGTERM if subprocess.Popen does not have terminate()
Thomas Arendsen Hein <thomas@intevation.de>
parents: 14598
diff changeset
   125
                terminate(p)
14001
9c4da6ab4e5a run-tests: switch timeout handling from alarm to helper thread
Matt Mackall <mpm@selenic.com>
parents: 14000
diff changeset
   126
        threading.Thread(target=t).start()
9c4da6ab4e5a run-tests: switch timeout handling from alarm to helper thread
Matt Mackall <mpm@selenic.com>
parents: 14000
diff changeset
   127
8280
0b02d98d44d0 util: always use subprocess
Martin Geisler <mg@lazybytes.net>
parents: 8258
diff changeset
   128
    return p
0b02d98d44d0 util: always use subprocess
Martin Geisler <mg@lazybytes.net>
parents: 8258
diff changeset
   129
15448
873f94ecd706 run-tests: convert windows paths to unix
Mads Kiilerich <mads@kiilerich.com>
parents: 15447
diff changeset
   130
PYTHON = sys.executable.replace('\\', '/')
10758
2ed667a9dfcb tests: adapt the test runner to work with jython
Ronny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
parents: 10750
diff changeset
   131
IMPL_PATH = 'PYTHONPATH'
2ed667a9dfcb tests: adapt the test runner to work with jython
Ronny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
parents: 10750
diff changeset
   132
if 'java' in sys.platform:
2ed667a9dfcb tests: adapt the test runner to work with jython
Ronny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
parents: 10750
diff changeset
   133
    IMPL_PATH = 'JYTHONPATH'
4881
c51c9bc4579d Add hghave utility and run-tests.py support.
Patrick Mezard <pmezard@gmail.com>
parents: 4880
diff changeset
   134
6366
07c3cd695b48 run-tests.py: Allow environment variables to set jobs/timeout/port.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 6343
diff changeset
   135
defaults = {
07c3cd695b48 run-tests.py: Allow environment variables to set jobs/timeout/port.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 6343
diff changeset
   136
    'jobs': ('HGTEST_JOBS', 1),
07c3cd695b48 run-tests.py: Allow environment variables to set jobs/timeout/port.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 6343
diff changeset
   137
    'timeout': ('HGTEST_TIMEOUT', 180),
07c3cd695b48 run-tests.py: Allow environment variables to set jobs/timeout/port.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 6343
diff changeset
   138
    'port': ('HGTEST_PORT', 20059),
15941
af289d6cd422 tests: let run-tests.py default to use 'sh' in $PATH instead of '/bin/sh'
Mads Kiilerich <mads@kiilerich.com>
parents: 15940
diff changeset
   139
    'shell': ('HGTEST_SHELL', 'sh'),
6366
07c3cd695b48 run-tests.py: Allow environment variables to set jobs/timeout/port.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 6343
diff changeset
   140
}
07c3cd695b48 run-tests.py: Allow environment variables to set jobs/timeout/port.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 6343
diff changeset
   141
14493
5cc7905bccc9 run-tests: allow whitelisting tests that should always run
Augie Fackler <durin42@gmail.com>
parents: 14454
diff changeset
   142
def parselistfiles(files, listtype, warn=True):
5cc7905bccc9 run-tests: allow whitelisting tests that should always run
Augie Fackler <durin42@gmail.com>
parents: 14454
diff changeset
   143
    entries = dict()
5cc7905bccc9 run-tests: allow whitelisting tests that should always run
Augie Fackler <durin42@gmail.com>
parents: 14454
diff changeset
   144
    for filename in files:
5cc7905bccc9 run-tests: allow whitelisting tests that should always run
Augie Fackler <durin42@gmail.com>
parents: 14454
diff changeset
   145
        try:
5cc7905bccc9 run-tests: allow whitelisting tests that should always run
Augie Fackler <durin42@gmail.com>
parents: 14454
diff changeset
   146
            path = os.path.expanduser(os.path.expandvars(filename))
21916
792ebd7dc5f6 run-tests: write out scripts in binary mode
Augie Fackler <raf@durin42.com>
parents: 21763
diff changeset
   147
            f = open(path, "rb")
25031
0adc22a0b6b3 python3: update killdaemons and run-tests print and exception syntax
Augie Fackler <augie@google.com>
parents: 24984
diff changeset
   148
        except IOError as err:
14493
5cc7905bccc9 run-tests: allow whitelisting tests that should always run
Augie Fackler <durin42@gmail.com>
parents: 14454
diff changeset
   149
            if err.errno != errno.ENOENT:
5cc7905bccc9 run-tests: allow whitelisting tests that should always run
Augie Fackler <durin42@gmail.com>
parents: 14454
diff changeset
   150
                raise
5cc7905bccc9 run-tests: allow whitelisting tests that should always run
Augie Fackler <durin42@gmail.com>
parents: 14454
diff changeset
   151
            if warn:
25031
0adc22a0b6b3 python3: update killdaemons and run-tests print and exception syntax
Augie Fackler <augie@google.com>
parents: 24984
diff changeset
   152
                print("warning: no such %s file: %s" % (listtype, filename))
14493
5cc7905bccc9 run-tests: allow whitelisting tests that should always run
Augie Fackler <durin42@gmail.com>
parents: 14454
diff changeset
   153
            continue
5cc7905bccc9 run-tests: allow whitelisting tests that should always run
Augie Fackler <durin42@gmail.com>
parents: 14454
diff changeset
   154
5cc7905bccc9 run-tests: allow whitelisting tests that should always run
Augie Fackler <durin42@gmail.com>
parents: 14454
diff changeset
   155
        for line in f.readlines():
5cc7905bccc9 run-tests: allow whitelisting tests that should always run
Augie Fackler <durin42@gmail.com>
parents: 14454
diff changeset
   156
            line = line.split('#', 1)[0].strip()
5cc7905bccc9 run-tests: allow whitelisting tests that should always run
Augie Fackler <durin42@gmail.com>
parents: 14454
diff changeset
   157
            if line:
5cc7905bccc9 run-tests: allow whitelisting tests that should always run
Augie Fackler <durin42@gmail.com>
parents: 14454
diff changeset
   158
                entries[line] = filename
5cc7905bccc9 run-tests: allow whitelisting tests that should always run
Augie Fackler <durin42@gmail.com>
parents: 14454
diff changeset
   159
5cc7905bccc9 run-tests: allow whitelisting tests that should always run
Augie Fackler <durin42@gmail.com>
parents: 14454
diff changeset
   160
        f.close()
5cc7905bccc9 run-tests: allow whitelisting tests that should always run
Augie Fackler <durin42@gmail.com>
parents: 14454
diff changeset
   161
    return entries
5cc7905bccc9 run-tests: allow whitelisting tests that should always run
Augie Fackler <durin42@gmail.com>
parents: 14454
diff changeset
   162
21008
c1dd04be3d9a run-tests: allow option parser to be extended
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21007
diff changeset
   163
def getparser():
21383
772ed56e2519 run-tests: add some docstrings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21382
diff changeset
   164
    """Obtain the OptionParser used by the CLI."""
8091
e85cc856d2e1 run-tests: factor out parse_args(). Clarify use of globals a bit.
Greg Ward <greg-hg@gerg.ca>
parents: 8060
diff changeset
   165
    parser = optparse.OptionParser("%prog [options] [tests]")
11039
cf0a309f6c4d run-tests: sort options
Matt Mackall <mpm@selenic.com>
parents: 11038
diff changeset
   166
cf0a309f6c4d run-tests: sort options
Matt Mackall <mpm@selenic.com>
parents: 11038
diff changeset
   167
    # keep these sorted
cf0a309f6c4d run-tests: sort options
Matt Mackall <mpm@selenic.com>
parents: 11038
diff changeset
   168
    parser.add_option("--blacklist", action="append",
cf0a309f6c4d run-tests: sort options
Matt Mackall <mpm@selenic.com>
parents: 11038
diff changeset
   169
        help="skip tests listed in the specified blacklist file")
14493
5cc7905bccc9 run-tests: allow whitelisting tests that should always run
Augie Fackler <durin42@gmail.com>
parents: 14454
diff changeset
   170
    parser.add_option("--whitelist", action="append",
5cc7905bccc9 run-tests: allow whitelisting tests that should always run
Augie Fackler <durin42@gmail.com>
parents: 14454
diff changeset
   171
        help="always run tests listed in the specified whitelist file")
20821
3d1d16b19e7d tests: add run-tests --changed option for running tests changed in revisions
Mads Kiilerich <madski@unity3d.com>
parents: 20793
diff changeset
   172
    parser.add_option("--changed", type="string",
3d1d16b19e7d tests: add run-tests --changed option for running tests changed in revisions
Mads Kiilerich <madski@unity3d.com>
parents: 20793
diff changeset
   173
        help="run tests that are changed in parent rev or working directory")
8091
e85cc856d2e1 run-tests: factor out parse_args(). Clarify use of globals a bit.
Greg Ward <greg-hg@gerg.ca>
parents: 8060
diff changeset
   174
    parser.add_option("-C", "--annotate", action="store_true",
e85cc856d2e1 run-tests: factor out parse_args(). Clarify use of globals a bit.
Greg Ward <greg-hg@gerg.ca>
parents: 8060
diff changeset
   175
        help="output files annotated with coverage")
e85cc856d2e1 run-tests: factor out parse_args(). Clarify use of globals a bit.
Greg Ward <greg-hg@gerg.ca>
parents: 8060
diff changeset
   176
    parser.add_option("-c", "--cover", action="store_true",
e85cc856d2e1 run-tests: factor out parse_args(). Clarify use of globals a bit.
Greg Ward <greg-hg@gerg.ca>
parents: 8060
diff changeset
   177
        help="print a test coverage report")
11039
cf0a309f6c4d run-tests: sort options
Matt Mackall <mpm@selenic.com>
parents: 11038
diff changeset
   178
    parser.add_option("-d", "--debug", action="store_true",
cf0a309f6c4d run-tests: sort options
Matt Mackall <mpm@selenic.com>
parents: 11038
diff changeset
   179
        help="debug mode: write output of test scripts to console"
21024
7731a2281cf0 spelling: fixes from spell checker
Mads Kiilerich <madski@unity3d.com>
parents: 21023
diff changeset
   180
             " rather than capturing and diffing it (disables timeout)")
8091
e85cc856d2e1 run-tests: factor out parse_args(). Clarify use of globals a bit.
Greg Ward <greg-hg@gerg.ca>
parents: 8060
diff changeset
   181
    parser.add_option("-f", "--first", action="store_true",
e85cc856d2e1 run-tests: factor out parse_args(). Clarify use of globals a bit.
Greg Ward <greg-hg@gerg.ca>
parents: 8060
diff changeset
   182
        help="exit on the first test failure")
15859
44a371823f83 tests: add htmlcov option
Markus Zapke-Gründemann <info@keimlink.de>
parents: 15858
diff changeset
   183
    parser.add_option("-H", "--htmlcov", action="store_true",
44a371823f83 tests: add htmlcov option
Markus Zapke-Gründemann <info@keimlink.de>
parents: 15858
diff changeset
   184
        help="create an HTML report of the coverage of the files")
8091
e85cc856d2e1 run-tests: factor out parse_args(). Clarify use of globals a bit.
Greg Ward <greg-hg@gerg.ca>
parents: 8060
diff changeset
   185
    parser.add_option("-i", "--interactive", action="store_true",
e85cc856d2e1 run-tests: factor out parse_args(). Clarify use of globals a bit.
Greg Ward <greg-hg@gerg.ca>
parents: 8060
diff changeset
   186
        help="prompt to accept changed output")
e85cc856d2e1 run-tests: factor out parse_args(). Clarify use of globals a bit.
Greg Ward <greg-hg@gerg.ca>
parents: 8060
diff changeset
   187
    parser.add_option("-j", "--jobs", type="int",
e85cc856d2e1 run-tests: factor out parse_args(). Clarify use of globals a bit.
Greg Ward <greg-hg@gerg.ca>
parents: 8060
diff changeset
   188
        help="number of jobs to run in parallel"
e85cc856d2e1 run-tests: factor out parse_args(). Clarify use of globals a bit.
Greg Ward <greg-hg@gerg.ca>
parents: 8060
diff changeset
   189
             " (default: $%s or %d)" % defaults['jobs'])
e85cc856d2e1 run-tests: factor out parse_args(). Clarify use of globals a bit.
Greg Ward <greg-hg@gerg.ca>
parents: 8060
diff changeset
   190
    parser.add_option("--keep-tmpdir", action="store_true",
9706
f8b4df4b033d run-tests: make --tmpdir option more useful.
Greg Ward <greg-hg@gerg.ca>
parents: 9582
diff changeset
   191
        help="keep temporary directory after running tests")
11039
cf0a309f6c4d run-tests: sort options
Matt Mackall <mpm@selenic.com>
parents: 11038
diff changeset
   192
    parser.add_option("-k", "--keywords",
cf0a309f6c4d run-tests: sort options
Matt Mackall <mpm@selenic.com>
parents: 11038
diff changeset
   193
        help="run tests matching keywords")
cf0a309f6c4d run-tests: sort options
Matt Mackall <mpm@selenic.com>
parents: 11038
diff changeset
   194
    parser.add_option("-l", "--local", action="store_true",
cf0a309f6c4d run-tests: sort options
Matt Mackall <mpm@selenic.com>
parents: 11038
diff changeset
   195
        help="shortcut for --with-hg=<testdir>/../hg")
19283
8300adf9ca33 run-tests: add --loop support
Matt Mackall <mpm@selenic.com>
parents: 19282
diff changeset
   196
    parser.add_option("--loop", action="store_true",
8300adf9ca33 run-tests: add --loop support
Matt Mackall <mpm@selenic.com>
parents: 19282
diff changeset
   197
        help="loop tests repeatedly")
24329
e7ca4d4b10e1 run-tests: add --runs-per-test flag
Augie Fackler <augie@google.com>
parents: 24306
diff changeset
   198
    parser.add_option("--runs-per-test", type="int", dest="runs_per_test",
e7ca4d4b10e1 run-tests: add --runs-per-test flag
Augie Fackler <augie@google.com>
parents: 24306
diff changeset
   199
        help="run each test N times (default=1)", default=1)
11039
cf0a309f6c4d run-tests: sort options
Matt Mackall <mpm@selenic.com>
parents: 11038
diff changeset
   200
    parser.add_option("-n", "--nodiff", action="store_true",
cf0a309f6c4d run-tests: sort options
Matt Mackall <mpm@selenic.com>
parents: 11038
diff changeset
   201
        help="skip showing test changes")
8091
e85cc856d2e1 run-tests: factor out parse_args(). Clarify use of globals a bit.
Greg Ward <greg-hg@gerg.ca>
parents: 8060
diff changeset
   202
    parser.add_option("-p", "--port", type="int",
e85cc856d2e1 run-tests: factor out parse_args(). Clarify use of globals a bit.
Greg Ward <greg-hg@gerg.ca>
parents: 8060
diff changeset
   203
        help="port on which servers should listen"
e85cc856d2e1 run-tests: factor out parse_args(). Clarify use of globals a bit.
Greg Ward <greg-hg@gerg.ca>
parents: 8060
diff changeset
   204
             " (default: $%s or %d)" % defaults['port'])
17966
ae20cde050c2 run-tests: add a --compiler option
Bryan O'Sullivan <bryano@fb.com>
parents: 17965
diff changeset
   205
    parser.add_option("--compiler", type="string",
ae20cde050c2 run-tests: add a --compiler option
Bryan O'Sullivan <bryano@fb.com>
parents: 17965
diff changeset
   206
        help="compiler to build with")
11039
cf0a309f6c4d run-tests: sort options
Matt Mackall <mpm@selenic.com>
parents: 11038
diff changeset
   207
    parser.add_option("--pure", action="store_true",
cf0a309f6c4d run-tests: sort options
Matt Mackall <mpm@selenic.com>
parents: 11038
diff changeset
   208
        help="use pure Python code instead of C extensions")
cf0a309f6c4d run-tests: sort options
Matt Mackall <mpm@selenic.com>
parents: 11038
diff changeset
   209
    parser.add_option("-R", "--restart", action="store_true",
cf0a309f6c4d run-tests: sort options
Matt Mackall <mpm@selenic.com>
parents: 11038
diff changeset
   210
        help="restart at last error")
8091
e85cc856d2e1 run-tests: factor out parse_args(). Clarify use of globals a bit.
Greg Ward <greg-hg@gerg.ca>
parents: 8060
diff changeset
   211
    parser.add_option("-r", "--retest", action="store_true",
e85cc856d2e1 run-tests: factor out parse_args(). Clarify use of globals a bit.
Greg Ward <greg-hg@gerg.ca>
parents: 8060
diff changeset
   212
        help="retest failed tests")
9580
25858f9e65e8 run-tests: add --noskips option
Matt Mackall <mpm@selenic.com>
parents: 9408
diff changeset
   213
    parser.add_option("-S", "--noskips", action="store_true",
25858f9e65e8 run-tests: add --noskips option
Matt Mackall <mpm@selenic.com>
parents: 9408
diff changeset
   214
        help="don't report skip tests verbosely")
14202
b68a41420397 run-tests: add --shell command line flag
Martin Geisler <mg@lazybytes.net>
parents: 14201
diff changeset
   215
    parser.add_option("--shell", type="string",
b68a41420397 run-tests: add --shell command line flag
Martin Geisler <mg@lazybytes.net>
parents: 14201
diff changeset
   216
        help="shell to use (default: $%s or %s)" % defaults['shell'])
8091
e85cc856d2e1 run-tests: factor out parse_args(). Clarify use of globals a bit.
Greg Ward <greg-hg@gerg.ca>
parents: 8060
diff changeset
   217
    parser.add_option("-t", "--timeout", type="int",
e85cc856d2e1 run-tests: factor out parse_args(). Clarify use of globals a bit.
Greg Ward <greg-hg@gerg.ca>
parents: 8060
diff changeset
   218
        help="kill errant tests after TIMEOUT seconds"
e85cc856d2e1 run-tests: factor out parse_args(). Clarify use of globals a bit.
Greg Ward <greg-hg@gerg.ca>
parents: 8060
diff changeset
   219
             " (default: $%s or %d)" % defaults['timeout'])
17921
4ac9cf3d810c run-tests: add --time option to log times for each test
Siddharth Agarwal <sid0@fb.com>
parents: 17920
diff changeset
   220
    parser.add_option("--time", action="store_true",
4ac9cf3d810c run-tests: add --time option to log times for each test
Siddharth Agarwal <sid0@fb.com>
parents: 17920
diff changeset
   221
        help="time how long each test takes")
22391
c42e69268f5b run-tests: added '--json' functionality to store test result in json file
anuraggoel <anurag.dsps@gmail.com>
parents: 22361
diff changeset
   222
    parser.add_option("--json", action="store_true",
c42e69268f5b run-tests: added '--json' functionality to store test result in json file
anuraggoel <anurag.dsps@gmail.com>
parents: 22361
diff changeset
   223
                      help="store test result data in 'report.json' file")
11039
cf0a309f6c4d run-tests: sort options
Matt Mackall <mpm@selenic.com>
parents: 11038
diff changeset
   224
    parser.add_option("--tmpdir", type="string",
cf0a309f6c4d run-tests: sort options
Matt Mackall <mpm@selenic.com>
parents: 11038
diff changeset
   225
        help="run tests in the given temporary directory"
cf0a309f6c4d run-tests: sort options
Matt Mackall <mpm@selenic.com>
parents: 11038
diff changeset
   226
             " (implies --keep-tmpdir)")
8091
e85cc856d2e1 run-tests: factor out parse_args(). Clarify use of globals a bit.
Greg Ward <greg-hg@gerg.ca>
parents: 8060
diff changeset
   227
    parser.add_option("-v", "--verbose", action="store_true",
e85cc856d2e1 run-tests: factor out parse_args(). Clarify use of globals a bit.
Greg Ward <greg-hg@gerg.ca>
parents: 8060
diff changeset
   228
        help="output verbose messages")
22044
a06172e85fd4 run-tests: add support for xunit test reports
Augie Fackler <raf@durin42.com>
parents: 21997
diff changeset
   229
    parser.add_option("--xunit", type="string",
a06172e85fd4 run-tests: add support for xunit test reports
Augie Fackler <raf@durin42.com>
parents: 21997
diff changeset
   230
                      help="record xunit results at specified path")
11040
8f951ed6c63c run-tests: add --view switch to use external diff viewer
Matt Mackall <mpm@selenic.com>
parents: 11039
diff changeset
   231
    parser.add_option("--view", type="string",
8f951ed6c63c run-tests: add --view switch to use external diff viewer
Matt Mackall <mpm@selenic.com>
parents: 11039
diff changeset
   232
        help="external diff viewer")
8091
e85cc856d2e1 run-tests: factor out parse_args(). Clarify use of globals a bit.
Greg Ward <greg-hg@gerg.ca>
parents: 8060
diff changeset
   233
    parser.add_option("--with-hg", type="string",
8674
0941ee76489e run-tests: redefine --with-hg so it takes the 'hg' script to run.
Greg Ward <greg-hg@gerg.ca>
parents: 8673
diff changeset
   234
        metavar="HG",
0941ee76489e run-tests: redefine --with-hg so it takes the 'hg' script to run.
Greg Ward <greg-hg@gerg.ca>
parents: 8673
diff changeset
   235
        help="test using specified hg script rather than a "
0941ee76489e run-tests: redefine --with-hg so it takes the 'hg' script to run.
Greg Ward <greg-hg@gerg.ca>
parents: 8673
diff changeset
   236
             "temporary installation")
9028
bea567ae3ff6 tests: add -3 switch to run-tests.py
Alejandro Santos <alejolp@alejolp.com>
parents: 8943
diff changeset
   237
    parser.add_option("-3", "--py3k-warnings", action="store_true",
bea567ae3ff6 tests: add -3 switch to run-tests.py
Alejandro Santos <alejolp@alejolp.com>
parents: 8943
diff changeset
   238
        help="enable Py3k warnings on Python 2.6+")
14134
8468ec1109d1 run-tests: add flag to provide extra hgrc options for test runs
Augie Fackler <durin42@gmail.com>
parents: 14062
diff changeset
   239
    parser.add_option('--extra-config-opt', action="append",
8468ec1109d1 run-tests: add flag to provide extra hgrc options for test runs
Augie Fackler <durin42@gmail.com>
parents: 14062
diff changeset
   240
                      help='set the given config opt in the test hgrc')
19057
3d265e0822d3 run-tests: introduce --random for running tests in random error
Mads Kiilerich <madski@unity3d.com>
parents: 18788
diff changeset
   241
    parser.add_option('--random', action="store_true",
3d265e0822d3 run-tests: introduce --random for running tests in random error
Mads Kiilerich <madski@unity3d.com>
parents: 18788
diff changeset
   242
                      help='run tests in random order')
2110
25a8d116ab6a Add a pure python version of run-tests.
Stephen Darnell <stephen@darnell.plus.com>
parents:
diff changeset
   243
14201
57e04ded3da4 run-tests: use type of default to convert environment variable
Martin Geisler <mg@lazybytes.net>
parents: 14192
diff changeset
   244
    for option, (envvar, default) in defaults.items():
57e04ded3da4 run-tests: use type of default to convert environment variable
Martin Geisler <mg@lazybytes.net>
parents: 14192
diff changeset
   245
        defaults[option] = type(default)(os.environ.get(envvar, default))
8091
e85cc856d2e1 run-tests: factor out parse_args(). Clarify use of globals a bit.
Greg Ward <greg-hg@gerg.ca>
parents: 8060
diff changeset
   246
    parser.set_defaults(**defaults)
21008
c1dd04be3d9a run-tests: allow option parser to be extended
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21007
diff changeset
   247
c1dd04be3d9a run-tests: allow option parser to be extended
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21007
diff changeset
   248
    return parser
c1dd04be3d9a run-tests: allow option parser to be extended
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21007
diff changeset
   249
c1dd04be3d9a run-tests: allow option parser to be extended
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21007
diff changeset
   250
def parseargs(args, parser):
21383
772ed56e2519 run-tests: add some docstrings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21382
diff changeset
   251
    """Parse arguments with our OptionParser and validate results."""
21006
723e41ad59b4 run-tests: Pass arguments into argument parser
Gregory Szorc <gregory.szorc@gmail.com>
parents: 20821
diff changeset
   252
    (options, args) = parser.parse_args(args)
8091
e85cc856d2e1 run-tests: factor out parse_args(). Clarify use of globals a bit.
Greg Ward <greg-hg@gerg.ca>
parents: 8060
diff changeset
   253
10758
2ed667a9dfcb tests: adapt the test runner to work with jython
Ronny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
parents: 10750
diff changeset
   254
    # jython is always pure
10766
afbcea270bb8 run-tests: force to test pure on pypy as well
Ronny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
parents: 10765
diff changeset
   255
    if 'java' in sys.platform or '__pypy__' in sys.modules:
10765
fd31a3237498 Fix run-tests.py -jX after 2ed667a9dfcb
Ronny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
parents: 10758
diff changeset
   256
        options.pure = True
10758
2ed667a9dfcb tests: adapt the test runner to work with jython
Ronny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
parents: 10750
diff changeset
   257
8674
0941ee76489e run-tests: redefine --with-hg so it takes the 'hg' script to run.
Greg Ward <greg-hg@gerg.ca>
parents: 8673
diff changeset
   258
    if options.with_hg:
15942
d7a34c07e69b run-tests: expand user in --with-hg
Mads Kiilerich <mads@kiilerich.com>
parents: 15941
diff changeset
   259
        options.with_hg = os.path.expanduser(options.with_hg)
8674
0941ee76489e run-tests: redefine --with-hg so it takes the 'hg' script to run.
Greg Ward <greg-hg@gerg.ca>
parents: 8673
diff changeset
   260
        if not (os.path.isfile(options.with_hg) and
0941ee76489e run-tests: redefine --with-hg so it takes the 'hg' script to run.
Greg Ward <greg-hg@gerg.ca>
parents: 8673
diff changeset
   261
                os.access(options.with_hg, os.X_OK)):
0941ee76489e run-tests: redefine --with-hg so it takes the 'hg' script to run.
Greg Ward <greg-hg@gerg.ca>
parents: 8673
diff changeset
   262
            parser.error('--with-hg must specify an executable hg script')
0941ee76489e run-tests: redefine --with-hg so it takes the 'hg' script to run.
Greg Ward <greg-hg@gerg.ca>
parents: 8673
diff changeset
   263
        if not os.path.basename(options.with_hg) == 'hg':
14359
ad5c68a0db6a run-tests: print a newline after all warnings
Thomas Arendsen Hein <thomas@intevation.de>
parents: 14340
diff changeset
   264
            sys.stderr.write('warning: --with-hg should specify an hg script\n')
8674
0941ee76489e run-tests: redefine --with-hg so it takes the 'hg' script to run.
Greg Ward <greg-hg@gerg.ca>
parents: 8673
diff changeset
   265
    if options.local:
0941ee76489e run-tests: redefine --with-hg so it takes the 'hg' script to run.
Greg Ward <greg-hg@gerg.ca>
parents: 8673
diff changeset
   266
        testdir = os.path.dirname(os.path.realpath(sys.argv[0]))
0941ee76489e run-tests: redefine --with-hg so it takes the 'hg' script to run.
Greg Ward <greg-hg@gerg.ca>
parents: 8673
diff changeset
   267
        hgbin = os.path.join(os.path.dirname(testdir), 'hg')
16538
dd194e5df4c1 tests: don't require 'hg' without extension on windows
Mads Kiilerich <mads@kiilerich.com>
parents: 16346
diff changeset
   268
        if os.name != 'nt' and not os.access(hgbin, os.X_OK):
8674
0941ee76489e run-tests: redefine --with-hg so it takes the 'hg' script to run.
Greg Ward <greg-hg@gerg.ca>
parents: 8673
diff changeset
   269
            parser.error('--local specified, but %r not found or not executable'
0941ee76489e run-tests: redefine --with-hg so it takes the 'hg' script to run.
Greg Ward <greg-hg@gerg.ca>
parents: 8673
diff changeset
   270
                         % hgbin)
0941ee76489e run-tests: redefine --with-hg so it takes the 'hg' script to run.
Greg Ward <greg-hg@gerg.ca>
parents: 8673
diff changeset
   271
        options.with_hg = hgbin
0941ee76489e run-tests: redefine --with-hg so it takes the 'hg' script to run.
Greg Ward <greg-hg@gerg.ca>
parents: 8673
diff changeset
   272
15859
44a371823f83 tests: add htmlcov option
Markus Zapke-Gründemann <info@keimlink.de>
parents: 15858
diff changeset
   273
    options.anycoverage = options.cover or options.annotate or options.htmlcov
10648
58128004cca1 tests: use external coverage, mandate newer version
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents: 10413
diff changeset
   274
    if options.anycoverage:
58128004cca1 tests: use external coverage, mandate newer version
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents: 10413
diff changeset
   275
        try:
58128004cca1 tests: use external coverage, mandate newer version
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents: 10413
diff changeset
   276
            import coverage
58128004cca1 tests: use external coverage, mandate newer version
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents: 10413
diff changeset
   277
            covver = version.StrictVersion(coverage.__version__).version
58128004cca1 tests: use external coverage, mandate newer version
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents: 10413
diff changeset
   278
            if covver < (3, 3):
58128004cca1 tests: use external coverage, mandate newer version
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents: 10413
diff changeset
   279
                parser.error('coverage options require coverage 3.3 or later')
58128004cca1 tests: use external coverage, mandate newer version
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents: 10413
diff changeset
   280
        except ImportError:
58128004cca1 tests: use external coverage, mandate newer version
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents: 10413
diff changeset
   281
            parser.error('coverage options now require the coverage package')
8095
f5428d4ffd97 run-tests: reduce global variables set by parse_args().
Greg Ward <greg-hg@gerg.ca>
parents: 8094
diff changeset
   282
10648
58128004cca1 tests: use external coverage, mandate newer version
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents: 10413
diff changeset
   283
    if options.anycoverage and options.local:
58128004cca1 tests: use external coverage, mandate newer version
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents: 10413
diff changeset
   284
        # this needs some path mangling somewhere, I guess
58128004cca1 tests: use external coverage, mandate newer version
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents: 10413
diff changeset
   285
        parser.error("sorry, coverage options do not work when --local "
58128004cca1 tests: use external coverage, mandate newer version
Dirkjan Ochtman <djc.ochtman@kentyde.com>
parents: 10413
diff changeset
   286
                     "is specified")
8674
0941ee76489e run-tests: redefine --with-hg so it takes the 'hg' script to run.
Greg Ward <greg-hg@gerg.ca>
parents: 8673
diff changeset
   287
24506
60bbb4079c28 run-tests: report code coverage from source directory
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24505
diff changeset
   288
    if options.anycoverage and options.with_hg:
60bbb4079c28 run-tests: report code coverage from source directory
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24505
diff changeset
   289
        parser.error("sorry, coverage options do not work when --with-hg "
60bbb4079c28 run-tests: report code coverage from source directory
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24505
diff changeset
   290
                     "is specified")
60bbb4079c28 run-tests: report code coverage from source directory
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24505
diff changeset
   291
19250
5fa946330970 run-tests: make vlog a proper function
Matt Mackall <mpm@selenic.com>
parents: 19249
diff changeset
   292
    global verbose
8095
f5428d4ffd97 run-tests: reduce global variables set by parse_args().
Greg Ward <greg-hg@gerg.ca>
parents: 8094
diff changeset
   293
    if options.verbose:
19279
3868c9ab4bf8 run-tests: drop options.child and users
Matt Mackall <mpm@selenic.com>
parents: 19278
diff changeset
   294
        verbose = ''
8091
e85cc856d2e1 run-tests: factor out parse_args(). Clarify use of globals a bit.
Greg Ward <greg-hg@gerg.ca>
parents: 8060
diff changeset
   295
9394
31203db1b2ac run-tests: expand --tmpdir and create it if needed
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9031
diff changeset
   296
    if options.tmpdir:
31203db1b2ac run-tests: expand --tmpdir and create it if needed
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9031
diff changeset
   297
        options.tmpdir = os.path.expanduser(options.tmpdir)
31203db1b2ac run-tests: expand --tmpdir and create it if needed
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9031
diff changeset
   298
8091
e85cc856d2e1 run-tests: factor out parse_args(). Clarify use of globals a bit.
Greg Ward <greg-hg@gerg.ca>
parents: 8060
diff changeset
   299
    if options.jobs < 1:
9408
70bf7f853adc run-tests: standardize on --foo instead of -f/--foo
Martin Geisler <mg@lazybytes.net>
parents: 9407
diff changeset
   300
        parser.error('--jobs must be positive')
9707
38deec407f8d run-tests: add "debug" mode: don't capture child output, just show it.
Greg Ward <greg-hg@gerg.ca>
parents: 9706
diff changeset
   301
    if options.interactive and options.debug:
38deec407f8d run-tests: add "debug" mode: don't capture child output, just show it.
Greg Ward <greg-hg@gerg.ca>
parents: 9706
diff changeset
   302
        parser.error("-i/--interactive and -d/--debug are incompatible")
38deec407f8d run-tests: add "debug" mode: don't capture child output, just show it.
Greg Ward <greg-hg@gerg.ca>
parents: 9706
diff changeset
   303
    if options.debug:
38deec407f8d run-tests: add "debug" mode: don't capture child output, just show it.
Greg Ward <greg-hg@gerg.ca>
parents: 9706
diff changeset
   304
        if options.timeout != defaults['timeout']:
38deec407f8d run-tests: add "debug" mode: don't capture child output, just show it.
Greg Ward <greg-hg@gerg.ca>
parents: 9706
diff changeset
   305
            sys.stderr.write(
38deec407f8d run-tests: add "debug" mode: don't capture child output, just show it.
Greg Ward <greg-hg@gerg.ca>
parents: 9706
diff changeset
   306
                'warning: --timeout option ignored with --debug\n')
38deec407f8d run-tests: add "debug" mode: don't capture child output, just show it.
Greg Ward <greg-hg@gerg.ca>
parents: 9706
diff changeset
   307
        options.timeout = 0
9028
bea567ae3ff6 tests: add -3 switch to run-tests.py
Alejandro Santos <alejolp@alejolp.com>
parents: 8943
diff changeset
   308
    if options.py3k_warnings:
bea567ae3ff6 tests: add -3 switch to run-tests.py
Alejandro Santos <alejolp@alejolp.com>
parents: 8943
diff changeset
   309
        if sys.version_info[:2] < (2, 6) or sys.version_info[:2] >= (3, 0):
9408
70bf7f853adc run-tests: standardize on --foo instead of -f/--foo
Martin Geisler <mg@lazybytes.net>
parents: 9407
diff changeset
   310
            parser.error('--py3k-warnings can only be used on Python 2.6+')
9959
b37b060d84c7 run-tests: add a "--blacklist target" option to skip predefined test lists
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9958
diff changeset
   311
    if options.blacklist:
14493
5cc7905bccc9 run-tests: allow whitelisting tests that should always run
Augie Fackler <durin42@gmail.com>
parents: 14454
diff changeset
   312
        options.blacklist = parselistfiles(options.blacklist, 'blacklist')
5cc7905bccc9 run-tests: allow whitelisting tests that should always run
Augie Fackler <durin42@gmail.com>
parents: 14454
diff changeset
   313
    if options.whitelist:
19279
3868c9ab4bf8 run-tests: drop options.child and users
Matt Mackall <mpm@selenic.com>
parents: 19278
diff changeset
   314
        options.whitelisted = parselistfiles(options.whitelist, 'whitelist')
14493
5cc7905bccc9 run-tests: allow whitelisting tests that should always run
Augie Fackler <durin42@gmail.com>
parents: 14454
diff changeset
   315
    else:
5cc7905bccc9 run-tests: allow whitelisting tests that should always run
Augie Fackler <durin42@gmail.com>
parents: 14454
diff changeset
   316
        options.whitelisted = {}
8091
e85cc856d2e1 run-tests: factor out parse_args(). Clarify use of globals a bit.
Greg Ward <greg-hg@gerg.ca>
parents: 8060
diff changeset
   317
e85cc856d2e1 run-tests: factor out parse_args(). Clarify use of globals a bit.
Greg Ward <greg-hg@gerg.ca>
parents: 8060
diff changeset
   318
    return (options, args)
5384
e3a0c092b4e2 Allow tests to run in parallel.
Bryan O'Sullivan <bos@serpentine.com>
parents: 5383
diff changeset
   319
5800
2f597243e1d7 Make run-tests.py --interactive work on Windows
Patrick Mezard <pmezard@gmail.com>
parents: 5524
diff changeset
   320
def rename(src, dst):
2f597243e1d7 Make run-tests.py --interactive work on Windows
Patrick Mezard <pmezard@gmail.com>
parents: 5524
diff changeset
   321
    """Like os.rename(), trade atomicity and opened files friendliness
2f597243e1d7 Make run-tests.py --interactive work on Windows
Patrick Mezard <pmezard@gmail.com>
parents: 5524
diff changeset
   322
    for existing destination support.
2f597243e1d7 Make run-tests.py --interactive work on Windows
Patrick Mezard <pmezard@gmail.com>
parents: 5524
diff changeset
   323
    """
2f597243e1d7 Make run-tests.py --interactive work on Windows
Patrick Mezard <pmezard@gmail.com>
parents: 5524
diff changeset
   324
    shutil.copy(src, dst)
2f597243e1d7 Make run-tests.py --interactive work on Windows
Patrick Mezard <pmezard@gmail.com>
parents: 5524
diff changeset
   325
    os.remove(src)
2f597243e1d7 Make run-tests.py --interactive work on Windows
Patrick Mezard <pmezard@gmail.com>
parents: 5524
diff changeset
   326
21521
855f092c96e5 run-tests: move diff generation into TestResult
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21520
diff changeset
   327
def getdiff(expected, output, ref, err):
21022
52e9e63f1495 run-tests: test result shows when a failed test could not start a server
Simon Heimberg <simohe@besonet.ch>
parents: 21009
diff changeset
   328
    servefail = False
21521
855f092c96e5 run-tests: move diff generation into TestResult
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21520
diff changeset
   329
    lines = []
10088
ec8304e66ea5 run-tests.py: Show paths to failing tests, .err and .out
Mads Kiilerich <mads@kiilerich.com>
parents: 10030
diff changeset
   330
    for line in difflib.unified_diff(expected, output, ref, err):
21737
8a63e691179a run-tests: filter whitespace at end of error diffs
Matt Mackall <mpm@selenic.com>
parents: 21736
diff changeset
   331
        if line.startswith('+++') or line.startswith('---'):
23077
605a8cb61a0c run-tests: output diffs with only forward slashes
Matt Mackall <mpm@selenic.com>
parents: 23053
diff changeset
   332
            line = line.replace('\\', '/')
21737
8a63e691179a run-tests: filter whitespace at end of error diffs
Matt Mackall <mpm@selenic.com>
parents: 21736
diff changeset
   333
            if line.endswith(' \n'):
8a63e691179a run-tests: filter whitespace at end of error diffs
Matt Mackall <mpm@selenic.com>
parents: 21736
diff changeset
   334
                line = line[:-2] + '\n'
21521
855f092c96e5 run-tests: move diff generation into TestResult
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21520
diff changeset
   335
        lines.append(line)
21022
52e9e63f1495 run-tests: test result shows when a failed test could not start a server
Simon Heimberg <simohe@besonet.ch>
parents: 21009
diff changeset
   336
        if not servefail and line.startswith(
52e9e63f1495 run-tests: test result shows when a failed test could not start a server
Simon Heimberg <simohe@besonet.ch>
parents: 21009
diff changeset
   337
                             '+  abort: child process failed to start'):
52e9e63f1495 run-tests: test result shows when a failed test could not start a server
Simon Heimberg <simohe@besonet.ch>
parents: 21009
diff changeset
   338
            servefail = True
52e9e63f1495 run-tests: test result shows when a failed test could not start a server
Simon Heimberg <simohe@besonet.ch>
parents: 21009
diff changeset
   339
21521
855f092c96e5 run-tests: move diff generation into TestResult
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21520
diff changeset
   340
    return servefail, lines
2110
25a8d116ab6a Add a pure python version of run-tests.
Stephen Darnell <stephen@darnell.plus.com>
parents:
diff changeset
   341
19250
5fa946330970 run-tests: make vlog a proper function
Matt Mackall <mpm@selenic.com>
parents: 19249
diff changeset
   342
verbose = False
5fa946330970 run-tests: make vlog a proper function
Matt Mackall <mpm@selenic.com>
parents: 19249
diff changeset
   343
def vlog(*msg):
21535
ab7e224bc089 run-tests: avoid duplicate code in vlog()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21534
diff changeset
   344
    """Log only when in verbose mode."""
ab7e224bc089 run-tests: avoid duplicate code in vlog()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21534
diff changeset
   345
    if verbose is False:
ab7e224bc089 run-tests: avoid duplicate code in vlog()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21534
diff changeset
   346
        return
ab7e224bc089 run-tests: avoid duplicate code in vlog()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21534
diff changeset
   347
ab7e224bc089 run-tests: avoid duplicate code in vlog()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21534
diff changeset
   348
    return log(*msg)
19250
5fa946330970 run-tests: make vlog a proper function
Matt Mackall <mpm@selenic.com>
parents: 19249
diff changeset
   349
22044
a06172e85fd4 run-tests: add support for xunit test reports
Augie Fackler <raf@durin42.com>
parents: 21997
diff changeset
   350
# Bytes that break XML even in a CDATA block: control characters 0-31
a06172e85fd4 run-tests: add support for xunit test reports
Augie Fackler <raf@durin42.com>
parents: 21997
diff changeset
   351
# sans \t, \n and \r
a06172e85fd4 run-tests: add support for xunit test reports
Augie Fackler <raf@durin42.com>
parents: 21997
diff changeset
   352
CDATA_EVIL = re.compile(r"[\000-\010\013\014\016-\037]")
a06172e85fd4 run-tests: add support for xunit test reports
Augie Fackler <raf@durin42.com>
parents: 21997
diff changeset
   353
a06172e85fd4 run-tests: add support for xunit test reports
Augie Fackler <raf@durin42.com>
parents: 21997
diff changeset
   354
def cdatasafe(data):
a06172e85fd4 run-tests: add support for xunit test reports
Augie Fackler <raf@durin42.com>
parents: 21997
diff changeset
   355
    """Make a string safe to include in a CDATA block.
a06172e85fd4 run-tests: add support for xunit test reports
Augie Fackler <raf@durin42.com>
parents: 21997
diff changeset
   356
a06172e85fd4 run-tests: add support for xunit test reports
Augie Fackler <raf@durin42.com>
parents: 21997
diff changeset
   357
    Certain control characters are illegal in a CDATA block, and
a06172e85fd4 run-tests: add support for xunit test reports
Augie Fackler <raf@durin42.com>
parents: 21997
diff changeset
   358
    there's no way to include a ]]> in a CDATA either. This function
a06172e85fd4 run-tests: add support for xunit test reports
Augie Fackler <raf@durin42.com>
parents: 21997
diff changeset
   359
    replaces illegal bytes with ? and adds a space between the ]] so
a06172e85fd4 run-tests: add support for xunit test reports
Augie Fackler <raf@durin42.com>
parents: 21997
diff changeset
   360
    that it won't break the CDATA block.
a06172e85fd4 run-tests: add support for xunit test reports
Augie Fackler <raf@durin42.com>
parents: 21997
diff changeset
   361
    """
a06172e85fd4 run-tests: add support for xunit test reports
Augie Fackler <raf@durin42.com>
parents: 21997
diff changeset
   362
    return CDATA_EVIL.sub('?', data).replace(']]>', '] ]>')
a06172e85fd4 run-tests: add support for xunit test reports
Augie Fackler <raf@durin42.com>
parents: 21997
diff changeset
   363
19251
6857f53456f2 run-tests: add a log function
Matt Mackall <mpm@selenic.com>
parents: 19250
diff changeset
   364
def log(*msg):
21535
ab7e224bc089 run-tests: avoid duplicate code in vlog()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21534
diff changeset
   365
    """Log something to stdout.
ab7e224bc089 run-tests: avoid duplicate code in vlog()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21534
diff changeset
   366
ab7e224bc089 run-tests: avoid duplicate code in vlog()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21534
diff changeset
   367
    Arguments are strings to print.
ab7e224bc089 run-tests: avoid duplicate code in vlog()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21534
diff changeset
   368
    """
19251
6857f53456f2 run-tests: add a log function
Matt Mackall <mpm@selenic.com>
parents: 19250
diff changeset
   369
    iolock.acquire()
6857f53456f2 run-tests: add a log function
Matt Mackall <mpm@selenic.com>
parents: 19250
diff changeset
   370
    if verbose:
25031
0adc22a0b6b3 python3: update killdaemons and run-tests print and exception syntax
Augie Fackler <augie@google.com>
parents: 24984
diff changeset
   371
        print(verbose, end=' ')
19251
6857f53456f2 run-tests: add a log function
Matt Mackall <mpm@selenic.com>
parents: 19250
diff changeset
   372
    for m in msg:
25031
0adc22a0b6b3 python3: update killdaemons and run-tests print and exception syntax
Augie Fackler <augie@google.com>
parents: 24984
diff changeset
   373
        print(m, end=' ')
0adc22a0b6b3 python3: update killdaemons and run-tests print and exception syntax
Augie Fackler <augie@google.com>
parents: 24984
diff changeset
   374
    print()
19251
6857f53456f2 run-tests: add a log function
Matt Mackall <mpm@selenic.com>
parents: 19250
diff changeset
   375
    sys.stdout.flush()
6857f53456f2 run-tests: add a log function
Matt Mackall <mpm@selenic.com>
parents: 19250
diff changeset
   376
    iolock.release()
6857f53456f2 run-tests: add a log function
Matt Mackall <mpm@selenic.com>
parents: 19250
diff changeset
   377
14821
2017495bd552 run-tests: fallback to SIGTERM if subprocess.Popen does not have terminate()
Thomas Arendsen Hein <thomas@intevation.de>
parents: 14598
diff changeset
   378
def terminate(proc):
2017495bd552 run-tests: fallback to SIGTERM if subprocess.Popen does not have terminate()
Thomas Arendsen Hein <thomas@intevation.de>
parents: 14598
diff changeset
   379
    """Terminate subprocess (with fallback for Python versions < 2.6)"""
2017495bd552 run-tests: fallback to SIGTERM if subprocess.Popen does not have terminate()
Thomas Arendsen Hein <thomas@intevation.de>
parents: 14598
diff changeset
   380
    vlog('# Terminating process %d' % proc.pid)
2017495bd552 run-tests: fallback to SIGTERM if subprocess.Popen does not have terminate()
Thomas Arendsen Hein <thomas@intevation.de>
parents: 14598
diff changeset
   381
    try:
14971
0b21ae0a2366 tests: use getattr instead of hasattr
Augie Fackler <durin42@gmail.com>
parents: 14867
diff changeset
   382
        getattr(proc, 'terminate', lambda : os.kill(proc.pid, signal.SIGTERM))()
14821
2017495bd552 run-tests: fallback to SIGTERM if subprocess.Popen does not have terminate()
Thomas Arendsen Hein <thomas@intevation.de>
parents: 14598
diff changeset
   383
    except OSError:
2017495bd552 run-tests: fallback to SIGTERM if subprocess.Popen does not have terminate()
Thomas Arendsen Hein <thomas@intevation.de>
parents: 14598
diff changeset
   384
        pass
2017495bd552 run-tests: fallback to SIGTERM if subprocess.Popen does not have terminate()
Thomas Arendsen Hein <thomas@intevation.de>
parents: 14598
diff changeset
   385
19263
062c0a0a5549 run-tests: use env dict to kill daemons
Matt Mackall <mpm@selenic.com>
parents: 19262
diff changeset
   386
def killdaemons(pidfile):
062c0a0a5549 run-tests: use env dict to kill daemons
Matt Mackall <mpm@selenic.com>
parents: 19262
diff changeset
   387
    return killmod.killdaemons(pidfile, tryhard=False, remove=True,
17464
eddfb9a550d0 run-tests: do not duplicate killdaemons() code
Patrick Mezard <patrick@mezard.eu>
parents: 16906
diff changeset
   388
                               logfn=vlog)
10336
bc9a3bb267fa run-tests: kill daemons on ^C with -j.
Brendan Cully <brendan@kublai.com>
parents: 10300
diff changeset
   389
21488
feb8ad2d57ee run-tests: merge MercurialTest into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21464
diff changeset
   390
class Test(unittest.TestCase):
21309
0b123e6a318c run-tests: allow Test.run() to run multiple times
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21308
diff changeset
   391
    """Encapsulates a single, runnable test.
0b123e6a318c run-tests: allow Test.run() to run multiple times
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21308
diff changeset
   392
21497
798c81e32b5e run-tests: refactor temporary directories in Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21496
diff changeset
   393
    While this class conforms to the unittest.TestCase API, it differs in that
798c81e32b5e run-tests: refactor temporary directories in Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21496
diff changeset
   394
    instances need to be instantiated manually. (Typically, unittest.TestCase
798c81e32b5e run-tests: refactor temporary directories in Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21496
diff changeset
   395
    classes are instantiated automatically by scanning modules.)
21309
0b123e6a318c run-tests: allow Test.run() to run multiple times
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21308
diff changeset
   396
    """
21296
cd8776030833 run-tests: create classes for representing tests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21231
diff changeset
   397
21380
de6ea36ca9f7 run-tests: move SKIPPED_STATUS into Test class
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21379
diff changeset
   398
    # Status code reserved for skipped tests (used by hghave).
de6ea36ca9f7 run-tests: move SKIPPED_STATUS into Test class
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21379
diff changeset
   399
    SKIPPED_STATUS = 80
de6ea36ca9f7 run-tests: move SKIPPED_STATUS into Test class
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21379
diff changeset
   400
21520
fa6ba4372678 run-tests: remove global abort flag
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21518
diff changeset
   401
    def __init__(self, path, tmpdir, keeptmpdir=False,
21521
855f092c96e5 run-tests: move diff generation into TestResult
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21520
diff changeset
   402
                 debug=False,
21523
9fb6f328576a run-tests: move interactive test acceptance into TestResult
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21522
diff changeset
   403
                 timeout=defaults['timeout'],
21516
1e275c09242e run-tests: move py3kwarnings to Test.__init__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21515
diff changeset
   404
                 startport=defaults['port'], extraconfigopts=None,
21517
af7d3a5c330b run-tests: move shell to Test.__init__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21516
diff changeset
   405
                 py3kwarnings=False, shell=None):
21502
f8515564d617 run-tests: pass a full test path into Test.__init__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21501
diff changeset
   406
        """Create a test from parameters.
f8515564d617 run-tests: pass a full test path into Test.__init__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21501
diff changeset
   407
f8515564d617 run-tests: pass a full test path into Test.__init__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21501
diff changeset
   408
        path is the full path to the file defining the test.
21338
3cd2d2de4060 run-tests: move computation of test paths into Test.__init__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21337
diff changeset
   409
21504
888a5dfe1569 run-tests: pass temp dir into Test.__init__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21502
diff changeset
   410
        tmpdir is the main temporary directory to use for this test.
21505
3a1881dbf860 run-tests: pass abort into Test.__init__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21504
diff changeset
   411
21509
d21d53ee0d7a run-tests: factor options.keep_tmpdir into an argument to Test.__init__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21508
diff changeset
   412
        keeptmpdir determines whether to keep the test's temporary directory
d21d53ee0d7a run-tests: factor options.keep_tmpdir into an argument to Test.__init__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21508
diff changeset
   413
        after execution. It defaults to removal (False).
21510
97127c4ce460 run-tests: move debug into an argument to Test.__init__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21509
diff changeset
   414
97127c4ce460 run-tests: move debug into an argument to Test.__init__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21509
diff changeset
   415
        debug mode will make the test execute verbosely, with unfiltered
97127c4ce460 run-tests: move debug into an argument to Test.__init__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21509
diff changeset
   416
        output.
21511
3ec3e81a4110 run-tests: move diff options into arguments of Test.__init__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21510
diff changeset
   417
21513
acfd19f3e79c run-tests: move timeout into Test.__init__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21512
diff changeset
   418
        timeout controls the maximum run time of the test. It is ignored when
acfd19f3e79c run-tests: move timeout into Test.__init__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21512
diff changeset
   419
        debug is True.
21514
59fe123dbb00 run-tests: refactor port number declaration
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21513
diff changeset
   420
59fe123dbb00 run-tests: refactor port number declaration
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21513
diff changeset
   421
        startport controls the starting port number to use for this test. Each
59fe123dbb00 run-tests: refactor port number declaration
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21513
diff changeset
   422
        test will reserve 3 port numbers for execution. It is the caller's
59fe123dbb00 run-tests: refactor port number declaration
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21513
diff changeset
   423
        responsibility to allocate a non-overlapping port range to Test
59fe123dbb00 run-tests: refactor port number declaration
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21513
diff changeset
   424
        instances.
21515
9978ff48b1e8 run-tests: move extra config options to Test.__init__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21514
diff changeset
   425
9978ff48b1e8 run-tests: move extra config options to Test.__init__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21514
diff changeset
   426
        extraconfigopts is an iterable of extra hgrc config options. Values
9978ff48b1e8 run-tests: move extra config options to Test.__init__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21514
diff changeset
   427
        must have the form "key=value" (something understood by hgrc). Values
9978ff48b1e8 run-tests: move extra config options to Test.__init__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21514
diff changeset
   428
        of the form "foo.key=value" will result in "[foo] key=value".
21516
1e275c09242e run-tests: move py3kwarnings to Test.__init__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21515
diff changeset
   429
1e275c09242e run-tests: move py3kwarnings to Test.__init__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21515
diff changeset
   430
        py3kwarnings enables Py3k warnings.
21517
af7d3a5c330b run-tests: move shell to Test.__init__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21516
diff changeset
   431
af7d3a5c330b run-tests: move shell to Test.__init__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21516
diff changeset
   432
        shell is the shell to execute tests in.
21502
f8515564d617 run-tests: pass a full test path into Test.__init__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21501
diff changeset
   433
        """
f8515564d617 run-tests: pass a full test path into Test.__init__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21501
diff changeset
   434
25034
af6e6a0781d7 run-tests: use bytes explicitly for tmpdir and hgrc construction
Augie Fackler <augie@google.com>
parents: 25033
diff changeset
   435
        self.path = path.encode('utf-8')
21502
f8515564d617 run-tests: pass a full test path into Test.__init__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21501
diff changeset
   436
        self.name = os.path.basename(path)
f8515564d617 run-tests: pass a full test path into Test.__init__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21501
diff changeset
   437
        self._testdir = os.path.dirname(path)
21507
d839e4820da7 run-tests: move test filtering into TestSuite.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21506
diff changeset
   438
        self.errpath = os.path.join(self._testdir, '%s.err' % self.name)
21435
f376f56a354e run-tests: rename Test._test to Test.name
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21434
diff changeset
   439
25034
af6e6a0781d7 run-tests: use bytes explicitly for tmpdir and hgrc construction
Augie Fackler <augie@google.com>
parents: 25033
diff changeset
   440
        self._threadtmp = tmpdir.encode('utf-8')
21509
d21d53ee0d7a run-tests: factor options.keep_tmpdir into an argument to Test.__init__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21508
diff changeset
   441
        self._keeptmpdir = keeptmpdir
21510
97127c4ce460 run-tests: move debug into an argument to Test.__init__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21509
diff changeset
   442
        self._debug = debug
21513
acfd19f3e79c run-tests: move timeout into Test.__init__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21512
diff changeset
   443
        self._timeout = timeout
21514
59fe123dbb00 run-tests: refactor port number declaration
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21513
diff changeset
   444
        self._startport = startport
21515
9978ff48b1e8 run-tests: move extra config options to Test.__init__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21514
diff changeset
   445
        self._extraconfigopts = extraconfigopts or []
21516
1e275c09242e run-tests: move py3kwarnings to Test.__init__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21515
diff changeset
   446
        self._py3kwarnings = py3kwarnings
21517
af7d3a5c330b run-tests: move shell to Test.__init__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21516
diff changeset
   447
        self._shell = shell
21520
fa6ba4372678 run-tests: remove global abort flag
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21518
diff changeset
   448
fa6ba4372678 run-tests: remove global abort flag
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21518
diff changeset
   449
        self._aborted = False
21319
44c96e2bab20 run-tests: kill daemons during Test.cleanup()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21318
diff changeset
   450
        self._daemonpids = []
21447
f8c5b8a288c5 run-tests: keep track of test execution state in Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21446
diff changeset
   451
        self._finished = None
21449
aedf18bcde11 run-tests: store test return code and output in Test instance
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21448
diff changeset
   452
        self._ret = None
aedf18bcde11 run-tests: store test return code and output in Test instance
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21448
diff changeset
   453
        self._out = None
21453
aaf52b78327e run-tests: store skipped state on Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21452
diff changeset
   454
        self._skipped = None
21454
046587aa1c8a run-tests: refactor testtmp
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21453
diff changeset
   455
        self._testtmp = None
21447
f8c5b8a288c5 run-tests: keep track of test execution state in Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21446
diff changeset
   456
21318
6b3d66e4d3be run-tests: capture reference output in Test.__init__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21317
diff changeset
   457
        # If we're not in --debug mode and reference output file exists,
6b3d66e4d3be run-tests: capture reference output in Test.__init__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21317
diff changeset
   458
        # check test output against it.
21510
97127c4ce460 run-tests: move debug into an argument to Test.__init__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21509
diff changeset
   459
        if debug:
21318
6b3d66e4d3be run-tests: capture reference output in Test.__init__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21317
diff changeset
   460
            self._refout = None # to match "out is None"
21521
855f092c96e5 run-tests: move diff generation into TestResult
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21520
diff changeset
   461
        elif os.path.exists(self.refpath):
21916
792ebd7dc5f6 run-tests: write out scripts in binary mode
Augie Fackler <raf@durin42.com>
parents: 21763
diff changeset
   462
            f = open(self.refpath, 'rb')
21318
6b3d66e4d3be run-tests: capture reference output in Test.__init__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21317
diff changeset
   463
            self._refout = f.read().splitlines(True)
6b3d66e4d3be run-tests: capture reference output in Test.__init__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21317
diff changeset
   464
            f.close()
6b3d66e4d3be run-tests: capture reference output in Test.__init__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21317
diff changeset
   465
        else:
6b3d66e4d3be run-tests: capture reference output in Test.__init__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21317
diff changeset
   466
            self._refout = []
6b3d66e4d3be run-tests: capture reference output in Test.__init__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21317
diff changeset
   467
24965
cecbe207cebd run-tests: implement Test._testMethodName
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24926
diff changeset
   468
    # needed to get base class __repr__ running
cecbe207cebd run-tests: implement Test._testMethodName
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24926
diff changeset
   469
    @property
cecbe207cebd run-tests: implement Test._testMethodName
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24926
diff changeset
   470
    def _testMethodName(self):
cecbe207cebd run-tests: implement Test._testMethodName
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24926
diff changeset
   471
        return self.name
cecbe207cebd run-tests: implement Test._testMethodName
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24926
diff changeset
   472
21463
c908ff887589 run-tests: implement Test.__str__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21462
diff changeset
   473
    def __str__(self):
c908ff887589 run-tests: implement Test.__str__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21462
diff changeset
   474
        return self.name
c908ff887589 run-tests: implement Test.__str__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21462
diff changeset
   475
21488
feb8ad2d57ee run-tests: merge MercurialTest into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21464
diff changeset
   476
    def shortDescription(self):
feb8ad2d57ee run-tests: merge MercurialTest into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21464
diff changeset
   477
        return self.name
feb8ad2d57ee run-tests: merge MercurialTest into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21464
diff changeset
   478
21446
9a3b4f795f62 run-tests: support setUp() and tearDown() in TestCase wrapper
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21445
diff changeset
   479
    def setUp(self):
9a3b4f795f62 run-tests: support setUp() and tearDown() in TestCase wrapper
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21445
diff changeset
   480
        """Tasks to perform before run()."""
21447
f8c5b8a288c5 run-tests: keep track of test execution state in Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21446
diff changeset
   481
        self._finished = False
21449
aedf18bcde11 run-tests: store test return code and output in Test instance
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21448
diff changeset
   482
        self._ret = None
aedf18bcde11 run-tests: store test return code and output in Test instance
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21448
diff changeset
   483
        self._out = None
21453
aaf52b78327e run-tests: store skipped state on Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21452
diff changeset
   484
        self._skipped = None
21446
9a3b4f795f62 run-tests: support setUp() and tearDown() in TestCase wrapper
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21445
diff changeset
   485
21497
798c81e32b5e run-tests: refactor temporary directories in Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21496
diff changeset
   486
        try:
798c81e32b5e run-tests: refactor temporary directories in Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21496
diff changeset
   487
            os.mkdir(self._threadtmp)
25031
0adc22a0b6b3 python3: update killdaemons and run-tests print and exception syntax
Augie Fackler <augie@google.com>
parents: 24984
diff changeset
   488
        except OSError as e:
21497
798c81e32b5e run-tests: refactor temporary directories in Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21496
diff changeset
   489
            if e.errno != errno.EEXIST:
798c81e32b5e run-tests: refactor temporary directories in Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21496
diff changeset
   490
                raise
798c81e32b5e run-tests: refactor temporary directories in Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21496
diff changeset
   491
21454
046587aa1c8a run-tests: refactor testtmp
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21453
diff changeset
   492
        self._testtmp = os.path.join(self._threadtmp,
21507
d839e4820da7 run-tests: move test filtering into TestSuite.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21506
diff changeset
   493
                                     os.path.basename(self.path))
21454
046587aa1c8a run-tests: refactor testtmp
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21453
diff changeset
   494
        os.mkdir(self._testtmp)
046587aa1c8a run-tests: refactor testtmp
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21453
diff changeset
   495
21457
12dd94e32102 run-tests: move errpath deletion to setUp()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21456
diff changeset
   496
        # Remove any previous output files.
21507
d839e4820da7 run-tests: move test filtering into TestSuite.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21506
diff changeset
   497
        if os.path.exists(self.errpath):
24332
9612b96730d7 run-tests: ignore ENOENT failures when removing old .err results
Augie Fackler <augie@google.com>
parents: 24331
diff changeset
   498
            try:
9612b96730d7 run-tests: ignore ENOENT failures when removing old .err results
Augie Fackler <augie@google.com>
parents: 24331
diff changeset
   499
                os.remove(self.errpath)
25031
0adc22a0b6b3 python3: update killdaemons and run-tests print and exception syntax
Augie Fackler <augie@google.com>
parents: 24984
diff changeset
   500
            except OSError as e:
24332
9612b96730d7 run-tests: ignore ENOENT failures when removing old .err results
Augie Fackler <augie@google.com>
parents: 24331
diff changeset
   501
                # We might have raced another test to clean up a .err
9612b96730d7 run-tests: ignore ENOENT failures when removing old .err results
Augie Fackler <augie@google.com>
parents: 24331
diff changeset
   502
                # file, so ignore ENOENT when removing a previous .err
9612b96730d7 run-tests: ignore ENOENT failures when removing old .err results
Augie Fackler <augie@google.com>
parents: 24331
diff changeset
   503
                # file.
9612b96730d7 run-tests: ignore ENOENT failures when removing old .err results
Augie Fackler <augie@google.com>
parents: 24331
diff changeset
   504
                if e.errno != errno.ENOENT:
9612b96730d7 run-tests: ignore ENOENT failures when removing old .err results
Augie Fackler <augie@google.com>
parents: 24331
diff changeset
   505
                    raise
21457
12dd94e32102 run-tests: move errpath deletion to setUp()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21456
diff changeset
   506
21488
feb8ad2d57ee run-tests: merge MercurialTest into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21464
diff changeset
   507
    def run(self, result):
21536
92a6b16c9186 run-tests: add docstrings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21535
diff changeset
   508
        """Run this test and report results against a TestResult instance."""
92a6b16c9186 run-tests: add docstrings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21535
diff changeset
   509
        # This function is extremely similar to unittest.TestCase.run(). Once
92a6b16c9186 run-tests: add docstrings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21535
diff changeset
   510
        # we require Python 2.7 (or at least its version of unittest), this
92a6b16c9186 run-tests: add docstrings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21535
diff changeset
   511
        # function can largely go away.
21521
855f092c96e5 run-tests: move diff generation into TestResult
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21520
diff changeset
   512
        self._result = result
21488
feb8ad2d57ee run-tests: merge MercurialTest into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21464
diff changeset
   513
        result.startTest(self)
feb8ad2d57ee run-tests: merge MercurialTest into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21464
diff changeset
   514
        try:
feb8ad2d57ee run-tests: merge MercurialTest into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21464
diff changeset
   515
            try:
feb8ad2d57ee run-tests: merge MercurialTest into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21464
diff changeset
   516
                self.setUp()
feb8ad2d57ee run-tests: merge MercurialTest into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21464
diff changeset
   517
            except (KeyboardInterrupt, SystemExit):
21520
fa6ba4372678 run-tests: remove global abort flag
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21518
diff changeset
   518
                self._aborted = True
21488
feb8ad2d57ee run-tests: merge MercurialTest into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21464
diff changeset
   519
                raise
feb8ad2d57ee run-tests: merge MercurialTest into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21464
diff changeset
   520
            except Exception:
feb8ad2d57ee run-tests: merge MercurialTest into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21464
diff changeset
   521
                result.addError(self, sys.exc_info())
feb8ad2d57ee run-tests: merge MercurialTest into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21464
diff changeset
   522
                return
feb8ad2d57ee run-tests: merge MercurialTest into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21464
diff changeset
   523
feb8ad2d57ee run-tests: merge MercurialTest into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21464
diff changeset
   524
            success = False
feb8ad2d57ee run-tests: merge MercurialTest into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21464
diff changeset
   525
            try:
feb8ad2d57ee run-tests: merge MercurialTest into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21464
diff changeset
   526
                self.runTest()
feb8ad2d57ee run-tests: merge MercurialTest into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21464
diff changeset
   527
            except KeyboardInterrupt:
21520
fa6ba4372678 run-tests: remove global abort flag
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21518
diff changeset
   528
                self._aborted = True
21488
feb8ad2d57ee run-tests: merge MercurialTest into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21464
diff changeset
   529
                raise
25031
0adc22a0b6b3 python3: update killdaemons and run-tests print and exception syntax
Augie Fackler <augie@google.com>
parents: 24984
diff changeset
   530
            except SkipTest as e:
21488
feb8ad2d57ee run-tests: merge MercurialTest into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21464
diff changeset
   531
                result.addSkip(self, str(e))
21997
93c3b3f55d59 run-tests: fix test result counts with --keyword specified or skips occurring
Augie Fackler <raf@durin42.com>
parents: 21993
diff changeset
   532
                # The base class will have already counted this as a
93c3b3f55d59 run-tests: fix test result counts with --keyword specified or skips occurring
Augie Fackler <raf@durin42.com>
parents: 21993
diff changeset
   533
                # test we "ran", but we want to exclude skipped tests
93c3b3f55d59 run-tests: fix test result counts with --keyword specified or skips occurring
Augie Fackler <raf@durin42.com>
parents: 21993
diff changeset
   534
                # from those we count towards those run.
93c3b3f55d59 run-tests: fix test result counts with --keyword specified or skips occurring
Augie Fackler <raf@durin42.com>
parents: 21993
diff changeset
   535
                result.testsRun -= 1
25031
0adc22a0b6b3 python3: update killdaemons and run-tests print and exception syntax
Augie Fackler <augie@google.com>
parents: 24984
diff changeset
   536
            except IgnoreTest as e:
21488
feb8ad2d57ee run-tests: merge MercurialTest into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21464
diff changeset
   537
                result.addIgnore(self, str(e))
21997
93c3b3f55d59 run-tests: fix test result counts with --keyword specified or skips occurring
Augie Fackler <raf@durin42.com>
parents: 21993
diff changeset
   538
                # As with skips, ignores also should be excluded from
93c3b3f55d59 run-tests: fix test result counts with --keyword specified or skips occurring
Augie Fackler <raf@durin42.com>
parents: 21993
diff changeset
   539
                # the number of tests executed.
93c3b3f55d59 run-tests: fix test result counts with --keyword specified or skips occurring
Augie Fackler <raf@durin42.com>
parents: 21993
diff changeset
   540
                result.testsRun -= 1
25031
0adc22a0b6b3 python3: update killdaemons and run-tests print and exception syntax
Augie Fackler <augie@google.com>
parents: 24984
diff changeset
   541
            except WarnTest as e:
21488
feb8ad2d57ee run-tests: merge MercurialTest into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21464
diff changeset
   542
                result.addWarn(self, str(e))
25031
0adc22a0b6b3 python3: update killdaemons and run-tests print and exception syntax
Augie Fackler <augie@google.com>
parents: 24984
diff changeset
   543
            except self.failureException as e:
21488
feb8ad2d57ee run-tests: merge MercurialTest into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21464
diff changeset
   544
                # This differs from unittest in that we don't capture
feb8ad2d57ee run-tests: merge MercurialTest into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21464
diff changeset
   545
                # the stack trace. This is for historical reasons and
23139
e53f6b72a0e4 spelling: fixes from proofreading of spell checker issues
Mads Kiilerich <madski@unity3d.com>
parents: 23077
diff changeset
   546
                # this decision could be revisited in the future,
21488
feb8ad2d57ee run-tests: merge MercurialTest into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21464
diff changeset
   547
                # especially for PythonTest instances.
21753
b7baef94a333 run-tests: checks behaviour of test on failure while testing
anuraggoel <anurag.dsps@gmail.com>
parents: 21740
diff changeset
   548
                if result.addFailure(self, str(e)):
b7baef94a333 run-tests: checks behaviour of test on failure while testing
anuraggoel <anurag.dsps@gmail.com>
parents: 21740
diff changeset
   549
                    success = True
21488
feb8ad2d57ee run-tests: merge MercurialTest into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21464
diff changeset
   550
            except Exception:
feb8ad2d57ee run-tests: merge MercurialTest into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21464
diff changeset
   551
                result.addError(self, sys.exc_info())
feb8ad2d57ee run-tests: merge MercurialTest into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21464
diff changeset
   552
            else:
feb8ad2d57ee run-tests: merge MercurialTest into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21464
diff changeset
   553
                success = True
feb8ad2d57ee run-tests: merge MercurialTest into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21464
diff changeset
   554
feb8ad2d57ee run-tests: merge MercurialTest into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21464
diff changeset
   555
            try:
feb8ad2d57ee run-tests: merge MercurialTest into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21464
diff changeset
   556
                self.tearDown()
feb8ad2d57ee run-tests: merge MercurialTest into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21464
diff changeset
   557
            except (KeyboardInterrupt, SystemExit):
21520
fa6ba4372678 run-tests: remove global abort flag
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21518
diff changeset
   558
                self._aborted = True
21488
feb8ad2d57ee run-tests: merge MercurialTest into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21464
diff changeset
   559
                raise
feb8ad2d57ee run-tests: merge MercurialTest into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21464
diff changeset
   560
            except Exception:
feb8ad2d57ee run-tests: merge MercurialTest into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21464
diff changeset
   561
                result.addError(self, sys.exc_info())
feb8ad2d57ee run-tests: merge MercurialTest into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21464
diff changeset
   562
                success = False
feb8ad2d57ee run-tests: merge MercurialTest into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21464
diff changeset
   563
feb8ad2d57ee run-tests: merge MercurialTest into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21464
diff changeset
   564
            if success:
feb8ad2d57ee run-tests: merge MercurialTest into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21464
diff changeset
   565
                result.addSuccess(self)
feb8ad2d57ee run-tests: merge MercurialTest into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21464
diff changeset
   566
        finally:
21520
fa6ba4372678 run-tests: remove global abort flag
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21518
diff changeset
   567
            result.stopTest(self, interrupted=self._aborted)
21488
feb8ad2d57ee run-tests: merge MercurialTest into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21464
diff changeset
   568
feb8ad2d57ee run-tests: merge MercurialTest into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21464
diff changeset
   569
    def runTest(self):
21383
772ed56e2519 run-tests: add some docstrings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21382
diff changeset
   570
        """Run this test instance.
772ed56e2519 run-tests: add some docstrings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21382
diff changeset
   571
772ed56e2519 run-tests: add some docstrings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21382
diff changeset
   572
        This will return a tuple describing the result of the test.
772ed56e2519 run-tests: add some docstrings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21382
diff changeset
   573
        """
21514
59fe123dbb00 run-tests: refactor port number declaration
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21513
diff changeset
   574
        env = self._getenv()
21319
44c96e2bab20 run-tests: kill daemons during Test.cleanup()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21318
diff changeset
   575
        self._daemonpids.append(env['DAEMON_PIDS'])
21382
4b8ffe3abdd2 run-tests: move createhgrc into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21381
diff changeset
   576
        self._createhgrc(env['HGRCPATH'])
21300
a2774731a51a run-tests: move createhgrc() call into Test.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21299
diff changeset
   577
21435
f376f56a354e run-tests: rename Test._test to Test.name
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21434
diff changeset
   578
        vlog('# Test', self.name)
21337
79930515604f run-tests: move logging of test start into Test.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21336
diff changeset
   579
24516
62fb03e0d990 run-tests: obtain replacements inside Test._runcommand
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24510
diff changeset
   580
        ret, out = self._run(env)
21500
130cc0d7bfde run-tests: don't trap exceptions in Test.runTest()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21499
diff changeset
   581
        self._finished = True
130cc0d7bfde run-tests: don't trap exceptions in Test.runTest()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21499
diff changeset
   582
        self._ret = ret
130cc0d7bfde run-tests: don't trap exceptions in Test.runTest()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21499
diff changeset
   583
        self._out = out
21305
d7a7825ff2cf run-tests: capture execution results in a TestResult class
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21304
diff changeset
   584
21326
04e04766065f run-tests: move output difference processing to Test.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21325
diff changeset
   585
        def describe(ret):
04e04766065f run-tests: move output difference processing to Test.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21325
diff changeset
   586
            if ret < 0:
04e04766065f run-tests: move output difference processing to Test.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21325
diff changeset
   587
                return 'killed by signal: %d' % -ret
04e04766065f run-tests: move output difference processing to Test.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21325
diff changeset
   588
            return 'returned error code %d' % ret
04e04766065f run-tests: move output difference processing to Test.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21325
diff changeset
   589
21453
aaf52b78327e run-tests: store skipped state on Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21452
diff changeset
   590
        self._skipped = False
21336
45ab0668d1b2 run-tests: remove remaining uses of TestResult
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21335
diff changeset
   591
21380
de6ea36ca9f7 run-tests: move SKIPPED_STATUS into Test class
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21379
diff changeset
   592
        if ret == self.SKIPPED_STATUS:
21324
6454ddaee991 run-tests: add skip processing to Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21323
diff changeset
   593
            if out is None: # Debug mode, nothing to parse.
6454ddaee991 run-tests: add skip processing to Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21323
diff changeset
   594
                missing = ['unknown']
6454ddaee991 run-tests: add skip processing to Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21323
diff changeset
   595
                failed = None
6454ddaee991 run-tests: add skip processing to Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21323
diff changeset
   596
            else:
21379
ab1a95270a50 run-tests: move parsehghaveoutput() into TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21378
diff changeset
   597
                missing, failed = TTest.parsehghaveoutput(out)
21324
6454ddaee991 run-tests: add skip processing to Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21323
diff changeset
   598
6454ddaee991 run-tests: add skip processing to Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21323
diff changeset
   599
            if not missing:
22292
102f0e926668 run-tests: report skipped tests as "skipped" - they might still be "relevant"
Mads Kiilerich <madski@unity3d.com>
parents: 22165
diff changeset
   600
                missing = ['skipped']
21324
6454ddaee991 run-tests: add skip processing to Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21323
diff changeset
   601
6454ddaee991 run-tests: add skip processing to Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21323
diff changeset
   602
            if failed:
21523
9fb6f328576a run-tests: move interactive test acceptance into TestResult
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21522
diff changeset
   603
                self.fail('hg have failed checking for %s' % failed[-1])
21324
6454ddaee991 run-tests: add skip processing to Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21323
diff changeset
   604
            else:
21453
aaf52b78327e run-tests: store skipped state on Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21452
diff changeset
   605
                self._skipped = True
21490
588ebd47cd87 run-tests: replace Test.skip() with raise SkipTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21489
diff changeset
   606
                raise SkipTest(missing[-1])
21325
0e66eb57e42a run-tests: generate timeout result in Test.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21324
diff changeset
   607
        elif ret == 'timeout':
21523
9fb6f328576a run-tests: move interactive test acceptance into TestResult
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21522
diff changeset
   608
            self.fail('timed out')
21522
eeaec308ad5f run-tests: raise WarnTest outside of Test.fail()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21521
diff changeset
   609
        elif ret is False:
eeaec308ad5f run-tests: raise WarnTest outside of Test.fail()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21521
diff changeset
   610
            raise WarnTest('no result code from test')
21326
04e04766065f run-tests: move output difference processing to Test.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21325
diff changeset
   611
        elif out != self._refout:
21614
609a642dff61 run-tests: write .err files earlier
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21613
diff changeset
   612
            # Diff generation may rely on written .err file.
609a642dff61 run-tests: write .err files earlier
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21613
diff changeset
   613
            if (ret != 0 or out != self._refout) and not self._skipped \
609a642dff61 run-tests: write .err files earlier
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21613
diff changeset
   614
                and not self._debug:
609a642dff61 run-tests: write .err files earlier
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21613
diff changeset
   615
                f = open(self.errpath, 'wb')
609a642dff61 run-tests: write .err files earlier
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21613
diff changeset
   616
                for line in out:
609a642dff61 run-tests: write .err files earlier
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21613
diff changeset
   617
                    f.write(line)
609a642dff61 run-tests: write .err files earlier
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21613
diff changeset
   618
                f.close()
609a642dff61 run-tests: write .err files earlier
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21613
diff changeset
   619
21521
855f092c96e5 run-tests: move diff generation into TestResult
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21520
diff changeset
   620
            # The result object handles diff calculation for us.
21763
84cd5ee787ed run-tests: hold iolock across diff/prompt when interactive
Matt Mackall <mpm@selenic.com>
parents: 21754
diff changeset
   621
            if self._result.addOutputMismatch(self, ret, out, self._refout):
84cd5ee787ed run-tests: hold iolock across diff/prompt when interactive
Matt Mackall <mpm@selenic.com>
parents: 21754
diff changeset
   622
                # change was accepted, skip failing
84cd5ee787ed run-tests: hold iolock across diff/prompt when interactive
Matt Mackall <mpm@selenic.com>
parents: 21754
diff changeset
   623
                return
21521
855f092c96e5 run-tests: move diff generation into TestResult
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21520
diff changeset
   624
21326
04e04766065f run-tests: move output difference processing to Test.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21325
diff changeset
   625
            if ret:
21521
855f092c96e5 run-tests: move diff generation into TestResult
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21520
diff changeset
   626
                msg = 'output changed and ' + describe(ret)
21326
04e04766065f run-tests: move output difference processing to Test.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21325
diff changeset
   627
            else:
21521
855f092c96e5 run-tests: move diff generation into TestResult
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21520
diff changeset
   628
                msg = 'output changed'
21326
04e04766065f run-tests: move output difference processing to Test.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21325
diff changeset
   629
21523
9fb6f328576a run-tests: move interactive test acceptance into TestResult
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21522
diff changeset
   630
            self.fail(msg)
21327
206814c9072a run-tests: move remaining result processing to Test.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21326
diff changeset
   631
        elif ret:
21523
9fb6f328576a run-tests: move interactive test acceptance into TestResult
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21522
diff changeset
   632
            self.fail(describe(ret))
21324
6454ddaee991 run-tests: add skip processing to Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21323
diff changeset
   633
21446
9a3b4f795f62 run-tests: support setUp() and tearDown() in TestCase wrapper
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21445
diff changeset
   634
    def tearDown(self):
9a3b4f795f62 run-tests: support setUp() and tearDown() in TestCase wrapper
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21445
diff changeset
   635
        """Tasks to perform after run()."""
21456
a06a4142ad1f run-tests: kill daemons during tearDown()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21455
diff changeset
   636
        for entry in self._daemonpids:
a06a4142ad1f run-tests: kill daemons during tearDown()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21455
diff changeset
   637
            killdaemons(entry)
a06a4142ad1f run-tests: kill daemons during tearDown()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21455
diff changeset
   638
        self._daemonpids = []
a06a4142ad1f run-tests: kill daemons during tearDown()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21455
diff changeset
   639
21509
d21d53ee0d7a run-tests: factor options.keep_tmpdir into an argument to Test.__init__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21508
diff changeset
   640
        if not self._keeptmpdir:
21461
a46a91989d57 run-tests: ignore failures from rmtree
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21460
diff changeset
   641
            shutil.rmtree(self._testtmp, True)
21497
798c81e32b5e run-tests: refactor temporary directories in Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21496
diff changeset
   642
            shutil.rmtree(self._threadtmp, True)
21454
046587aa1c8a run-tests: refactor testtmp
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21453
diff changeset
   643
21455
0f0bace82149 run-tests: move err file writing to tearDown()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21454
diff changeset
   644
        if (self._ret != 0 or self._out != self._refout) and not self._skipped \
21510
97127c4ce460 run-tests: move debug into an argument to Test.__init__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21509
diff changeset
   645
            and not self._debug and self._out:
21507
d839e4820da7 run-tests: move test filtering into TestSuite.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21506
diff changeset
   646
            f = open(self.errpath, 'wb')
21455
0f0bace82149 run-tests: move err file writing to tearDown()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21454
diff changeset
   647
            for line in self._out:
0f0bace82149 run-tests: move err file writing to tearDown()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21454
diff changeset
   648
                f.write(line)
0f0bace82149 run-tests: move err file writing to tearDown()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21454
diff changeset
   649
            f.close()
0f0bace82149 run-tests: move err file writing to tearDown()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21454
diff changeset
   650
24926
3fe1e07f1a32 run-test: include test name in the return vlog
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24811
diff changeset
   651
        vlog("# Ret was:", self._ret, '(%s)' % self.name)
21452
1517c0461b75 run-tests: move some functionality to Test.tearDown()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21451
diff changeset
   652
24516
62fb03e0d990 run-tests: obtain replacements inside Test._runcommand
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24510
diff changeset
   653
    def _run(self, env):
21339
de25e968b4d8 run-tests: refactor runone() into gettest() and scheduletests()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21338
diff changeset
   654
        # This should be implemented in child classes to run tests.
21490
588ebd47cd87 run-tests: replace Test.skip() with raise SkipTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21489
diff changeset
   655
        raise SkipTest('unknown test type')
21296
cd8776030833 run-tests: create classes for representing tests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21231
diff changeset
   656
21520
fa6ba4372678 run-tests: remove global abort flag
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21518
diff changeset
   657
    def abort(self):
fa6ba4372678 run-tests: remove global abort flag
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21518
diff changeset
   658
        """Terminate execution of this test."""
fa6ba4372678 run-tests: remove global abort flag
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21518
diff changeset
   659
        self._aborted = True
fa6ba4372678 run-tests: remove global abort flag
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21518
diff changeset
   660
21454
046587aa1c8a run-tests: refactor testtmp
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21453
diff changeset
   661
    def _getreplacements(self):
21536
92a6b16c9186 run-tests: add docstrings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21535
diff changeset
   662
        """Obtain a mapping of text replacements to apply to test output.
92a6b16c9186 run-tests: add docstrings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21535
diff changeset
   663
92a6b16c9186 run-tests: add docstrings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21535
diff changeset
   664
        Test output needs to be normalized so it can be compared to expected
92a6b16c9186 run-tests: add docstrings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21535
diff changeset
   665
        output. This function defines how some of that normalization will
92a6b16c9186 run-tests: add docstrings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21535
diff changeset
   666
        occur.
92a6b16c9186 run-tests: add docstrings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21535
diff changeset
   667
        """
21298
ba4750352180 run-tests: move replacements generation into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21297
diff changeset
   668
        r = [
21514
59fe123dbb00 run-tests: refactor port number declaration
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21513
diff changeset
   669
            (r':%s\b' % self._startport, ':$HGPORT'),
59fe123dbb00 run-tests: refactor port number declaration
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21513
diff changeset
   670
            (r':%s\b' % (self._startport + 1), ':$HGPORT1'),
59fe123dbb00 run-tests: refactor port number declaration
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21513
diff changeset
   671
            (r':%s\b' % (self._startport + 2), ':$HGPORT2'),
23728
31d3f973d079 run-tests: automatically add (glob) to "saved backup bundle to" lines
Mads Kiilerich <madski@unity3d.com>
parents: 23388
diff changeset
   672
            (r'(?m)^(saved backup bundle to .*\.hg)( \(glob\))?$',
31d3f973d079 run-tests: automatically add (glob) to "saved backup bundle to" lines
Mads Kiilerich <madski@unity3d.com>
parents: 23388
diff changeset
   673
             r'\1 (glob)'),
21298
ba4750352180 run-tests: move replacements generation into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21297
diff changeset
   674
            ]
ba4750352180 run-tests: move replacements generation into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21297
diff changeset
   675
ba4750352180 run-tests: move replacements generation into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21297
diff changeset
   676
        if os.name == 'nt':
ba4750352180 run-tests: move replacements generation into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21297
diff changeset
   677
            r.append(
ba4750352180 run-tests: move replacements generation into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21297
diff changeset
   678
                (''.join(c.isalpha() and '[%s%s]' % (c.lower(), c.upper()) or
ba4750352180 run-tests: move replacements generation into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21297
diff changeset
   679
                    c in '/\\' and r'[/\\]' or c.isdigit() and c or '\\' + c
21454
046587aa1c8a run-tests: refactor testtmp
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21453
diff changeset
   680
                    for c in self._testtmp), '$TESTTMP'))
21298
ba4750352180 run-tests: move replacements generation into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21297
diff changeset
   681
        else:
21454
046587aa1c8a run-tests: refactor testtmp
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21453
diff changeset
   682
            r.append((re.escape(self._testtmp), '$TESTTMP'))
21298
ba4750352180 run-tests: move replacements generation into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21297
diff changeset
   683
21514
59fe123dbb00 run-tests: refactor port number declaration
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21513
diff changeset
   684
        return r
21298
ba4750352180 run-tests: move replacements generation into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21297
diff changeset
   685
21514
59fe123dbb00 run-tests: refactor port number declaration
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21513
diff changeset
   686
    def _getenv(self):
21536
92a6b16c9186 run-tests: add docstrings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21535
diff changeset
   687
        """Obtain environment variables to use during test execution."""
21299
7861de61583b run-tests: move createenv() into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21298
diff changeset
   688
        env = os.environ.copy()
21454
046587aa1c8a run-tests: refactor testtmp
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21453
diff changeset
   689
        env['TESTTMP'] = self._testtmp
046587aa1c8a run-tests: refactor testtmp
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21453
diff changeset
   690
        env['HOME'] = self._testtmp
21514
59fe123dbb00 run-tests: refactor port number declaration
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21513
diff changeset
   691
        env["HGPORT"] = str(self._startport)
59fe123dbb00 run-tests: refactor port number declaration
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21513
diff changeset
   692
        env["HGPORT1"] = str(self._startport + 1)
59fe123dbb00 run-tests: refactor port number declaration
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21513
diff changeset
   693
        env["HGPORT2"] = str(self._startport + 2)
25034
af6e6a0781d7 run-tests: use bytes explicitly for tmpdir and hgrc construction
Augie Fackler <augie@google.com>
parents: 25033
diff changeset
   694
        env["HGRCPATH"] = os.path.join(self._threadtmp, b'.hgrc')
af6e6a0781d7 run-tests: use bytes explicitly for tmpdir and hgrc construction
Augie Fackler <augie@google.com>
parents: 25033
diff changeset
   695
        env["DAEMON_PIDS"] = os.path.join(self._threadtmp, b'daemon.pids')
23347
49cdf51cbc6c run-tests: include quotes in the HGEDITOR value when storing sys.executable
Matt Harbison <matt_harbison@yahoo.com>
parents: 23263
diff changeset
   696
        env["HGEDITOR"] = ('"' + sys.executable + '"'
49cdf51cbc6c run-tests: include quotes in the HGEDITOR value when storing sys.executable
Matt Harbison <matt_harbison@yahoo.com>
parents: 23263
diff changeset
   697
                           + ' -c "import sys; sys.exit(0)"')
21299
7861de61583b run-tests: move createenv() into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21298
diff changeset
   698
        env["HGMERGE"] = "internal:merge"
7861de61583b run-tests: move createenv() into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21298
diff changeset
   699
        env["HGUSER"]   = "test"
7861de61583b run-tests: move createenv() into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21298
diff changeset
   700
        env["HGENCODING"] = "ascii"
7861de61583b run-tests: move createenv() into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21298
diff changeset
   701
        env["HGENCODINGMODE"] = "strict"
7861de61583b run-tests: move createenv() into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21298
diff changeset
   702
7861de61583b run-tests: move createenv() into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21298
diff changeset
   703
        # Reset some environment variables to well-known values so that
7861de61583b run-tests: move createenv() into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21298
diff changeset
   704
        # the tests produce repeatable output.
7861de61583b run-tests: move createenv() into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21298
diff changeset
   705
        env['LANG'] = env['LC_ALL'] = env['LANGUAGE'] = 'C'
7861de61583b run-tests: move createenv() into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21298
diff changeset
   706
        env['TZ'] = 'GMT'
7861de61583b run-tests: move createenv() into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21298
diff changeset
   707
        env["EMAIL"] = "Foo Bar <foo.bar@example.com>"
7861de61583b run-tests: move createenv() into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21298
diff changeset
   708
        env['COLUMNS'] = '80'
7861de61583b run-tests: move createenv() into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21298
diff changeset
   709
        env['TERM'] = 'xterm'
7861de61583b run-tests: move createenv() into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21298
diff changeset
   710
7861de61583b run-tests: move createenv() into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21298
diff changeset
   711
        for k in ('HG HGPROF CDPATH GREP_OPTIONS http_proxy no_proxy ' +
7861de61583b run-tests: move createenv() into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21298
diff changeset
   712
                  'NO_PROXY').split():
7861de61583b run-tests: move createenv() into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21298
diff changeset
   713
            if k in env:
7861de61583b run-tests: move createenv() into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21298
diff changeset
   714
                del env[k]
7861de61583b run-tests: move createenv() into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21298
diff changeset
   715
7861de61583b run-tests: move createenv() into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21298
diff changeset
   716
        # unset env related to hooks
7861de61583b run-tests: move createenv() into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21298
diff changeset
   717
        for k in env.keys():
7861de61583b run-tests: move createenv() into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21298
diff changeset
   718
            if k.startswith('HG_'):
7861de61583b run-tests: move createenv() into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21298
diff changeset
   719
                del env[k]
7861de61583b run-tests: move createenv() into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21298
diff changeset
   720
7861de61583b run-tests: move createenv() into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21298
diff changeset
   721
        return env
7861de61583b run-tests: move createenv() into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21298
diff changeset
   722
21382
4b8ffe3abdd2 run-tests: move createhgrc into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21381
diff changeset
   723
    def _createhgrc(self, path):
21536
92a6b16c9186 run-tests: add docstrings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21535
diff changeset
   724
        """Create an hgrc file for this test."""
21916
792ebd7dc5f6 run-tests: write out scripts in binary mode
Augie Fackler <raf@durin42.com>
parents: 21763
diff changeset
   725
        hgrc = open(path, 'wb')
25034
af6e6a0781d7 run-tests: use bytes explicitly for tmpdir and hgrc construction
Augie Fackler <augie@google.com>
parents: 25033
diff changeset
   726
        hgrc.write(b'[ui]\n')
af6e6a0781d7 run-tests: use bytes explicitly for tmpdir and hgrc construction
Augie Fackler <augie@google.com>
parents: 25033
diff changeset
   727
        hgrc.write(b'slash = True\n')
af6e6a0781d7 run-tests: use bytes explicitly for tmpdir and hgrc construction
Augie Fackler <augie@google.com>
parents: 25033
diff changeset
   728
        hgrc.write(b'interactive = False\n')
af6e6a0781d7 run-tests: use bytes explicitly for tmpdir and hgrc construction
Augie Fackler <augie@google.com>
parents: 25033
diff changeset
   729
        hgrc.write(b'mergemarkers = detailed\n')
af6e6a0781d7 run-tests: use bytes explicitly for tmpdir and hgrc construction
Augie Fackler <augie@google.com>
parents: 25033
diff changeset
   730
        hgrc.write(b'promptecho = True\n')
af6e6a0781d7 run-tests: use bytes explicitly for tmpdir and hgrc construction
Augie Fackler <augie@google.com>
parents: 25033
diff changeset
   731
        hgrc.write(b'[defaults]\n')
af6e6a0781d7 run-tests: use bytes explicitly for tmpdir and hgrc construction
Augie Fackler <augie@google.com>
parents: 25033
diff changeset
   732
        hgrc.write(b'backout = -d "0 0"\n')
af6e6a0781d7 run-tests: use bytes explicitly for tmpdir and hgrc construction
Augie Fackler <augie@google.com>
parents: 25033
diff changeset
   733
        hgrc.write(b'commit = -d "0 0"\n')
af6e6a0781d7 run-tests: use bytes explicitly for tmpdir and hgrc construction
Augie Fackler <augie@google.com>
parents: 25033
diff changeset
   734
        hgrc.write(b'shelve = --date "0 0"\n')
af6e6a0781d7 run-tests: use bytes explicitly for tmpdir and hgrc construction
Augie Fackler <augie@google.com>
parents: 25033
diff changeset
   735
        hgrc.write(b'tag = -d "0 0"\n')
af6e6a0781d7 run-tests: use bytes explicitly for tmpdir and hgrc construction
Augie Fackler <augie@google.com>
parents: 25033
diff changeset
   736
        hgrc.write(b'[devel]\n')
af6e6a0781d7 run-tests: use bytes explicitly for tmpdir and hgrc construction
Augie Fackler <augie@google.com>
parents: 25033
diff changeset
   737
        hgrc.write(b'all = true\n')
af6e6a0781d7 run-tests: use bytes explicitly for tmpdir and hgrc construction
Augie Fackler <augie@google.com>
parents: 25033
diff changeset
   738
        hgrc.write(b'[largefiles]\n')
af6e6a0781d7 run-tests: use bytes explicitly for tmpdir and hgrc construction
Augie Fackler <augie@google.com>
parents: 25033
diff changeset
   739
        hgrc.write(b'usercache = %s\n' %
af6e6a0781d7 run-tests: use bytes explicitly for tmpdir and hgrc construction
Augie Fackler <augie@google.com>
parents: 25033
diff changeset
   740
                   (os.path.join(self._testtmp, b'.cache/largefiles')))
23388
42ed0780ec4b run-tests: set a default largefiles usercache in the default hgrc file
Matt Harbison <matt_harbison@yahoo.com>
parents: 23352
diff changeset
   741
21515
9978ff48b1e8 run-tests: move extra config options to Test.__init__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21514
diff changeset
   742
        for opt in self._extraconfigopts:
9978ff48b1e8 run-tests: move extra config options to Test.__init__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21514
diff changeset
   743
            section, key = opt.split('.', 1)
9978ff48b1e8 run-tests: move extra config options to Test.__init__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21514
diff changeset
   744
            assert '=' in key, ('extra config opt %s must '
9978ff48b1e8 run-tests: move extra config options to Test.__init__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21514
diff changeset
   745
                                'have an = for assignment' % opt)
25034
af6e6a0781d7 run-tests: use bytes explicitly for tmpdir and hgrc construction
Augie Fackler <augie@google.com>
parents: 25033
diff changeset
   746
            hgrc.write(b'[%s]\n%s\n' % (section, key))
21382
4b8ffe3abdd2 run-tests: move createhgrc into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21381
diff changeset
   747
        hgrc.close()
4b8ffe3abdd2 run-tests: move createhgrc into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21381
diff changeset
   748
21523
9fb6f328576a run-tests: move interactive test acceptance into TestResult
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21522
diff changeset
   749
    def fail(self, msg):
21522
eeaec308ad5f run-tests: raise WarnTest outside of Test.fail()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21521
diff changeset
   750
        # unittest differentiates between errored and failed.
eeaec308ad5f run-tests: raise WarnTest outside of Test.fail()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21521
diff changeset
   751
        # Failed is denoted by AssertionError (by default at least).
eeaec308ad5f run-tests: raise WarnTest outside of Test.fail()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21521
diff changeset
   752
        raise AssertionError(msg)
21323
a7c677e2f6ae run-tests: move fail() into Test
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21322
diff changeset
   753
24516
62fb03e0d990 run-tests: obtain replacements inside Test._runcommand
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24510
diff changeset
   754
    def _runcommand(self, cmd, env, normalizenewlines=False):
24508
fbe2fb71a6e6 run-tests: move run into Test class
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24507
diff changeset
   755
        """Run command in a sub-process, capturing the output (stdout and
fbe2fb71a6e6 run-tests: move run into Test class
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24507
diff changeset
   756
        stderr).
fbe2fb71a6e6 run-tests: move run into Test class
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24507
diff changeset
   757
fbe2fb71a6e6 run-tests: move run into Test class
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24507
diff changeset
   758
        Return a tuple (exitcode, output). output is None in debug mode.
fbe2fb71a6e6 run-tests: move run into Test class
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24507
diff changeset
   759
        """
24509
27092bb70293 run-tests: remove arguments from Test._runcommand
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24508
diff changeset
   760
        if self._debug:
27092bb70293 run-tests: remove arguments from Test._runcommand
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24508
diff changeset
   761
            proc = subprocess.Popen(cmd, shell=True, cwd=self._testtmp,
27092bb70293 run-tests: remove arguments from Test._runcommand
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24508
diff changeset
   762
                                    env=env)
24508
fbe2fb71a6e6 run-tests: move run into Test class
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24507
diff changeset
   763
            ret = proc.wait()
fbe2fb71a6e6 run-tests: move run into Test class
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24507
diff changeset
   764
            return (ret, None)
fbe2fb71a6e6 run-tests: move run into Test class
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24507
diff changeset
   765
24509
27092bb70293 run-tests: remove arguments from Test._runcommand
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24508
diff changeset
   766
        proc = Popen4(cmd, self._testtmp, self._timeout, env)
24508
fbe2fb71a6e6 run-tests: move run into Test class
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24507
diff changeset
   767
        def cleanup():
fbe2fb71a6e6 run-tests: move run into Test class
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24507
diff changeset
   768
            terminate(proc)
fbe2fb71a6e6 run-tests: move run into Test class
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24507
diff changeset
   769
            ret = proc.wait()
fbe2fb71a6e6 run-tests: move run into Test class
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24507
diff changeset
   770
            if ret == 0:
fbe2fb71a6e6 run-tests: move run into Test class
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24507
diff changeset
   771
                ret = signal.SIGTERM << 8
fbe2fb71a6e6 run-tests: move run into Test class
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24507
diff changeset
   772
            killdaemons(env['DAEMON_PIDS'])
fbe2fb71a6e6 run-tests: move run into Test class
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24507
diff changeset
   773
            return ret
fbe2fb71a6e6 run-tests: move run into Test class
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24507
diff changeset
   774
fbe2fb71a6e6 run-tests: move run into Test class
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24507
diff changeset
   775
        output = ''
fbe2fb71a6e6 run-tests: move run into Test class
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24507
diff changeset
   776
        proc.tochild.close()
fbe2fb71a6e6 run-tests: move run into Test class
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24507
diff changeset
   777
fbe2fb71a6e6 run-tests: move run into Test class
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24507
diff changeset
   778
        try:
fbe2fb71a6e6 run-tests: move run into Test class
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24507
diff changeset
   779
            output = proc.fromchild.read()
fbe2fb71a6e6 run-tests: move run into Test class
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24507
diff changeset
   780
        except KeyboardInterrupt:
fbe2fb71a6e6 run-tests: move run into Test class
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24507
diff changeset
   781
            vlog('# Handling keyboard interrupt')
fbe2fb71a6e6 run-tests: move run into Test class
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24507
diff changeset
   782
            cleanup()
fbe2fb71a6e6 run-tests: move run into Test class
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24507
diff changeset
   783
            raise
fbe2fb71a6e6 run-tests: move run into Test class
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24507
diff changeset
   784
fbe2fb71a6e6 run-tests: move run into Test class
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24507
diff changeset
   785
        ret = proc.wait()
fbe2fb71a6e6 run-tests: move run into Test class
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24507
diff changeset
   786
        if wifexited(ret):
fbe2fb71a6e6 run-tests: move run into Test class
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24507
diff changeset
   787
            ret = os.WEXITSTATUS(ret)
fbe2fb71a6e6 run-tests: move run into Test class
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24507
diff changeset
   788
fbe2fb71a6e6 run-tests: move run into Test class
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24507
diff changeset
   789
        if proc.timeout:
fbe2fb71a6e6 run-tests: move run into Test class
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24507
diff changeset
   790
            ret = 'timeout'
fbe2fb71a6e6 run-tests: move run into Test class
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24507
diff changeset
   791
fbe2fb71a6e6 run-tests: move run into Test class
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24507
diff changeset
   792
        if ret:
fbe2fb71a6e6 run-tests: move run into Test class
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24507
diff changeset
   793
            killdaemons(env['DAEMON_PIDS'])
fbe2fb71a6e6 run-tests: move run into Test class
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24507
diff changeset
   794
24516
62fb03e0d990 run-tests: obtain replacements inside Test._runcommand
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24510
diff changeset
   795
        for s, r in self._getreplacements():
24508
fbe2fb71a6e6 run-tests: move run into Test class
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24507
diff changeset
   796
            output = re.sub(s, r, output)
24510
8d6fd0b8f622 run-tests: separate newline normalization from replacements
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24509
diff changeset
   797
8d6fd0b8f622 run-tests: separate newline normalization from replacements
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24509
diff changeset
   798
        if normalizenewlines:
8d6fd0b8f622 run-tests: separate newline normalization from replacements
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24509
diff changeset
   799
            output = output.replace('\r\n', '\n')
8d6fd0b8f622 run-tests: separate newline normalization from replacements
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24509
diff changeset
   800
24508
fbe2fb71a6e6 run-tests: move run into Test class
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24507
diff changeset
   801
        return ret, output.splitlines(True)
fbe2fb71a6e6 run-tests: move run into Test class
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24507
diff changeset
   802
21296
cd8776030833 run-tests: create classes for representing tests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21231
diff changeset
   803
class PythonTest(Test):
cd8776030833 run-tests: create classes for representing tests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21231
diff changeset
   804
    """A Python-based test."""
21501
98a0c58ee200 run-tests: factor refpath into Test classes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21500
diff changeset
   805
98a0c58ee200 run-tests: factor refpath into Test classes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21500
diff changeset
   806
    @property
21521
855f092c96e5 run-tests: move diff generation into TestResult
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21520
diff changeset
   807
    def refpath(self):
21501
98a0c58ee200 run-tests: factor refpath into Test classes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21500
diff changeset
   808
        return os.path.join(self._testdir, '%s.out' % self.name)
98a0c58ee200 run-tests: factor refpath into Test classes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21500
diff changeset
   809
24516
62fb03e0d990 run-tests: obtain replacements inside Test._runcommand
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24510
diff changeset
   810
    def _run(self, env):
21516
1e275c09242e run-tests: move py3kwarnings to Test.__init__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21515
diff changeset
   811
        py3kswitch = self._py3kwarnings and ' -3' or ''
21507
d839e4820da7 run-tests: move test filtering into TestSuite.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21506
diff changeset
   812
        cmd = '%s%s "%s"' % (PYTHON, py3kswitch, self.path)
21311
f9a7018a35ff run-tests: roll pytest() into PythonTest._run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21310
diff changeset
   813
        vlog("# Running", cmd)
24510
8d6fd0b8f622 run-tests: separate newline normalization from replacements
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24509
diff changeset
   814
        normalizenewlines = os.name == 'nt'
24516
62fb03e0d990 run-tests: obtain replacements inside Test._runcommand
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24510
diff changeset
   815
        result = self._runcommand(cmd, env,
24510
8d6fd0b8f622 run-tests: separate newline normalization from replacements
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24509
diff changeset
   816
                                  normalizenewlines=normalizenewlines)
21520
fa6ba4372678 run-tests: remove global abort flag
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21518
diff changeset
   817
        if self._aborted:
fa6ba4372678 run-tests: remove global abort flag
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21518
diff changeset
   818
            raise KeyboardInterrupt()
fa6ba4372678 run-tests: remove global abort flag
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21518
diff changeset
   819
fa6ba4372678 run-tests: remove global abort flag
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21518
diff changeset
   820
        return result
21311
f9a7018a35ff run-tests: roll pytest() into PythonTest._run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21310
diff changeset
   821
23352
5bd04faaa3ee run-tests: don't warn on unnecessary globs mandated by check-code.py
Matt Harbison <matt_harbison@yahoo.com>
parents: 23347
diff changeset
   822
# This script may want to drop globs from lines matching these patterns on
5bd04faaa3ee run-tests: don't warn on unnecessary globs mandated by check-code.py
Matt Harbison <matt_harbison@yahoo.com>
parents: 23347
diff changeset
   823
# Windows, but check-code.py wants a glob on these lines unconditionally.  Don't
5bd04faaa3ee run-tests: don't warn on unnecessary globs mandated by check-code.py
Matt Harbison <matt_harbison@yahoo.com>
parents: 23347
diff changeset
   824
# warn if that is the case for anything matching these lines.
5bd04faaa3ee run-tests: don't warn on unnecessary globs mandated by check-code.py
Matt Harbison <matt_harbison@yahoo.com>
parents: 23347
diff changeset
   825
checkcodeglobpats = [
5bd04faaa3ee run-tests: don't warn on unnecessary globs mandated by check-code.py
Matt Harbison <matt_harbison@yahoo.com>
parents: 23347
diff changeset
   826
    re.compile(r'^pushing to \$TESTTMP/.*[^)]$'),
5bd04faaa3ee run-tests: don't warn on unnecessary globs mandated by check-code.py
Matt Harbison <matt_harbison@yahoo.com>
parents: 23347
diff changeset
   827
    re.compile(r'^moving \S+/.*[^)]$'),
5bd04faaa3ee run-tests: don't warn on unnecessary globs mandated by check-code.py
Matt Harbison <matt_harbison@yahoo.com>
parents: 23347
diff changeset
   828
    re.compile(r'^pulling from \$TESTTMP/.*[^)]$')
5bd04faaa3ee run-tests: don't warn on unnecessary globs mandated by check-code.py
Matt Harbison <matt_harbison@yahoo.com>
parents: 23347
diff changeset
   829
]
5bd04faaa3ee run-tests: don't warn on unnecessary globs mandated by check-code.py
Matt Harbison <matt_harbison@yahoo.com>
parents: 23347
diff changeset
   830
25036
61fc2cdbc57c run-tests: work around chr() producing unicode in Python 3
Augie Fackler <augie@google.com>
parents: 25035
diff changeset
   831
bchr = chr
61fc2cdbc57c run-tests: work around chr() producing unicode in Python 3
Augie Fackler <augie@google.com>
parents: 25035
diff changeset
   832
if sys.version_info[0] == 3:
61fc2cdbc57c run-tests: work around chr() producing unicode in Python 3
Augie Fackler <augie@google.com>
parents: 25035
diff changeset
   833
    bchr = lambda x: bytes([x])
61fc2cdbc57c run-tests: work around chr() producing unicode in Python 3
Augie Fackler <augie@google.com>
parents: 25035
diff changeset
   834
21296
cd8776030833 run-tests: create classes for representing tests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21231
diff changeset
   835
class TTest(Test):
cd8776030833 run-tests: create classes for representing tests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21231
diff changeset
   836
    """A "t test" is a test backed by a .t file."""
cd8776030833 run-tests: create classes for representing tests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21231
diff changeset
   837
21381
9aa5784992d4 run-tests: move SKIPPED_PREFIX and FAILED_PREFIX into TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21380
diff changeset
   838
    SKIPPED_PREFIX = 'skipped: '
9aa5784992d4 run-tests: move SKIPPED_PREFIX and FAILED_PREFIX into TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21380
diff changeset
   839
    FAILED_PREFIX = 'hghave check failed: '
21384
a36cc85a5b7b run-tests: move string escaping to TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21383
diff changeset
   840
    NEEDESCAPE = re.compile(r'[\x00-\x08\x0b-\x1f\x7f-\xff]').search
a36cc85a5b7b run-tests: move string escaping to TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21383
diff changeset
   841
a36cc85a5b7b run-tests: move string escaping to TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21383
diff changeset
   842
    ESCAPESUB = re.compile(r'[\x00-\x08\x0b-\x1f\\\x7f-\xff]').sub
21539
0c4077905932 run-tests: assign value to ESCAPEMAP - dict.update do not return self
Mads Kiilerich <madski@unity3d.com>
parents: 21538
diff changeset
   843
    ESCAPEMAP = dict((chr(i), r'\x%02x' % i) for i in range(256))
0c4077905932 run-tests: assign value to ESCAPEMAP - dict.update do not return self
Mads Kiilerich <madski@unity3d.com>
parents: 21538
diff changeset
   844
    ESCAPEMAP.update({'\\': '\\\\', '\r': r'\r'})
21381
9aa5784992d4 run-tests: move SKIPPED_PREFIX and FAILED_PREFIX into TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21380
diff changeset
   845
21501
98a0c58ee200 run-tests: factor refpath into Test classes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21500
diff changeset
   846
    @property
21521
855f092c96e5 run-tests: move diff generation into TestResult
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21520
diff changeset
   847
    def refpath(self):
21501
98a0c58ee200 run-tests: factor refpath into Test classes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21500
diff changeset
   848
        return os.path.join(self._testdir, self.name)
98a0c58ee200 run-tests: factor refpath into Test classes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21500
diff changeset
   849
24516
62fb03e0d990 run-tests: obtain replacements inside Test._runcommand
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24510
diff changeset
   850
    def _run(self, env):
21916
792ebd7dc5f6 run-tests: write out scripts in binary mode
Augie Fackler <raf@durin42.com>
parents: 21763
diff changeset
   851
        f = open(self.path, 'rb')
21313
a2bd02a3b6d2 run-tests: move t test execution from tsttest() to TTest.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21312
diff changeset
   852
        lines = f.readlines()
a2bd02a3b6d2 run-tests: move t test execution from tsttest() to TTest.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21312
diff changeset
   853
        f.close()
a2bd02a3b6d2 run-tests: move t test execution from tsttest() to TTest.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21312
diff changeset
   854
21454
046587aa1c8a run-tests: refactor testtmp
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21453
diff changeset
   855
        salt, script, after, expected = self._parsetest(lines)
21313
a2bd02a3b6d2 run-tests: move t test execution from tsttest() to TTest.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21312
diff changeset
   856
a2bd02a3b6d2 run-tests: move t test execution from tsttest() to TTest.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21312
diff changeset
   857
        # Write out the generated script.
21454
046587aa1c8a run-tests: refactor testtmp
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21453
diff changeset
   858
        fname = '%s.sh' % self._testtmp
21916
792ebd7dc5f6 run-tests: write out scripts in binary mode
Augie Fackler <raf@durin42.com>
parents: 21763
diff changeset
   859
        f = open(fname, 'wb')
21313
a2bd02a3b6d2 run-tests: move t test execution from tsttest() to TTest.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21312
diff changeset
   860
        for l in script:
a2bd02a3b6d2 run-tests: move t test execution from tsttest() to TTest.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21312
diff changeset
   861
            f.write(l)
a2bd02a3b6d2 run-tests: move t test execution from tsttest() to TTest.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21312
diff changeset
   862
        f.close()
a2bd02a3b6d2 run-tests: move t test execution from tsttest() to TTest.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21312
diff changeset
   863
21517
af7d3a5c330b run-tests: move shell to Test.__init__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21516
diff changeset
   864
        cmd = '%s "%s"' % (self._shell, fname)
21313
a2bd02a3b6d2 run-tests: move t test execution from tsttest() to TTest.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21312
diff changeset
   865
        vlog("# Running", cmd)
a2bd02a3b6d2 run-tests: move t test execution from tsttest() to TTest.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21312
diff changeset
   866
24516
62fb03e0d990 run-tests: obtain replacements inside Test._runcommand
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24510
diff changeset
   867
        exitcode, output = self._runcommand(cmd, env)
21520
fa6ba4372678 run-tests: remove global abort flag
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21518
diff changeset
   868
fa6ba4372678 run-tests: remove global abort flag
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21518
diff changeset
   869
        if self._aborted:
fa6ba4372678 run-tests: remove global abort flag
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21518
diff changeset
   870
            raise KeyboardInterrupt()
fa6ba4372678 run-tests: remove global abort flag
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21518
diff changeset
   871
21313
a2bd02a3b6d2 run-tests: move t test execution from tsttest() to TTest.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21312
diff changeset
   872
        # Do not merge output if skipped. Return hghave message instead.
a2bd02a3b6d2 run-tests: move t test execution from tsttest() to TTest.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21312
diff changeset
   873
        # Similarly, with --debug, output is None.
21380
de6ea36ca9f7 run-tests: move SKIPPED_STATUS into Test class
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21379
diff changeset
   874
        if exitcode == self.SKIPPED_STATUS or output is None:
21313
a2bd02a3b6d2 run-tests: move t test execution from tsttest() to TTest.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21312
diff changeset
   875
            return exitcode, output
a2bd02a3b6d2 run-tests: move t test execution from tsttest() to TTest.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21312
diff changeset
   876
21314
76d7967d8f3d run-tests: finish moving tsttest() into TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21313
diff changeset
   877
        return self._processoutput(exitcode, output, salt, after, expected)
21296
cd8776030833 run-tests: create classes for representing tests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21231
diff changeset
   878
21454
046587aa1c8a run-tests: refactor testtmp
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21453
diff changeset
   879
    def _hghave(self, reqs):
21312
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   880
        # TODO do something smarter when all other uses of hghave are gone.
21341
cb88d4a04f58 run-tests: move TESTDIR out of a global
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21340
diff changeset
   881
        tdir = self._testdir.replace('\\', '/')
21312
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   882
        proc = Popen4('%s -c "%s/hghave %s"' %
21517
af7d3a5c330b run-tests: move shell to Test.__init__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21516
diff changeset
   883
                      (self._shell, tdir, ' '.join(reqs)),
23933
769027075e21 run-tests.py: execute hghave with same env vars as ones for actual tests
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23859
diff changeset
   884
                      self._testtmp, 0, self._getenv())
21312
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   885
        stdout, stderr = proc.communicate()
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   886
        ret = proc.wait()
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   887
        if wifexited(ret):
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   888
            ret = os.WEXITSTATUS(ret)
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   889
        if ret == 2:
25031
0adc22a0b6b3 python3: update killdaemons and run-tests print and exception syntax
Augie Fackler <augie@google.com>
parents: 24984
diff changeset
   890
            print(stdout)
21312
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   891
            sys.exit(1)
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   892
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   893
        return ret == 0
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   894
21454
046587aa1c8a run-tests: refactor testtmp
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21453
diff changeset
   895
    def _parsetest(self, lines):
21312
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   896
        # We generate a shell script which outputs unique markers to line
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   897
        # up script results with our source. These markers include input
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   898
        # line number and the last return code.
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   899
        salt = "SALT" + str(time.time())
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   900
        def addsalt(line, inpython):
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   901
            if inpython:
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   902
                script.append('%s %d 0\n' % (salt, line))
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   903
            else:
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   904
                script.append('echo %s %s $?\n' % (salt, line))
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   905
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   906
        script = []
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   907
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   908
        # After we run the shell script, we re-unify the script output
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   909
        # with non-active parts of the source, with synchronization by our
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   910
        # SALT line number markers. The after table contains the non-active
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   911
        # components, ordered by line number.
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   912
        after = {}
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   913
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   914
        # Expected shell script output.
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   915
        expected = {}
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   916
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   917
        pos = prepos = -1
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   918
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   919
        # True or False when in a true or false conditional section
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   920
        skipping = None
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   921
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   922
        # We keep track of whether or not we're in a Python block so we
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   923
        # can generate the surrounding doctest magic.
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   924
        inpython = False
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   925
21510
97127c4ce460 run-tests: move debug into an argument to Test.__init__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21509
diff changeset
   926
        if self._debug:
21312
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   927
            script.append('set -x\n')
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   928
        if os.getenv('MSYSTEM'):
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   929
            script.append('alias pwd="pwd -W"\n')
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   930
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   931
        for n, l in enumerate(lines):
25035
1203ca7005fa run-tests: use bytes when constructing shell script
Augie Fackler <augie@google.com>
parents: 25034
diff changeset
   932
            if not l.endswith(b'\n'):
1203ca7005fa run-tests: use bytes when constructing shell script
Augie Fackler <augie@google.com>
parents: 25034
diff changeset
   933
                l += b'\n'
1203ca7005fa run-tests: use bytes when constructing shell script
Augie Fackler <augie@google.com>
parents: 25034
diff changeset
   934
            if l.startswith(b'#require'):
22045
769198c6a62d run-tests: add #require to abort full test
Matt Mackall <mpm@selenic.com>
parents: 22044
diff changeset
   935
                lsplit = l.split()
25035
1203ca7005fa run-tests: use bytes when constructing shell script
Augie Fackler <augie@google.com>
parents: 25034
diff changeset
   936
                if len(lsplit) < 2 or lsplit[0] != b'#require':
22045
769198c6a62d run-tests: add #require to abort full test
Matt Mackall <mpm@selenic.com>
parents: 22044
diff changeset
   937
                    after.setdefault(pos, []).append('  !!! invalid #require\n')
769198c6a62d run-tests: add #require to abort full test
Matt Mackall <mpm@selenic.com>
parents: 22044
diff changeset
   938
                if not self._hghave(lsplit[1:]):
769198c6a62d run-tests: add #require to abort full test
Matt Mackall <mpm@selenic.com>
parents: 22044
diff changeset
   939
                    script = ["exit 80\n"]
769198c6a62d run-tests: add #require to abort full test
Matt Mackall <mpm@selenic.com>
parents: 22044
diff changeset
   940
                    break
769198c6a62d run-tests: add #require to abort full test
Matt Mackall <mpm@selenic.com>
parents: 22044
diff changeset
   941
                after.setdefault(pos, []).append(l)
25035
1203ca7005fa run-tests: use bytes when constructing shell script
Augie Fackler <augie@google.com>
parents: 25034
diff changeset
   942
            elif l.startswith(b'#if'):
21312
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   943
                lsplit = l.split()
25035
1203ca7005fa run-tests: use bytes when constructing shell script
Augie Fackler <augie@google.com>
parents: 25034
diff changeset
   944
                if len(lsplit) < 2 or lsplit[0] != b'#if':
21312
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   945
                    after.setdefault(pos, []).append('  !!! invalid #if\n')
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   946
                if skipping is not None:
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   947
                    after.setdefault(pos, []).append('  !!! nested #if\n')
21454
046587aa1c8a run-tests: refactor testtmp
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21453
diff changeset
   948
                skipping = not self._hghave(lsplit[1:])
21312
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   949
                after.setdefault(pos, []).append(l)
25035
1203ca7005fa run-tests: use bytes when constructing shell script
Augie Fackler <augie@google.com>
parents: 25034
diff changeset
   950
            elif l.startswith(b'#else'):
21312
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   951
                if skipping is None:
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   952
                    after.setdefault(pos, []).append('  !!! missing #if\n')
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   953
                skipping = not skipping
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   954
                after.setdefault(pos, []).append(l)
25035
1203ca7005fa run-tests: use bytes when constructing shell script
Augie Fackler <augie@google.com>
parents: 25034
diff changeset
   955
            elif l.startswith(b'#endif'):
21312
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   956
                if skipping is None:
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   957
                    after.setdefault(pos, []).append('  !!! missing #if\n')
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   958
                skipping = None
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   959
                after.setdefault(pos, []).append(l)
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   960
            elif skipping:
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   961
                after.setdefault(pos, []).append(l)
25035
1203ca7005fa run-tests: use bytes when constructing shell script
Augie Fackler <augie@google.com>
parents: 25034
diff changeset
   962
            elif l.startswith(b'  >>> '): # python inlines
21312
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   963
                after.setdefault(pos, []).append(l)
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   964
                prepos = pos
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   965
                pos = n
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   966
                if not inpython:
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   967
                    # We've just entered a Python block. Add the header.
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   968
                    inpython = True
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   969
                    addsalt(prepos, False) # Make sure we report the exit code.
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   970
                    script.append('%s -m heredoctest <<EOF\n' % PYTHON)
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   971
                addsalt(n, True)
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   972
                script.append(l[2:])
25035
1203ca7005fa run-tests: use bytes when constructing shell script
Augie Fackler <augie@google.com>
parents: 25034
diff changeset
   973
            elif l.startswith(b'  ... '): # python inlines
21312
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   974
                after.setdefault(prepos, []).append(l)
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   975
                script.append(l[2:])
25035
1203ca7005fa run-tests: use bytes when constructing shell script
Augie Fackler <augie@google.com>
parents: 25034
diff changeset
   976
            elif l.startswith(b'  $ '): # commands
21312
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   977
                if inpython:
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   978
                    script.append('EOF\n')
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   979
                    inpython = False
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   980
                after.setdefault(pos, []).append(l)
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   981
                prepos = pos
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   982
                pos = n
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   983
                addsalt(n, False)
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   984
                cmd = l[4:].split()
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   985
                if len(cmd) == 2 and cmd[0] == 'cd':
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   986
                    l = '  $ cd %s || exit 1\n' % cmd[1]
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   987
                script.append(l[4:])
25035
1203ca7005fa run-tests: use bytes when constructing shell script
Augie Fackler <augie@google.com>
parents: 25034
diff changeset
   988
            elif l.startswith(b'  > '): # continuations
21312
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   989
                after.setdefault(prepos, []).append(l)
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   990
                script.append(l[4:])
25035
1203ca7005fa run-tests: use bytes when constructing shell script
Augie Fackler <augie@google.com>
parents: 25034
diff changeset
   991
            elif l.startswith(b'  '): # results
21312
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   992
                # Queue up a list of expected results.
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   993
                expected.setdefault(pos, []).append(l[2:])
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   994
            else:
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   995
                if inpython:
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   996
                    script.append('EOF\n')
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   997
                    inpython = False
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   998
                # Non-command/result. Queue up for merged output.
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
   999
                after.setdefault(pos, []).append(l)
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
  1000
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
  1001
        if inpython:
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
  1002
            script.append('EOF\n')
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
  1003
        if skipping is not None:
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
  1004
            after.setdefault(pos, []).append('  !!! missing #endif\n')
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
  1005
        addsalt(n + 1, False)
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
  1006
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
  1007
        return salt, script, after, expected
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
  1008
21314
76d7967d8f3d run-tests: finish moving tsttest() into TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21313
diff changeset
  1009
    def _processoutput(self, exitcode, output, salt, after, expected):
76d7967d8f3d run-tests: finish moving tsttest() into TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21313
diff changeset
  1010
        # Merge the script output back into a unified test.
76d7967d8f3d run-tests: finish moving tsttest() into TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21313
diff changeset
  1011
        warnonly = 1 # 1: not yet; 2: yes; 3: for sure not
76d7967d8f3d run-tests: finish moving tsttest() into TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21313
diff changeset
  1012
        if exitcode != 0:
76d7967d8f3d run-tests: finish moving tsttest() into TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21313
diff changeset
  1013
            warnonly = 3
76d7967d8f3d run-tests: finish moving tsttest() into TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21313
diff changeset
  1014
76d7967d8f3d run-tests: finish moving tsttest() into TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21313
diff changeset
  1015
        pos = -1
76d7967d8f3d run-tests: finish moving tsttest() into TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21313
diff changeset
  1016
        postout = []
76d7967d8f3d run-tests: finish moving tsttest() into TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21313
diff changeset
  1017
        for l in output:
76d7967d8f3d run-tests: finish moving tsttest() into TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21313
diff changeset
  1018
            lout, lcmd = l, None
76d7967d8f3d run-tests: finish moving tsttest() into TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21313
diff changeset
  1019
            if salt in l:
76d7967d8f3d run-tests: finish moving tsttest() into TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21313
diff changeset
  1020
                lout, lcmd = l.split(salt, 1)
76d7967d8f3d run-tests: finish moving tsttest() into TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21313
diff changeset
  1021
76d7967d8f3d run-tests: finish moving tsttest() into TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21313
diff changeset
  1022
            if lout:
76d7967d8f3d run-tests: finish moving tsttest() into TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21313
diff changeset
  1023
                if not lout.endswith('\n'):
76d7967d8f3d run-tests: finish moving tsttest() into TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21313
diff changeset
  1024
                    lout += ' (no-eol)\n'
76d7967d8f3d run-tests: finish moving tsttest() into TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21313
diff changeset
  1025
76d7967d8f3d run-tests: finish moving tsttest() into TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21313
diff changeset
  1026
                # Find the expected output at the current position.
76d7967d8f3d run-tests: finish moving tsttest() into TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21313
diff changeset
  1027
                el = None
76d7967d8f3d run-tests: finish moving tsttest() into TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21313
diff changeset
  1028
                if expected.get(pos, None):
76d7967d8f3d run-tests: finish moving tsttest() into TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21313
diff changeset
  1029
                    el = expected[pos].pop(0)
76d7967d8f3d run-tests: finish moving tsttest() into TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21313
diff changeset
  1030
21315
56610da39b48 run-tests: make linematch a static method of TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21314
diff changeset
  1031
                r = TTest.linematch(el, lout)
21314
76d7967d8f3d run-tests: finish moving tsttest() into TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21313
diff changeset
  1032
                if isinstance(r, str):
76d7967d8f3d run-tests: finish moving tsttest() into TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21313
diff changeset
  1033
                    if r == '+glob':
76d7967d8f3d run-tests: finish moving tsttest() into TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21313
diff changeset
  1034
                        lout = el[:-1] + ' (glob)\n'
76d7967d8f3d run-tests: finish moving tsttest() into TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21313
diff changeset
  1035
                        r = '' # Warn only this line.
76d7967d8f3d run-tests: finish moving tsttest() into TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21313
diff changeset
  1036
                    elif r == '-glob':
76d7967d8f3d run-tests: finish moving tsttest() into TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21313
diff changeset
  1037
                        lout = ''.join(el.rsplit(' (glob)', 1))
76d7967d8f3d run-tests: finish moving tsttest() into TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21313
diff changeset
  1038
                        r = '' # Warn only this line.
76d7967d8f3d run-tests: finish moving tsttest() into TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21313
diff changeset
  1039
                    else:
76d7967d8f3d run-tests: finish moving tsttest() into TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21313
diff changeset
  1040
                        log('\ninfo, unknown linematch result: %r\n' % r)
76d7967d8f3d run-tests: finish moving tsttest() into TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21313
diff changeset
  1041
                        r = False
76d7967d8f3d run-tests: finish moving tsttest() into TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21313
diff changeset
  1042
                if r:
76d7967d8f3d run-tests: finish moving tsttest() into TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21313
diff changeset
  1043
                    postout.append('  ' + el)
76d7967d8f3d run-tests: finish moving tsttest() into TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21313
diff changeset
  1044
                else:
21384
a36cc85a5b7b run-tests: move string escaping to TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21383
diff changeset
  1045
                    if self.NEEDESCAPE(lout):
21538
05925bb5f95a run-tests: fix invalid reference to stringescape after a36cc85a5b7b
Mads Kiilerich <madski@unity3d.com>
parents: 21536
diff changeset
  1046
                        lout = TTest._stringescape('%s (esc)\n' %
05925bb5f95a run-tests: fix invalid reference to stringescape after a36cc85a5b7b
Mads Kiilerich <madski@unity3d.com>
parents: 21536
diff changeset
  1047
                                                   lout.rstrip('\n'))
21314
76d7967d8f3d run-tests: finish moving tsttest() into TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21313
diff changeset
  1048
                    postout.append('  ' + lout) # Let diff deal with it.
76d7967d8f3d run-tests: finish moving tsttest() into TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21313
diff changeset
  1049
                    if r != '': # If line failed.
76d7967d8f3d run-tests: finish moving tsttest() into TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21313
diff changeset
  1050
                        warnonly = 3 # for sure not
76d7967d8f3d run-tests: finish moving tsttest() into TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21313
diff changeset
  1051
                    elif warnonly == 1: # Is "not yet" and line is warn only.
76d7967d8f3d run-tests: finish moving tsttest() into TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21313
diff changeset
  1052
                        warnonly = 2 # Yes do warn.
76d7967d8f3d run-tests: finish moving tsttest() into TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21313
diff changeset
  1053
76d7967d8f3d run-tests: finish moving tsttest() into TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21313
diff changeset
  1054
            if lcmd:
76d7967d8f3d run-tests: finish moving tsttest() into TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21313
diff changeset
  1055
                # Add on last return code.
76d7967d8f3d run-tests: finish moving tsttest() into TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21313
diff changeset
  1056
                ret = int(lcmd.split()[1])
76d7967d8f3d run-tests: finish moving tsttest() into TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21313
diff changeset
  1057
                if ret != 0:
76d7967d8f3d run-tests: finish moving tsttest() into TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21313
diff changeset
  1058
                    postout.append('  [%s]\n' % ret)
76d7967d8f3d run-tests: finish moving tsttest() into TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21313
diff changeset
  1059
                if pos in after:
76d7967d8f3d run-tests: finish moving tsttest() into TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21313
diff changeset
  1060
                    # Merge in non-active test bits.
76d7967d8f3d run-tests: finish moving tsttest() into TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21313
diff changeset
  1061
                    postout += after.pop(pos)
76d7967d8f3d run-tests: finish moving tsttest() into TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21313
diff changeset
  1062
                pos = int(lcmd.split()[0])
76d7967d8f3d run-tests: finish moving tsttest() into TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21313
diff changeset
  1063
76d7967d8f3d run-tests: finish moving tsttest() into TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21313
diff changeset
  1064
        if pos in after:
76d7967d8f3d run-tests: finish moving tsttest() into TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21313
diff changeset
  1065
            postout += after.pop(pos)
76d7967d8f3d run-tests: finish moving tsttest() into TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21313
diff changeset
  1066
76d7967d8f3d run-tests: finish moving tsttest() into TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21313
diff changeset
  1067
        if warnonly == 2:
76d7967d8f3d run-tests: finish moving tsttest() into TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21313
diff changeset
  1068
            exitcode = False # Set exitcode to warned.
76d7967d8f3d run-tests: finish moving tsttest() into TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21313
diff changeset
  1069
76d7967d8f3d run-tests: finish moving tsttest() into TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21313
diff changeset
  1070
        return exitcode, postout
21312
986b8a58a6d3 run-tests: move t test parsing into its own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21311
diff changeset
  1071
21315
56610da39b48 run-tests: make linematch a static method of TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21314
diff changeset
  1072
    @staticmethod
21316
ab9bf8a5e573 run-tests: make rematch a static method of TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21315
diff changeset
  1073
    def rematch(el, l):
ab9bf8a5e573 run-tests: make rematch a static method of TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21315
diff changeset
  1074
        try:
ab9bf8a5e573 run-tests: make rematch a static method of TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21315
diff changeset
  1075
            # use \Z to ensure that the regex matches to the end of the string
ab9bf8a5e573 run-tests: make rematch a static method of TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21315
diff changeset
  1076
            if os.name == 'nt':
ab9bf8a5e573 run-tests: make rematch a static method of TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21315
diff changeset
  1077
                return re.match(el + r'\r?\n\Z', l)
ab9bf8a5e573 run-tests: make rematch a static method of TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21315
diff changeset
  1078
            return re.match(el + r'\n\Z', l)
ab9bf8a5e573 run-tests: make rematch a static method of TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21315
diff changeset
  1079
        except re.error:
ab9bf8a5e573 run-tests: make rematch a static method of TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21315
diff changeset
  1080
            # el is an invalid regex
ab9bf8a5e573 run-tests: make rematch a static method of TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21315
diff changeset
  1081
            return False
ab9bf8a5e573 run-tests: make rematch a static method of TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21315
diff changeset
  1082
ab9bf8a5e573 run-tests: make rematch a static method of TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21315
diff changeset
  1083
    @staticmethod
21317
58a599784a0c run-tests: make globmatch a static method of TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21316
diff changeset
  1084
    def globmatch(el, l):
58a599784a0c run-tests: make globmatch a static method of TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21316
diff changeset
  1085
        # The only supported special characters are * and ? plus / which also
58a599784a0c run-tests: make globmatch a static method of TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21316
diff changeset
  1086
        # matches \ on windows. Escaping of these characters is supported.
58a599784a0c run-tests: make globmatch a static method of TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21316
diff changeset
  1087
        if el + '\n' == l:
58a599784a0c run-tests: make globmatch a static method of TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21316
diff changeset
  1088
            if os.altsep:
58a599784a0c run-tests: make globmatch a static method of TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21316
diff changeset
  1089
                # matching on "/" is not needed for this line
23352
5bd04faaa3ee run-tests: don't warn on unnecessary globs mandated by check-code.py
Matt Harbison <matt_harbison@yahoo.com>
parents: 23347
diff changeset
  1090
                for pat in checkcodeglobpats:
5bd04faaa3ee run-tests: don't warn on unnecessary globs mandated by check-code.py
Matt Harbison <matt_harbison@yahoo.com>
parents: 23347
diff changeset
  1091
                    if pat.match(el):
5bd04faaa3ee run-tests: don't warn on unnecessary globs mandated by check-code.py
Matt Harbison <matt_harbison@yahoo.com>
parents: 23347
diff changeset
  1092
                        return True
21317
58a599784a0c run-tests: make globmatch a static method of TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21316
diff changeset
  1093
                return '-glob'
58a599784a0c run-tests: make globmatch a static method of TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21316
diff changeset
  1094
            return True
58a599784a0c run-tests: make globmatch a static method of TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21316
diff changeset
  1095
        i, n = 0, len(el)
58a599784a0c run-tests: make globmatch a static method of TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21316
diff changeset
  1096
        res = ''
58a599784a0c run-tests: make globmatch a static method of TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21316
diff changeset
  1097
        while i < n:
58a599784a0c run-tests: make globmatch a static method of TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21316
diff changeset
  1098
            c = el[i]
58a599784a0c run-tests: make globmatch a static method of TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21316
diff changeset
  1099
            i += 1
24811
a2dcf460e141 run-tests: don't error when glob matched line ends with backslash
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24751
diff changeset
  1100
            if c == '\\' and i < n and el[i] in '*?\\/':
21317
58a599784a0c run-tests: make globmatch a static method of TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21316
diff changeset
  1101
                res += el[i - 1:i + 1]
58a599784a0c run-tests: make globmatch a static method of TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21316
diff changeset
  1102
                i += 1
58a599784a0c run-tests: make globmatch a static method of TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21316
diff changeset
  1103
            elif c == '*':
58a599784a0c run-tests: make globmatch a static method of TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21316
diff changeset
  1104
                res += '.*'
58a599784a0c run-tests: make globmatch a static method of TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21316
diff changeset
  1105
            elif c == '?':
58a599784a0c run-tests: make globmatch a static method of TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21316
diff changeset
  1106
                res += '.'
58a599784a0c run-tests: make globmatch a static method of TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21316
diff changeset
  1107
            elif c == '/' and os.altsep:
58a599784a0c run-tests: make globmatch a static method of TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21316
diff changeset
  1108
                res += '[/\\\\]'
58a599784a0c run-tests: make globmatch a static method of TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21316
diff changeset
  1109
            else:
58a599784a0c run-tests: make globmatch a static method of TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21316
diff changeset
  1110
                res += re.escape(c)
58a599784a0c run-tests: make globmatch a static method of TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21316
diff changeset
  1111
        return TTest.rematch(res, l)
58a599784a0c run-tests: make globmatch a static method of TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21316
diff changeset
  1112
58a599784a0c run-tests: make globmatch a static method of TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21316
diff changeset
  1113
    @staticmethod
21315
56610da39b48 run-tests: make linematch a static method of TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21314
diff changeset
  1114
    def linematch(el, l):
56610da39b48 run-tests: make linematch a static method of TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21314
diff changeset
  1115
        if el == l: # perfect match (fast)
56610da39b48 run-tests: make linematch a static method of TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21314
diff changeset
  1116
            return True
56610da39b48 run-tests: make linematch a static method of TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21314
diff changeset
  1117
        if el:
56610da39b48 run-tests: make linematch a static method of TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21314
diff changeset
  1118
            if el.endswith(" (esc)\n"):
56610da39b48 run-tests: make linematch a static method of TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21314
diff changeset
  1119
                el = el[:-7].decode('string-escape') + '\n'
56610da39b48 run-tests: make linematch a static method of TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21314
diff changeset
  1120
            if el == l or os.name == 'nt' and el[:-1] + '\r\n' == l:
56610da39b48 run-tests: make linematch a static method of TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21314
diff changeset
  1121
                return True
56610da39b48 run-tests: make linematch a static method of TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21314
diff changeset
  1122
            if el.endswith(" (re)\n"):
21316
ab9bf8a5e573 run-tests: make rematch a static method of TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21315
diff changeset
  1123
                return TTest.rematch(el[:-6], l)
21315
56610da39b48 run-tests: make linematch a static method of TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21314
diff changeset
  1124
            if el.endswith(" (glob)\n"):
23728
31d3f973d079 run-tests: automatically add (glob) to "saved backup bundle to" lines
Mads Kiilerich <madski@unity3d.com>
parents: 23388
diff changeset
  1125
                # ignore '(glob)' added to l by 'replacements'
31d3f973d079 run-tests: automatically add (glob) to "saved backup bundle to" lines
Mads Kiilerich <madski@unity3d.com>
parents: 23388
diff changeset
  1126
                if l.endswith(" (glob)\n"):
31d3f973d079 run-tests: automatically add (glob) to "saved backup bundle to" lines
Mads Kiilerich <madski@unity3d.com>
parents: 23388
diff changeset
  1127
                    l = l[:-8] + "\n"
21317
58a599784a0c run-tests: make globmatch a static method of TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21316
diff changeset
  1128
                return TTest.globmatch(el[:-8], l)
21315
56610da39b48 run-tests: make linematch a static method of TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21314
diff changeset
  1129
            if os.altsep and l.replace('\\', '/') == el:
56610da39b48 run-tests: make linematch a static method of TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21314
diff changeset
  1130
                return '+glob'
56610da39b48 run-tests: make linematch a static method of TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21314
diff changeset
  1131
        return False
56610da39b48 run-tests: make linematch a static method of TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21314
diff changeset
  1132
21379
ab1a95270a50 run-tests: move parsehghaveoutput() into TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21378
diff changeset
  1133
    @staticmethod
ab1a95270a50 run-tests: move parsehghaveoutput() into TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21378
diff changeset
  1134
    def parsehghaveoutput(lines):
ab1a95270a50 run-tests: move parsehghaveoutput() into TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21378
diff changeset
  1135
        '''Parse hghave log lines.
ab1a95270a50 run-tests: move parsehghaveoutput() into TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21378
diff changeset
  1136
ab1a95270a50 run-tests: move parsehghaveoutput() into TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21378
diff changeset
  1137
        Return tuple of lists (missing, failed):
ab1a95270a50 run-tests: move parsehghaveoutput() into TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21378
diff changeset
  1138
          * the missing/unknown features
ab1a95270a50 run-tests: move parsehghaveoutput() into TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21378
diff changeset
  1139
          * the features for which existence check failed'''
ab1a95270a50 run-tests: move parsehghaveoutput() into TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21378
diff changeset
  1140
        missing = []
ab1a95270a50 run-tests: move parsehghaveoutput() into TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21378
diff changeset
  1141
        failed = []
ab1a95270a50 run-tests: move parsehghaveoutput() into TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21378
diff changeset
  1142
        for line in lines:
21381
9aa5784992d4 run-tests: move SKIPPED_PREFIX and FAILED_PREFIX into TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21380
diff changeset
  1143
            if line.startswith(TTest.SKIPPED_PREFIX):
21379
ab1a95270a50 run-tests: move parsehghaveoutput() into TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21378
diff changeset
  1144
                line = line.splitlines()[0]
21381
9aa5784992d4 run-tests: move SKIPPED_PREFIX and FAILED_PREFIX into TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21380
diff changeset
  1145
                missing.append(line[len(TTest.SKIPPED_PREFIX):])
9aa5784992d4 run-tests: move SKIPPED_PREFIX and FAILED_PREFIX into TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21380
diff changeset
  1146
            elif line.startswith(TTest.FAILED_PREFIX):
21379
ab1a95270a50 run-tests: move parsehghaveoutput() into TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21378
diff changeset
  1147
                line = line.splitlines()[0]
21381
9aa5784992d4 run-tests: move SKIPPED_PREFIX and FAILED_PREFIX into TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21380
diff changeset
  1148
                failed.append(line[len(TTest.FAILED_PREFIX):])
21379
ab1a95270a50 run-tests: move parsehghaveoutput() into TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21378
diff changeset
  1149
ab1a95270a50 run-tests: move parsehghaveoutput() into TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21378
diff changeset
  1150
        return missing, failed
ab1a95270a50 run-tests: move parsehghaveoutput() into TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21378
diff changeset
  1151
21384
a36cc85a5b7b run-tests: move string escaping to TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21383
diff changeset
  1152
    @staticmethod
a36cc85a5b7b run-tests: move string escaping to TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21383
diff changeset
  1153
    def _escapef(m):
a36cc85a5b7b run-tests: move string escaping to TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21383
diff changeset
  1154
        return TTest.ESCAPEMAP[m.group(0)]
a36cc85a5b7b run-tests: move string escaping to TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21383
diff changeset
  1155
a36cc85a5b7b run-tests: move string escaping to TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21383
diff changeset
  1156
    @staticmethod
a36cc85a5b7b run-tests: move string escaping to TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21383
diff changeset
  1157
    def _stringescape(s):
a36cc85a5b7b run-tests: move string escaping to TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21383
diff changeset
  1158
        return TTest.ESCAPESUB(TTest._escapef, s)
a36cc85a5b7b run-tests: move string escaping to TTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21383
diff changeset
  1159
22104
70bdf59d27b6 run-tests: attempt to fix iolock handling
Matt Mackall <mpm@selenic.com>
parents: 22045
diff changeset
  1160
iolock = threading.RLock()
14000
636a6f5aa2cd run-tests: add locking on results struct
Matt Mackall <mpm@selenic.com>
parents: 13999
diff changeset
  1161
21442
867a1116be3c run-tests: record skips by raising SkipTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21441
diff changeset
  1162
class SkipTest(Exception):
867a1116be3c run-tests: record skips by raising SkipTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21441
diff changeset
  1163
    """Raised to indicate that a test is to be skipped."""
867a1116be3c run-tests: record skips by raising SkipTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21441
diff changeset
  1164
21443
a6845a042d46 run-tests: record ignored tests by raising IgnoreTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21442
diff changeset
  1165
class IgnoreTest(Exception):
a6845a042d46 run-tests: record ignored tests by raising IgnoreTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21442
diff changeset
  1166
    """Raised to indicate that a test is to be ignored."""
a6845a042d46 run-tests: record ignored tests by raising IgnoreTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21442
diff changeset
  1167
21444
2b7d364690d8 run-tests: record warnings by raising WarnTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21443
diff changeset
  1168
class WarnTest(Exception):
2b7d364690d8 run-tests: record warnings by raising WarnTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21443
diff changeset
  1169
    """Raised to indicate that a test warned."""
2b7d364690d8 run-tests: record warnings by raising WarnTest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21443
diff changeset
  1170
21429
203ed3cf6c81 run-tests: define custom result and runner classes for unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21428
diff changeset
  1171
class TestResult(unittest._TextTestResult):
203ed3cf6c81 run-tests: define custom result and runner classes for unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21428
diff changeset
  1172
    """Holds results when executing via unittest."""
203ed3cf6c81 run-tests: define custom result and runner classes for unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21428
diff changeset
  1173
    # Don't worry too much about accessing the non-public _TextTestResult.
203ed3cf6c81 run-tests: define custom result and runner classes for unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21428
diff changeset
  1174
    # It is relatively common in Python testing tools.
21460
df580990507e run-tests: abort tests after first failure in unittest mode
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21459
diff changeset
  1175
    def __init__(self, options, *args, **kwargs):
21429
203ed3cf6c81 run-tests: define custom result and runner classes for unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21428
diff changeset
  1176
        super(TestResult, self).__init__(*args, **kwargs)
203ed3cf6c81 run-tests: define custom result and runner classes for unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21428
diff changeset
  1177
21460
df580990507e run-tests: abort tests after first failure in unittest mode
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21459
diff changeset
  1178
        self._options = options
df580990507e run-tests: abort tests after first failure in unittest mode
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21459
diff changeset
  1179
21430
cf2992656bf8 run-tests: teach unittest about skipped tests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21429
diff changeset
  1180
        # unittest.TestResult didn't have skipped until 2.7. We need to
cf2992656bf8 run-tests: teach unittest about skipped tests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21429
diff changeset
  1181
        # polyfill it.
cf2992656bf8 run-tests: teach unittest about skipped tests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21429
diff changeset
  1182
        self.skipped = []
cf2992656bf8 run-tests: teach unittest about skipped tests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21429
diff changeset
  1183
21431
0f12bc8aed80 run-tests: teach unittest about ignored tests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21430
diff changeset
  1184
        # We have a custom "ignored" result that isn't present in any Python
0f12bc8aed80 run-tests: teach unittest about ignored tests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21430
diff changeset
  1185
        # unittest implementation. It is very similar to skipped. It may make
0f12bc8aed80 run-tests: teach unittest about ignored tests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21430
diff changeset
  1186
        # sense to map it into skip some day.
0f12bc8aed80 run-tests: teach unittest about ignored tests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21430
diff changeset
  1187
        self.ignored = []
0f12bc8aed80 run-tests: teach unittest about ignored tests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21430
diff changeset
  1188
21433
ff4a270bd334 run-tests: teach unittest about warned results
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21432
diff changeset
  1189
        # We have a custom "warned" result that isn't present in any Python
ff4a270bd334 run-tests: teach unittest about warned results
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21432
diff changeset
  1190
        # unittest implementation. It is very similar to failed. It may make
ff4a270bd334 run-tests: teach unittest about warned results
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21432
diff changeset
  1191
        # sense to map it into fail some day.
ff4a270bd334 run-tests: teach unittest about warned results
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21432
diff changeset
  1192
        self.warned = []
ff4a270bd334 run-tests: teach unittest about warned results
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21432
diff changeset
  1193
21495
b162bdc78bef run-tests: capture execution times in TestResult
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21494
diff changeset
  1194
        self.times = []
22044
a06172e85fd4 run-tests: add support for xunit test reports
Augie Fackler <raf@durin42.com>
parents: 21997
diff changeset
  1195
        # Data stored for the benefit of generating xunit reports.
a06172e85fd4 run-tests: add support for xunit test reports
Augie Fackler <raf@durin42.com>
parents: 21997
diff changeset
  1196
        self.successes = []
a06172e85fd4 run-tests: add support for xunit test reports
Augie Fackler <raf@durin42.com>
parents: 21997
diff changeset
  1197
        self.faildata = {}
21495
b162bdc78bef run-tests: capture execution times in TestResult
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21494
diff changeset
  1198
21462
8a4ef661f08d run-tests: make failure reporting in unittest mode equivalent to default
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21461
diff changeset
  1199
    def addFailure(self, test, reason):
8a4ef661f08d run-tests: make failure reporting in unittest mode equivalent to default
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21461
diff changeset
  1200
        self.failures.append((test, reason))
21460
df580990507e run-tests: abort tests after first failure in unittest mode
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21459
diff changeset
  1201
df580990507e run-tests: abort tests after first failure in unittest mode
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21459
diff changeset
  1202
        if self._options.first:
df580990507e run-tests: abort tests after first failure in unittest mode
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21459
diff changeset
  1203
            self.stop()
21735
5ee547fdb0be run-tests: produce error on running a failing test
anuraggoel <anurag.dsps@gmail.com>
parents: 21733
diff changeset
  1204
        else:
21993
284a8c9f74f3 run-tests: add iolock to failure output
Matt Mackall <mpm@selenic.com>
parents: 21977
diff changeset
  1205
            iolock.acquire()
21735
5ee547fdb0be run-tests: produce error on running a failing test
anuraggoel <anurag.dsps@gmail.com>
parents: 21733
diff changeset
  1206
            if not self._options.nodiff:
5ee547fdb0be run-tests: produce error on running a failing test
anuraggoel <anurag.dsps@gmail.com>
parents: 21733
diff changeset
  1207
                self.stream.write('\nERROR: %s output changed\n' % test)
21754
7e14d026c4c4 run-tests: fixes the '--interactive' option error
anuraggoel <anurag.dsps@gmail.com>
parents: 21753
diff changeset
  1208
21736
07eb76186b41 run-tests: produce '!' mark after running a failing test
anuraggoel <anurag.dsps@gmail.com>
parents: 21735
diff changeset
  1209
            self.stream.write('!')
22146
58b5196cce20 run-tests: fix some io ordering
Matt Mackall <mpm@selenic.com>
parents: 22122
diff changeset
  1210
            self.stream.flush()
21993
284a8c9f74f3 run-tests: add iolock to failure output
Matt Mackall <mpm@selenic.com>
parents: 21977
diff changeset
  1211
            iolock.release()
21460
df580990507e run-tests: abort tests after first failure in unittest mode
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21459
diff changeset
  1212
22044
a06172e85fd4 run-tests: add support for xunit test reports
Augie Fackler <raf@durin42.com>
parents: 21997
diff changeset
  1213
    def addSuccess(self, test):
22104
70bdf59d27b6 run-tests: attempt to fix iolock handling
Matt Mackall <mpm@selenic.com>
parents: 22045
diff changeset
  1214
        iolock.acquire()
22044
a06172e85fd4 run-tests: add support for xunit test reports
Augie Fackler <raf@durin42.com>
parents: 21997
diff changeset
  1215
        super(TestResult, self).addSuccess(test)
22104
70bdf59d27b6 run-tests: attempt to fix iolock handling
Matt Mackall <mpm@selenic.com>
parents: 22045
diff changeset
  1216
        iolock.release()
22044
a06172e85fd4 run-tests: add support for xunit test reports
Augie Fackler <raf@durin42.com>
parents: 21997
diff changeset
  1217
        self.successes.append(test)
21460
df580990507e run-tests: abort tests after first failure in unittest mode
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21459
diff changeset
  1218
22044
a06172e85fd4 run-tests: add support for xunit test reports
Augie Fackler <raf@durin42.com>
parents: 21997
diff changeset
  1219
    def addError(self, test, err):
a06172e85fd4 run-tests: add support for xunit test reports
Augie Fackler <raf@durin42.com>
parents: 21997
diff changeset
  1220
        super(TestResult, self).addError(test, err)
21460
df580990507e run-tests: abort tests after first failure in unittest mode
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21459
diff changeset
  1221
        if self._options.first:
df580990507e run-tests: abort tests after first failure in unittest mode
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21459
diff changeset
  1222
            self.stop()
df580990507e run-tests: abort tests after first failure in unittest mode
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21459
diff changeset
  1223
21430
cf2992656bf8 run-tests: teach unittest about skipped tests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21429
diff changeset
  1224
    # Polyfill.
cf2992656bf8 run-tests: teach unittest about skipped tests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21429
diff changeset
  1225
    def addSkip(self, test, reason):
cf2992656bf8 run-tests: teach unittest about skipped tests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21429
diff changeset
  1226
        self.skipped.append((test, reason))
22104
70bdf59d27b6 run-tests: attempt to fix iolock handling
Matt Mackall <mpm@selenic.com>
parents: 22045
diff changeset
  1227
        iolock.acquire()
21430
cf2992656bf8 run-tests: teach unittest about skipped tests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21429
diff changeset
  1228
        if self.showAll:
cf2992656bf8 run-tests: teach unittest about skipped tests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21429
diff changeset
  1229
            self.stream.writeln('skipped %s' % reason)
cf2992656bf8 run-tests: teach unittest about skipped tests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21429
diff changeset
  1230
        else:
cf2992656bf8 run-tests: teach unittest about skipped tests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21429
diff changeset
  1231
            self.stream.write('s')
cf2992656bf8 run-tests: teach unittest about skipped tests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21429
diff changeset
  1232
            self.stream.flush()
22104
70bdf59d27b6 run-tests: attempt to fix iolock handling
Matt Mackall <mpm@selenic.com>
parents: 22045
diff changeset
  1233
        iolock.release()
21430
cf2992656bf8 run-tests: teach unittest about skipped tests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21429
diff changeset
  1234
21431
0f12bc8aed80 run-tests: teach unittest about ignored tests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21430
diff changeset
  1235
    def addIgnore(self, test, reason):
0f12bc8aed80 run-tests: teach unittest about ignored tests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21430
diff changeset
  1236
        self.ignored.append((test, reason))
22104
70bdf59d27b6 run-tests: attempt to fix iolock handling
Matt Mackall <mpm@selenic.com>
parents: 22045
diff changeset
  1237
        iolock.acquire()
21431
0f12bc8aed80 run-tests: teach unittest about ignored tests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21430
diff changeset
  1238
        if self.showAll:
0f12bc8aed80 run-tests: teach unittest about ignored tests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21430
diff changeset
  1239
            self.stream.writeln('ignored %s' % reason)
0f12bc8aed80 run-tests: teach unittest about ignored tests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21430
diff changeset
  1240
        else:
22107
3b5cf39ffcc4 run-tests: don't show 'i' for tests that don't match a keyword
Matt Mackall <mpm@selenic.com>
parents: 22104
diff changeset
  1241
            if reason != 'not retesting' and reason != "doesn't match keyword":
21739
11e5ad038a7b run-tests: skipped test should not produce 'i' mark while retesting
anuraggoel <anurag.dsps@gmail.com>
parents: 21737
diff changeset
  1242
                self.stream.write('i')
21997
93c3b3f55d59 run-tests: fix test result counts with --keyword specified or skips occurring
Augie Fackler <raf@durin42.com>
parents: 21993
diff changeset
  1243
            else:
93c3b3f55d59 run-tests: fix test result counts with --keyword specified or skips occurring
Augie Fackler <raf@durin42.com>
parents: 21993
diff changeset
  1244
                self.testsRun += 1
21431
0f12bc8aed80 run-tests: teach unittest about ignored tests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21430
diff changeset
  1245
            self.stream.flush()
22104
70bdf59d27b6 run-tests: attempt to fix iolock handling
Matt Mackall <mpm@selenic.com>
parents: 22045
diff changeset
  1246
        iolock.release()
21431
0f12bc8aed80 run-tests: teach unittest about ignored tests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21430
diff changeset
  1247
21433
ff4a270bd334 run-tests: teach unittest about warned results
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21432
diff changeset
  1248
    def addWarn(self, test, reason):
ff4a270bd334 run-tests: teach unittest about warned results
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21432
diff changeset
  1249
        self.warned.append((test, reason))
ff4a270bd334 run-tests: teach unittest about warned results
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21432
diff changeset
  1250
21460
df580990507e run-tests: abort tests after first failure in unittest mode
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21459
diff changeset
  1251
        if self._options.first:
df580990507e run-tests: abort tests after first failure in unittest mode
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21459
diff changeset
  1252
            self.stop()
df580990507e run-tests: abort tests after first failure in unittest mode
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21459
diff changeset
  1253
22104
70bdf59d27b6 run-tests: attempt to fix iolock handling
Matt Mackall <mpm@selenic.com>
parents: 22045
diff changeset
  1254
        iolock.acquire()
21433
ff4a270bd334 run-tests: teach unittest about warned results
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21432
diff changeset
  1255
        if self.showAll:
ff4a270bd334 run-tests: teach unittest about warned results
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21432
diff changeset
  1256
            self.stream.writeln('warned %s' % reason)
ff4a270bd334 run-tests: teach unittest about warned results
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21432
diff changeset
  1257
        else:
ff4a270bd334 run-tests: teach unittest about warned results
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21432
diff changeset
  1258
            self.stream.write('~')
ff4a270bd334 run-tests: teach unittest about warned results
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21432
diff changeset
  1259
            self.stream.flush()
22104
70bdf59d27b6 run-tests: attempt to fix iolock handling
Matt Mackall <mpm@selenic.com>
parents: 22045
diff changeset
  1260
        iolock.release()
21433
ff4a270bd334 run-tests: teach unittest about warned results
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21432
diff changeset
  1261
21523
9fb6f328576a run-tests: move interactive test acceptance into TestResult
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21522
diff changeset
  1262
    def addOutputMismatch(self, test, ret, got, expected):
21521
855f092c96e5 run-tests: move diff generation into TestResult
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21520
diff changeset
  1263
        """Record a mismatch in test output for a particular test."""
22838
9a20f53e436f run-tests: handle --jobs and --first gracefully
Augie Fackler <raf@durin42.com>
parents: 22486
diff changeset
  1264
        if self.shouldStop:
9a20f53e436f run-tests: handle --jobs and --first gracefully
Augie Fackler <raf@durin42.com>
parents: 22486
diff changeset
  1265
            # don't print, some other test case already failed and
9a20f53e436f run-tests: handle --jobs and --first gracefully
Augie Fackler <raf@durin42.com>
parents: 22486
diff changeset
  1266
            # printed, we're just stale and probably failed due to our
9a20f53e436f run-tests: handle --jobs and --first gracefully
Augie Fackler <raf@durin42.com>
parents: 22486
diff changeset
  1267
            # temp dir getting cleaned up.
9a20f53e436f run-tests: handle --jobs and --first gracefully
Augie Fackler <raf@durin42.com>
parents: 22486
diff changeset
  1268
            return
21521
855f092c96e5 run-tests: move diff generation into TestResult
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21520
diff changeset
  1269
21763
84cd5ee787ed run-tests: hold iolock across diff/prompt when interactive
Matt Mackall <mpm@selenic.com>
parents: 21754
diff changeset
  1270
        accepted = False
22044
a06172e85fd4 run-tests: add support for xunit test reports
Augie Fackler <raf@durin42.com>
parents: 21997
diff changeset
  1271
        failed = False
a06172e85fd4 run-tests: add support for xunit test reports
Augie Fackler <raf@durin42.com>
parents: 21997
diff changeset
  1272
        lines = []
21763
84cd5ee787ed run-tests: hold iolock across diff/prompt when interactive
Matt Mackall <mpm@selenic.com>
parents: 21754
diff changeset
  1273
84cd5ee787ed run-tests: hold iolock across diff/prompt when interactive
Matt Mackall <mpm@selenic.com>
parents: 21754
diff changeset
  1274
        iolock.acquire()
21521
855f092c96e5 run-tests: move diff generation into TestResult
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21520
diff changeset
  1275
        if self._options.nodiff:
21763
84cd5ee787ed run-tests: hold iolock across diff/prompt when interactive
Matt Mackall <mpm@selenic.com>
parents: 21754
diff changeset
  1276
            pass
84cd5ee787ed run-tests: hold iolock across diff/prompt when interactive
Matt Mackall <mpm@selenic.com>
parents: 21754
diff changeset
  1277
        elif self._options.view:
21919
c350cff58444 run-tests: make --view work again
Matt Mackall <mpm@selenic.com>
parents: 21918
diff changeset
  1278
            os.system("%s %s %s" %
c350cff58444 run-tests: make --view work again
Matt Mackall <mpm@selenic.com>
parents: 21918
diff changeset
  1279
                      (self._options.view, test.refpath, test.errpath))
21521
855f092c96e5 run-tests: move diff generation into TestResult
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21520
diff changeset
  1280
        else:
22839
9f0f50c63e16 run-tests: more accurate/helpful message than "diff generation failed"
Kyle Lippincott <spectral@google.com>
parents: 22838
diff changeset
  1281
            servefail, lines = getdiff(expected, got,
9f0f50c63e16 run-tests: more accurate/helpful message than "diff generation failed"
Kyle Lippincott <spectral@google.com>
parents: 22838
diff changeset
  1282
                                       test.refpath, test.errpath)
9f0f50c63e16 run-tests: more accurate/helpful message than "diff generation failed"
Kyle Lippincott <spectral@google.com>
parents: 22838
diff changeset
  1283
            if servefail:
9f0f50c63e16 run-tests: more accurate/helpful message than "diff generation failed"
Kyle Lippincott <spectral@google.com>
parents: 22838
diff changeset
  1284
                self.addFailure(
9f0f50c63e16 run-tests: more accurate/helpful message than "diff generation failed"
Kyle Lippincott <spectral@google.com>
parents: 22838
diff changeset
  1285
                    test,
9f0f50c63e16 run-tests: more accurate/helpful message than "diff generation failed"
Kyle Lippincott <spectral@google.com>
parents: 22838
diff changeset
  1286
                    'server failed to start (HGPORT=%s)' % test._startport)
21521
855f092c96e5 run-tests: move diff generation into TestResult
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21520
diff changeset
  1287
            else:
855f092c96e5 run-tests: move diff generation into TestResult
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21520
diff changeset
  1288
                self.stream.write('\n')
855f092c96e5 run-tests: move diff generation into TestResult
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21520
diff changeset
  1289
                for line in lines:
855f092c96e5 run-tests: move diff generation into TestResult
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21520
diff changeset
  1290
                    self.stream.write(line)
855f092c96e5 run-tests: move diff generation into TestResult
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21520
diff changeset
  1291
                self.stream.flush()
855f092c96e5 run-tests: move diff generation into TestResult
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21520
diff changeset
  1292
22361
eb6adf750954 run-tests: make --interactive work with --view
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22292
diff changeset
  1293
        # handle interactive prompt without releasing iolock
eb6adf750954 run-tests: make --interactive work with --view
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22292
diff changeset
  1294
        if self._options.interactive:
eb6adf750954 run-tests: make --interactive work with --view
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22292
diff changeset
  1295
            self.stream.write('Accept this change? [n] ')
eb6adf750954 run-tests: make --interactive work with --view
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22292
diff changeset
  1296
            answer = sys.stdin.readline().strip()
eb6adf750954 run-tests: make --interactive work with --view
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22292
diff changeset
  1297
            if answer.lower() in ('y', 'yes'):
eb6adf750954 run-tests: make --interactive work with --view
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22292
diff changeset
  1298
                if test.name.endswith('.t'):
eb6adf750954 run-tests: make --interactive work with --view
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22292
diff changeset
  1299
                    rename(test.errpath, test.path)
eb6adf750954 run-tests: make --interactive work with --view
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22292
diff changeset
  1300
                else:
eb6adf750954 run-tests: make --interactive work with --view
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22292
diff changeset
  1301
                    rename(test.errpath, '%s.out' % test.path)
eb6adf750954 run-tests: make --interactive work with --view
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22292
diff changeset
  1302
                accepted = True
eb6adf750954 run-tests: make --interactive work with --view
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22292
diff changeset
  1303
        if not accepted and not failed:
eb6adf750954 run-tests: make --interactive work with --view
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22292
diff changeset
  1304
            self.faildata[test.name] = ''.join(lines)
21763
84cd5ee787ed run-tests: hold iolock across diff/prompt when interactive
Matt Mackall <mpm@selenic.com>
parents: 21754
diff changeset
  1305
        iolock.release()
84cd5ee787ed run-tests: hold iolock across diff/prompt when interactive
Matt Mackall <mpm@selenic.com>
parents: 21754
diff changeset
  1306
84cd5ee787ed run-tests: hold iolock across diff/prompt when interactive
Matt Mackall <mpm@selenic.com>
parents: 21754
diff changeset
  1307
        return accepted
21523
9fb6f328576a run-tests: move interactive test acceptance into TestResult
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21522
diff changeset
  1308
21495
b162bdc78bef run-tests: capture execution times in TestResult
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21494
diff changeset
  1309
    def startTest(self, test):
b162bdc78bef run-tests: capture execution times in TestResult
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21494
diff changeset
  1310
        super(TestResult, self).startTest(test)
b162bdc78bef run-tests: capture execution times in TestResult
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21494
diff changeset
  1311
21977
4ca4e1572022 run-tests: '--time' option provide more details to Linux users
anuraggoel <anurag.dsps@gmail.com>
parents: 21919
diff changeset
  1312
        # os.times module computes the user time and system time spent by
4ca4e1572022 run-tests: '--time' option provide more details to Linux users
anuraggoel <anurag.dsps@gmail.com>
parents: 21919
diff changeset
  1313
        # child's processes along with real elapsed time taken by a process.
4ca4e1572022 run-tests: '--time' option provide more details to Linux users
anuraggoel <anurag.dsps@gmail.com>
parents: 21919
diff changeset
  1314
        # This module has one limitation. It can only work for Linux user
4ca4e1572022 run-tests: '--time' option provide more details to Linux users
anuraggoel <anurag.dsps@gmail.com>
parents: 21919
diff changeset
  1315
        # and not for Windows.
24331
d3bdd8c7174f run-tests: stop storing start/stop times in a dict by test name
Augie Fackler <augie@google.com>
parents: 24330
diff changeset
  1316
        test.started = os.times()
21495
b162bdc78bef run-tests: capture execution times in TestResult
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21494
diff changeset
  1317
b162bdc78bef run-tests: capture execution times in TestResult
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21494
diff changeset
  1318
    def stopTest(self, test, interrupted=False):
b162bdc78bef run-tests: capture execution times in TestResult
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21494
diff changeset
  1319
        super(TestResult, self).stopTest(test)
b162bdc78bef run-tests: capture execution times in TestResult
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21494
diff changeset
  1320
24331
d3bdd8c7174f run-tests: stop storing start/stop times in a dict by test name
Augie Fackler <augie@google.com>
parents: 24330
diff changeset
  1321
        test.stopped = os.times()
21977
4ca4e1572022 run-tests: '--time' option provide more details to Linux users
anuraggoel <anurag.dsps@gmail.com>
parents: 21919
diff changeset
  1322
24331
d3bdd8c7174f run-tests: stop storing start/stop times in a dict by test name
Augie Fackler <augie@google.com>
parents: 24330
diff changeset
  1323
        starttime = test.started
d3bdd8c7174f run-tests: stop storing start/stop times in a dict by test name
Augie Fackler <augie@google.com>
parents: 24330
diff changeset
  1324
        endtime = test.stopped
24984
5195322b9f80 run-tests: spread and document the content of time tuple
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24983
diff changeset
  1325
        self.times.append((test.name,
5195322b9f80 run-tests: spread and document the content of time tuple
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24983
diff changeset
  1326
                           endtime[2] - starttime[2], # user space CPU time
5195322b9f80 run-tests: spread and document the content of time tuple
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24983
diff changeset
  1327
                           endtime[3] - starttime[3], # sys  space CPU time
5195322b9f80 run-tests: spread and document the content of time tuple
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24983
diff changeset
  1328
                           endtime[4] - starttime[4], # real time
5195322b9f80 run-tests: spread and document the content of time tuple
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24983
diff changeset
  1329
                           ))
21977
4ca4e1572022 run-tests: '--time' option provide more details to Linux users
anuraggoel <anurag.dsps@gmail.com>
parents: 21919
diff changeset
  1330
21495
b162bdc78bef run-tests: capture execution times in TestResult
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21494
diff changeset
  1331
        if interrupted:
22104
70bdf59d27b6 run-tests: attempt to fix iolock handling
Matt Mackall <mpm@selenic.com>
parents: 22045
diff changeset
  1332
            iolock.acquire()
21495
b162bdc78bef run-tests: capture execution times in TestResult
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21494
diff changeset
  1333
            self.stream.writeln('INTERRUPTED: %s (after %d seconds)' % (
21977
4ca4e1572022 run-tests: '--time' option provide more details to Linux users
anuraggoel <anurag.dsps@gmail.com>
parents: 21919
diff changeset
  1334
                test.name, self.times[-1][3]))
22104
70bdf59d27b6 run-tests: attempt to fix iolock handling
Matt Mackall <mpm@selenic.com>
parents: 22045
diff changeset
  1335
            iolock.release()
21495
b162bdc78bef run-tests: capture execution times in TestResult
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21494
diff changeset
  1336
21439
2e22954b97e3 run-tests: define a custom TestSuite that uses _executetests()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21438
diff changeset
  1337
class TestSuite(unittest.TestSuite):
23139
e53f6b72a0e4 spelling: fixes from proofreading of spell checker issues
Mads Kiilerich <madski@unity3d.com>
parents: 23077
diff changeset
  1338
    """Custom unittest TestSuite that knows how to execute Mercurial tests."""
21528
32b9bbca2052 run-tests: pass jobs into TestSuite constructor
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21523
diff changeset
  1339
21533
aecac8059c00 run-tests: make testdir an argument of TestSuite.__init__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21532
diff changeset
  1340
    def __init__(self, testdir, jobs=1, whitelist=None, blacklist=None,
24329
e7ca4d4b10e1 run-tests: add --runs-per-test flag
Augie Fackler <augie@google.com>
parents: 24306
diff changeset
  1341
                 retest=False, keywords=None, loop=False, runs_per_test=1,
24330
799bc18e14d1 run-tests: avoid running the same test instance concurrently
Augie Fackler <augie@google.com>
parents: 24329
diff changeset
  1342
                 loadtest=None,
21529
117e027390ab run-tests: move whitelist and blacklist to named arguments of TestSuite
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21528
diff changeset
  1343
                 *args, **kwargs):
21528
32b9bbca2052 run-tests: pass jobs into TestSuite constructor
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21523
diff changeset
  1344
        """Create a new instance that can run tests with a configuration.
21439
2e22954b97e3 run-tests: define a custom TestSuite that uses _executetests()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21438
diff changeset
  1345
21533
aecac8059c00 run-tests: make testdir an argument of TestSuite.__init__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21532
diff changeset
  1346
        testdir specifies the directory where tests are executed from. This
aecac8059c00 run-tests: make testdir an argument of TestSuite.__init__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21532
diff changeset
  1347
        is typically the ``tests`` directory from Mercurial's source
aecac8059c00 run-tests: make testdir an argument of TestSuite.__init__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21532
diff changeset
  1348
        repository.
aecac8059c00 run-tests: make testdir an argument of TestSuite.__init__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21532
diff changeset
  1349
21528
32b9bbca2052 run-tests: pass jobs into TestSuite constructor
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21523
diff changeset
  1350
        jobs specifies the number of jobs to run concurrently. Each test
32b9bbca2052 run-tests: pass jobs into TestSuite constructor
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21523
diff changeset
  1351
        executes on its own thread. Tests actually spawn new processes, so
32b9bbca2052 run-tests: pass jobs into TestSuite constructor
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21523
diff changeset
  1352
        state mutation should not be an issue.
21529
117e027390ab run-tests: move whitelist and blacklist to named arguments of TestSuite
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21528
diff changeset
  1353
117e027390ab run-tests: move whitelist and blacklist to named arguments of TestSuite
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21528
diff changeset
  1354
        whitelist and blacklist denote tests that have been whitelisted and
117e027390ab run-tests: move whitelist and blacklist to named arguments of TestSuite
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21528
diff changeset
  1355
        blacklisted, respectively. These arguments don't belong in TestSuite.
117e027390ab run-tests: move whitelist and blacklist to named arguments of TestSuite
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21528
diff changeset
  1356
        Instead, whitelist and blacklist should be handled by the thing that
117e027390ab run-tests: move whitelist and blacklist to named arguments of TestSuite
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21528
diff changeset
  1357
        populates the TestSuite with tests. They are present to preserve
117e027390ab run-tests: move whitelist and blacklist to named arguments of TestSuite
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21528
diff changeset
  1358
        backwards compatible behavior which reports skipped tests as part
117e027390ab run-tests: move whitelist and blacklist to named arguments of TestSuite
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21528
diff changeset
  1359
        of the results.
21530
78289625e986 run-tests: make retest a named argument of TestSuite.__init__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21529
diff changeset
  1360
78289625e986 run-tests: make retest a named argument of TestSuite.__init__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21529
diff changeset
  1361
        retest denotes whether to retest failed tests. This arguably belongs
78289625e986 run-tests: make retest a named argument of TestSuite.__init__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21529
diff changeset
  1362
        outside of TestSuite.
21531
7fcda22acc43 run-tests: make keywords a named argument to TestSuite.__init__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21530
diff changeset
  1363
7fcda22acc43 run-tests: make keywords a named argument to TestSuite.__init__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21530
diff changeset
  1364
        keywords denotes key words that will be used to filter which tests
7fcda22acc43 run-tests: make keywords a named argument to TestSuite.__init__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21530
diff changeset
  1365
        to execute. This arguably belongs outside of TestSuite.
21532
9d2ba7e2324d run-tests: move loop to a named argument of TestSuite.__init__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21531
diff changeset
  1366
9d2ba7e2324d run-tests: move loop to a named argument of TestSuite.__init__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21531
diff changeset
  1367
        loop denotes whether to loop over tests forever.
21528
32b9bbca2052 run-tests: pass jobs into TestSuite constructor
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21523
diff changeset
  1368
        """
21439
2e22954b97e3 run-tests: define a custom TestSuite that uses _executetests()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21438
diff changeset
  1369
        super(TestSuite, self).__init__(*args, **kwargs)
2e22954b97e3 run-tests: define a custom TestSuite that uses _executetests()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21438
diff changeset
  1370
21528
32b9bbca2052 run-tests: pass jobs into TestSuite constructor
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21523
diff changeset
  1371
        self._jobs = jobs
21529
117e027390ab run-tests: move whitelist and blacklist to named arguments of TestSuite
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21528
diff changeset
  1372
        self._whitelist = whitelist
117e027390ab run-tests: move whitelist and blacklist to named arguments of TestSuite
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21528
diff changeset
  1373
        self._blacklist = blacklist
21530
78289625e986 run-tests: make retest a named argument of TestSuite.__init__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21529
diff changeset
  1374
        self._retest = retest
21531
7fcda22acc43 run-tests: make keywords a named argument to TestSuite.__init__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21530
diff changeset
  1375
        self._keywords = keywords
21532
9d2ba7e2324d run-tests: move loop to a named argument of TestSuite.__init__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21531
diff changeset
  1376
        self._loop = loop
24329
e7ca4d4b10e1 run-tests: add --runs-per-test flag
Augie Fackler <augie@google.com>
parents: 24306
diff changeset
  1377
        self._runs_per_test = runs_per_test
24330
799bc18e14d1 run-tests: avoid running the same test instance concurrently
Augie Fackler <augie@google.com>
parents: 24329
diff changeset
  1378
        self._loadtest = loadtest
21439
2e22954b97e3 run-tests: define a custom TestSuite that uses _executetests()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21438
diff changeset
  1379
2e22954b97e3 run-tests: define a custom TestSuite that uses _executetests()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21438
diff changeset
  1380
    def run(self, result):
21507
d839e4820da7 run-tests: move test filtering into TestSuite.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21506
diff changeset
  1381
        # We have a number of filters that need to be applied. We do this
d839e4820da7 run-tests: move test filtering into TestSuite.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21506
diff changeset
  1382
        # here instead of inside Test because it makes the running logic for
d839e4820da7 run-tests: move test filtering into TestSuite.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21506
diff changeset
  1383
        # Test simpler.
d839e4820da7 run-tests: move test filtering into TestSuite.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21506
diff changeset
  1384
        tests = []
24330
799bc18e14d1 run-tests: avoid running the same test instance concurrently
Augie Fackler <augie@google.com>
parents: 24329
diff changeset
  1385
        num_tests = [0]
21507
d839e4820da7 run-tests: move test filtering into TestSuite.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21506
diff changeset
  1386
        for test in self._tests:
24330
799bc18e14d1 run-tests: avoid running the same test instance concurrently
Augie Fackler <augie@google.com>
parents: 24329
diff changeset
  1387
            def get():
799bc18e14d1 run-tests: avoid running the same test instance concurrently
Augie Fackler <augie@google.com>
parents: 24329
diff changeset
  1388
                num_tests[0] += 1
799bc18e14d1 run-tests: avoid running the same test instance concurrently
Augie Fackler <augie@google.com>
parents: 24329
diff changeset
  1389
                if getattr(test, 'should_reload', False):
799bc18e14d1 run-tests: avoid running the same test instance concurrently
Augie Fackler <augie@google.com>
parents: 24329
diff changeset
  1390
                    return self._loadtest(test.name, num_tests[0])
799bc18e14d1 run-tests: avoid running the same test instance concurrently
Augie Fackler <augie@google.com>
parents: 24329
diff changeset
  1391
                return test
21507
d839e4820da7 run-tests: move test filtering into TestSuite.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21506
diff changeset
  1392
            if not os.path.exists(test.path):
d839e4820da7 run-tests: move test filtering into TestSuite.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21506
diff changeset
  1393
                result.addSkip(test, "Doesn't exist")
d839e4820da7 run-tests: move test filtering into TestSuite.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21506
diff changeset
  1394
                continue
d839e4820da7 run-tests: move test filtering into TestSuite.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21506
diff changeset
  1395
21529
117e027390ab run-tests: move whitelist and blacklist to named arguments of TestSuite
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21528
diff changeset
  1396
            if not (self._whitelist and test.name in self._whitelist):
117e027390ab run-tests: move whitelist and blacklist to named arguments of TestSuite
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21528
diff changeset
  1397
                if self._blacklist and test.name in self._blacklist:
21507
d839e4820da7 run-tests: move test filtering into TestSuite.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21506
diff changeset
  1398
                    result.addSkip(test, 'blacklisted')
d839e4820da7 run-tests: move test filtering into TestSuite.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21506
diff changeset
  1399
                    continue
d839e4820da7 run-tests: move test filtering into TestSuite.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21506
diff changeset
  1400
21530
78289625e986 run-tests: make retest a named argument of TestSuite.__init__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21529
diff changeset
  1401
                if self._retest and not os.path.exists(test.errpath):
21507
d839e4820da7 run-tests: move test filtering into TestSuite.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21506
diff changeset
  1402
                    result.addIgnore(test, 'not retesting')
d839e4820da7 run-tests: move test filtering into TestSuite.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21506
diff changeset
  1403
                    continue
d839e4820da7 run-tests: move test filtering into TestSuite.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21506
diff changeset
  1404
21531
7fcda22acc43 run-tests: make keywords a named argument to TestSuite.__init__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21530
diff changeset
  1405
                if self._keywords:
21916
792ebd7dc5f6 run-tests: write out scripts in binary mode
Augie Fackler <raf@durin42.com>
parents: 21763
diff changeset
  1406
                    f = open(test.path, 'rb')
21507
d839e4820da7 run-tests: move test filtering into TestSuite.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21506
diff changeset
  1407
                    t = f.read().lower() + test.name.lower()
d839e4820da7 run-tests: move test filtering into TestSuite.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21506
diff changeset
  1408
                    f.close()
d839e4820da7 run-tests: move test filtering into TestSuite.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21506
diff changeset
  1409
                    ignored = False
21531
7fcda22acc43 run-tests: make keywords a named argument to TestSuite.__init__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21530
diff changeset
  1410
                    for k in self._keywords.lower().split():
21507
d839e4820da7 run-tests: move test filtering into TestSuite.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21506
diff changeset
  1411
                        if k not in t:
d839e4820da7 run-tests: move test filtering into TestSuite.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21506
diff changeset
  1412
                            result.addIgnore(test, "doesn't match keyword")
d839e4820da7 run-tests: move test filtering into TestSuite.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21506
diff changeset
  1413
                            ignored = True
d839e4820da7 run-tests: move test filtering into TestSuite.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21506
diff changeset
  1414
                            break
d839e4820da7 run-tests: move test filtering into TestSuite.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21506
diff changeset
  1415
d839e4820da7 run-tests: move test filtering into TestSuite.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21506
diff changeset
  1416
                    if ignored:
d839e4820da7 run-tests: move test filtering into TestSuite.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21506
diff changeset
  1417
                        continue
24329
e7ca4d4b10e1 run-tests: add --runs-per-test flag
Augie Fackler <augie@google.com>
parents: 24306
diff changeset
  1418
            for _ in xrange(self._runs_per_test):
24330
799bc18e14d1 run-tests: avoid running the same test instance concurrently
Augie Fackler <augie@google.com>
parents: 24329
diff changeset
  1419
                tests.append(get())
21496
f145914e698d run-tests: move _executetests into TestSuite
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21495
diff changeset
  1420
21520
fa6ba4372678 run-tests: remove global abort flag
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21518
diff changeset
  1421
        runtests = list(tests)
21496
f145914e698d run-tests: move _executetests into TestSuite
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21495
diff changeset
  1422
        done = queue.Queue()
f145914e698d run-tests: move _executetests into TestSuite
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21495
diff changeset
  1423
        running = 0
f145914e698d run-tests: move _executetests into TestSuite
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21495
diff changeset
  1424
f145914e698d run-tests: move _executetests into TestSuite
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21495
diff changeset
  1425
        def job(test, result):
f145914e698d run-tests: move _executetests into TestSuite
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21495
diff changeset
  1426
            try:
f145914e698d run-tests: move _executetests into TestSuite
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21495
diff changeset
  1427
                test(result)
f145914e698d run-tests: move _executetests into TestSuite
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21495
diff changeset
  1428
                done.put(None)
f145914e698d run-tests: move _executetests into TestSuite
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21495
diff changeset
  1429
            except KeyboardInterrupt:
f145914e698d run-tests: move _executetests into TestSuite
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21495
diff changeset
  1430
                pass
f145914e698d run-tests: move _executetests into TestSuite
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21495
diff changeset
  1431
            except: # re-raises
f145914e698d run-tests: move _executetests into TestSuite
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21495
diff changeset
  1432
                done.put(('!', test, 'run-test raised an error, see traceback'))
f145914e698d run-tests: move _executetests into TestSuite
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21495
diff changeset
  1433
                raise
f145914e698d run-tests: move _executetests into TestSuite
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21495
diff changeset
  1434
24507
a0668a587c04 run-tests: wait for test threads after first error
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24506
diff changeset
  1435
        stoppedearly = False
a0668a587c04 run-tests: wait for test threads after first error
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24506
diff changeset
  1436
21496
f145914e698d run-tests: move _executetests into TestSuite
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21495
diff changeset
  1437
        try:
f145914e698d run-tests: move _executetests into TestSuite
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21495
diff changeset
  1438
            while tests or running:
21528
32b9bbca2052 run-tests: pass jobs into TestSuite constructor
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21523
diff changeset
  1439
                if not done.empty() or running == self._jobs or not tests:
21496
f145914e698d run-tests: move _executetests into TestSuite
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21495
diff changeset
  1440
                    try:
f145914e698d run-tests: move _executetests into TestSuite
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21495
diff changeset
  1441
                        done.get(True, 1)
24507
a0668a587c04 run-tests: wait for test threads after first error
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24506
diff changeset
  1442
                        running -= 1
21496
f145914e698d run-tests: move _executetests into TestSuite
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21495
diff changeset
  1443
                        if result and result.shouldStop:
24507
a0668a587c04 run-tests: wait for test threads after first error
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24506
diff changeset
  1444
                            stoppedearly = True
21496
f145914e698d run-tests: move _executetests into TestSuite
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21495
diff changeset
  1445
                            break
f145914e698d run-tests: move _executetests into TestSuite
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21495
diff changeset
  1446
                    except queue.Empty:
f145914e698d run-tests: move _executetests into TestSuite
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21495
diff changeset
  1447
                        continue
21528
32b9bbca2052 run-tests: pass jobs into TestSuite constructor
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21523
diff changeset
  1448
                if tests and not running == self._jobs:
21496
f145914e698d run-tests: move _executetests into TestSuite
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21495
diff changeset
  1449
                    test = tests.pop(0)
21532
9d2ba7e2324d run-tests: move loop to a named argument of TestSuite.__init__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21531
diff changeset
  1450
                    if self._loop:
24330
799bc18e14d1 run-tests: avoid running the same test instance concurrently
Augie Fackler <augie@google.com>
parents: 24329
diff changeset
  1451
                        if getattr(test, 'should_reload', False):
799bc18e14d1 run-tests: avoid running the same test instance concurrently
Augie Fackler <augie@google.com>
parents: 24329
diff changeset
  1452
                            num_tests[0] += 1
799bc18e14d1 run-tests: avoid running the same test instance concurrently
Augie Fackler <augie@google.com>
parents: 24329
diff changeset
  1453
                            tests.append(
799bc18e14d1 run-tests: avoid running the same test instance concurrently
Augie Fackler <augie@google.com>
parents: 24329
diff changeset
  1454
                                self._loadtest(test.name, num_tests[0]))
799bc18e14d1 run-tests: avoid running the same test instance concurrently
Augie Fackler <augie@google.com>
parents: 24329
diff changeset
  1455
                        else:
799bc18e14d1 run-tests: avoid running the same test instance concurrently
Augie Fackler <augie@google.com>
parents: 24329
diff changeset
  1456
                            tests.append(test)
21496
f145914e698d run-tests: move _executetests into TestSuite
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21495
diff changeset
  1457
                    t = threading.Thread(target=job, name=test.name,
f145914e698d run-tests: move _executetests into TestSuite
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21495
diff changeset
  1458
                                         args=(test, result))
f145914e698d run-tests: move _executetests into TestSuite
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21495
diff changeset
  1459
                    t.start()
f145914e698d run-tests: move _executetests into TestSuite
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21495
diff changeset
  1460
                    running += 1
24507
a0668a587c04 run-tests: wait for test threads after first error
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24506
diff changeset
  1461
a0668a587c04 run-tests: wait for test threads after first error
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24506
diff changeset
  1462
            # If we stop early we still need to wait on started tests to
a0668a587c04 run-tests: wait for test threads after first error
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24506
diff changeset
  1463
            # finish. Otherwise, there is a race between the test completing
a0668a587c04 run-tests: wait for test threads after first error
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24506
diff changeset
  1464
            # and the test's cleanup code running. This could result in the
a0668a587c04 run-tests: wait for test threads after first error
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24506
diff changeset
  1465
            # test reporting incorrect.
a0668a587c04 run-tests: wait for test threads after first error
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24506
diff changeset
  1466
            if stoppedearly:
a0668a587c04 run-tests: wait for test threads after first error
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24506
diff changeset
  1467
                while running:
a0668a587c04 run-tests: wait for test threads after first error
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24506
diff changeset
  1468
                    try:
a0668a587c04 run-tests: wait for test threads after first error
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24506
diff changeset
  1469
                        done.get(True, 1)
a0668a587c04 run-tests: wait for test threads after first error
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24506
diff changeset
  1470
                        running -= 1
a0668a587c04 run-tests: wait for test threads after first error
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24506
diff changeset
  1471
                    except queue.Empty:
a0668a587c04 run-tests: wait for test threads after first error
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24506
diff changeset
  1472
                        continue
21496
f145914e698d run-tests: move _executetests into TestSuite
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21495
diff changeset
  1473
        except KeyboardInterrupt:
21520
fa6ba4372678 run-tests: remove global abort flag
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21518
diff changeset
  1474
            for test in runtests:
fa6ba4372678 run-tests: remove global abort flag
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21518
diff changeset
  1475
                test.abort()
21439
2e22954b97e3 run-tests: define a custom TestSuite that uses _executetests()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21438
diff changeset
  1476
2e22954b97e3 run-tests: define a custom TestSuite that uses _executetests()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21438
diff changeset
  1477
        return result
2e22954b97e3 run-tests: define a custom TestSuite that uses _executetests()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21438
diff changeset
  1478
21429
203ed3cf6c81 run-tests: define custom result and runner classes for unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21428
diff changeset
  1479
class TextTestRunner(unittest.TextTestRunner):
203ed3cf6c81 run-tests: define custom result and runner classes for unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21428
diff changeset
  1480
    """Custom unittest test runner that uses appropriate settings."""
203ed3cf6c81 run-tests: define custom result and runner classes for unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21428
diff changeset
  1481
21459
d5945324b130 run-tests: print compatible output from TextTestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21458
diff changeset
  1482
    def __init__(self, runner, *args, **kwargs):
d5945324b130 run-tests: print compatible output from TextTestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21458
diff changeset
  1483
        super(TextTestRunner, self).__init__(*args, **kwargs)
d5945324b130 run-tests: print compatible output from TextTestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21458
diff changeset
  1484
d5945324b130 run-tests: print compatible output from TextTestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21458
diff changeset
  1485
        self._runner = runner
d5945324b130 run-tests: print compatible output from TextTestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21458
diff changeset
  1486
d5945324b130 run-tests: print compatible output from TextTestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21458
diff changeset
  1487
    def run(self, test):
21460
df580990507e run-tests: abort tests after first failure in unittest mode
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21459
diff changeset
  1488
        result = TestResult(self._runner.options, self.stream,
df580990507e run-tests: abort tests after first failure in unittest mode
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21459
diff changeset
  1489
                            self.descriptions, self.verbosity)
21459
d5945324b130 run-tests: print compatible output from TextTestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21458
diff changeset
  1490
d5945324b130 run-tests: print compatible output from TextTestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21458
diff changeset
  1491
        test(result)
d5945324b130 run-tests: print compatible output from TextTestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21458
diff changeset
  1492
d5945324b130 run-tests: print compatible output from TextTestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21458
diff changeset
  1493
        failed = len(result.failures)
d5945324b130 run-tests: print compatible output from TextTestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21458
diff changeset
  1494
        warned = len(result.warned)
d5945324b130 run-tests: print compatible output from TextTestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21458
diff changeset
  1495
        skipped = len(result.skipped)
d5945324b130 run-tests: print compatible output from TextTestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21458
diff changeset
  1496
        ignored = len(result.ignored)
d5945324b130 run-tests: print compatible output from TextTestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21458
diff changeset
  1497
22104
70bdf59d27b6 run-tests: attempt to fix iolock handling
Matt Mackall <mpm@selenic.com>
parents: 22045
diff changeset
  1498
        iolock.acquire()
21459
d5945324b130 run-tests: print compatible output from TextTestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21458
diff changeset
  1499
        self.stream.writeln('')
d5945324b130 run-tests: print compatible output from TextTestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21458
diff changeset
  1500
d5945324b130 run-tests: print compatible output from TextTestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21458
diff changeset
  1501
        if not self._runner.options.noskips:
d5945324b130 run-tests: print compatible output from TextTestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21458
diff changeset
  1502
            for test, msg in result.skipped:
d5945324b130 run-tests: print compatible output from TextTestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21458
diff changeset
  1503
                self.stream.writeln('Skipped %s: %s' % (test.name, msg))
d5945324b130 run-tests: print compatible output from TextTestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21458
diff changeset
  1504
        for test, msg in result.warned:
d5945324b130 run-tests: print compatible output from TextTestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21458
diff changeset
  1505
            self.stream.writeln('Warned %s: %s' % (test.name, msg))
d5945324b130 run-tests: print compatible output from TextTestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21458
diff changeset
  1506
        for test, msg in result.failures:
d5945324b130 run-tests: print compatible output from TextTestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21458
diff changeset
  1507
            self.stream.writeln('Failed %s: %s' % (test.name, msg))
d5945324b130 run-tests: print compatible output from TextTestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21458
diff changeset
  1508
        for test, msg in result.errors:
d5945324b130 run-tests: print compatible output from TextTestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21458
diff changeset
  1509
            self.stream.writeln('Errored %s: %s' % (test.name, msg))
d5945324b130 run-tests: print compatible output from TextTestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21458
diff changeset
  1510
22044
a06172e85fd4 run-tests: add support for xunit test reports
Augie Fackler <raf@durin42.com>
parents: 21997
diff changeset
  1511
        if self._runner.options.xunit:
a06172e85fd4 run-tests: add support for xunit test reports
Augie Fackler <raf@durin42.com>
parents: 21997
diff changeset
  1512
            xuf = open(self._runner.options.xunit, 'wb')
a06172e85fd4 run-tests: add support for xunit test reports
Augie Fackler <raf@durin42.com>
parents: 21997
diff changeset
  1513
            try:
24982
5c15f7e0f52b run-tests: stop explicit expansion of time data
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24981
diff changeset
  1514
                timesd = dict((t[0], t[3]) for t in result.times)
22044
a06172e85fd4 run-tests: add support for xunit test reports
Augie Fackler <raf@durin42.com>
parents: 21997
diff changeset
  1515
                doc = minidom.Document()
a06172e85fd4 run-tests: add support for xunit test reports
Augie Fackler <raf@durin42.com>
parents: 21997
diff changeset
  1516
                s = doc.createElement('testsuite')
a06172e85fd4 run-tests: add support for xunit test reports
Augie Fackler <raf@durin42.com>
parents: 21997
diff changeset
  1517
                s.setAttribute('name', 'run-tests')
a06172e85fd4 run-tests: add support for xunit test reports
Augie Fackler <raf@durin42.com>
parents: 21997
diff changeset
  1518
                s.setAttribute('tests', str(result.testsRun))
a06172e85fd4 run-tests: add support for xunit test reports
Augie Fackler <raf@durin42.com>
parents: 21997
diff changeset
  1519
                s.setAttribute('errors', "0") # TODO
a06172e85fd4 run-tests: add support for xunit test reports
Augie Fackler <raf@durin42.com>
parents: 21997
diff changeset
  1520
                s.setAttribute('failures', str(failed))
a06172e85fd4 run-tests: add support for xunit test reports
Augie Fackler <raf@durin42.com>
parents: 21997
diff changeset
  1521
                s.setAttribute('skipped', str(skipped + ignored))
a06172e85fd4 run-tests: add support for xunit test reports
Augie Fackler <raf@durin42.com>
parents: 21997
diff changeset
  1522
                doc.appendChild(s)
a06172e85fd4 run-tests: add support for xunit test reports
Augie Fackler <raf@durin42.com>
parents: 21997
diff changeset
  1523
                for tc in result.successes:
a06172e85fd4 run-tests: add support for xunit test reports
Augie Fackler <raf@durin42.com>
parents: 21997
diff changeset
  1524
                    t = doc.createElement('testcase')
a06172e85fd4 run-tests: add support for xunit test reports
Augie Fackler <raf@durin42.com>
parents: 21997
diff changeset
  1525
                    t.setAttribute('name', tc.name)
a06172e85fd4 run-tests: add support for xunit test reports
Augie Fackler <raf@durin42.com>
parents: 21997
diff changeset
  1526
                    t.setAttribute('time', '%.3f' % timesd[tc.name])
a06172e85fd4 run-tests: add support for xunit test reports
Augie Fackler <raf@durin42.com>
parents: 21997
diff changeset
  1527
                    s.appendChild(t)
a06172e85fd4 run-tests: add support for xunit test reports
Augie Fackler <raf@durin42.com>
parents: 21997
diff changeset
  1528
                for tc, err in sorted(result.faildata.iteritems()):
a06172e85fd4 run-tests: add support for xunit test reports
Augie Fackler <raf@durin42.com>
parents: 21997
diff changeset
  1529
                    t = doc.createElement('testcase')
a06172e85fd4 run-tests: add support for xunit test reports
Augie Fackler <raf@durin42.com>
parents: 21997
diff changeset
  1530
                    t.setAttribute('name', tc)
a06172e85fd4 run-tests: add support for xunit test reports
Augie Fackler <raf@durin42.com>
parents: 21997
diff changeset
  1531
                    t.setAttribute('time', '%.3f' % timesd[tc])
24500
7b0a20cd8c95 run-tests: explicitly handle unicode when writing xunit file
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24332
diff changeset
  1532
                    # createCDATASection expects a unicode or it will convert
7b0a20cd8c95 run-tests: explicitly handle unicode when writing xunit file
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24332
diff changeset
  1533
                    # using default conversion rules, which will fail if
7b0a20cd8c95 run-tests: explicitly handle unicode when writing xunit file
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24332
diff changeset
  1534
                    # string isn't ASCII.
7b0a20cd8c95 run-tests: explicitly handle unicode when writing xunit file
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24332
diff changeset
  1535
                    err = cdatasafe(err).decode('utf-8', 'replace')
7b0a20cd8c95 run-tests: explicitly handle unicode when writing xunit file
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24332
diff changeset
  1536
                    cd = doc.createCDATASection(err)
22044
a06172e85fd4 run-tests: add support for xunit test reports
Augie Fackler <raf@durin42.com>
parents: 21997
diff changeset
  1537
                    t.appendChild(cd)
a06172e85fd4 run-tests: add support for xunit test reports
Augie Fackler <raf@durin42.com>
parents: 21997
diff changeset
  1538
                    s.appendChild(t)
a06172e85fd4 run-tests: add support for xunit test reports
Augie Fackler <raf@durin42.com>
parents: 21997
diff changeset
  1539
                xuf.write(doc.toprettyxml(indent='  ', encoding='utf-8'))
a06172e85fd4 run-tests: add support for xunit test reports
Augie Fackler <raf@durin42.com>
parents: 21997
diff changeset
  1540
            finally:
a06172e85fd4 run-tests: add support for xunit test reports
Augie Fackler <raf@durin42.com>
parents: 21997
diff changeset
  1541
                xuf.close()
a06172e85fd4 run-tests: add support for xunit test reports
Augie Fackler <raf@durin42.com>
parents: 21997
diff changeset
  1542
22391
c42e69268f5b run-tests: added '--json' functionality to store test result in json file
anuraggoel <anurag.dsps@gmail.com>
parents: 22361
diff changeset
  1543
        if self._runner.options.json:
c42e69268f5b run-tests: added '--json' functionality to store test result in json file
anuraggoel <anurag.dsps@gmail.com>
parents: 22361
diff changeset
  1544
            if json is None:
c42e69268f5b run-tests: added '--json' functionality to store test result in json file
anuraggoel <anurag.dsps@gmail.com>
parents: 22361
diff changeset
  1545
                raise ImportError("json module not installed")
c42e69268f5b run-tests: added '--json' functionality to store test result in json file
anuraggoel <anurag.dsps@gmail.com>
parents: 22361
diff changeset
  1546
            jsonpath = os.path.join(self._runner._testdir, 'report.json')
c42e69268f5b run-tests: added '--json' functionality to store test result in json file
anuraggoel <anurag.dsps@gmail.com>
parents: 22361
diff changeset
  1547
            fp = open(jsonpath, 'w')
c42e69268f5b run-tests: added '--json' functionality to store test result in json file
anuraggoel <anurag.dsps@gmail.com>
parents: 22361
diff changeset
  1548
            try:
c42e69268f5b run-tests: added '--json' functionality to store test result in json file
anuraggoel <anurag.dsps@gmail.com>
parents: 22361
diff changeset
  1549
                timesd = {}
24982
5c15f7e0f52b run-tests: stop explicit expansion of time data
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24981
diff changeset
  1550
                for tdata in result.times:
5c15f7e0f52b run-tests: stop explicit expansion of time data
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24981
diff changeset
  1551
                    test = tdata[0]
24983
277bc9b0b4bd run-tests: when building json, get time data in the same order as elsewhere
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24982
diff changeset
  1552
                    timesd[test] = tdata[1:]
22391
c42e69268f5b run-tests: added '--json' functionality to store test result in json file
anuraggoel <anurag.dsps@gmail.com>
parents: 22361
diff changeset
  1553
c42e69268f5b run-tests: added '--json' functionality to store test result in json file
anuraggoel <anurag.dsps@gmail.com>
parents: 22361
diff changeset
  1554
                outcome = {}
24981
012b79d549d8 run-tests: refactor json entry creation logic
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24980
diff changeset
  1555
                groups = [('success', ((tc, None) for tc in result.successes)),
012b79d549d8 run-tests: refactor json entry creation logic
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24980
diff changeset
  1556
                          ('failure', result.failures),
012b79d549d8 run-tests: refactor json entry creation logic
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24980
diff changeset
  1557
                          ('skip', result.skipped)]
012b79d549d8 run-tests: refactor json entry creation logic
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24980
diff changeset
  1558
                for res, testcases in groups:
012b79d549d8 run-tests: refactor json entry creation logic
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24980
diff changeset
  1559
                    for tc, __ in testcases:
012b79d549d8 run-tests: refactor json entry creation logic
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24980
diff changeset
  1560
                        testresult = {'result': res,
24983
277bc9b0b4bd run-tests: when building json, get time data in the same order as elsewhere
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24982
diff changeset
  1561
                                      'time': ('%0.3f' % timesd[tc.name][2]),
277bc9b0b4bd run-tests: when building json, get time data in the same order as elsewhere
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24982
diff changeset
  1562
                                      'cuser': ('%0.3f' % timesd[tc.name][0]),
277bc9b0b4bd run-tests: when building json, get time data in the same order as elsewhere
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24982
diff changeset
  1563
                                      'csys': ('%0.3f' % timesd[tc.name][1])}
24981
012b79d549d8 run-tests: refactor json entry creation logic
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24980
diff changeset
  1564
                        outcome[tc.name] = testresult
22391
c42e69268f5b run-tests: added '--json' functionality to store test result in json file
anuraggoel <anurag.dsps@gmail.com>
parents: 22361
diff changeset
  1565
c42e69268f5b run-tests: added '--json' functionality to store test result in json file
anuraggoel <anurag.dsps@gmail.com>
parents: 22361
diff changeset
  1566
                jsonout = json.dumps(outcome, sort_keys=True, indent=4)
c42e69268f5b run-tests: added '--json' functionality to store test result in json file
anuraggoel <anurag.dsps@gmail.com>
parents: 22361
diff changeset
  1567
                fp.writelines(("testreport =", jsonout))
c42e69268f5b run-tests: added '--json' functionality to store test result in json file
anuraggoel <anurag.dsps@gmail.com>
parents: 22361
diff changeset
  1568
            finally:
c42e69268f5b run-tests: added '--json' functionality to store test result in json file
anuraggoel <anurag.dsps@gmail.com>
parents: 22361
diff changeset
  1569
                fp.close()
c42e69268f5b run-tests: added '--json' functionality to store test result in json file
anuraggoel <anurag.dsps@gmail.com>
parents: 22361
diff changeset
  1570
21459
d5945324b130 run-tests: print compatible output from TextTestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21458
diff changeset
  1571
        self._runner._checkhglib('Tested')
d5945324b130 run-tests: print compatible output from TextTestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21458
diff changeset
  1572
d5945324b130 run-tests: print compatible output from TextTestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21458
diff changeset
  1573
        self.stream.writeln('# Ran %d tests, %d skipped, %d warned, %d failed.'
21997
93c3b3f55d59 run-tests: fix test result counts with --keyword specified or skips occurring
Augie Fackler <raf@durin42.com>
parents: 21993
diff changeset
  1574
            % (result.testsRun,
21459
d5945324b130 run-tests: print compatible output from TextTestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21458
diff changeset
  1575
               skipped + ignored, warned, failed))
d5945324b130 run-tests: print compatible output from TextTestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21458
diff changeset
  1576
        if failed:
d5945324b130 run-tests: print compatible output from TextTestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21458
diff changeset
  1577
            self.stream.writeln('python hash seed: %s' %
d5945324b130 run-tests: print compatible output from TextTestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21458
diff changeset
  1578
                os.environ['PYTHONHASHSEED'])
d5945324b130 run-tests: print compatible output from TextTestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21458
diff changeset
  1579
        if self._runner.options.time:
21495
b162bdc78bef run-tests: capture execution times in TestResult
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21494
diff changeset
  1580
            self.printtimes(result.times)
21494
dcefc4091c86 run-tests: move outputtimes() into unittest runner class
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21493
diff changeset
  1581
22104
70bdf59d27b6 run-tests: attempt to fix iolock handling
Matt Mackall <mpm@selenic.com>
parents: 22045
diff changeset
  1582
        iolock.release()
70bdf59d27b6 run-tests: attempt to fix iolock handling
Matt Mackall <mpm@selenic.com>
parents: 22045
diff changeset
  1583
21613
b3213b9fafed run-tests: exit with non-0 exit code when tests fail or warn
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21539
diff changeset
  1584
        return result
b3213b9fafed run-tests: exit with non-0 exit code when tests fail or warn
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21539
diff changeset
  1585
21495
b162bdc78bef run-tests: capture execution times in TestResult
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21494
diff changeset
  1586
    def printtimes(self, times):
22104
70bdf59d27b6 run-tests: attempt to fix iolock handling
Matt Mackall <mpm@selenic.com>
parents: 22045
diff changeset
  1587
        # iolock held by run
21494
dcefc4091c86 run-tests: move outputtimes() into unittest runner class
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21493
diff changeset
  1588
        self.stream.writeln('# Producing time report')
21977
4ca4e1572022 run-tests: '--time' option provide more details to Linux users
anuraggoel <anurag.dsps@gmail.com>
parents: 21919
diff changeset
  1589
        times.sort(key=lambda t: (t[3]))
4ca4e1572022 run-tests: '--time' option provide more details to Linux users
anuraggoel <anurag.dsps@gmail.com>
parents: 21919
diff changeset
  1590
        cols = '%7.3f %7.3f %7.3f   %s'
4ca4e1572022 run-tests: '--time' option provide more details to Linux users
anuraggoel <anurag.dsps@gmail.com>
parents: 21919
diff changeset
  1591
        self.stream.writeln('%-7s %-7s %-7s   %s' % ('cuser', 'csys', 'real',
4ca4e1572022 run-tests: '--time' option provide more details to Linux users
anuraggoel <anurag.dsps@gmail.com>
parents: 21919
diff changeset
  1592
                    'Test'))
24982
5c15f7e0f52b run-tests: stop explicit expansion of time data
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24981
diff changeset
  1593
        for tdata in times:
5c15f7e0f52b run-tests: stop explicit expansion of time data
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24981
diff changeset
  1594
            test = tdata[0]
5c15f7e0f52b run-tests: stop explicit expansion of time data
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24981
diff changeset
  1595
            cuser, csys, real = tdata[1:4]
21977
4ca4e1572022 run-tests: '--time' option provide more details to Linux users
anuraggoel <anurag.dsps@gmail.com>
parents: 21919
diff changeset
  1596
            self.stream.writeln(cols % (cuser, csys, real, test))
21429
203ed3cf6c81 run-tests: define custom result and runner classes for unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21428
diff changeset
  1597
21340
fda36de1cb0e run-tests: establish a class to hold testing state
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21339
diff changeset
  1598
class TestRunner(object):
fda36de1cb0e run-tests: establish a class to hold testing state
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21339
diff changeset
  1599
    """Holds context for executing tests.
fda36de1cb0e run-tests: establish a class to hold testing state
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21339
diff changeset
  1600
fda36de1cb0e run-tests: establish a class to hold testing state
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21339
diff changeset
  1601
    Tests rely on a lot of state. This object holds it for them.
fda36de1cb0e run-tests: establish a class to hold testing state
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21339
diff changeset
  1602
    """
21357
4c4f64b8df3c run-tests: move gettest() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21356
diff changeset
  1603
21536
92a6b16c9186 run-tests: add docstrings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21535
diff changeset
  1604
    # Programs required to run tests.
21365
10cf9054d941 run-tests: move program searching into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21364
diff changeset
  1605
    REQUIREDTOOLS = [
10cf9054d941 run-tests: move program searching into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21364
diff changeset
  1606
        os.path.basename(sys.executable),
10cf9054d941 run-tests: move program searching into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21364
diff changeset
  1607
        'diff',
10cf9054d941 run-tests: move program searching into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21364
diff changeset
  1608
        'grep',
10cf9054d941 run-tests: move program searching into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21364
diff changeset
  1609
        'unzip',
10cf9054d941 run-tests: move program searching into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21364
diff changeset
  1610
        'gunzip',
10cf9054d941 run-tests: move program searching into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21364
diff changeset
  1611
        'bunzip2',
10cf9054d941 run-tests: move program searching into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21364
diff changeset
  1612
        'sed',
10cf9054d941 run-tests: move program searching into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21364
diff changeset
  1613
    ]
10cf9054d941 run-tests: move program searching into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21364
diff changeset
  1614
21536
92a6b16c9186 run-tests: add docstrings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21535
diff changeset
  1615
    # Maps file extensions to test class.
21357
4c4f64b8df3c run-tests: move gettest() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21356
diff changeset
  1616
    TESTTYPES = [
21501
98a0c58ee200 run-tests: factor refpath into Test classes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21500
diff changeset
  1617
        ('.py', PythonTest),
98a0c58ee200 run-tests: factor refpath into Test classes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21500
diff changeset
  1618
        ('.t', TTest),
21357
4c4f64b8df3c run-tests: move gettest() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21356
diff changeset
  1619
    ]
4c4f64b8df3c run-tests: move gettest() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21356
diff changeset
  1620
21341
cb88d4a04f58 run-tests: move TESTDIR out of a global
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21340
diff changeset
  1621
    def __init__(self):
21348
b3399154505f run-tests: add options to runner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21347
diff changeset
  1622
        self.options = None
24506
60bbb4079c28 run-tests: report code coverage from source directory
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24505
diff changeset
  1623
        self._hgroot = None
21534
3ece55d16044 run-tests: make attributes of TestRunner internal
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21533
diff changeset
  1624
        self._testdir = None
3ece55d16044 run-tests: make attributes of TestRunner internal
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21533
diff changeset
  1625
        self._hgtmp = None
3ece55d16044 run-tests: make attributes of TestRunner internal
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21533
diff changeset
  1626
        self._installdir = None
3ece55d16044 run-tests: make attributes of TestRunner internal
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21533
diff changeset
  1627
        self._bindir = None
3ece55d16044 run-tests: make attributes of TestRunner internal
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21533
diff changeset
  1628
        self._tmpbinddir = None
3ece55d16044 run-tests: make attributes of TestRunner internal
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21533
diff changeset
  1629
        self._pythondir = None
3ece55d16044 run-tests: make attributes of TestRunner internal
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21533
diff changeset
  1630
        self._coveragefile = None
21352
39fd89fbbc3c run-tests: move createdfiles out of a global and into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21351
diff changeset
  1631
        self._createdfiles = []
21385
28414e5ac9ec run-tests: move _gethgpath() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21384
diff changeset
  1632
        self._hgpath = None
24967
00790cc2b753 run-test: ensure the test ports are available before launching test
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24965
diff changeset
  1633
        self._portoffset = 0
00790cc2b753 run-test: ensure the test ports are available before launching test
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24965
diff changeset
  1634
        self._ports = {}
21340
fda36de1cb0e run-tests: establish a class to hold testing state
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21339
diff changeset
  1635
21376
e4366bc08879 run-tests: move option parser logic to TestRunner.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21375
diff changeset
  1636
    def run(self, args, parser=None):
21366
5047248536c5 run-tests: establish TestRunner.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21365
diff changeset
  1637
        """Run the test suite."""
25031
0adc22a0b6b3 python3: update killdaemons and run-tests print and exception syntax
Augie Fackler <augie@google.com>
parents: 24984
diff changeset
  1638
        oldmask = os.umask(0o22)
21375
bd70dcb91af6 run-tests: move umask into TestRunner.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21374
diff changeset
  1639
        try:
21376
e4366bc08879 run-tests: move option parser logic to TestRunner.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21375
diff changeset
  1640
            parser = parser or getparser()
e4366bc08879 run-tests: move option parser logic to TestRunner.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21375
diff changeset
  1641
            options, args = parseargs(args, parser)
e4366bc08879 run-tests: move option parser logic to TestRunner.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21375
diff changeset
  1642
            self.options = options
e4366bc08879 run-tests: move option parser logic to TestRunner.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21375
diff changeset
  1643
21375
bd70dcb91af6 run-tests: move umask into TestRunner.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21374
diff changeset
  1644
            self._checktools()
bd70dcb91af6 run-tests: move umask into TestRunner.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21374
diff changeset
  1645
            tests = self.findtests(args)
bd70dcb91af6 run-tests: move umask into TestRunner.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21374
diff changeset
  1646
            return self._run(tests)
bd70dcb91af6 run-tests: move umask into TestRunner.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21374
diff changeset
  1647
        finally:
bd70dcb91af6 run-tests: move umask into TestRunner.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21374
diff changeset
  1648
            os.umask(oldmask)
21366
5047248536c5 run-tests: establish TestRunner.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21365
diff changeset
  1649
5047248536c5 run-tests: establish TestRunner.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21365
diff changeset
  1650
    def _run(self, tests):
21372
3a44787e50e2 run-tests: move test shuffling and sorting into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21371
diff changeset
  1651
        if self.options.random:
3a44787e50e2 run-tests: move test shuffling and sorting into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21371
diff changeset
  1652
            random.shuffle(tests)
3a44787e50e2 run-tests: move test shuffling and sorting into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21371
diff changeset
  1653
        else:
3a44787e50e2 run-tests: move test shuffling and sorting into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21371
diff changeset
  1654
            # keywords for slow tests
3a44787e50e2 run-tests: move test shuffling and sorting into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21371
diff changeset
  1655
            slow = 'svn gendoc check-code-hg'.split()
3a44787e50e2 run-tests: move test shuffling and sorting into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21371
diff changeset
  1656
            def sortkey(f):
3a44787e50e2 run-tests: move test shuffling and sorting into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21371
diff changeset
  1657
                # run largest tests first, as they tend to take the longest
3a44787e50e2 run-tests: move test shuffling and sorting into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21371
diff changeset
  1658
                try:
3a44787e50e2 run-tests: move test shuffling and sorting into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21371
diff changeset
  1659
                    val = -os.stat(f).st_size
25031
0adc22a0b6b3 python3: update killdaemons and run-tests print and exception syntax
Augie Fackler <augie@google.com>
parents: 24984
diff changeset
  1660
                except OSError as e:
21372
3a44787e50e2 run-tests: move test shuffling and sorting into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21371
diff changeset
  1661
                    if e.errno != errno.ENOENT:
3a44787e50e2 run-tests: move test shuffling and sorting into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21371
diff changeset
  1662
                        raise
3a44787e50e2 run-tests: move test shuffling and sorting into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21371
diff changeset
  1663
                    return -1e9 # file does not exist, tell early
3a44787e50e2 run-tests: move test shuffling and sorting into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21371
diff changeset
  1664
                for kw in slow:
3a44787e50e2 run-tests: move test shuffling and sorting into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21371
diff changeset
  1665
                    if kw in f:
3a44787e50e2 run-tests: move test shuffling and sorting into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21371
diff changeset
  1666
                        val *= 10
3a44787e50e2 run-tests: move test shuffling and sorting into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21371
diff changeset
  1667
                return val
3a44787e50e2 run-tests: move test shuffling and sorting into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21371
diff changeset
  1668
            tests.sort(key=sortkey)
3a44787e50e2 run-tests: move test shuffling and sorting into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21371
diff changeset
  1669
21534
3ece55d16044 run-tests: make attributes of TestRunner internal
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21533
diff changeset
  1670
        self._testdir = os.environ['TESTDIR'] = os.getcwd()
21371
a10ba7870c2d run-tests: assign testdir in TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21370
diff changeset
  1671
21370
97475f27bebe run-tests: move hash seed logic to TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21369
diff changeset
  1672
        if 'PYTHONHASHSEED' not in os.environ:
97475f27bebe run-tests: move hash seed logic to TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21369
diff changeset
  1673
            # use a random python hash seed all the time
97475f27bebe run-tests: move hash seed logic to TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21369
diff changeset
  1674
            # we do the randomness ourself to know what seed is used
97475f27bebe run-tests: move hash seed logic to TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21369
diff changeset
  1675
            os.environ['PYTHONHASHSEED'] = str(random.getrandbits(32))
97475f27bebe run-tests: move hash seed logic to TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21369
diff changeset
  1676
21369
1d0aa8bccc87 run-tests: move tmpdir calculations into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21368
diff changeset
  1677
        if self.options.tmpdir:
1d0aa8bccc87 run-tests: move tmpdir calculations into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21368
diff changeset
  1678
            self.options.keep_tmpdir = True
1d0aa8bccc87 run-tests: move tmpdir calculations into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21368
diff changeset
  1679
            tmpdir = self.options.tmpdir
1d0aa8bccc87 run-tests: move tmpdir calculations into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21368
diff changeset
  1680
            if os.path.exists(tmpdir):
1d0aa8bccc87 run-tests: move tmpdir calculations into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21368
diff changeset
  1681
                # Meaning of tmpdir has changed since 1.3: we used to create
1d0aa8bccc87 run-tests: move tmpdir calculations into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21368
diff changeset
  1682
                # HGTMP inside tmpdir; now HGTMP is tmpdir.  So fail if
1d0aa8bccc87 run-tests: move tmpdir calculations into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21368
diff changeset
  1683
                # tmpdir already exists.
25031
0adc22a0b6b3 python3: update killdaemons and run-tests print and exception syntax
Augie Fackler <augie@google.com>
parents: 24984
diff changeset
  1684
                print("error: temp dir %r already exists" % tmpdir)
21369
1d0aa8bccc87 run-tests: move tmpdir calculations into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21368
diff changeset
  1685
                return 1
1d0aa8bccc87 run-tests: move tmpdir calculations into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21368
diff changeset
  1686
1d0aa8bccc87 run-tests: move tmpdir calculations into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21368
diff changeset
  1687
                # Automatically removing tmpdir sounds convenient, but could
1d0aa8bccc87 run-tests: move tmpdir calculations into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21368
diff changeset
  1688
                # really annoy anyone in the habit of using "--tmpdir=/tmp"
1d0aa8bccc87 run-tests: move tmpdir calculations into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21368
diff changeset
  1689
                # or "--tmpdir=$HOME".
1d0aa8bccc87 run-tests: move tmpdir calculations into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21368
diff changeset
  1690
                #vlog("# Removing temp dir", tmpdir)
1d0aa8bccc87 run-tests: move tmpdir calculations into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21368
diff changeset
  1691
                #shutil.rmtree(tmpdir)
1d0aa8bccc87 run-tests: move tmpdir calculations into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21368
diff changeset
  1692
            os.makedirs(tmpdir)
1d0aa8bccc87 run-tests: move tmpdir calculations into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21368
diff changeset
  1693
        else:
1d0aa8bccc87 run-tests: move tmpdir calculations into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21368
diff changeset
  1694
            d = None
1d0aa8bccc87 run-tests: move tmpdir calculations into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21368
diff changeset
  1695
            if os.name == 'nt':
1d0aa8bccc87 run-tests: move tmpdir calculations into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21368
diff changeset
  1696
                # without this, we get the default temp dir location, but
1d0aa8bccc87 run-tests: move tmpdir calculations into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21368
diff changeset
  1697
                # in all lowercase, which causes troubles with paths (issue3490)
1d0aa8bccc87 run-tests: move tmpdir calculations into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21368
diff changeset
  1698
                d = os.getenv('TMP')
1d0aa8bccc87 run-tests: move tmpdir calculations into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21368
diff changeset
  1699
            tmpdir = tempfile.mkdtemp('', 'hgtests.', d)
21534
3ece55d16044 run-tests: make attributes of TestRunner internal
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21533
diff changeset
  1700
        self._hgtmp = os.environ['HGTMP'] = os.path.realpath(tmpdir)
21369
1d0aa8bccc87 run-tests: move tmpdir calculations into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21368
diff changeset
  1701
21368
a884548f5421 run-tests: move more path calculations into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21367
diff changeset
  1702
        if self.options.with_hg:
21534
3ece55d16044 run-tests: make attributes of TestRunner internal
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21533
diff changeset
  1703
            self._installdir = None
3ece55d16044 run-tests: make attributes of TestRunner internal
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21533
diff changeset
  1704
            self._bindir = os.path.dirname(os.path.realpath(
3ece55d16044 run-tests: make attributes of TestRunner internal
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21533
diff changeset
  1705
                                           self.options.with_hg))
3ece55d16044 run-tests: make attributes of TestRunner internal
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21533
diff changeset
  1706
            self._tmpbindir = os.path.join(self._hgtmp, 'install', 'bin')
3ece55d16044 run-tests: make attributes of TestRunner internal
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21533
diff changeset
  1707
            os.makedirs(self._tmpbindir)
21368
a884548f5421 run-tests: move more path calculations into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21367
diff changeset
  1708
a884548f5421 run-tests: move more path calculations into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21367
diff changeset
  1709
            # This looks redundant with how Python initializes sys.path from
a884548f5421 run-tests: move more path calculations into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21367
diff changeset
  1710
            # the location of the script being executed.  Needed because the
a884548f5421 run-tests: move more path calculations into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21367
diff changeset
  1711
            # "hg" specified by --with-hg is not the only Python script
a884548f5421 run-tests: move more path calculations into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21367
diff changeset
  1712
            # executed in the test suite that needs to import 'mercurial'
a884548f5421 run-tests: move more path calculations into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21367
diff changeset
  1713
            # ... which means it's not really redundant at all.
21534
3ece55d16044 run-tests: make attributes of TestRunner internal
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21533
diff changeset
  1714
            self._pythondir = self._bindir
21368
a884548f5421 run-tests: move more path calculations into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21367
diff changeset
  1715
        else:
21534
3ece55d16044 run-tests: make attributes of TestRunner internal
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21533
diff changeset
  1716
            self._installdir = os.path.join(self._hgtmp, "install")
3ece55d16044 run-tests: make attributes of TestRunner internal
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21533
diff changeset
  1717
            self._bindir = os.environ["BINDIR"] = \
3ece55d16044 run-tests: make attributes of TestRunner internal
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21533
diff changeset
  1718
                os.path.join(self._installdir, "bin")
3ece55d16044 run-tests: make attributes of TestRunner internal
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21533
diff changeset
  1719
            self._tmpbindir = self._bindir
3ece55d16044 run-tests: make attributes of TestRunner internal
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21533
diff changeset
  1720
            self._pythondir = os.path.join(self._installdir, "lib", "python")
21368
a884548f5421 run-tests: move more path calculations into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21367
diff changeset
  1721
21534
3ece55d16044 run-tests: make attributes of TestRunner internal
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21533
diff changeset
  1722
        os.environ["BINDIR"] = self._bindir
21368
a884548f5421 run-tests: move more path calculations into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21367
diff changeset
  1723
        os.environ["PYTHON"] = PYTHON
a884548f5421 run-tests: move more path calculations into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21367
diff changeset
  1724
23859
661b246bf1c4 run-tests: include testdir in $PATH so tests easily can use helper tools
Mads Kiilerich <madski@unity3d.com>
parents: 23728
diff changeset
  1725
        runtestdir = os.path.abspath(os.path.dirname(__file__))
661b246bf1c4 run-tests: include testdir in $PATH so tests easily can use helper tools
Mads Kiilerich <madski@unity3d.com>
parents: 23728
diff changeset
  1726
        path = [self._bindir, runtestdir] + os.environ["PATH"].split(os.pathsep)
24742
39ee0444e27c run-tests: also follow symlink when update PATH with 'run-tests.py' dir
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24516
diff changeset
  1727
        if os.path.islink(__file__):
39ee0444e27c run-tests: also follow symlink when update PATH with 'run-tests.py' dir
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24516
diff changeset
  1728
            # test helper will likely be at the end of the symlink
39ee0444e27c run-tests: also follow symlink when update PATH with 'run-tests.py' dir
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24516
diff changeset
  1729
            realfile = os.path.realpath(__file__)
39ee0444e27c run-tests: also follow symlink when update PATH with 'run-tests.py' dir
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24516
diff changeset
  1730
            realdir = os.path.abspath(os.path.dirname(realfile))
39ee0444e27c run-tests: also follow symlink when update PATH with 'run-tests.py' dir
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24516
diff changeset
  1731
            path.insert(2, realdir)
21534
3ece55d16044 run-tests: make attributes of TestRunner internal
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21533
diff changeset
  1732
        if self._tmpbindir != self._bindir:
3ece55d16044 run-tests: make attributes of TestRunner internal
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21533
diff changeset
  1733
            path = [self._tmpbindir] + path
21368
a884548f5421 run-tests: move more path calculations into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21367
diff changeset
  1734
        os.environ["PATH"] = os.pathsep.join(path)
a884548f5421 run-tests: move more path calculations into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21367
diff changeset
  1735
21367
522e3d24a461 run-tests: move pypath manipulation into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21366
diff changeset
  1736
        # Include TESTDIR in PYTHONPATH so that out-of-tree extensions
522e3d24a461 run-tests: move pypath manipulation into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21366
diff changeset
  1737
        # can run .../tests/run-tests.py test-foo where test-foo
522e3d24a461 run-tests: move pypath manipulation into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21366
diff changeset
  1738
        # adds an extension to HGRC. Also include run-test.py directory to
522e3d24a461 run-tests: move pypath manipulation into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21366
diff changeset
  1739
        # import modules like heredoctest.
23859
661b246bf1c4 run-tests: include testdir in $PATH so tests easily can use helper tools
Mads Kiilerich <madski@unity3d.com>
parents: 23728
diff changeset
  1740
        pypath = [self._pythondir, self._testdir, runtestdir]
21367
522e3d24a461 run-tests: move pypath manipulation into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21366
diff changeset
  1741
        # We have to augment PYTHONPATH, rather than simply replacing
522e3d24a461 run-tests: move pypath manipulation into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21366
diff changeset
  1742
        # it, in case external libraries are only available via current
522e3d24a461 run-tests: move pypath manipulation into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21366
diff changeset
  1743
        # PYTHONPATH.  (In particular, the Subversion bindings on OS X
522e3d24a461 run-tests: move pypath manipulation into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21366
diff changeset
  1744
        # are in /opt/subversion.)
522e3d24a461 run-tests: move pypath manipulation into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21366
diff changeset
  1745
        oldpypath = os.environ.get(IMPL_PATH)
522e3d24a461 run-tests: move pypath manipulation into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21366
diff changeset
  1746
        if oldpypath:
522e3d24a461 run-tests: move pypath manipulation into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21366
diff changeset
  1747
            pypath.append(oldpypath)
522e3d24a461 run-tests: move pypath manipulation into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21366
diff changeset
  1748
        os.environ[IMPL_PATH] = os.pathsep.join(pypath)
522e3d24a461 run-tests: move pypath manipulation into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21366
diff changeset
  1749
23935
d64dd1252386 run-tests.py: inherit --pure option from outer run-tests.py execution
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23933
diff changeset
  1750
        if self.options.pure:
d64dd1252386 run-tests.py: inherit --pure option from outer run-tests.py execution
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23933
diff changeset
  1751
            os.environ["HGTEST_RUN_TESTS_PURE"] = "--pure"
d64dd1252386 run-tests.py: inherit --pure option from outer run-tests.py execution
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23933
diff changeset
  1752
21534
3ece55d16044 run-tests: make attributes of TestRunner internal
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21533
diff changeset
  1753
        self._coveragefile = os.path.join(self._testdir, '.coverage')
21367
522e3d24a461 run-tests: move pypath manipulation into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21366
diff changeset
  1754
21534
3ece55d16044 run-tests: make attributes of TestRunner internal
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21533
diff changeset
  1755
        vlog("# Using TESTDIR", self._testdir)
3ece55d16044 run-tests: make attributes of TestRunner internal
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21533
diff changeset
  1756
        vlog("# Using HGTMP", self._hgtmp)
21366
5047248536c5 run-tests: establish TestRunner.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21365
diff changeset
  1757
        vlog("# Using PATH", os.environ["PATH"])
5047248536c5 run-tests: establish TestRunner.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21365
diff changeset
  1758
        vlog("# Using", IMPL_PATH, os.environ[IMPL_PATH])
5047248536c5 run-tests: establish TestRunner.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21365
diff changeset
  1759
5047248536c5 run-tests: establish TestRunner.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21365
diff changeset
  1760
        try:
5047248536c5 run-tests: establish TestRunner.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21365
diff changeset
  1761
            return self._runtests(tests) or 0
5047248536c5 run-tests: establish TestRunner.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21365
diff changeset
  1762
        finally:
5047248536c5 run-tests: establish TestRunner.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21365
diff changeset
  1763
            time.sleep(.1)
5047248536c5 run-tests: establish TestRunner.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21365
diff changeset
  1764
            self._cleanup()
5047248536c5 run-tests: establish TestRunner.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21365
diff changeset
  1765
21363
00e5f5b9fc90 run-tests: move test discovery logic into a function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21362
diff changeset
  1766
    def findtests(self, args):
00e5f5b9fc90 run-tests: move test discovery logic into a function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21362
diff changeset
  1767
        """Finds possible test files from arguments.
00e5f5b9fc90 run-tests: move test discovery logic into a function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21362
diff changeset
  1768
00e5f5b9fc90 run-tests: move test discovery logic into a function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21362
diff changeset
  1769
        If you wish to inject custom tests into the test harness, this would
00e5f5b9fc90 run-tests: move test discovery logic into a function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21362
diff changeset
  1770
        be a good function to monkeypatch or override in a derived class.
00e5f5b9fc90 run-tests: move test discovery logic into a function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21362
diff changeset
  1771
        """
00e5f5b9fc90 run-tests: move test discovery logic into a function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21362
diff changeset
  1772
        if not args:
00e5f5b9fc90 run-tests: move test discovery logic into a function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21362
diff changeset
  1773
            if self.options.changed:
00e5f5b9fc90 run-tests: move test discovery logic into a function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21362
diff changeset
  1774
                proc = Popen4('hg st --rev "%s" -man0 .' %
00e5f5b9fc90 run-tests: move test discovery logic into a function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21362
diff changeset
  1775
                              self.options.changed, None, 0)
00e5f5b9fc90 run-tests: move test discovery logic into a function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21362
diff changeset
  1776
                stdout, stderr = proc.communicate()
00e5f5b9fc90 run-tests: move test discovery logic into a function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21362
diff changeset
  1777
                args = stdout.strip('\0').split('\0')
00e5f5b9fc90 run-tests: move test discovery logic into a function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21362
diff changeset
  1778
            else:
00e5f5b9fc90 run-tests: move test discovery logic into a function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21362
diff changeset
  1779
                args = os.listdir('.')
00e5f5b9fc90 run-tests: move test discovery logic into a function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21362
diff changeset
  1780
00e5f5b9fc90 run-tests: move test discovery logic into a function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21362
diff changeset
  1781
        return [t for t in args
00e5f5b9fc90 run-tests: move test discovery logic into a function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21362
diff changeset
  1782
                if os.path.basename(t).startswith('test-')
00e5f5b9fc90 run-tests: move test discovery logic into a function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21362
diff changeset
  1783
                    and (t.endswith('.py') or t.endswith('.t'))]
00e5f5b9fc90 run-tests: move test discovery logic into a function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21362
diff changeset
  1784
21366
5047248536c5 run-tests: establish TestRunner.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21365
diff changeset
  1785
    def _runtests(self, tests):
21360
becce297ae0c run-tests: move runtests() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21359
diff changeset
  1786
        try:
21534
3ece55d16044 run-tests: make attributes of TestRunner internal
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21533
diff changeset
  1787
            if self._installdir:
21378
f7ac3c63d844 run-tests: make some methods of TestRunner internal
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21377
diff changeset
  1788
                self._installhg()
f7ac3c63d844 run-tests: make some methods of TestRunner internal
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21377
diff changeset
  1789
                self._checkhglib("Testing")
21360
becce297ae0c run-tests: move runtests() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21359
diff changeset
  1790
            else:
21378
f7ac3c63d844 run-tests: make some methods of TestRunner internal
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21377
diff changeset
  1791
                self._usecorrectpython()
21360
becce297ae0c run-tests: move runtests() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21359
diff changeset
  1792
becce297ae0c run-tests: move runtests() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21359
diff changeset
  1793
            if self.options.restart:
becce297ae0c run-tests: move runtests() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21359
diff changeset
  1794
                orig = list(tests)
becce297ae0c run-tests: move runtests() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21359
diff changeset
  1795
                while tests:
becce297ae0c run-tests: move runtests() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21359
diff changeset
  1796
                    if os.path.exists(tests[0] + ".err"):
becce297ae0c run-tests: move runtests() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21359
diff changeset
  1797
                        break
becce297ae0c run-tests: move runtests() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21359
diff changeset
  1798
                    tests.pop(0)
becce297ae0c run-tests: move runtests() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21359
diff changeset
  1799
                if not tests:
25031
0adc22a0b6b3 python3: update killdaemons and run-tests print and exception syntax
Augie Fackler <augie@google.com>
parents: 24984
diff changeset
  1800
                    print("running all tests")
21360
becce297ae0c run-tests: move runtests() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21359
diff changeset
  1801
                    tests = orig
becce297ae0c run-tests: move runtests() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21359
diff changeset
  1802
21464
d19164a018a1 run-tests: execute tests via unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21463
diff changeset
  1803
            tests = [self._gettest(t, i) for i, t in enumerate(tests)]
21437
d9532be2fc4d run-tests: pass Test instances into TestRunner._executetests()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21436
diff changeset
  1804
21458
c42219733f30 run-tests: don't print results in unittest mode
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21457
diff changeset
  1805
            failed = False
c42219733f30 run-tests: don't print results in unittest mode
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21457
diff changeset
  1806
            warned = False
c42219733f30 run-tests: don't print results in unittest mode
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21457
diff changeset
  1807
21534
3ece55d16044 run-tests: make attributes of TestRunner internal
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21533
diff changeset
  1808
            suite = TestSuite(self._testdir,
21533
aecac8059c00 run-tests: make testdir an argument of TestSuite.__init__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21532
diff changeset
  1809
                              jobs=self.options.jobs,
21529
117e027390ab run-tests: move whitelist and blacklist to named arguments of TestSuite
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21528
diff changeset
  1810
                              whitelist=self.options.whitelisted,
117e027390ab run-tests: move whitelist and blacklist to named arguments of TestSuite
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21528
diff changeset
  1811
                              blacklist=self.options.blacklist,
21530
78289625e986 run-tests: make retest a named argument of TestSuite.__init__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21529
diff changeset
  1812
                              retest=self.options.retest,
21531
7fcda22acc43 run-tests: make keywords a named argument to TestSuite.__init__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21530
diff changeset
  1813
                              keywords=self.options.keywords,
21532
9d2ba7e2324d run-tests: move loop to a named argument of TestSuite.__init__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21531
diff changeset
  1814
                              loop=self.options.loop,
24329
e7ca4d4b10e1 run-tests: add --runs-per-test flag
Augie Fackler <augie@google.com>
parents: 24306
diff changeset
  1815
                              runs_per_test=self.options.runs_per_test,
24330
799bc18e14d1 run-tests: avoid running the same test instance concurrently
Augie Fackler <augie@google.com>
parents: 24329
diff changeset
  1816
                              tests=tests, loadtest=self._gettest)
21464
d19164a018a1 run-tests: execute tests via unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21463
diff changeset
  1817
            verbosity = 1
d19164a018a1 run-tests: execute tests via unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21463
diff changeset
  1818
            if self.options.verbose:
d19164a018a1 run-tests: execute tests via unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21463
diff changeset
  1819
                verbosity = 2
d19164a018a1 run-tests: execute tests via unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21463
diff changeset
  1820
            runner = TextTestRunner(self, verbosity=verbosity)
21613
b3213b9fafed run-tests: exit with non-0 exit code when tests fail or warn
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21539
diff changeset
  1821
            result = runner.run(suite)
b3213b9fafed run-tests: exit with non-0 exit code when tests fail or warn
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21539
diff changeset
  1822
b3213b9fafed run-tests: exit with non-0 exit code when tests fail or warn
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21539
diff changeset
  1823
            if result.failures:
b3213b9fafed run-tests: exit with non-0 exit code when tests fail or warn
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21539
diff changeset
  1824
                failed = True
b3213b9fafed run-tests: exit with non-0 exit code when tests fail or warn
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21539
diff changeset
  1825
            if result.warned:
b3213b9fafed run-tests: exit with non-0 exit code when tests fail or warn
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21539
diff changeset
  1826
                warned = True
21360
becce297ae0c run-tests: move runtests() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21359
diff changeset
  1827
becce297ae0c run-tests: move runtests() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21359
diff changeset
  1828
            if self.options.anycoverage:
21378
f7ac3c63d844 run-tests: make some methods of TestRunner internal
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21377
diff changeset
  1829
                self._outputcoverage()
21360
becce297ae0c run-tests: move runtests() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21359
diff changeset
  1830
        except KeyboardInterrupt:
becce297ae0c run-tests: move runtests() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21359
diff changeset
  1831
            failed = True
25031
0adc22a0b6b3 python3: update killdaemons and run-tests print and exception syntax
Augie Fackler <augie@google.com>
parents: 24984
diff changeset
  1832
            print("\ninterrupted!")
21360
becce297ae0c run-tests: move runtests() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21359
diff changeset
  1833
becce297ae0c run-tests: move runtests() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21359
diff changeset
  1834
        if failed:
becce297ae0c run-tests: move runtests() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21359
diff changeset
  1835
            return 1
becce297ae0c run-tests: move runtests() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21359
diff changeset
  1836
        if warned:
becce297ae0c run-tests: move runtests() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21359
diff changeset
  1837
            return 80
becce297ae0c run-tests: move runtests() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21359
diff changeset
  1838
24967
00790cc2b753 run-test: ensure the test ports are available before launching test
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24965
diff changeset
  1839
    def _getport(self, count):
00790cc2b753 run-test: ensure the test ports are available before launching test
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24965
diff changeset
  1840
        port = self._ports.get(count) # do we have a cached entry?
00790cc2b753 run-test: ensure the test ports are available before launching test
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24965
diff changeset
  1841
        if port is None:
00790cc2b753 run-test: ensure the test ports are available before launching test
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24965
diff changeset
  1842
            port = self.options.port + self._portoffset
00790cc2b753 run-test: ensure the test ports are available before launching test
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24965
diff changeset
  1843
            portneeded = 3
00790cc2b753 run-test: ensure the test ports are available before launching test
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24965
diff changeset
  1844
            # above 100 tries we just give up and let test reports failure
00790cc2b753 run-test: ensure the test ports are available before launching test
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24965
diff changeset
  1845
            for tries in xrange(100):
00790cc2b753 run-test: ensure the test ports are available before launching test
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24965
diff changeset
  1846
                allfree = True
00790cc2b753 run-test: ensure the test ports are available before launching test
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24965
diff changeset
  1847
                for idx in xrange(portneeded):
00790cc2b753 run-test: ensure the test ports are available before launching test
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24965
diff changeset
  1848
                    if not checkportisavailable(port + idx):
00790cc2b753 run-test: ensure the test ports are available before launching test
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24965
diff changeset
  1849
                        allfree = False
00790cc2b753 run-test: ensure the test ports are available before launching test
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24965
diff changeset
  1850
                        break
00790cc2b753 run-test: ensure the test ports are available before launching test
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24965
diff changeset
  1851
                self._portoffset += portneeded
00790cc2b753 run-test: ensure the test ports are available before launching test
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24965
diff changeset
  1852
                if allfree:
00790cc2b753 run-test: ensure the test ports are available before launching test
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24965
diff changeset
  1853
                    break
00790cc2b753 run-test: ensure the test ports are available before launching test
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24965
diff changeset
  1854
            self._ports[count] = port
00790cc2b753 run-test: ensure the test ports are available before launching test
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24965
diff changeset
  1855
        return port
00790cc2b753 run-test: ensure the test ports are available before launching test
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24965
diff changeset
  1856
21464
d19164a018a1 run-tests: execute tests via unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21463
diff changeset
  1857
    def _gettest(self, test, count):
21357
4c4f64b8df3c run-tests: move gettest() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21356
diff changeset
  1858
        """Obtain a Test by looking at its filename.
4c4f64b8df3c run-tests: move gettest() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21356
diff changeset
  1859
4c4f64b8df3c run-tests: move gettest() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21356
diff changeset
  1860
        Returns a Test instance. The Test may not be runnable if it doesn't
4c4f64b8df3c run-tests: move gettest() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21356
diff changeset
  1861
        map to a known type.
4c4f64b8df3c run-tests: move gettest() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21356
diff changeset
  1862
        """
4c4f64b8df3c run-tests: move gettest() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21356
diff changeset
  1863
        lctest = test.lower()
4c4f64b8df3c run-tests: move gettest() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21356
diff changeset
  1864
        testcls = Test
4c4f64b8df3c run-tests: move gettest() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21356
diff changeset
  1865
21501
98a0c58ee200 run-tests: factor refpath into Test classes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21500
diff changeset
  1866
        for ext, cls in self.TESTTYPES:
21357
4c4f64b8df3c run-tests: move gettest() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21356
diff changeset
  1867
            if lctest.endswith(ext):
4c4f64b8df3c run-tests: move gettest() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21356
diff changeset
  1868
                testcls = cls
4c4f64b8df3c run-tests: move gettest() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21356
diff changeset
  1869
                break
4c4f64b8df3c run-tests: move gettest() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21356
diff changeset
  1870
21534
3ece55d16044 run-tests: make attributes of TestRunner internal
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21533
diff changeset
  1871
        refpath = os.path.join(self._testdir, test)
3ece55d16044 run-tests: make attributes of TestRunner internal
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21533
diff changeset
  1872
        tmpdir = os.path.join(self._hgtmp, 'child%d' % count)
21504
888a5dfe1569 run-tests: pass temp dir into Test.__init__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21502
diff changeset
  1873
24330
799bc18e14d1 run-tests: avoid running the same test instance concurrently
Augie Fackler <augie@google.com>
parents: 24329
diff changeset
  1874
        t = testcls(refpath, tmpdir,
799bc18e14d1 run-tests: avoid running the same test instance concurrently
Augie Fackler <augie@google.com>
parents: 24329
diff changeset
  1875
                    keeptmpdir=self.options.keep_tmpdir,
799bc18e14d1 run-tests: avoid running the same test instance concurrently
Augie Fackler <augie@google.com>
parents: 24329
diff changeset
  1876
                    debug=self.options.debug,
799bc18e14d1 run-tests: avoid running the same test instance concurrently
Augie Fackler <augie@google.com>
parents: 24329
diff changeset
  1877
                    timeout=self.options.timeout,
24967
00790cc2b753 run-test: ensure the test ports are available before launching test
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24965
diff changeset
  1878
                    startport=self._getport(count),
24330
799bc18e14d1 run-tests: avoid running the same test instance concurrently
Augie Fackler <augie@google.com>
parents: 24329
diff changeset
  1879
                    extraconfigopts=self.options.extra_config_opt,
799bc18e14d1 run-tests: avoid running the same test instance concurrently
Augie Fackler <augie@google.com>
parents: 24329
diff changeset
  1880
                    py3kwarnings=self.options.py3k_warnings,
799bc18e14d1 run-tests: avoid running the same test instance concurrently
Augie Fackler <augie@google.com>
parents: 24329
diff changeset
  1881
                    shell=self.options.shell)
799bc18e14d1 run-tests: avoid running the same test instance concurrently
Augie Fackler <augie@google.com>
parents: 24329
diff changeset
  1882
        t.should_reload = True
799bc18e14d1 run-tests: avoid running the same test instance concurrently
Augie Fackler <augie@google.com>
parents: 24329
diff changeset
  1883
        return t
21357
4c4f64b8df3c run-tests: move gettest() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21356
diff changeset
  1884
21366
5047248536c5 run-tests: establish TestRunner.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21365
diff changeset
  1885
    def _cleanup(self):
21350
dfcef61f5bd4 run-tests: move cleanup() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21349
diff changeset
  1886
        """Clean up state from this test invocation."""
dfcef61f5bd4 run-tests: move cleanup() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21349
diff changeset
  1887
dfcef61f5bd4 run-tests: move cleanup() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21349
diff changeset
  1888
        if self.options.keep_tmpdir:
dfcef61f5bd4 run-tests: move cleanup() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21349
diff changeset
  1889
            return
dfcef61f5bd4 run-tests: move cleanup() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21349
diff changeset
  1890
21534
3ece55d16044 run-tests: make attributes of TestRunner internal
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21533
diff changeset
  1891
        vlog("# Cleaning up HGTMP", self._hgtmp)
3ece55d16044 run-tests: make attributes of TestRunner internal
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21533
diff changeset
  1892
        shutil.rmtree(self._hgtmp, True)
21352
39fd89fbbc3c run-tests: move createdfiles out of a global and into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21351
diff changeset
  1893
        for f in self._createdfiles:
21350
dfcef61f5bd4 run-tests: move cleanup() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21349
diff changeset
  1894
            try:
dfcef61f5bd4 run-tests: move cleanup() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21349
diff changeset
  1895
                os.remove(f)
dfcef61f5bd4 run-tests: move cleanup() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21349
diff changeset
  1896
            except OSError:
dfcef61f5bd4 run-tests: move cleanup() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21349
diff changeset
  1897
                pass
dfcef61f5bd4 run-tests: move cleanup() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21349
diff changeset
  1898
21378
f7ac3c63d844 run-tests: make some methods of TestRunner internal
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21377
diff changeset
  1899
    def _usecorrectpython(self):
21536
92a6b16c9186 run-tests: add docstrings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21535
diff changeset
  1900
        """Configure the environment to use the appropriate Python in tests."""
92a6b16c9186 run-tests: add docstrings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21535
diff changeset
  1901
        # Tests must use the same interpreter as us or bad things will happen.
21351
fe5647506565 run-tests: move usecorrectpython() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21350
diff changeset
  1902
        pyexename = sys.platform == 'win32' and 'python.exe' or 'python'
fe5647506565 run-tests: move usecorrectpython() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21350
diff changeset
  1903
        if getattr(os, 'symlink', None):
fe5647506565 run-tests: move usecorrectpython() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21350
diff changeset
  1904
            vlog("# Making python executable in test path a symlink to '%s'" %
fe5647506565 run-tests: move usecorrectpython() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21350
diff changeset
  1905
                 sys.executable)
21534
3ece55d16044 run-tests: make attributes of TestRunner internal
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21533
diff changeset
  1906
            mypython = os.path.join(self._tmpbindir, pyexename)
21351
fe5647506565 run-tests: move usecorrectpython() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21350
diff changeset
  1907
            try:
fe5647506565 run-tests: move usecorrectpython() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21350
diff changeset
  1908
                if os.readlink(mypython) == sys.executable:
fe5647506565 run-tests: move usecorrectpython() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21350
diff changeset
  1909
                    return
fe5647506565 run-tests: move usecorrectpython() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21350
diff changeset
  1910
                os.unlink(mypython)
25031
0adc22a0b6b3 python3: update killdaemons and run-tests print and exception syntax
Augie Fackler <augie@google.com>
parents: 24984
diff changeset
  1911
            except OSError as err:
21351
fe5647506565 run-tests: move usecorrectpython() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21350
diff changeset
  1912
                if err.errno != errno.ENOENT:
fe5647506565 run-tests: move usecorrectpython() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21350
diff changeset
  1913
                    raise
21365
10cf9054d941 run-tests: move program searching into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21364
diff changeset
  1914
            if self._findprogram(pyexename) != sys.executable:
21351
fe5647506565 run-tests: move usecorrectpython() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21350
diff changeset
  1915
                try:
fe5647506565 run-tests: move usecorrectpython() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21350
diff changeset
  1916
                    os.symlink(sys.executable, mypython)
21352
39fd89fbbc3c run-tests: move createdfiles out of a global and into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21351
diff changeset
  1917
                    self._createdfiles.append(mypython)
25031
0adc22a0b6b3 python3: update killdaemons and run-tests print and exception syntax
Augie Fackler <augie@google.com>
parents: 24984
diff changeset
  1918
                except OSError as err:
21351
fe5647506565 run-tests: move usecorrectpython() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21350
diff changeset
  1919
                    # child processes may race, which is harmless
fe5647506565 run-tests: move usecorrectpython() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21350
diff changeset
  1920
                    if err.errno != errno.EEXIST:
fe5647506565 run-tests: move usecorrectpython() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21350
diff changeset
  1921
                        raise
fe5647506565 run-tests: move usecorrectpython() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21350
diff changeset
  1922
        else:
fe5647506565 run-tests: move usecorrectpython() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21350
diff changeset
  1923
            exedir, exename = os.path.split(sys.executable)
fe5647506565 run-tests: move usecorrectpython() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21350
diff changeset
  1924
            vlog("# Modifying search path to find %s as %s in '%s'" %
fe5647506565 run-tests: move usecorrectpython() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21350
diff changeset
  1925
                 (exename, pyexename, exedir))
fe5647506565 run-tests: move usecorrectpython() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21350
diff changeset
  1926
            path = os.environ['PATH'].split(os.pathsep)
fe5647506565 run-tests: move usecorrectpython() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21350
diff changeset
  1927
            while exedir in path:
fe5647506565 run-tests: move usecorrectpython() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21350
diff changeset
  1928
                path.remove(exedir)
fe5647506565 run-tests: move usecorrectpython() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21350
diff changeset
  1929
            os.environ['PATH'] = os.pathsep.join([exedir] + path)
21365
10cf9054d941 run-tests: move program searching into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21364
diff changeset
  1930
            if not self._findprogram(pyexename):
25031
0adc22a0b6b3 python3: update killdaemons and run-tests print and exception syntax
Augie Fackler <augie@google.com>
parents: 24984
diff changeset
  1931
                print("WARNING: Cannot find %s in search path" % pyexename)
21351
fe5647506565 run-tests: move usecorrectpython() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21350
diff changeset
  1932
21378
f7ac3c63d844 run-tests: make some methods of TestRunner internal
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21377
diff changeset
  1933
    def _installhg(self):
21536
92a6b16c9186 run-tests: add docstrings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21535
diff changeset
  1934
        """Install hg into the test environment.
92a6b16c9186 run-tests: add docstrings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21535
diff changeset
  1935
92a6b16c9186 run-tests: add docstrings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21535
diff changeset
  1936
        This will also configure hg with the appropriate testing settings.
92a6b16c9186 run-tests: add docstrings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21535
diff changeset
  1937
        """
21353
a42a5195a182 run-tests: move installhg() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21352
diff changeset
  1938
        vlog("# Performing temporary installation of HG")
a42a5195a182 run-tests: move installhg() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21352
diff changeset
  1939
        installerrs = os.path.join("tests", "install.err")
a42a5195a182 run-tests: move installhg() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21352
diff changeset
  1940
        compiler = ''
a42a5195a182 run-tests: move installhg() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21352
diff changeset
  1941
        if self.options.compiler:
a42a5195a182 run-tests: move installhg() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21352
diff changeset
  1942
            compiler = '--compiler ' + self.options.compiler
24306
6ddc86eedc3b style: kill ersatz if-else ternary operators
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 24075
diff changeset
  1943
        if self.options.pure:
6ddc86eedc3b style: kill ersatz if-else ternary operators
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 24075
diff changeset
  1944
            pure = "--pure"
6ddc86eedc3b style: kill ersatz if-else ternary operators
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 24075
diff changeset
  1945
        else:
6ddc86eedc3b style: kill ersatz if-else ternary operators
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 24075
diff changeset
  1946
            pure = ""
21353
a42a5195a182 run-tests: move installhg() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21352
diff changeset
  1947
        py3 = ''
a42a5195a182 run-tests: move installhg() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21352
diff changeset
  1948
        if sys.version_info[0] == 3:
a42a5195a182 run-tests: move installhg() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21352
diff changeset
  1949
            py3 = '--c2to3'
a42a5195a182 run-tests: move installhg() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21352
diff changeset
  1950
a42a5195a182 run-tests: move installhg() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21352
diff changeset
  1951
        # Run installer in hg root
a42a5195a182 run-tests: move installhg() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21352
diff changeset
  1952
        script = os.path.realpath(sys.argv[0])
a42a5195a182 run-tests: move installhg() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21352
diff changeset
  1953
        hgroot = os.path.dirname(os.path.dirname(script))
24506
60bbb4079c28 run-tests: report code coverage from source directory
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24505
diff changeset
  1954
        self._hgroot = hgroot
21353
a42a5195a182 run-tests: move installhg() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21352
diff changeset
  1955
        os.chdir(hgroot)
a42a5195a182 run-tests: move installhg() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21352
diff changeset
  1956
        nohome = '--home=""'
a42a5195a182 run-tests: move installhg() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21352
diff changeset
  1957
        if os.name == 'nt':
a42a5195a182 run-tests: move installhg() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21352
diff changeset
  1958
            # The --home="" trick works only on OS where os.sep == '/'
a42a5195a182 run-tests: move installhg() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21352
diff changeset
  1959
            # because of a distutils convert_path() fast-path. Avoid it at
a42a5195a182 run-tests: move installhg() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21352
diff changeset
  1960
            # least on Windows for now, deal with .pydistutils.cfg bugs
a42a5195a182 run-tests: move installhg() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21352
diff changeset
  1961
            # when they happen.
a42a5195a182 run-tests: move installhg() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21352
diff changeset
  1962
            nohome = ''
a42a5195a182 run-tests: move installhg() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21352
diff changeset
  1963
        cmd = ('%(exe)s setup.py %(py3)s %(pure)s clean --all'
a42a5195a182 run-tests: move installhg() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21352
diff changeset
  1964
               ' build %(compiler)s --build-base="%(base)s"'
a42a5195a182 run-tests: move installhg() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21352
diff changeset
  1965
               ' install --force --prefix="%(prefix)s"'
a42a5195a182 run-tests: move installhg() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21352
diff changeset
  1966
               ' --install-lib="%(libdir)s"'
a42a5195a182 run-tests: move installhg() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21352
diff changeset
  1967
               ' --install-scripts="%(bindir)s" %(nohome)s >%(logfile)s 2>&1'
a42a5195a182 run-tests: move installhg() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21352
diff changeset
  1968
               % {'exe': sys.executable, 'py3': py3, 'pure': pure,
a42a5195a182 run-tests: move installhg() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21352
diff changeset
  1969
                  'compiler': compiler,
21534
3ece55d16044 run-tests: make attributes of TestRunner internal
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21533
diff changeset
  1970
                  'base': os.path.join(self._hgtmp, "build"),
3ece55d16044 run-tests: make attributes of TestRunner internal
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21533
diff changeset
  1971
                  'prefix': self._installdir, 'libdir': self._pythondir,
3ece55d16044 run-tests: make attributes of TestRunner internal
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21533
diff changeset
  1972
                  'bindir': self._bindir,
21353
a42a5195a182 run-tests: move installhg() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21352
diff changeset
  1973
                  'nohome': nohome, 'logfile': installerrs})
24075
4bf484276787 run-tests: ensure install directories exist
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24074
diff changeset
  1974
4bf484276787 run-tests: ensure install directories exist
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24074
diff changeset
  1975
        # setuptools requires install directories to exist.
4bf484276787 run-tests: ensure install directories exist
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24074
diff changeset
  1976
        def makedirs(p):
4bf484276787 run-tests: ensure install directories exist
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24074
diff changeset
  1977
            try:
4bf484276787 run-tests: ensure install directories exist
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24074
diff changeset
  1978
                os.makedirs(p)
25031
0adc22a0b6b3 python3: update killdaemons and run-tests print and exception syntax
Augie Fackler <augie@google.com>
parents: 24984
diff changeset
  1979
            except OSError as e:
24075
4bf484276787 run-tests: ensure install directories exist
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24074
diff changeset
  1980
                if e.errno != errno.EEXIST:
4bf484276787 run-tests: ensure install directories exist
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24074
diff changeset
  1981
                    raise
4bf484276787 run-tests: ensure install directories exist
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24074
diff changeset
  1982
        makedirs(self._pythondir)
4bf484276787 run-tests: ensure install directories exist
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24074
diff changeset
  1983
        makedirs(self._bindir)
4bf484276787 run-tests: ensure install directories exist
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24074
diff changeset
  1984
21353
a42a5195a182 run-tests: move installhg() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21352
diff changeset
  1985
        vlog("# Running", cmd)
a42a5195a182 run-tests: move installhg() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21352
diff changeset
  1986
        if os.system(cmd) == 0:
a42a5195a182 run-tests: move installhg() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21352
diff changeset
  1987
            if not self.options.verbose:
a42a5195a182 run-tests: move installhg() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21352
diff changeset
  1988
                os.remove(installerrs)
a42a5195a182 run-tests: move installhg() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21352
diff changeset
  1989
        else:
21916
792ebd7dc5f6 run-tests: write out scripts in binary mode
Augie Fackler <raf@durin42.com>
parents: 21763
diff changeset
  1990
            f = open(installerrs, 'rb')
21353
a42a5195a182 run-tests: move installhg() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21352
diff changeset
  1991
            for line in f:
24074
4d9c738d942f run-tests: avoid printing extra newlines from install log
Gregory Szorc <gregory.szorc@gmail.com>
parents: 23935
diff changeset
  1992
                sys.stdout.write(line)
21353
a42a5195a182 run-tests: move installhg() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21352
diff changeset
  1993
            f.close()
a42a5195a182 run-tests: move installhg() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21352
diff changeset
  1994
            sys.exit(1)
21534
3ece55d16044 run-tests: make attributes of TestRunner internal
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21533
diff changeset
  1995
        os.chdir(self._testdir)
21353
a42a5195a182 run-tests: move installhg() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21352
diff changeset
  1996
21378
f7ac3c63d844 run-tests: make some methods of TestRunner internal
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21377
diff changeset
  1997
        self._usecorrectpython()
21353
a42a5195a182 run-tests: move installhg() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21352
diff changeset
  1998
a42a5195a182 run-tests: move installhg() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21352
diff changeset
  1999
        if self.options.py3k_warnings and not self.options.anycoverage:
a42a5195a182 run-tests: move installhg() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21352
diff changeset
  2000
            vlog("# Updating hg command to enable Py3k Warnings switch")
21916
792ebd7dc5f6 run-tests: write out scripts in binary mode
Augie Fackler <raf@durin42.com>
parents: 21763
diff changeset
  2001
            f = open(os.path.join(self._bindir, 'hg'), 'rb')
21353
a42a5195a182 run-tests: move installhg() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21352
diff changeset
  2002
            lines = [line.rstrip() for line in f]
a42a5195a182 run-tests: move installhg() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21352
diff changeset
  2003
            lines[0] += ' -3'
a42a5195a182 run-tests: move installhg() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21352
diff changeset
  2004
            f.close()
21916
792ebd7dc5f6 run-tests: write out scripts in binary mode
Augie Fackler <raf@durin42.com>
parents: 21763
diff changeset
  2005
            f = open(os.path.join(self._bindir, 'hg'), 'wb')
21353
a42a5195a182 run-tests: move installhg() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21352
diff changeset
  2006
            for line in lines:
a42a5195a182 run-tests: move installhg() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21352
diff changeset
  2007
                f.write(line + '\n')
a42a5195a182 run-tests: move installhg() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21352
diff changeset
  2008
            f.close()
a42a5195a182 run-tests: move installhg() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21352
diff changeset
  2009
21534
3ece55d16044 run-tests: make attributes of TestRunner internal
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21533
diff changeset
  2010
        hgbat = os.path.join(self._bindir, 'hg.bat')
21353
a42a5195a182 run-tests: move installhg() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21352
diff changeset
  2011
        if os.path.isfile(hgbat):
a42a5195a182 run-tests: move installhg() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21352
diff changeset
  2012
            # hg.bat expects to be put in bin/scripts while run-tests.py
a42a5195a182 run-tests: move installhg() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21352
diff changeset
  2013
            # installation layout put it in bin/ directly. Fix it
a42a5195a182 run-tests: move installhg() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21352
diff changeset
  2014
            f = open(hgbat, 'rb')
a42a5195a182 run-tests: move installhg() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21352
diff changeset
  2015
            data = f.read()
a42a5195a182 run-tests: move installhg() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21352
diff changeset
  2016
            f.close()
a42a5195a182 run-tests: move installhg() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21352
diff changeset
  2017
            if '"%~dp0..\python" "%~dp0hg" %*' in data:
a42a5195a182 run-tests: move installhg() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21352
diff changeset
  2018
                data = data.replace('"%~dp0..\python" "%~dp0hg" %*',
a42a5195a182 run-tests: move installhg() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21352
diff changeset
  2019
                                    '"%~dp0python" "%~dp0hg" %*')
a42a5195a182 run-tests: move installhg() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21352
diff changeset
  2020
                f = open(hgbat, 'wb')
a42a5195a182 run-tests: move installhg() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21352
diff changeset
  2021
                f.write(data)
a42a5195a182 run-tests: move installhg() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21352
diff changeset
  2022
                f.close()
a42a5195a182 run-tests: move installhg() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21352
diff changeset
  2023
            else:
25031
0adc22a0b6b3 python3: update killdaemons and run-tests print and exception syntax
Augie Fackler <augie@google.com>
parents: 24984
diff changeset
  2024
                print('WARNING: cannot fix hg.bat reference to python.exe')
21353
a42a5195a182 run-tests: move installhg() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21352
diff changeset
  2025
a42a5195a182 run-tests: move installhg() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21352
diff changeset
  2026
        if self.options.anycoverage:
21534
3ece55d16044 run-tests: make attributes of TestRunner internal
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21533
diff changeset
  2027
            custom = os.path.join(self._testdir, 'sitecustomize.py')
3ece55d16044 run-tests: make attributes of TestRunner internal
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21533
diff changeset
  2028
            target = os.path.join(self._pythondir, 'sitecustomize.py')
21353
a42a5195a182 run-tests: move installhg() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21352
diff changeset
  2029
            vlog('# Installing coverage trigger to %s' % target)
a42a5195a182 run-tests: move installhg() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21352
diff changeset
  2030
            shutil.copyfile(custom, target)
21534
3ece55d16044 run-tests: make attributes of TestRunner internal
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21533
diff changeset
  2031
            rc = os.path.join(self._testdir, '.coveragerc')
21353
a42a5195a182 run-tests: move installhg() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21352
diff changeset
  2032
            vlog('# Installing coverage rc to %s' % rc)
a42a5195a182 run-tests: move installhg() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21352
diff changeset
  2033
            os.environ['COVERAGE_PROCESS_START'] = rc
24505
031947baf4d0 run-tests: collect aggregate code coverage
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24504
diff changeset
  2034
            covdir = os.path.join(self._installdir, '..', 'coverage')
031947baf4d0 run-tests: collect aggregate code coverage
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24504
diff changeset
  2035
            try:
031947baf4d0 run-tests: collect aggregate code coverage
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24504
diff changeset
  2036
                os.mkdir(covdir)
25031
0adc22a0b6b3 python3: update killdaemons and run-tests print and exception syntax
Augie Fackler <augie@google.com>
parents: 24984
diff changeset
  2037
            except OSError as e:
24505
031947baf4d0 run-tests: collect aggregate code coverage
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24504
diff changeset
  2038
                if e.errno != errno.EEXIST:
031947baf4d0 run-tests: collect aggregate code coverage
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24504
diff changeset
  2039
                    raise
031947baf4d0 run-tests: collect aggregate code coverage
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24504
diff changeset
  2040
031947baf4d0 run-tests: collect aggregate code coverage
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24504
diff changeset
  2041
            os.environ['COVERAGE_DIR'] = covdir
21353
a42a5195a182 run-tests: move installhg() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21352
diff changeset
  2042
21378
f7ac3c63d844 run-tests: make some methods of TestRunner internal
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21377
diff changeset
  2043
    def _checkhglib(self, verb):
21354
29629ef43d39 run-tests: move checkhglib into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21353
diff changeset
  2044
        """Ensure that the 'mercurial' package imported by python is
29629ef43d39 run-tests: move checkhglib into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21353
diff changeset
  2045
        the one we expect it to be.  If not, print a warning to stderr."""
21733
9ad11d5bcc2f run-tests: don't check for the mercurial library used when using --with-hg
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 21614
diff changeset
  2046
        if ((self._bindir == self._pythondir) and
9ad11d5bcc2f run-tests: don't check for the mercurial library used when using --with-hg
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 21614
diff changeset
  2047
            (self._bindir != self._tmpbindir)):
23139
e53f6b72a0e4 spelling: fixes from proofreading of spell checker issues
Mads Kiilerich <madski@unity3d.com>
parents: 23077
diff changeset
  2048
            # The pythondir has been inferred from --with-hg flag.
e53f6b72a0e4 spelling: fixes from proofreading of spell checker issues
Mads Kiilerich <madski@unity3d.com>
parents: 23077
diff changeset
  2049
            # We cannot expect anything sensible here.
21733
9ad11d5bcc2f run-tests: don't check for the mercurial library used when using --with-hg
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 21614
diff changeset
  2050
            return
21534
3ece55d16044 run-tests: make attributes of TestRunner internal
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21533
diff changeset
  2051
        expecthg = os.path.join(self._pythondir, 'mercurial')
21385
28414e5ac9ec run-tests: move _gethgpath() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21384
diff changeset
  2052
        actualhg = self._gethgpath()
21354
29629ef43d39 run-tests: move checkhglib into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21353
diff changeset
  2053
        if os.path.abspath(actualhg) != os.path.abspath(expecthg):
29629ef43d39 run-tests: move checkhglib into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21353
diff changeset
  2054
            sys.stderr.write('warning: %s with unexpected mercurial lib: %s\n'
29629ef43d39 run-tests: move checkhglib into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21353
diff changeset
  2055
                             '         (expected %s)\n'
29629ef43d39 run-tests: move checkhglib into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21353
diff changeset
  2056
                             % (verb, actualhg, expecthg))
21385
28414e5ac9ec run-tests: move _gethgpath() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21384
diff changeset
  2057
    def _gethgpath(self):
28414e5ac9ec run-tests: move _gethgpath() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21384
diff changeset
  2058
        """Return the path to the mercurial package that is actually found by
28414e5ac9ec run-tests: move _gethgpath() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21384
diff changeset
  2059
        the current Python interpreter."""
28414e5ac9ec run-tests: move _gethgpath() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21384
diff changeset
  2060
        if self._hgpath is not None:
28414e5ac9ec run-tests: move _gethgpath() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21384
diff changeset
  2061
            return self._hgpath
28414e5ac9ec run-tests: move _gethgpath() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21384
diff changeset
  2062
28414e5ac9ec run-tests: move _gethgpath() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21384
diff changeset
  2063
        cmd = '%s -c "import mercurial; print (mercurial.__path__[0])"'
28414e5ac9ec run-tests: move _gethgpath() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21384
diff changeset
  2064
        pipe = os.popen(cmd % PYTHON)
28414e5ac9ec run-tests: move _gethgpath() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21384
diff changeset
  2065
        try:
28414e5ac9ec run-tests: move _gethgpath() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21384
diff changeset
  2066
            self._hgpath = pipe.read().strip()
28414e5ac9ec run-tests: move _gethgpath() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21384
diff changeset
  2067
        finally:
28414e5ac9ec run-tests: move _gethgpath() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21384
diff changeset
  2068
            pipe.close()
28414e5ac9ec run-tests: move _gethgpath() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21384
diff changeset
  2069
28414e5ac9ec run-tests: move _gethgpath() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21384
diff changeset
  2070
        return self._hgpath
21354
29629ef43d39 run-tests: move checkhglib into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21353
diff changeset
  2071
21378
f7ac3c63d844 run-tests: make some methods of TestRunner internal
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21377
diff changeset
  2072
    def _outputcoverage(self):
21536
92a6b16c9186 run-tests: add docstrings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21535
diff changeset
  2073
        """Produce code coverage output."""
24504
7046ecabd9a8 run-tests: obtain code coverage via Python API
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24500
diff changeset
  2074
        from coverage import coverage
21356
f96d7dfd8cb5 run-tests: move outputcoverage() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21355
diff changeset
  2075
24504
7046ecabd9a8 run-tests: obtain code coverage via Python API
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24500
diff changeset
  2076
        vlog('# Producing coverage report')
7046ecabd9a8 run-tests: obtain code coverage via Python API
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24500
diff changeset
  2077
        # chdir is the easiest way to get short, relative paths in the
7046ecabd9a8 run-tests: obtain code coverage via Python API
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24500
diff changeset
  2078
        # output.
24506
60bbb4079c28 run-tests: report code coverage from source directory
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24505
diff changeset
  2079
        os.chdir(self._hgroot)
24505
031947baf4d0 run-tests: collect aggregate code coverage
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24504
diff changeset
  2080
        covdir = os.path.join(self._installdir, '..', 'coverage')
031947baf4d0 run-tests: collect aggregate code coverage
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24504
diff changeset
  2081
        cov = coverage(data_file=os.path.join(covdir, 'cov'))
24506
60bbb4079c28 run-tests: report code coverage from source directory
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24505
diff changeset
  2082
60bbb4079c28 run-tests: report code coverage from source directory
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24505
diff changeset
  2083
        # Map install directory paths back to source directory.
60bbb4079c28 run-tests: report code coverage from source directory
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24505
diff changeset
  2084
        cov.config.paths['srcdir'] = ['.', self._pythondir]
60bbb4079c28 run-tests: report code coverage from source directory
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24505
diff changeset
  2085
24505
031947baf4d0 run-tests: collect aggregate code coverage
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24504
diff changeset
  2086
        cov.combine()
21356
f96d7dfd8cb5 run-tests: move outputcoverage() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21355
diff changeset
  2087
24504
7046ecabd9a8 run-tests: obtain code coverage via Python API
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24500
diff changeset
  2088
        omit = [os.path.join(x, '*') for x in [self._bindir, self._testdir]]
7046ecabd9a8 run-tests: obtain code coverage via Python API
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24500
diff changeset
  2089
        cov.report(ignore_errors=True, omit=omit)
7046ecabd9a8 run-tests: obtain code coverage via Python API
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24500
diff changeset
  2090
21356
f96d7dfd8cb5 run-tests: move outputcoverage() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21355
diff changeset
  2091
        if self.options.htmlcov:
21534
3ece55d16044 run-tests: make attributes of TestRunner internal
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21533
diff changeset
  2092
            htmldir = os.path.join(self._testdir, 'htmlcov')
24504
7046ecabd9a8 run-tests: obtain code coverage via Python API
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24500
diff changeset
  2093
            cov.html_report(directory=htmldir, omit=omit)
21356
f96d7dfd8cb5 run-tests: move outputcoverage() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21355
diff changeset
  2094
        if self.options.annotate:
21534
3ece55d16044 run-tests: make attributes of TestRunner internal
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21533
diff changeset
  2095
            adir = os.path.join(self._testdir, 'annotated')
21356
f96d7dfd8cb5 run-tests: move outputcoverage() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21355
diff changeset
  2096
            if not os.path.isdir(adir):
f96d7dfd8cb5 run-tests: move outputcoverage() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21355
diff changeset
  2097
                os.mkdir(adir)
24504
7046ecabd9a8 run-tests: obtain code coverage via Python API
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24500
diff changeset
  2098
            cov.annotate(directory=adir, omit=omit)
21356
f96d7dfd8cb5 run-tests: move outputcoverage() into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21355
diff changeset
  2099
21365
10cf9054d941 run-tests: move program searching into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21364
diff changeset
  2100
    def _findprogram(self, program):
10cf9054d941 run-tests: move program searching into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21364
diff changeset
  2101
        """Search PATH for a executable program"""
25038
66da89457c47 run-tests: fix _findprogram to reliably return bytes
Augie Fackler <augie@google.com>
parents: 25037
diff changeset
  2102
        if sys.version_info[0] > 2:
66da89457c47 run-tests: fix _findprogram to reliably return bytes
Augie Fackler <augie@google.com>
parents: 25037
diff changeset
  2103
            dpb = os.defpath.encode('utf-8')
66da89457c47 run-tests: fix _findprogram to reliably return bytes
Augie Fackler <augie@google.com>
parents: 25037
diff changeset
  2104
            sepb = os.pathsep.encode('utf-8')
66da89457c47 run-tests: fix _findprogram to reliably return bytes
Augie Fackler <augie@google.com>
parents: 25037
diff changeset
  2105
        else:
66da89457c47 run-tests: fix _findprogram to reliably return bytes
Augie Fackler <augie@google.com>
parents: 25037
diff changeset
  2106
            dpb = os.defpath
66da89457c47 run-tests: fix _findprogram to reliably return bytes
Augie Fackler <augie@google.com>
parents: 25037
diff changeset
  2107
            sepb = os.pathsep
66da89457c47 run-tests: fix _findprogram to reliably return bytes
Augie Fackler <augie@google.com>
parents: 25037
diff changeset
  2108
        for p in osenvironb.get(b'PATH', dpb).split(sepb):
21365
10cf9054d941 run-tests: move program searching into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21364
diff changeset
  2109
            name = os.path.join(p, program)
10cf9054d941 run-tests: move program searching into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21364
diff changeset
  2110
            if os.name == 'nt' or os.access(name, os.X_OK):
10cf9054d941 run-tests: move program searching into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21364
diff changeset
  2111
                return name
10cf9054d941 run-tests: move program searching into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21364
diff changeset
  2112
        return None
10cf9054d941 run-tests: move program searching into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21364
diff changeset
  2113
21374
592b3d2616d7 run-tests: move checktools into TestRunner.run()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21373
diff changeset
  2114
    def _checktools(self):
21536
92a6b16c9186 run-tests: add docstrings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21535
diff changeset
  2115
        """Ensure tools required to run tests are present."""
21365
10cf9054d941 run-tests: move program searching into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21364
diff changeset
  2116
        for p in self.REQUIREDTOOLS:
10cf9054d941 run-tests: move program searching into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21364
diff changeset
  2117
            if os.name == 'nt' and not p.endswith('.exe'):
10cf9054d941 run-tests: move program searching into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21364
diff changeset
  2118
                p += '.exe'
10cf9054d941 run-tests: move program searching into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21364
diff changeset
  2119
            found = self._findprogram(p)
10cf9054d941 run-tests: move program searching into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21364
diff changeset
  2120
            if found:
10cf9054d941 run-tests: move program searching into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21364
diff changeset
  2121
                vlog("# Found prerequisite", p, "at", found)
10cf9054d941 run-tests: move program searching into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21364
diff changeset
  2122
            else:
25031
0adc22a0b6b3 python3: update killdaemons and run-tests print and exception syntax
Augie Fackler <augie@google.com>
parents: 24984
diff changeset
  2123
                print("WARNING: Did not find prerequisite tool: %s " % p)
21365
10cf9054d941 run-tests: move program searching into TestRunner
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21364
diff changeset
  2124
13347
ce07defe7d9f run-tests: loadable as module
Simon Heimberg <simohe@besonet.ch>
parents: 13031
diff changeset
  2125
if __name__ == '__main__':
21377
71081f7f9e52 run-tests: eliminate main()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21376
diff changeset
  2126
    runner = TestRunner()
22120
68a7ef4311ce run-tests: self-test on Windows needs binary streams
Matt Mackall <mpm@selenic.com>
parents: 21919
diff changeset
  2127
68a7ef4311ce run-tests: self-test on Windows needs binary streams
Matt Mackall <mpm@selenic.com>
parents: 21919
diff changeset
  2128
    try:
68a7ef4311ce run-tests: self-test on Windows needs binary streams
Matt Mackall <mpm@selenic.com>
parents: 21919
diff changeset
  2129
        import msvcrt
68a7ef4311ce run-tests: self-test on Windows needs binary streams
Matt Mackall <mpm@selenic.com>
parents: 21919
diff changeset
  2130
        msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY)
68a7ef4311ce run-tests: self-test on Windows needs binary streams
Matt Mackall <mpm@selenic.com>
parents: 21919
diff changeset
  2131
        msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
68a7ef4311ce run-tests: self-test on Windows needs binary streams
Matt Mackall <mpm@selenic.com>
parents: 21919
diff changeset
  2132
        msvcrt.setmode(sys.stderr.fileno(), os.O_BINARY)
68a7ef4311ce run-tests: self-test on Windows needs binary streams
Matt Mackall <mpm@selenic.com>
parents: 21919
diff changeset
  2133
    except ImportError:
68a7ef4311ce run-tests: self-test on Windows needs binary streams
Matt Mackall <mpm@selenic.com>
parents: 21919
diff changeset
  2134
        pass
68a7ef4311ce run-tests: self-test on Windows needs binary streams
Matt Mackall <mpm@selenic.com>
parents: 21919
diff changeset
  2135
21377
71081f7f9e52 run-tests: eliminate main()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21376
diff changeset
  2136
    sys.exit(runner.run(sys.argv[1:]))