Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions build/tests/integration/lib/controllerSetup.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>;
/**
* Tests if JS-Controller is already installed
Expand Down
54 changes: 54 additions & 0 deletions build/tests/integration/lib/controllerSetup.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -108,6 +160,8 @@ class ControllerSetup {
if (wasJsControllerInstalled) {
await this.setupJsController();
}
// Save the controller version for future reference
await this.saveControllerVersion(controllerVersion);
debug(' => done!');
}
/**
Expand Down
61 changes: 60 additions & 1 deletion src/tests/integration/lib/controllerSetup.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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<string | null> {
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<void> {
const versionFilePath = this.getControllerVersionFilePath();
await writeFile(versionFilePath, version, 'utf8');
}

/**
* Clears the tmp directory when switching controller versions
*/
private async clearTmpDirectory(): Promise<void> {
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<void> {
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),
Expand Down Expand Up @@ -87,6 +143,9 @@ export class ControllerSetup {
await this.setupJsController();
}

// Save the controller version for future reference
await this.saveControllerVersion(controllerVersion);

debug(' => done!');
}

Expand Down