Skip to content

Commit

Permalink
[tests] Use dynamic import for 'chai' and 'sinon-chai' modules
Browse files Browse the repository at this point in the history
Signed-off-by: Victor Rubezhny <[email protected]>
  • Loading branch information
vrubezhny committed Dec 11, 2024
1 parent b219af4 commit 6f9c84f
Show file tree
Hide file tree
Showing 25 changed files with 2,856 additions and 2,788 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,6 @@
"@segment/analytics-next": "^1.76.0",
"@svgr/plugin-jsx": "^8.1.0",
"@types/chai": "^5.0.1",
"@typescript-eslint/eslint-plugin": "^8.18.0",
"@typescript-eslint/parser": "^8.18.0",
"@types/dockerode": "^3.3.32",
"@types/express": "^5.0.0",
"@types/fs-extra": "^11.0.4",
Expand All @@ -115,6 +113,8 @@
"@types/unzip-stream": "^0.3.4",
"@types/validator": "^13.12.2",
"@types/vscode": "1.82.0",
"@typescript-eslint/eslint-plugin": "^8.18.0",
"@typescript-eslint/parser": "^8.18.0",
"@uiw/codemirror-theme-github": "^4.23.6",
"@uiw/codemirror-theme-vscode": "^4.23.6",
"@uiw/react-codemirror": "^4.23.6",
Expand Down
289 changes: 146 additions & 143 deletions test/integration/odoWrapper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
*-----------------------------------------------------------------------------------------------*/

import { fail } from 'assert';
import { expect } from 'chai';
import * as fs from 'fs/promises';
import { suite, suiteSetup } from 'mocha';
import * as path from 'path';
Expand All @@ -17,178 +16,182 @@ import { Odo } from '../../src/odo/odoWrapper';
import { LoginUtil } from '../../src/util/loginUtil';
import { Platform } from '../../src/util/platform';

