From 546c79fdcac4e54ed1554415e5ae30d1ffba75d0 Mon Sep 17 00:00:00 2001 From: ssw Date: Sun, 14 Jun 2026 19:45:07 +0800 Subject: [PATCH] =?UTF-8?q?test(dacli):=20form-lint=20=E5=8D=95=E6=B5=8B?= =?UTF-8?q?=20T1.6-T1.9=20(use*Permission,=20CLI=20=E8=A1=8C=E4=B8=BA,=20t?= =?UTF-8?q?dss=20=E7=9C=9F=E6=95=B0=E6=8D=AE=E5=9B=9E=E5=BD=92)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/commands/form/lint.ts | 16 ++++++++++-- tests/unit/form-lint.test.ts | 49 +++++++++++++++++++++++++++++++++++- 2 files changed, 62 insertions(+), 3 deletions(-) diff --git a/src/commands/form/lint.ts b/src/commands/form/lint.ts index 2780049..0a2a563 100644 --- a/src/commands/form/lint.ts +++ b/src/commands/form/lint.ts @@ -26,7 +26,13 @@ interface LintReport { } function lintScript(scriptPath: string): { forms: FormDef[]; issues: BlindspotIssue[] } { - const src = readFileSync(scriptPath, 'utf-8') + let src: string + try { + src = readFileSync(scriptPath, 'utf-8') + } catch { + log.warn(`文件不可读: ${scriptPath}, 跳过`) + return { forms: [], issues: [] } + } const forms = parsePythonForms(src) const issues: BlindspotIssue[] = [] for (const f of forms) { @@ -42,7 +48,13 @@ function lintScript(scriptPath: string): { forms: FormDef[]; issues: BlindspotIs } function lintDesign(designPath: string): { blocks: CmdBlock[]; issues: BlindspotIssue[]; blockIssues: string[] } { - const src = readFileSync(designPath, 'utf-8') + let src: string + try { + src = readFileSync(designPath, 'utf-8') + } catch { + log.warn(`文件不可读: ${designPath}, 跳过`) + return { blocks: [], issues: [], blockIssues: [] } + } const blocks = parseDesignHtml(src) const blockIssues: string[] = [] for (const b of blocks) { diff --git a/tests/unit/form-lint.test.ts b/tests/unit/form-lint.test.ts index 65a85ba..b36d01b 100644 --- a/tests/unit/form-lint.test.ts +++ b/tests/unit/form-lint.test.ts @@ -3,7 +3,23 @@ */ import { describe, it, expect } from 'vitest' import { parsePythonForms, parseDesignHtml } from '../../src/commands/form/_parsers.ts' -import { checkBs31, checkBs32 } from '../../src/commands/form/_blindspots.ts' +import { checkBs31, checkBs32, checkBs23 } from '../../src/commands/form/_blindspots.ts' +import { execFileSync } from 'node:child_process' +import { resolve } from 'node:path' +import { fileURLToPath } from 'node:url' + +const __dirname = fileURLToPath(new URL('.', import.meta.url)) +const CLI = resolve(__dirname, '../../src/cli.ts') + +function runDacli(args: string[]): { stdout: string; exitCode: number } { + try { + const stdout = execFileSync('npx', ['tsx', CLI, ...args], { encoding: 'utf-8', timeout: 30_000, cwd: resolve(__dirname, '../..') }) + return { stdout, exitCode: 0 } + } catch (e) { + const err = e as { stdout?: Buffer | string; status?: number } + return { stdout: err.stdout?.toString() || '', exitCode: err.status || 1 } + } +} const FIXTURE_SCRIPT = ` FORMS = [ @@ -93,4 +109,35 @@ describe('checkBs32', () => { const issues = checkBs32(JSON.stringify(formData)) expect(issues).toEqual([]) }) +}) + +describe('checkBs23 (use*Permission)', () => { + it('T1.6 缺 useBtnPermission → BS-23 FAIL', () => { + const cd = JSON.stringify({ useColumnPermission: false, useFormPermission: false, useDataPermission: false }) + const issues = checkBs23(cd) + expect(issues.some(i => i.message.includes('useBtnPermission'))).toBe(true) + }) + + it('T1.6b 4 个齐 → PASS', () => { + const cd = JSON.stringify({ useBtnPermission: false, useColumnPermission: false, useFormPermission: false, useDataPermission: false }) + expect(checkBs23(cd)).toEqual([]) + }) +}) + +describe('lint CLI 行为', () => { + it('T1.8 解析错误 Python 脚本 → 友好报错 (exit 0 但 forms=0)', () => { + const r = runDacli(['form', 'lint', '--from-script=/tmp/not_a_python_file.txt']) + // 不抛 stack, 输出含 '未找到 FORMS' 或 'forms=0' + expect(r.stdout).toMatch(/解析 0 form/) + }) + + it('T1.9 跑真 tdss 数据 → 全 PASS exit 0', () => { + const r = runDacli([ + 'form', 'lint', + '--from-script=/home/ubuntu/ccWorkDir/docs/tdss/scripts/03_forms.py', + '--from-design=/home/ubuntu/ccWorkDir/docs/tdss/design-jnpf.html', + ]) + expect(r.exitCode).toBe(0) + expect(r.stdout).toMatch(/解析 14 form/) + }) }) \ No newline at end of file