diff --git a/packages/cspell-glob/src/GlobMatcher.test.ts b/packages/cspell-glob/src/GlobMatcher.test.ts index 86014e30e4b4..f6885f3e7851 100644 --- a/packages/cspell-glob/src/GlobMatcher.test.ts +++ b/packages/cspell-glob/src/GlobMatcher.test.ts @@ -6,7 +6,7 @@ import mm from 'micromatch'; import pp from 'picomatch'; import { describe, expect, test } from 'vitest'; -import { fileOrGlobToGlob, workaroundPicomatchBug } from './globHelper.ts'; +import { escapeGlobPath, fileOrGlobToGlob, workaroundPicomatchBug } from './globHelper.ts'; import type { GlobMatchOptions, MatcherMode } from './GlobMatcher.ts'; import { GlobMatcher } from './GlobMatcher.ts'; import type { GlobMatch, GlobPattern, GlobPatternNormalized, GlobPatternWithOptionalRoot, PathInterface } from './GlobMatcherTypes.ts'; @@ -487,6 +487,17 @@ describe('Validate GlobMatcher', () => { } [pathWin32, pathPosix].forEach(runTestOn); + + test.each` + glob | filename | expected + ${'b'} | ${'/r/b '} | ${false} + ${escapeGlobPath('b ')} | ${'/r/b '} | ${true} + ${'a/b'} | ${'/r/ a/b'} | ${false} + ${escapeGlobPath(' a/b')} | ${'/r/ a/b'} | ${true} + `('a name that starts or ends with a space: $glob $filename', ({ glob, filename, expected }) => { + const matcher = new GlobMatcher(glob, { root: '/r', nodePath: path.posix }); + expect(matcher.match(filename)).toBe(expected); + }); }); describe('Validate GlobMatcher excludeMode patternsNormalizedToRoot', () => { diff --git a/packages/cspell-url/src/FileUrlBuilder.mts b/packages/cspell-url/src/FileUrlBuilder.mts index 1f437cc30fb3..ad484c7124af 100644 --- a/packages/cspell-url/src/FileUrlBuilder.mts +++ b/packages/cspell-url/src/FileUrlBuilder.mts @@ -32,6 +32,9 @@ export const carriageReturnRegEx: RegExp = /\r/g; export const tabRegEx: RegExp = /\t/g; export const questionRegex: RegExp = /\?/g; export const hashRegex: RegExp = /#/g; +/** The URL parser strips C0 control characters and spaces from both ends of its input. */ +// eslint-disable-next-line no-control-regex +const edgeSpaceOrControlRegex = /^[\x00-\x20]+|[\x00-\x20]+$/g; export interface ParsedPath { /** @@ -134,6 +137,7 @@ export class FileUrlBuilder { filePath = this.encodePathChars(filePath); filePath = filePath.replaceAll(questionRegex, '%3F'); filePath = filePath.replaceAll(hashRegex, '%23'); + filePath = filePath.replaceAll(edgeSpaceOrControlRegex, (s) => encodeURIComponent(s)); const pathname = filePath.replaceAll('\\', '/'); return pathname.replace(isWindowsPathRegEx, (drive) => `/${drive}`.toUpperCase()); } diff --git a/packages/cspell-url/src/FileUrlBuilder.test.mts b/packages/cspell-url/src/FileUrlBuilder.test.mts index ce34b4f07f06..67127c5b46a3 100644 --- a/packages/cspell-url/src/FileUrlBuilder.test.mts +++ b/packages/cspell-url/src/FileUrlBuilder.test.mts @@ -54,6 +54,10 @@ describe('FileUrlBuilder', () => { ${'file://host/E$/user/test/project/'} | ${undefined} | ${Path.win32} | ${'file://host/E$/user/test/project/'} ${'../sibling'} | ${'file://host/E$/user/test/project/'} | ${Path.win32} | ${'file://host/E$/user/test/sibling'} ${fileURLToPath(import.meta.url)} | ${'file://host/E$/user/test/project/'} | ${Path.win32} | ${import.meta.url} + ${' notes/readme.txt'} | ${'file:///E:/user/project/'} | ${Path.win32} | ${'file:///E:/user/project/%20notes/readme.txt'} + ${' notes/readme.txt'} | ${'file:///user/project/'} | ${Path.posix} | ${'file:///user/project/%20notes/readme.txt'} + ${'notes/readme.txt '} | ${'file:///user/project/'} | ${Path.posix} | ${'file:///user/project/notes/readme.txt%20'} + ${'/user/project/readme.txt '} | ${'file:///'} | ${Path.posix} | ${'file:///user/project/readme.txt%20'} `('toFileURL $file $relativeTo', ({ file, relativeTo, path, expected }) => { const builder = new FileUrlBuilder({ path }); const url = builder.toFileURL(file, relativeTo); diff --git a/packages/cspell-url/src/fileUrl.test.mts b/packages/cspell-url/src/fileUrl.test.mts index 60b86c097635..96d15ec2ae4f 100644 --- a/packages/cspell-url/src/fileUrl.test.mts +++ b/packages/cspell-url/src/fileUrl.test.mts @@ -103,6 +103,10 @@ describe('util', () => { path | expected ${'path/to/file'} | ${'path/to/file'} ${'/some/path%.c'} | ${'/some/path%25.c'} + ${' notes/file'} | ${'%20notes/file'} + ${'notes/file '} | ${'notes/file%20'} + ${'my notes/file'} | ${'my notes/file'} + ${'\x01notes/'} | ${'%01notes/'} `('normalizePathForUrl $path', ({ path, expected }) => { expect(normalizeFilePathForUrl(path)).toEqual(expected); }); diff --git a/packages/cspell/src/lint/LintRequest.test.ts b/packages/cspell/src/lint/LintRequest.test.ts index c9e159d8c999..800ca64c142f 100644 --- a/packages/cspell/src/lint/LintRequest.test.ts +++ b/packages/cspell/src/lint/LintRequest.test.ts @@ -12,6 +12,7 @@ describe('LintRequest', () => { ${{ files: ['one\ntwo'] }} | ${oc({ fileGlobs: [], files: ['one', 'two'] })} ${{ file: ['one', 'two'], files: ['two'] }} | ${oc({ fileGlobs: [], files: ['one', 'two'] })} ${{ file: ['one', 'two'] }} | ${oc({ fileGlobs: [], files: ['one', 'two'] })} + ${{ files: [' one \r\n two\n'] }} | ${oc({ fileGlobs: [], files: [' one', ' two'] })} ${{ showContext: undefined }} | ${oc({ showContext: 0 })} ${{ showContext: true }} | ${oc({ showContext: 20 })} ${{ showContext: 3 }} | ${oc({ showContext: 3 })} diff --git a/packages/cspell/src/lint/LintRequest.ts b/packages/cspell/src/lint/LintRequest.ts index 02b0165e13db..48f0972f879e 100644 --- a/packages/cspell/src/lint/LintRequest.ts +++ b/packages/cspell/src/lint/LintRequest.ts @@ -70,7 +70,7 @@ export class LintRequest { function mergeFiles(a: string[] | undefined, b: string[] | undefined): string[] | undefined { const files = merge(a, b); if (!files) return undefined; - return [...new Set(files.flatMap((a) => a.split('\n').map((a) => a.trim())).filter((a) => !!a))]; + return [...new Set(files.flatMap((a) => a.split('\n').map((a) => a.trimEnd())).filter((a) => !!a))]; } function merge(a: T[] | undefined, b: T[] | undefined): T[] | undefined { diff --git a/packages/cspell/src/lint/lint.leadingSpace.test.ts b/packages/cspell/src/lint/lint.leadingSpace.test.ts new file mode 100644 index 000000000000..b0578c749294 --- /dev/null +++ b/packages/cspell/src/lint/lint.leadingSpace.test.ts @@ -0,0 +1,59 @@ +import { promises as fsp } from 'node:fs'; +import * as os from 'node:os'; +import * as path from 'node:path'; + +import { afterAll, beforeAll, describe, expect, test } from 'vitest'; + +import type { LinterCliOptions } from '../options.js'; +import { InMemoryReporter } from '../util/InMemoryReporter.js'; +import { runLint } from './lint.js'; +import { LintRequest } from './LintRequest.js'; + +const oc = (...params: Parameters) => expect.objectContaining(...params); + +describe('lint a folder whose name starts with a space', () => { + let tmp = ''; + let config = ''; + + beforeAll(async () => { + tmp = await fsp.mkdtemp(path.join(os.tmpdir(), 'cspell-lint-leading-space-')); + await fsp.mkdir(path.join(tmp, ' notes')); + await fsp.writeFile(path.join(tmp, ' notes/readme.txt'), 'zqxnotes\n'); + await fsp.writeFile(path.join(tmp, 'list.txt'), ' notes/readme.txt\n'); + config = path.join(tmp, 'cspell.json'); + await fsp.writeFile(config, '{"version": "0.2", "ignorePaths": ["list.txt"]}\n'); + }); + + afterAll(async () => { + await fsp.rm(tmp, { recursive: true, force: true }); + }); + + async function lint(globs: string[], options: LinterCliOptions) { + const reporter = new InMemoryReporter(); + const result = await runLint(new LintRequest(globs, { root: tmp, config, ...options }, reporter)); + const words = reporter.issues.map((i) => i.text); + return { result, words, reporter }; + } + + test.each` + globs | options + ${['**/readme.txt']} | ${{}} + ${['./ notes/readme.txt']} | ${{}} + ${['[ ]notes/readme.txt']} | ${{}} + ${[]} | ${{ files: [' notes/readme.txt'] }} + `('lint $globs $options', async ({ globs, options }) => { + const r = await lint(globs, options); + expect(r.reporter.errors).toEqual([]); + expect(r.words).toEqual(['zqxnotes']); + expect(r.result).toEqual(oc({ errors: 0, files: 1 })); + }); + + test('lint a file list', async () => { + const r = await lint([], { fileList: [path.join(tmp, 'list.txt')] }); + expect(r.reporter.errors).toEqual([]); + expect(r.words).toEqual(['zqxnotes']); + expect(r.result).toEqual(oc({ errors: 0, files: 1 })); + }); +}); + +// cspell:ignore zqxnotes diff --git a/packages/cspell/src/util/fileHelper.test.ts b/packages/cspell/src/util/fileHelper.test.ts index e2078f5a1ecc..1ab8525190d4 100644 --- a/packages/cspell/src/util/fileHelper.test.ts +++ b/packages/cspell/src/util/fileHelper.test.ts @@ -51,6 +51,13 @@ describe('fileHelper', () => { } }); + test('readFileListFile keeps leading spaces', async () => { + const mockStreamConsumersText = vi.mocked(streamConsumers.text); + mockStreamConsumersText.mockImplementation(async () => ' notes/readme.txt \r\n\r\nb\r\n'); + const r = await readFileListFile('stdin'); + expect(r).toEqual([path.resolve(' notes/readme.txt'), path.resolve('b')]); + }); + test('readFileListFiles', async () => { const files = ['file1', '../file2', 'dir/file3', 'nested/file2.txt']; const r = await asyncIterableToArray(readFileListFiles([fileListFile, fileListFile2])); @@ -119,6 +126,7 @@ describe('fileHelper', () => { ${__dirname} | ${undefined} | ${__dirname} ${'not_found'} | ${__dirname} | ${path.join(__dirname, 'not_found')} ${'not_found'} | ${undefined} | ${path.resolve('not_found')} + ${' notes/readme.txt'} | ${__dirname} | ${path.join(__dirname, ' notes/readme.txt')} ${'stdin'} | ${undefined} | ${'stdin://'} ${'file://file.txt'} | ${undefined} | ${path.resolve('file.txt')} ${'stdin://source.ts'} | ${undefined} | ${pathToFileURL('source.ts').href.replace(/^file:/, 'stdin:')} diff --git a/packages/cspell/src/util/fileHelper.ts b/packages/cspell/src/util/fileHelper.ts index 893a526816f4..a832e8ae826a 100644 --- a/packages/cspell/src/util/fileHelper.ts +++ b/packages/cspell/src/util/fileHelper.ts @@ -177,7 +177,7 @@ export async function readFileListFile(listFile: string): Promise { const content = await readFile(listFile); const lines = content .split('\n') - .map((a) => a.trim()) + .map((a) => a.trimEnd()) .filter((a) => !!a) .map((file) => path.resolve(relTo, file)); return lines;