Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add glob support for URLs #5824

Merged
merged 21 commits into from
Jun 29, 2024
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
6 changes: 6 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
"options": {
"trailingComma": "none"
}
},
{
"files": "**/src/GlobMatcher.test.ts",
"options": {
"printWidth": 180
}
}
]
}
9 changes: 8 additions & 1 deletion cspell.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,14 @@
"autoAttachChildProcesses": true,
"skipFiles": ["<node_internals>/**", "**/node_modules/**"],
"program": "${workspaceRoot:cspell-monorepo}/node_modules/vitest/vitest.mjs",
"args": ["run", "--test-timeout=600000", "${relativeFile}"],
"args": [
"run",
"--testTimeout=600000",
"--hideSkippedTests",
"--reporter=basic",
"--no-file-parallelism",
"${relativeFile}"
],
"cwd": "${fileWorkspaceFolder}",
"smartStep": true,
"console": "integratedTerminal"
Expand Down
2 changes: 2 additions & 0 deletions packages/cspell-gitignore/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@
"node": ">=18"
},
"dependencies": {
"@cspell/url": "workspace:*",
"cspell-glob": "workspace:*",
"cspell-io": "workspace:*",
"find-up-simple": "^1.0.0"
}
}
44 changes: 35 additions & 9 deletions packages/cspell-gitignore/src/GitIgnore.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
import * as path from 'node:path';
import { fileURLToPath } from 'node:url';

import { describe, expect, test } from 'vitest';

import { GitIgnore } from './GitIgnore.js';

const pkg = path.resolve(__dirname, '..');
const packages = path.resolve(pkg, '..');
const gitRoot = path.resolve(packages, '..');
const samples = path.resolve(pkg, 'samples');
const pkgCSpellLib = path.join(packages, 'cspell-lib');
const gitIgnoreFile = path.resolve(gitRoot, '.gitignore');
const dirUrl = new URL('.', import.meta.url);

const pkgUrl = new URL('../', dirUrl);
const packagesUrl = new URL('../', pkgUrl);
const gitRootUrl = new URL('../', packagesUrl);
const samplesUrl = new URL('samples/', pkgUrl);
const pkgCSpellLibUrl = new URL('cspell-lib/', packagesUrl);
const gitIgnoreFileUrl = new URL('.gitignore', gitRootUrl);

const pkg = fileURLToPath(pkgUrl);
const packages = fileURLToPath(packagesUrl);
const gitRoot = fileURLToPath(gitRootUrl);
const samples = fileURLToPath(samplesUrl);
const pkgCSpellLib = fileURLToPath(pkgCSpellLibUrl);
const gitIgnoreFile = fileURLToPath(gitIgnoreFileUrl);
// const pathSamples = path.resolve(pkg, 'samples');
// const gitIgnoreSamples = path.resolve(pathSamples, '.gitignore');