suite('./odo/odoWrapper.ts', function () {
const isOpenShift: boolean = Boolean(parseInt(process.env.IS_OPENSHIFT, 10)) || false;
const clusterUrl = process.env.CLUSTER_URL || 'https://api.crc.testing:6443';
const username = process.env.CLUSTER_USER || 'developer';
const password = process.env.CLUSTER_PASSWORD || 'developer';
void import('chai').then((chai) => {
const expect = chai.expect;

async function dirExists(path: string): Promise<boolean> {
try {
if ((await fs.stat(path)).isDirectory()) {
return true;
}
} catch {
// Ignore
}
return false;
}
suite('./odo/odoWrapper.ts', function () {
const isOpenShift: boolean = Boolean(parseInt(process.env.IS_OPENSHIFT, 10)) || false;
const clusterUrl = process.env.CLUSTER_URL || 'https://api.crc.testing:6443';
const username = process.env.CLUSTER_USER || 'developer';
const password = process.env.CLUSTER_PASSWORD || 'developer';

async function fileExists(path: string): Promise<boolean> {
try {
if ((await fs.stat(path)).isFile()) {
return true;
}
} catch {
// Ignore
}
return false;
}

async function checkOdoPreference() {
const odoUserPreferenceDir = `${Platform.getUserHomePath()}/.odo`;
const odoUserPreferenceFile = `${odoUserPreferenceDir}/preference.yaml`;
if (await dirExists(odoUserPreferenceDir)) {
if (!await fileExists(odoUserPreferenceFile)) {
fail(`checkOdoPreference: ODO preference file not found: ${odoUserPreferenceFile}! `);
async function dirExists(path: string): Promise<boolean> {
try {
if ((await fs.stat(path)).isDirectory()) {
return true;
}
} catch {
// Ignore
}
} else {
fail(`checkOdoPreference: ODO preference directory not found: ${odoUserPreferenceDir}!`)
return false;
}
}

suiteSetup(async function () {
await OdoPreference.Instance.getRegistries(); // This creates the ODO preferences, if needed
await checkOdoPreference();
if (isOpenShift) {
async function fileExists(path: string): Promise<boolean> {
try {
await LoginUtil.Instance.logout();
if ((await fs.stat(path)).isFile()) {
return true;
}
} catch {
// do nothing
// Ignore
}
await Oc.Instance.loginWithUsernamePassword(clusterUrl, username, password);
}
});

suiteTeardown(async function () {
// ensure projects are cleaned up
try {
await Oc.Instance.deleteProject('my-test-project-1');
} catch {
// do nothing
}
try {
await Oc.Instance.deleteProject('my-test-project-2');
} catch {
// do nothing
return false;
}

if (isOpenShift) {
await LoginUtil.Instance.logout();
async function checkOdoPreference() {
const odoUserPreferenceDir = `${Platform.getUserHomePath()}/.odo`;
const odoUserPreferenceFile = `${odoUserPreferenceDir}/preference.yaml`;
if (await dirExists(odoUserPreferenceDir)) {
if (!await fileExists(odoUserPreferenceFile)) {
fail(`checkOdoPreference: ODO preference file not found: ${odoUserPreferenceFile}! `);
}
} else {
fail(`checkOdoPreference: ODO preference directory not found: ${odoUserPreferenceDir}!`)
}
}
});

suite('components', function () {
const project1 = 'my-test-project-1';

let tmpFolder1: Uri;
let tmpFolder2: Uri;

suiteSetup(async function () {
await OdoPreference.Instance.getRegistries(); // This creates the ODO preferences, if needed
await checkOdoPreference();
await Oc.Instance.createProject(project1);
tmpFolder1 = Uri.parse(await promisify(tmp.dir)());
tmpFolder2 = Uri.parse(await promisify(tmp.dir)());
await Odo.Instance.createComponentFromFolder(
'nodejs',
undefined,
undefined,
'component1',
tmpFolder1,
'nodejs-starter',
);
await Odo.Instance.createComponentFromFolder(
'go',
undefined,
undefined,
'component2',
tmpFolder2,
'go-starter',
);
if (isOpenShift) {
try {
await LoginUtil.Instance.logout();
} catch {
// do nothing
}
await Oc.Instance.loginWithUsernamePassword(clusterUrl, username, password);
}
});

suiteTeardown(async function () {
// ensure projects are cleaned up
try {
await Oc.Instance.deleteProject('my-test-project-1');
} catch {
// do nothing
}
try {
await Oc.Instance.deleteProject('my-test-project-2');
} catch {
// do nothing
}

if (isOpenShift) {
await Oc.Instance.loginWithUsernamePassword(clusterUrl, username, password);
await LoginUtil.Instance.logout();
}
const newWorkspaceFolders = workspace.workspaceFolders.filter((workspaceFolder) => {
const fsPath = workspaceFolder.uri.fsPath;
return (fsPath !== tmpFolder1.fsPath && fsPath !== tmpFolder2.fsPath);
});
workspace.updateWorkspaceFolders(0, workspace.workspaceFolders.length, ...newWorkspaceFolders);
await fs.rm(tmpFolder1.fsPath, { force: true, recursive: true });
await fs.rm(tmpFolder2.fsPath, { force: true, recursive: true });
await Oc.Instance.deleteProject(project1);
});

test('describeComponent()', async function () {
const componentDescription1 = await Odo.Instance.describeComponent(tmpFolder1.fsPath);
expect(componentDescription1).to.exist;
expect(componentDescription1.managedBy).to.equal('odo');
const componentDescription2 = await Odo.Instance.describeComponent(tmpFolder2.fsPath);
expect(componentDescription2).to.exist;
expect(componentDescription2.managedBy).to.equal('odo');
suite('components', function () {
const project1 = 'my-test-project-1';

let tmpFolder1: Uri;
let tmpFolder2: Uri;

suiteSetup(async function () {
await checkOdoPreference();
await Oc.Instance.createProject(project1);
tmpFolder1 = Uri.parse(await promisify(tmp.dir)());
tmpFolder2 = Uri.parse(await promisify(tmp.dir)());
await Odo.Instance.createComponentFromFolder(
'nodejs',
undefined,
undefined,
'component1',
tmpFolder1,
'nodejs-starter',
);
await Odo.Instance.createComponentFromFolder(
'go',
undefined,
undefined,
'component2',
tmpFolder2,
'go-starter',
);
});

suiteTeardown(async function () {
if (isOpenShift) {
await Oc.Instance.loginWithUsernamePassword(clusterUrl, username, password);
}
const newWorkspaceFolders = workspace.workspaceFolders.filter((workspaceFolder) => {
const fsPath = workspaceFolder.uri.fsPath;
return (fsPath !== tmpFolder1.fsPath && fsPath !== tmpFolder2.fsPath);
});
workspace.updateWorkspaceFolders(0, workspace.workspaceFolders.length, ...newWorkspaceFolders);
await fs.rm(tmpFolder1.fsPath, { force: true, recursive: true });
await fs.rm(tmpFolder2.fsPath, { force: true, recursive: true });
await Oc.Instance.deleteProject(project1);
});

test('describeComponent()', async function () {
const componentDescription1 = await Odo.Instance.describeComponent(tmpFolder1.fsPath);
expect(componentDescription1).to.exist;
expect(componentDescription1.managedBy).to.equal('odo');
const componentDescription2 = await Odo.Instance.describeComponent(tmpFolder2.fsPath);
expect(componentDescription2).to.exist;
expect(componentDescription2.managedBy).to.equal('odo');
});
});
});

suite('create component', function() {
suite('create component', function() {

const COMPONENT_TYPE = 'dotnet50';
const COMPONENT_VERSION = 'latest';
const COMPONENT_TYPE = 'dotnet50';
const COMPONENT_VERSION = 'latest';

let tmpFolder: string;
let tmpFolder: string;

suiteSetup(async function() {
await checkOdoPreference();
tmpFolder = await promisify(tmp.dir)();
});
suiteSetup(async function() {
await checkOdoPreference();
tmpFolder = await promisify(tmp.dir)();
});

suiteTeardown(async function() {
await fs.rm(tmpFolder, { recursive: true, force: true });
});
suiteTeardown(async function() {
await fs.rm(tmpFolder, { recursive: true, force: true });
});

test('createComponentFromTemplateProject()', async function() {
await Odo.Instance.createComponentFromTemplateProject(tmpFolder, 'my-component', 8080,
COMPONENT_TYPE, COMPONENT_VERSION, OdoPreference.DEFAULT_DEVFILE_REGISTRY_NAME, 'dotnet50-example');
try {
await fs.access(path.join(tmpFolder, 'devfile.yaml'));
} catch {
fail('Expected devfile to be created');
}
});
test('createComponentFromTemplateProject()', async function() {
await Odo.Instance.createComponentFromTemplateProject(tmpFolder, 'my-component', 8080,
COMPONENT_TYPE, COMPONENT_VERSION, OdoPreference.DEFAULT_DEVFILE_REGISTRY_NAME, 'dotnet50-example');
try {
await fs.access(path.join(tmpFolder, 'devfile.yaml'));
} catch {
fail('Expected devfile to be created');
}
});

test('deleteComponentConfiguration()', async function() {
await Odo.Instance.deleteComponentConfiguration(tmpFolder);
try {
await fs.access(path.join(tmpFolder, 'devfile.yaml'));
fail('devfile.yaml should have been deleted')
} catch {
// deleted successfully
}
});
test('deleteComponentConfiguration()', async function() {
await Odo.Instance.deleteComponentConfiguration(tmpFolder);
try {
await fs.access(path.join(tmpFolder, 'devfile.yaml'));
fail('devfile.yaml should have been deleted')
} catch {
// deleted successfully
}
});

test('createComponentFromLocation()', async function() {
// the project already exists from the previous step,
// we just need to recreate the Devfile
await Odo.Instance.createComponentFromLocation('dotnet50', undefined, 'my-component', 8080, Uri.file(tmpFolder));
try {
await fs.access(path.join(tmpFolder, 'devfile.yaml'));
} catch {
fail('Expected devfile to be created');
}
test('createComponentFromLocation()', async function() {
// the project already exists from the previous step,
// we just need to recreate the Devfile
await Odo.Instance.createComponentFromLocation('dotnet50', undefined, 'my-component', 8080, Uri.file(tmpFolder));
try {
await fs.access(path.join(tmpFolder, 'devfile.yaml'));
} catch {
fail('Expected devfile to be created');
}
});
});
});

test('deleteComponentConfiguration');
test('deleteComponentConfiguration');

});
});
});
Loading

0 comments on commit 6f9c84f

Please sign in to comment.