subpackage-boundaries.test.mjs
2.99 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
import { afterEach, describe, it } from 'node:test'
import assert from 'node:assert/strict'
import { mkdtemp, mkdir, rm, writeFile } from 'node:fs/promises'
import os from 'node:os'
import path from 'node:path'
import { checkSubpackageBoundaries } from '../../scripts/check-subpackage-boundaries.mjs'
const tempRoots = []
afterEach(async () => {
await Promise.all(tempRoots.splice(0).map(root => rm(root, { recursive: true, force: true })))
})
async function createFixture(files) {
const root = await mkdtemp(path.join(os.tmpdir(), 'subpackage-boundaries-'))
tempRoots.push(root)
for (const [relativePath, content] of Object.entries(files)) {
const absolutePath = path.join(root, relativePath)
await mkdir(path.dirname(absolutePath), { recursive: true })
await writeFile(absolutePath, content)
}
return root
}
const appConfig = {
pages: ['pages/index/index'],
subpackages: [
{ root: 'packageA', pages: ['pages/index/index'] },
{ root: 'packageB', pages: ['pages/index/index'] }
]
}
describe('subpackage static dependency boundaries', () => {
it('detects sibling-package and main-to-subpackage JavaScript imports', async () => {
const root = await createFixture({
// 分段生成故意断裂的测试源码,避免仓库级旧 import 扫描器把夹具本身计入结果。
'pages/index/index.js': ["import x from '", "../../packageA/shared.js'\n"].join(''),
'packageA/pages/index/index.js': ["import x from '", "../../../packageB/shared.js'\n"].join(''),
'packageA/shared.js': 'export default 1\n',
'packageB/shared.js': 'export default 2\n'
})
const issues = await checkSubpackageBoundaries(root, appConfig, {
roots: ['__main__', 'packageA']
})
assert.deepEqual(
issues.map(issue => [issue.importerPackage, issue.targetPackage, issue.kind]),
[
['packageA', 'packageB', 'subpackage-to-subpackage'],
['__main__', 'packageA', 'main-to-subpackage']
]
)
})
it('checks usingComponents, WXML/WXS and style imports while allowing main resources', async () => {
const root = await createFixture({
'utils/main.js': 'export default 1\n',
'packageA/pages/index/index.js': "import main from '~/utils/main.js'\n",
'packageA/pages/index/index.json': JSON.stringify({
usingComponents: { forbidden: '/packageB/components/item/item' }
}),
'packageA/pages/index/index.wxml': '<include src="/packageB/template/item.wxml"/><wxs src="/packageB/wxs/tool.wxs" module="tool"/>',
'packageA/pages/index/index.wxss': '@import "/packageB/styles/base.wxss";',
'packageB/components/item/item.js': 'Component({})\n',
'packageB/template/item.wxml': '<view/>',
'packageB/wxs/tool.wxs': 'module.exports = {}',
'packageB/styles/base.wxss': '.x{}'
})
const issues = await checkSubpackageBoundaries(root, appConfig, {
roots: ['packageA']
})
assert.equal(issues.length, 4)
assert.deepEqual(new Set(issues.map(issue => issue.kind)), new Set([
'subpackage-to-subpackage'
]))
assert.equal(issues.some(issue => issue.specifier === '~/utils/main.js'), false)
})
})