-
Notifications
You must be signed in to change notification settings - Fork 66.9k
Expand file tree
/
Copy pathliquid.js
More file actions
245 lines (220 loc) · 9.57 KB
/
liquid.js
File metadata and controls
245 lines (220 loc) · 9.57 KB
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
import { jest } from '@jest/globals'
import { liquid } from '../../lib/render-content/index.js'
import shortVersionsMiddleware from '../../middleware/contextualizers/short-versions.js'
import featureVersionsMiddleware from '../../middleware/contextualizers/features.js'
import { allVersions } from '../../lib/all-versions.js'
import enterpriseServerReleases from '../../lib/enterprise-server-releases.js'
const template = `
{% if currentVersion ver_gt "[email protected]" %}up to date{% endif %}
{% if currentVersion ver_lt "[email protected]" %}out of date{% endif %}
`
// Setup these variables so we don't need to manually update tests as GHES
// versions continually get deprecated. For example, if we deprecate GHES 3.0,
// oldestSupportedGhes will be 3.1, secondOldestSupportedGhes will be 3.2, and
// thirdOldestSupportedGhes will be 3.3.
const oldestSupportedGhes =
enterpriseServerReleases.supported[enterpriseServerReleases.supported.length - 1]
const secondOldestSupportedGhes =
enterpriseServerReleases.supported[enterpriseServerReleases.supported.length - 2]
const thirdOldestSupportedGhes =
enterpriseServerReleases.supported[enterpriseServerReleases.supported.length - 3]
const shortVersionsTemplate = `
{% ifversion fpt %} I am FPT {% endif %}
{% ifversion ghae %} I am GHAE {% endif %}
{% ifversion ghec %} I am GHEC{% endif %}
{% ifversion ghes %} I am GHES {% endif %}
{% ifversion ghes = ${secondOldestSupportedGhes} %} I am GHES = ${secondOldestSupportedGhes} {% endif %}
{% ifversion ghes > ${secondOldestSupportedGhes} %} I am GHES > ${secondOldestSupportedGhes} {% endif %}
{% ifversion ghes < ${secondOldestSupportedGhes} %} I am GHES < ${secondOldestSupportedGhes} {% endif %}
{% ifversion fpt or ghes < ${secondOldestSupportedGhes} %} I am FTP or GHES < ${secondOldestSupportedGhes} {% endif %}
{% ifversion ghes < ${thirdOldestSupportedGhes} and ghes > ${oldestSupportedGhes} %} I am ${secondOldestSupportedGhes} only {% endif %}
`
const negativeVersionsTemplate = `
{% ifversion not ghae %} I am not GHAE {% endif %}
{% ifversion not ghec %} I am not GHEC {% endif %}
{% ifversion not ghes %} I am not GHES {% endif %}
{% ifversion ghes != ${secondOldestSupportedGhes} %} I am not GHES ${secondOldestSupportedGhes} {% endif %}
`
const featureVersionsTemplate = `
{% if placeholder %} I am placeholder content {% endif %}
`
describe('liquid template parser', () => {
jest.setTimeout(60 * 1000)
describe('custom operators', () => {
describe('ver_gt', () => {
test('works as expected', async () => {
const context = { currentVersion: '[email protected]' }
const output = await liquid.parseAndRender(template, context)
expect(output.trim()).toBe('up to date')
})
test('returns false when given value is not numeric, like `dotcom`', async () => {
const context = { currentVersion: 'free-pro-team@latest' }
const output = await liquid.parseAndRender(template, context)
expect(output.trim()).toBe('')
})
test('returns false when given value is falsy', async () => {
const context = {}
const output = await liquid.parseAndRender(template, context)
expect(output.trim()).toBe('')
})
})
describe('ver_lt', () => {
test('works as expected', async () => {
const context = { currentVersion: '[email protected]' }
const output = await liquid.parseAndRender(template, context)
expect(output.trim()).toBe('out of date')
})
})
})
describe('short versions', () => {
// Create a fake req so we can test the shortVersions middleware
const req = { language: 'en', query: {} }
test('FPT works as expected when it is FPT', async () => {
req.context = {
currentVersion: 'free-pro-team@latest',
page: {},
allVersions,
enterpriseServerReleases,
}
await shortVersionsMiddleware(req, null, () => {})
const output = await liquid.parseAndRender(shortVersionsTemplate, req.context)
// We should have TWO results because we are supporting two shortcuts
expect(output.replace(/\s\s+/g, ' ').trim()).toBe(
`I am FPT I am FTP or GHES < ${secondOldestSupportedGhes}`
)
})
test('GHAE works as expected', async () => {
req.context = {
currentVersion: 'github-ae@latest',
page: {},
allVersions,
enterpriseServerReleases,
}
await shortVersionsMiddleware(req, null, () => {})
const output = await liquid.parseAndRender(shortVersionsTemplate, req.context)
expect(output.trim()).toBe('I am GHAE')
})
test('GHEC works as expected', async () => {
req.context = {
currentVersion: 'enterprise-cloud@latest',
page: {},
allVersions,
enterpriseServerReleases,
}
await shortVersionsMiddleware(req, null, () => {})
const output = await liquid.parseAndRender(shortVersionsTemplate, req.context)
expect(output.trim()).toBe('I am GHEC')
})
test('GHES works as expected', async () => {
req.context = {
currentVersion: `enterprise-server@${oldestSupportedGhes}`,
page: {},
allVersions,
enterpriseServerReleases,
}
await shortVersionsMiddleware(req, null, () => {})
const output = await liquid.parseAndRender(shortVersionsTemplate, req.context)
expect(output.replace(/\s\s+/g, ' ').trim()).toBe(
`I am GHES I am GHES < ${secondOldestSupportedGhes} I am FTP or GHES < ${secondOldestSupportedGhes}`
)
})
test('AND statements work as expected', async () => {
req.context = {
currentVersion: `enterprise-server@${secondOldestSupportedGhes}`,
page: {},
allVersions,
enterpriseServerReleases,
}
await shortVersionsMiddleware(req, null, () => {})
const output = await liquid.parseAndRender(shortVersionsTemplate, req.context)
expect(output.replace(/\s\s+/g, ' ').trim()).toBe(
`I am GHES I am GHES = ${secondOldestSupportedGhes} I am ${secondOldestSupportedGhes} only`
)
})
test('NOT statements work as expected on versions without numbered releases', async () => {
req.context = {
currentVersion: 'github-ae@latest',
page: {},
allVersions,
enterpriseServerReleases,
}
await shortVersionsMiddleware(req, null, () => {})
const output = await liquid.parseAndRender(negativeVersionsTemplate, req.context)
expect(output.replace(/\s\s+/g, ' ').trim()).toBe(
`I am not GHEC I am not GHES I am not GHES ${secondOldestSupportedGhes}`
)
})
test('NOT statements work as expected on versions with numbered releases', async () => {
req.context = {
currentVersion: `enterprise-server@${oldestSupportedGhes}`,
page: {},
allVersions,
enterpriseServerReleases,
}
await shortVersionsMiddleware(req, null, () => {})
const output = await liquid.parseAndRender(negativeVersionsTemplate, req.context)
expect(output.replace(/\s\s+/g, ' ').trim()).toBe(
`I am not GHAE I am not GHEC I am not GHES ${secondOldestSupportedGhes}`
)
})
test('The != operator works as expected', async () => {
req.context = {
currentVersion: `enterprise-server@${secondOldestSupportedGhes}`,
page: {},
allVersions,
enterpriseServerReleases,
}
await shortVersionsMiddleware(req, null, () => {})
const output = await liquid.parseAndRender(negativeVersionsTemplate, req.context)
expect(output.replace(/\s\s+/g, ' ').trim()).toBe('I am not GHAE I am not GHEC')
})
})
describe('feature versions', () => {
// Create a fake req so we can test the feature versions middleware
const req = { language: 'en', query: {} }
test('does not render in FPT because feature is not available in FPT', async () => {
req.context = {
currentVersion: 'free-pro-team@latest',
page: {},
allVersions,
enterpriseServerReleases,
}
await featureVersionsMiddleware(req, null, () => {})
const outputFpt = await liquid.parseAndRender(featureVersionsTemplate, req.context)
expect(outputFpt.includes('placeholder content')).toBe(false)
})
test('renders in GHES because feature is available in GHES', async () => {
req.context = {
currentVersion: `enterprise-server@${enterpriseServerReleases.latest}`,
page: {},
allVersions,
enterpriseServerReleases,
}
await featureVersionsMiddleware(req, null, () => {})
const outputFpt = await liquid.parseAndRender(featureVersionsTemplate, req.context)
expect(outputFpt.includes('placeholder content')).toBe(true)
})
test('renders in GHEC because feature is available in GHEC', async () => {
req.context = {
currentVersion: 'enterprise-cloud@latest',
page: {},
allVersions,
enterpriseServerReleases,
}
await featureVersionsMiddleware(req, null, () => {})
const outputFpt = await liquid.parseAndRender(featureVersionsTemplate, req.context)
expect(outputFpt.includes('placeholder content')).toBe(true)
})
test('renders in GHAE because feature is available in GHAE', async () => {
req.context = {
currentVersion: 'github-ae@latest',
page: {},
allVersions,
enterpriseServerReleases,
}
await featureVersionsMiddleware(req, null, () => {})
const outputFpt = await liquid.parseAndRender(featureVersionsTemplate, req.context)
expect(outputFpt.includes('placeholder content')).toBe(true)
})
})
})