Skip to content

Commit f60f3e0

Browse files
test: improved local test running (#1441)
- Added node script to add terminal aliases (helps to easily run test groups) - Integrated mocha watch - Removed redundant sh helper scripts - See mochajs/mocha#5149 --------- Co-authored-by: Arya <[email protected]>
1 parent d4999de commit f60f3e0

33 files changed

+145
-170
lines changed

CONTRIBUTING.md

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,33 @@ Install the [`aws-cli`](https://docs.aws.amazon.com/cli/latest/userguide/getting
2626

2727
## Executing Tests Locally
2828

29-
Some of the tests require infrastructure components (databases etc.) to run locally. The easiest way to run all required components locally is to use Docker and on top of this [Docker Compose](https://docs.docker.com/compose/). Start the script `node bin/start-test-containers.js` to run all containers (not recommended).
30-
31-
Instead:
29+
Some of the tests require infrastructure components (databases etc.) to run locally. The easiest way to run required components locally is to use Docker and on top of this [Docker Compose](https://docs.docker.com/compose/).
3230

3331
```sh
3432
node bin/start-test-containers.js --mongo --redis
3533
```
3634

37-
It's not recommended to run all tests using `bin/run-tests.sh` - it takes too long. Take a look at the root package.json and run a specific test group or run e.g. `bin/run-collector-tests.sh` with the mocha `.only` attribute.
35+
### Using terminal aliases
36+
37+
Add aliases to your terminal:
38+
39+
```sh
40+
node bin/add-test-aliases.js bash|zsh
41+
```
42+
43+
Add a mocha `.only` on the test you would like to execute and then run the target scope:
44+
45+
```sh
46+
runcollector # Run the 'collector' scope with watch mode
47+
runcollector-nw # Run the 'collector' scope without watch mode
48+
```
49+
50+
### Manually
51+
52+
```sh
53+
bin/run-tests.sh --scope=@instana/collector
54+
bin/run-tests.sh --scope=@instana/collector --watch
55+
```
3856

3957
If you want to see the Node.js collector's debug output while running the tests, make sure the environment variable `WITH_STDOUT` is set to a non-empty string.
4058

bin/add-test-aliases.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* (c) Copyright IBM Corp. 2024
3+
*/
4+
5+
'use strict';
6+
7+
const { execSync } = require('child_process');
8+
const fs = require('fs');
9+
const os = require('os');
10+
11+
const addAliasIfNotExists = (aliasCommand, configFile) => {
12+
let fileContent = '';
13+
try {
14+
fileContent = fs.readFileSync(configFile, 'utf8');
15+
} catch (err) {
16+
console.error(`Could not read ${configFile}: ${err.message}`);
17+
return;
18+
}
19+
20+
if (!fileContent.includes(aliasCommand)) {
21+
fs.appendFileSync(configFile, `\n${aliasCommand}\n`);
22+
console.log(`Added alias: ${aliasCommand}`);
23+
} else {
24+
console.log(`Alias already exists: ${aliasCommand}`);
25+
}
26+
};
27+
28+
const output = execSync('lerna list --json', { encoding: 'utf-8' });
29+
const packages = JSON.parse(output);
30+
const scopeNames = packages.map(pkg => pkg.name);
31+
const shellArg = process.argv[2];
32+
33+
if (!shellArg) {
34+
console.error('Error: Please specify either "bash" or "zsh".');
35+
process.exit(1);
36+
}
37+
38+
let configFile;
39+
if (shellArg === 'bash') {
40+
configFile = `${os.homedir()}/.bashrc`;
41+
} else if (shellArg === 'zsh') {
42+
configFile = `${os.homedir()}/.zshrc`;
43+
} else {
44+
console.error('Error: Invalid argument. Please specify "bash" or "zsh".');
45+
process.exit(1);
46+
}
47+
48+
scopeNames.forEach(scope => {
49+
const cleanedScope = scope.replace('@instana/', '');
50+
51+
const watchAlias = `alias run${cleanedScope}='bin/run-tests.sh --scope=${scope} --watch'`;
52+
const nwAlias = `alias run${cleanedScope}-nw='bin/run-tests.sh --scope=${scope}'`;
53+
54+
addAliasIfNotExists(watchAlias, configFile);
55+
addAliasIfNotExists(nwAlias, configFile);
56+
});
57+
58+
console.log('Aliases added. Please run the following command to apply the changes:');
59+
console.log(` source ${configFile}`);
60+
console.log('Alternatively, restart your terminal.');
61+
62+
console.log('Done');

bin/run-autoprofile-tests.sh

Lines changed: 0 additions & 11 deletions
This file was deleted.

bin/run-aws-lambda-auto-wrap.sh

Lines changed: 0 additions & 10 deletions
This file was deleted.

bin/run-azure-container-services-tests.sh

Lines changed: 0 additions & 9 deletions
This file was deleted.

bin/run-collector-tests.sh

Lines changed: 0 additions & 11 deletions
This file was deleted.

bin/run-core-tests.sh

Lines changed: 0 additions & 11 deletions
This file was deleted.

bin/run-fargate-tests.sh

Lines changed: 0 additions & 11 deletions
This file was deleted.

bin/run-google-cloud-run-tests.sh

Lines changed: 0 additions & 11 deletions
This file was deleted.

bin/run-lambda-tests.sh

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)