Skip to content

Commit 42cbea6

Browse files
committed
Add more tests and fixed incorrect behaviour in decompile task
1 parent f1d0852 commit 42cbea6

File tree

10 files changed

+218
-15
lines changed

10 files changed

+218
-15
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "azure-devops-bicep-task",
3-
"version": "0.3.2",
3+
"version": "0.3.3",
44
"description": "Tasks for installing Bicep CLI and running Bicep CLI build commands",
55
"main": "index.js",
66
"scripts": {

src/decompile/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export function checkForVersionCompatibility(outputProcess: OutputType): void {
9090

9191
const bicepVersionNumber = Number.parseFloat(bicepToolVersion);
9292
if (
93-
(bicepVersionNumber < 0.4 ||
93+
(bicepVersionNumber < 0.3 ||
9494
bicepToolVersion === '0.3.255' ||
9595
bicepToolVersion === '0.3.126' ||
9696
bicepToolVersion === '0.3.1') &&

src/decompile/task.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"version": {
1717
"Major": 0,
1818
"Minor": 3,
19-
"Patch": 2
19+
"Patch": 3
2020
},
2121
"instanceNameFormat": "Run Bicep CLI decompile command",
2222
"inputs": [

src/install/task.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"version": {
1717
"Major": 0,
1818
"Minor": 3,
19-
"Patch": 2
19+
"Patch": 3
2020
},
2121
"instanceNameFormat": "Install Bicep CLI",
2222
"inputs": [

src/run/task.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"version": {
1717
"Major": 0,
1818
"Minor": 3,
19-
"Patch": 2
19+
"Patch": 3
2020
},
2121
"instanceNameFormat": "Run Bicep CLI build command",
2222
"inputs": [
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import * as fs from 'fs';
2+
import { createDirectoryIfNotExists, getFilesList } from '../../../src/decompile/common/fileUtils';
3+
4+
jest.mock('fs');
5+
6+
const path = require('path');
7+
const glob = require('glob');
8+
9+
const existsSyncMock = jest.spyOn(fs, 'existsSync');
10+
const mkdirSyncMock = jest.spyOn(fs, 'mkdirSync');
11+
const pathJoinMock = jest.spyOn(path, 'join');
12+
const globHasMagicMock = jest.spyOn(glob, 'hasMagic');
13+
const globSyncMock = jest.spyOn(glob, 'sync');
14+
15+
function prepareMocks(exists = false) {
16+
existsSyncMock.mockImplementation(() => exists);
17+
mkdirSyncMock.mockImplementation(() => undefined);
18+
globSyncMock.mockImplementation(() => []);
19+
}
20+
21+
function restoreMocks() {
22+
existsSyncMock.mockClear();
23+
mkdirSyncMock.mockClear();
24+
pathJoinMock.mockClear();
25+
globHasMagicMock.mockClear();
26+
globSyncMock.mockClear();
27+
}
28+
29+
describe('getFilesList perform valid actions', () => {
30+
afterEach(() => restoreMocks());
31+
32+
test('if path has not wildcards', () => {
33+
prepareMocks();
34+
35+
getFilesList('./arm_templates');
36+
expect(globHasMagicMock).toHaveBeenCalled();
37+
expect(pathJoinMock).toHaveBeenCalled();
38+
expect(globSyncMock).toHaveBeenCalledWith('arm_templates/**', { nodir: true });
39+
});
40+
41+
test('if path has wildcards', () => {
42+
prepareMocks();
43+
44+
getFilesList('./arm_templates/*.json');
45+
expect(globHasMagicMock).toHaveBeenCalled();
46+
expect(pathJoinMock).not.toHaveBeenCalled();
47+
expect(globSyncMock).toHaveBeenCalledWith('./arm_templates/*.json', { nodir: true });
48+
});
49+
});
50+
51+
describe('createDirectoryIfNotExists creates directory if not exists', () => {
52+
afterEach(() => restoreMocks());
53+
54+
test('if path already exists', () => {
55+
prepareMocks(true);
56+
57+
createDirectoryIfNotExists('any_directory', false);
58+
expect(existsSyncMock).toHaveBeenCalled();
59+
expect(mkdirSyncMock).not.toHaveBeenCalled();
60+
});
61+
62+
test('if path not exists', () => {
63+
prepareMocks(false);
64+
65+
createDirectoryIfNotExists('any_directory', false);
66+
expect(existsSyncMock).toHaveBeenCalled();
67+
expect(mkdirSyncMock).toHaveBeenCalled();
68+
});
69+
});

test/decompile/index.spec.ts

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import { OutputType } from '../../src/run/common/enums/outputType';
2+
import { ProcessingType } from '../../src/run/common/enums/processingType';
3+
import { checkInputsAreValid, getOutputTypeForFiles, getProcessingTypeForFiles } from '../../src/decompile/index';
4+
5+
describe('checkInputsAreValid throws correct errors', () => {
6+
test('if source directory is not defined and process type is multiple', () => {
7+
expect(() =>
8+
checkInputsAreValid(undefined, undefined, 'multiple', undefined, undefined, undefined),
9+
).toThrowError("The variable 'sourceDirectory' is mandatory when process is 'multiple'.");
10+
});
11+
12+
test('if source directory is not defined and process type is undefined', () => {
13+
expect(() =>
14+
checkInputsAreValid(undefined, undefined, undefined, undefined, undefined, undefined),
15+
).toThrowError("The variable 'sourceDirectory' is mandatory when process is 'multiple'.");
16+
});
17+
18+
test('if source file is not defined and process type is single', () => {
19+
expect(() => checkInputsAreValid(undefined, undefined, 'single', undefined, undefined, undefined)).toThrowError(
20+
"The variable 'sourceFile' is mandatory when process is 'single'.",
21+
);
22+
});
23+
24+
test('if output directory is not defined and output type is outDir', () => {
25+
expect(() =>
26+
checkInputsAreValid('any path', undefined, 'multiple', 'outDir', undefined, undefined),
27+
).toThrowError("The variable 'outDir' is mandatory when outputProcess is 'outDir'.");
28+
});
29+
30+
test('if output file is not defined and output type is outFile', () => {
31+
expect(() =>
32+
checkInputsAreValid('any path', undefined, 'multiple', 'outFile', undefined, undefined),
33+
).toThrowError("The variable 'outFile' is mandatory when outputProcess is 'outFile'.");
34+
});
35+
});
36+
37+
describe('getProcessingTypeForFiles returns correct ProcessingType', () => {
38+
test('if source directory is defined and process type is multiple then processing type is multiple', () => {
39+
expect(getProcessingTypeForFiles('.', undefined, 'multiple')).toEqual(ProcessingType.Multiple);
40+
});
41+
42+
test('if source directory is not defined and process type is multiple then processing type is Invalid', () => {
43+
expect(getProcessingTypeForFiles(undefined, undefined, 'multiple')).toEqual(ProcessingType.Invalid);
44+
});
45+
46+
test('if source directory is defined and process type is undefined then processing type is multiple', () => {
47+
expect(getProcessingTypeForFiles('.', undefined, undefined)).toEqual(ProcessingType.Multiple);
48+
});
49+
50+
test('if source directory is not defined and process type is undefined then processing type is Invalid', () => {
51+
expect(getProcessingTypeForFiles(undefined, undefined, undefined)).toEqual(ProcessingType.Invalid);
52+
});
53+
54+
test('if source file is defined and process type is single then processing type is single', () => {
55+
expect(getProcessingTypeForFiles(undefined, '.', 'single')).toEqual(ProcessingType.Single);
56+
});
57+
58+
test('if source file is not defined and process type is single then processing type is Invalid', () => {
59+
expect(getProcessingTypeForFiles(undefined, undefined, 'single')).toEqual(ProcessingType.Invalid);
60+
});
61+
});
62+
63+
describe('getOutputTypeForFiles returns correct OutputType', () => {
64+
test('if stdout enabled then returns OutputType.Stdout', () => {
65+
expect(getOutputTypeForFiles(undefined, undefined, true, undefined)).toEqual(OutputType.Stdout);
66+
});
67+
68+
test('if output file is set then returns OutputType.OutFile', () => {
69+
expect(getOutputTypeForFiles(undefined, 'anyFile', undefined, undefined)).toEqual(OutputType.OutFile);
70+
});
71+
72+
test('if output directory is set then returns OutputType.OutDir', () => {
73+
expect(getOutputTypeForFiles('.', undefined, undefined, undefined)).toEqual(OutputType.OutDir);
74+
});
75+
76+
test('if no option is set then returns OutputType.Default', () => {
77+
expect(getOutputTypeForFiles(undefined, undefined, undefined, undefined)).toEqual(OutputType.Default);
78+
});
79+
80+
test('if outputProcess is set to stdout then returns OutputType.Stdout', () => {
81+
expect(getOutputTypeForFiles(undefined, undefined, undefined, 'stdout')).toEqual(OutputType.Stdout);
82+
});
83+
84+
test('if outputProcess is set to outFile then returns OutputType.OutFile', () => {
85+
expect(getOutputTypeForFiles(undefined, undefined, undefined, 'outFile')).toEqual(OutputType.OutFile);
86+
});
87+
88+
test('if outputProcess is set to outDir then returns OutputType.OutDir', () => {
89+
expect(getOutputTypeForFiles(undefined, undefined, undefined, 'outDir')).toEqual(OutputType.OutDir);
90+
});
91+
});

test/run/index.spec.ts

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,37 @@
1+
import { OutputType } from '../../src/run/common/enums/outputType';
12
import { ProcessingType } from '../../src/run/common/enums/processingType';
2-
import { checkInputsAreValid, getProcessingTypeForFiles } from '../../src/run/index';
3+
import { checkInputsAreValid, getOutputTypeForFiles, getProcessingTypeForFiles } from '../../src/run/index';
34

45
describe('checkInputsAreValid throws correct errors', () => {
56
test('if source directory is not defined and process type is multiple', () => {
6-
expect(() => checkInputsAreValid(undefined, undefined, 'multiple')).toThrowError(
7-
"The variable 'sourceDirectory' is mandatory when process is 'multiple'.",
8-
);
7+
expect(() =>
8+
checkInputsAreValid(undefined, undefined, 'multiple', undefined, undefined, undefined),
9+
).toThrowError("The variable 'sourceDirectory' is mandatory when process is 'multiple'.");
910
});
1011

1112
test('if source directory is not defined and process type is undefined', () => {
12-
expect(() => checkInputsAreValid(undefined, undefined, undefined)).toThrowError(
13-
"The variable 'sourceDirectory' is mandatory when process is 'multiple'.",
14-
);
13+
expect(() =>
14+
checkInputsAreValid(undefined, undefined, undefined, undefined, undefined, undefined),
15+
).toThrowError("The variable 'sourceDirectory' is mandatory when process is 'multiple'.");
1516
});
1617

1718
test('if source file is not defined and process type is single', () => {
18-
expect(() => checkInputsAreValid(undefined, undefined, 'single')).toThrowError(
19+
expect(() => checkInputsAreValid(undefined, undefined, 'single', undefined, undefined, undefined)).toThrowError(
1920
"The variable 'sourceFile' is mandatory when process is 'single'.",
2021
);
2122
});
23+
24+
test('if output directory is not defined and output type is outDir', () => {
25+
expect(() =>
26+
checkInputsAreValid('any path', undefined, 'multiple', 'outDir', undefined, undefined),
27+
).toThrowError("The variable 'outDir' is mandatory when outputProcess is 'outDir'.");
28+
});
29+
30+
test('if output file is not defined and output type is outFile', () => {
31+
expect(() =>
32+
checkInputsAreValid('any path', undefined, 'multiple', 'outFile', undefined, undefined),
33+
).toThrowError("The variable 'outFile' is mandatory when outputProcess is 'outFile'.");
34+
});
2235
});
2336

2437
describe('getProcessingTypeForFiles returns correct ProcessingType', () => {
@@ -46,3 +59,33 @@ describe('getProcessingTypeForFiles returns correct ProcessingType', () => {
4659
expect(getProcessingTypeForFiles(undefined, undefined, 'single')).toEqual(ProcessingType.Invalid);
4760
});
4861
});
62+
63+
describe('getOutputTypeForFiles returns correct OutputType', () => {
64+
test('if stdout enabled then returns OutputType.Stdout', () => {
65+
expect(getOutputTypeForFiles(undefined, undefined, true, undefined)).toEqual(OutputType.Stdout);
66+
});
67+
68+
test('if output file is set then returns OutputType.OutFile', () => {
69+
expect(getOutputTypeForFiles(undefined, 'anyFile', undefined, undefined)).toEqual(OutputType.OutFile);
70+
});
71+
72+
test('if output directory is set then returns OutputType.OutDir', () => {
73+
expect(getOutputTypeForFiles('.', undefined, undefined, undefined)).toEqual(OutputType.OutDir);
74+
});
75+
76+
test('if no option is set then returns OutputType.Default', () => {
77+
expect(getOutputTypeForFiles(undefined, undefined, undefined, undefined)).toEqual(OutputType.Default);
78+
});
79+
80+
test('if outputProcess is set to stdout then returns OutputType.Stdout', () => {
81+
expect(getOutputTypeForFiles(undefined, undefined, undefined, 'stdout')).toEqual(OutputType.Stdout);
82+
});
83+
84+
test('if outputProcess is set to outFile then returns OutputType.OutFile', () => {
85+
expect(getOutputTypeForFiles(undefined, undefined, undefined, 'outFile')).toEqual(OutputType.OutFile);
86+
});
87+
88+
test('if outputProcess is set to outDir then returns OutputType.OutDir', () => {
89+
expect(getOutputTypeForFiles(undefined, undefined, undefined, 'outDir')).toEqual(OutputType.OutDir);
90+
});
91+
});

vss-extension.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"manifestVersion": 1,
33
"id": "bicep-tasks",
4-
"version": "0.3.2",
4+
"version": "0.3.3",
55
"name": "Bicep Tasks",
66
"publisher": "piraces",
77
"description": "Provides Azure DevOps tasks to install and run Microsoft Bicep CLI commands (cross-platform)",

0 commit comments

Comments
 (0)