-
Notifications
You must be signed in to change notification settings - Fork 121
/
CliRunner.test.ts
164 lines (147 loc) · 5.8 KB
/
CliRunner.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
import * as path from 'path';
import { Loader } from 'componentsjs';
import { CliRunner } from '../../../src/init/CliRunner';
import type { Initializer } from '../../../src/init/Initializer';
const initializer: jest.Mocked<Initializer> = {
handleSafe: jest.fn(),
} as any;
const loader: jest.Mocked<Loader> = {
instantiateFromUrl: jest.fn(async(): Promise<any> => initializer),
registerAvailableModuleResources: jest.fn(),
} as any;
jest.mock('componentsjs', (): any => ({
// eslint-disable-next-line @typescript-eslint/naming-convention
Loader: jest.fn((): Loader => loader),
}));
jest.spyOn(process, 'cwd').mockReturnValue('/var/cwd');
const write = jest.spyOn(process.stderr, 'write').mockImplementation(jest.fn());
const exit = jest.spyOn(process, 'exit').mockImplementation(jest.fn() as any);
describe('CliRunner', (): void => {
afterEach((): void => {
jest.clearAllMocks();
});
it('starts the server with default settings.', async(): Promise<void> => {
new CliRunner().run({
argv: [ 'node', 'script' ],
});
await initializer.handleSafe();
expect(Loader).toHaveBeenCalledTimes(1);
expect(Loader).toHaveBeenCalledWith({
mainModulePath: path.join(__dirname, '../../../'),
});
expect(loader.instantiateFromUrl).toHaveBeenCalledTimes(1);
expect(loader.instantiateFromUrl).toHaveBeenCalledWith(
'urn:solid-server:default:Initializer',
path.join(__dirname, '/../../../config/config-default.json'),
undefined,
{
variables: {
'urn:solid-server:default:variable:port': 3000,
'urn:solid-server:default:variable:baseUrl': 'http://localhost:3000/',
'urn:solid-server:default:variable:rootFilePath': process.cwd(),
'urn:solid-server:default:variable:sparqlEndpoint': undefined,
'urn:solid-server:default:variable:loggingLevel': 'info',
'urn:solid-server:default:variable:podTemplateFolder': path.join(__dirname, '../../../templates'),
},
},
);
expect(loader.registerAvailableModuleResources).toHaveBeenCalledTimes(1);
expect(loader.registerAvailableModuleResources).toHaveBeenCalledWith();
expect(initializer.handleSafe).toHaveBeenCalledTimes(1);
expect(initializer.handleSafe).toHaveBeenCalledWith();
});
it('accepts abbreviated flags.', async(): Promise<void> => {
new CliRunner().run({
argv: [
'node', 'script',
'-b', 'http://pod.example/',
'-c', 'myconfig.json',
'-f', '/root',
'-g',
'-l', 'debug',
'-m', 'module/path',
'-p', '4000',
'-s', 'http://localhost:5000/sparql',
'-t', 'templates',
],
});
await initializer.handleSafe();
expect(Loader).toHaveBeenCalledTimes(1);
expect(Loader).toHaveBeenCalledWith({
mainModulePath: '/var/cwd/module/path',
scanGlobal: true,
});
expect(loader.instantiateFromUrl).toHaveBeenCalledWith(
'urn:solid-server:default:Initializer',
'/var/cwd/myconfig.json',
undefined,
{
variables: {
'urn:solid-server:default:variable:baseUrl': 'http://pod.example/',
'urn:solid-server:default:variable:loggingLevel': 'debug',
'urn:solid-server:default:variable:podTemplateFolder': 'templates',
'urn:solid-server:default:variable:port': 4000,
'urn:solid-server:default:variable:rootFilePath': '/root',
'urn:solid-server:default:variable:sparqlEndpoint': 'http://localhost:5000/sparql',
},
},
);
});
it('accepts full flags.', async(): Promise<void> => {
new CliRunner().run({
argv: [
'node', 'script',
'--baseUrl', 'http://pod.example/',
'--config', 'myconfig.json',
'--globalModules',
'--loggingLevel', 'debug',
'--mainModulePath', 'module/path',
'--podTemplateFolder', 'templates',
'--port', '4000',
'--rootFilePath', '/root',
'--sparqlEndpoint', 'http://localhost:5000/sparql',
],
});
await initializer.handleSafe();
expect(Loader).toHaveBeenCalledTimes(1);
expect(Loader).toHaveBeenCalledWith({
mainModulePath: '/var/cwd/module/path',
scanGlobal: true,
});
expect(loader.instantiateFromUrl).toHaveBeenCalledWith(
'urn:solid-server:default:Initializer',
'/var/cwd/myconfig.json',
undefined,
{
variables: {
'urn:solid-server:default:variable:baseUrl': 'http://pod.example/',
'urn:solid-server:default:variable:loggingLevel': 'debug',
'urn:solid-server:default:variable:podTemplateFolder': 'templates',
'urn:solid-server:default:variable:port': 4000,
'urn:solid-server:default:variable:rootFilePath': '/root',
'urn:solid-server:default:variable:sparqlEndpoint': 'http://localhost:5000/sparql',
},
},
);
});
it('exits with output to stderr when instantiation fails.', async(): Promise<void> => {
loader.instantiateFromUrl.mockRejectedValueOnce(new Error('Fatal'));
new CliRunner().run();
await new Promise((resolve): any => setImmediate(resolve));
expect(write).toHaveBeenCalledTimes(2);
expect(write).toHaveBeenNthCalledWith(1,
expect.stringMatching(/^Error: could not instantiate server from .*config-default\.json/u));
expect(write).toHaveBeenNthCalledWith(2,
expect.stringMatching(/^Error: Fatal/u));
expect(exit).toHaveBeenCalledTimes(1);
expect(exit).toHaveBeenCalledWith(1);
});
it('exits without output to stderr when initialization fails.', async(): Promise<void> => {
initializer.handleSafe.mockRejectedValueOnce(new Error('Fatal'));
new CliRunner().run();
await new Promise((resolve): any => setImmediate(resolve));
expect(write).toHaveBeenCalledTimes(0);
expect(exit).toHaveBeenCalledTimes(1);
expect(exit).toHaveBeenCalledWith(1);
});
});