Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal-plugins/system/public/plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
"cmds": [
{
"type": "regex",
"match": "/^(?:~?\\/[^/\\n\\r\\f\\v]+)+\\/?$/",
"match": "/^(?:(?:~?\\/[^/\\n\\r\\f\\v]+)+\\/?|[A-Za-z]:\\\\(?:[^\\\\/:*?\"<>|\\n\\r\\f\\v]+\\\\?)*)$/",
"label": "前往文件夹",
"minLength": 2
}
Expand Down
74 changes: 74 additions & 0 deletions tests/main/systemPluginOpenFolderRegex.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { describe, expect, it } from 'vitest'
import { readFileSync } from 'fs'
import path from 'path'

interface PluginFeatureCommand {
type?: string
match?: string
}

interface PluginFeature {
code: string
cmds: Array<string | PluginFeatureCommand>
}

interface PluginConfig {
features: PluginFeature[]
}

function getOpenFolderRegex(): RegExp {
const pluginJsonPath = path.join(
process.cwd(),
'internal-plugins',
'system',
'public',
'plugin.json'
)
const pluginConfig = JSON.parse(readFileSync(pluginJsonPath, 'utf-8')) as PluginConfig
const feature = pluginConfig.features.find((item) => item.code === 'open-folder')
const regexCmd = feature?.cmds?.find(
(item): item is PluginFeatureCommand => typeof item !== 'string' && item.type === 'regex'
)
const regexString = regexCmd?.match
const match = typeof regexString === 'string' ? regexString.match(/^\/(.+)\/([gimuy]*)$/) : null

if (!match) {
throw new Error('open-folder regex command is missing or invalid')
}

return new RegExp(match[1], match[2])
}

describe('system plugin open-folder regex', () => {
const regex = getOpenFolderRegex()

it('matches Windows drive paths that use backslashes', () => {
expect(regex.test('C:\\Users\\Test\\Desktop')).toBe(true)
expect(regex.test('D:\\Work Projects\\ZTools')).toBe(true)
})

it('matches Unix-style folder paths', () => {
expect(regex.test('~/Downloads')).toBe(true)
expect(regex.test('/Users/test/Documents')).toBe(true)
})

it('does not match unsupported folder path forms', () => {
expect(regex.test('C:/Users/Test/Desktop')).toBe(false)
expect(regex.test('\\\\server\\share\\folder')).toBe(false)
expect(regex.test('%USERPROFILE%\\Desktop')).toBe(false)
})

it('does not match Windows paths that contain invalid path characters', () => {
expect(regex.test('C:\\Users\\bad*name')).toBe(false)
expect(regex.test('C:\\Users\\bad?name')).toBe(false)
expect(regex.test('C:\\Users\\bad"name')).toBe(false)
expect(regex.test('C:\\Users\\bad<name')).toBe(false)
expect(regex.test('C:\\Users\\bad>name')).toBe(false)
expect(regex.test('C:\\Users\\bad|name')).toBe(false)
expect(regex.test('C:\\Users\\bad:name')).toBe(false)
})

it('does not match urls', () => {
expect(regex.test('https://example.com')).toBe(false)
})
})