Skip to content

Commit

Permalink
optimized for non-standard tags
Browse files Browse the repository at this point in the history
  • Loading branch information
DavertMik committed Mar 20, 2024
1 parent 4dfb98d commit 0435ed1
Showing 3 changed files with 70 additions and 10 deletions.
17 changes: 17 additions & 0 deletions example/features/tags3.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
@some-tag @priority-example @some_context-drop
Feature: Create Todos with BDD

@some-tag @priority-example @some_context-drop
Scenario: Create a single todo item @bdd
Given I have an empty todo list
When I create a todo 1
Then I see the new todo on my list

@some-tag @priority-example
Scenario: Create multiple todos @bdd
Given I have these todos on my list
| name |
| Milk |
| Butter |
| Bread |
Then I see 4 todos on my list
39 changes: 38 additions & 1 deletion tests/util_test.js
Original file line number Diff line number Diff line change
@@ -35,7 +35,7 @@ const createTestFiles = (folderName) => {

const cleanFiles = (folderName) => {
const targetPath = path.join(__dirname, '..', folderName);
fs.rmdirSync(targetPath, { recursive: true, force: true });
fs.rmSync(targetPath, { recursive: true, force: true });
};

describe('Utils', () => {
@@ -185,4 +185,41 @@ describe('Utils', () => {
expect(file1).not.to.include('@T4d7f20ed');
//expect(file2).not.to.include('@T22222');
});

it('should clean suite and test ids with non-standard tags', async () => {
createTestFiles('tags_examples');
const tagIdMap = {
tests: {
'Create Todos with BDD': '@T40257bf0',
'Create a single todo item': '@T40257bf1',
'Create multiple todos': '@T40257bf3',
},
suites: {
'Create Todos with BDD': '@S12345678',
},
}

const file0 = fs.readFileSync(path.join(process.cwd(), 'tags_examples', 'features', 'tags3.feature'), { encoding: 'utf8' });

const features = await analyse('**/tags3.feature', path.join(__dirname, '..', 'tags_examples'));
util.updateFiles(features, tagIdMap, path.join(__dirname, '..', 'tags_examples'));

const file1 = fs.readFileSync(path.join(process.cwd(), 'tags_examples', 'features', 'tags3.feature'), { encoding: 'utf8' });

expect(file1).to.include('@some-tag @priority-example @some_context-drop @T40257bf1');
expect(file1).to.include('@some-tag @priority-example @T40257bf3');


const updatedFeatures = await analyse('**/tags3.feature', path.join(__dirname, '..', 'tags_examples'));
const files = util.cleanFiles(updatedFeatures, {}, path.join(__dirname, '..', 'tags_examples'), true);

const file2 = fs.readFileSync(path.join(process.cwd(), 'tags_examples', 'features', 'tags3.feature'), { encoding: 'utf8' });

expect(file2).to.not.include('@T40257bf1');
expect(file2).to.include('@some-tag @priority-example @some_context-drop');

expect(file2.trim()).to.eql(file0.trim())
//expect(file2).not.to.include('@T22222');
});

});
24 changes: 15 additions & 9 deletions util.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
const insertLine = require('insert-line');
const fs = require('fs');

const tagRegex = /\@[\w\d\=\-\_\(\)\.\:\&]*[\w\d]/g;
const suiteRegex = /\@S[\w\d]{8}/g;
const testRegex = /\@T[\w\d]{8}/g;

const getSpace = (code) => {
const lines = code.split('\n');
let line = lines[0];
@@ -9,11 +13,11 @@ const getSpace = (code) => {
};

const getTitle = (name) => {
return name.replace(/@([\w\d\-\(\)\.\,\*:]+)/g, '').trim();
return name.replace(tagRegex, '').trim();
}

const parseTest = testTitle => {
const captures = testTitle.match(/@T([\w\d]+)/);
const captures = testTitle.match(testRegex);
if (captures) {
return captures[1];
}
@@ -22,7 +26,7 @@ const parseTest = testTitle => {
};

const parseSuite = suiteTitle => {
const captures = suiteTitle.match(/@S([\w\d]+)/);
const captures = suiteTitle.match(suiteRegex);
if (captures) {
return captures[1];
}
@@ -75,11 +79,11 @@ function updateFiles(features, testomatioMap, workDir) {
} else if (suite.tags.length) {
const hasId = suite.tags.map(t => '@' + t).find(t => t === id);
if (hasId) continue;
if (suite.tags.find(t => t.match(/@S([\w\d-]{8})/))) {
if (suite.tags.find(t => t.match(suiteRegex))) {
hasOtherIds = true;
continue;
}
const tags = getLine(featureFile, at - 1).split(' ').filter(v => v.startsWith('@'))
const tags = getLine(featureFile, at - 1).split(' ').filter(v => v.match(tagRegex))
insertLineToFile(featureFile, `${tags.join(' ')} ${id}`, { overwrite: true, at });
} else {
insertLineToFile(featureFile, `${id}`, { at });
@@ -110,13 +114,13 @@ function updateFiles(features, testomatioMap, workDir) {
if (scenario.tags.length) {
const hasId = scenario.tags.map(t => '@' + t).find(t => t === id);
if (hasId) continue;
if (suite.tags.find(t => t.match(/@S([\w\d-]{8})/))) {
if (suite.tags.find(t => t.match(suiteRegex))) {
hasOtherIds = true;
continue;
}
const at = scenario.line + 1 + lineInc;
const prevLine = getLine(file, at - 1)
const tags = prevLine.split(' ').filter(v => v.startsWith('@'))
const tags = prevLine.split(' ').filter(v => v.match(tagRegex))
insertLineToFile(file, ' '.repeat(spaceCount) + (`${tags.join(' ')} ${id}`.trim()), { overwrite: true, at });
} else {
insertLineToFile(file, `\n${' '.repeat(spaceCount)}${id}`, { overwrite: true, at: scenario.line + lineInc });
@@ -156,10 +160,12 @@ function cleanFiles(features, testomatioMap = {}, workDir, dangerous = false) {
suiteIds.forEach(sid => fileContent = fileContent.replace(new RegExp('[ \\t]*' + sid + '\\s'), ''))
testIds.forEach(tid => fileContent = fileContent.replace(new RegExp('[ \\t]*' + tid + '\\s'), ''))
} else {
fileContent = fileContent.replace(/[ \t]*@S([\w\d-]{8})\s/g, '');
fileContent = fileContent.replace(/[ \t]*@T([\w\d-]{8})\s/g, '');
fileContent = fileContent.replace(suiteRegex, '');
fileContent = fileContent.replace(testRegex, '');
}

fileContent = fileContent.split('\n').map(l => l.replace(/\s+$/, '')).join('\n');

files.push(file);
fs.writeFileSync(file, fileContent, (err) => {
if (err) throw err;

0 comments on commit 0435ed1

Please sign in to comment.