|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +/** |
| 18 | + * This script can be used with `mocha --require ...` to support skipping |
| 19 | + * tests if the current version of Node.js is too old. For example, say |
| 20 | + * a package's unit tests cannot run with Node.js 14, but the CI unit tests |
| 21 | + * run all package test with that version. |
| 22 | + * |
| 23 | + * 1. Change this in "package.json": |
| 24 | + * "test": "nyc mocha ...", |
| 25 | + * to this: |
| 26 | + * "test": "SKIP_TEST_IF_NODE_OLDER_THAN=18 nyc mocha --require '../../../scripts/skip-test-if.js' ... ", |
| 27 | + * where `SKIP_TEST_IF_NODE_OLDER_THAN` indicates the minimum Node.js major |
| 28 | + * version. |
| 29 | + * |
| 30 | + * 2. ".tav.yml" blocks should set SKIP_TEST_IF_DISABLE=true to |
| 31 | + * disable the skipping. Via this in each test block: |
| 32 | + * env: |
| 33 | + * - SKIP_TEST_IF_DISABLE=true |
| 34 | + */ |
| 35 | + |
| 36 | +function skipTestIf() { |
| 37 | + if (process.env.SKIP_TEST_IF_DISABLE) { |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + const minNodeMajor = process.env.SKIP_TEST_IF_NODE_OLDER_THAN ?? Number(process.env.SKIP_TEST_IF_NODE_OLDER_THAN); |
| 42 | + if (!minNodeMajor || isNaN(minNodeMajor)) { |
| 43 | + console.warn('skip-test-if warning: set a minimum Node.js major version via SKIP_TEST_IF_NODE_OLDER_THAN=<num>'); |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + const nodeMajor = Number(process.versions.node.split('.')[0]); |
| 48 | + if (nodeMajor < minNodeMajor) { |
| 49 | + process.stderr.write(`skip-test-if: skipping tests on old Node.js (${nodeMajor} < ${minNodeMajor})\n`); |
| 50 | + // "Skip" tests by exiting the process. Mocha is all in one process. |
| 51 | + process.exit(0); |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +skipTestIf() |
| 56 | + |
0 commit comments