|
| 1 | +""" |
| 2 | +Javascript test tasks |
| 3 | +""" |
| 4 | + |
| 5 | + |
| 6 | +import os |
| 7 | +import re |
| 8 | +import sys |
| 9 | + |
| 10 | +from paver.easy import cmdopts, needs, sh, task |
| 11 | + |
| 12 | +from pavelib.utils.envs import Env |
| 13 | +from pavelib.utils.test.suites import JestSnapshotTestSuite, JsTestSuite |
| 14 | +from pavelib.utils.timer import timed |
| 15 | + |
| 16 | +try: |
| 17 | + from pygments.console import colorize |
| 18 | +except ImportError: |
| 19 | + colorize = lambda color, text: text |
| 20 | + |
| 21 | +__test__ = False # do not collect |
| 22 | + |
| 23 | + |
| 24 | +@task |
| 25 | +@needs( |
| 26 | + 'pavelib.prereqs.install_node_prereqs', |
| 27 | + 'pavelib.utils.test.utils.clean_reports_dir', |
| 28 | +) |
| 29 | +@cmdopts([ |
| 30 | + ("suite=", "s", "Test suite to run"), |
| 31 | + ("mode=", "m", "dev or run"), |
| 32 | + ("coverage", "c", "Run test under coverage"), |
| 33 | + ("port=", "p", "Port to run test server on (dev mode only)"), |
| 34 | + ('skip-clean', 'C', 'skip cleaning repository before running tests'), |
| 35 | + ('skip_clean', None, 'deprecated in favor of skip-clean'), |
| 36 | +], share_with=["pavelib.utils.tests.utils.clean_reports_dir"]) |
| 37 | +@timed |
| 38 | +def test_js(options): |
| 39 | + """ |
| 40 | + Run the JavaScript tests |
| 41 | + """ |
| 42 | + mode = getattr(options, 'mode', 'run') |
| 43 | + port = None |
| 44 | + skip_clean = getattr(options, 'skip_clean', False) |
| 45 | + |
| 46 | + if mode == 'run': |
| 47 | + suite = getattr(options, 'suite', 'all') |
| 48 | + coverage = getattr(options, 'coverage', False) |
| 49 | + elif mode == 'dev': |
| 50 | + suite = getattr(options, 'suite', None) |
| 51 | + coverage = False |
| 52 | + port = getattr(options, 'port', None) |
| 53 | + else: |
| 54 | + sys.stderr.write("Invalid mode. Please choose 'dev' or 'run'.") |
| 55 | + return |
| 56 | + |
| 57 | + if (suite != 'all') and (suite not in Env.JS_TEST_ID_KEYS): |
| 58 | + sys.stderr.write( |
| 59 | + "Unknown test suite. Please choose from ({suites})\n".format( |
| 60 | + suites=", ".join(Env.JS_TEST_ID_KEYS) |
| 61 | + ) |
| 62 | + ) |
| 63 | + return |
| 64 | + |
| 65 | + if suite != 'jest-snapshot': |
| 66 | + test_suite = JsTestSuite(suite, mode=mode, with_coverage=coverage, port=port, skip_clean=skip_clean) |
| 67 | + test_suite.run() |
| 68 | + |
| 69 | + if (suite == 'jest-snapshot') or (suite == 'all'): # lint-amnesty, pylint: disable=consider-using-in |
| 70 | + test_suite = JestSnapshotTestSuite('jest') |
| 71 | + test_suite.run() |
| 72 | + |
| 73 | + |
| 74 | +@task |
| 75 | +@cmdopts([ |
| 76 | + ("suite=", "s", "Test suite to run"), |
| 77 | + ("coverage", "c", "Run test under coverage"), |
| 78 | +]) |
| 79 | +@timed |
| 80 | +def test_js_run(options): |
| 81 | + """ |
| 82 | + Run the JavaScript tests and print results to the console |
| 83 | + """ |
| 84 | + options.mode = 'run' |
| 85 | + test_js(options) |
| 86 | + |
| 87 | + |
| 88 | +@task |
| 89 | +@cmdopts([ |
| 90 | + ("suite=", "s", "Test suite to run"), |
| 91 | + ("port=", "p", "Port to run test server on"), |
| 92 | +]) |
| 93 | +@timed |
| 94 | +def test_js_dev(options): |
| 95 | + """ |
| 96 | + Run the JavaScript tests in your default browsers |
| 97 | + """ |
| 98 | + options.mode = 'dev' |
| 99 | + test_js(options) |
| 100 | + |
| 101 | + |
| 102 | +@task |
| 103 | +@needs('pavelib.prereqs.install_coverage_prereqs') |
| 104 | +@cmdopts([ |
| 105 | + ("compare-branch=", "b", "Branch to compare against, defaults to origin/master"), |
| 106 | +], share_with=['coverage']) |
| 107 | +@timed |
| 108 | +def diff_coverage(options): |
| 109 | + """ |
| 110 | + Build the diff coverage reports |
| 111 | + """ |
| 112 | + compare_branch = options.get('compare_branch', 'origin/master') |
| 113 | + |
| 114 | + # Find all coverage XML files (both Python and JavaScript) |
| 115 | + xml_reports = [] |
| 116 | + |
| 117 | + for filepath in Env.REPORT_DIR.walk(): |
| 118 | + if bool(re.match(r'^coverage.*\.xml$', filepath.basename())): |
| 119 | + xml_reports.append(filepath) |
| 120 | + |
| 121 | + if not xml_reports: |
| 122 | + err_msg = colorize( |
| 123 | + 'red', |
| 124 | + "No coverage info found. Run `paver test` before running " |
| 125 | + "`paver coverage`.\n" |
| 126 | + ) |
| 127 | + sys.stderr.write(err_msg) |
| 128 | + else: |
| 129 | + xml_report_str = ' '.join(xml_reports) |
| 130 | + diff_html_path = os.path.join(Env.REPORT_DIR, 'diff_coverage_combined.html') |
| 131 | + |
| 132 | + # Generate the diff coverage reports (HTML and console) |
| 133 | + # The --diff-range-notation parameter is a workaround for https://github.com/Bachmann1234/diff_cover/issues/153 |
| 134 | + sh( |
| 135 | + "diff-cover {xml_report_str} --diff-range-notation '..' --compare-branch={compare_branch} " |
| 136 | + "--html-report {diff_html_path}".format( |
| 137 | + xml_report_str=xml_report_str, |
| 138 | + compare_branch=compare_branch, |
| 139 | + diff_html_path=diff_html_path, |
| 140 | + ) |
| 141 | + ) |
| 142 | + |
| 143 | + print("\n") |
0 commit comments