diff --git a/build/tests/integration/lib/controllerSetup.d.ts b/build/tests/integration/lib/controllerSetup.d.ts index aead29cf..f3771c09 100644 --- a/build/tests/integration/lib/controllerSetup.d.ts +++ b/build/tests/integration/lib/controllerSetup.d.ts @@ -8,6 +8,22 @@ export declare class ControllerSetup { private testAdapterDir; private testControllerDir; private testDataDir; + /** + * Gets the path to the file that tracks the installed controller version + */ + private getControllerVersionFilePath; + /** + * Reads the currently installed controller version from the tracking file + */ + private getInstalledControllerVersion; + /** + * Saves the controller version to the tracking file + */ + private saveControllerVersion; + /** + * Clears the tmp directory when switching controller versions + */ + private clearTmpDirectory; prepareTestDir(controllerVersion?: string): Promise; /** * Tests if JS-Controller is already installed diff --git a/build/tests/integration/lib/controllerSetup.js b/build/tests/integration/lib/controllerSetup.js index c9aa08ce..e84f780e 100644 --- a/build/tests/integration/lib/controllerSetup.js +++ b/build/tests/integration/lib/controllerSetup.js @@ -63,10 +63,62 @@ class ControllerSetup { debug(` appName: ${this.appName}`); debug(` adapterName: ${this.adapterName}`); } + /** + * Gets the path to the file that tracks the installed controller version + */ + getControllerVersionFilePath() { + return path.join(this.testDir, '.controller-version'); + } + /** + * Reads the currently installed controller version from the tracking file + */ + async getInstalledControllerVersion() { + const versionFilePath = this.getControllerVersionFilePath(); + if (await (0, fs_extra_1.pathExists)(versionFilePath)) { + try { + const version = await (0, fs_extra_1.readFile)(versionFilePath, 'utf8'); + return version.trim(); + } + catch (error) { + debug(`Failed to read controller version file: ${String(error)}`); + return null; + } + } + return null; + } + /** + * Saves the controller version to the tracking file + */ + async saveControllerVersion(version) { + const versionFilePath = this.getControllerVersionFilePath(); + await (0, fs_extra_1.writeFile)(versionFilePath, version, 'utf8'); + } + /** + * Clears the tmp directory when switching controller versions + */ + async clearTmpDirectory() { + debug('Clearing tmp directory for controller version switch...'); + // Clear the node_modules directory + const nodeModulesPath = path.join(this.testDir, 'node_modules'); + if (await (0, fs_extra_1.pathExists)(nodeModulesPath)) { + await (0, fs_extra_1.emptyDir)(nodeModulesPath); + } + // Clear the data directory + if (await (0, fs_extra_1.pathExists)(this.testDataDir)) { + await (0, fs_extra_1.emptyDir)(this.testDataDir); + } + debug(' => tmp directory cleared!'); + } async prepareTestDir(controllerVersion = 'dev') { debug(`Preparing the test directory. JS-Controller version: "${controllerVersion}"...`); // Make sure the test dir exists await (0, fs_extra_1.ensureDir)(this.testDir); + // Check if the controller version has changed + const installedVersion = await this.getInstalledControllerVersion(); + if (installedVersion && installedVersion !== controllerVersion) { + debug(`Controller version changed from "${installedVersion}" to "${controllerVersion}"`); + await this.clearTmpDirectory(); + } // Write the package.json const packageJson = { name: path.basename(this.testDir), @@ -108,6 +160,8 @@ class ControllerSetup { if (wasJsControllerInstalled) { await this.setupJsController(); } + // Save the controller version for future reference + await this.saveControllerVersion(controllerVersion); debug(' => done!'); } /** diff --git a/src/tests/integration/lib/controllerSetup.ts b/src/tests/integration/lib/controllerSetup.ts index 94c2fa67..29c42482 100644 --- a/src/tests/integration/lib/controllerSetup.ts +++ b/src/tests/integration/lib/controllerSetup.ts @@ -1,6 +1,6 @@ // Add debug logging for tests import debugModule from 'debug'; -import { emptyDir, ensureDir, pathExists, unlink, writeFile, writeJSON } from 'fs-extra'; +import { emptyDir, ensureDir, pathExists, readFile, unlink, writeFile, writeJSON } from 'fs-extra'; import { Socket } from 'node:net'; import * as path from 'node:path'; import { getAdapterName, getAppName } from '../../../lib/adapterTools'; @@ -37,11 +37,67 @@ export class ControllerSetup { private testControllerDir: string; private testDataDir: string; + /** + * Gets the path to the file that tracks the installed controller version + */ + private getControllerVersionFilePath(): string { + return path.join(this.testDir, '.controller-version'); + } + + /** + * Reads the currently installed controller version from the tracking file + */ + private async getInstalledControllerVersion(): Promise { + const versionFilePath = this.getControllerVersionFilePath(); + if (await pathExists(versionFilePath)) { + try { + const version = await readFile(versionFilePath, 'utf8'); + return version.trim(); + } catch (error) { + debug(`Failed to read controller version file: ${String(error)}`); + return null; + } + } + return null; + } + + /** + * Saves the controller version to the tracking file + */ + private async saveControllerVersion(version: string): Promise { + const versionFilePath = this.getControllerVersionFilePath(); + await writeFile(versionFilePath, version, 'utf8'); + } + + /** + * Clears the tmp directory when switching controller versions + */ + private async clearTmpDirectory(): Promise { + debug('Clearing tmp directory for controller version switch...'); + // Clear the node_modules directory + const nodeModulesPath = path.join(this.testDir, 'node_modules'); + if (await pathExists(nodeModulesPath)) { + await emptyDir(nodeModulesPath); + } + // Clear the data directory + if (await pathExists(this.testDataDir)) { + await emptyDir(this.testDataDir); + } + debug(' => tmp directory cleared!'); + } + public async prepareTestDir(controllerVersion: string = 'dev'): Promise { debug(`Preparing the test directory. JS-Controller version: "${controllerVersion}"...`); // Make sure the test dir exists await ensureDir(this.testDir); + // Check if the controller version has changed + const installedVersion = await this.getInstalledControllerVersion(); + if (installedVersion && installedVersion !== controllerVersion) { + debug(`Controller version changed from "${installedVersion}" to "${controllerVersion}"`); + await this.clearTmpDirectory(); + } + // Write the package.json const packageJson = { name: path.basename(this.testDir), @@ -87,6 +143,9 @@ export class ControllerSetup { await this.setupJsController(); } + // Save the controller version for future reference + await this.saveControllerVersion(controllerVersion); + debug(' => done!'); }