Skip to content

Commit 08972a3

Browse files
authored
[MPQEditor] Implement 'findMPQName' (#1518)
This commit implements 'findMPQName' and adds tests for it. ONE-vscode-DCO-1.0-Signed-off-by: s.malakhov <[email protected]>
1 parent 27d7fa2 commit 08972a3

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

src/MPQEditor/MPQEditor.ts

+35
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
import * as fs from "fs";
18+
import * as glob from "glob";
1819
import * as path from "path";
1920
import * as vscode from "vscode";
2021

@@ -74,6 +75,40 @@ export class MPQEditorProvider implements vscode.CustomTextEditorProvider {
7475
return undefined;
7576
}
7677

78+
/**
79+
* @brief A helper function to find unoccupied mpq file-name
80+
* @returns valid file name for mpq configuration or undefined on failure
81+
* @throw Error, when input is invalid (e.g. baseMPQName is empty)
82+
*/
83+
public static findMPQName(
84+
baseMPQName: string,
85+
dirPath: string
86+
): string | undefined {
87+
if (baseMPQName.length === 0) {
88+
throw new Error("Invalid mixed precision quantization file name");
89+
}
90+
91+
const baseName = baseMPQName;
92+
let mpqName: string | undefined = undefined;
93+
94+
const options = { cwd: dirPath };
95+
// set maximal trials as maximal quantity of files + 1
96+
const files = glob.sync("*" + MPQEditorProvider.fileExtension, options);
97+
const maxMPQIndex = files.length + 1;
98+
99+
for (let i = 0; i < maxMPQIndex; i++) {
100+
mpqName = baseMPQName + MPQEditorProvider.fileExtension;
101+
const mpqPath: string = path.join(dirPath, mpqName);
102+
if (!fs.existsSync(mpqPath)) {
103+
break;
104+
}
105+
baseMPQName = baseName + `(${i + 1})`;
106+
mpqName = undefined;
107+
}
108+
109+
return mpqName;
110+
}
111+
77112
constructor(private readonly context: vscode.ExtensionContext) {}
78113

79114
/**

src/Tests/MPQEditor/MPQEditor.test.ts

+33
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,38 @@ suite("MPQEditor", function () {
5959
assert.isDefined(retValue);
6060
});
6161
});
62+
63+
suite("#findMPQName", function () {
64+
test("test findMPQName", function () {
65+
const baseMPQName: string = "model";
66+
const dirPath: string = testBuilder.dirInTemp;
67+
68+
const content = `
69+
empty content
70+
`;
71+
72+
let targetNames: string[] = [
73+
"model.mpq.json",
74+
"model(1).mpq.json",
75+
"model(2).mpq.json",
76+
"model(3).mpq.json",
77+
];
78+
79+
for (let i = 0; i < targetNames.length; i++) {
80+
// Get file paths inside the temp directory
81+
const mpqName = MPQEditorProvider.findMPQName(baseMPQName, dirPath);
82+
assert.isDefined(mpqName);
83+
assert.strictEqual(mpqName, targetNames[i]);
84+
85+
// create dummy mpq.json file
86+
testBuilder.writeFileSync(mpqName!, content);
87+
}
88+
});
89+
90+
test("NEG: findMPQName throws on empty string", function () {
91+
const dirPath: string = testBuilder.dirInTemp;
92+
assert.throws(() => MPQEditorProvider.findMPQName("", dirPath));
93+
});
94+
});
6295
});
6396
});

0 commit comments

Comments
 (0)