Skip to content

Commit

Permalink
- исправление ошибки [#200](#200) - сохранение модульных тестов с нес…
Browse files Browse the repository at this point in the history
…колькими блоками expect и комментариями.
  • Loading branch information
DmitryOffsec committed Sep 8, 2024
1 parent 236d8f4 commit 04904a9
Show file tree
Hide file tree
Showing 14 changed files with 53 additions and 37 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.en.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Change Log

## 3.19.2 (Pre-Release)

- bug fix [#200](https://github.com/Security-Experts-Community/vscode-xp/issues/200) - saving unit tests with multiple expect blocks and comments.

## 3.19.1 (Pre-Release)

- the feature for updating integration tests when they change their files is temporarily disabled (special thanks @g4n8g).

## 3.19.0 (Pre-Release)

- fixed the error of replacing the expected event actually in integration tests (special thanks @Bobyboba18, @GeorgeFloyd_Official).
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log

## 3.19.2 (Pre-Release)

- исправление ошибки [#200](https://github.com/Security-Experts-Community/vscode-xp/issues/200) - сохранение модульных тестов с несколькими блоками expect и комментариями.

## 3.19.1 (Pre-Release)

- временно отключена фича по обновлению интеграционных тестов при их изменении их файлов (special thanks @g4n8g).
Expand Down
2 changes: 1 addition & 1 deletion client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "Language client",
"author": "Dmitry Fedosov (@DmitryOffsec)",
"license": "MIT",
"version": "3.19.1",
"version": "3.19.2",
"repository": {
"type": "git",
"url": "https://github.com/Security-Experts-Community/vscode-xp"
Expand Down
2 changes: 1 addition & 1 deletion client/src/helpers/testHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ export class TestHelper {
}
catch (error) {
// Если не удалось отформатировать, пропускаем и пишем в лог.
Log.error(error, `Не удалось отформатировать событие ${compressedEvent}`, );
Log.error(error, `Не удалось отформатировать событие ${compressedEvent}`);
continue;
}
}
Expand Down
2 changes: 1 addition & 1 deletion client/src/helpers/vsCodeApiHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export class VsCodeApiHelper {
const openFilePath = td.fileName.toLocaleLowerCase();
const ruleDirectoryPath = rule.getDirectoryPath().toLocaleLowerCase();

if(openFilePath.includes(ruleDirectoryPath) && FileSystemHelper.isIncludeDirectoryInPath(openFilePath, "tests") ) {
if(openFilePath.includes(ruleDirectoryPath) && FileSystemHelper.isIncludeDirectoryInPath(openFilePath, RuleBaseItem.TESTS_DIRNAME) ) {
await td.save();
}
});
Expand Down
10 changes: 6 additions & 4 deletions client/src/models/tests/baseUnitTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export abstract class BaseUnitTest extends vscode.TreeItem {
}

public getTestsDirPath() : string {
return path.join(this.getRuleDirectoryPath(), "tests");
return path.join(this.getRuleDirectoryPath(), RuleBaseItem.TESTS_DIRNAME);
}

public abstract getTestExpectationPath() : string;
Expand All @@ -47,11 +47,11 @@ export abstract class BaseUnitTest extends vscode.TreeItem {
}

public setTestExpectation(testExpectation: string): void {
this._testExpectation = testExpectation;
this.testExpectation = testExpectation;
}

public getTestExpectation() : string {
return this._testExpectation;
return this.testExpectation;
}

public getActualData() : string {
Expand Down Expand Up @@ -116,11 +116,13 @@ export abstract class BaseUnitTest extends vscode.TreeItem {
return this._rule;
}

public static UNIT_TEST_NEWLINE_SYMBOL = '\n';

protected _rule : RuleBaseItem;

private _number: number;

private _testExpectation : string;
private testExpectation : string;
private _actualEvent : string;

private _inputData : string;
Expand Down
38 changes: 18 additions & 20 deletions client/src/models/tests/correlationUnitTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Correlation } from '../content/correlation';
import { XpException } from '../xpException';
import { Enrichment } from '../content/enrichment';
import { TestHelper } from '../../helpers/testHelper';
import { StringHelper } from '../../helpers/stringHelper';

export class CorrelationUnitTest extends BaseUnitTest {

Expand Down Expand Up @@ -47,38 +48,39 @@ export class CorrelationUnitTest extends BaseUnitTest {
if (!fs.existsSync(filePath)){
throw new XpException(`Невозможно создать тест. Файла ${filePath} нет на диске`);
}
const testFileContent = fs.readFileSync(filePath, "utf8");
const testFileContent = FileSystemHelper.readContentFileSync(filePath);
const unitTest = rule.createNewUnitTest();

let inputData = "";
const inputDataStrings : string[] = [];

const table_list_default = /(?:^#.*$|\r?\n)*^table_list\s+default$/m.exec(testFileContent);
if (table_list_default && table_list_default.length === 1) {
inputData += '\n' + this.fixStrings(table_list_default[0]);
inputDataStrings.push(table_list_default[0].trim());
}

const table_list = /(?:^#.*$|\r?\n)*^table_list\s+\{.*?\}$/m.exec(testFileContent);
if (table_list && table_list.length === 1) {
inputData += '\n' + this.fixStrings(table_list[0]);
inputDataStrings.push(table_list_default[0].trim());
}

let m: RegExpExecArray;
const event_pattern = /(?:^#.*?$|\s)*(?:^\{.*?\}$)/gm;
while((m = event_pattern.exec(testFileContent))) {
inputData += '\n' + this.fixStrings(m[0]);
inputDataStrings.push(m[0].trim());
}

if (inputData && inputData !== '') {
unitTest.setTestInputData(inputData.trim());
}
const inputDataResultString = inputDataStrings.join(BaseUnitTest.UNIT_TEST_NEWLINE_SYMBOL);
unitTest.setTestInputData(inputDataResultString);

const pattern = /(?:^#.*$|\r?\n)*(?:^expect\s+(?:\d+|not)\s+\{.*?\}$)(?:\s*(?:^expect\s+(?:\d+|not)\s+\{.*?\}$))*/m;
const expectation = pattern.exec(testFileContent);
if(expectation && expectation.length === 1) {
const expectedCondition = expectation[0].replace(unitTest.getDefaultInputData(), '');
unitTest.setTestExpectation(this.fixStrings(expectedCondition));
// expected block
let expectationData = testFileContent;
for(const ids of inputDataStrings) {
expectationData = StringHelper.safeReplace(expectationData, ids, "");
}

expectationData = expectationData.trim();
unitTest.setTestExpectation(expectationData);

unitTest.command = {
command: UnitTestContentEditorViewProvider.onTestSelectionChangeCommand,
title: "ModularTestContentEditorViewProvider.onTestSelectionChangeCommand",
Expand All @@ -90,7 +92,7 @@ export class CorrelationUnitTest extends BaseUnitTest {

public static parseFromRuleDirectory(rule: Correlation | Enrichment) : CorrelationUnitTest [] {
const ruleDirectoryFullPath = rule.getDirectoryPath();
const testsFullPath = path.join(ruleDirectoryFullPath, "tests");
const testsFullPath = path.join(ruleDirectoryFullPath, RuleBaseItem.TESTS_DIRNAME);
if (!fs.existsSync(testsFullPath)){
return [];
}
Expand All @@ -113,7 +115,7 @@ export class CorrelationUnitTest extends BaseUnitTest {
const baseDirFullPath = rule.getDirectoryPath();
let testsFullPath : string;
if(baseDirFullPath) {
testsFullPath = path.join(baseDirFullPath, "tests");
testsFullPath = path.join(baseDirFullPath, RuleBaseItem.TESTS_DIRNAME);
}

for(let testNumber = 1; testNumber < CorrelationUnitTest.MAX_TEST_INDEX; testNumber++) {
Expand Down Expand Up @@ -147,18 +149,14 @@ export class CorrelationUnitTest extends BaseUnitTest {
throw new XpException("Для модульного теста не задан порядковый номер");
}

// if(!this.getTestExpectation()) {
// throw new XpException("Нельзя сохранять пустой тест");
// }

// Модульные тесты корреляций содержат условия и начальные данные в одном файле
const minifiedTestInput = this.getTestInputData();
this.setTestInputData(minifiedTestInput);

const minifiedTestExpectation = TestHelper.compressTestCode(this.getTestExpectation());
this.setTestExpectation(minifiedTestExpectation);

const fileContent = minifiedTestInput + '\n\n' + minifiedTestExpectation;
const fileContent = minifiedTestInput + BaseUnitTest.UNIT_TEST_NEWLINE_SYMBOL + BaseUnitTest.UNIT_TEST_NEWLINE_SYMBOL + minifiedTestExpectation;
const filePath = this.getTestExpectationPath();

return fs.writeFileSync(filePath, fileContent, FileSystemHelper._fileEncoding);
Expand Down
7 changes: 4 additions & 3 deletions client/src/models/tests/normalizationUnitTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { FileSystemHelper } from '../../helpers/fileSystemHelper';
import { Normalization } from '../content/normalization';
import { XpException } from '../xpException';
import { Log } from '../../extension';
import { RuleBaseItem } from '../content/ruleBaseItem';

export class NormalizationUnitTest extends BaseUnitTest {
public getDefaultExpectation(): string {
Expand All @@ -20,7 +21,7 @@ export class NormalizationUnitTest extends BaseUnitTest {
public static parseFromRuleDirectory(rule: Normalization) : NormalizationUnitTest [] {

const ruleDirectoryFullPath = rule.getDirectoryPath();
const testsFullPath = path.join(ruleDirectoryFullPath, "tests");
const testsFullPath = path.join(ruleDirectoryFullPath, RuleBaseItem.TESTS_DIRNAME);
if (!fs.existsSync(testsFullPath)){
return [];
}
Expand All @@ -29,7 +30,7 @@ export class NormalizationUnitTest extends BaseUnitTest {
.filter(f => f.endsWith(".js"))
.filter(f => fs.existsSync(f))
.map((f, _) => {
const expectedNormalizedEvent = fs.readFileSync(f, "utf8");
const expectedNormalizedEvent = FileSystemHelper.readContentFileSync(f);
const regex = /norm_(\d+)\.js/.exec(f);
if (regex && regex.length > 0) {
const index = parseInt(regex[1]);
Expand Down Expand Up @@ -85,7 +86,7 @@ export class NormalizationUnitTest extends BaseUnitTest {

public static createFromExistFile(rule : Normalization) : NormalizationUnitTest {
const baseDirFullPath = rule.getDirectoryPath();
const testsFullPath = path.join(baseDirFullPath, "tests");
const testsFullPath = path.join(baseDirFullPath, RuleBaseItem.TESTS_DIRNAME);

for(let testNumber = 1; testNumber < NormalizationUnitTest.MaxTestIndex; testNumber++) {
const testFullPath = path.join(testsFullPath, `norm_${testNumber}.js`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as path from 'path';

import { Correlation } from '../../../models/content/correlation';
import { TestFixture } from '../../helper';
import { RuleBaseItem } from '../../../models/content/ruleBaseItem';

suite('Сохранение интеграционных тестов', () => {

Expand All @@ -14,10 +15,10 @@ suite('Сохранение интеграционных тестов', () => {
const it = rule.createIntegrationTest();
await it.save();

const rawEventsFilePath = path.join(ruleParentPath, "SuperDuperCorrelation", "tests", "raw_events_1.json");
const rawEventsFilePath = path.join(ruleParentPath, "SuperDuperCorrelation", RuleBaseItem.TESTS_DIRNAME, "raw_events_1.json");
assert.ok(fs.existsSync(rawEventsFilePath));

const testCodeFilePath = path.join(ruleParentPath, "SuperDuperCorrelation", "tests", "test_conds_1.tc");
const testCodeFilePath = path.join(ruleParentPath, "SuperDuperCorrelation", RuleBaseItem.TESTS_DIRNAME, "test_conds_1.tc");
assert.ok(fs.existsSync(testCodeFilePath));
});

Expand Down
3 changes: 2 additions & 1 deletion client/src/test/tests/unit/enrichmentUnitTest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as path from 'path';

import { TestFixture } from '../../helper';
import { Enrichment } from '../../../models/content/enrichment';
import { RuleBaseItem } from '../../../models/content/ruleBaseItem';

suite('Модульный тест обогащения', () => {

Expand All @@ -19,7 +20,7 @@ suite('Модульный тест обогащения', () => {
const rule = await Enrichment.parseFromDirectory(rulePath);
const unitTest = rule.createNewUnitTest();

const expectTestPath = path.join(rulePath, "tests", "test_1.sc");
const expectTestPath = path.join(rulePath, RuleBaseItem.TESTS_DIRNAME, "test_1.sc");
const actualTestPath = unitTest.getTestExpectationPath();
assert.strictEqual(actualTestPath, expectTestPath);
});
Expand Down
3 changes: 2 additions & 1 deletion client/src/test/tests/unit/normalizationUnitTest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as path from 'path';

import { TestFixture } from '../../helper';
import { Normalization } from '../../../models/content/normalization';
import { RuleBaseItem } from '../../../models/content/ruleBaseItem';

suite('Модульный тест нормализации', () => {

Expand All @@ -19,7 +20,7 @@ suite('Модульный тест нормализации', () => {
const rule = await Normalization.parseFromDirectory(rulePath);
const unitTest = rule.createNewUnitTest();

const expectTestPath = path.join(rulePath, "tests", "norm_1.js");
const expectTestPath = path.join(rulePath, RuleBaseItem.TESTS_DIRNAME, "norm_1.js");
const actualTestPath = unitTest.getTestExpectationPath();
assert.strictEqual(actualTestPath, expectTestPath);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ export class UnitTestContentEditorViewProvider extends WebViewProviderBase {
}

default: {
DialogHelper.showError("Переданная команда не поддерживается");
DialogHelper.showError("The transmitted command is not supported");
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"author": "Dmitry Fedosov (@DmitryOffsec)",
"icon": "resources/xp.png",
"license": "MIT",
"version": "3.19.1",
"version": "3.19.2",
"repository": {
"type": "git",
"url": "https://github.com/Security-Experts-Community/vscode-xp"
Expand Down
2 changes: 1 addition & 1 deletion server/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "server",
"description": "Language server",
"version": "3.19.1",
"version": "3.19.2",
"author": "Dmitry Fedosov (@DmitryOffsec)",
"license": "MIT",
"engines": {
Expand Down

0 comments on commit 04904a9

Please sign in to comment.