@teamscale/coverage-collector 0.0.1-beta.1 → 0.0.1-beta.25

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -9,7 +9,7 @@ files in the [Teamscale Simple Coverage Format](https://docs.teamscale.com/refer
9
9
  The Teamscale JavaScript Profiler consists of this Coverage Collector and the
10
10
  [JavaScript Instrumenter](https://www.npmjs.com/package/@teamscale/javascript-instrumenter).
11
11
  More details on using them (in combination) can be found
12
- on the projects' [Github page](https://github.com/cqse/teamscale-javascript-profiler/).
12
+ in the [Teamscale Documentation](https://docs.teamscale.com/howto/recording-test-coverage-for-javascript/).
13
13
 
14
14
  The JavaScript Coverage Collector starts a server process that listens for
15
15
  code coverage information from manually or automatically exercised (tested)
@@ -32,7 +32,7 @@ yarn build
32
32
  There are several options to run the Collector. For example, via `yarn` by running
33
33
 
34
34
  ```
35
- yarn serve --port 54678 --dump-to-file=./coverage.simple
35
+ yarn collector --port 54678 --dump-to-file=./coverage.simple
36
36
  ```
37
37
 
38
38
  or via `npx` by running
@@ -49,8 +49,3 @@ for example, by setting a corresponding environment variable.
49
49
  export NODE_OPTIONS="$NODE_OPTIONS --max-old-space-size=8192"
50
50
  ```
51
51
 
52
- ## Publishing
53
-
54
- The list of files to publish is defined by the `files` attribute in `package.json`.
55
- The actual files packed by npm can be listed by running `npx npm-packlist`.
56
-
package/dist/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@teamscale/coverage-collector",
3
- "version": "0.0.1-beta.1",
3
+ "version": "0.0.1-beta.25",
4
4
  "description": "Collector for JavaScript code coverage information",
5
- "main": "dist/main.js",
6
- "bin": "dist/main.js",
7
- "types": "dist/main.d.ts",
5
+ "main": "dist/src/main.js",
6
+ "bin": "dist/src/main.js",
7
+ "types": "dist/src/main.d.ts",
8
8
  "author": "CQSE GmbH",
9
9
  "license": "Apache-2.0",
10
10
  "repository": {
@@ -12,9 +12,10 @@
12
12
  "url": "https://github.com/cqse/teamscale-javascript-profiler.git"
13
13
  },
14
14
  "scripts": {
15
+ "prepublishOnly": "yarn clean && yarn build",
15
16
  "clean": "rimraf dist tsconfig.tsbuildinfo",
16
17
  "build": "tsc",
17
- "serve": "node dist/src/main.js",
18
+ "collector": "node dist/src/main.js",
18
19
  "test": "yarn build && NODE_OPTIONS='--experimental-vm-modules' jest --forceExit --coverage --silent=true --detectOpenHandles"
19
20
  },
20
21
  "files": [
@@ -24,8 +25,12 @@
24
25
  "@cqse/commons": "^0.0.1-beta.1",
25
26
  "argparse": "^2.0.1",
26
27
  "async": "^3.2.0",
28
+ "axios": "^0.24.0",
29
+ "dotenv": "^14.1.0",
30
+ "form-data": "^4.0.0",
27
31
  "rxjs": "^7.1.0",
28
32
  "source-map": "^0.7.3",
33
+ "tmp": "^0.2.1",
29
34
  "typescript-optional": "^2.0.1",
30
35
  "winston": "^3.3.3",
31
36
  "ws": "^7.4.5"
@@ -38,6 +43,7 @@
38
43
  "@types/jest": "^27.0.1",
39
44
  "@types/node": "^15.0.1",
40
45
  "@types/source-map": "^0.5.7",
46
+ "@types/tmp": "^0.2.3",
41
47
  "@types/winston": "^2.4.4",
42
48
  "@types/ws": "^7.4.2",
43
49
  "babel-jest": "^27.2.0",
@@ -1,7 +1,8 @@
1
1
  #!/usr/bin/env node
2
+ import 'dotenv/config';
2
3
  /**
3
4
  * The main class of the Teamscale JavaScript Collector.
4
- * Used to start-up the collector for with a given configuration.
5
+ * Used to start the collector for with a given configuration.
5
6
  */
6
7
  export declare class Main {
7
8
  /**
@@ -23,9 +24,11 @@ export declare class Main {
23
24
  /**
24
25
  * Start a timer for dumping the data, depending on the configuration.
25
26
  *
26
- * @param config - The config that determines whether or not to do the timed dump.
27
+ * @param config - The config that determines whether to do the timed dump or not.
27
28
  * @param storage - The storage with the information to dump.
28
29
  * @param logger - The logger to use.
29
30
  */
30
31
  private static maybeStartDumpTimer;
32
+ private static dumpCoverage;
33
+ private static uploadToTeamscale;
31
34
  }
package/dist/src/main.js CHANGED
@@ -1,5 +1,28 @@
1
1
  #!/usr/bin/env node
2
2
  "use strict";
3
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
4
+ if (k2 === undefined) k2 = k;
5
+ var desc = Object.getOwnPropertyDescriptor(m, k);
6
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
7
+ desc = { enumerable: true, get: function() { return m[k]; } };
8
+ }
9
+ Object.defineProperty(o, k2, desc);
10
+ }) : (function(o, m, k, k2) {
11
+ if (k2 === undefined) k2 = k;
12
+ o[k2] = m[k];
13
+ }));
14
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
15
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
16
+ }) : function(o, v) {
17
+ o["default"] = v;
18
+ });
19
+ var __importStar = (this && this.__importStar) || function (mod) {
20
+ if (mod && mod.__esModule) return mod;
21
+ var result = {};
22
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
23
+ __setModuleDefault(result, mod);
24
+ return result;
25
+ };
3
26
  var __importDefault = (this && this.__importDefault) || function (mod) {
4
27
  return (mod && mod.__esModule) ? mod : { "default": mod };
5
28
  };
@@ -10,32 +33,77 @@ const argparse_1 = require("argparse");
10
33
  const winston_1 = __importDefault(require("winston"));
11
34
  const DataStorage_1 = require("./storage/DataStorage");
12
35
  const CollectingServer_1 = require("./receiver/CollectingServer");
36
+ require("dotenv/config");
37
+ const fs = __importStar(require("fs"));
38
+ const axios_1 = __importDefault(require("axios"));
39
+ const form_data_1 = __importDefault(require("form-data"));
40
+ const QueryParameters_1 = __importDefault(require("./utils/QueryParameters"));
41
+ const util_1 = require("util");
42
+ const tmp_1 = __importDefault(require("tmp"));
13
43
  /**
14
44
  * The main class of the Teamscale JavaScript Collector.
15
- * Used to start-up the collector for with a given configuration.
45
+ * Used to start the collector for with a given configuration.
16
46
  */
17
47
  class Main {
18
48
  /**
19
49
  * Construct the object for parsing the command line arguments.
20
50
  */
21
51
  static buildParser() {
52
+ var _a;
22
53
  const parser = new argparse_1.ArgumentParser({
23
54
  description: 'Collector of the Teamscale JavaScript Profiler. Collects coverage information from a' +
24
55
  '(headless) Web browser that executes code instrumented with our instrumenter.'
25
56
  });
26
57
  parser.add_argument('-v', '--version', { action: 'version', version: package_json_1.version });
27
58
  parser.add_argument('-p', '--port', { help: 'The port to receive coverage information on.', default: 54678 });
28
- parser.add_argument('-f', '--dump-to-file', { help: 'Target file', default: './coverage.simple' });
59
+ parser.add_argument('-f', '--dump-to-file', { help: 'Target file to write coverage to.' });
29
60
  parser.add_argument('-l', '--log-to-file', { help: 'Log file', default: 'logs/collector-combined.log' });
30
61
  parser.add_argument('-e', '--log-level', { help: 'Log level', default: 'info' });
31
- parser.add_argument('-s', '--dump-after-secs', {
32
- help: 'Dump the coverage information to the target file every N seconds.',
33
- default: 120
62
+ parser.add_argument('-t', '--dump-after-mins', {
63
+ help: 'Dump the coverage information to the target file every N minutes.',
64
+ default: 360
34
65
  });
35
66
  parser.add_argument('-d', '--debug', {
36
67
  help: 'Print received coverage information to the terminal?',
37
68
  default: false
38
69
  });
70
+ // Parameters for the upload to Teamscale
71
+ parser.add_argument('-u', '--teamscale-server-url', {
72
+ help: 'Upload the coverage to the given Teamscale server URL, for example, https://teamscale.dev.example.com:8080/production.',
73
+ default: process.env.TEAMSCALE_SERVER_URL
74
+ });
75
+ parser.add_argument('--teamscale-access-token', {
76
+ help: 'The API key to use for uploading to Teamscale.',
77
+ default: process.env.TEAMSCALE_ACCESS_TOKEN
78
+ });
79
+ parser.add_argument('--teamscale-project', {
80
+ help: 'The project ID to upload coverage to.',
81
+ default: process.env.TEAMSCALE_PROJECT
82
+ });
83
+ parser.add_argument('--teamscale-user', {
84
+ help: 'The user for uploading coverage to Teamscale.',
85
+ default: process.env.TEAMSCALE_USER
86
+ });
87
+ parser.add_argument('--teamscale-partition', {
88
+ help: 'The partition to upload coverage to.',
89
+ default: process.env.TEAMSCALE_PARTITION
90
+ });
91
+ parser.add_argument('--teamscale-revision', {
92
+ help: 'The revision (commit hash, version id) to upload coverage for.',
93
+ default: process.env.TEAMSCALE_REVISION
94
+ });
95
+ parser.add_argument('--teamscale-commit', {
96
+ help: 'The branch and timestamp to upload coverage for, separated by colon.',
97
+ default: process.env.TEAMSCALE_COMMIT
98
+ });
99
+ parser.add_argument('--teamscale-repository', {
100
+ help: 'The repository to upload coverage for. Optional: Only needed when uploading via revision to a project that has more than one connector.',
101
+ default: process.env.TEAMSCALE_REPOSITORY
102
+ });
103
+ parser.add_argument('--teamscale-message', {
104
+ help: 'The commit message shown within Teamscale for the coverage upload. Default is "JavaScript coverage upload".',
105
+ default: (_a = process.env.TEAMSCALE_MESSAGE) !== null && _a !== void 0 ? _a : 'JavaScript coverage upload'
106
+ });
39
107
  return parser;
40
108
  }
41
109
  /**
@@ -70,6 +138,11 @@ class Main {
70
138
  const logger = this.buildLogger(config);
71
139
  logger.info(`Starting collector in working directory "${process.cwd()}".`);
72
140
  logger.info(`Logging "${config.log_level}" to "${config.log_to_file}".`);
141
+ // Check the command line arguments
142
+ if (!config.dump_to_file && !config.teamscale_server_url) {
143
+ logger.error('The Collector must be configured to either dump to a file or upload to Teamscale.');
144
+ process.exit(1);
145
+ }
73
146
  // Prepare the storage and the server
74
147
  const storage = new DataStorage_1.DataStorage(logger);
75
148
  const server = new CollectingServer_1.WebSocketCollectingServer(config.port, storage, logger);
@@ -79,7 +152,9 @@ class Main {
79
152
  // Optionally, start a timer that dumps the coverage after a N seconds
80
153
  this.maybeStartDumpTimer(config, storage, logger);
81
154
  // Say bye bye on CTRL+C and exit the process
82
- process.on('SIGINT', () => {
155
+ process.on('SIGINT', async () => {
156
+ // ... and do a final dump before.
157
+ await this.dumpCoverage(config, storage, logger).then();
83
158
  logger.info('Bye bye.');
84
159
  process.exit();
85
160
  });
@@ -87,32 +162,100 @@ class Main {
87
162
  /**
88
163
  * Start a timer for dumping the data, depending on the configuration.
89
164
  *
90
- * @param config - The config that determines whether or not to do the timed dump.
165
+ * @param config - The config that determines whether to do the timed dump or not.
91
166
  * @param storage - The storage with the information to dump.
92
167
  * @param logger - The logger to use.
93
168
  */
94
169
  static maybeStartDumpTimer(config, storage, logger) {
95
- if (config.dump_after_secs > 0) {
170
+ if (config.dump_after_mins > 0) {
171
+ logger.info(`Will dump coverage information every ${config.dump_after_mins} minute(s).`);
96
172
  const timer = setInterval(() => {
97
- try {
98
- const lines = storage.dumpToSimpleCoverageFile(config.dump_to_file);
99
- logger.info(`Conducted periodic coverage dump with ${lines} lines to ${config.dump_to_file}.`);
100
- }
101
- catch (e) {
102
- logger.error('Timed coverage dump failed.', e);
103
- }
104
- }, config.dump_after_secs * 1000);
173
+ this.dumpCoverage(config, storage, logger).then();
174
+ }, config.dump_after_mins * 1000 * 60);
105
175
  process.on('SIGINT', () => {
106
176
  // Stop the timed file dump
107
177
  if (timer) {
108
178
  clearInterval(timer);
109
179
  }
110
- // ... and do a final dump
111
- const written = storage.dumpToSimpleCoverageFile(config.dump_to_file);
112
- logger.info(`\nCaught interrupt signal. Written ${written} lines of the latest coverage.`);
113
180
  });
114
181
  }
115
182
  }
183
+ static async dumpCoverage(config, storage, logger) {
184
+ var _a;
185
+ try {
186
+ const deleteCoverageFileAfterUpload = !config.dump_to_file;
187
+ const coverageFile = (_a = config.dump_to_file) !== null && _a !== void 0 ? _a : tmp_1.default.tmpNameSync();
188
+ try {
189
+ // 1. Write coverage to a file
190
+ const lines = storage.dumpToSimpleCoverageFile(coverageFile);
191
+ logger.info(`Dumped ${lines} lines of coverage to ${coverageFile}.`);
192
+ // 2. Upload to Teamscale if configured
193
+ if (config.teamscale_server_url) {
194
+ await this.uploadToTeamscale(config, logger, coverageFile, lines);
195
+ }
196
+ }
197
+ finally {
198
+ if (deleteCoverageFileAfterUpload) {
199
+ fs.unlinkSync(coverageFile);
200
+ }
201
+ }
202
+ }
203
+ catch (e) {
204
+ logger.error('Coverage dump failed.', e);
205
+ }
206
+ }
207
+ static async uploadToTeamscale(config, logger, coverageFile, lines) {
208
+ if (!(config.teamscale_access_token && config.teamscale_user && config.teamscale_server_url)) {
209
+ logger.error('Cannot upload to Teamscale: API key and user name must be configured!');
210
+ return;
211
+ }
212
+ if (lines === 0) {
213
+ return;
214
+ }
215
+ logger.info('Preparing upload to Teamscale');
216
+ const form = new form_data_1.default();
217
+ form.append('report', fs.createReadStream(coverageFile), 'coverage.simple');
218
+ const parameters = new QueryParameters_1.default();
219
+ parameters.addIfDefined('format', 'SIMPLE');
220
+ parameters.addIfDefined('message', config.teamscale_message);
221
+ parameters.addIfDefined('repository', config.teamscale_repository);
222
+ parameters.addIfDefined('t', config.teamscale_commit);
223
+ parameters.addIfDefined('revision', config.teamscale_revision);
224
+ parameters.addIfDefined('partition', config.teamscale_partition);
225
+ await axios_1.default
226
+ .post(`${config.teamscale_server_url.replace(/\/$/, '')}/api/projects/${config.teamscale_project}/external-analysis/session/auto-create/report?${parameters.toString()}`, form, {
227
+ auth: {
228
+ username: config.teamscale_user,
229
+ password: config.teamscale_access_token
230
+ },
231
+ headers: {
232
+ Accept: '*/*',
233
+ 'Content-Type': `multipart/form-data; boundary=${form.getBoundary()}`
234
+ }
235
+ })
236
+ .catch(function (error) {
237
+ if (error.response) {
238
+ const response = error.response;
239
+ if (response.status >= 400) {
240
+ logger.error(`Upload failed with code ${response.status}: ${response.statusText}`);
241
+ logger.error(`Request failed with following response: ${response.data}`);
242
+ }
243
+ else {
244
+ logger.info(`Upload with status code ${response.status} finished.`);
245
+ }
246
+ }
247
+ else if (error.request) {
248
+ logger.error(`Upload request did not receive a response.`);
249
+ }
250
+ if (error.message) {
251
+ logger.error(`Something went wrong when uploading data: ${error.message}`);
252
+ logger.debug(`Details of the error: ${(0, util_1.inspect)(error)}`);
253
+ }
254
+ else {
255
+ logger.error(`Something went wrong when uploading data: ${(0, util_1.inspect)(error)}`);
256
+ }
257
+ });
258
+ }
116
259
  }
117
260
  exports.Main = Main;
118
261
  Main.run();
@@ -1,7 +1,11 @@
1
1
  "use strict";
2
2
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
3
  if (k2 === undefined) k2 = k;
4
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
5
9
  }) : (function(o, m, k, k2) {
6
10
  if (k2 === undefined) k2 = k;
7
11
  o[k2] = m[k];
@@ -1,7 +1,11 @@
1
1
  "use strict";
2
2
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
3
  if (k2 === undefined) k2 = k;
4
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
5
9
  }) : (function(o, m, k, k2) {
6
10
  if (k2 === undefined) k2 = k;
7
11
  o[k2] = m[k];
@@ -1,7 +1,11 @@
1
1
  "use strict";
2
2
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
3
  if (k2 === undefined) k2 = k;
4
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
5
9
  }) : (function(o, m, k, k2) {
6
10
  if (k2 === undefined) k2 = k;
7
11
  o[k2] = m[k];
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Helper class for building HTTP query parameter strings.
3
+ */
4
+ export default class QueryParameters extends URLSearchParams {
5
+ /**
6
+ * Adds a parameter if the value is defined.
7
+ */
8
+ addIfDefined(key: string, value: string | undefined): void;
9
+ }
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ /**
4
+ * Helper class for building HTTP query parameter strings.
5
+ */
6
+ class QueryParameters extends URLSearchParams {
7
+ /**
8
+ * Adds a parameter if the value is defined.
9
+ */
10
+ addIfDefined(key, value) {
11
+ if (value) {
12
+ this.append(key, value);
13
+ }
14
+ else {
15
+ this.delete(key);
16
+ }
17
+ }
18
+ }
19
+ exports.default = QueryParameters;
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@teamscale/coverage-collector",
3
- "version": "0.0.1-beta.1",
3
+ "version": "0.0.1-beta.25",
4
4
  "description": "Collector for JavaScript code coverage information",
5
- "main": "dist/main.js",
6
- "bin": "dist/main.js",
7
- "types": "dist/main.d.ts",
5
+ "main": "dist/src/main.js",
6
+ "bin": "dist/src/main.js",
7
+ "types": "dist/src/main.d.ts",
8
8
  "author": "CQSE GmbH",
9
9
  "license": "Apache-2.0",
10
10
  "repository": {
@@ -12,9 +12,10 @@
12
12
  "url": "https://github.com/cqse/teamscale-javascript-profiler.git"
13
13
  },
14
14
  "scripts": {
15
+ "prepublishOnly": "yarn clean && yarn build",
15
16
  "clean": "rimraf dist tsconfig.tsbuildinfo",
16
17
  "build": "tsc",
17
- "serve": "node dist/src/main.js",
18
+ "collector": "node dist/src/main.js",
18
19
  "test": "yarn build && NODE_OPTIONS='--experimental-vm-modules' jest --forceExit --coverage --silent=true --detectOpenHandles"
19
20
  },
20
21
  "files": [
@@ -24,8 +25,12 @@
24
25
  "@cqse/commons": "^0.0.1-beta.1",
25
26
  "argparse": "^2.0.1",
26
27
  "async": "^3.2.0",
28
+ "axios": "^0.24.0",
29
+ "dotenv": "^14.1.0",
30
+ "form-data": "^4.0.0",
27
31
  "rxjs": "^7.1.0",
28
32
  "source-map": "^0.7.3",
33
+ "tmp": "^0.2.1",
29
34
  "typescript-optional": "^2.0.1",
30
35
  "winston": "^3.3.3",
31
36
  "ws": "^7.4.5"
@@ -38,6 +43,7 @@
38
43
  "@types/jest": "^27.0.1",
39
44
  "@types/node": "^15.0.1",
40
45
  "@types/source-map": "^0.5.7",
46
+ "@types/tmp": "^0.2.3",
41
47
  "@types/winston": "^2.4.4",
42
48
  "@types/ws": "^7.4.2",
43
49
  "babel-jest": "^27.2.0",