Skip to content

Commit cd52b9a

Browse files
committed
fix(templates): adds tests for getFiles and replaces Array.flat
1 parent 2693c05 commit cd52b9a

File tree

3 files changed

+140
-15
lines changed

3 files changed

+140
-15
lines changed

__tests__/templating/data.test.ts

+132-10
Original file line numberDiff line numberDiff line change
@@ -73,23 +73,145 @@ describe('base API configuration', () => {
7373
});
7474
});
7575

76-
describe('getTemplateFiles', () => {
76+
describe('with a mocked GitHub API', () => {
7777
const gitHubApi = nock('https://api.github.com');
7878

7979
afterEach(() => {
8080
gitHubApi.done();
8181
});
8282

83-
test('contacts the GitHub API', async () => {
84-
const { getTemplateFiles } = require('../../src/templating/data');
83+
describe('getTemplateFiles', () => {
84+
test('contacts the GitHub API', async () => {
85+
const { getTemplateFiles } = require('../../src/templating/data');
8586

86-
const basePath = '/repos/twilio-labs/function-templates/contents/blank';
87-
const templateContentScope = gitHubApi
88-
.get(`${basePath}?ref=master`)
89-
.reply(200, []);
90-
const result = await getTemplateFiles('blank');
87+
const basePath = '/repos/twilio-labs/function-templates/contents/blank';
88+
const templateContentScope = gitHubApi
89+
.get(`${basePath}?ref=master`)
90+
.reply(200, []);
91+
const result = await getTemplateFiles('blank');
9192

92-
expect(result).toEqual([]);
93-
expect(templateContentScope.isDone()).toBeTruthy();
93+
expect(result).toEqual([]);
94+
expect(templateContentScope.isDone()).toBeTruthy();
95+
});
96+
});
97+
98+
describe('getFiles', () => {
99+
const { getFiles } = require('../../src/templating/data');
100+
101+
test('gets a template directory from GH and translate to template file info', async () => {
102+
const basePath =
103+
'/repos/twilio-labs/function-templates/contents/blank/functions';
104+
const templateContentScope = gitHubApi
105+
.get(`${basePath}?ref=master`)
106+
.reply(200, [
107+
{
108+
name: 'blank.js',
109+
path: 'blank/functions/blank.js',
110+
sha: '8ffaf92aea1c5cd224fafa30165462c9eb0214bd',
111+
size: 80,
112+
url:
113+
'https://api.github.com/repos/twilio-labs/function-templates/contents/blank/functions/blank.js?ref=master',
114+
html_url:
115+
'https://github.com/twilio-labs/function-templates/blob/master/blank/functions/blank.js',
116+
git_url:
117+
'https://api.github.com/repos/twilio-labs/function-templates/git/blobs/8ffaf92aea1c5cd224fafa30165462c9eb0214bd',
118+
download_url:
119+
'https://raw.githubusercontent.com/twilio-labs/function-templates/master/blank/functions/blank.js',
120+
type: 'file',
121+
_links: {
122+
self:
123+
'https://api.github.com/repos/twilio-labs/function-templates/contents/blank/functions/blank.js?ref=master',
124+
git:
125+
'https://api.github.com/repos/twilio-labs/function-templates/git/blobs/8ffaf92aea1c5cd224fafa30165462c9eb0214bd',
126+
html:
127+
'https://github.com/twilio-labs/function-templates/blob/master/blank/functions/blank.js',
128+
},
129+
},
130+
]);
131+
132+
const files = await getFiles('blank', 'functions');
133+
expect(files.length).toEqual(1);
134+
const file = files[0];
135+
expect(file.directory).toEqual('');
136+
expect(file.name).toEqual('blank.js');
137+
expect(file.type).toEqual('functions');
138+
expect(file.content).toEqual(
139+
'https://raw.githubusercontent.com/twilio-labs/function-templates/master/blank/functions/blank.js'
140+
);
141+
});
142+
143+
test('gets a template directory with nested functions from GH and translate to template file info', async () => {
144+
const basePath =
145+
'/repos/twilio-labs/function-templates/contents/nested/functions';
146+
gitHubApi.get(`${basePath}?ref=master`).reply(200, [
147+
{
148+
name: 'blank.js',
149+
path: 'nested/functions/blank.js',
150+
sha: '8ffaf92aea1c5cd224fafa30165462c9eb0214bd',
151+
size: 80,
152+
url:
153+
'https://api.github.com/repos/twilio-labs/function-templates/contents/nested/functions/blank.js?ref=master',
154+
html_url:
155+
'https://github.com/twilio-labs/function-templates/blob/master/nested/functions/blank.js',
156+
git_url:
157+
'https://api.github.com/repos/twilio-labs/function-templates/git/blobs/8ffaf92aea1c5cd224fafa30165462c9eb0214bd',
158+
download_url:
159+
'https://raw.githubusercontent.com/twilio-labs/function-templates/master/nested/functions/blank.js',
160+
type: 'file',
161+
},
162+
{
163+
name: 'admin',
164+
path: 'blank/functions/admin',
165+
sha: '2e5bf79f4c70ad016b93e5563374867b18078d60',
166+
size: 0,
167+
url:
168+
'https://api.github.com/repos/twilio-labs/function-templates/contents/nested/functions/admin',
169+
html_url:
170+
'https://github.com/twilio-labs/function-templates/tree/nested-directory/nested-directory/functions/admin',
171+
git_url:
172+
'https://api.github.com/repos/twilio-labs/function-templates/git/trees/2e5bf79f4c70ad016b93e5563374867b18078d60',
173+
download_url: null,
174+
type: 'dir',
175+
},
176+
]);
177+
gitHubApi
178+
.get(
179+
`/repos/twilio-labs/function-templates/contents/nested/functions/admin`
180+
)
181+
.reply(200, [
182+
{
183+
name: 'admin.js',
184+
path: 'nested/functions/admin/admin.js',
185+
sha: '83f046ef49a8207b6876393074f638842590bccb',
186+
size: 116,
187+
url:
188+
'https://api.github.com/repos/twilio-labs/function-templates/contents/nested-directory/functions/admin/admin.js?ref=nested-directory',
189+
html_url:
190+
'https://github.com/twilio-labs/function-templates/blob/nested-directory/nested-directory/functions/admin/admin.js',
191+
git_url:
192+
'https://api.github.com/repos/twilio-labs/function-templates/git/blobs/83f046ef49a8207b6876393074f638842590bccb',
193+
download_url:
194+
'https://raw.githubusercontent.com/twilio-labs/function-templates/nested-directory/nested-directory/functions/admin/admin.js',
195+
type: 'file',
196+
},
197+
]);
198+
199+
const files = await getFiles('nested', 'functions');
200+
expect(files.length).toEqual(2);
201+
const file = files[0];
202+
expect(file.directory).toEqual('');
203+
expect(file.name).toEqual('blank.js');
204+
expect(file.type).toEqual('functions');
205+
expect(file.content).toEqual(
206+
'https://raw.githubusercontent.com/twilio-labs/function-templates/master/nested/functions/blank.js'
207+
);
208+
const nestedFile = files[1];
209+
expect(nestedFile.directory).toEqual('admin');
210+
expect(nestedFile.name).toEqual('admin.js');
211+
expect(nestedFile.type).toEqual('functions');
212+
expect(nestedFile.content).toEqual(
213+
'https://raw.githubusercontent.com/twilio-labs/function-templates/nested-directory/nested-directory/functions/admin/admin.js'
214+
);
215+
});
94216
});
95217
});

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
"listr": "^0.14.3",
6464
"lodash.camelcase": "^4.3.0",
6565
"lodash.debounce": "^4.0.8",
66+
"lodash.flatten": "^4.4.0",
6667
"lodash.kebabcase": "^4.1.1",
6768
"lodash.startcase": "^4.4.0",
6869
"log-symbols": "^2.2.0",
@@ -91,6 +92,7 @@
9192
"@types/jest": "^24.0.15",
9293
"@types/listr": "^0.14.0",
9394
"@types/lodash.debounce": "^4.0.6",
95+
"@types/lodash.flatten": "^4.4.6",
9496
"@types/lodash.kebabcase": "^4.1.6",
9597
"@types/lodash.startcase": "^4.4.6",
9698
"@types/prompts": "^2.0.1",

src/templating/data.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import path from 'path';
22
import { stripIndent } from 'common-tags';
33
import got from 'got';
4+
import flatten from 'lodash.flatten';
45
import { OutgoingHttpHeaders } from 'http';
56
import { getDebugFunction } from '../utils/logger';
67
const debug = getDebugFunction('twilio-run:new:template-data');
@@ -74,7 +75,7 @@ async function getNestedRepoContents(
7475
): Promise<TemplateFileInfo[]> {
7576
const response = await getFromUrl(url);
7677
const repoContents = response.body as RawContentsPayload;
77-
return (
78+
return flatten(
7879
await Promise.all(
7980
repoContents.map(async file => {
8081
if (file.type === 'dir') {
@@ -93,18 +94,18 @@ async function getNestedRepoContents(
9394
}
9495
})
9596
)
96-
).flat();
97+
);
9798
}
9899

99-
async function getFiles(
100+
export async function getFiles(
100101
templateId: string,
101102
directory: string
102103
): Promise<TemplateFileInfo[]> {
103104
const response = await getFromUrl(
104105
CONTENT_BASE_URL + `/${templateId}/${directory}?ref=${TEMPLATE_BASE_BRANCH}`
105106
);
106107
const repoContents = response.body as RawContentsPayload;
107-
return (
108+
return flatten(
108109
await Promise.all(
109110
repoContents.map(async file => {
110111
if (file.type === 'dir') {
@@ -119,7 +120,7 @@ async function getFiles(
119120
}
120121
})
121122
)
122-
).flat();
123+
);
123124
}
124125

125126
export async function getTemplateFiles(

0 commit comments

Comments
 (0)