Skip to content

Commit 27d7fa2

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

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

src/MPQEditor/MPQEditor.ts

+26
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
* limitations under the License.
1515
*/
1616

17+
import * as fs from "fs";
18+
import * as path from "path";
1719
import * as vscode from "vscode";
1820

1921
import { getNonce } from "../Utils/external/Nonce";
@@ -48,6 +50,30 @@ export class MPQEditorProvider implements vscode.CustomTextEditorProvider {
4850
);
4951
}
5052

53+
/**
54+
* @brief A helper function to validate mpqName
55+
* @note It checks whether
56+
* (1) 'mpqName' already exists in 'dirPath' directory
57+
* (2) 'mpqName' has valid extension
58+
* @returns 'undefined' on success or the cause of failure otherwise
59+
*/
60+
public static validateMPQName(
61+
dirPath: string,
62+
mpqName: string
63+
): string | undefined {
64+
const mpqPath: string = path.join(dirPath, mpqName);
65+
66+
if (!mpqPath.endsWith(MPQEditorProvider.fileExtension)) {
67+
return "A file extension must be " + MPQEditorProvider.fileExtension;
68+
}
69+
70+
if (fs.existsSync(mpqPath)) {
71+
return `A file or folder ${mpqPath} already exists at this location. Please choose a different name.`;
72+
}
73+
74+
return undefined;
75+
}
76+
5177
constructor(private readonly context: vscode.ExtensionContext) {}
5278

5379
/**

src/Tests/MPQEditor/MPQEditor.test.ts

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright (c) 2023 Samsung Electronics Co., Ltd. All Rights Reserved
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import { assert } from "chai";
18+
import { MPQEditorProvider } from "../../MPQEditor/MPQEditor";
19+
import { TestBuilder } from "../TestBuilder";
20+
21+
suite("MPQEditor", function () {
22+
suite("MPQEditorProvider", function () {
23+
let testBuilder: TestBuilder;
24+
25+
setup(() => {
26+
testBuilder = new TestBuilder(this);
27+
testBuilder.setUp();
28+
});
29+
30+
teardown(() => {
31+
testBuilder.tearDown();
32+
});
33+
34+
suite("#validateMPQName", function () {
35+
test("test validateMPQName", function () {
36+
const dirPath: string = testBuilder.dirInTemp;
37+
const mpqName: string = "model-test-validateMPQName.mpq.json";
38+
39+
const retValue = MPQEditorProvider.validateMPQName(dirPath, mpqName);
40+
assert.isUndefined(retValue);
41+
});
42+
43+
test("NEG: test validateMPQName which exists", function () {
44+
const dirPath: string = testBuilder.dirInTemp;
45+
const mpqName: string =
46+
"model-test-validateMPQName_NEG_EXISTS.mpq.json";
47+
const content = `empty content`;
48+
49+
testBuilder.writeFileSync(mpqName, content);
50+
const retValue = MPQEditorProvider.validateMPQName(dirPath, mpqName);
51+
assert.isDefined(retValue);
52+
});
53+
54+
test("NEG: test validateMPQName with wrong extension", function () {
55+
const dirPath: string = testBuilder.dirInTemp;
56+
const mpqName: string = "model-test-validateMPQName_NEG_EXT.json";
57+
58+
const retValue = MPQEditorProvider.validateMPQName(dirPath, mpqName);
59+
assert.isDefined(retValue);
60+
});
61+
});
62+
});
63+
});

0 commit comments

Comments
 (0)