Skip to content

Commit

Permalink
Corrected removed functions
Browse files Browse the repository at this point in the history
  • Loading branch information
GermanBluefox committed Jan 23, 2025
1 parent 88808dd commit 6beae75
Show file tree
Hide file tree
Showing 33 changed files with 259 additions and 207 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
PLACEHOLDER for the next version:
## **WORK IN PROGRESS**
-->
## 5.0.2 (2025-01-21)
## **WORK IN PROGRESS**
* Packages were updated

## 5.0.0 (2024-09-14)
Expand Down
6 changes: 6 additions & 0 deletions build/lib/adapterTools.d.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,37 @@
/**
* Loads an adapter's package.json
*
* @param adapterDir The directory the adapter resides in
*/
export declare function loadNpmPackage(adapterDir: string): Record<string, any>;
/**
* Loads an adapter's io-package.json
*
* @param adapterDir The directory the adapter resides in
*/
export declare function loadIoPackage(adapterDir: string): Record<string, any>;
export declare function getAdapterExecutionMode(adapterDir: string): ioBroker.AdapterCommon['mode'];
/**
* Locates an adapter's main file
*
* @param adapterDir The directory the adapter resides in
*/
export declare function locateAdapterMainFile(adapterDir: string): Promise<string>;
/**
* Locates an adapter's config to populate the `adapter.config` object with
*
* @param adapterDir The directory the adapter resides in
*/
export declare function loadAdapterConfig(adapterDir: string): Record<string, any>;
/**
* Loads the adapter's common configuration from `io-package.json`
*
* @param adapterDir The directory the adapter resides in
*/
export declare function loadAdapterCommon(adapterDir: string): Record<string, any>;
/**
* Loads the instanceObjects for an adapter from its `io-package.json`
*
* @param adapterDir The directory the adapter resides in
*/
export declare function loadInstanceObjects(adapterDir: string): ioBroker.Object[];
Expand Down
14 changes: 9 additions & 5 deletions build/lib/adapterTools.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,26 +48,25 @@ exports.getAdapterName = getAdapterName;
exports.getAdapterFullName = getAdapterFullName;
exports.getAdapterDependencies = getAdapterDependencies;
// Add debug logging for tests
// @ts-expect-error no types
const typeguards_1 = require("alcalzone-shared/typeguards");
const debug_1 = __importDefault(require("debug"));
const fs_extra_1 = require("fs-extra");
const path = __importStar(require("path"));
const debug = (0, debug_1.default)('testing:unit:adapterTools');
/**
* Loads an adapter's package.json
*
* @param adapterDir The directory the adapter resides in
*/
function loadNpmPackage(adapterDir) {
// eslint-disable-next-line @typescript-eslint/no-require-imports
return require(path.join(adapterDir, 'package.json'));
}
/**
* Loads an adapter's io-package.json
*
* @param adapterDir The directory the adapter resides in
*/
function loadIoPackage(adapterDir) {
// eslint-disable-next-line @typescript-eslint/no-require-imports
return require(path.join(adapterDir, 'io-package.json'));
}
function getAdapterExecutionMode(adapterDir) {
Expand All @@ -76,6 +75,7 @@ function getAdapterExecutionMode(adapterDir) {
}
/**
* Locates an adapter's main file
*
* @param adapterDir The directory the adapter resides in
*/
async function locateAdapterMainFile(adapterDir) {
Expand All @@ -95,7 +95,7 @@ async function locateAdapterMainFile(adapterDir) {
return ret;
}
// If both don't exist, JS-Controller uses <adapter name>.js as another fallback
ret = path.join(adapterDir, ioPackage.common.name + '.js');
ret = path.join(adapterDir, `${ioPackage.common.name}.js`);
debug(` => trying ${ret}`);
if (await (0, fs_extra_1.pathExists)(ret)) {
debug(` => found ${mainFile}`);
Expand All @@ -105,6 +105,7 @@ async function locateAdapterMainFile(adapterDir) {
}
/**
* Locates an adapter's config to populate the `adapter.config` object with
*
* @param adapterDir The directory the adapter resides in
*/
function loadAdapterConfig(adapterDir) {
Expand All @@ -113,6 +114,7 @@ function loadAdapterConfig(adapterDir) {
}
/**
* Loads the adapter's common configuration from `io-package.json`
*
* @param adapterDir The directory the adapter resides in
*/
function loadAdapterCommon(adapterDir) {
Expand All @@ -121,6 +123,7 @@ function loadAdapterCommon(adapterDir) {
}
/**
* Loads the instanceObjects for an adapter from its `io-package.json`
*
* @param adapterDir The directory the adapter resides in
*/
function loadInstanceObjects(adapterDir) {
Expand Down Expand Up @@ -153,8 +156,9 @@ function getAdapterDependencies(adapterDir) {
}
else if ((0, typeguards_1.isObject)(dep)) {
const key = Object.keys(dep)[0];
if (key)
if (key) {
ret[key] = dep[key] || 'latest';
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions build/lib/executeCommand.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export interface ExecuteCommandResult {
export declare function executeCommand(command: string, options?: Partial<ExecuteCommandOptions>): Promise<ExecuteCommandResult>;
/**
* Executes a command and returns the exit code and (if requested) the stdout
*
* @param command The command to execute
* @param args The command line arguments for the command
* @param options (optional) Some options for the command execution
Expand Down
20 changes: 13 additions & 7 deletions build/lib/executeCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,19 @@ function executeCommand(command, argsOrOptions, options) {
// no args were given
options = argsOrOptions;
}
if (options == null)
if (options == null) {
options = {};
if (args == null)
}
if (args == null) {
args = [];
}
const spawnOptions = {
stdio: [options.stdin || process.stdin, options.stdout || process.stdout, options.stderr || process.stderr],
windowsHide: true,
};
if (options.cwd != null)
if (options.cwd != null) {
spawnOptions.cwd = options.cwd;
}
// Fix npm / node executable paths on Windows
if (isWindows) {
if (command === 'npm') {
Expand All @@ -35,10 +38,11 @@ function executeCommand(command, argsOrOptions, options) {
command += '.exe';
}
}
if (options.logCommandExecution == null)
if (options.logCommandExecution == null) {
options.logCommandExecution = false;
}
if (options.logCommandExecution) {
console.log('executing: ' + `${command} ${args.join(' ')}`);
console.log(`executing: ${command} ${args.join(' ')}`);
}
// Now execute the npm process and avoid throwing errors
try {
Expand All @@ -56,16 +60,18 @@ function executeCommand(command, argsOrOptions, options) {
if (options.stdout === 'pipe') {
bufferedStdout = '';
cmd.stdout.on('data', (chunk) => {
if (Buffer.isBuffer(chunk))
if (Buffer.isBuffer(chunk)) {
chunk = chunk.toString('utf8');
}
bufferedStdout += chunk;
});
}
if (options.stderr === 'pipe') {
bufferedStderr = '';
cmd.stderr.on('data', (chunk) => {
if (Buffer.isBuffer(chunk))
if (Buffer.isBuffer(chunk)) {
chunk = chunk.toString('utf8');
}
bufferedStderr += chunk;
});
}
Expand Down
1 change: 1 addition & 0 deletions build/lib/testAdapter.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {};
1 change: 1 addition & 0 deletions build/lib/testAdapter.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use strict";
// TODO: Do we need this file?
// import * as utils from "@iobroker/adapter-core";
Object.defineProperty(exports, "__esModule", { value: true });
// const adapter = utils.adapter({
// name: "foo",
// ready() {
Expand Down
4 changes: 2 additions & 2 deletions build/tests/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { testAdapterWithMocks } from './unit';
import { createMocks } from './unit/harness/createMocks';
import { createAsserts } from './unit/mocks/mockDatabase';
export { TestHarness as IntegrationTestHarness } from './integration/lib/harness';
export { MockAdapter } from './unit/mocks/mockAdapter';
export type { MockAdapter } from './unit/mocks/mockAdapter';
export { MockDatabase } from './unit/mocks/mockDatabase';
/** Predefined test sets */
export declare const tests: {
Expand All @@ -19,6 +19,6 @@ export declare const utils: {
createMocks: typeof createMocks;
createAsserts: typeof createAsserts;
/** @deprecated Adapter startup unit tests are no longer supported */
startMockAdapter: () => {};
startMockAdapter: () => any;
};
};
7 changes: 6 additions & 1 deletion build/tests/integration/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,15 @@ function testAdapter(adapterDir, options = {}) {
// Adapters with these modes are allowed to "immediately" exit with code 0
switch (harness.getAdapterExecutionMode()) {
case 'schedule':
allowedExitCodes.add(0);
break;
case 'once':
allowedExitCodes.add(0);
break;
// @ts-expect-error subscribe was deprecated
case 'subscribe':
allowedExitCodes.add(0);
break;
}
return new Promise((resolve, reject) => {
// Register a handler to check the alive state and exit codes
Expand All @@ -147,7 +152,7 @@ function testAdapter(adapterDir, options = {}) {
resolve(`The expected ${typeof code === 'number' ? 'exit code' : 'signal'} ${code} was received.`);
}
});
harness.startAdapter();
void harness.startAdapter();
}).then(msg => console.log(msg));
});
});
Expand Down
18 changes: 10 additions & 8 deletions build/tests/integration/lib/adapterSetup.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
Object.defineProperty(exports, "__esModule", { value: true });
exports.AdapterSetup = void 0;
// Add debug logging for tests
// @ts-expect-error no types
const objects_1 = require("alcalzone-shared/objects");
const debug_1 = __importDefault(require("debug"));
const fs_extra_1 = require("fs-extra");
const path = __importStar(require("path"));
Expand Down Expand Up @@ -78,8 +76,9 @@ class AdapterSetup {
const packResult = await (0, executeCommand_1.executeCommand)('npm', ['pack', '--loglevel', 'silent'], {
stdout: 'pipe',
});
if (packResult.exitCode !== 0 || typeof packResult.stdout !== 'string')
if (packResult.exitCode !== 0 || typeof packResult.stdout !== 'string') {
throw new Error(`Packing the adapter tarball failed!`);
}
// The last non-empty line of `npm pack`s STDOUT contains the tarball path
const stdoutLines = packResult.stdout.trim().split(/[\r\n]+/);
const tarballName = stdoutLines[stdoutLines.length - 1].trim();
Expand All @@ -99,16 +98,18 @@ class AdapterSetup {
const packageJsonPath = path.join(this.testDir, 'package.json');
const packageJson = await (0, fs_extra_1.readJSON)(packageJsonPath);
packageJson.dependencies[this.adapterFullName] = `file:./${tarballName}`;
for (const [dep, version] of (0, objects_1.entries)((0, adapterTools_1.getAdapterDependencies)(this.adapterDir))) {
for (const [dep, version] of Object.entries((0, adapterTools_1.getAdapterDependencies)(this.adapterDir))) {
// Don't overwrite the js-controller GitHub dependency with a probably lower one
if (dep === 'js-controller')
if (dep === 'js-controller') {
continue;
}
packageJson.dependencies[`${this.appName}.${dep}`] = version;
}
await (0, fs_extra_1.writeJSON)(packageJsonPath, packageJson, { spaces: 2 });
debug('Deleting old remains of this adapter');
if (await (0, fs_extra_1.pathExists)(this.testAdapterDir))
if (await (0, fs_extra_1.pathExists)(this.testAdapterDir)) {
await (0, fs_extra_1.remove)(this.testAdapterDir);
}
debug('Installing adapter');
// Defer to npm to install the controller (if it wasn't already)
await (0, executeCommand_1.executeCommand)('npm', ['i', '--omit=dev'], {
Expand All @@ -126,15 +127,16 @@ class AdapterSetup {
cwd: this.testControllerDir,
stdout: 'ignore',
});
if (addResult.exitCode !== 0)
if (addResult.exitCode !== 0) {
throw new Error(`Adding the adapter instance failed!`);
}
debug(' => done!');
}
async deleteOldInstances(dbConnection) {
debug('Removing old adapter instances...');
const allKeys = new Set([...(await dbConnection.getObjectIDs()), ...(await dbConnection.getStateIDs())]);
const instanceRegex = new RegExp(`^system\\.adapter\\.${this.adapterName}\\.\\d+`);
const instanceObjsRegex = new RegExp(`^${this.adapterName}\\.\\d+\.`);
const instanceObjsRegex = new RegExp(`^${this.adapterName}\\.\\d+\\.`);
const belongsToAdapter = (id) => {
return (instanceRegex.test(id) ||
instanceObjsRegex.test(id) ||
Expand Down
9 changes: 0 additions & 9 deletions build/tests/integration/lib/controllerSetup.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ export declare class ControllerSetup {
prepareTestDir(controllerVersion?: string): Promise<void>;
/**
* Tests if JS-Controller is already installed
* @param appName The branded name of "iobroker"
* @param testDir The directory the integration tests are executed in
*/
isJsControllerInstalled(): Promise<boolean>;
/**
Expand All @@ -25,25 +23,18 @@ export declare class ControllerSetup {
setupJsController(): Promise<void>;
/**
* Changes the objects and states db to use alternative ports
* @param appName The branded name of "iobroker"
* @param testDir The directory the integration tests are executed in
*/
setupSystemConfig(dbConnection: DBConnection): void;
/**
* Clears the log dir for integration tests (and creates it if it doesn't exist)
* @param appName The branded name of "iobroker"
* @param testDir The directory the integration tests are executed in
*/
clearLogDir(): Promise<void>;
/**
* Clears the sqlite DB dir for integration tests (and creates it if it doesn't exist)
* @param appName The branded name of "iobroker"
* @param testDir The directory the integration tests are executed in
*/
clearDBDir(): Promise<void>;
/**
* Disables all admin instances in the objects DB
* @param objects The contents of objects.json
*/
disableAdminInstances(dbConnection: DBConnection): Promise<void>;
}
Loading

0 comments on commit 6beae75

Please sign in to comment.