Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not import path to avoid issues in vscode extension, remove filenamify, add unit tests for getTestTitlePath #135

Merged
merged 1 commit into from
Mar 27, 2024
Merged
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
78 changes: 0 additions & 78 deletions client/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
"dependencies": {
"@suitest/types": "^4.6.0",
"ajv": "^6.12.6",
"filenamify": "^4.3.0",
"fs-extra": "^7.0.1",
"http-network-proxy": "^1.0.11",
"needle": "^2.9.1",
Expand Down Expand Up @@ -91,7 +90,7 @@
},
"mocha": {
"timeout": 72000,
"bail": true,
"bail": false,
"require": [
"source-map-support/register"
],
Expand Down
3 changes: 1 addition & 2 deletions client/src/RokuDevice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import * as fsExtra from 'fs-extra';
import * as querystring from 'needle/lib/querystring';
import type * as mocha from 'mocha';
import * as net from 'net';
import * as path from 'path';

import type { ConfigOptions } from './types/ConfigOptions';
import { utils } from './utils';
Expand Down Expand Up @@ -180,7 +179,7 @@ export class RokuDevice {
}

public async getTestScreenshot(contextOrSuite: mocha.Context | mocha.Suite, basePath = '', postFix = '', separator = '_') {
const screenshotPath = path.join(basePath, utils.getTestTitlePath(contextOrSuite).join(separator)) + postFix;
const screenshotPath = utils['getPath']().join(basePath, utils.getTestTitlePath(contextOrSuite).join(separator)) + postFix;
return await this.getScreenshot(screenshotPath);
}

Expand Down
12 changes: 12 additions & 0 deletions client/src/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,16 @@ describe('utils', function () {
assert.fail('Should have thrown an exception');
});
});

describe('getTestTitlePath', function () {
it('Does not mess with test name if sanitize is off', function () {
const result = utils.getTestTitlePath(this, false);
expect(result[2]).to.equal(this.test?.title);
});

it('Properly sanitizes if turned on ;,.:*^', function () {
const result = utils.getTestTitlePath(this, true);
expect(result[2]).to.equal('Properly_sanitizes_if_turned_on_______');
});
});
});
22 changes: 16 additions & 6 deletions client/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import type * as fsExtra from 'fs-extra';
import type * as path from 'path';
import type * as Mocha from 'mocha';
import * as filenamify from 'filenamify';
import * as Ajv from 'ajv';
const ajv = new Ajv();

import type { ConfigOptions, DeviceConfigOptions } from './types/ConfigOptions';
type PathType = typeof path;

class Utils {
private path?: typeof path;
private path?: PathType;

private fsExtra?: typeof fsExtra;

Expand All @@ -19,7 +19,7 @@ class Utils {

private getPath() {
if (!this.path) {
this.path = this.require<typeof path>('path');
this.path = this.require<PathType>('path');
}
return this.path;
}
Expand Down Expand Up @@ -210,14 +210,24 @@ class Utils {
throw new Error('Neither Mocha.Context or Mocha.Suite passed in');
}

if (!(ctx.currentTest?.constructor.name === 'Test')) {
let test: Mocha.Runnable;
if (ctx.currentTest?.constructor.name === 'Test') {
test = ctx.currentTest;
} else if (ctx.test?.constructor.name === 'Test') {
test = ctx.test;
} else {
throw new Error('Mocha.Context did not contain test. At least surrounding Mocha.Suite must use non arrow function');
}

const pathParts = ctx.currentTest?.titlePath();
const pathParts = test.titlePath();
if (sanitize) {
for (const [index, pathPart] of pathParts.entries()) {
pathParts[index] = filenamify(pathPart);
if (sanitize) {
pathParts[index] = pathPart.replace(/[^a-zA-Z0-9_]/g, '_');
} else {
pathParts[index] = pathPart;
}

}

}
Expand Down
Loading