Skip to content
This repository was archived by the owner on Oct 3, 2023. It is now read-only.

Commit ce6d020

Browse files
Added basic structure of tests
1 parent 26c75a0 commit ce6d020

9 files changed

Lines changed: 1771 additions & 56 deletions

File tree

packages/opencensus-core/package-lock.json

Lines changed: 1463 additions & 55 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/opencensus-core/package.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@
55
"main": "build/src/index.js",
66
"types": "build/src/index.d.js",
77
"scripts": {
8-
"test": "echo \"Error: no test specified\" && exit 1"
8+
"test": "npm run script run-unit-tests",
9+
"compile-all": "npm run script compile-auto",
10+
"compile-strict": "npm run script compile-auto-strict",
11+
"compile": "npm run script compile-auto compile-auto-strict",
12+
"clean": "rimraf build/*",
13+
"script": "ts-node -P ./scripts ./scripts"
914
},
1015
"keywords": [
1116
"opencensus",
@@ -27,7 +32,10 @@
2732
"@types/semver": "^5.5.0",
2833
"@types/shimmer": "^1.0.1",
2934
"@types/uuid": "^3.4.3",
35+
"gts": "^0.5.1",
36+
"ncp": "^2.0.0",
3037
"mocha": "^5.0.4",
38+
"ts-node": "^4.0.0",
3139
"typescript": "^2.7.2"
3240
},
3341
"dependencies": {
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import * as path from 'path';
2+
import { forkP } from './utils';
3+
import * as ts from 'typescript';
4+
import * as semver from 'semver';
5+
6+
export interface CompileOptions {
7+
strict: boolean;
8+
languageLevel: string;
9+
}
10+
11+
export async function compile(options: CompileOptions) {
12+
let { strict, languageLevel } = options;
13+
if (languageLevel === 'auto') {
14+
languageLevel = semver.satisfies(process.version, '>=7.5') ? 'es2017' : 'es2015';
15+
}
16+
await forkP(`node_modules/typescript/lib/tsc`, [
17+
'-p',
18+
strict ? '.' : './tsconfig.full.json',
19+
'--target',
20+
languageLevel
21+
]);
22+
}
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/**
2+
* The main entry point for cross-platform build scripts.
3+
* Usage (in repository root directory):
4+
* ts-node -P ./scripts ./scripts [step1] [step2 ... stepN]
5+
* Alias for above:
6+
* npm run script [step1] [step2 ... stepN]
7+
*/
8+
9+
const [, , ...steps] = process.argv;
10+
const {
11+
CI_PULL_REQUEST,
12+
TRACE_TEST_EXCLUDE_INTEGRATION,
13+
TRACE_SYSTEM_TEST_ENCRYPTED_CREDENTIALS_KEY,
14+
TRACE_SYSTEM_TEST_ENCRYPTED_CREDENTIALS_IV
15+
} = process.env;
16+
17+
import { checkInstall } from './check-install';
18+
import { compile } from './compile';
19+
import { encryptCredentials, decryptCredentials } from './credentials';
20+
import { initTestFixtures } from './init-test-fixtures';
21+
import { reportCoverage } from './report-coverage';
22+
import { runTests } from './run-tests';
23+
import { testNonInterference } from './test-non-interference';
24+
import { BUILD_DIRECTORY, existsP, spawnP } from './utils';
25+
26+
// The identifying string in the service account credentials file path.
27+
const keyID = 'de480e4f9023';
28+
29+
// Globs to exclude when running unit tests only.
30+
const unitTestExcludeGlobs: string[] = TRACE_TEST_EXCLUDE_INTEGRATION ? [
31+
`${BUILD_DIRECTORY}/test/plugins/test-*`,
32+
`${BUILD_DIRECTORY}/test/test-agent-stopped.js`,
33+
`${BUILD_DIRECTORY}/test/test-grpc-context.js`,
34+
`${BUILD_DIRECTORY}/test/test-mysql-pool.js`,
35+
`${BUILD_DIRECTORY}/test/test-plugins-*`,
36+
`${BUILD_DIRECTORY}/test/test-trace-web-frameworks.js`,
37+
`${BUILD_DIRECTORY}/test/test-unpatch.js`
38+
] : [];
39+
40+
/**
41+
* Sequentially runs a list of commands.
42+
*/
43+
async function run(steps: string[]) {
44+
for (const step of steps) {
45+
console.log(`> Running step: ${step}`);
46+
// If the step string is prefixed with "npm-", treat it as an "npm run"
47+
// command, and then short-circuit.
48+
if (step.indexOf('npm-') === 0) {
49+
const moduleAndArgs = step.split('-');
50+
await spawnP(
51+
'npm',
52+
[
53+
'run',
54+
moduleAndArgs.slice(1).join('-')
55+
]
56+
);
57+
continue;
58+
} else if (step.startsWith('compile-')) {
59+
const [, languageLevel, strict] = step.split('-');
60+
await compile({ strict: !!strict, languageLevel });
61+
continue;
62+
} else {
63+
switch (step) {
64+
case 'run-unit-tests':
65+
await runTests({
66+
includeGlobs: [
67+
`${BUILD_DIRECTORY}/test/test-*.js`,
68+
`${BUILD_DIRECTORY}/test/plugins/test-*.js`
69+
],
70+
excludeGlobs: unitTestExcludeGlobs,
71+
rootDir: BUILD_DIRECTORY,
72+
coverage: false,
73+
timeout: 4000
74+
});
75+
break;
76+
case 'run-unit-tests-with-coverage':
77+
await runTests({
78+
includeGlobs: [
79+
`${BUILD_DIRECTORY}/test/test-*.js`,
80+
`${BUILD_DIRECTORY}/test/plugins/test-*.js`
81+
],
82+
excludeGlobs: unitTestExcludeGlobs,
83+
rootDir: BUILD_DIRECTORY,
84+
coverage: true,
85+
timeout: 4000
86+
});
87+
break;
88+
case 'run-system-tests':
89+
await spawnP(
90+
'npm', ['install'], { cwd: 'system-test' }
91+
);
92+
if (CI_PULL_REQUEST && !(await existsP('node-team-test-d0b0be11c23d.json'))) {
93+
console.log('> Not running system tests in PRs');
94+
} else {
95+
await runTests({
96+
includeGlobs: [
97+
`system-test/*.js`,
98+
],
99+
rootDir: '.',
100+
coverage: false
101+
});
102+
}
103+
break;
104+
case 'report-coverage':
105+
await reportCoverage();
106+
break;
107+
case 'test-non-interference':
108+
await testNonInterference();
109+
break;
110+
default:
111+
console.log(`> ${step}: not found`);
112+
break;
113+
}
114+
}
115+
}
116+
}
117+
118+
run(steps).catch((err) => {
119+
console.error(err);
120+
process.exit(1);
121+
});
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import * as path from 'path';
2+
import { globP, forkP } from './utils';
3+
4+
export interface Options {
5+
includeGlobs: string[],
6+
excludeGlobs?: string[],
7+
rootDir: string,
8+
coverage?: boolean,
9+
timeout?: number
10+
}
11+
12+
export async function runTests(options: Options) {
13+
const { includeGlobs, excludeGlobs, rootDir, coverage, timeout } = options;
14+
function nodule(nodule: string) {
15+
return path.relative(rootDir, `node_modules/${nodule}`);
16+
}
17+
let testNum = 0;
18+
const excludedFiles = ([] as string[])
19+
.concat(...await Promise.all((excludeGlobs || []).map(glob => globP(glob))));
20+
const includedFiles = ([] as string[])
21+
.concat(...await Promise.all(includeGlobs.map(glob => globP(glob))));
22+
// Take the difference
23+
const files = includedFiles.filter(i => excludedFiles.indexOf(i) < 0);
24+
for (const file of files) {
25+
const moduleAndArgs = [
26+
...coverage ? [
27+
nodule('.bin/nyc'),
28+
'--reporter',
29+
'lcov',
30+
'--report-dir',
31+
`./coverage/${testNum++}`,
32+
'--exclude',
33+
'build/test/**'
34+
] : [],
35+
nodule('mocha/bin/_mocha'),
36+
'--require',
37+
'source-map-support/register',
38+
path.relative(rootDir, file),
39+
...timeout ? [
40+
'--timeout',
41+
`${timeout}`
42+
] : [
43+
'--no-timeouts'
44+
]
45+
];
46+
47+
await forkP(
48+
moduleAndArgs[0],
49+
moduleAndArgs.slice(1),
50+
{ cwd: rootDir }
51+
);
52+
}
53+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"extends": "../node_modules/gts/tsconfig-google.json",
3+
"compilerOptions": {
4+
"rootDir": "..",
5+
"outDir": "../build"
6+
},
7+
"include": [
8+
"*.ts"
9+
]
10+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { mkdir, Stats, stat, readFile, writeFile } from 'fs';
2+
import * as glob from 'glob';
3+
import { ncp } from 'ncp';
4+
import * as path from 'path';
5+
import * as pify from 'pify';
6+
import { ChildProcess, ForkOptions, fork, SpawnOptions, spawn } from 'child_process';
7+
import * as once from 'once';
8+
import * as tmp from 'tmp';
9+
10+
export const BUILD_DIRECTORY = 'build';
11+
12+
export const globP: (pattern: string) => Promise<string[]> = pify(glob);
13+
export const mkdirP: (dir: string) => Promise<void> = pify(mkdir);
14+
export const ncpP: (src: string, dest: string) => Promise<void> = pify(ncp);
15+
export const readFileP: (path: string, encoding?: string) => Promise<Buffer|string> = pify(readFile);
16+
export const writeFileP: (path: string, data: Buffer|string, encoding?: string) => Promise<void> = pify(writeFile);
17+
export const statP: (path: string) => Promise<Stats> = pify(stat);
18+
export const tmpDirP: () => Promise<string> = pify(tmp.dir);
19+
20+
export function nodule(nodule: string) {
21+
return path.relative(BUILD_DIRECTORY, `node_modules/${nodule}`);
22+
}
23+
24+
export function flatten<T>(arr: Array<Array<T>>): Array<T> {
25+
return arr.reduce((acc, e) => acc.concat(e), []);
26+
}
27+
28+
export function existsP(path: string): Promise<boolean> {
29+
return statP(path).then(
30+
() => Promise.resolve(true),
31+
() => Promise.resolve(false)
32+
);
33+
}
34+
35+
function promisifyChildProcess(childProcess: ChildProcess): Promise<void> {
36+
return new Promise((resolve, reject) => {
37+
const exit = (err?: Error) => once(() => err ? reject(err) : resolve())();
38+
childProcess.on('error', exit);
39+
childProcess.on('close', (code) => {
40+
if (code === 0) {
41+
exit();
42+
} else {
43+
exit(new Error(`Process ${childProcess.pid} exited with code ${code}.`));
44+
}
45+
});
46+
});
47+
}
48+
49+
export function spawnP(command: string, args?: string[], options?: SpawnOptions): Promise<void> {
50+
const stringifiedCommand = `\`${command}${args ? (' ' + args.join(' ')) : ''}\``;
51+
console.log(`> Running: ${stringifiedCommand}`);
52+
return promisifyChildProcess(spawn(command, args, Object.assign({
53+
stdio: 'inherit',
54+
shell: true
55+
}, options)));
56+
}
57+
58+
export function forkP(moduleName: string, args?: string[], options?: ForkOptions): Promise<void> {
59+
const stringifiedCommand = `\`${moduleName}${args ? (' ' + args.join(' ')) : ''}\``;
60+
console.log(`> Running: ${stringifiedCommand}`);
61+
return promisifyChildProcess(fork(moduleName, args, Object.assign({
62+
stdio: 'inherit'
63+
}, options)));
64+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { Trace } from '../src/trace/trace';
2+
3+
var assert = require('assert');
4+
5+
describe('Trace', function() {
6+
it('should be a Trace instance', function() {
7+
var trace = new Trace();
8+
assert.ok(trace instanceof Trace);
9+
});
10+
});
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"extends": "./node_modules/gts/tsconfig-google.json",
3+
"compilerOptions": {
4+
"rootDir": ".",
5+
"outDir": "build",
6+
"target": "es2015",
7+
"allowUnreachableCode": true,
8+
"noUnusedParameters": false,
9+
"noImplicitAny": false,
10+
"noImplicitThis": false,
11+
"noEmitOnError": false
12+
},
13+
"include": [
14+
"src/*.ts",
15+
"src/plugins/*.ts",
16+
"test/*.ts",
17+
"test/plugins/*.ts"
18+
]
19+
}

0 commit comments

Comments
 (0)