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
13 changes: 12 additions & 1 deletion packages/cspell-glob/src/GlobMatcher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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', () => {
Expand Down
4 changes: 4 additions & 0 deletions packages/cspell-url/src/FileUrlBuilder.mts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
/**
Expand Down Expand Up @@ -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());
}
Expand Down
4 changes: 4 additions & 0 deletions packages/cspell-url/src/FileUrlBuilder.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions packages/cspell-url/src/fileUrl.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down
1 change: 1 addition & 0 deletions packages/cspell/src/lint/LintRequest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 })}
Expand Down
2 changes: 1 addition & 1 deletion packages/cspell/src/lint/LintRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(a: T[] | undefined, b: T[] | undefined): T[] | undefined {
Expand Down
59 changes: 59 additions & 0 deletions packages/cspell/src/lint/lint.leadingSpace.test.ts
Original file line number Diff line number Diff line change
@@ -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<typeof expect.objectContaining>) => 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
8 changes: 8 additions & 0 deletions packages/cspell/src/util/fileHelper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]));
Expand Down Expand Up @@ -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:')}
Expand Down
2 changes: 1 addition & 1 deletion packages/cspell/src/util/fileHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ export async function readFileListFile(listFile: string): Promise<string[]> {
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;
Expand Down