Expand Down Expand Up @@ -58,12 +68,23 @@ describe('GitIgnoreServer', () => {
file | roots | expected
${__filename} | ${undefined} | ${undefined}
${p(samples, 'ignored/keepme.md')} | ${undefined} | ${undefined}
${p(samples, 'ignored/file.txt')} | ${undefined} | ${{ glob: 'ignored/**', matched: true, line: 3, root: samples, gitIgnoreFile: p(samples, '.gitignore') }}
${p(pkg, 'node_modules/bin')} | ${undefined} | ${oc({ glob: 'node_modules/', matched: true, root: gitRoot, gitIgnoreFile: gitIgnoreFile })}
${p(samples, 'ignored/file.txt')} | ${undefined} | ${{ glob: 'ignored/**', matched: true, line: 3, root: pr(samples), gitIgnoreFile: p(samples, '.gitignore') }}
${p(pkg, 'node_modules/bin')} | ${undefined} | ${oc({ glob: 'node_modules/', matched: true, root: pr(gitRoot), gitIgnoreFile: gitIgnoreFile })}
${p(pkg, 'node_modules/')} | ${undefined} | ${oc({ glob: 'node_modules/', matched: true, root: pr(gitRoot), gitIgnoreFile: gitIgnoreFile })}
${__filename} | ${[p(samples, 'ignored')]} | ${undefined}
${p(samples, 'ignored/keepme.md')} | ${[p(samples, 'ignored')]} | ${undefined}
${p(samples, 'ignored/file.txt')} | ${[p(samples, 'ignored')]} | ${undefined}
${p(pkg, 'node_modules/bin')} | ${[p(samples, 'ignored')]} | ${oc({ glob: 'node_modules/', matched: true, root: gitRoot, gitIgnoreFile: gitIgnoreFile })}
${p(pkg, 'node_modules/bin')} | ${[p(samples, 'ignored')]} | ${oc({ glob: 'node_modules/', matched: true, root: pr(gitRoot), gitIgnoreFile: gitIgnoreFile })}
`('isIgnoredEx $file $roots', async ({ file, roots, expected }) => {
const dir = path.dirname(file);
const gs = new GitIgnore(roots);
const r = await gs.findGitIgnoreHierarchy(dir);
expect(r.isIgnoredEx(file)).toEqual(expected);
});

test.each`
file | roots | expected
${p(pkg, 'node_modules/')} | ${undefined} | ${oc({ glob: 'node_modules/', matched: true, root: pr(gitRoot), gitIgnoreFile: gitIgnoreFile })}
`('isIgnoredEx $file $roots', async ({ file, roots, expected }) => {
const dir = path.dirname(file);
const gs = new GitIgnore(roots);
Expand All @@ -77,6 +98,7 @@ describe('GitIgnoreServer', () => {
p(samples, 'ignored/keepme.md'),
p(samples, 'ignored/file.txt'),
p(pkg, 'node_modules/bin'),
p(pkg, 'node_modules/'),
];
const gs = new GitIgnore();
const r = await gs.filterOutIgnored(files);
Expand Down Expand Up @@ -118,4 +140,8 @@ describe('GitIgnoreServer', () => {
function p(dir: string, ...dirs: string[]) {
return path.join(dir, ...dirs);
}

function pr(...dirs: string[]) {
return path.join(path.resolve(...dirs), './');
}
});
4 changes: 3 additions & 1 deletion packages/cspell-gitignore/src/GitIgnoreFile.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as path from 'node:path';
import { fileURLToPath } from 'node:url';

import { GlobMatcher } from 'cspell-glob';
import { describe, expect, test } from 'vitest';
Expand All @@ -7,8 +8,9 @@ import { __testing__, GitIgnoreFile, GitIgnoreHierarchy, loadGitIgnore } from '.

const { mustBeHierarchical } = __testing__;

const __dirname = fileURLToPath(new URL('./', import.meta.url));
const pathPackage = path.resolve(__dirname, '..');
const pathRepo = path.resolve(pathPackage, '../..');
const pathRepo = path.join(path.resolve(pathPackage, '../..'), './');
const gitIgnoreFile = path.resolve(pathRepo, '.gitignore');

const oc = (obj: unknown) => expect.objectContaining(obj);
Expand Down
4 changes: 4 additions & 0 deletions packages/cspell-gitignore/src/__snapshots__/app.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ exports[`app > app.run [ '../node_modules' ] 1`] = `".gitignore:50:node_modules/

exports[`app > app.run [ '../node_modules' ] 2`] = `""`;

exports[`app > app.run [ '../node_modules/.bin/run.mjs' ] 1`] = `".gitignore:50:node_modules/ ../node_modules/.bin/run.mjs"`;

exports[`app > app.run [ '../node_modules/.bin/run.mjs' ] 2`] = `""`;

exports[`app > app.run [ '-r', '.' ] 1`] = `""`;

exports[`app > app.run [ '-r', '.' ] 2`] = `"Missing files"`;
Expand Down
1 change: 1 addition & 0 deletions packages/cspell-gitignore/src/app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ describe('app', () => {
params
${[path.basename(__dirname) + '/code.ts']}
${['../node_modules']}
${['../node_modules/.bin/run.mjs']}
${['-r', '.', 'dist']}
${['temp']}
${['-r', '.', 'temp']}
Expand Down
3 changes: 2 additions & 1 deletion packages/cspell-gitignore/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ export async function run(args: string[]): Promise<void> {
for (const file of files) {
const filename = path.relative(cwd, file);
const pFile = gi.isIgnoredEx(file);
const pDir = gi.isIgnoredEx(file + '/ ');
const pDir = gi.isIgnoredEx(file + '/');
const r = (await pFile) || (await pDir);
console.warn('%o', { pFile: await pFile, pDir: await pDir });
const gitignore = r?.gitIgnoreFile ? path.relative(repo, r.gitIgnoreFile) : '';
const line = r?.line || '';
const glob = r?.glob || '';
Expand Down
11 changes: 0 additions & 11 deletions packages/cspell-gitignore/tsconfig.esm.json

This file was deleted.

9 changes: 7 additions & 2 deletions packages/cspell-gitignore/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
{
"files": [],
"references": [{ "path": "./tsconfig.esm.json" }]
"extends": "../../tsconfig.esm.json",
"compilerOptions": {
"rootDir": "src",
"outDir": "dist",
"types": ["node"]
},
"include": ["src"]
}
17 changes: 11 additions & 6 deletions packages/cspell-glob/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
"license": "MIT",
"type": "module",
"sideEffects": false,
"types": "dist/esm/index.d.ts",
"module": "dist/esm/index.js",
"types": "dist/index.d.ts",
"module": "dist/index.js",
"exports": {
".": {
"import": "./dist/esm/index.js"
"import": "./dist/index.js"
}
},
"files": [
Expand All @@ -25,18 +25,22 @@
"!**/__mocks__",
"!**/test/**",
"!**/*.test.*",
"!**/perf/**",
"!**/*.perf.*",
"!**/*.spec.*",
"!**/*.map"
],
"scripts": {
"clean": "shx rm -rf dist temp coverage \"*.tsbuildInfo\"",
"build": "tsc -b . -f",
"build:esm": "tsc -p tsconfig.esm.json",
"build": "tsc -p .",
"clean-build": "pnpm run clean && pnpm run build",
"coverage": "vitest run --coverage",
"test:perf": "insight --file \"dist/perf/**/*.perf.{mjs,js}\" -t 1000",
"test:perf:ts": "insight --register ts-node/esm --file \"src/perf/**/*.perf.{mts,ts}\" -t 1000",
"test:perf:prof": "NODE_ENV=production node --cpu-prof ../../node_modules/perf-insight/bin.mjs --file \"dist/perf/**/*.perf.{mjs,js}\" -t 1000",
"test:watch": "vitest",
"test": "vitest run",
"watch": "tsc -b . -w -f"
"watch": "tsc -p . -w"
},
"repository": {
"type": "git",
Expand All @@ -50,6 +54,7 @@
"node": ">=18"
},
"dependencies": {
"@cspell/url": "workspace:*",
"micromatch": "^4.0.7"
},
"devDependencies": {
Expand Down
Loading