generated from shadow578/typescript-project-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PowerShellCall.test.ts
199 lines (173 loc) · 5.33 KB
/
PowerShellCall.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/* eslint-disable @typescript-eslint/no-unused-vars */
import { PowerShellCall, psCall } from '../../binding/PowerShellCall';
import { PowerShellBinding } from '../../binding/PowerShellBinding';
import { matchArray, matchObject } from '@shadow578/type-guardian';
import { isVoid, isString, isNumber, isStringArray } from '@shadow578/type-guardian/lib/TypeGuards';
interface Person {
FirstName: string;
LastName: string;
Age: number;
Friends: string[];
}
const isPerson = matchObject<Person>({
FirstName: 'string',
LastName: 'string',
Age: 'number',
Friends: matchArray('string'),
});
class ExampleBinding extends PowerShellBinding {
/**
* no parameters, no return value
*/
@PowerShellCall('$x = 0', isVoid)
nothing() {
return psCall<void>();
}
/**
* no parameters,
* @returns "Hello, World!"
*/
@PowerShellCall('Write-Output "Hello, World!"', isString)
getHelloWorld() {
return psCall<string>();
}
/**
* @param name the name to greet
* @returns "Hello, $name"
*/
@PowerShellCall('Write-Output "Hello, $name"', isString)
greet(name: string) {
return psCall<string>();
}
/**
* @param a the first number
* @param b the second number
* @returns result of a / b
*/
@PowerShellCall('Write-Output ($a / $b)', isNumber)
divide(a: number, b: number) {
return psCall<number>();
}
/**
* echos the input as-is.
* Note: the input is typed as string or number, but
* the return type is only allowed to be a string.
* this is done intentionally to easily test the type guard.
* In production, you should not do this!
*
* @param input the input to echo
* @returns the input, with same type
*/
@PowerShellCall('Write-Output $params.input', isString, { expandParameters: false })
echo(input: string | number) {
return psCall<string>();
}
/**
* @returns array [ first, second, third, fourth ]
*/
@PowerShellCall('Write-Output @($first, $second, $third, $fourth)', isStringArray)
getArray(first: string, second: string, third: string, fourth: string) {
return psCall<string[]>();
}
/**
* @returns object: { FirstName: "Peter", LastName: "Parker", Age: 24, Friends: [ "Tony", "Aunt May", "Uncle Ben" ] }
*/
@PowerShellCall(
'Write-Output (@{ FirstName = "Peter"; LastName = "Parker"; Age = 24; Friends = @( "Tony", "Aunt May", "Uncle Ben" ) })',
isPerson,
{ serializationDepth: 10 },
)
getPeter() {
return psCall<Person>();
}
/**
* @returns "person.FirstName $person.LastName"
*/
@PowerShellCall('Write-Output "$($person.FirstName) $($person.LastName)"', isString)
getPersonFullName(person: Person) {
return psCall<string>();
}
/**
* always throws a exception with the message "test exception"
*/
@PowerShellCall('throw "test exception"', isVoid)
throwExeption() {
return psCall<void>();
}
/**
* always throws a exception with the message "test exception".
* serialization depth is set to 100
*/
@PowerShellCall('throw "test exception"', isVoid, { serializationDepth: 100 })
throwExeptionDeep() {
return psCall<void>();
}
}
let binding: ExampleBinding;
beforeAll(() => {
binding = new ExampleBinding();
});
afterAll(() => {
binding.shell.dispose();
});
describe('@PowerShellCall', () => {
describe('executes powershell', () => {
test('no parameters, no return value', async () => {
await expect(binding.nothing()).resolves.toBe(undefined);
});
test('no parameters, return value', async () => {
await expect(binding.getHelloWorld()).resolves.toBe('Hello, World!');
});
test('parameters, return value', async () => {
await expect(binding.greet('Peter')).resolves.toBe('Hello, Peter');
});
test('multiple parameters, return value', async () => {
await expect(binding.divide(10, 2)).resolves.toBe(5);
});
test('return array', async () => {
await expect(binding.getArray('Hello', 'World', '!', 'This is a test')).resolves.toEqual([
'Hello',
'World',
'!',
'This is a test',
]);
});
test('return object', async () => {
await expect(binding.getPeter()).resolves.toEqual({
FirstName: 'Peter',
LastName: 'Parker',
Age: 24,
Friends: ['Tony', 'Aunt May', 'Uncle Ben'],
});
});
test('input object', async () => {
await expect(
binding.getPersonFullName({
FirstName: 'Peter',
LastName: 'Parker',
Age: 24,
Friends: [],
}),
).resolves.toBe('Peter Parker');
});
});
describe('type guard', () => {
test('valid return type accepted', async () => {
await expect(binding.echo('Test')).resolves.toBe('Test');
});
test('invalid return type rejected', async () => {
await expect(binding.echo(10)).rejects.toThrowError(TypeError);
});
});
describe('handles errors', () => {
test('re-throws powershell exception', async () => {
await expect(binding.divide(10, 0)).rejects.toThrowError(/RuntimeException/);
});
test('re-throws custom exception', async () => {
await expect(binding.throwExeption()).rejects.toThrowError(/test exception/);
});
test('serialization depth does not affect expeption re-throw', async () => {
await expect(binding.throwExeptionDeep()).rejects.toThrowError(/test exception/);
});
});
});