-
Notifications
You must be signed in to change notification settings - Fork 192
/
exampleMiner.spec.ts
254 lines (230 loc) · 9.15 KB
/
exampleMiner.spec.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*
MIT License
Copyright (c) 2021 Looker Data Sciences, Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
import path from 'path';
import type { SDKCalls } from '@looker/sdk-codegen';
import {
CodeMiner,
ExampleMiner,
MarkdownMiner,
filterCodeFiles,
getAllFiles,
getCodeFiles,
getCommitHash,
getPermalinkRoot,
getRemoteHttpOrigin,
} from './exampleMiner';
describe('example mining', () => {
const sourcePath = path.join(__dirname, '/../../../examples');
const exampleFile = (fileName: string) =>
path.join(sourcePath, '/', fileName);
describe('gets files', () => {
it('all files', () => {
const actual = getAllFiles(sourcePath);
expect(actual.length).toBeGreaterThan(0);
});
it('code files', () => {
const actual = getCodeFiles(sourcePath);
expect(actual.length).toBeGreaterThan(0);
});
it('should get code files matching pattern', () => {
const filter = (pattern: RegExp, fileName: string) => {
const fileMatch = pattern.test(fileName);
return filterCodeFiles(fileName) && fileMatch;
};
const actual = getCodeFiles(
sourcePath,
undefined,
filter.bind(null, /^download_.*\.py/)
);
expect(actual).toHaveLength(3);
});
});
describe('link info functions', () => {
it('getCommitHash', () => {
const actual = getCommitHash();
expect(actual).toBeDefined();
expect(actual.length).toEqual(40);
});
it('getRemoteHttpOrigin', () => {
const actual = getRemoteHttpOrigin();
expect(
actual.startsWith('https://github.com/looker-open-source/sdk-codegen')
).toEqual(true);
});
it('getPermalinkRoot', () => {
const actual = getPermalinkRoot();
expect(actual).toEqual(
'https://github.com/looker-open-source/sdk-codegen'
);
});
});
describe('MarkdownMiner', () => {
const marker = new MarkdownMiner();
it('ignores http and https links', () => {
const ignore1 = 'http://foo.bar/.ts';
const ignore2 = 'HTTPS://foo.bar/.ts';
expect(marker.ignoreLink(ignore1)).toEqual(true);
expect(marker.ignoreLink(ignore2)).toEqual(true);
});
it('strips hash information from a file name', () => {
const expected = 'example/typescript/user.ts';
const hashed = 'example/typescript/user.ts#L1:L2';
expect(marker.stripSearch(hashed)).toEqual(expected);
});
it('accepts a source code file', () => {
const accept1 = 'example/typescript/user.ts';
const accept2 = 'example/typescript/user.ts#L1:L2';
expect(marker.ignoreLink(accept1)).toEqual(false);
expect(marker.ignoreLink(accept2)).toEqual(false);
});
describe('mineContent', () => {
describe('simple refs', () => {
it('processes standard url patterns', () => {
const md = '[summary1](example.ts#strip me!)';
const actual = marker.mineContent('example/typescript/README.md', md);
expect(actual.length).toEqual(1);
const first = actual[0];
expect(first.summary).toEqual('summary1');
expect(first.sourceFile).toEqual('example/typescript/example.ts');
});
it('processes multiple refs on the same line', () => {
const md =
'generators like [TypeScript](/packages/sdk-codegen/src/typescript.gen.ts) or [Python](/packages/sdk-codegen/src/python.gen.ts) are useful';
const actual = marker.mineContent(
'packages/sdk-codegen/README.md',
md
);
expect(actual.length).toEqual(2);
const first = actual[0];
expect(first.summary).toEqual('TypeScript');
expect(first.sourceFile).toEqual(
'packages/sdk-codegen/src/typescript.gen.ts'
);
const second = actual[1];
expect(second.summary).toEqual('Python');
expect(second.sourceFile).toEqual(
'packages/sdk-codegen/src/python.gen.ts'
);
});
});
it('processes link url patterns', () => {
const md =
'Logout all users on the instance [[link]](logout_all_users.rb)';
const actual = marker.mineContent('example/ruby/README.md', md);
expect(actual.length).toEqual(1);
const first = actual[0];
expect(first.summary).toEqual('Logout all users on the instance');
expect(first.sourceFile).toEqual('example/ruby/logout_all_users.rb');
});
it('processes link url patterns and strips leading dash', () => {
const md =
'\t - Logout all users on the instance [[link]](logout_all_users.rb)';
const actual = marker.mineContent('example/ruby/README.md', md);
expect(actual.length).toEqual(1);
const first = actual[0];
expect(first.summary).toEqual('Logout all users on the instance');
expect(first.sourceFile).toEqual('example/ruby/logout_all_users.rb');
});
it.skip('processes multiple link url patterns and strips leading dash', () => {
const md =
'\t - Logout all users on the instance [[link]](logout_all_users.rb) logs in [[link]](logs_in.rb)';
const actual = marker.mineContent('example/ruby/README.md', md);
expect(actual.length).toEqual(2);
const first = actual[0];
expect(first.summary).toEqual('Logout all users on the instance');
expect(first.sourceFile).toEqual('example/ruby/logout_all_users.rb');
const second = actual[0];
expect(second.summary).toEqual('logs in');
expect(second.sourceFile).toEqual('example/ruby/logs_in.rb');
});
});
it('processes a file', () => {
const fileName = exampleFile('ruby/README.md');
const actual = marker.mineFile(fileName);
expect(actual.length).toBeGreaterThanOrEqual(20);
});
});
describe('CodeMiner', () => {
const coder = new CodeMiner();
const probe = (code: string, expected: SDKCalls) => {
const actual = coder.mineCode(code);
expect(actual).toEqual(expected);
};
it('is empty for no sdk calls', () => {
probe('', []);
probe('one() two() three()', []);
probe('foo.one() bar.two() boo.three()', []);
probe('foo.one()\nbar.two()\nboo.three()', []);
});
it('ignores ok and finds ts calls', () => {
probe(`const value = await sdk.ok(sdk.me())`, [
{ sdk: 'sdk', operationId: 'me', line: 1, column: 27 },
]);
});
it('ignores ok and finds kotlin calls', () => {
probe(`val look = sdk.ok<Look>(sdk.create_look(WriteLookWithQuery(`, [
{ sdk: 'sdk', operationId: 'create_look', line: 1, column: 24 },
]);
});
it.skip('ignores comments and ok', () => {
probe(
`// this is a code comment sdk.comment()\nconst value = await coreSDK.ok(coreSDK.me())`,
[{ sdk: 'coreSDK', operationId: 'me', line: 2, column: 33 }]
);
});
it('mines a python file', () => {
const fileName = exampleFile('python/run_look_with_filters.py');
const actual = coder.mineFile(fileName);
expect(actual.length).toBeGreaterThan(0);
});
it('mines a ruby file', () => {
const fileName = exampleFile('ruby/delete_unused_content.rb');
const actual = coder.mineFile(fileName);
expect(actual.length).toBeGreaterThan(0);
});
it('mines a swift file', () => {
const fileName = exampleFile(
'swift/sample-swift-sdk/sample-swift-sdk/Dashboards.swift'
);
const actual = coder.mineFile(fileName);
expect(actual.length).toBeGreaterThan(0);
});
it('mines a typescript file', () => {
const fileName = exampleFile('typescript/utils.ts');
const actual = coder.mineFile(fileName);
expect(actual.length).toBeGreaterThan(0);
});
});
describe('Miner', () => {
it('relate', () => {
expect(ExampleMiner.relate('/a/b/c/', '/a/b/c/d.txt')).toEqual('d.txt');
expect(ExampleMiner.relate('/a/b/c/', '/a/b/c/d/e.txt')).toEqual(
'd/e.txt'
);
});
it('processes files', () => {
const miner = new ExampleMiner(sourcePath);
const actual = miner.lode;
expect(actual).toBeDefined();
expect(actual.commitHash).toBeDefined();
expect(Object.entries(actual.nuggets).length).toBeGreaterThan(40);
});
});
});