This repository has been archived by the owner on Jul 15, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils.js
236 lines (194 loc) · 7.47 KB
/
utils.js
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
import { test, withoutWhitespace } from './helpers'
import os from 'os'
import fs from 'fs'
import path from 'path'
import * as utils from '../src/production/Utils'
import http from 'http'
const nodeScriptRegExp = /\<get\>(.*?)\<\/get\>/s
const nodeScript = `
export default function (request, response) {
return { title: 'Hello, world!' }
}
`
const nodeScriptIncludingDataTags = `
<get>
${nodeScript}
</get>
`
const topOfPage = `
<!-- Test -->
`
const restOfPage = `
</get>
<script>
export let data
</script>
<h1>{data.title}</h1>
`
const source = `
${topOfPage}
${nodeScriptIncludingDataTags}
${restOfPage}
`
test('basepath', t => {
const relativePathToEmptyProject = 'tests/fixtures/emptyProject'
const absolutePathToEmptyProject = path.resolve(relativePathToEmptyProject)
const relativePathToProjectWithSrcFolder = 'tests/fixtures/emptyProjectWithSrcFolder'
const absolutePathToProjectWithSrcFolder = path.resolve(relativePathToProjectWithSrcFolder)
// Since these are empty folders, they will not be held in the
// git repository. So we ensure they exist and create them if they do not
// our test doesn’t fail erroneously.
if (!fs.existsSync(absolutePathToEmptyProject)) {
fs.mkdirSync(absolutePathToEmptyProject)
}
if (!fs.existsSync(absolutePathToProjectWithSrcFolder)) {
fs.mkdirSync(path.join(absolutePathToProjectWithSrcFolder, 'src'), {recursive: true})
}
const relativeNonExistentPath = 'this-path-does-not-exist'
const absoluteNonExistentPath = path.resolve(relativeNonExistentPath)
const defaultWorkingDirectory = '.'
const basePath = utils.setBasePath(defaultWorkingDirectory, relativePathToEmptyProject)
const basePathWithSourceFolder = utils.setBasePath(defaultWorkingDirectory, relativePathToProjectWithSrcFolder)
t.equal(basePath, absolutePathToEmptyProject, 'base path to empty project is correct')
t.equal(basePathWithSourceFolder, path.join(absolutePathToProjectWithSrcFolder, 'src'), 'base path to project with src folder is correct')
t.throws(() => utils.setBasePath(defaultWorkingDirectory, relativeNonExistentPath), error => { return error.message === `Basepath (${absoluteNonExistentPath}) does not exist`}, 'Non existent paths throw.')
// TODO: Also test non-default working directory.
// TODO: Also test that process.env.basePath is set.
t.end()
})
test ('configuration path', t => {
const expectedConfigurationPath = path.join(os.homedir(), '.small-tech.org', 'nodekit')
const actualConfigurationPath = utils.configurationPath()
t.equal(actualConfigurationPath, expectedConfigurationPath, 'configuration path is as expected')
t.ok(fs.existsSync(actualConfigurationPath), 'configuration path exists on file system')
t.end()
})
test ('privileged ports', async t => {
utils.ensurePrivilegedPortsAreDisabled()
// We only need to test the port 80 server since, if that can be created
// without running into privilege issues, so can the 443 server (as we know
// the lowest privileged port is set to < 80).
const port80Server = http.createServer(() => {})
await t.doesNotReject(async () => {
await new Promise ((resolve, reject) => {
const timeoutInterval = setTimeout(() => reject('timeout'), 500)
port80Server.on('error', error => {
reject(error)
})
port80Server.listen(80, () => {
clearInterval(timeoutInterval)
resolve()
})
})
})
port80Server.close()
t.end()
})
test ('class name from route', t => {
t.equal(
utils.classNameFromRoutePattern('/some_route/with/underscores-and-hyphens:and-a-parameter/or:two'), 'SomeRouteWithUnderscoresAndHyphensAndAParameterOrTwoPage'
)
t.equal(utils.classNameFromRoutePattern('/'), 'IndexPage')
t.end()
})
test ('extract', t => {
const { normalisedSource, extracted } = utils.extract(source, nodeScriptRegExp)
t.equal(withoutWhitespace(normalisedSource), withoutWhitespace(topOfPage + restOfPage))
t.equal(withoutWhitespace(extracted), withoutWhitespace(nodeScript))
t.end()
})
test ('parseSource', t => {
const { normalisedSource, nodeScript: extracted } = utils.parseSource(source, nodeScriptRegExp)
t.equal(withoutWhitespace(normalisedSource), withoutWhitespace(topOfPage + restOfPage))
t.equal(withoutWhitespace(extracted), withoutWhitespace(nodeScript))
t.end()
})
test ('routeFromFilePath', t => {
const basePath = process.env.basePath
const supportedExtensions = ['get', 'head', 'patch', 'options', 'connect', 'delete', 'trace', 'post', 'put', 'page', 'socket']
// Some manual tests of actual routes in the Domain app (https://github.com/small-tech/domain).
const expectations = [
[path.join(basePath, 'index_[property1]_and_[property2].page'), '/:property1/and/:property2'],
[path.join(basePath, 'index_with_[property1]_and_[property2].page'), '/with/:property1/and/:property2'],
[path.join(basePath, 'admin/index_[password].socket'), '/admin/:password'],
[path.join(basePath, 'domain/available_[domain].get'), '/domain/available/:domain'],
[path.join(basePath, 'private_[token]_[domain].get'), '/private/:token/:domain']
]
for (const supportedExtension of supportedExtensions) {
expectations.push([
path.join(basePath, 'a', 'route-with', `this_[property].${supportedExtension}`),
`/a/route-with/this/:property`
])
}
for (const expectation of expectations) {
t.equal(utils.routePatternFromFilePath(expectation[0]), expectation[1], expectation[0])
}
t.end()
})
test ('nodeKitAppPath', t => {
// Note: this doesn’t test the app path when run from the nodekit bundle or
// the nodekit launch script from source. It only tests it when run from this
// file in the unit tests.
t.equal(utils.nodekitAppPath, path.resolve('.') + '/')
t.end()
})
test ('loaderPaths', async t => {
const { nodekitAppPath, svelteExports } = await utils.loaderPaths()
t.ok(nodekitAppPath)
const expectedSvelteExports = JSON.stringify({
'./package.json': './package.json',
'.': {
types: './types/runtime/index.d.ts',
browser: { import: './index.mjs', require: './index.js' },
node: { import: './ssr.mjs', require: './ssr.js' },
import: './index.mjs',
require: './index.js'
},
'./compiler': {
types: './types/compiler/index.d.ts',
import: './compiler.mjs',
require: './compiler.js'
},
'./action': {
types: './types/runtime/action/index.d.ts'
},
'./animate': {
types: './types/runtime/animate/index.d.ts',
import: './animate/index.mjs',
require: './animate/index.js'
},
'./easing': {
types: './types/runtime/easing/index.d.ts',
import: './easing/index.mjs',
require: './easing/index.js'
},
'./internal': {
types: './types/runtime/internal/index.d.ts',
import: './internal/index.mjs',
require: './internal/index.js'
},
'./motion': {
types: './types/runtime/motion/index.d.ts',
import: './motion/index.mjs',
require: './motion/index.js'
},
'./register': { require: './register.js' },
'./store': {
types: './types/runtime/store/index.d.ts',
import: './store/index.mjs',
require: './store/index.js'
},
'./transition': {
types: './types/runtime/transition/index.d.ts',
import: './transition/index.mjs',
require: './transition/index.js'
},
'./ssr': {
types: './types/runtime/index.d.ts',
import: './ssr.mjs',
require: './ssr.js'
}
})
t.equal(JSON.stringify(svelteExports), expectedSvelteExports)
t.end()
})