Skip to content

Commit

Permalink
test: Add tests for issue #5034
Browse files Browse the repository at this point in the history
So far it is not possible to reproduce.
  • Loading branch information
Jason3S committed Dec 6, 2023
1 parent 8d481f3 commit a5aeed6
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import type { CSpellSettingsWithSourceTrace, CSpellUserSettings, ImportFileRef } from '@cspell/cspell-types';
import type { CSpellConfigFile } from 'cspell-config-lib';
import * as path from 'path';
import { pathToFileURL } from 'url';
import { beforeEach, describe, expect, test, vi } from 'vitest';
import { fileURLToPath, pathToFileURL } from 'url';
import { assert, beforeEach, describe, expect, test, vi } from 'vitest';

import {
pathPackageRoot,
pathPackageSamples,
pathPackageSamplesURL,
pathRepoRoot,
pathRepoTestFixtures,
pathRepoTestFixturesURL,
} from '../../../../test-util/test.locations.cjs';
import { logError, logWarning } from '../../../util/logger.js';
import { resolveFileWithURL, toFilePathOrHref, toFileUrl } from '../../../util/url.js';
Expand All @@ -29,7 +30,7 @@ import {
readRawSettings,
searchForConfig,
} from './defaultConfigLoader.js';
import { extractImportErrors } from './extractImportErrors.js';
import { extractImportErrors, extractImports } from './extractImportErrors.js';
import { readSettings } from './readSettings.js';
import { readSettingsFiles } from './readSettingsFiles.js';

Expand All @@ -41,7 +42,10 @@ const samplesDir = pathPackageSamples;
const samplesSrc = path.join(samplesDir, 'src');
const testFixtures = pathRepoTestFixtures;

const urlIssues = new URL('./issues/', pathRepoTestFixturesURL);

const oc = expect.objectContaining;
const sm = expect.stringMatching;

vi.mock('../../../util/logger');

Expand Down Expand Up @@ -410,6 +414,20 @@ describe('Validate search/load config files', () => {
expect(searchResult?.url.href).toEqual(expectedConfig.href);
});

test.each`
dir | expectedImports
${new URL('issue-5034/cspell.json', urlIssues).href} | ${[oc({ filename: sm(/cspell.config.yaml/) }), oc({ filename: sm(/cspell-ext.json/) }), oc({ filename: sm(/cspell.json/) })]}
`('Search and merge from $dir', async ({ dir, expectedImports }) => {
const loader = getDefaultConfigLoader();
const url = toFileUrl(dir);
const searchResult = await loader.searchForConfigFile(url);
assert(searchResult);
expect(searchResult.url.href).toEqual(url.href);
const settings = await loader.mergeConfigFileWithImports(searchResult);
expect(settings?.__importRef?.filename).toEqual(fileURLToPath(url));
expect(extractImports(settings)).toEqual(expectedImports);
});

test.each`
dir | expectedConfig | expectedImportErrors
${pathToFileURL(samplesSrc + '/').href} | ${cfg(s('.cspell.json'))} | ${[]}
Expand Down
19 changes: 19 additions & 0 deletions packages/cspell-lib/src/lib/util/resolveFile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ import * as path from 'path';
import { fileURLToPath, pathToFileURL } from 'url';
import { describe, expect, test } from 'vitest';

import { pathRepoTestFixturesURL } from '../../test-util/index.mjs';
import { __testing__, resolveFile } from './resolveFile.js';
import { toFilePathOrHref } from './url.js';

interface Config {
import: string[];
}

const issuesFolderURL = new URL('./issues/', pathRepoTestFixturesURL);

const defaultConfigFile = require.resolve('@cspell/cspell-bundled-dicts/cspell-default.json');
const defaultConfigLocation = path.dirname(defaultConfigFile);

Expand All @@ -26,6 +29,7 @@ const rr = {
};

const oc = expect.objectContaining;
const sm = expect.stringMatching;

const { isFileURL, tryUrl } = __testing__;

Expand Down Expand Up @@ -75,6 +79,21 @@ describe('Validate resolveFile', () => {
expect(r.found).toBe(found);
});

const urlIssue5034 = new URL('issue-5034/', issuesFolderURL);

test.each`
filename | relativeTo | expected | found
${'./nested/cspell.config.yaml'} | ${urlIssue5034.href} | ${sm(/nested\/cspell\.config\.yaml$/)} | ${true}
${'./nested/cspell.config.yaml'} | ${new URL('cspell.json', urlIssue5034).href} | ${sm(/nested\/cspell\.config\.yaml$/)} | ${true}
${'./nested/cspell.config.yaml'} | ${fileURLToPath(urlIssue5034)} | ${sm(/nested\/cspell\.config\.yaml$/)} | ${true}
${'./nested/node_modules/@cspell/dict-fr-fr/cspell-ext.json'} | ${urlIssue5034.href} | ${sm(/cspell-ext\.json$/)} | ${true}
`('resolveFile $filename rel $relativeTo', async ({ filename, relativeTo, expected, found }) => {
const r = resolveFile(filename, relativeTo);
// console.error('r %o', r);
expect(r.filename).toEqual(expected);
expect(r.found).toBe(found);
});

test.each`
url | expected
${'/User/home'} | ${false}
Expand Down
5 changes: 5 additions & 0 deletions test-fixtures/issues/issue-5034/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Read me

Related to [#5034](https://github.com/streetsidesoftware/cspell/issues/5034)

bon après-midi
6 changes: 6 additions & 0 deletions test-fixtures/issues/issue-5034/cspell.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"import": [
"./nested/cspell.config.yaml",
"./nested/node_modules/@cspell/dict-fr-fr/cspell-ext.json"
]
}
4 changes: 4 additions & 0 deletions test-fixtures/issues/issue-5034/nested/cspell.config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
name: Nested Config
version: "0.2"
words:
- cspell

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions test-fixtures/issues/issue-5034/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}

0 comments on commit a5aeed6

Please sign in to comment.