Skip to content

Commit dc62095

Browse files
authored
test: support shared steps (#14248)
* test: support shared steps * test: rename regex for clearer intent * test: skip only the current iteration * test: rename regex for clearer intent
1 parent 214c8ed commit dc62095

File tree

1 file changed

+72
-27
lines changed

1 file changed

+72
-27
lines changed

packages/tests/scripts/fetch-test-plan-test-cases.ts

Lines changed: 72 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -123,37 +123,82 @@ async function run() {
123123

124124
//if (tags && tags.includes("VSCUSE")) {
125125
const steps = workItem.fields?.["Microsoft.VSTS.TCM.Steps"];
126+
const stepOrComprefRegex =
127+
/(<step\s[\s\S]*?<\/step>|<compref id="\d+" ref="\d+">)/gi;
126128
if (typeof steps === "string") {
127-
const stepBlocks = steps.match(/<step[\s\S]*?<\/step>/gi) || [];
128-
129-
stepBlocks.forEach((stepBlock, idx) => {
130-
const paramMatch = stepBlock.match(
131-
/<parameterizedString[^>]*>([\s\S]*?)<\/parameterizedString>/i
132-
);
133-
let text = "";
134-
if (paramMatch && paramMatch[1]) {
135-
const html = paramMatch[1]
136-
.replace(/&lt;/g, "<")
137-
.replace(/&gt;/g, ">");
138-
const pMatch =
139-
html.match(/<p>([\s\S]*?)<\/p>/i) ||
140-
html.match(/<P>([\s\S]*?)<\/P>/i);
141-
if (pMatch && pMatch[1]) {
142-
text = pMatch[1].replace(/<[^>]+>/g, "").trim();
129+
const stepBlocks = steps.match(stepOrComprefRegex) || [];
130+
131+
const getStepDetails = async (
132+
stepBlocks: string[],
133+
outputLines: string[]
134+
) => {
135+
for (const stepBlock of stepBlocks) {
136+
const paramMatch = stepBlock.match(
137+
/<parameterizedString[^>]*>([\s\S]*?)<\/parameterizedString>/i
138+
);
139+
let text = "";
140+
if (paramMatch && paramMatch[1]) {
141+
const html = paramMatch[1]
142+
.replace(/&lt;/g, "<")
143+
.replace(/&gt;/g, ">");
144+
const pMatch =
145+
html.match(/<p>([\s\S]*?)<\/p>/i) ||
146+
html.match(/<P>([\s\S]*?)<\/P>/i);
147+
if (pMatch && pMatch[1]) {
148+
text = pMatch[1].replace(/<[^>]+>/g, "").trim();
149+
}
150+
if (!text) {
151+
const match =
152+
stepBlock.match(/<p>([\s\S]*?)<\/p>/i) ||
153+
stepBlock.match(/<P>([\s\S]*?)<\/P>/i);
154+
if (match && match[1]) {
155+
text = match[1].replace(/<[^>]+>/g, "").trim();
156+
}
157+
}
158+
if (text) {
159+
outputLines.push(text);
160+
}
161+
continue;
143162
}
144-
}
145-
if (!text) {
146-
const match =
147-
stepBlock.match(/<p>([\s\S]*?)<\/p>/i) ||
148-
stepBlock.match(/<P>([\s\S]*?)<\/P>/i);
149-
if (match && match[1]) {
150-
text = match[1].replace(/<[^>]+>/g, "").trim();
163+
164+
const comprefMatch = stepBlock.match(
165+
/<compref id="\d+" ref="(\d+)">/i
166+
);
167+
if (comprefMatch && comprefMatch[1]) {
168+
const url = `https://msazure.visualstudio.com/_apis/wit/workItems/${comprefMatch[1]}`;
169+
console.log(
170+
`Fetching shared step work item from URL: ${url}`
171+
);
172+
try {
173+
// Use the Azure AD token for authentication
174+
const response = await fetch(url, {
175+
headers: {
176+
Authorization: `Bearer ${token.token}`,
177+
"Content-Type": "application/json",
178+
},
179+
});
180+
if (!response.ok) {
181+
console.error(
182+
`Failed to fetch work item: shared step ${comprefMatch[1]}, status: ${response.status}`
183+
);
184+
continue;
185+
}
186+
const workItem = (await response.json()) as WorkItem;
187+
const steps =
188+
workItem.fields?.["Microsoft.VSTS.TCM.Steps"];
189+
const stepBlocksChild =
190+
steps.match(stepOrComprefRegex) || [];
191+
await getStepDetails(stepBlocksChild, outputLines);
192+
} catch (err) {
193+
console.error(
194+
`Error fetching shared step work item ${comprefMatch[1]}:`,
195+
err
196+
);
197+
}
151198
}
152199
}
153-
if (text) {
154-
outputLines.push(text);
155-
}
156-
});
200+
};
201+
await getStepDetails(stepBlocks, outputLines);
157202
const filePath = path.join(outputDir, `${tc.testCase.id}.txt`);
158203
fs.writeFileSync(filePath, outputLines.join("\n"), {
159204
encoding: "utf8",

0 commit comments

Comments
 (0)