Skip to content
Open
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
5 changes: 2 additions & 3 deletions api/tests/setup/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ import { TestEnv } from "./env";
import path from "path";
import { readdirSync } from "fs";


export const envVars = TestEnv.getEnvironmentVariables();
const testStorage = new JsonStorage();
const testConfig = new JsonConfig();

export async function createConnection(reloadSettings?: boolean) {
export async function newConnection(reloadSettings?: boolean) {
// Setup credentials
const envVars = TestEnv.getEnvironmentVariables();
const credentials: ConnectionData = {
name: `${envVars.VITE_IBMI_USER}@${envVars.VITE_IBMI_HOST}`,
host: envVars.VITE_IBMI_HOST,
Expand Down
9 changes: 9 additions & 0 deletions api/tests/setup/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ export class JsonStorage extends BaseStorage {
public save() {
this.config.save();
}

public matchesConnection(connectionName: string): boolean {
return this.config.has(`serverSettingsCache_${connectionName}`);
}
}

export class JsonConfig extends Config {
Expand All @@ -54,4 +58,9 @@ export class JsonConfig extends Config {
public save() {
this.config.save();
}

public matchesConnection(connectionName: string): boolean {
const settings = this.config.get(`connectionSettings`);
return Array.isArray(settings) && settings.some((s: any) => s.name === connectionName);
}
}
36 changes: 23 additions & 13 deletions api/tests/setup/setup.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,42 @@
import type { TestProject } from "vitest/node";
import { disposeConnection, createConnection } from "./connection";
import { disposeConnection, envVars, newConnection } from "./connection";
import { existsSync } from "fs";
import path from "path";
import { JsonConfig, JsonStorage } from "./json";
import { TestEnv } from "./env";

export async function setup(project: TestProject) {
console.log(`------------------------------`)
console.log(`------------------------------`);
TestEnv.logEnvironmentVariables();
console.log(`------------------------------`)
console.log(`------------------------------`);

const connectionName = `${envVars.VITE_IBMI_USER}@${envVars.VITE_IBMI_HOST}`;

const jsonStoragePath = path.join(__dirname, `..`, JsonStorage.NAME);
const jsonStorageExists = existsSync(jsonStoragePath);
const jsonConfigPath = path.join(__dirname, `..`, JsonConfig.NAME);
const jsonConfigExists = existsSync(jsonConfigPath);

const configsExist = jsonStorageExists && jsonConfigExists;
if (configsExist) {
console.log(`⏩ JSON server cache storage and connection config found. Skipping connection setup.\n`);
const configsExist = existsSync(jsonStoragePath) && existsSync(jsonConfigPath);
const testStorage = new JsonStorage();
const testConfig = new JsonConfig();
const configsMatchConnection = configsExist &&
testStorage.matchesConnection(connectionName) &&
testConfig.matchesConnection(connectionName);

if (configsMatchConnection) {
console.log(`⏩ JSON server cache storage and connection config found for ${connectionName}. Skipping new connection setup.\n`);
} else {
console.log(`⏳ JSON server cache storage and connection config not found. Starting connection setup.\n`);
const connection = await createConnection(true);
if (configsExist) {
console.log(`⏳ JSON server cache storage and connection config exist but do not match current connection (${connectionName}). Starting new connection setup.\n`);
} else {
console.log(`⏳ JSON server cache storage and connection config not found. Starting new connection setup.\n`);
}
const connection = await newConnection(true);
await disposeConnection(connection);
console.log(`✅ Connection setup complete.`);
}
console.log(`Server cache storage: ${jsonStoragePath}`)
console.log(`Connection config: ${jsonConfigPath}`)
console.log(`------------------------------\n`)
console.log(`Server cache storage: ${jsonStoragePath}`);
console.log(`Connection config: ${jsonConfigPath}`);
console.log(`------------------------------\n`);
}

export async function teardown() { }
7 changes: 3 additions & 4 deletions api/tests/suites/example.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import { afterAll, beforeAll, describe, expect, test } from 'vitest'
import { disposeConnection, createConnection } from '../setup/connection';
import { disposeConnection, envVars, newConnection } from '../setup/connection';
import IBMi from 'vscode-ibmi/src/api/IBMi';
import { TestEnv } from '../setup/env';
import { XMLParser } from '../../xmlParser';

describe('Sample suite', () => {
const envVars = TestEnv.getEnvironmentVariables();
let connection: IBMi

beforeAll(async () => {
connection = await createConnection();
}, envVars.VITE_CONNECTION_TIMEOUT)
connection = await newConnection();
}, envVars.VITE_CONNECTION_TIMEOUT);

afterAll(async () => {
await disposeConnection(connection);
Expand Down
Loading