Skip to content

[MPQEditor] Implement 'validateMPQName' #1516

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 5, 2023
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
26 changes: 26 additions & 0 deletions src/MPQEditor/MPQEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*/

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

import { getNonce } from "../Utils/external/Nonce";
Expand Down Expand Up @@ -48,6 +50,30 @@ export class MPQEditorProvider implements vscode.CustomTextEditorProvider {
);
}

/**
* @brief A helper function to validate mpqName
* @note It checks whether
* (1) 'mpqName' already exists in 'dirPath' directory
* (2) 'mpqName' has valid extension
* @returns 'undefined' on success or the cause of failure otherwise
*/
public static validateMPQName(
dirPath: string,
mpqName: string
): string | undefined {
const mpqPath: string = path.join(dirPath, mpqName);

if (!mpqPath.endsWith(MPQEditorProvider.fileExtension)) {
return "A file extension must be " + MPQEditorProvider.fileExtension;
}

if (fs.existsSync(mpqPath)) {
return `A file or folder ${mpqPath} already exists at this location. Please choose a different name.`;
}

return undefined;
}

constructor(private readonly context: vscode.ExtensionContext) {}

/**
Expand Down
63 changes: 63 additions & 0 deletions src/Tests/MPQEditor/MPQEditor.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright (c) 2023 Samsung Electronics Co., Ltd. All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { assert } from "chai";
import { MPQEditorProvider } from "../../MPQEditor/MPQEditor";
import { TestBuilder } from "../TestBuilder";

suite("MPQEditor", function () {
suite("MPQEditorProvider", function () {
let testBuilder: TestBuilder;

setup(() => {
testBuilder = new TestBuilder(this);
testBuilder.setUp();
});

teardown(() => {
testBuilder.tearDown();
});

suite("#validateMPQName", function () {
test("test validateMPQName", function () {
const dirPath: string = testBuilder.dirInTemp;
const mpqName: string = "model-test-validateMPQName.mpq.json";

const retValue = MPQEditorProvider.validateMPQName(dirPath, mpqName);
assert.isUndefined(retValue);
});

test("NEG: test validateMPQName which exists", function () {
const dirPath: string = testBuilder.dirInTemp;
const mpqName: string =
"model-test-validateMPQName_NEG_EXISTS.mpq.json";
const content = `empty content`;

testBuilder.writeFileSync(mpqName, content);
const retValue = MPQEditorProvider.validateMPQName(dirPath, mpqName);
assert.isDefined(retValue);
});

test("NEG: test validateMPQName with wrong extension", function () {
const dirPath: string = testBuilder.dirInTemp;
const mpqName: string = "model-test-validateMPQName_NEG_EXT.json";

const retValue = MPQEditorProvider.validateMPQName(dirPath, mpqName);
assert.isDefined(retValue);
});
});
});
});