Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/MPQEditor/MPQEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,38 @@ export class MPQEditorProvider implements vscode.CustomTextEditorProvider {
);
}

/**
* @brief create file with default mpq configuration
* @returns valid uri of file on success or undefined on failure
*/
public static async createDefaultMPQ(
mpqName: string,
dirPath: string,
circleName: string
): Promise<vscode.Uri | undefined> {
const content = `{"default_quantization_dtype": "uint8",
"default_granularity": "channel",
"layers": [],
"model_path": "${circleName}"}`;

// 'uri' path is not occupied, assured by validateInputPath
const uri = vscode.Uri.file(`${dirPath}/${mpqName}`);

const edit = new vscode.WorkspaceEdit();
edit.createFile(uri);
edit.insert(uri, new vscode.Position(0, 0), content);

try {
await vscode.workspace.applyEdit(edit);
let document = await vscode.workspace.openTextDocument(uri);
document.save();
} catch (error) {
return undefined;
}

return uri;
}

/**
* @brief A helper function to validate mpqName
* @note It checks whether
Expand Down
39 changes: 39 additions & 0 deletions src/Tests/MPQEditor/MPQEditor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
* limitations under the License.
*/

import * as fs from "fs";
import * as path from "path";

import { assert } from "chai";
import { MPQEditorProvider } from "../../MPQEditor/MPQEditor";
import { TestBuilder } from "../TestBuilder";
Expand Down Expand Up @@ -92,5 +95,41 @@ suite("MPQEditor", function () {
assert.throws(() => MPQEditorProvider.findMPQName("", dirPath));
});
});

suite("#createDefaultMPQ", function () {
test("test createDefaultMPQ", function () {
// create dummy mpq.json file
const dirPath: string = testBuilder.dirInTemp;
const mpqName: string = "model-test-createMPQ.mpq.json";
const circleName: string = "model-test-createMPQ.circle";
MPQEditorProvider.createDefaultMPQ(mpqName, dirPath, circleName).then(
(uri) => {
assert.isTrue(uri !== undefined);
const mpqPath: string = path.join(dirPath, mpqName);
assert.isTrue(fs.existsSync(mpqPath));
const contents: string = fs.readFileSync(mpqPath, "utf-8");
const cont: any = JSON.parse(contents);
assert.strictEqual(cont["default_quantization_dtype"], "uint8");
assert.strictEqual(cont["default_granularity"], "channel");
assert.strictEqual(cont["model_path"], circleName);
}
);
});
test("NEG: test createDefaultMPQ on exsisting file", function () {
// create dummy mpq.json file
const dirPath: string = testBuilder.dirInTemp;
const mpqName: string = "model-test-createMPQ_NEG.mpq.json";
const circleName: string = "model-test-createMPQ_NEG.circle";

const content = `empty content`;
testBuilder.writeFileSync(mpqName, content);

MPQEditorProvider.createDefaultMPQ(mpqName, dirPath, circleName).then(
(uri) => {
assert.isTrue(uri === undefined);
}
);
});
});
});
});