-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert.js
147 lines (127 loc) · 4.71 KB
/
convert.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
const fs = require('fs');
const csv = require('csv-parse/sync');
const { stringify } = require('csv-stringify/sync');
const path = require('path');
// Get input file from command line arguments
const inputFile = process.argv[2];
if (!inputFile) {
console.error('Please provide an input file path as an argument');
console.error('Usage: node convert.js <input-file.csv>');
process.exit(1);
}
// Generate output filename by adding _Testomatio before the extension
const parsedPath = path.parse(inputFile);
const outputFile = path.join(
parsedPath.dir,
`${parsedPath.name}_Testomatio${parsedPath.ext}`
);
function convertTestCases(inputFile, outputFile) {
// Read and parse input CSV
const inputData = fs.readFileSync(inputFile, 'utf-8');
const records = csv.parse(inputData, {
columns: true,
skip_empty_lines: true
});
// Define required columns
const requiredColumns = [
'Entity Key', 'Test Case Summary', 'Test Case Folder Path',
];
// Validate columns
const inputColumns = Object.keys(records[0]);
const missingColumns = requiredColumns.filter(col => !inputColumns.includes(col));
if (missingColumns.length > 0) {
console.error('The following required columns are missing from the input file:', missingColumns.join(', '));
process.exit(1);
}
let currentTestCase = null;
const transformedData = [];
records.forEach(record => {
if (record['Test Case Summary']) {
// If we encounter a new test case summary, push the previous one to the transformed data
if (currentTestCase) {
transformedData.push(currentTestCase);
}
console.log('✅', record['Test Case Summary'])
// Start a new test case
currentTestCase = {
'ID': `TS${record['Entity Key']}`,
'Title': record['Test Case Summary'],
'Folder': record['Test Case Folder Path'],
'Emoji': '',
'Priority': mapPriority(record['Test Case Priority']),
'Tags': record['Label(s)']?.split(',')?.map(tag => tag.trim())?.join(','),
'Owner': record['Created By']?.split('[')[0],
'Description': '',
'Examples': '',
'Labels': '',
'Url': '',
'Matched': '',
'Steps': []
};
}
// Add steps to the current test case
const stepDescription = record['Step Description'] || '';
const stepExpectedOutcome = record['Step Expected Outcome(Plain Text)'] || '';
if (stepDescription && !stepDescription.includes('Preconditions:')) {
if (!currentTestCase.Steps.join(' ').includes('## Steps')) {
currentTestCase.Steps.push('\n\n## Steps\n');
}
currentTestCase.Steps.push(`* ${stepDescription.trim()}`);
if (stepExpectedOutcome) {
currentTestCase.Steps.push(` *Expected:* ${stepExpectedOutcome.trim()}`);
}
}
// Add preconditions if they exist
if (stepDescription.includes('Preconditions:')) {
currentTestCase.Steps.push('\n## Precondition\n\n' + stepDescription
.split('Preconditions:')[1]
.trim());
}
});
// Push the last test case to the transformed data
if (currentTestCase) {
transformedData.push(currentTestCase);
}
// Transform the data to the target format
const finalData = transformedData.map(testCase => {
return {
'ID': testCase.ID,
'Title': testCase.Title,
'Folder': testCase.Folder,
'Emoji': '',
'Priority': testCase.Priority,
'Tags': testCase.Tags,
'Owner': testCase.Owner,
'Description': testCase.Steps.join('\n'),
'Examples': '',
'Labels': '',
'Url': '',
'Matched': ''
};
});
// Write to output CSV
const output = stringify(finalData, {
header: true,
columns: [
'ID', 'Title', 'Folder', 'Emoji', 'Priority',
'Tags', 'Owner', 'Description', 'Examples', 'Labels', 'Url', 'Matched'
]
});
fs.writeFileSync(outputFile, output);
console.log(`Conversion complete. Output written to ${outputFile}`);
}
function mapPriority(priority) {
const priorityMap = {
'Blocker': 'high',
'Critical': 'high',
'Major': 'normal',
'Minor': 'normal',
'Trivial': 'low'
};
return priorityMap[priority] || 'normal';
}
try {
convertTestCases(inputFile, outputFile);
} catch (error) {
console.error('Error during conversion:', error.message);
}