@rocketmakers/shell-tests 0.1.0
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 +3 -0
- package/lib/suite.d.ts +39 -0
- package/lib/suite.js +59 -0
- package/package.json +47 -0
package/README.md
ADDED
package/lib/suite.d.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { Logger } from '@rocketmakers/log';
|
|
2
|
+
import { IShellExecutor } from '@rocketmakers/shell/executor';
|
|
3
|
+
export interface ITestSuite {
|
|
4
|
+
suiteName: string;
|
|
5
|
+
sandboxRootPath: string;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Runs a sandboxed test suite.
|
|
9
|
+
*
|
|
10
|
+
* This utility function ensures that a sandbox testing directory exists and is empty before each test.
|
|
11
|
+
* It provides a clean environment for every test run by automatically creating and cleaning up the sandbox directory.
|
|
12
|
+
*
|
|
13
|
+
* @param suiteConfig - Configuration for the test suite.
|
|
14
|
+
* @param suiteConfig.suiteName - The name of the test suite, used for logging and test descriptions.
|
|
15
|
+
* @param suiteConfig.sandboxRootPath - The root path of the sandbox directory to be used for testing.
|
|
16
|
+
* @param runSuite - A callback function that contains the test suite logic. It receives a logger and shell executor as arguments.
|
|
17
|
+
*
|
|
18
|
+
* ### Example Usage:
|
|
19
|
+
* ```typescript
|
|
20
|
+
* sandboxedTestSuite(
|
|
21
|
+
* { suiteName: 'My Test Suite', sandboxRootPath: '/tmp/my-sandbox' },
|
|
22
|
+
* (logger, shell) => {
|
|
23
|
+
* it('should perform a test', async () => {
|
|
24
|
+
* logger.info('Running test...');
|
|
25
|
+
* await shell.exec(['echo', 'Hello, World!']);
|
|
26
|
+
* });
|
|
27
|
+
* }
|
|
28
|
+
* );
|
|
29
|
+
* ```
|
|
30
|
+
*
|
|
31
|
+
* @remarks
|
|
32
|
+
* - The `logger` is created using the suite name and can be used for logging within the test suite.
|
|
33
|
+
* - The `shell` is an instance of `IShellExecutor` and can be used to execute shell commands.
|
|
34
|
+
*
|
|
35
|
+
* @throws
|
|
36
|
+
* - Errors from `Prerequisites.check` if prerequisites are not met.
|
|
37
|
+
* - Errors from `mkdir` or `rm` commands if the sandbox directory cannot be managed.
|
|
38
|
+
*/
|
|
39
|
+
export declare function sandboxedTestSuite({ suiteName, sandboxRootPath }: ITestSuite, runSuite: (logger: Logger, shell: IShellExecutor) => void): void;
|
package/lib/suite.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.sandboxedTestSuite = sandboxedTestSuite;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const logger_1 = require("@rocketmakers/shell/logger");
|
|
6
|
+
const Prerequisites = tslib_1.__importStar(require("@rocketmakers/shell/prerequisites"));
|
|
7
|
+
const shell_executor_1 = require("@rocketmakers/shell-executor");
|
|
8
|
+
const promises_1 = require("fs/promises");
|
|
9
|
+
/**
|
|
10
|
+
* Runs a sandboxed test suite.
|
|
11
|
+
*
|
|
12
|
+
* This utility function ensures that a sandbox testing directory exists and is empty before each test.
|
|
13
|
+
* It provides a clean environment for every test run by automatically creating and cleaning up the sandbox directory.
|
|
14
|
+
*
|
|
15
|
+
* @param suiteConfig - Configuration for the test suite.
|
|
16
|
+
* @param suiteConfig.suiteName - The name of the test suite, used for logging and test descriptions.
|
|
17
|
+
* @param suiteConfig.sandboxRootPath - The root path of the sandbox directory to be used for testing.
|
|
18
|
+
* @param runSuite - A callback function that contains the test suite logic. It receives a logger and shell executor as arguments.
|
|
19
|
+
*
|
|
20
|
+
* ### Example Usage:
|
|
21
|
+
* ```typescript
|
|
22
|
+
* sandboxedTestSuite(
|
|
23
|
+
* { suiteName: 'My Test Suite', sandboxRootPath: '/tmp/my-sandbox' },
|
|
24
|
+
* (logger, shell) => {
|
|
25
|
+
* it('should perform a test', async () => {
|
|
26
|
+
* logger.info('Running test...');
|
|
27
|
+
* await shell.exec(['echo', 'Hello, World!']);
|
|
28
|
+
* });
|
|
29
|
+
* }
|
|
30
|
+
* );
|
|
31
|
+
* ```
|
|
32
|
+
*
|
|
33
|
+
* @remarks
|
|
34
|
+
* - The `logger` is created using the suite name and can be used for logging within the test suite.
|
|
35
|
+
* - The `shell` is an instance of `IShellExecutor` and can be used to execute shell commands.
|
|
36
|
+
*
|
|
37
|
+
* @throws
|
|
38
|
+
* - Errors from `Prerequisites.check` if prerequisites are not met.
|
|
39
|
+
* - Errors from `mkdir` or `rm` commands if the sandbox directory cannot be managed.
|
|
40
|
+
*/
|
|
41
|
+
function sandboxedTestSuite({ suiteName, sandboxRootPath }, runSuite) {
|
|
42
|
+
const logger = (0, logger_1.createLogger)(suiteName);
|
|
43
|
+
const shell = new shell_executor_1.ShellExecutor();
|
|
44
|
+
(0, logger_1.setDefaultLoggerLevel)(process.env.LOG_LEVEL || 'error');
|
|
45
|
+
const deleteSandboxDir = () => shell.exec(['rm', '-rf', sandboxRootPath]);
|
|
46
|
+
describe(`${suiteName}`, () => {
|
|
47
|
+
beforeAll(async () => {
|
|
48
|
+
await Prerequisites.check(shell);
|
|
49
|
+
});
|
|
50
|
+
beforeEach(async () => {
|
|
51
|
+
await deleteSandboxDir();
|
|
52
|
+
await (0, promises_1.mkdir)(sandboxRootPath);
|
|
53
|
+
});
|
|
54
|
+
afterEach(async () => {
|
|
55
|
+
await deleteSandboxDir();
|
|
56
|
+
});
|
|
57
|
+
runSuite(logger, shell);
|
|
58
|
+
});
|
|
59
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@rocketmakers/shell-tests",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"exports": {
|
|
5
|
+
"./suite": {
|
|
6
|
+
"types": "./lib/suite.d.ts",
|
|
7
|
+
"default": "./lib/suite.js"
|
|
8
|
+
}
|
|
9
|
+
},
|
|
10
|
+
"dependencies": {
|
|
11
|
+
"@rocketmakers/log": "0.1.0",
|
|
12
|
+
"@rocketmakers/shell": "0.1.0",
|
|
13
|
+
"@rocketmakers/shell-executor": "0.1.0"
|
|
14
|
+
},
|
|
15
|
+
"devDependencies": {
|
|
16
|
+
"@types/jest": "29.5.14",
|
|
17
|
+
"@types/node": "22.13.9",
|
|
18
|
+
"eslint": "8.57.1",
|
|
19
|
+
"jest": "29.7.0",
|
|
20
|
+
"jest-junit": "16.0.0",
|
|
21
|
+
"ts-jest": "29.3.0",
|
|
22
|
+
"typescript": "5.8.2",
|
|
23
|
+
"tslib": "2.8.1",
|
|
24
|
+
"eslint-config-shell": "0.1.0",
|
|
25
|
+
"tsconfig-shell": "0.1.0"
|
|
26
|
+
},
|
|
27
|
+
"directories": {
|
|
28
|
+
"lib": "lib"
|
|
29
|
+
},
|
|
30
|
+
"engines": {
|
|
31
|
+
"node": "^20 || ^22"
|
|
32
|
+
},
|
|
33
|
+
"files": [
|
|
34
|
+
"lib/**/*.js",
|
|
35
|
+
"lib/**/*.d.ts",
|
|
36
|
+
"!lib/index.js",
|
|
37
|
+
"!lib/index.d.ts"
|
|
38
|
+
],
|
|
39
|
+
"keywords": [],
|
|
40
|
+
"license": "MIT",
|
|
41
|
+
"repository": "Rocketmakers/rocketmakers-packages",
|
|
42
|
+
"scripts": {
|
|
43
|
+
"build": "tsc -p .",
|
|
44
|
+
"lint": "eslint . --ext .ts",
|
|
45
|
+
"lint-fix": "pnpm lint --fix"
|
|
46
|
+
}
|
|
47
|
+
}
|