Skip to content

Commit 5f760ae

Browse files
committed
write tests
1 parent 1382cc9 commit 5f760ae

2 files changed

Lines changed: 43 additions & 15 deletions

File tree

src/extension.ts

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import * as vscode from 'vscode';
2-
const path = require('path');
2+
import * as path from 'path';
33
import * as fs from 'fs';
44
import * as _ from 'lodash';
55

6-
function GetTemplates(templatePath: fs.PathLike): Array<Array<string>> {
6+
export function GetTemplates(templatePath: fs.PathLike): Array<Array<string>> {
77
const files = fs.readdirSync(templatePath);
88
const templates: Array<string> = [];
99
files.filter(e => e.includes(".dockerignore") ? false : true).forEach(file => {
@@ -12,14 +12,28 @@ function GetTemplates(templatePath: fs.PathLike): Array<Array<string>> {
1212
return [templates, files];
1313
}
1414

15-
function CreateDockerFile(option: any , templatePath: String, workspaceFolder: String): void {
16-
const data = fs.readFileSync(`${templatePath}/${option}.dockerfile`);
17-
fs.writeFileSync(`${workspaceFolder}/Dockerfile`, data);
15+
export function CreateDockerFile(option: any, templatePath: String, workspaceFolder: String): Boolean {
16+
try {
17+
const data = fs.readFileSync(`${templatePath}/${option}.dockerfile`);
18+
fs.writeFileSync(`${workspaceFolder}/Dockerfile`, data);
19+
} catch (error) {
20+
return false
21+
}
22+
finally {
23+
return true
24+
}
1825
}
1926

20-
function CreateIgnoreDockerFile(option: any , templatePath: String, workspaceFolder: String): void {
21-
const data = fs.readFileSync(`${templatePath}/.dockerignore ${option}`);
22-
fs.writeFileSync(`${workspaceFolder}/.dockerignore`, data);
27+
export function CreateIgnoreDockerFile(option: any, templatePath: String, workspaceFolder: String): Boolean {
28+
try {
29+
const data = fs.readFileSync(`${templatePath}/.dockerignore ${option}`);
30+
fs.writeFileSync(`${workspaceFolder}/.dockerignore`, data);
31+
} catch (error) {
32+
return false
33+
}
34+
finally {
35+
return true
36+
}
2337
}
2438

2539
export function activate(context: vscode.ExtensionContext) {
@@ -45,9 +59,9 @@ export function activate(context: vscode.ExtensionContext) {
4559
}
4660
else {
4761
vscode.window.showInformationMessage("workspaceFolder not found")
48-
}
62+
}
4963
})
5064
context.subscriptions.push(disposable);
5165
}
5266

53-
export function deactivate() {}
67+
export function deactivate() { }

src/test/suite/extension.test.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,29 @@
11
import * as assert from 'assert';
2-
2+
import * as path from 'path';
33
// You can import and use all API from the 'vscode' module
44
// as well as import your extension to test it
55
import * as vscode from 'vscode';
6-
// import * as myExtension from '../../extension';
6+
import { CreateDockerFile, CreateIgnoreDockerFile, GetTemplates } from '../../extension';
77

88
suite('Extension Test Suite', () => {
99
vscode.window.showInformationMessage('Start all tests.');
1010

11-
test('Sample test', () => {
12-
assert.strictEqual(-1, [1, 2, 3].indexOf(5));
13-
assert.strictEqual(-1, [1, 2, 3].indexOf(0));
11+
test('CreateDockerFile', () => {
12+
const templatePath = path.join(__dirname, '../templates');
13+
const { workspace: { workspaceFolders } } = vscode;
14+
const workspaceFolder = workspaceFolders ? workspaceFolders[0].uri.fsPath : path.join(__dirname, '../');
15+
assert.strictEqual(CreateDockerFile("Nodejs app",templatePath,workspaceFolder), true);
16+
});
17+
18+
test('CreateIgnoreDockerFile', () => {
19+
const templatePath = path.join(__dirname, '../templates');
20+
const { workspace: { workspaceFolders } } = vscode;
21+
const workspaceFolder = workspaceFolders ? workspaceFolders[0].uri.fsPath : path.join(__dirname, '../');
22+
assert.strictEqual(CreateIgnoreDockerFile("Nodejs app",templatePath,workspaceFolder), true);
23+
});
24+
25+
test('GetTemplates', () => {
26+
const templatePath = path.join(__dirname, '../../../templates');
27+
assert.strictEqual(GetTemplates(templatePath).length > 0 ? true : false, true);
1428
});
1529
});

0 commit comments

Comments
 (0)